mirror of
https://github.com/publiclab/image-sequencer.git
synced 2025-12-13 20:00:05 +01:00
Phase 1 Completion
This commit is contained in:
6
index.js
6
index.js
@@ -1,8 +1,4 @@
|
|||||||
console.log('\x1b[31m%s\x1b[0m',"This is the output of the module");
|
console.log('\x1b[31m%s\x1b[0m',"This is the output of the module");
|
||||||
require('./src/ImageSequencerNode');
|
require('./src/ImageSequencerNode');
|
||||||
sequencer = ImageSequencer();
|
sequencer = ImageSequencer();
|
||||||
sequencer.loadImage('sundar','examples/SundarPichai.jpeg',function(){
|
sequencer.loadImages({red:'../../red.jpg'});
|
||||||
sequencer.loadImage('timetable','examples/test.png',function(){
|
|
||||||
sequencer.addSteps('do-nothing-pix');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|||||||
1
output.txt
Normal file
1
output.txt
Normal file
File diff suppressed because one or more lines are too long
@@ -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}
|
else {window = global; var isBrowser = false}
|
||||||
|
|
||||||
ImageSequencer = function ImageSequencer(options) {
|
ImageSequencer = function ImageSequencer(options) {
|
||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
options.inBrowser = options.inBrowser || isBrowser;
|
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;
|
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,
|
var image,
|
||||||
steps = [],
|
steps = [],
|
||||||
modules = require('./Modules'),
|
modules = require('./ModulesNode'),
|
||||||
images = [];
|
images = {};
|
||||||
|
|
||||||
// if in browser, prompt for an image
|
// if in browser, prompt for an image
|
||||||
if (options.imageSelect || options.inBrowser) addStep('image-select');
|
// if (options.imageSelect || options.inBrowser) addStep('image-select');
|
||||||
else if (options.imageUrl) loadImage(imageUrl);
|
// else if (options.imageUrl) loadImage(imageUrl);
|
||||||
|
|
||||||
// soon, detect local or URL?
|
function addStep(image, name, o) {
|
||||||
function addStep(name, o) {
|
log('\x1b[36m%s\x1b[0m','adding step \"' + name + '\" to \"' + image + '\".');
|
||||||
console.log('adding step "' + name + '"');
|
|
||||||
|
|
||||||
if (typeof(window) != "undefined")
|
|
||||||
for(var variable in window)
|
|
||||||
if(window[variable] == this)
|
|
||||||
options.instanceName = variable;
|
|
||||||
|
|
||||||
o = o || {};
|
o = o || {};
|
||||||
o.id = options.sequencerCounter++; //Gives a Unique ID to each step
|
o.id = options.sequencerCounter++; //Gives a Unique ID to each step
|
||||||
o.name = o.name || name;
|
o.name = o.name || name;
|
||||||
o.selector = o.selector || 'ismod-' + name;
|
o.selector = o.selector || 'ismod-' + name;
|
||||||
o.container = o.container || options.selector;
|
o.container = o.container || options.selector;
|
||||||
|
o.image = image;
|
||||||
|
|
||||||
var module = modules[name](o);
|
var module = modules[name](o);
|
||||||
|
images[image].steps.push(module);
|
||||||
steps.push(module);
|
|
||||||
|
|
||||||
function defaultSetupModule() {
|
function defaultSetupModule() {
|
||||||
if (options.ui) module.options.ui = options.ui({
|
if (options.ui && options.ui!="none") module.options.ui = options.ui({
|
||||||
selector: o.selector,
|
selector: o.selector,
|
||||||
title: module.options.title,
|
title: module.options.title,
|
||||||
id: o.id,
|
id: o.id
|
||||||
instanceName: options.instanceName
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
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 {
|
function addSteps(){
|
||||||
|
argtype = [];
|
||||||
// add a default UI, unless the module has one specified
|
json_q = {};
|
||||||
if (module.hasOwnProperty('setup')) module.setup();
|
for (i in images) {
|
||||||
else {
|
lastimage = i;
|
||||||
defaultSetupModule.apply(module); // run default setup() in scope of module (is this right?)
|
|
||||||
}
|
}
|
||||||
|
for (var arg in arguments) {
|
||||||
var previousStep = steps[steps.length - 2];
|
argtype.push(objTypeOf(arguments[arg]));
|
||||||
|
}
|
||||||
if (previousStep) {
|
if (arguments.length == 1) {
|
||||||
// connect output of last step to input of this step
|
if(argtype[0] == "Object")
|
||||||
previousStep.options.output = function output(image) {
|
json_q = arguments[0];
|
||||||
if (sequencer.steps[0].options.initialImage) {
|
else
|
||||||
options.initialImage = sequencer.steps[0].options.initialImage;
|
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
|
for (i in json_q)
|
||||||
if (previousStep.options.ui && previousStep.options.ui.display) previousStep.options.ui.display(image);
|
for (j in json_q[i])
|
||||||
module.draw(image);
|
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);
|
||||||
}
|
}
|
||||||
|
function filter(json_q){
|
||||||
// Pre-set the initial output behavior of the final step,
|
for (image in json_q) {
|
||||||
// which will be changed if an additional step is added.
|
prevstep = images[image].steps[json_q[image]-1];
|
||||||
module.options.output = function output(image) {
|
while (typeof(prevstep) == "undefined" || typeof(prevstep.output) == "undefined") {
|
||||||
if (module.options.ui && module.options.ui.display) module.options.ui.display(image);
|
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) {
|
function loadImage(name, src, callback) {
|
||||||
for (i=0;i<steps.length;i++) {
|
image = {
|
||||||
if (steps[i].options.id == id && steps[i].options.name != 'image-select'){
|
src: src,
|
||||||
console.log('removing step "'+steps[i].options.name+'"');
|
steps: [{
|
||||||
if (options.inBrowser) steps[i].options.ui.remove();
|
options: {
|
||||||
steps.splice(i,1);
|
id: options.sequencerCounter++,
|
||||||
run(options.initialImage);
|
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])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
else if (arguments.length == 3) {
|
||||||
|
loadImage(arguments[0],arguments[1],arguments[2])
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
image.src = src;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
options: options,
|
options: options,
|
||||||
loadImage: loadImage,
|
loadImages: loadImages,
|
||||||
images: images,
|
addSteps: addSteps,
|
||||||
addStep: addStep,
|
removeSteps: removeSteps,
|
||||||
removeStep: removeStep,
|
insertSteps: insertSteps,
|
||||||
run: run,
|
run: run,
|
||||||
modules: modules,
|
modules: modules,
|
||||||
steps: steps,
|
images: images,
|
||||||
ui: options.ui
|
ui: options.ui
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
|
||||||
@@ -2,11 +2,8 @@
|
|||||||
* Core modules
|
* Core modules
|
||||||
*/
|
*/
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
'do-nothing': require('./modules/DoNothing'),
|
||||||
'image-select': require('./modules/ImageSelect'),
|
|
||||||
'green-channel': require('./modules/GreenChannel'),
|
'green-channel': require('./modules/GreenChannel'),
|
||||||
'ndvi-red': require('./modules/NdviRed'),
|
'ndvi-red': require('./modules/NdviRed'),
|
||||||
'plot': require('./modules/Plot'),
|
'do-nothing-pix': require('./modules/DoNothingPix')
|
||||||
'image-threshold': require('./modules/ImageThreshold'),
|
|
||||||
'crop': require('./modules/Crop')
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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')
|
|
||||||
}
|
|
||||||
@@ -8,14 +8,12 @@ module.exports = function DoNothing(options) {
|
|||||||
var image;
|
var image;
|
||||||
var output;
|
var output;
|
||||||
|
|
||||||
function draw() {
|
function draw(callback) {
|
||||||
thisimage = this.images[options.image];
|
step = require('./_Step')(this,options);
|
||||||
for (i in thisimage.steps){
|
newdata = step[0];
|
||||||
if (thisimage.steps[i].options.id == options.id) pos = i;
|
pos = step[1];
|
||||||
}
|
thisimage.steps[pos].output = {src:newdata.src,format:newdata.format};
|
||||||
olddata = thisimage.steps[i-1].output;
|
callback();
|
||||||
var newdata = JSON.parse(JSON.stringify(olddata));
|
|
||||||
thisimage.steps[i].output = {src:newdata.src,mimeType:newdata.mimeType};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get() {
|
function get() {
|
||||||
|
|||||||
@@ -9,26 +9,22 @@ module.exports = function GreenChannel(options) {
|
|||||||
|
|
||||||
//function setup() {} // optional
|
//function setup() {} // optional
|
||||||
|
|
||||||
function draw() {
|
function draw(callback) {
|
||||||
images = this.images;
|
step = require('./_Step')(this,options);
|
||||||
thisimage = images[options.image];
|
newdata = step[0];
|
||||||
for (i in thisimage.steps){
|
pos = step[1];
|
||||||
if (thisimage.steps[i].options.id == options.id) pos = i;
|
|
||||||
}
|
|
||||||
olddata = thisimage.steps[i-1].output;
|
|
||||||
var newdata = JSON.parse(JSON.stringify(olddata));
|
|
||||||
|
|
||||||
function changePixel(r, g, b, a) {
|
function changePixel(r, g, b, a) {
|
||||||
return [r, g, b, a];
|
return [r, g, b, a];
|
||||||
}
|
}
|
||||||
function output(image,datauri,mimetype){
|
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, {
|
return require('./PixelManipulation.js')(newdata, {
|
||||||
output: output,
|
output: output,
|
||||||
changePixel: changePixel,
|
changePixel: changePixel,
|
||||||
format: newdata.mimeType.split('/')[1],
|
format: newdata.format,
|
||||||
image: options.image
|
image: options.image,
|
||||||
|
callback: callback
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,26 +9,22 @@ module.exports = function GreenChannel(options) {
|
|||||||
|
|
||||||
//function setup() {} // optional
|
//function setup() {} // optional
|
||||||
|
|
||||||
function draw() {
|
function draw(callback) {
|
||||||
images = this.images;
|
step = require('./_Step')(this,options);
|
||||||
thisimage = images[options.image];
|
newdata = step[0];
|
||||||
for (i in thisimage.steps){
|
pos = step[1];
|
||||||
if (thisimage.steps[i].options.id == options.id) pos = i;
|
|
||||||
}
|
|
||||||
olddata = thisimage.steps[i-1].output;
|
|
||||||
var newdata = JSON.parse(JSON.stringify(olddata));
|
|
||||||
|
|
||||||
function changePixel(r, g, b, a) {
|
function changePixel(r, g, b, a) {
|
||||||
return [0, g, 0, a];
|
return [0, g, 0, a];
|
||||||
}
|
}
|
||||||
function output(image,datauri,mimetype){
|
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, {
|
return require('./PixelManipulation.js')(newdata, {
|
||||||
output: output,
|
output: output,
|
||||||
changePixel: changePixel,
|
changePixel: changePixel,
|
||||||
format: newdata.mimeType.split('/')[1],
|
format: newdata.format,
|
||||||
image: options.image
|
image: options.image,
|
||||||
|
callback: callback
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,27 +8,23 @@ module.exports = function NdviRed(options) {
|
|||||||
|
|
||||||
//function setup() {} // optional
|
//function setup() {} // optional
|
||||||
|
|
||||||
function draw(image) {
|
function draw(callback) {
|
||||||
images = this.images;
|
step = require('./_Step')(this,options);
|
||||||
thisimage = images[options.image];
|
newdata = step[0];
|
||||||
for (i in thisimage.steps){
|
pos = step[1];
|
||||||
if (thisimage.steps[i].options.id == options.id) pos = i;
|
|
||||||
}
|
|
||||||
olddata = thisimage.steps[i-1].output;
|
|
||||||
var newdata = JSON.parse(JSON.stringify(olddata));
|
|
||||||
|
|
||||||
function changePixel(r, g, b, a) {
|
function changePixel(r, g, b, a) {
|
||||||
var ndvi = 255 * (b - r) / (1.00 * b + r);
|
var ndvi = 255 * (b - r) / (1.00 * b + r);
|
||||||
return [ndvi, ndvi, ndvi, a];
|
return [ndvi, ndvi, ndvi, a];
|
||||||
}
|
}
|
||||||
function output(image,datauri,mimetype){
|
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, {
|
return require('./PixelManipulation.js')(newdata, {
|
||||||
output: output,
|
output: output,
|
||||||
changePixel: changePixel,
|
changePixel: changePixel,
|
||||||
format: newdata.mimeType.split('/')[1],
|
format: newdata.format,
|
||||||
image: options.image
|
image: options.image,
|
||||||
|
callback: callback
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,13 +43,11 @@ module.exports = function PixelManipulation(image, options) {
|
|||||||
// there may be a more efficient means to encode an image object,
|
// there may be a more efficient means to encode an image object,
|
||||||
// but node modules and their documentation are essentially arcane on this point
|
// but node modules and their documentation are essentially arcane on this point
|
||||||
var buffer = base64.encode();
|
var buffer = base64.encode();
|
||||||
savePixels(pixels, options.format)
|
savePixels(pixels, (options.format=="png"?"jpeg":options.format)).on('end', function() {
|
||||||
.on('end', function() {
|
data = buffer.read().toString();
|
||||||
|
datauri = 'data:image/' + (options.format=="png"?"jpeg":options.format) + ';base64,' + data;
|
||||||
datauri = 'data:image/' + options.format + ';base64,' + buffer.read().toString();
|
if (options.output) options.output(options.image,datauri,(options.format=="png"?"jpeg":options.format));
|
||||||
|
if (options.callback) options.callback();
|
||||||
if (options.output) options.output(options.image,datauri,options.format);
|
|
||||||
|
|
||||||
}).pipe(buffer);
|
}).pipe(buffer);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
11
src/modules/_Step.js
Normal file
11
src/modules/_Step.js
Normal 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];
|
||||||
|
}
|
||||||
@@ -6,9 +6,9 @@ var test = require('tape');
|
|||||||
// We should only test headless code here.
|
// We should only test headless code here.
|
||||||
// http://stackoverflow.com/questions/21358015/error-jquery-requires-a-window-with-a-document#25622933
|
// 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) {
|
//function read (file) {
|
||||||
// return fs.readFileSync('./test/fixtures/' + file, 'utf8').trim();
|
// return fs.readFileSync('./test/fixtures/' + file, 'utf8').trim();
|
||||||
@@ -19,38 +19,41 @@ var sequencer = ImageSequencer({ ui: false });
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
test('Image Sequencer has tests', function (t) {
|
test('Image Sequencer has tests', function (t) {
|
||||||
// read('something.html')
|
|
||||||
t.equal(true, true);
|
t.equal(true, true);
|
||||||
t.end();
|
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) {
|
test('addStep adds a step', function (t) {
|
||||||
t.equal(sequencer.steps.length, 0);
|
sequencer.addSteps('test','do-nothing');
|
||||||
sequencer.addStep('ndvi-red');
|
t.equal(sequencer.images.test.steps[1].options.name, "do-nothing");
|
||||||
t.equal(sequencer.steps.length, 1);
|
t.equal(sequencer.images.test.steps.length, 2, "It Does!")
|
||||||
t.end();
|
t.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('each module conforms to base API except image-select', function (t) {
|
test('removeSteps removes a step', function (t) {
|
||||||
Object.keys(sequencer.modules).forEach(function(moduleName, i) {
|
sequencer.removeSteps('test',1);
|
||||||
if (moduleName != "image-select") sequencer.addStep(moduleName);
|
t.equal(sequencer.images.test.steps.length, 1, "It Does!");
|
||||||
});
|
|
||||||
// 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);
|
|
||||||
});
|
|
||||||
t.end();
|
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('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();
|
||||||
|
});
|
||||||
|
|
||||||
//test('a module modifies an image', function (t) {
|
|
||||||
|
|
||||||
//});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user