mirror of
https://github.com/publiclab/image-sequencer.git
synced 2025-12-12 11:20:02 +01:00
Improve Code Quality and Optimize (#763)
Signed-off-by: tech4GT <varun.gupta1798@gmail.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user