Skip to content

Commit

Permalink
Run optimize (#275)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
tech4GT authored and jywarren committed Jun 8, 2018
1 parent a0f7e6c commit 10c6ad5
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 42 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 20 additions & 10 deletions dist/image-sequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion dist/image-sequencer.min.js

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions examples/lib/defaultHtmlSequencerUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function DefaultHtmlSequencerUi(_sequencer, options) {

if (hash) {
_sequencer.importString(hash);
_sequencer.run();
_sequencer.run({index:0});
}
setUrlHashParameter("steps", sequencer.toString());
}
Expand All @@ -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());
}
Expand All @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion examples/lib/defaultHtmlStepUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -62,4 +62,4 @@
"bin": {
"sequencer": "./index.js"
}
}
}
26 changes: 18 additions & 8 deletions src/ImageSequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
});
Expand Down
4 changes: 2 additions & 2 deletions src/Run.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
12 changes: 5 additions & 7 deletions test/modules/image-manip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down Expand Up @@ -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();
});
})
Expand All @@ -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();
});
Expand All @@ -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)
});
4 changes: 2 additions & 2 deletions test/modules/image-sequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});
Expand Down
29 changes: 29 additions & 0 deletions test/modules/run.js
Original file line number Diff line number Diff line change
@@ -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();
})
});
});
});

0 comments on commit 10c6ad5

Please sign in to comment.