Dynamic fix as fun (#207)

getNeighborPixel(x, y) method for Dynamic module
This commit is contained in:
Varun Gupta
2018-03-31 03:11:30 +05:30
committed by Jeffrey Warren
parent 0ceb36ffde
commit fba80bb151
2 changed files with 49 additions and 26 deletions

View File

@@ -1,22 +1,22 @@
module.exports = function Dynamic(options,UI) { module.exports = function Dynamic(options,UI) {
options = options || {}; options = options || {};
options.title = "Dynamic"; options.title = "Dynamic";
// Tell the UI that a step has been set up. // Tell the UI that a step has been set up.
UI.onSetup(options.step); UI.onSetup(options.step);
var output; var output;
// This function is called on every draw. // This function is called on every draw.
function draw(input,callback) { function draw(input,callback) {
// Tell the UI that the step is being drawn // Tell the UI that the step is being drawn
UI.onDraw(options.step); UI.onDraw(options.step);
var step = this; var step = this;
// start with monochrome, but if options.red, options.green, and options.blue are set, accept them too // start with monochrome, but if options.red, options.green, and options.blue are set, accept them too
options.monochrome = options.monochrome || "(R+G+B)/3"; options.monochrome = options.monochrome || "(R+G+B)/3";
function generator(expression) { function generator(expression) {
var func = 'f = function (r, g, b, a) { var R = r, G = g, B = b, A = a;' var func = 'f = function (r, g, b, a) { var R = r, G = g, B = b, A = a;'
func = func + 'return '; func = func + 'return ';
@@ -25,48 +25,65 @@ module.exports = function Dynamic(options,UI) {
eval(func); eval(func);
return f; return f;
} }
var channels = ['red', 'green', 'blue', 'alpha']; var channels = ['red', 'green', 'blue', 'alpha'];
channels.forEach(function(channel) { channels.forEach(function(channel) {
if (options.hasOwnProperty(channel)) options[channel + '_function'] = generator(options[channel]); if (options.hasOwnProperty(channel)) options[channel + '_function'] = generator(options[channel]);
else if (channel === 'alpha') options['alpha_function'] = function() { return 255; } else if (channel === 'alpha') options['alpha_function'] = function() { return 255; }
else options[channel + '_function'] = generator(options.monochrome); else options[channel + '_function'] = generator(options.monochrome);
}); });
function changePixel(r, g, b, a) { function changePixel(r, g, b, a) {
/* neighbourpixels can be calculated by
this.getNeighbourPixel.fun(x,y) or this.getNeighborPixel.fun(x,y)
*/
var combined = (r + g + b) / 3.000; var combined = (r + g + b) / 3.000;
return [ return [
options.red_function( r, g, b, a), options.red_function(r, g, b, a),
options.green_function(r, g, b, a), options.green_function(r, g, b, a),
options.blue_function( r, g, b, a), options.blue_function(r, g, b, a),
options.alpha_function(r, g, b, a), options.alpha_function(r, g, b, a),
]; ];
} }
/* Functions to get the neighbouring pixel by position (x,y) */
function getNeighbourPixel(pixels,curX,curY,distX,distY){
return [
pixels.get(curX+distX,curY+distY,0)
,pixels.get(curX+distX,curY+distY,1)
,pixels.get(curX+distX,curY+distY,2)
,pixels.get(curX+distX,curY+distY,3)
]
}
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 the UI // This output is accessible by the UI
options.step.output = datauri; options.step.output = datauri;
// Tell the UI that the draw is complete // Tell the UI that the draw is complete
UI.onComplete(options.step); UI.onComplete(options.step);
} }
return require('../_nomodule/PixelManipulation.js')(input, { return require('../_nomodule/PixelManipulation.js')(input, {
output: output, output: output,
changePixel: changePixel, changePixel: changePixel,
getNeighbourPixel: getNeighbourPixel,
getNeighborPixel: getNeighbourPixel,
format: input.format, format: input.format,
image: options.image, image: options.image,
inBrowser: options.inBrowser, inBrowser: options.inBrowser,
callback: callback callback: callback
}); });
} }
return { return {
options: options, options: options,
draw: draw, draw: draw,

View File

@@ -21,6 +21,12 @@ module.exports = function PixelManipulation(image, options) {
console.log('Bad image path'); console.log('Bad image path');
return; return;
} }
if(options.getNeighbourPixel){
options.getNeighbourPixel.fun = function (distX,distY) {
return options.getNeighbourPixel(pixels,x,y,distX,distY);
};
}
// iterate through pixels; // iterate through pixels;
// this could possibly be more efficient; see // this could possibly be more efficient; see
@@ -31,13 +37,13 @@ module.exports = function PixelManipulation(image, options) {
for(var x = 0; x < pixels.shape[0]; x++) { for(var x = 0; x < pixels.shape[0]; x++) {
for(var y = 0; y < pixels.shape[1]; y++) { for(var y = 0; y < pixels.shape[1]; y++) {
var pixel = options.changePixel( var pixel = options.changePixel(
pixels.get(x, y, 0), pixels.get(x, y, 0),
pixels.get(x, y, 1), pixels.get(x, y, 1),
pixels.get(x, y, 2), pixels.get(x, y, 2),
pixels.get(x, y, 3) pixels.get(x, y, 3)
); );
pixels.set(x, y, 0, pixel[0]); pixels.set(x, y, 0, pixel[0]);
pixels.set(x, y, 1, pixel[1]); pixels.set(x, y, 1, pixel[1]);
@@ -48,9 +54,9 @@ module.exports = function PixelManipulation(image, options) {
pace.op() pace.op()
} }
} }
if(options.extraManipulation) if(options.extraManipulation)
pixels = options.extraManipulation(pixels) pixels = options.extraManipulation(pixels)
// there may be a more efficient means to encode an image object, // there may be a more efficient means to encode an image object,
// but node modules and their documentation are essentially arcane on this point // but node modules and their documentation are essentially arcane on this point