From 94caefa2cd75a47965529c1a53844a7dfaba5d0c Mon Sep 17 00:00:00 2001 From: Rishabh Shukla <42492389+blurry-x-face@users.noreply.github.com> Date: Thu, 16 Jan 2020 23:17:55 +0530 Subject: [PATCH] Contrast module now uses changePixel Method (#1482) * contrast module uses changePixel Method * change if block to Math function * fix bug Co-authored-by: Harsh Khandeparkar <34770591+HarshKhandeparkar@users.noreply.github.com> Co-authored-by: Jeffrey Warren --- src/modules/Contrast/Contrast.js | 30 ------------------------------ src/modules/Contrast/Module.js | 26 ++++++++++++++++++++++---- 2 files changed, 22 insertions(+), 34 deletions(-) delete mode 100644 src/modules/Contrast/Contrast.js diff --git a/src/modules/Contrast/Contrast.js b/src/modules/Contrast/Contrast.js deleted file mode 100644 index 1be556d4..00000000 --- a/src/modules/Contrast/Contrast.js +++ /dev/null @@ -1,30 +0,0 @@ -var _ = require('lodash'); -const pixelSetter = require('../../util/pixelSetter.js'); - -module.exports = exports = function(pixels, contrast) { - let oldpix = _.cloneDeep(pixels); - contrast = Number(contrast); - if (contrast < -100) contrast = -100; - if (contrast > 100) contrast = 100; - contrast = (100.0 + contrast) / 100.0; - contrast *= contrast; - - for (let i = 0; i < pixels.shape[0]; i++) { - for (let j = 0; j < pixels.shape[1]; j++) { - - var rgbarray = [oldpix.get(i, j, 0) / 255.0, oldpix.get(i, j, 1) / 255.0, oldpix.get(i, j, 2) / 255.0]; - for(var idx = 0;idx < 3;idx++){ - rgbarray[idx] -= 0.5; - rgbarray[idx] *= contrast; - rgbarray[idx] += 0.5; - rgbarray[idx] *= 255; - if (rgbarray[idx] < 0) rgbarray[idx] = 0; - if (rgbarray[idx] > 255) rgbarray[idx] = 255; - } - - pixelSetter(i, j, rgbarray, pixels); - - } - } - return pixels; -}; \ No newline at end of file diff --git a/src/modules/Contrast/Module.js b/src/modules/Contrast/Module.js index 440d1066..d76d8971 100644 --- a/src/modules/Contrast/Module.js +++ b/src/modules/Contrast/Module.js @@ -15,9 +15,27 @@ module.exports = function Contrast(options, UI) { var step = this; - function extraManipulation(pixels) { - pixels = require('./Contrast')(pixels, options.contrast); - return pixels; + let contrast = options.contrast; + + contrast = Number(contrast); + if (contrast < -100) contrast = -100; + if (contrast > 100) contrast = 100; + contrast = (100.0 + contrast) / 100.0; + contrast *= contrast; + + function changeContrast(p){ + p -= 0.5; + p *= contrast; + p += 0.5; + p *= 255; + p = Math.max(0, p); + p = Math.min(p, 255); + return p; + } + + function changePixel(r, g, b, a) { + + return [changeContrast(r / 255), changeContrast(g / 255), changeContrast(b / 255), a]; } function output(image, datauri, mimetype, wasmSuccess) { @@ -27,7 +45,7 @@ module.exports = function Contrast(options, UI) { return require('../_nomodule/PixelManipulation.js')(input, { output: output, ui: options.step.ui, - extraManipulation: extraManipulation, + changePixel: changePixel, format: input.format, image: options.image, callback: callback,