From 2f4f971f829ac4ce8ba06594ee3a287801679446 Mon Sep 17 00:00:00 2001 From: h Date: Tue, 21 Jan 2020 22:36:37 +0530 Subject: [PATCH] fix extraManipulation --- src/modules/TextOverlay/Module.js | 13 ++++--- src/modules/_nomodule/PixelManipulation.js | 40 +++------------------- src/util/getDataUri.js | 31 +++++++++++++++++ 3 files changed, 44 insertions(+), 40 deletions(-) create mode 100644 src/util/getDataUri.js diff --git a/src/modules/TextOverlay/Module.js b/src/modules/TextOverlay/Module.js index 803cb187ae..9d6f005021 100644 --- a/src/modules/TextOverlay/Module.js +++ b/src/modules/TextOverlay/Module.js @@ -7,13 +7,16 @@ module.exports = function TextOverlay(options, UI) { var step = this; - function extraManipulation(pixels, setRenderState, generateOutput, url1) { + function extraManipulation(pixels, setRenderState, generateOutput) { //if (options.step.inBrowser) - setRenderState(false); - pixels = require('./TextOverlay')(pixels, options, url1, () => { + const getDataUri = require('../../util/getDataUri'); + getDataUri(pixels, input.format).then(dataUri => { + setRenderState(false); + pixels = require('./TextOverlay')(pixels, options, dataUri, () => { + setRenderState(true); + generateOutput(); + }); - setRenderState(true); - generateOutput(); }); } diff --git a/src/modules/_nomodule/PixelManipulation.js b/src/modules/_nomodule/PixelManipulation.js index dc20ef517d..0a5dfa21df 100644 --- a/src/modules/_nomodule/PixelManipulation.js +++ b/src/modules/_nomodule/PixelManipulation.js @@ -4,7 +4,8 @@ const pixelSetter = require('../../util/pixelSetter.js'), ndarray = require('ndarray'), gifshot = require('gifshot'), fs = require('fs'), - path = require('path'); + path = require('path'), + getDataUri = require('../../util/getDataUri'); /* * General purpose per-pixel manipulation * accepting a changePixel() method to remix a pixel's channels @@ -45,36 +46,7 @@ module.exports = function PixelManipulation(image, options) { } options = options || {}; - - /** - * @description Returns the DataURI of an image from its pixels - * @param {"ndarray"} pix pixels ndarray of pixels of the image. - * @param {String} format Format/MimeType of the image input. - * @returns {Promise} Promise with DataURI as parameter in the callback. - */ - const getDataUri = (pix, format) => { - return new Promise(resolve => { - let chunks = [], - totalLength = 0; - - let r = savePixels(pix, format, { - quality: 100 - }); - - r.on('data', function (chunk) { - totalLength += chunk.length; - chunks.push(chunk); - }); - - r.on('end', function () { - let data = Buffer.concat(chunks, totalLength).toString('base64'); - let datauri = 'data:image/' + format + ';base64,' + data; - - resolve(datauri); - }); - }); - }; - + getPixels(image.src, function (err, pixels) { if (err) { console.log('get-pixels error: ', err); @@ -314,10 +286,8 @@ module.exports = function PixelManipulation(image, options) { else resolvedFrames++; if (options.extraManipulation){ - getDataUri(frames[f], options.format).then(datauri => { - frames[f] = options.extraManipulation(framePix, setRenderState, generateOutput, datauri) || framePix; // extraManipulation is used to manipulate each pixel individually. - perFrameShape = frames[f].shape; - }); + frames[f] = options.extraManipulation(framePix, setRenderState, generateOutput) || framePix; // extraManipulation is used to manipulate each pixel individually. + perFrameShape = frames[f].shape; } generateOutput(); } diff --git a/src/util/getDataUri.js b/src/util/getDataUri.js new file mode 100644 index 0000000000..d589e48de4 --- /dev/null +++ b/src/util/getDataUri.js @@ -0,0 +1,31 @@ +const savePixels = require('save-pixels'); + +/** + * @description Returns the DataURI of an image from its pixels + * @param {"ndarray"} pix pixels ndarray of pixels of the image. + * @param {String} format Format/MimeType of the image input. + * @returns {Promise} Promise with DataURI as parameter in the callback. +*/ + +module.exports = getDataUri = (pix, format) => { + return new Promise(resolve => { + let chunks = [], + totalLength = 0; + + let r = savePixels(pix, format, { + quality: 100 + }); + + r.on('data', function(chunk) { + totalLength += chunk.length; + chunks.push(chunk); + }); + + r.on('end', function() { + let data = Buffer.concat(chunks, totalLength).toString('base64'); + let datauri = 'data:image/' + format + ';base64,' + data; + + resolve(datauri); + }); + }); +};