Phase 1 Completion

This commit is contained in:
Chinmay Pandhare
2017-06-14 08:20:01 +05:30
parent 95e066fdc7
commit ec2790652c
13 changed files with 344 additions and 352 deletions

View File

@@ -1,8 +1,4 @@
console.log('\x1b[31m%s\x1b[0m',"This is the output of the module");
require('./src/ImageSequencerNode');
sequencer = ImageSequencer();
sequencer.loadImage('sundar','examples/SundarPichai.jpeg',function(){
sequencer.loadImage('timetable','examples/test.png',function(){
sequencer.addSteps('do-nothing-pix');
});
});
sequencer.loadImages({red:'../../red.jpg'});

1
output.txt Normal file

File diff suppressed because one or more lines are too long

View File

@@ -1,132 +1,314 @@
if (typeof window !== 'undefined') window.$ = window.jQuery = require('jquery');
if (typeof window !== 'undefined') {window.$ = window.jQuery = require('jquery'); isBrowser = true}
else {window = global; var isBrowser = false}
ImageSequencer = function ImageSequencer(options) {
options = options || {};
options.inBrowser = options.inBrowser || isBrowser;
if (options.inBrowser) options.ui = options.ui || require('./UserInterface');
// if (options.inBrowser) options.ui = options.ui || require('./UserInterface');
options.sequencerCounter = 0;
function CImage(src) {
datauri = (options.inBrowser)?(src):require('urify')(src);
image = {
src: datauri,
format: datauri.split(':')[1].split(';')[0].split('/')[1]
}
return image;
}
function objTypeOf(object){
return Object.prototype.toString.call(object).split(" ")[1].slice(0,-1)
}
function log(color,msg) {
if(options.ui!="none") {
if(arguments.length==1) console.log(arguments[0]);
else if(arguments.length==2) console.log(color,msg);
}
}
var image,
steps = [],
modules = require('./Modules'),
images = [];
modules = require('./ModulesNode'),
images = {};
// if in browser, prompt for an image
if (options.imageSelect || options.inBrowser) addStep('image-select');
else if (options.imageUrl) loadImage(imageUrl);
// if (options.imageSelect || options.inBrowser) addStep('image-select');
// else if (options.imageUrl) loadImage(imageUrl);
// soon, detect local or URL?
function addStep(name, o) {
console.log('adding step "' + name + '"');
if (typeof(window) != "undefined")
for(var variable in window)
if(window[variable] == this)
options.instanceName = variable;
function addStep(image, name, o) {
log('\x1b[36m%s\x1b[0m','adding step \"' + name + '\" to \"' + image + '\".');
o = o || {};
o.id = options.sequencerCounter++; //Gives a Unique ID to each step
o.name = o.name || name;
o.selector = o.selector || 'ismod-' + name;
o.container = o.container || options.selector;
o.image = image;
var module = modules[name](o);
steps.push(module);
images[image].steps.push(module);
function defaultSetupModule() {
if (options.ui) module.options.ui = options.ui({
if (options.ui && options.ui!="none") module.options.ui = options.ui({
selector: o.selector,
title: module.options.title,
id: o.id,
instanceName: options.instanceName
id: o.id
});
}
if (module.hasOwnProperty('setup')) module.setup(); // add a default UI, unless the module has one specified
else defaultSetupModule.apply(module); // run default setup() in scope of module (is this right?)
if (name === "image-select") {
// tell the UI that a step has been added.
module.setup(); // just set up initial ImageSelect; it has own UI
return true;
}
} else {
// add a default UI, unless the module has one specified
if (module.hasOwnProperty('setup')) module.setup();
else {
defaultSetupModule.apply(module); // run default setup() in scope of module (is this right?)
function addSteps(){
argtype = [];
json_q = {};
for (i in images) {
lastimage = i;
}
var previousStep = steps[steps.length - 2];
if (previousStep) {
// connect output of last step to input of this step
previousStep.options.output = function output(image) {
if (sequencer.steps[0].options.initialImage) {
options.initialImage = sequencer.steps[0].options.initialImage;
for (var arg in arguments) {
argtype.push(objTypeOf(arguments[arg]));
}
if (arguments.length == 1) {
if(argtype[0] == "Object")
json_q = arguments[0];
else
for (i in images)
json_q[i] = (argtype[0]=="Array")?arguments[0]:[arguments[0]];
}
else if (arguments.length == 2) {
if(argtype[1]=="String") arguments[1] = [arguments[1]];
if(argtype[0]=="String")
json_q[arguments[0]] = arguments[1];
else if(argtype[0]=="Array")
for (var i in arguments[0]) {
json_q[arguments[0][i]] = arguments[1];
}
log('running module "' + name + '"');
// display the image in any available ui
if (previousStep.options.ui && previousStep.options.ui.display) previousStep.options.ui.display(image);
module.draw(image);
}
for (i in json_q)
for (j in json_q[i])
addStep.call(this,i,json_q[i][j]);
}
function removeStep(image,index) {
//remove the step from images[image].steps and redraw remaining images
if(index>0) {
log('\x1b[31m%s\x1b[0m',"Removing "+index+" from "+image);
images[image].steps.splice(index,1);
}
//tell the UI a step has been removed
}
function removeSteps(image,index) {
run = {};
this_ = this;
if(arguments.length==2) {
removeStep(image,index);
run[image] = index;
}
else if(arguments.length==1) {
if (typeof(arguments[0])=="number" || objTypeOf(arguments[0])=="Array") {
indx = arguments[0];
arguments[0] = {};
for (img in this_.images) arguments[0][img] = indx;
}
if (objTypeOf(arguments[0])=='Object') {
for (img in arguments[0]) {
var indexes = arguments[0][img];
if (typeof(indexes) == "number")
{removeStep(img,indexes); run[img]=indexes;}
else if (objTypeOf(indexes) == "Array") {
indexes = indexes.sort(function(a,b){return b-a});
run[img] = indexes[indexes.length-1];
for (i in indexes)
removeStep(img,indexes[i]);
}
}
} // end if argument is object
}
this.run(run)
}
function insertStep(image, index, name, o) {
log('\x1b[36m%s\x1b[0m','inserting step \"' + name + '\" to \"' + image + '\" at \"'+index+'\".');
o = o || {};
o.id = options.sequencerCounter++; //Gives a Unique ID to each step
o.name = o.name || name;
o.selector = o.selector || 'ismod-' + name;
o.container = o.container || options.selector;
o.image = image;
var module = modules[name](o);
images[image].steps.splice(index, 0, module);
function defaultSetupModule() {
if (options.ui && options.ui!="none") module.options.ui = options.ui({
selector: o.selector,
title: module.options.title,
id: o.id
});
}
if (module.hasOwnProperty('setup')) module.setup(); // add a default UI, unless the module has one specified
else defaultSetupModule.apply(module); // run default setup() in scope of module (is this right?)
// tell the UI that a step has been inserted.
return true;
}
function insertSteps(image, index, name, o) {
run = {};
this_ = this;
if(arguments.length==4 || arguments.length==3) {
o = o || {};
size = this_.images[image].steps.length;
index = (index==size)?index:index%size;
if (index<0) index += size+1;
insertStep(image,index,name, o);
run[image] = index;
}
else if(arguments.length==1) {
if (objTypeOf(arguments[0])=='Object') {
for (img in arguments[0]) {
var details = arguments[0][img];
if (objTypeOf(details) == "Object")
{insertStep(img,details.index,details.name,details.o); run[img]=details.index;}
else if (objTypeOf(details) == "Array") {
details = details.sort(function(a,b){return b.index-a.index});
run[img] = details[details.length-1].index;
for (i in details)
insertStep(img,details[i].index,details[i].name,details[i].o);
}
}
} // end if argument is object
}
this.run(run)
}
function run(t_image,t_from) {
log('\x1b[31m%s\x1b[0m',"Running the Sequencer!");
this_ = this;
runimg = {};
json_q = {};
for (image in images) {
runimg[image] = 0;
}
function drawStep(drawarray,pos) {
if(pos>=drawarray.length) return true;
image = drawarray[pos].image;
i = drawarray[pos].i;
images[image].steps[i].draw.call(this_,function(){
drawStep(drawarray,++pos);
});
}
function drawSteps(json_q) {
drawarray = [];
for (image in json_q) {
no_steps = images[image].steps.length;
init = json_q[image];
for(i = 0; i < no_steps-init; i++) {
drawarray.push({image: image,i: init+i});
}
}
drawStep(drawarray,0);
}
// Pre-set the initial output behavior of the final step,
// which will be changed if an additional step is added.
module.options.output = function output(image) {
if (module.options.ui && module.options.ui.display) module.options.ui.display(image);
function filter(json_q){
for (image in json_q) {
prevstep = images[image].steps[json_q[image]-1];
while (typeof(prevstep) == "undefined" || typeof(prevstep.output) == "undefined") {
prevstep = images[image].steps[(--json_q[image]) - 1];
}
}
return json_q;
}
if (arguments.length == 0) {
for (image in images)
json_q[image] = 1;
}
else if (arguments.length == 1) {
if (typeof(arguments[0]) == "string")
json_q[arguments[0]] = 1;
else if (typeof(arguments[0]) == "number")
for (image in images)
json_q[image] = arguments[0];
else if (objTypeOf(arguments[0]) == "Array")
for (image in arguments[0])
json_q[arguments[0][image]] = 1;
else if (objTypeOf(arguments[0]) == "Object")
json_q = arguments[0];
}
else if (arguments.length == 2) {
json_q[t_image] = t_from;
}
json_q = filter(json_q);
drawSteps(json_q);
}
function removeStep (id) {
for (i=0;i<steps.length;i++) {
if (steps[i].options.id == id && steps[i].options.name != 'image-select'){
console.log('removing step "'+steps[i].options.name+'"');
if (options.inBrowser) steps[i].options.ui.remove();
steps.splice(i,1);
run(options.initialImage);
function loadImage(name, src, callback) {
image = {
src: src,
steps: [{
options: {
id: options.sequencerCounter++,
name: "load-image",
title: "Load Image"
},
draw: function() {
if(arguments.length==1){
this.outputData = CImage(arguments[0]);
return true;
}
return false;
},
output: CImage(src)
}]
};
images[name] = image;
if (callback) callback();
}
function loadImages() {
if (arguments.length == 1) {
for (image in arguments[0])
loadImage(image,arguments[0][image]);
}
else if (arguments.length == 2) {
if (objTypeOf(arguments[1]) == "Function") {
for (image in arguments[0]) {
loadImage(image,arguments[0][image])
}
arguments[1]();
}
else {
loadImage(arguments[0],arguments[1])
}
}
}
// passed image is optional but you can pass a
// non-stored image through the whole steps chain
function run(image) {
if (image) steps[1].draw(image);
else steps[0].draw();
}
function log(msg) {
// $('.log').append(msg + ' at ' + new Date());
console.log(msg);
}
// load default starting image
// i.e. from parameter
// this could send the image to ImageSelect, or something?
function loadImage(src, callback) {
image = new Image();
image.onload = function() {
run(image);
if (callback) callback(image);
options.initialImage = image;
else if (arguments.length == 3) {
loadImage(arguments[0],arguments[1],arguments[2])
}
image.src = src;
}
return {
options: options,
loadImage: loadImage,
images: images,
addStep: addStep,
removeStep: removeStep,
loadImages: loadImages,
addSteps: addSteps,
removeSteps: removeSteps,
insertSteps: insertSteps,
run: run,
modules: modules,
steps: steps,
images: images,
ui: options.ui
}

View File

@@ -1,173 +0,0 @@
if (typeof window !== 'undefined') {window.$ = window.jQuery = require('jquery'); isBrowser = true}
else {window = global; var isBrowser = false}
ImageSequencer = function ImageSequencer(options) {
options = options || {};
options.inBrowser = options.inBrowser || isBrowser;
// if (options.inBrowser) options.ui = options.ui || require('./UserInterface');
options.sequencerCounter = 0;
function CImage(src) {
datauri = (options.inBrowser)?(src):require('urify')(src);
image = {
src: datauri,
mimeType: datauri.split(':')[1].split(';')[0]
}
return image;
}
var image,
steps = [],
modules = require('./ModulesNode'),
images = {};
// if in browser, prompt for an image
// if (options.imageSelect || options.inBrowser) addStep('image-select');
// else if (options.imageUrl) loadImage(imageUrl);
// soon, detect local or URL?
function addStep(image, name, o) {
console.log('\x1b[36m%s\x1b[0m','adding step \"' + name + '\" to \"' + image + '\".');
o = o || {};
o.id = options.sequencerCounter++; //Gives a Unique ID to each step
o.name = o.name || name;
o.selector = o.selector || 'ismod-' + name;
o.container = o.container || options.selector;
o.image = image;
var module = modules[name](o);
images[image].steps.push(module);
function defaultSetupModule() {
if (options.ui) module.options.ui = options.ui({
selector: o.selector,
title: module.options.title,
id: o.id
});
}
if (module.hasOwnProperty('setup')) module.setup(); // add a default UI, unless the module has one specified
else defaultSetupModule.apply(module); // run default setup() in scope of module (is this right?)
// run the draw method.
module.draw.call(this);
// tell the UI that a step has been added.
return true;
}
function objTypeOf(object){
return Object.prototype.toString.call(object).split(" ")[1].slice(0,-1)
}
function addSteps(){
argtype = [];
json_q = {};
for (i in images) {
lastimage = i;
}
for (var arg in arguments) {
argtype.push(objTypeOf(arguments[arg]));
}
if (arguments.length == 1) {
if(argtype[0] == "Object")
json_q = arguments[0];
else
for (i in images)
json_q[i] = [arguments[0]];
}
else if (arguments.length == 2) {
if(argtype[1]=="String") arguments[1] = [arguments[1]];
if(argtype[0]=="String")
json_q[arguments[0]] = arguments[1];
else if(argtype[0]=="Array")
for (var i in arguments[0]) {
json_q[arguments[0][i]] = arguments[1];
}
}
for (i in json_q)
for (j in json_q[i])
addStep.call(this,i,json_q[i][j]);
}
function removeStep(image,index) {
for (i=0;i<steps.length;i++) {
if (steps[i].options.id == id && steps[i].options.name != 'image-select'){
console.log('\x1b[36m%s\x1b[0m','removing step "'+steps[i].options.name+'"');
// if (options.inBrowser) steps[i].options.ui.remove();
steps.splice(i,1);
if (steps.length != 0)
run(options.initialImage);
}
}
return "Removed.";
}
function removeSteps() {
if(arguments.length==1) {
}
}
function run() {
if (arguments.length == 0)
for (image in images) {
for (i in images[image].steps)
images[image].steps[i].draw.call(this);
}
else if (objTypeOf[arguments[0]]=="Array")
for (image in arguments[0]) {
for (i in images[image].steps)
images[image].steps[i].draw.call(this);
}
else if (objTypeOf(arguments[0])=="String" && (image = arguments[0])) {
for (i in images[image].steps)
images[image].steps[i].draw.call(this);
}
}
function log(msg) {
console.log(msg);
}
function loadImage(name, src, callback) {
image = {
src: src,
steps: [{
options: {
id: options.sequencerCounter++,
name: "load-image",
title: "Load Image"
},
draw: function() {
if(arguments.length==1){
this.outputData = CImage(arguments[0]);
return true;
}
return false;
},
output: CImage(src)
}]
};
images[name] = image;
if (callback) callback();
}
return {
options: options,
loadImage: loadImage,
addSteps: addSteps,
removeSteps: removeSteps,
run: run,
modules: modules,
steps: steps,
images: images,
ui: options.ui
}
}
module.exports = ImageSequencer;

View File

@@ -2,11 +2,8 @@
* Core modules
*/
module.exports = {
'image-select': require('./modules/ImageSelect'),
'do-nothing': require('./modules/DoNothing'),
'green-channel': require('./modules/GreenChannel'),
'ndvi-red': require('./modules/NdviRed'),
'plot': require('./modules/Plot'),
'image-threshold': require('./modules/ImageThreshold'),
'crop': require('./modules/Crop')
'do-nothing-pix': require('./modules/DoNothingPix')
}

View File

@@ -1,9 +0,0 @@
/*
* Core modules
*/
module.exports = {
'do-nothing': require('./modules/DoNothing'),
'green-channel': require('./modules/GreenChannel'),
'ndvi-red': require('./modules/NdviRed'),
'do-nothing-pix': require('./modules/DoNothingPix')
}

View File

@@ -8,14 +8,12 @@ module.exports = function DoNothing(options) {
var image;
var output;
function draw() {
thisimage = this.images[options.image];
for (i in thisimage.steps){
if (thisimage.steps[i].options.id == options.id) pos = i;
}
olddata = thisimage.steps[i-1].output;
var newdata = JSON.parse(JSON.stringify(olddata));
thisimage.steps[i].output = {src:newdata.src,mimeType:newdata.mimeType};
function draw(callback) {
step = require('./_Step')(this,options);
newdata = step[0];
pos = step[1];
thisimage.steps[pos].output = {src:newdata.src,format:newdata.format};
callback();
}
function get() {

View File

@@ -9,26 +9,22 @@ module.exports = function GreenChannel(options) {
//function setup() {} // optional
function draw() {
images = this.images;
thisimage = images[options.image];
for (i in thisimage.steps){
if (thisimage.steps[i].options.id == options.id) pos = i;
}
olddata = thisimage.steps[i-1].output;
var newdata = JSON.parse(JSON.stringify(olddata));
function draw(callback) {
step = require('./_Step')(this,options);
newdata = step[0];
pos = step[1];
function changePixel(r, g, b, a) {
return [r, g, b, a];
}
function output(image,datauri,mimetype){
images[image].steps[i].output = {src:datauri,mimeType:mimetype}
images[image].steps[pos].output = {src:datauri,format:mimetype}
}
return require('./PixelManipulation.js')(newdata, {
output: output,
changePixel: changePixel,
format: newdata.mimeType.split('/')[1],
image: options.image
format: newdata.format,
image: options.image,
callback: callback
});
}

View File

@@ -9,26 +9,22 @@ module.exports = function GreenChannel(options) {
//function setup() {} // optional
function draw() {
images = this.images;
thisimage = images[options.image];
for (i in thisimage.steps){
if (thisimage.steps[i].options.id == options.id) pos = i;
}
olddata = thisimage.steps[i-1].output;
var newdata = JSON.parse(JSON.stringify(olddata));
function draw(callback) {
step = require('./_Step')(this,options);
newdata = step[0];
pos = step[1];
function changePixel(r, g, b, a) {
return [0, g, 0, a];
}
function output(image,datauri,mimetype){
images[image].steps[i].output = {src:datauri,mimeType:mimetype}
images[image].steps[pos].output = {src:datauri,format:mimetype}
}
return require('./PixelManipulation.js')(newdata, {
output: output,
changePixel: changePixel,
format: newdata.mimeType.split('/')[1],
image: options.image
format: newdata.format,
image: options.image,
callback: callback
});
}

View File

@@ -8,27 +8,23 @@ module.exports = function NdviRed(options) {
//function setup() {} // optional
function draw(image) {
images = this.images;
thisimage = images[options.image];
for (i in thisimage.steps){
if (thisimage.steps[i].options.id == options.id) pos = i;
}
olddata = thisimage.steps[i-1].output;
var newdata = JSON.parse(JSON.stringify(olddata));
function draw(callback) {
step = require('./_Step')(this,options);
newdata = step[0];
pos = step[1];
function changePixel(r, g, b, a) {
var ndvi = 255 * (b - r) / (1.00 * b + r);
return [ndvi, ndvi, ndvi, a];
}
function output(image,datauri,mimetype){
images[image].steps[i].output = {src:datauri,mimeType:mimetype}
images[image].steps[pos].output = {src:datauri,format:mimetype}
}
return require('./PixelManipulation.js')(newdata, {
output: output,
changePixel: changePixel,
format: newdata.mimeType.split('/')[1],
image: options.image
format: newdata.format,
image: options.image,
callback: callback
});
}

View File

@@ -43,13 +43,11 @@ module.exports = function PixelManipulation(image, options) {
// there may be a more efficient means to encode an image object,
// but node modules and their documentation are essentially arcane on this point
var buffer = base64.encode();
savePixels(pixels, options.format)
.on('end', function() {
datauri = 'data:image/' + options.format + ';base64,' + buffer.read().toString();
if (options.output) options.output(options.image,datauri,options.format);
savePixels(pixels, (options.format=="png"?"jpeg":options.format)).on('end', function() {
data = buffer.read().toString();
datauri = 'data:image/' + (options.format=="png"?"jpeg":options.format) + ';base64,' + data;
if (options.output) options.output(options.image,datauri,(options.format=="png"?"jpeg":options.format));
if (options.callback) options.callback();
}).pipe(buffer);
});

11
src/modules/_Step.js Normal file
View File

@@ -0,0 +1,11 @@
module.exports = function(ref,options) {
images = ref.images;
thisimage = images[options.image];
for (i in thisimage.steps){
if (thisimage.steps[i].options.id == options.id) pos = i;
}
olddata = thisimage.steps[pos-1].output;
if (typeof(olddata) == 'undefined') return false;
var newdata = JSON.parse(JSON.stringify(olddata));
return [newdata,pos];
}

View File

@@ -3,12 +3,12 @@
var fs = require('fs');
var test = require('tape');
// We should only test headless code here.
// http://stackoverflow.com/questions/21358015/error-jquery-requires-a-window-with-a-document#25622933
// We should only test headless code here.
// http://stackoverflow.com/questions/21358015/error-jquery-requires-a-window-with-a-document#25622933
require('../dist/image-sequencer.js');
require('../src/ImageSequencerNode.js');
var sequencer = ImageSequencer({ ui: false });
var sequencer = ImageSequencer({ ui: "none" });
//function read (file) {
// return fs.readFileSync('./test/fixtures/' + file, 'utf8').trim();
@@ -19,38 +19,41 @@ var sequencer = ImageSequencer({ ui: false });
//}
test('Image Sequencer has tests', function (t) {
// read('something.html')
t.equal(true, true);
t.end();
});
test('loadImages loads a step', function (t){
sequencer.loadImages('test','examples/SundarPichai.jpeg');
t.equal(sequencer.images.test.steps.length, 1, "It Does!");
t.end();
});
test('addStep adds a step', function (t) {
t.equal(sequencer.steps.length, 0);
sequencer.addStep('ndvi-red');
t.equal(sequencer.steps.length, 1);
sequencer.addSteps('test','do-nothing');
t.equal(sequencer.images.test.steps[1].options.name, "do-nothing");
t.equal(sequencer.images.test.steps.length, 2, "It Does!")
t.end();
});
test('each module conforms to base API except image-select', function (t) {
Object.keys(sequencer.modules).forEach(function(moduleName, i) {
if (moduleName != "image-select") sequencer.addStep(moduleName);
});
// should already have image-select:
t.equal(sequencer.steps.length, Object.keys(sequencer.modules).length);
sequencer.steps.forEach(function(step, i) {
//t.equal(step.test(step.testInput),step.testOutput);
// or check that it's equal with a diff method?
// we could also test each type of output
t.equal(step.setup === 'undefined', false);
t.equal(step.draw === 'undefined', false);
});
test('removeSteps removes a step', function (t) {
sequencer.removeSteps('test',1);
t.equal(sequencer.images.test.steps.length, 1, "It Does!");
t.end();
});
//test('a blank module does not modify an image, according to diff', function (t) {
//});
test('insertStep inserts a step', function (t) {
sequencer.insertSteps('test',1,'do-nothing');
t.equal(sequencer.images.test.steps[1].options.name, "do-nothing");
t.equal(sequencer.images.test.steps.length, 2, "It Does!");
t.end();
});
//test('a module modifies an image', function (t) {
//});
test('run creates output of steps', function (t) {
sequencer.run();
var steps = sequencer.images.test.steps
var type = typeof(steps[steps.length-1].output)
t.equal(type,"object");
t.end();
});