Run optimize (#275)

* fixes #269

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

* update demo

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

* refactor and comments

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

* update readme

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

* updated and refactored

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

* update readme

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

* trying to fix tests

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

* update documentation

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

* update to config.mode = test

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

* finaly everything works

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

* 1.8.0

* update test

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

* add test for run

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>
This commit is contained in:
Varun Gupta
2018-06-08 23:43:41 +05:30
committed by Jeffrey Warren
parent a0f7e6c766
commit 10c6ad5865
13 changed files with 118 additions and 42 deletions

View File

@@ -204,13 +204,35 @@ modules.
sequencer.run();
```
Additionally, an optional callback can be passed to this method.
Sequencer can be run with a custom config object
```js
sequencer.run(function(out){
// The config object enables custom progress bars in node environment and
// ability to run the sequencer from a particular index(of the steps array)
sequencer.run(config);
```
The config object can have the following keys
```js
config: {
progressObj: , //A custom object to handle progress bar
index: //Index to run the sequencer from (defaults to 0)
}
```
Additionally, an optional callback function can be passed to this method.
```js
sequencer.run(function callback(out){
// this gets called back.
// "out" is the DataURL of the final image.
});
sequencer.run(config,function callback(out){
// the callback is supported with all types of invocations
});
```
return value: **`sequencer`** (To allow method chaining)

View File

@@ -47644,11 +47644,21 @@ ImageSequencer = function ImageSequencer(options) {
return this;
}
function run(spinnerObj,t_image,t_from) {
let progressObj;
if(arguments[0] != 'test'){
progressObj = spinnerObj
delete arguments['0']
// Config is an object which contains the runtime configuration like progress bar
// information and index from which the sequencer should run
function run(config,t_image,t_from) {
let progressObj,index=0;
config = config || {mode: 'no-arg'};
if(config.index) index = config.index;
if(config.mode != 'test'){
if(config.mode != "no-arg" && typeof config != 'function'){
if(config.progressObj) progressObj = config.progressObj;
delete arguments['0'];
}
}
else{
arguments['0'] = config.mode;
}
var this_ = (this.name == "ImageSequencer")?this:this.sequencer;
@@ -47662,7 +47672,7 @@ ImageSequencer = function ImageSequencer(options) {
var json_q = formatInput.call(this_,args,"r");
require('./Run')(this_, json_q, callback,progressObj);
require('./Run')(this_, json_q, callback,index,progressObj);
return true;
}
@@ -47794,7 +47804,7 @@ ImageSequencer = function ImageSequencer(options) {
function importString(str){
let sequencer = this;
if(this.name != "ImageSequencer")
sequencer = this.sequencer;
sequencer = this.sequencer;
var stepsFromString = stringToJSON(str);
stepsFromString.forEach(function eachStep(stepObj) {
sequencer.addSteps(stepObj.name,stepObj.options);
@@ -47805,7 +47815,7 @@ ImageSequencer = function ImageSequencer(options) {
function importJSON(obj){
let sequencer = this;
if(this.name != "ImageSequencer")
sequencer = this.sequencer;
sequencer = this.sequencer;
obj.forEach(function eachStep(stepObj) {
sequencer.addSteps(stepObj.name,stepObj.options);
});
@@ -48003,7 +48013,7 @@ module.exports = ReplaceImage;
},{}],142:[function(require,module,exports){
const getStepUtils = require('./util/getStep.js');
function Run(ref, json_q, callback, progressObj) {
function Run(ref, json_q, callback,ind, progressObj) {
if (!progressObj) progressObj = { stop: function () { } };
function drawStep(drawarray, pos) {
@@ -48056,7 +48066,7 @@ function Run(ref, json_q, callback, progressObj) {
drawarray.push({ image: image, i: init + i });
}
}
drawStep(drawarray, 0);
drawStep(drawarray, ind);
}
function filter(json_q) {

File diff suppressed because one or more lines are too long

View File

@@ -15,7 +15,7 @@ function DefaultHtmlSequencerUi(_sequencer, options) {
if (hash) {
_sequencer.importString(hash);
_sequencer.run();
_sequencer.run({index:0});
}
setUrlHashParameter("steps", sequencer.toString());
}
@@ -27,7 +27,7 @@ function DefaultHtmlSequencerUi(_sequencer, options) {
function removeStepUi() {
var index = $(removeStepSel).index(this) + 1;
sequencer.removeSteps(index).run();
sequencer.removeSteps(index).run({index : sequencer.images.image1.steps.length-1});
// remove from URL hash too
setUrlHashParameter("steps", sequencer.toString());
}
@@ -36,9 +36,15 @@ function DefaultHtmlSequencerUi(_sequencer, options) {
if ($(addStepSel + " select").val() == "none") return;
var newStepName = $(addStepSel + " select").val();
/*
* after adding the step we run the sequencer from defined step
* and since loadImage is not a part of the drawarray the step lies at current
* length - 2 of the drawarray
*/
_sequencer
.addSteps(newStepName, options)
.run(null);
.run({index: _sequencer.images.image1.steps.length - 2});
// add to URL hash too
setUrlHashParameter("steps", _sequencer.toString());

View File

@@ -101,7 +101,8 @@ function DefaultHtmlStepUi(_sequencer, options) {
.each(function(i, input) {
step.options[$(input).attr("name")] = input.value;
});
_sequencer.run();
_sequencer.run({index: _sequencer.images.image1.steps.length - 2});
// modify the url hash
setUrlHashParameter("steps", _sequencer.toString());
}

View File

@@ -131,7 +131,7 @@ sequencer.loadImages(program.image, function() {
spinnerObj = Spinner("Your Image is being processed..").start();
// Run the sequencer.
sequencer.run(spinnerObj, function() {
sequencer.run({progressObj: spinnerObj}, function() {
// Export all images or final image as binary files.
sequencer.exportBin(program.output, program.basic);

View File

@@ -1,11 +1,11 @@
{
"name": "image-sequencer",
"version": "1.7.0",
"version": "1.8.0",
"description": "A modular JavaScript image manipulation library modeled on a storyboard.",
"main": "src/ImageSequencer.js",
"scripts": {
"debug": "TEST=true node ./index.js -i ./examples/images/monarch.png -s invert",
"test": "TEST=true tape test/**/*.js test/*.js | tap-spec; browserify test/modules/image-sequencer.js test/modules/chain.js test/modules/replace.js test/modules/import-export.js | tape-run --render=\"tap-spec\"",
"test": "TEST=true tape test/**/*.js test/*.js | tap-spec; browserify test/modules/image-sequencer.js test/modules/chain.js test/modules/replace.js test/modules/import-export.js test/modules/run.js | tape-run --render=\"tap-spec\"",
"start": "grunt serve"
},
"repository": {

View File

@@ -115,11 +115,21 @@ ImageSequencer = function ImageSequencer(options) {
return this;
}
function run(spinnerObj,t_image,t_from) {
let progressObj;
if(arguments[0] != 'test'){
progressObj = spinnerObj
delete arguments['0']
// Config is an object which contains the runtime configuration like progress bar
// information and index from which the sequencer should run
function run(config,t_image,t_from) {
let progressObj,index=0;
config = config || {mode: 'no-arg'};
if(config.index) index = config.index;
if(config.mode != 'test'){
if(config.mode != "no-arg" && typeof config != 'function'){
if(config.progressObj) progressObj = config.progressObj;
delete arguments['0'];
}
}
else{
arguments['0'] = config.mode;
}
var this_ = (this.name == "ImageSequencer")?this:this.sequencer;
@@ -133,7 +143,7 @@ ImageSequencer = function ImageSequencer(options) {
var json_q = formatInput.call(this_,args,"r");
require('./Run')(this_, json_q, callback,progressObj);
require('./Run')(this_, json_q, callback,index,progressObj);
return true;
}
@@ -265,7 +275,7 @@ ImageSequencer = function ImageSequencer(options) {
function importString(str){
let sequencer = this;
if(this.name != "ImageSequencer")
sequencer = this.sequencer;
sequencer = this.sequencer;
var stepsFromString = stringToJSON(str);
stepsFromString.forEach(function eachStep(stepObj) {
sequencer.addSteps(stepObj.name,stepObj.options);
@@ -276,7 +286,7 @@ ImageSequencer = function ImageSequencer(options) {
function importJSON(obj){
let sequencer = this;
if(this.name != "ImageSequencer")
sequencer = this.sequencer;
sequencer = this.sequencer;
obj.forEach(function eachStep(stepObj) {
sequencer.addSteps(stepObj.name,stepObj.options);
});

View File

@@ -1,6 +1,6 @@
const getStepUtils = require('./util/getStep.js');
function Run(ref, json_q, callback, progressObj) {
function Run(ref, json_q, callback,ind, progressObj) {
if (!progressObj) progressObj = { stop: function () { } };
function drawStep(drawarray, pos) {
@@ -53,7 +53,7 @@ function Run(ref, json_q, callback, progressObj) {
drawarray.push({ image: image, i: init + i });
}
}
drawStep(drawarray, 0);
drawStep(drawarray, ind);
}
function filter(json_q) {

View File

@@ -6,8 +6,8 @@ const test = require('tape');
test('Output directory is correctly generated',function(t){
cliUtils.makedir('./output/',function(){
require('fs').access('./output/.',function(err){
t.true(!err,"Access the created dir")
t.end()
t.true(!err,"Access the created dir");
t.end();
});
});
});

View File

@@ -15,13 +15,12 @@ var sequencer = ImageSequencer({ ui: false });
var qr = require('./images/IS-QR.js');
var test_png = require('./images/test.png.js');
var test_gif = require('./images/test.gif.js');
var spinner = require('ora')('').start()
sequencer.loadImages(test_png);
sequencer.addSteps(['invert','invert']);
test("Preload", function(t) {
sequencer.run(spinner,function(){
sequencer.run({mode:'test'},function(){
t.end();
});
});
@@ -52,7 +51,7 @@ test("Twice inverted image is identical to original image", function (t) {
test("Decode QR module works properly :: setup", function (t) {
sequencer.loadImage(qr,function(){
this.addSteps('decode-qr').run(spinner.start(),function(){
this.addSteps('decode-qr').run({mode:'test'},function(){
t.end();
});
})
@@ -65,7 +64,7 @@ test("Decode QR module works properly :: teardown", function (t) {
test("PixelManipulation works for PNG images", function (t) {
sequencer.loadImages(test_png,function(){
this.addSteps('invert').run(spinner.start(),function(out){
this.addSteps('invert').run({mode:'test'},function(out){
t.equal(1,1)
t.end();
});
@@ -74,10 +73,9 @@ test("PixelManipulation works for PNG images", function (t) {
test("PixelManipulation works for GIF images", function (t) {
sequencer.loadImages(test_gif,function(){
this.addSteps('invert').run(spinner,function(out){
this.addSteps('invert').run({mode:'test'},function(out){
t.equal(1,1)
t.end();
});
});
});
spinner.stop(true)

View File

@@ -166,7 +166,7 @@ test('insertSteps({image: {index: index, name: "module", o: options} }) inserts
test('run() runs the sequencer and returns output to callback', function (t) {
sequencer.run('test', function (out) {
sequencer.run({mode:'test'}, function (out) {
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.end();
@@ -177,7 +177,7 @@ test('run() runs the sequencer and returns output to callback', function (t) {
test('getStep(offset) returns the step at offset distance relative to current step',function(t){
sequencer.addSteps('test','invert',{});
sequencer.addSteps('test','blend',{});
sequencer.run('test',function(out){
sequencer.run({mode:'test'},function(out){
t.equal(!!out,true,"Blend generates output");
t.end();
});

29
test/modules/run.js Normal file
View File

@@ -0,0 +1,29 @@
'use strict';
var fs = require('fs');
var test = require('tape');
var DataURItoBuffer = require('data-uri-to-buffer');
require('../../src/ImageSequencer.js');
var sequencer = ImageSequencer({ ui: false });
var red = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAAQABADASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAABgj/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwCdABykX//Z";
sequencer.loadImages('image1', red);
sequencer.addSteps('invert');
sequencer.addSteps('invert');
sequencer.addSteps('invert');
test('run() works with all possible argument combinations',function(t){
sequencer.run(function (out) {
var output1 = DataURItoBuffer(sequencer.images.image1.steps.slice(-1)[0].output.src);
sequencer.images.image1.steps.splice(1,1);
sequencer.run({index: 1},function(out){
var output2 = DataURItoBuffer(sequencer.images.image1.steps.slice(-1)[0].output.src);
t.deepEqual(output1,output2,"output remains same after removing a step and running sequencer from a greater index");
sequencer.run(function(out){
t.end();
})
});
});
});