From d33afe09da371c8aa69a3e84027b030f47b476bf Mon Sep 17 00:00:00 2001 From: Rishabh Shukla <42492389+blurry-x-face@users.noreply.github.com> Date: Fri, 16 Oct 2020 10:04:59 +0530 Subject: [PATCH] refactored resize (#1562) Co-authored-by: h Co-authored-by: Harsh Khandeparkar <34770591+HarshKhandeparkar@users.noreply.github.com> Co-authored-by: Jeffrey Warren --- src/modules/Resize/Module.js | 42 +--------------------------------- src/modules/Resize/Resize.js | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 41 deletions(-) create mode 100644 src/modules/Resize/Resize.js diff --git a/src/modules/Resize/Module.js b/src/modules/Resize/Module.js index b097ae0f7f..a0caeda138 100644 --- a/src/modules/Resize/Module.js +++ b/src/modules/Resize/Module.js @@ -1,6 +1,3 @@ -const imagejs = require('imagejs'), - pixelSetter = require('../../util/pixelSetter'), - ndarray = require('ndarray'); /* * Resize the image by given percentage value */ @@ -19,44 +16,7 @@ module.exports = function Resize(options, UI) { const step = this; function extraManipulation(pixels) { - // Value above 100% scales up, and below 100% scales down - const resize_value = parseInt(options.resize.slice(0, -1)); - - if (resize_value == 100) return pixels; - - - const new_width = Math.round(pixels.shape[0] * (resize_value / 100)), - new_height = Math.round(pixels.shape[1] * (resize_value / 100)); - - const bitmap = new imagejs.Bitmap({ width: pixels.shape[0], height: pixels.shape[1] }); - - for (let x = 0; x < pixels.shape[0]; x++) { - for (let y = 0; y < pixels.shape[1]; y++) { - let r = pixels.get(x, y, 0), - g = pixels.get(x, y, 1), - b = pixels.get(x, y, 2), - a = pixels.get(x, y, 3); - - bitmap.setPixel(x, y, r, g, b, a); - } - } - - const resized = bitmap.resize({ - width: new_width, - height: new_height, - algorithm: 'bicubicInterpolation' - }); - - const newPix = new ndarray([], [new_width, new_height, 4]); - - for (let x = 0; x < new_width; x++) { - for (let y = 0; y < new_height; y++) { - const {r, g, b, a} = resized.getPixel(x, y); - pixelSetter(x, y, [r, g, b, a], newPix); - } - } - - return newPix; + return require('./Resize')(pixels, options); } function output(image, datauri, mimetype, wasmSuccess) { diff --git a/src/modules/Resize/Resize.js b/src/modules/Resize/Resize.js new file mode 100644 index 0000000000..142412ec4c --- /dev/null +++ b/src/modules/Resize/Resize.js @@ -0,0 +1,44 @@ +const imagejs = require('imagejs'), + pixelSetter = require('../../util/pixelSetter'), + ndarray = require('ndarray'); +module.exports = function Resize(pixels, options) { + const resize_value = parseInt(options.resize.slice(0, -1)); + + if (resize_value == 100) return pixels; + + const new_width = Math.round(pixels.shape[0] * (resize_value / 100)), + new_height = Math.round(pixels.shape[1] * (resize_value / 100)); + + const bitmap = new imagejs.Bitmap({ + width: pixels.shape[0], + height: pixels.shape[1] + }); + + for (let x = 0; x < pixels.shape[0]; x++) { + for (let y = 0; y < pixels.shape[1]; y++) { + let r = pixels.get(x, y, 0), + g = pixels.get(x, y, 1), + b = pixels.get(x, y, 2), + a = pixels.get(x, y, 3); + + bitmap.setPixel(x, y, r, g, b, a); + } + } + + const resized = bitmap.resize({ + width: new_width, + height: new_height, + algorithm: 'bicubicInterpolation' + }); + + const newPix = new ndarray([], [new_width, new_height, 4]); + + for (let x = 0; x < new_width; x++) { + for (let y = 0; y < new_height; y++) { + const { r, g, b, a } = resized.getPixel(x, y); + pixelSetter(x, y, [r, g, b, a], newPix); + } + } + + return newPix; +};