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

[WIP] Fisheye for GIF #1489

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
98 changes: 56 additions & 42 deletions src/modules/FisheyeGl/Module.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,77 @@
/*
* Resolves Fisheye Effect
*/
const _ = require('lodash');
module.exports = function DoNothing(options, UI) {

var output;
getPixels = require('get-pixels');
gl = require('fisheyegl');


var gl = require('fisheyegl');
function draw(input, callback,progressObj) {

function draw(input, callback) {
progressObj.stop(true);
progressObj.overrideFlag = true;

var step = this;

if (!options.inBrowser) {
require('../_nomodule/gl-context')(input, callback, step, options);
var curr = 0;
var isGif = input.src.includes('image/gif');
function changePixel(r, g, b, a) {
return [r, g, b, a];
}
else {
// Create a canvas, if it doesn't already exist.
if (!document.querySelector('#image-sequencer-canvas')) {


function extraManipulation(pixels, setRenderState, generateOutput,datauri) {
curr++;
const oldPixels = _.cloneDeep(pixels);
setRenderState(false); // Prevent rendering of final output image until extraManipulation completes.
var canvas2 = document.createElement('canvas');
canvas2.width = oldPixels.shape[0]; //img.width();
canvas2.height = oldPixels.shape[1]; //img.height();
var ctx2 = canvas2.getContext('2d');

ctx2.putImageData(new ImageData(new Uint8ClampedArray(pixels.data), pixels.shape[0], pixels.shape[1]), 0, 0);
var url1 = datauri;

if (!options.inBrowser) {
require('../_nomodule/gl-context')(input, callback, step, options);
}
else {

var canvas = document.createElement('canvas');
canvas.style.display = 'none';
canvas.setAttribute('id', 'image-sequencer-canvas');
canvas.setAttribute('id', 'image-sequencer-canvas'+curr.toString());
document.body.append(canvas);

var distorter = new FisheyeGl({
selector: '#image-sequencer-canvas'+curr.toString()
});
}
else var canvas = document.querySelector('#image-sequencer-canvas');

distorter = FisheyeGl({
selector: '#image-sequencer-canvas'
});

// Parse the inputs
options.a = parseFloat(options.a) || distorter.lens.a;
options.b = parseFloat(options.b) || distorter.lens.b;
options.Fx = parseFloat(options.Fx) || distorter.lens.Fx;
options.Fy = parseFloat(options.Fy) || distorter.lens.Fy;
options.scale = parseFloat(options.scale) || distorter.lens.scale;
options.x = parseFloat(options.x) || distorter.fov.x;
options.y = parseFloat(options.y) || distorter.fov.y;

// Set fisheyegl inputs
distorter.lens.a = options.a;
distorter.lens.b = options.b;
distorter.lens.Fx = options.Fx;
distorter.lens.Fy = options.Fy;
distorter.lens.scale = options.scale;
distorter.fov.x = options.x;
distorter.fov.y = options.y;

// generate fisheyegl output
distorter.setImage(input.src, function() {

// this output is accessible to Image Sequencer
step.output = { src: canvas.toDataURL(), format: input.format };

// Tell Image Sequencer and UI that step has been drawn
callback();


require('./fisheye')(options, pixels, oldPixels,url1,distorter,isGif, () => {

setRenderState(true); // Allow rendering in the callback.
generateOutput();

});
}

function output(image, datauri, mimetype, wasmSuccess) {
step.output = { src: datauri, format: mimetype, wasmSuccess, useWasm: options.useWasm };
}

return require('../_nomodule/PixelManipulation.js')(input, {
output: output,
ui: options.step.ui,
changePixel: changePixel,
extraManipulation: extraManipulation,
format: input.format,
image: options.image,
inBrowser: options.inBrowser,
callback: callback,
useWasm:options.useWasm
});
}

return {
Expand Down
48 changes: 48 additions & 0 deletions src/modules/FisheyeGl/fisheye.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const pixelSetter = require('../../util/pixelSetter.js'),
getPixels = require('get-pixels');

module.exports = exports = function (options, pixels, oldPixels, url1,distorter, isGif,cb) {
// Parse the inputs
options.a = parseFloat(options.a) || distorter.lens.a;
options.b = parseFloat(options.b) || distorter.lens.b;
options.Fx = parseFloat(options.Fx) || distorter.lens.Fx;
options.Fy = parseFloat(options.Fy) || distorter.lens.Fy;
options.scale = parseFloat(options.scale) || distorter.lens.scale;
options.x = parseFloat(options.x) || distorter.fov.x;
options.y = parseFloat(options.y) || distorter.fov.y;

// Set fisheyegl inputs
distorter.lens.a = options.a;
distorter.lens.b = options.b;
distorter.lens.Fx = options.Fx;
distorter.lens.Fy = options.Fy;
distorter.lens.scale = options.scale;
distorter.fov.x = options.x;
distorter.fov.y = options.y;

distorter.setImage(url1, function() {
getPixels(distorter.getImage().src, function (err, distorted) {

for (let x = 0; x < oldPixels.shape[0]; x++) {
for (let y = 0; y < oldPixels.shape[1]; y++) {


pixelSetter(
x,
y,
[
distorted.get(x, y, 0),
distorted.get(x, y, 1),
distorted.get(x, y, 2),
distorted.get(x, y, 3)
],
pixels
);

}
}

if(cb) cb();
});
});
};
6 changes: 4 additions & 2 deletions src/modules/_nomodule/PixelManipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,10 @@ module.exports = function PixelManipulation(image, options) {
else resolvedFrames++;

if (options.extraManipulation){
frames[f] = options.extraManipulation(framePix, setRenderState, generateOutput) || framePix; // extraManipulation is used to manipulate each pixel individually.
perFrameShape = frames[f].shape;
getDataUri(frames[f], options.format).then(datauri => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the data URI used for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the module (fisheye) we need an image source for distorter to set the image. One option was to put pixels into canvas and get the url using canvas.todataurl() but this failed for gifs as it was making the transpose of that array. Hence to counter that i directly used the source url without using canvas method

frames[f] = options.extraManipulation(framePix, setRenderState, generateOutput,datauri) || framePix; // extraManipulation is used to manipulate each pixel individually.
perFrameShape = frames[f].shape;
});
}
generateOutput();
}
Expand Down