Skip to content

Commit

Permalink
Add configurable parameter for blend module to chose image (#424)
Browse files Browse the repository at this point in the history
* Add configurable parameter for blend module to chose image

* Made minor changes

* Edit test to work with new parameter

* blend offset test added

* blend offset test added

* fixed blend offset test
  • Loading branch information
KusioDev authored and jywarren committed Oct 23, 2018
1 parent 11b6560 commit e5e372d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 48 deletions.
96 changes: 50 additions & 46 deletions src/modules/Blend/Module.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
module.exports = function Dynamic(options, UI, util) {

options.func = options.func || "function(r1, g1, b1, a1, r2, g2, b2, a2) { return [ r1, g2, b2, a2 ] }";
options.func = options.func || "function(r1, g1, b1, a1, r2, g2, b2, a2) { return [ r1, g2, b2, a2 ] }";
options.offset = options.offset || -2;

var output;
var output;

// This function is called on every draw.
function draw(input, callback, progressObj) {
// This function is called on every draw.
function draw(input, callback, progressObj) {

progressObj.stop(true);
progressObj.overrideFlag = true;
progressObj.stop(true);
progressObj.overrideFlag = true;

var step = this;
var step = this;

// convert to runnable code:
if (typeof options.func === "string") eval('options.func = ' + options.func);
// convert to runnable code:
if (typeof options.func === "string") eval('options.func = ' + options.func);

var getPixels = require('get-pixels');
var getPixels = require('get-pixels');

// save first image's pixels
var priorStep = this.getStep(-2);
// convert offset as string to int
if(typeof options.offset === "string") options.offset = parseInt(options.offset);

getPixels(priorStep.output.src, function(err, pixels) {
options.firstImagePixels = pixels;
// save first image's pixels
var priorStep = this.getStep(options.offset);

function changePixel(r2, g2, b2, a2, x, y) {
// blend!
var p = options.firstImagePixels;
return options.func(
r2, g2, b2, a2,
p.get(x, y, 0),
p.get(x, y, 1),
p.get(x, y, 2),
p.get(x, y, 3)
)
}
getPixels(priorStep.output.src, function(err, pixels) {
options.firstImagePixels = pixels;

function output(image, datauri, mimetype) {
function changePixel(r2, g2, b2, a2, x, y) {
// blend!
var p = options.firstImagePixels;
return options.func(
r2, g2, b2, a2,
p.get(x, y, 0),
p.get(x, y, 1),
p.get(x, y, 2),
p.get(x, y, 3)
)
}

// This output is accessible by Image Sequencer
step.output = { src: datauri, format: mimetype };
function output(image, datauri, mimetype) {

}
// This output is accessible by Image Sequencer
step.output = { src: datauri, format: mimetype };

// run PixelManipulatin on second image's pixels
return require('../_nomodule/PixelManipulation.js')(input, {
}

// run PixelManipulatin on second image's pixels
return require('../_nomodule/PixelManipulation.js')(input, {
output: output,
changePixel: changePixel,
format: input.format,
image: options.image,
inBrowser: options.inBrowser,
callback: callback
});
});
}

return {
options: options,
draw: draw,
output: output,
changePixel: changePixel,
format: input.format,
image: options.image,
inBrowser: options.inBrowser,
callback: callback
});
});
}

return {
options: options,
draw: draw,
output: output,
UI: UI
}
UI: UI
}
}
7 changes: 6 additions & 1 deletion src/modules/Blend/info.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"name": "Blend",
"description": "Blend the past two image steps with the given function. Defaults to using the red channel from image 1 and the green and blue and alpha channels of image 2. Easier to use interfaces coming soon!",
"description": "Blend two chosen image steps with the given function. Defaults to using the red channel from image 1 and the green and blue and alpha channels of image 2. Easier to use interfaces coming soon!",
"inputs": {
"offset": {
"type": "integer",
"desc": "Choose which image to blend the current image with. Two steps back is -2, three steps back is -3 etc.",
"default": -2
},
"blend": {
"type": "input",
"desc": "Function to use to blend the two images.",
Expand Down
15 changes: 14 additions & 1 deletion test/modules/image-sequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,23 @@ test('getStep(offset) returns the step at offset distance relative to current st
});

test('toCliString() returns the CLI command for the sequence', function(t) {
t.deepEqual(sequencer.toCliString(), `sequencer -i [PATH] -s "channel channel channel channel channel invert blend" -d '{"channel":"green"}'`, "works correctly");
t.deepEqual(sequencer.toCliString(), `sequencer -i [PATH] -s "channel channel channel channel channel invert blend" -d '{"channel":"green","offset":-2}'`, "works correctly");
t.end();
});

test('blend returns different output depending on the set offset', function(t) {
var blend_2;
sequencer.addSteps('test', 'invert', {});
sequencer.addSteps('test', 'invert', {});
sequencer.addSteps('test', 'blend', {});
// because we've added blend before, so instead of -3 we set it to -4
sequencer.addSteps('test', 'blend', {'offset': -4});
sequencer.run({ mode: 'test' }, function(out) {
t.notStrictEqual(out, sequencer.images.test.steps[sequencer.images.test.steps.length - 2].output.src, 'different offsets give different output');
t.end();
});
});

test('replaceImage returns false in NodeJS', function(t) {
var returnvalue = (sequencer.options.inBrowser) ? false : sequencer.replaceImage("#selector", "test");
t.equal(returnvalue, false, "It does.");
Expand Down

0 comments on commit e5e372d

Please sign in to comment.