add API to get steps (#259)

* add API to get steps

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* update dist and refactor

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* rename to getStepOutput

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* api complete

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* add test

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* change indentation to 2 spaces

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* minor fixes

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* new API

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* remove commented code

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>

* modifiication to invert

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>
This commit is contained in:
Varun Gupta
2018-05-24 06:08:20 +05:30
committed by Jeffrey Warren
parent bd490d2515
commit 09c4f250a2
7 changed files with 12356 additions and 11224 deletions

23434
dist/image-sequencer.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -23,7 +23,10 @@ function AddStep(_sequencer, image, name, o) {
options: o options: o
}; };
var UI = _sequencer.events; var UI = _sequencer.events;
var module = _sequencer.modules[name][0](o,UI); this.getStep = function getStep(offset) {
return _sequencer.images[image].steps.slice(offset - 1)[0];
}
var module = _sequencer.modules[name][0].bind(this)(o, UI);
_sequencer.images[image].steps.push(module); _sequencer.images[image].steps.push(module);
return true; return true;

View File

@@ -9,7 +9,7 @@ function InsertStep(ref, image, index, name, o) {
o.container = o_.container || ref.options.selector; o.container = o_.container || ref.options.selector;
o.image = image; o.image = image;
if(index==-1) index = ref.images[image].steps.length; if (index == -1) index = ref.images[image].steps.length;
o.step = { o.step = {
name: o.name, name: o.name,
@@ -22,8 +22,11 @@ function InsertStep(ref, image, index, name, o) {
options: o options: o
}; };
var UI = ref.events; var UI = ref.events;
var module = ref.modules[name][0](o,UI); this.getStep = function getStep(offset) {
ref.images[image].steps.splice(index,0,module); return ref.images[image].steps.slice(offset)[0];
}
var module = ref.modules[name][0].bind(this)(o, UI);
ref.images[image].steps.splice(index, 0, module);
return true; return true;
} }

View File

@@ -1,24 +1,29 @@
function Run(ref, json_q, callback,progressObj) { function Run(ref, json_q, callback, progressObj) {
if(!progressObj) progressObj = {stop: function(){}} if (!progressObj) progressObj = { stop: function () { } };
function drawStep(drawarray, pos) { function drawStep(drawarray, pos) {
if (pos == drawarray.length && drawarray[pos - 1] !== undefined) { if (pos == drawarray.length && drawarray[pos - 1] !== undefined) {
var image = drawarray[pos-1].image; var image = drawarray[pos - 1].image;
if(ref.objTypeOf(callback) == 'Function') { if (ref.objTypeOf(callback) == "Function") {
var steps = ref.images[image].steps; var steps = ref.images[image].steps;
var out = steps[steps.length-1].output.src; var out = steps[steps.length - 1].output.src;
callback(out); callback(out);
return true; return true;
} }
} }
// so we don't run on the loadImage module: // so we don't run on the loadImage module:
if (drawarray[pos] !== undefined) { if (drawarray[pos] !== undefined) {
var image = drawarray[pos].image; var image = drawarray[pos].image;
var i = drawarray[pos].i; var i = drawarray[pos].i;
var input = ref.images[image].steps[i - 1].output; var input = ref.images[image].steps[i - 1].output;
ref.images[image].steps[i].draw(ref.copy(input), function onEachStep() { ref.images[image].steps[i].draw(
drawStep(drawarray, ++pos); ref.copy(input),
},progressObj); function onEachStep() {
drawStep(drawarray, ++pos);
},
progressObj
);
} }
} }
@@ -27,14 +32,14 @@ function Run(ref, json_q, callback,progressObj) {
for (var image in json_q) { for (var image in json_q) {
var no_steps = ref.images[image].steps.length; var no_steps = ref.images[image].steps.length;
var init = json_q[image]; var init = json_q[image];
for(var i = 0; i < no_steps - init; i++) { for (var i = 0; i < no_steps - init; i++) {
drawarray.push({ image: image, i: init + i }); drawarray.push({ image: image, i: init + i });
} }
} }
drawStep(drawarray,0); drawStep(drawarray, 0);
} }
function filter(json_q){ function filter(json_q) {
for (var image in json_q) { for (var image in json_q) {
if (json_q[image] == 0 && ref.images[image].steps.length == 1) if (json_q[image] == 0 && ref.images[image].steps.length == 1)
delete json_q[image]; delete json_q[image];
@@ -42,8 +47,11 @@ function Run(ref, json_q, callback,progressObj) {
} }
for (var image in json_q) { for (var image in json_q) {
var prevstep = ref.images[image].steps[json_q[image] - 1]; var prevstep = ref.images[image].steps[json_q[image] - 1];
while (typeof(prevstep) == "undefined" || typeof(prevstep.output) == "undefined") { while (
prevstep = ref.images[image].steps[(--json_q[image]) - 1]; typeof prevstep == "undefined" ||
typeof prevstep.output == "undefined"
) {
prevstep = ref.images[image].steps[--json_q[image] - 1];
} }
} }
return json_q; return json_q;
@@ -51,6 +59,5 @@ function Run(ref, json_q, callback,progressObj) {
var json_q = filter(json_q); var json_q = filter(json_q);
return drawSteps(json_q); return drawSteps(json_q);
} }
module.exports = Run; module.exports = Run;

View File

@@ -1,7 +1,7 @@
/* /*
* Invert the image * Invert the image
*/ */
module.exports = function Invert(options,UI) { module.exports = function Invert(options, UI) {
options = options || {}; options = options || {};
@@ -10,8 +10,11 @@ module.exports = function Invert(options,UI) {
var output; var output;
// The function which is called on every draw. // The function which is called on every draw.
function draw(input,callback,progressObj) { function draw(input, callback, progressObj) {
console.log(getStep(-2).options.name);
console.log(getStep(-1).options.name);
console.log(getStep(0).options.name);
progressObj.stop(true); progressObj.stop(true);
progressObj.overrideFlag = true; progressObj.overrideFlag = true;
// Tell UI that a step is being drawn. // Tell UI that a step is being drawn.
@@ -20,13 +23,13 @@ module.exports = function Invert(options,UI) {
var step = this; var step = this;
function changePixel(r, g, b, a) { function changePixel(r, g, b, a) {
return [255-r, 255-g, 255-b, a]; return [255 - r, 255 - g, 255 - b, a];
} }
function output(image,datauri,mimetype){ function output(image, datauri, mimetype) {
// This output is accessible by Image Sequencer // This output is accessible by Image Sequencer
step.output = {src:datauri,format:mimetype}; step.output = { src: datauri, format: mimetype };
// This output is accessible by UI // This output is accessible by UI
options.step.output = datauri; options.step.output = datauri;
@@ -48,7 +51,7 @@ module.exports = function Invert(options,UI) {
return { return {
options: options, options: options,
draw: draw, draw: draw,
output: output, output: output,
UI: UI UI: UI
} }

View File

@@ -9,21 +9,21 @@ var test = require('tape');
require('../../src/ImageSequencer.js'); require('../../src/ImageSequencer.js');
// This function is used to test whether or not any additional global variables are being created // This function is used to test whether or not any additional global variables are being created
function copy(g,a) { function copy(g, a) {
var b = {}; var b = {};
var i = 0; var i = 0;
for (var v in a) for (var v in a)
if(g) { if (g) {
if(v != "sequencer") b[v] = a[v]; if (v != "sequencer") b[v] = a[v];
} }
else { else {
if(v != "sequencer" && v!="global1" && v!="global2" && !global1.hasOwnProperty(v)) i++; if (v != "sequencer" && v != "global1" && v != "global2" && !global1.hasOwnProperty(v)) i++;
} }
if(g) return b; if (g) return b;
else return i; else return i;
} }
var parent = (typeof(global)==="undefined")?window:global; var parent = (typeof (global) === "undefined") ? window : global;
var global1 = copy(true,parent); var global1 = copy(true, parent);
var sequencer = ImageSequencer({ ui: false }); var sequencer = ImageSequencer({ ui: false });
var red = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAQABADASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAABgj/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwCdABykX//Z"; var red = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAQABADASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAABgj/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwCdABykX//Z";
@@ -33,14 +33,14 @@ test('Image Sequencer has tests', function (t) {
t.end(); t.end();
}); });
test('loadImages loads a DataURL image and creates a step.', function (t){ test('loadImages loads a DataURL image and creates a step.', function (t) {
sequencer.loadImages('test',red); sequencer.loadImages('test', red);
t.equal(sequencer.images.test.steps.length, 1, "Initial Step Created"); t.equal(sequencer.images.test.steps.length, 1, "Initial Step Created");
t.equal(typeof(sequencer.images.test.steps[0].output.src), "string", "Initial output exists"); t.equal(typeof (sequencer.images.test.steps[0].output.src), "string", "Initial output exists");
t.end(); t.end();
}); });
test('modulesInfo() returns info for each module', function (t){ test('modulesInfo() returns info for each module', function (t) {
var info = sequencer.modulesInfo(); var info = sequencer.modulesInfo();
t.equal(Object.keys(info).length, Object.keys(sequencer.modules).length); t.equal(Object.keys(info).length, Object.keys(sequencer.modules).length);
t.equal(info.hasOwnProperty(Object.keys(sequencer.modules)[0]), true); t.equal(info.hasOwnProperty(Object.keys(sequencer.modules)[0]), true);
@@ -49,40 +49,40 @@ test('modulesInfo() returns info for each module', function (t){
t.end(); t.end();
}); });
if(!sequencer.options.inBrowser) if (!sequencer.options.inBrowser)
test('loadImage loads an image from URL and creates a step. (NodeJS)', function (t){ test('loadImage loads an image from URL and creates a step. (NodeJS)', function (t) {
require('dns').resolve('www.github.com', function(err) { require('dns').resolve('www.github.com', function (err) {
if (err) { if (err) {
console.log("Test aborted due to no internet"); console.log("Test aborted due to no internet");
t.end(); t.end();
} }
else { else {
sequencer.loadImage('URL','https://ccpandhare.github.io/image-sequencer/examples/images/red.jpg', function(){ sequencer.loadImage('URL', 'https://ccpandhare.github.io/image-sequencer/examples/images/red.jpg', function () {
t.equal(sequencer.images.URL.steps.length, 1, "Initial Step Created"); t.equal(sequencer.images.URL.steps.length, 1, "Initial Step Created");
t.equal(typeof(sequencer.images.URL.steps[0].output.src), "string", "Initial output exists"); t.equal(typeof (sequencer.images.URL.steps[0].output.src), "string", "Initial output exists");
t.end(); t.end();
}); });
} }
}); });
}); });
if(!sequencer.options.inBrowser) if (!sequencer.options.inBrowser)
test('loadImages loads an image from PATH and creates a step. (NodeJS)', function (t){ test('loadImages loads an image from PATH and creates a step. (NodeJS)', function (t) {
sequencer.loadImages('examples/images/red.jpg'); sequencer.loadImages('examples/images/red.jpg');
t.equal(sequencer.images.image1.steps.length, 1, "Initial Step Created"); t.equal(sequencer.images.image1.steps.length, 1, "Initial Step Created");
t.equal(typeof(sequencer.images.image1.steps[0].output.src), "string", "Initial output exists"); t.equal(typeof (sequencer.images.image1.steps[0].output.src), "string", "Initial output exists");
t.end(); t.end();
}); });
test('loadImage works too.', function (t){ test('loadImage works too.', function (t) {
sequencer.loadImage('test2',red); sequencer.loadImage('test2', red);
t.equal(sequencer.images.test2.steps.length, 1, "Initial Step Created"); t.equal(sequencer.images.test2.steps.length, 1, "Initial Step Created");
t.equal(typeof(sequencer.images.test2.steps[0].output.src), "string", "Initial output exists"); t.equal(typeof (sequencer.images.test2.steps[0].output.src), "string", "Initial output exists");
t.end(); t.end();
}); });
test('addSteps("image","name") adds a step', function (t) { test('addSteps("image","name") adds a step', function (t) {
sequencer.addSteps('test','channel'); sequencer.addSteps('test', 'channel');
t.equal(sequencer.images.test.steps.length, 2, "Length of steps increased") t.equal(sequencer.images.test.steps.length, 2, "Length of steps increased")
t.equal(sequencer.images.test.steps[1].options.name, "channel", "Correct Step Added"); t.equal(sequencer.images.test.steps[1].options.name, "channel", "Correct Step Added");
t.equal(sequencer.images.test.steps[1].options.description, "Displays only one color channel of an image -- default is green", "Step description shown"); t.equal(sequencer.images.test.steps[1].options.description, "Displays only one color channel of an image -- default is green", "Step description shown");
@@ -97,7 +97,7 @@ test('addSteps("name") adds a step', function (t) {
}); });
test('addSteps(["name"]) adds a step', function (t) { test('addSteps(["name"]) adds a step', function (t) {
sequencer.addSteps(['channel','invert']); sequencer.addSteps(['channel', 'invert']);
t.equal(sequencer.images.test.steps.length, 5, "Length of steps increased by two") t.equal(sequencer.images.test.steps.length, 5, "Length of steps increased by two")
t.equal(sequencer.images.test.steps[3].options.name, "channel", "Correct Step Added"); t.equal(sequencer.images.test.steps[3].options.name, "channel", "Correct Step Added");
t.equal(sequencer.images.test.steps[4].options.name, "invert", "Correct Step Added"); t.equal(sequencer.images.test.steps[4].options.name, "invert", "Correct Step Added");
@@ -105,81 +105,81 @@ test('addSteps(["name"]) adds a step', function (t) {
}); });
test('addSteps("name",o) adds a step', function (t) { test('addSteps("name",o) adds a step', function (t) {
sequencer.addSteps('channel',{}); sequencer.addSteps('channel', {});
t.equal(sequencer.images.test.steps.length, 6, "Length of steps increased"); t.equal(sequencer.images.test.steps.length, 6, "Length of steps increased");
t.equal(sequencer.images.test.steps[5].options.name, "channel", "Correct Step Added"); t.equal(sequencer.images.test.steps[5].options.name, "channel", "Correct Step Added");
t.end(); t.end();
}); });
test('addSteps("image","name",o) adds a step', function (t) { test('addSteps("image","name",o) adds a step', function (t) {
sequencer.addSteps('test','channel',{}); sequencer.addSteps('test', 'channel', {});
t.equal(sequencer.images.test.steps.length, 7, "Length of steps increased"); t.equal(sequencer.images.test.steps.length, 7, "Length of steps increased");
t.equal(sequencer.images.test.steps[6].options.name, "channel", "Correct Step Added"); t.equal(sequencer.images.test.steps[6].options.name, "channel", "Correct Step Added");
t.end(); t.end();
}); });
test('removeSteps("image",position) removes a step', function (t) { test('removeSteps("image",position) removes a step', function (t) {
sequencer.removeSteps('test',1); sequencer.removeSteps('test', 1);
t.equal(sequencer.images.test.steps.length, 6, "Length of steps reduced"); t.equal(sequencer.images.test.steps.length, 6, "Length of steps reduced");
t.end(); t.end();
}); });
test('removeSteps({image: [positions]}) removes steps', function (t) { test('removeSteps({image: [positions]}) removes steps', function (t) {
sequencer.removeSteps('test',[1,2]); sequencer.removeSteps('test', [1, 2]);
t.equal(sequencer.images.test.steps.length, 4, "Length of steps reduced"); t.equal(sequencer.images.test.steps.length, 4, "Length of steps reduced");
t.end(); t.end();
}); });
test('removeSteps(position) removes steps', function (t) { test('removeSteps(position) removes steps', function (t) {
sequencer.removeSteps([1,2]); sequencer.removeSteps([1, 2]);
t.equal(sequencer.images.test.steps.length, 2, "Length of steps reduced"); t.equal(sequencer.images.test.steps.length, 2, "Length of steps reduced");
t.end(); t.end();
}); });
test('insertSteps("image",position,"module",options) inserts a step', function (t) { test('insertSteps("image",position,"module",options) inserts a step', function (t) {
sequencer.insertSteps('test',1,'channel',{}); sequencer.insertSteps('test', 1, 'channel', {});
t.equal(sequencer.images.test.steps.length, 3, "Length of Steps increased"); t.equal(sequencer.images.test.steps.length, 3, "Length of Steps increased");
t.equal(sequencer.images.test.steps[1].options.name, "channel", "Correct Step Inserted"); t.equal(sequencer.images.test.steps[1].options.name, "channel", "Correct Step Inserted");
t.end(); t.end();
}); });
test('insertSteps("image",position,"module") inserts a step', function (t) { test('insertSteps("image",position,"module") inserts a step', function (t) {
sequencer.insertSteps('test',1,'channel'); sequencer.insertSteps('test', 1, 'channel');
t.equal(sequencer.images.test.steps.length, 4, "Length of Steps increased"); t.equal(sequencer.images.test.steps.length, 4, "Length of Steps increased");
t.equal(sequencer.images.test.steps[1].options.name, "channel", "Correct Step Inserted"); t.equal(sequencer.images.test.steps[1].options.name, "channel", "Correct Step Inserted");
t.end(); t.end();
}); });
test('insertSteps(position,"module") inserts a step', function (t) { test('insertSteps(position,"module") inserts a step', function (t) {
sequencer.insertSteps(1,'channel'); sequencer.insertSteps(1, 'channel');
t.equal(sequencer.images.test.steps.length, 5, "Length of Steps increased"); t.equal(sequencer.images.test.steps.length, 5, "Length of Steps increased");
t.equal(sequencer.images.test.steps[1].options.name, "channel", "Correct Step Inserted"); t.equal(sequencer.images.test.steps[1].options.name, "channel", "Correct Step Inserted");
t.end(); t.end();
}); });
test('insertSteps({image: {index: index, name: "module", o: options} }) inserts a step', function (t) { test('insertSteps({image: {index: index, name: "module", o: options} }) inserts a step', function (t) {
sequencer.insertSteps({test: {index:1, name:'channel', o:{} } }); sequencer.insertSteps({ test: { index: 1, name: 'channel', o: {} } });
t.equal(sequencer.images.test.steps.length, 6, "Length of Steps increased"); t.equal(sequencer.images.test.steps.length, 6, "Length of Steps increased");
t.equal(sequencer.images.test.steps[1].options.name, "channel", "Correct Step Inserted"); t.equal(sequencer.images.test.steps[1].options.name, "channel", "Correct Step Inserted");
t.end(); t.end();
}); });
test('run() runs the sequencer and returns output to callback', function (t) { test('run() runs the sequencer and returns output to callback', function (t) {
sequencer.run('test',function(out){ sequencer.run('test', function (out) {
t.equal(typeof(sequencer.images.test.steps[sequencer.images.test.steps.length - 1].output), "object", "Output is Generated"); t.equal(typeof (sequencer.images.test.steps[sequencer.images.test.steps.length - 1].output), "object", "Output is Generated");
t.equal(out,sequencer.images.test.steps[sequencer.images.test.steps.length - 1].output.src, "Output callback works"); t.equal(out, sequencer.images.test.steps[sequencer.images.test.steps.length - 1].output.src, "Output callback works");
}); });
t.end(); t.end();
}); });
test('replaceImage returns false in NodeJS', function (t) { test('replaceImage returns false in NodeJS', function (t) {
var returnvalue = (sequencer.options.inBrowser)?false:sequencer.replaceImage("#selector","test"); var returnvalue = (sequencer.options.inBrowser) ? false : sequencer.replaceImage("#selector", "test");
t.equal(returnvalue,false,"It does."); t.equal(returnvalue, false, "It does.");
t.end(); t.end();
}); });
var global2 = copy(false,parent); var global2 = copy(false, parent);
test('No Global Variables Created', function (t) { test('No Global Variables Created', function (t) {
t.equal(0,global2,"None Created."); t.equal(0, global2, "None Created.");
t.end(); t.end();
}); });