Pixel set redundancy removed (#1188)

* Pixels.set Redundancy removed

* eslint fixes

* move require to top with const
This commit is contained in:
aashna27
2019-08-15 01:04:14 +05:30
committed by Jeffrey Warren
parent 1b5607a339
commit 5b98d5cf44
16 changed files with 93 additions and 117 deletions

View File

@@ -1,4 +1,6 @@
module.exports = exports = function (options, pixels, oldPixels, callback) { module.exports = exports = function (options, pixels, oldPixels, callback) {
const pixelSetter = require('../../util/pixelSetter.js');
var QRCode = require('qrcode'); var QRCode = require('qrcode');
QRCode.toDataURL(options.qrCodeString, function (err, url) { QRCode.toDataURL(options.qrCodeString, function (err, url) {
var getPixels = require('get-pixels'); var getPixels = require('get-pixels');
@@ -26,17 +28,11 @@ module.exports = exports = function (options, pixels, oldPixels, callback) {
for (var m = 0; m < width; m++) { for (var m = 0; m < width; m++) {
for (var n = 0; n < height; n++) { for (var n = 0; n < height; n++) {
if (m >= xe && n >= ye) { if (m >= xe && n >= ye) {
pixels.set(m, n, 0, qrPixels.get(m - xe, n - ye, 0)); pixelSetter(m, n, [qrPixels.get(m - xe, n - ye, 0), qrPixels.get(m - xe, n - ye, 1), qrPixels.get(m - xe, n - ye, 2), qrPixels.get(m - xe, n - ye, 3)], pixels);
pixels.set(m, n, 1, qrPixels.get(m - xe, n - ye, 1));
pixels.set(m, n, 2, qrPixels.get(m - xe, n - ye, 2));
pixels.set(m, n, 3, qrPixels.get(m - xe, n - ye, 3));
} }
else { else {
pixels.set(m, n, 0, oldPixels.get(m, n, 0)); pixelSetter(m, n, [qrPixels.get(m, n, 0), qrPixels.get(m, n, 1), qrPixels.get(m, n, 2), qrPixels.get(m, n, 3)], pixels);
pixels.set(m, n, 1, oldPixels.get(m, n, 1));
pixels.set(m, n, 2, oldPixels.get(m, n, 2));
pixels.set(m, n, 3, oldPixels.get(m, n, 3));
} }
} }

View File

@@ -1,4 +1,6 @@
module.exports = exports = function(pixels, blur) { module.exports = exports = function(pixels, blur) {
const pixelSetter = require('../../util/pixelSetter.js');
let kernel = kernelGenerator(blur), let kernel = kernelGenerator(blur),
pixs = { pixs = {
r: [], r: [],
@@ -24,9 +26,10 @@ module.exports = exports = function(pixels, blur) {
for (let y = 0; y < pixels.shape[1]; y++){ for (let y = 0; y < pixels.shape[1]; y++){
for (let x = 0; x < pixels.shape[0]; x++){ for (let x = 0; x < pixels.shape[0]; x++){
pixels.set(x, y, 0, Math.max(0, Math.min(conPix[0][y][x], 255))); var pixelvalue = [Math.max(0, Math.min(conPix[0][y][x], 255)),
pixels.set(x, y, 1, Math.max(0, Math.min(conPix[1][y][x], 255))); Math.max(0, Math.min(conPix[1][y][x], 255)),
pixels.set(x, y, 2, Math.max(0, Math.min(conPix[2][y][x], 255))); Math.max(0, Math.min(conPix[2][y][x], 255))];
pixelSetter(x, y, pixelvalue, pixels);
} }
} }

View File

@@ -4,10 +4,10 @@
module.exports = function canvasResize(options, UI) { module.exports = function canvasResize(options, UI) {
var defaults = require('./../../util/getDefaults.js')(require('./info.json')); var defaults = require('./../../util/getDefaults.js')(require('./info.json'));
const pixelSetter = require('../../util/pixelSetter.js');
var output; var output;
function draw(input, callback, progressObj) { function draw(input, callback, progressObj) {
options.width = parseInt(options.width || defaults.width); options.width = parseInt(options.width || defaults.width);
@@ -21,16 +21,15 @@ module.exports = function canvasResize(options, UI) {
var step = this; var step = this;
function extraManipulation(pixels) { function extraManipulation(pixels) {
let newPixels = require('ndarray')(new Uint8Array(4 * options.width * options.height).fill(0), [options.width, options.height, 4]); let newPixels = require('ndarray')(new Uint8Array(4 * options.width * options.height).fill(0), [options.width, options.height, 4]);
let iMax = options.width - options.x, let iMax = options.width - options.x,
jMax = options.height - options.y; jMax = options.height - options.y;
for (let i = 0; i < iMax && i < pixels.shape[0]; i++) { for (let i = 0; i < iMax && i < pixels.shape[0]; i++) {
for (let j = 0; j < jMax && j < pixels.shape[1]; j++) { for (let j = 0; j < jMax && j < pixels.shape[1]; j++) {
let x = i + options.x, y = j + options.y; let x = i + options.x, y = j + options.y;
newPixels.set(x, y, 0, pixels.get(i, j, 0)); pixelSetter(x, y, [pixels.get(i, j, 0), pixels.get(i, j, 1), pixels.get(i, j, 2), pixels.get(i, j, 3)], newPixels);
newPixels.set(x, y, 1, pixels.get(i, j, 1));
newPixels.set(x, y, 2, pixels.get(i, j, 2));
newPixels.set(x, y, 3, pixels.get(i, j, 3));
} }
} }
return newPixels; return newPixels;

View File

@@ -1,4 +1,6 @@
module.exports = function ColorTemperature(options, UI) { module.exports = function ColorTemperature(options, UI) {
const pixelSetter = require('../../util/pixelSetter.js');
var output; var output;
@@ -38,17 +40,12 @@ module.exports = function ColorTemperature(options, UI) {
for (let i = 0; i < pixels.shape[0]; i++) { for (let i = 0; i < pixels.shape[0]; i++) {
for (let j = 0; j < pixels.shape[1]; j++) { for (let j = 0; j < pixels.shape[1]; j++) {
r_data = pixels.get(i, j, 0); var rgbdata = [pixels.get(i, j, 0), pixels.get(i, j, 1), pixels.get(i, j, 2)];
r_new_data = (255 / r) * r_data; rgbdata[0] = (255 / r) * rgbdata[0];
pixels.set(i, j, 0, r_new_data); rgbdata[1] = (255 / g) * rgbdata[1];
rgbdata[2] = (255 / b) * rgbdata[2];
pixelSetter(i, j, rgbdata, pixels);
g_data = pixels.get(i, j, 1);
g_new_data = (255 / g) * g_data;
pixels.set(i, j, 1, g_new_data);
b_data = pixels.get(i, j, 2);
b_new_data = (255 / b) * b_data;
pixels.set(i, j, 2, b_new_data);
} }
} }

View File

@@ -1,4 +1,5 @@
var _ = require('lodash'); var _ = require('lodash');
const pixelSetter = require('../../util/pixelSetter.js');
module.exports = exports = function(pixels, contrast) { module.exports = exports = function(pixels, contrast) {
let oldpix = _.cloneDeep(pixels); let oldpix = _.cloneDeep(pixels);
@@ -10,37 +11,19 @@ module.exports = exports = function(pixels, contrast) {
for (let i = 0; i < pixels.shape[0]; i++) { for (let i = 0; i < pixels.shape[0]; i++) {
for (let j = 0; j < pixels.shape[1]; j++) { for (let j = 0; j < pixels.shape[1]; j++) {
var r = oldpix.get(i, j, 0) / 255.0;
r -= 0.5; var rgbarray = [oldpix.get(i, j, 0) / 255.0, oldpix.get(i, j, 1) / 255.0, oldpix.get(i, j, 2) / 255.0];
r *= contrast; for(var idx = 0;idx < 3;idx++){
r += 0.5; rgbarray[idx] -= 0.5;
r *= 255; rgbarray[idx] *= contrast;
if (r < 0) r = 0; rgbarray[idx] += 0.5;
if (r > 255) r = 255; rgbarray[idx] *= 255;
if (rgbarray[idx] < 0) rgbarray[idx] = 0;
if (rgbarray[idx] > 255) rgbarray[idx] = 255;
var g = oldpix.get(i, j, 1) / 255.0; }
g -= 0.5;
g *= contrast; pixelSetter(i, j, rgbarray, pixels);
g += 0.5;
g *= 255;
if (g < 0) g = 0;
if (g > 255) g = 255;
var b = oldpix.get(i, j, 2) / 255.0;
b -= 0.5;
b *= contrast;
b += 0.5;
b *= 255;
if (b < 0) b = 0;
if (b > 255) b = 255;
pixels.set(i, j, 0, r);
pixels.set(i, j, 1, g);
pixels.set(i, j, 2, b);
} }
} }
return pixels; return pixels;

View File

@@ -1,4 +1,6 @@
var _ = require('lodash'); var _ = require('lodash');
const pixelSetter = require('../../util/pixelSetter.js');
module.exports = exports = function(pixels, constantFactor, kernelValues, texMode) { module.exports = exports = function(pixels, constantFactor, kernelValues, texMode) {
let kernel = kernelGenerator(constantFactor, kernelValues), let kernel = kernelGenerator(constantFactor, kernelValues),
pixs = { pixs = {
@@ -24,9 +26,8 @@ module.exports = exports = function(pixels, constantFactor, kernelValues, texMod
for (let y = 0; y < pixels.shape[1]; y++){ for (let y = 0; y < pixels.shape[1]; y++){
for (let x = 0; x < pixels.shape[0]; x++){ for (let x = 0; x < pixels.shape[0]; x++){
pixels.set(x, y, 0, Math.max(0, Math.min(conPix[0][y][x], 255))); var value = [Math.max(0, Math.min(conPix[0][y][x], 255)), Math.max(0, Math.min(conPix[1][y][x], 255)), Math.max(0, Math.min(conPix[2][y][x], 255))];
pixels.set(x, y, 1, Math.max(0, Math.min(conPix[1][y][x], 255))); pixelSetter(x, y, value, pixels);
pixels.set(x, y, 2, Math.max(0, Math.min(conPix[2][y][x], 255)));
} }
} }

View File

@@ -1,5 +1,6 @@
module.exports = exports = function(pixels, options){ module.exports = exports = function(pixels, options){
var defaults = require('./../../util/getDefaults.js')(require('./info.json')); var defaults = require('./../../util/getDefaults.js')(require('./info.json'));
const pixelSetter = require('../../util/pixelSetter.js');
options.startingX = options.startingX || defaults.startingX; options.startingX = options.startingX || defaults.startingX;
options.startingY = options.startingY || defaults.startingY; options.startingY = options.startingY || defaults.startingY;
@@ -17,10 +18,8 @@ module.exports = exports = function(pixels, options){
var drawSide = function(startX, startY, endX, endY){ var drawSide = function(startX, startY, endX, endY){
for (var n = startX; n <= endX + thickness; n++){ for (var n = startX; n <= endX + thickness; n++){
for (var k = startY; k <= endY + thickness; k++){ for (var k = startY; k <= endY + thickness; k++){
pixels.set(n, k, 0, color[0]);
pixels.set(n, k, 1, color[1]); pixelSetter(n, k, color, pixels); //to remove 4th channel - pixels.set(n, k, 3, color[3]);
pixels.set(n, k, 2, color[2]);
//pixels.set(n, k, 3, color[3]);
} }
} }
}; };

View File

@@ -1,4 +1,6 @@
// Define kernels for the sobel filter // Define kernels for the sobel filter
const pixelSetter = require('../../util/pixelSetter.js');
const kernelx = [ const kernelx = [
[-1, 0, 1], [-1, 0, 1],
[-2, 0, 2], [-2, 0, 2],
@@ -41,19 +43,14 @@ module.exports = function(pixels, highThresholdRatio, lowThresholdRatio, useHyst
return pixels; return pixels;
}; };
function supress(pixels, pixel) { function supress(pixels, pixel) {
pixels.set(pixel[0], pixel[1], 0, 0); pixelSetter(pixel[0], pixel[1], [0, 0, 0, 255], pixels);
pixels.set(pixel[0], pixel[1], 1, 0);
pixels.set(pixel[0], pixel[1], 2, 0);
pixels.set(pixel[0], pixel[1], 3, 255);
} }
function preserve(pixels, pixel) { function preserve(pixels, pixel) {
pixels.set(pixel[0], pixel[1], 0, 255); pixelSetter(pixel[0], pixel[1], [255, 255, 255, 255], pixels);
pixels.set(pixel[0], pixel[1], 1, 255);
pixels.set(pixel[0], pixel[1], 2, 255);
pixels.set(pixel[0], pixel[1], 3, 255);
} }
// sobelFilter function that convolves sobel kernel over every pixel // sobelFilter function that convolves sobel kernel over every pixel

View File

@@ -1,12 +1,12 @@
module.exports = function flipImage(oldPixels, pixels, axis) { module.exports = function flipImage(oldPixels, pixels, axis) {
const pixelSetter = require('../../util/pixelSetter.js');
var width = oldPixels.shape[0], var width = oldPixels.shape[0],
height = oldPixels.shape[1]; height = oldPixels.shape[1];
function copyPixel(x1, y1, x2, y2){ function copyPixel(x1, y1, x2, y2){
pixels.set(x1, y1, 0, oldPixels.get(x2, y2, 0)); pixelSetter(x1, y1, [oldPixels.get(x2, y2, 0), oldPixels.get(x2, y2, 1), oldPixels.get(x2, y2, 2), oldPixels.get(x2, y2, 3)], pixels);
pixels.set(x1, y1, 1, oldPixels.get(x2, y2, 1));
pixels.set(x1, y1, 2, oldPixels.get(x2, y2, 2));
pixels.set(x1, y1, 3, oldPixels.get(x2, y2, 3));
} }
function flip(){ function flip(){

View File

@@ -1,9 +1,11 @@
module.exports = function Invert(options, UI) { module.exports = function Invert(options, UI) {
const pixelSetter = require('../../util/pixelSetter.js');
var output; var output;
// The function which is called on every draw. // The function which is called on every draw.
function draw(input, callback, progressObj) { function draw(input, callback) {
var getPixels = require('get-pixels'); var getPixels = require('get-pixels');
var savePixels = require('save-pixels'); var savePixels = require('save-pixels');
@@ -21,10 +23,8 @@ module.exports = function Invert(options, UI) {
for (var i = 0; i < pixels.shape[0]; i++) { for (var i = 0; i < pixels.shape[0]; i++) {
for (var j = 0; j < pixels.shape[1]; j++) { for (var j = 0; j < pixels.shape[1]; j++) {
let val = (i / width) * 255; let val = (i / width) * 255;
pixels.set(i, j, 0, val); pixelSetter(i, j, [val, val, val, 255], pixels);
pixels.set(i, j, 1, val);
pixels.set(i, j, 2, val);
pixels.set(i, j, 3, 255);
} }
} }
var chunks = []; var chunks = [];

View File

@@ -1,5 +1,6 @@
module.exports = exports = function(pixels, options){ module.exports = exports = function(pixels, options){
var defaults = require('./../../util/getDefaults.js')(require('./info.json')); var defaults = require('./../../util/getDefaults.js')(require('./info.json'));
const pixelSetter = require('../../util/pixelSetter.js');
options.x = Number(options.x) || defaults.x; options.x = Number(options.x) || defaults.x;
options.y = Number(options.y) || defaults.y; options.y = Number(options.y) || defaults.y;
@@ -9,19 +10,15 @@ module.exports = exports = function(pixels, options){
for(var x = 0; x < pixels.shape[0]; x += options.x){ for(var x = 0; x < pixels.shape[0]; x += options.x){
for(var y = 0 ; y < pixels.shape[1]; y++){ for(var y = 0 ; y < pixels.shape[1]; y++){
pixels.set(x, y, 0, color[0]); pixelSetter(x, y, color, pixels); // to remove 4th channel - pixels.set(x, y, 3, color[3]);
pixels.set(x, y, 1, color[1]);
pixels.set(x, y, 2, color[2]);
//pixels.set(x, y, 3, color[3]);
} }
} }
for(var y = 0; y < pixels.shape[1]; y += options.y){ for(var y = 0; y < pixels.shape[1]; y += options.y){
for(var x = 0 ; x < pixels.shape[0]; x++){ for(var x = 0 ; x < pixels.shape[0]; x++){
pixels.set(x, y, 0, color[0]); pixelSetter(x, y, color, pixels); // to remove 4th channel - pixels.set(x, y, 3, color[3]);
pixels.set(x, y, 1, color[1]);
pixels.set(x, y, 2, color[2]);
//pixels.set(x, y, 3, color[3]);
} }
} }

View File

@@ -7,7 +7,9 @@ module.exports = function Channel(options, UI) {
function draw(input, callback, progressObj) { function draw(input, callback, progressObj) {
var defaults = require('./../../util/getDefaults.js')(require('./info.json')); const defaults = require('./../../util/getDefaults.js')(require('./info.json'));
const pixelSetter = require('../../util/pixelSetter.js');
options.gradient = options.gradient || defaults.gradient; options.gradient = options.gradient || defaults.gradient;
options.gradient = JSON.parse(options.gradient); options.gradient = JSON.parse(options.gradient);
@@ -32,10 +34,8 @@ module.exports = function Channel(options, UI) {
for (let x = 0; x < 256; x++) { for (let x = 0; x < 256; x++) {
for (let y = 0; y < 256; y++) { for (let y = 0; y < 256; y++) {
pixels.set(x, y, 0, 255); pixelSetter(x, y, [255, 255, 255, 255], pixels);
pixels.set(x, y, 1, 255);
pixels.set(x, y, 2, 255);
pixels.set(x, y, 3, 255);
} }
} }
@@ -43,9 +43,8 @@ module.exports = function Channel(options, UI) {
if (options.gradient) { if (options.gradient) {
for (let x = 0; x < 256; x++) { for (let x = 0; x < 256; x++) {
for (let y = 0; y < 10; y++) { for (let y = 0; y < 10; y++) {
pixels.set(x, 255 - y, 0, x); pixelSetter(x, 255 - y, [x, x, x], pixels);
pixels.set(x, 255 - y, 1, x);
pixels.set(x, 255 - y, 2, x);
} }
} }
} }
@@ -56,9 +55,8 @@ module.exports = function Channel(options, UI) {
let pixCount = Math.round(convfactor * hist[x]); let pixCount = Math.round(convfactor * hist[x]);
for (let y = startY; y < pixCount; y++) { for (let y = startY; y < pixCount; y++) {
pixels.set(x, 255 - y, 0, 204); pixelSetter(x, 255 - y, [204, 255, 153], pixels);
pixels.set(x, 255 - y, 1, 255);
pixels.set(x, 255 - y, 2, 153);
} }
} }

View File

@@ -2,6 +2,7 @@ module.exports = exports = function(pixels, options) {
let defaults = require('./../../util/getDefaults.js')(require('./info.json')); let defaults = require('./../../util/getDefaults.js')(require('./info.json'));
const pixelSetter = require('../../util/pixelSetter.js');
let fillColor = options.fillColor || defaults.fillColor, let fillColor = options.fillColor || defaults.fillColor,
x = parseInt(options.startingX) || defaults.startingX, x = parseInt(options.startingX) || defaults.startingX,
@@ -47,10 +48,8 @@ module.exports = exports = function(pixels, options) {
} while (isSimilar(currx, south) && south < height); } while (isSimilar(currx, south) && south < height);
for (n = north + 1; n < south; n += 1) { for (n = north + 1; n < south; n += 1) {
pixels.set(currx, n, 0, fillColor[0]); pixelSetter(currx, n, fillColor, pixels);
pixels.set(currx, n, 1, fillColor[1]);
pixels.set(currx, n, 2, fillColor[2]);
pixels.set(currx, n, 3, fillColor[3]);
if (isSimilar(currx - 1, n)) { if (isSimilar(currx - 1, n)) {
queuex.push(currx - 1); queuex.push(currx - 1);
queuey.push(n); queuey.push(n);

View File

@@ -1,4 +1,6 @@
module.exports = exports = function(pixels, options){ module.exports = exports = function(pixels, options){
const pixelSetter = require('../../util/pixelSetter.js');
var color = options.color || 'rgb(228,86,81)'; var color = options.color || 'rgb(228,86,81)';
color = color.substring(color.indexOf('(') + 1, color.length - 1); // extract only the values from rgba(_,_,_,_) color = color.substring(color.indexOf('(') + 1, color.length - 1); // extract only the values from rgba(_,_,_,_)
@@ -23,7 +25,6 @@ module.exports = exports = function(pixels, options){
g >= cg * minFactor && g <= cg * maxFactor && g >= cg * minFactor && g <= cg * maxFactor &&
b >= cb * minFactor && b <= cb * maxFactor); b >= cb * minFactor && b <= cb * maxFactor);
} }
for(var i = 0; i < pixels.shape[0]; i++){ for(var i = 0; i < pixels.shape[0]; i++){
for(var j = 0; j < pixels.shape[1]; j++){ for(var j = 0; j < pixels.shape[1]; j++){
var r = pixels.get(i, j, 0), var r = pixels.get(i, j, 0),
@@ -32,16 +33,16 @@ module.exports = exports = function(pixels, options){
if(isSimilar(r, g, b)){ if(isSimilar(r, g, b)){
if (replaceMethod == 'greyscale'){ if (replaceMethod == 'greyscale'){
var avg = (r + g + b) / 3; var avg = (r + g + b) / 3;
pixels.set(i, j, 0, avg); pixelSetter(i, j, [avg, avg, avg], pixels);
pixels.set(i, j, 1, avg);
pixels.set(i, j, 2, avg);
}else { }else {
pixels.set(i, j, 0, replaceColor[0]); pixelSetter(i, j, replaceColor, pixels);
pixels.set(i, j, 1, replaceColor[1]);
pixels.set(i, j, 2, replaceColor[2]);
} }
} }
} }
} }
return pixels; return pixels;
}; };

View File

@@ -5,6 +5,9 @@
module.exports = function PixelManipulation(image, options) { module.exports = function PixelManipulation(image, options) {
// To handle the case where pixelmanipulation is called on the input object itself // To handle the case where pixelmanipulation is called on the input object itself
// like input.pixelManipulation(options) // like input.pixelManipulation(options)
const pixelSetter = require('../../util/pixelSetter.js');
if (arguments.length <= 1) { if (arguments.length <= 1) {
options = image; options = image;
image = this; image = this;
@@ -86,10 +89,8 @@ module.exports = function PixelManipulation(image, options) {
y y
); );
pixels.set(x, y, 0, pixel[0]); pixelSetter(x, y, pixel, pixels);
pixels.set(x, y, 1, pixel[1]);
pixels.set(x, y, 2, pixel[2]);
pixels.set(x, y, 3, pixel[3]);
} }
} }
}; };

5
src/util/pixelSetter.js Normal file
View File

@@ -0,0 +1,5 @@
module.exports = function(x, y, value, pixels){
for(let i = 0; i < value.length; i++){
pixels.set(x, y, i, value[i]);
}
};