Improve Code Quality and Optimize (#763)

Signed-off-by: tech4GT <varun.gupta1798@gmail.com>
This commit is contained in:
Varun Gupta
2019-02-16 11:05:32 +05:30
committed by GitHub
parent f42aa2fd67
commit 75dc23fb55
26 changed files with 1332 additions and 921 deletions

View File

@@ -6,29 +6,17 @@ module.exports = function PixelManipulation(image, options) {
// To handle the case where pixelmanipulation is called on the input object itself
// like input.pixelManipulation(options)
if(arguments.length <= 1){
if (arguments.length <= 1) {
options = image;
image = this;
}
options = options || {};
options.changePixel =
options.changePixel ||
function changePixel(r, g, b, a) {
return [r, g, b, a];
};
//
options.extraManipulation =
options.extraManipulation ||
function extraManipulation(pixels) {
return pixels;
};
var getPixels = require("get-pixels"),
const getPixels = require("get-pixels"),
savePixels = require("save-pixels");
getPixels(image.src, function (err, pixels) {
getPixels(image.src, function(err, pixels) {
if (err) {
console.log("Bad image path", image);
return;
@@ -52,23 +40,31 @@ module.exports = function PixelManipulation(image, options) {
}
}
for (var x = 0; x < pixels.shape[0]; x++) {
for (var y = 0; y < pixels.shape[1]; y++) {
var pixel = options.changePixel(
pixels.get(x, y, 0),
pixels.get(x, y, 1),
pixels.get(x, y, 2),
pixels.get(x, y, 3),
x,
y
);
if (options.preProcess) pixels = options.preProcess(pixels); // Allow for preprocessing
pixels.set(x, y, 0, pixel[0]);
pixels.set(x, y, 1, pixel[1]);
pixels.set(x, y, 2, pixel[2]);
pixels.set(x, y, 3, pixel[3]);
if (options.changePixel) {
if (!options.inBrowser && !process.env.TEST) pace.op();
/* Allows for Flexibility
if per pixel manipulation is not required */
for (var x = 0; x < pixels.shape[0]; x++) {
for (var y = 0; y < pixels.shape[1]; y++) {
let pixel = options.changePixel(
pixels.get(x, y, 0),
pixels.get(x, y, 1),
pixels.get(x, y, 2),
pixels.get(x, y, 3),
x,
y
);
pixels.set(x, y, 0, pixel[0]);
pixels.set(x, y, 1, pixel[1]);
pixels.set(x, y, 2, pixel[2]);
pixels.set(x, y, 3, pixel[3]);
if (!options.inBrowser && !process.env.TEST) pace.op();
}
}
}
@@ -81,12 +77,12 @@ module.exports = function PixelManipulation(image, options) {
var totalLength = 0;
var r = savePixels(pixels, options.format, { quality: 100 });
r.on("data", function (chunk) {
r.on("data", function(chunk) {
totalLength += chunk.length;
chunks.push(chunk);
});
r.on("end", function () {
r.on("end", function() {
var data = Buffer.concat(chunks, totalLength).toString("base64");
var datauri = "data:image/" + options.format + ";base64," + data;
if (options.output)