Modularization

This commit is contained in:
Chinmay Pandhare
2017-06-27 00:26:03 +05:30
parent 4ec9f4640a
commit d22a95beaf
8 changed files with 208 additions and 161 deletions

View File

@@ -51,10 +51,13 @@ loadImages accepts 1, 2, or 3 parameters.
* 1/2 parameters (JSON) :
```js
sequencer.loadImages({
image_name_1: image_src,
image_name_2: image_src,
...
}, optional_callback);
images: {
image_name_1: image_src,
image_name_2: image_src,
...
},
callback: optional_callback
});
```
### Adding Steps on Images

View File

@@ -1,11 +1,11 @@
console.log('\x1b[31m%s\x1b[0m',"This is the output of the module");
require('./src/ImageSequencer');
sequencer = ImageSequencer();
sequencer.loadImages({red:'examples/red.jpg'},function(){
sequencer.loadImages({images:{red:'examples/red.jpg'},callback:function(){
sequencer.addSteps(['do-nothing','do-nothing-pix','do-nothing-pix','ndvi-red']);
sequencer.removeSteps(1);
sequencer.insertSteps({
red: [{index: -1, name: 'do-nothing', o:{}}]
});
sequencer.run();
});
}});

33
src/AddStep.js Normal file
View File

@@ -0,0 +1,33 @@
function AddStep(ref, image, name, o) {
function addStep(image, name, o_) {
ref.log('\x1b[36m%s\x1b[0m','adding step \"' + name + '\" to \"' + image + '\".');
o = {};
o.id = ref.options.sequencerCounter++; //Gives a Unique ID to each step
o.name = o_.name || name;
o.selector = o_.selector || 'ismod-' + name;
o.container = o_.container || ref.options.selector;
o.image = image;
var module = ref.modules[name].call(ref.images,o);
ref.images[image].steps.push(module);
function defaultSetupModule() {
if (ref.options.ui && ref.options.ui!="none") module.options.ui = ref.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 added.
return true;
}
addStep(image, name, o);
}
module.exports = AddStep;

View File

@@ -13,7 +13,13 @@ function makeArray(input) {
function copy(a) {
if (!typeof(a) == "object") return a;
if (objTypeOf(a) == "Array") return a.slice();
if (objTypeOf(a) == "Object") return JSON.parse(JSON.stringify(a));
if (objTypeOf(a) == "Object") {
var b = {};
for (v in a) {
b[v] = copy(a[v]);
}
return b;
}
return a;
}
@@ -32,6 +38,9 @@ function formatInput(args,format,images) {
format = ['o_string_a', 'number', 'string', 'o_object'];
else if (format == "r")
format = ['o_string_a', 'o_number'];
else if (format == "l")
format = ['string','string','o_function'];
/*
formats:
addSteps :: o_image_a, name_a, o_o
@@ -42,11 +51,14 @@ function formatInput(args,format,images) {
o_string_a, number, string, o_object => { image: [{index,name,o}] }
run :: o_image_a, o_from
o_string_a, o_number => { image: index }
loadImages :: image, src, o_function
string, string, o_function => { images: [{image:src}], callback }
optionals:
image: o_string_a
options: o_object
from: o_number
callback: o_function
*/
if(format[format.length-1] == "o_object") {
@@ -57,6 +69,11 @@ function formatInput(args,format,images) {
if(typeof(args[args.length-1]) != "number" && objTypeOf(args[0])!="Object")
args.push(1);
}
else if (format[format.length-1] == "o_function") {
if(objTypeOf(args[args.length-1]) != "Function" && objTypeOf(args[0])!="Object")
args.push(function(){});
}
if(format[0] == "o_string_a") {
if(args.length == format.length - 1) {
insert = false;
@@ -80,13 +97,21 @@ function formatInput(args,format,images) {
if (args.length == 1) {
json_q = copy(args[0]);
if(!(format_i == "r"))
for (img in json_q) {
if(!(format_i == "r" || format_i == "l")) {
for (img in json_q)
json_q[img] = makeArray(json_q[img]);
}
}
}
else if (format_i == "r")
else if (format_i == "r") {
for (img in args[0]) json_q[args[0][img]] = args[1];
}
else if (format_i == "l") {
json_q = {
images: {},
callback: args[2]
}
json_q.images[args[0]] = args[1];
}
else {
for (img in args[0]) {
image = args[0][img];

View File

@@ -8,15 +8,6 @@ ImageSequencer = function ImageSequencer(options) {
// 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)
}
@@ -31,7 +22,13 @@ ImageSequencer = function ImageSequencer(options) {
function copy(a) {
if (!typeof(a) == "object") return a;
if (objTypeOf(a) == "Array") return a.slice();
if (objTypeOf(a) == "Object") return JSON.parse(JSON.stringify(a));
if (objTypeOf(a) == "Object") {
var b = {};
for (v in a) {
b[v] = copy(a[v]);
}
return b;
}
return a;
}
@@ -50,34 +47,6 @@ ImageSequencer = function ImageSequencer(options) {
// if (options.imageSelect || options.inBrowser) addStep('image-select');
// else if (options.imageUrl) loadImage(imageUrl);
function addStep(image, name, o_) {
log('\x1b[36m%s\x1b[0m','adding step \"' + name + '\" to \"' + image + '\".');
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].call(this.images,o);
images[image].steps.push(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 added.
return true;
}
function addSteps(){
args = [];
json_q = {};
@@ -85,8 +54,7 @@ ImageSequencer = function ImageSequencer(options) {
json_q = formatInput.call(this,args,"+");
for (i in json_q)
for (j in json_q[i])
addStep.call(this,i,json_q[i][j].name,json_q[i][j].o);
require("./AddStep")(this,i,json_q[i][j].name,json_q[i][j].o);
}
function removeStep(image,index) {
@@ -96,7 +64,6 @@ ImageSequencer = function ImageSequencer(options) {
images[image].steps.splice(index,1);
}
//tell the UI a step has been removed
}
function removeSteps(image,index) {
@@ -114,34 +81,6 @@ ImageSequencer = function ImageSequencer(options) {
// this.run(run); // This is creating problems
}
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;
@@ -154,7 +93,7 @@ ImageSequencer = function ImageSequencer(options) {
var details = json_q[img];
details = details.sort(function(a,b){return b.index-a.index});
for (i in details)
insertStep(img,details[i].index,details[i].name,details[i].o);
require("./InsertStep")(this,img,details[i].index,details[i].name,details[i].o);
// run[img] = details[details.length-1].index;
}
// this.run(run); // This is Creating issues
@@ -165,92 +104,25 @@ ImageSequencer = function ImageSequencer(options) {
this_ = this;
args = [];
for (var arg in arguments) args.push(copy(arguments[arg]));
callback = function() {};
for (var arg in args)
if(objTypeOf(args[arg]) == "Function")
var callback = args.splice(arg,1)[0];
function drawStep(drawarray,pos) {
if(pos==drawarray.length) if(objTypeOf(callback)=='Function') callback();
if(pos>=drawarray.length) return true;
image = drawarray[pos].image;
i = drawarray[pos].i;
input = images[image].steps[i-1].output;
images[image].steps[i].draw(copy(input),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){
for (image in json_q) {
if (json_q[image]==0 && this_.images[image].steps.length==1)
delete json_q[image];
else json_q[image]++;
}
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;
}
callback = args.splice(arg,1)[0];
json_q = formatInput.call(this,args,"r");
json_q = filter(json_q);
drawSteps(json_q);
}
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();
require('./Run')(this, json_q, 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])
}
args = [];
for (arg in arguments) args.push(copy(arguments[arg]));
json_q = formatInput.call(this,args,"l");
for (i in json_q.images)
require('./LoadImage')(this,i,json_q.images[i])
json_q.callback();
}
return {
@@ -262,9 +134,11 @@ ImageSequencer = function ImageSequencer(options) {
run: run,
modules: modules,
images: images,
ui: options.ui
ui: options.ui,
log: log,
objTypeOf: objTypeOf,
copy: copy
}
}
module.exports = ImageSequencer;

35
src/InsertStep.js Normal file
View File

@@ -0,0 +1,35 @@
function InsertStep(ref, image, index, name, o) {
function insertStep(image, index, name, o) {
ref.log('\x1b[36m%s\x1b[0m','inserting step \"' + name + '\" to \"' + image + '\" at \"'+index+'\".');
o = o || {};
o.id = ref.options.sequencerCounter++; //Gives a Unique ID to each step
o.name = o.name || name;
o.selector = o.selector || 'ismod-' + name;
o.container = o.container || ref.options.selector;
o.image = image;
if(index==-1) index = ref.images[image].steps.length;
var module = ref.modules[name](o);
ref.images[image].steps.splice(index, 0, module);
function defaultSetupModule() {
if (ref.options.ui && ref.options.ui!="none") module.options.ui = ref.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;
}
insertStep(image, index, name, o);
}
module.exports = InsertStep;

36
src/LoadImage.js Normal file
View File

@@ -0,0 +1,36 @@
function LoadImage(ref, name, src) {
function CImage(src) {
datauri = (ref.options.inBrowser)?(src):require('urify')(src);
image = {
src: datauri,
format: datauri.split(':')[1].split(';')[0].split('/')[1]
}
return image;
}
function loadImage(name, src) {
image = {
src: src,
steps: [{
options: {
id: ref.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)
}]
};
ref.images[name] = image;
}
return loadImage(name,src);
}
module.exports = LoadImage;

41
src/Run.js Normal file
View File

@@ -0,0 +1,41 @@
function Run(ref, json_q, callback) {
function drawStep(drawarray,pos) {
if(pos==drawarray.length) if(ref.objTypeOf(callback)=='Function') callback();
if(pos>=drawarray.length) return true;
image = drawarray[pos].image;
i = drawarray[pos].i;
input = ref.images[image].steps[i-1].output;
ref.images[image].steps[i].draw(ref.copy(input),function(){
drawStep(drawarray,++pos);
});
}
function drawSteps(json_q) {
drawarray = [];
for (image in json_q) {
no_steps = ref.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){
for (image in json_q) {
if (json_q[image]==0 && ref.images[image].steps.length==1)
delete json_q[image];
else json_q[image]++;
}
for (image in json_q) {
prevstep = ref.images[image].steps[json_q[image]-1];
while (typeof(prevstep) == "undefined" || typeof(prevstep.output) == "undefined") {
prevstep = ref.images[image].steps[(--json_q[image]) - 1];
}
}
return json_q;
}
json_q = filter(json_q);
drawSteps(json_q);
}
module.exports = Run;