Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getNeighbourPixel for dynamic module #204

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions src/modules/Dynamic/Module.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
module.exports = function Dynamic(options,UI) {

options = options || {};
options.title = "Dynamic";

// Tell the UI that a step has been set up.
UI.onSetup(options.step);
var output;

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

// Tell the UI that the step is being drawn
UI.onDraw(options.step);
var step = this;

// 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";

function generator(expression) {
var func = 'f = function (r, g, b, a) { var R = r, G = g, B = b, A = a;'
func = func + 'return ';
Expand All @@ -25,48 +25,66 @@ module.exports = function Dynamic(options,UI) {
eval(func);
return f;
}

var channels = ['red', 'green', 'blue', 'alpha'];

channels.forEach(function(channel) {
if (options.hasOwnProperty(channel)) options[channel + '_function'] = generator(options[channel]);
else if (channel === 'alpha') options['alpha_function'] = function() { return 255; }
else options[channel + '_function'] = generator(options.monochrome);
});

function changePixel(r, g, b, a) {

/* neighbourpixels can be calculated by

this.getNeighbourPixel.fun(x,y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, just one more! could we go with the American English spelling of Neighbor here? OR alias it so we can use both? 👍

And just curious, sorry if it's a silly question but why does getNeighborPixel have to be an object -- can't it just be getNeighborPixel()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jywarren actually i have made it an object because pixelmanipulation remaps the options.getNeighbourpixel.fun to a new function which internally calls the original getneighbourpixels with the value of pixels it gets by closure. This remapping won't be possible if it's a function since we can't just say

 options.getneighbourspixel = something else

And it feels kind of natural to use the same function defined in the same file
To avoid this we have 2 options either we move all the logic inside pixelmanipulation or use a different name than getneighbourpixels for actual function that will be called

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jywarren i just opened another pull request doing it have getNeighbourPixel as function, #207 you can merge that if that looks more natural to you, thanks😁

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I left a comment on #207 -- that does look good!


*/
var combined = (r + g + b) / 3.000;
return [
options.red_function( r, g, b, a),
options.red_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),
];
}


/* Function to get the neighbouring pixel by position (x,y) */
var getNeighbourPixel = {}
getNeighbourPixel.fun = function(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){

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

// This output is accessible by the UI
options.step.output = datauri;

// Tell the UI that the draw is complete
UI.onComplete(options.step);

}
return require('../_nomodule/PixelManipulation.js')(input, {
output: output,
changePixel: changePixel,
getNeighbourPixel: getNeighbourPixel,
format: input.format,
image: options.image,
inBrowser: options.inBrowser,
callback: callback
});

}

return {
options: options,
draw: draw,
Expand Down
25 changes: 17 additions & 8 deletions src/modules/_nomodule/PixelManipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ module.exports = function PixelManipulation(image, options) {
console.log('Bad image path');
return;
}

var getNeighbourPixel;

if(options.getNeighbourPixel){
getNeighbourPixel = options.getNeighbourPixel.fun
options.getNeighbourPixel.fun = function (distX,distY) {
return getNeighbourPixel(pixels,x,y,distX,distY);
}
}

// iterate through pixels;
// this could possibly be more efficient; see
Expand All @@ -31,13 +40,13 @@ module.exports = function PixelManipulation(image, options) {

for(var x = 0; x < pixels.shape[0]; x++) {
for(var y = 0; y < pixels.shape[1]; y++) {

var pixel = options.changePixel(
pixels.get(x, y, 0),
pixels.get(x, y, 1),
pixels.get(x, y, 2),
pixels.get(x, y, 3)
);
pixels.get(x, y, 0),
pixels.get(x, y, 1),
pixels.get(x, y, 2),
pixels.get(x, y, 3)
);

pixels.set(x, y, 0, pixel[0]);
pixels.set(x, y, 1, pixel[1]);
Expand All @@ -48,9 +57,9 @@ module.exports = function PixelManipulation(image, options) {
pace.op()
}
}

if(options.extraManipulation)
pixels = options.extraManipulation(pixels)
pixels = options.extraManipulation(pixels)

// there may be a more efficient means to encode an image object,
// but node modules and their documentation are essentially arcane on this point
Expand Down