Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ExtraManipulation #1551

Merged
merged 4 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/modules/TextOverlay/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}

Expand Down
40 changes: 5 additions & 35 deletions src/modules/_nomodule/PixelManipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down
31 changes: 31 additions & 0 deletions src/util/getDataUri.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
};