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

Alpha masking module #1546

Merged
merged 17 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src/Modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
'gradient': require('./modules/Gradient'),
'grid-overlay': require('./modules/GridOverlay'),
'import-image': require('./modules/ImportImage'),
'mask': require('./modules/Mask'),
'minify-image': require('./modules/MinifyImage'),
// 'invert': require('image-sequencer-invert'),
'invert': require('./modules/Invert'),
Expand Down
82 changes: 82 additions & 0 deletions src/modules/Mask/Module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module.exports = function Mask(options, UI, util) {
var defaults = require('./../../util/getDefaults.js')(require('./info.json'));
options.offset = options.offset || defaults.offset;
options.resize = options.resize || defaults.resize;

var output;

// This function is called on every draw.
function draw(input, callback, progressObj) {
progressObj.stop(true);
progressObj.overrideFlag = true;

var step = this;

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

// convert offset as string to int
if (typeof options.offset === 'string')
options.offset = parseInt(options.offset);

// save first image's pixels
var priorStep = this.getStep(options.offset);

if (priorStep.output === undefined) {
this.output = input;
UI.notify('Offset Unavailable', 'offset-notification');
callback();
}

const alpha_masking = function(c, a) {
return (a * c + (255 - a) * c) / 255;
};
const internalSequencer = ImageSequencer({ inBrowser: false, ui: false });
internalSequencer.loadImage(priorStep.output.src, function() {
internalSequencer.importJSON([{ 'name': 'resize', 'options': { resize: options.resize } }]);
rishabhshuklax marked this conversation as resolved.
Show resolved Hide resolved
internalSequencer.run(function onCallback(internalOutput) {

getPixels(internalOutput, function(err, pixels) {
options.firstImagePixels = pixels;

function changePixel(r2, g2, b2, a2, x, y) {
let p = options.firstImagePixels;
let r1 = p.get(x, y, 0),
g1 = p.get(x, y, 1),
b1 = p.get(x, y, 2),
a1 = p.get(x, y, 3);

return [alpha_masking(r1, a2), alpha_masking(g1, a2), alpha_masking(b1, a2)];
}

function output(image, datauri, mimetype, wasmSuccess) {
step.output = {
src: datauri,
format: mimetype,
wasmSuccess,
useWasm: options.useWasm
};
}

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

return {
options: options,
draw: draw,
output: output,
UI: UI
};
};
4 changes: 4 additions & 0 deletions src/modules/Mask/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = [
require('./Module'),
require('./info.json')
];
18 changes: 18 additions & 0 deletions src/modules/Mask/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
Copy link
Member

Choose a reason for hiding this comment

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

Really nicely done. Well formatted, readable, excellent work!

"name": "mask",
"description": "Masks two images according to their Alpha values",
"inputs": {
"offset": {
"type": "integer",
"desc": "Choose which image to mask the current image with. Two steps back is -2, three steps back is -3 etc.",
"default": -2
},
"resize": {
"type": "string",
"desc": "Percentage value by which first image is to be resized",
"default": "125%"
}
},
"docs-link":"https://github.com/publiclab/image-sequencer/blob/main/docs/MODULES.md"
}

40 changes: 40 additions & 0 deletions test/core/modules/mask.js

Large diffs are not rendered by default.