From 9df0d1b46f1e65f3e0a355178ef470f5990d6391 Mon Sep 17 00:00:00 2001 From: blurry-x-face Date: Sat, 1 Feb 2020 16:56:42 +0530 Subject: [PATCH 1/3] add arbitrary crop --- src/Modules.js | 1 + src/modules/ArbitraryCrop/Module.js | 89 +++++++++++++++++++++++++++++ src/modules/ArbitraryCrop/index.js | 4 ++ src/modules/ArbitraryCrop/info.json | 48 ++++++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 src/modules/ArbitraryCrop/Module.js create mode 100644 src/modules/ArbitraryCrop/index.js create mode 100644 src/modules/ArbitraryCrop/info.json diff --git a/src/Modules.js b/src/Modules.js index 6a00ab6dfd..787926e813 100644 --- a/src/Modules.js +++ b/src/Modules.js @@ -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'), diff --git a/src/modules/ArbitraryCrop/Module.js b/src/modules/ArbitraryCrop/Module.js new file mode 100644 index 0000000000..a79fac8389 --- /dev/null +++ b/src/modules/ArbitraryCrop/Module.js @@ -0,0 +1,89 @@ +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); + 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]; + + else return [r, g, b, 0]; + } + + 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 + }); + } + + return { + options: options, + draw: draw, + output: output, + UI: UI + }; +}; diff --git a/src/modules/ArbitraryCrop/index.js b/src/modules/ArbitraryCrop/index.js new file mode 100644 index 0000000000..71549002ce --- /dev/null +++ b/src/modules/ArbitraryCrop/index.js @@ -0,0 +1,4 @@ +module.exports = [ + require('./Module'), + require('./info.json') +]; \ No newline at end of file diff --git a/src/modules/ArbitraryCrop/info.json b/src/modules/ArbitraryCrop/info.json new file mode 100644 index 0000000000..eb9c452ada --- /dev/null +++ b/src/modules/ArbitraryCrop/info.json @@ -0,0 +1,48 @@ +{ + "name": "arbitrary-crop", + "description": "Crops image for any arbitrary quadilateral", + "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" +} From 98e320b349161b63c421a2b9b48bc61c4a37e7f0 Mon Sep 17 00:00:00 2001 From: blurry-x-face Date: Sat, 1 Feb 2020 22:11:52 +0530 Subject: [PATCH 2/3] added functionality to crop the image to remove redundand pixels --- src/modules/ArbitraryCrop/Module.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/modules/ArbitraryCrop/Module.js b/src/modules/ArbitraryCrop/Module.js index a79fac8389..d6e352ec30 100644 --- a/src/modules/ArbitraryCrop/Module.js +++ b/src/modules/ArbitraryCrop/Module.js @@ -59,6 +59,22 @@ module.exports = function ArbitraryCrop(options, UI, util) { 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}); + + require('../../util/getDataUri')(newPixels, input.format).then(res => console.log(res)); + + return newPixels; + + } + function output(image, datauri, mimetype, wasmSuccess) { step.output = { src: datauri, @@ -76,7 +92,8 @@ module.exports = function ArbitraryCrop(options, UI, util) { image: options.image, inBrowser: options.inBrowser, callback: callback, - useWasm: options.useWasm + useWasm: options.useWasm, + extraManipulation }); } From 8c7252a1e02b31a5c029ec56b7590a1f43831198 Mon Sep 17 00:00:00 2001 From: blurry-x-face Date: Sat, 1 Feb 2020 22:24:34 +0530 Subject: [PATCH 3/3] add description for module --- src/modules/ArbitraryCrop/Module.js | 2 -- src/modules/ArbitraryCrop/info.json | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/modules/ArbitraryCrop/Module.js b/src/modules/ArbitraryCrop/Module.js index d6e352ec30..ad048a6f0c 100644 --- a/src/modules/ArbitraryCrop/Module.js +++ b/src/modules/ArbitraryCrop/Module.js @@ -69,8 +69,6 @@ module.exports = function ArbitraryCrop(options, UI, util) { newPixels = require('../Crop/Crop')(pixels, {x, y, w, h}); - require('../../util/getDataUri')(newPixels, input.format).then(res => console.log(res)); - return newPixels; } diff --git a/src/modules/ArbitraryCrop/info.json b/src/modules/ArbitraryCrop/info.json index eb9c452ada..9b977eeb25 100644 --- a/src/modules/ArbitraryCrop/info.json +++ b/src/modules/ArbitraryCrop/info.json @@ -1,6 +1,6 @@ { "name": "arbitrary-crop", - "description": "Crops image for any arbitrary quadilateral", + "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": {