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 arbitrary crop module #1607

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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 @@ -3,6 +3,7 @@
*/
module.exports = {
'add-qr': require('./modules/AddQR'),
'arbitrary-crop': require('./modules/ArbitraryCrop'),
'average': require('./modules/Average'),
'blend': require('./modules/Blend'),
'blob-analysis': require('./modules/BlobAnalysis'),
Expand Down
104 changes: 104 additions & 0 deletions src/modules/ArbitraryCrop/Module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
module.exports = function ArbitraryCrop(options, UI, util) {
var defaults = require('../../util/getDefaults.js')(require('./info.json'));

options.x1 = options.x1 || defaults.x1;
options.y1 = options.y1 || defaults.y1;
options.x2 = options.x2 || defaults.x2;
options.y2 = options.y2 || defaults.y2;
options.x3 = options.x3 || defaults.x3;
options.y3 = options.y3 || defaults.y3;
options.x4 = options.x4 || defaults.x4;
options.y4 = options.y4 || defaults.y4;

var output;

// This function is called on every draw.
function draw(input, callback, progressObj) {
progressObj.stop(true);
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need these? I remember taking out the progress code..

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't exactly know, should I remove this?

progressObj.overrideFlag = true;

var step = this;

var x1 = options.x1,
y1 = options.y1,
x2 = options.x2,
y2 = options.y2,
x3 = options.x3,
y3 = options.y3,
x4 = options.x4,
y4 = options.y4;

// Equation of line l1.
function l1(x, y) {
var d = (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1);
return d;
}

// Equation of line l2.
function l2(x, y) {
var d = (x - x2) * (y4 - y2) - (y - y2) * (x4 - x2);
return d;
}

// Equation of line l3.
function l3(x, y) {
var d = (x - x3) * (y4 - y3) - (y - y3) * (x4 - x3);
return d;
}

// Equation of line l4.
function l4(x, y) {
var d = (x - x1) * (y3 - y1) - (y - y1) * (x3 - x1);
return d;
}

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

if(l1(x, y) * l3(x, y) <= 0 && l2(x, y) * l4(x, y) <= 0) return [r, g, b, a];
Copy link
Member

Choose a reason for hiding this comment

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

Hey is this the formula that determines whether the point is inside the quadrilateral?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes


else return [r, g, b, 0];
}

function extraManipulation(pixels) {

x = Math.min(x1, x2, x3, x4);
y = Math.min(y1, y2, y3, y4);

w = Math.max(x1, x2, x3, x4);
h = Math.max(y1, y2, y3, y4);

newPixels = require('../Crop/Crop')(pixels, {x, y, w, h});

return newPixels;

}

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

return require('../_nomodule/PixelManipulation.js')(input, {
output: output,
ui: options.step.ui, //don't pass this in if you don't want your module to support progress bars
changePixel: changePixel,
format: input.format,
image: options.image,
inBrowser: options.inBrowser,
callback: callback,
useWasm: options.useWasm,
extraManipulation
});
}

return {
options: options,
draw: draw,
output: output,
UI: UI
};
};
4 changes: 4 additions & 0 deletions src/modules/ArbitraryCrop/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = [
require('./Module'),
require('./info.json')
];
48 changes: 48 additions & 0 deletions src/modules/ArbitraryCrop/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "arbitrary-crop",
"description": "Crops image for any arbitrary quadilateral according to given values, where (x1, y1) are top-left cordinates, (x2, y2) are top-right cordinates, (x3, y3) are bottom-right cordinates, (x4, y4) are bottom-left cordinates",
"url": "https://github.com/publiclab/image-sequencer/tree/master/MODULES.md",
"inputs": {
"x1": {
"type": "integer",
"desc": "Cordinate x1 of quadilateral",
"default": 100
},
"y1": {
"type": "integer",
"desc": "Cordinate y1 of quadilateral",
"default": 100
},
"x2": {
"type": "integer",
"desc": "Cordinate x2 of quadilateral",
"default": 200
},
"y2": {
"type": "integer",
"desc": "Cordinate y2 of quadilateral",
"default": 100
},
"x3": {
"type": "integer",
"desc": "Cordinate x3 of quadilateral",
"default": 100
},
"y3": {
"type": "integer",
"desc": "Cordinate y3 of quadilateral",
"default": 200
},
"x4": {
"type": "integer",
"desc": "Cordinate x4 of quadilateral",
"default": 200
},
"y4": {
"type": "integer",
"desc": "Cordinate y4 of quadilateral",
"default": 200
}
},
"docs-link": "https://github.com/publiclab/image-sequencer/blob/main/docs/MODULES.md"
}