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

WebglDistort for GIF's #1614

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all 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
99 changes: 68 additions & 31 deletions src/modules/WebglDistort/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = function DoNothing(options, UI) {

};

var warpWebGl = function warpWebGl(id, matrix1, matrix2, download) {
var warpWebGl = function warpWebGl(dataURI, matrix1, matrix2, download, cb) {

// try to create a WebGL canvas (will fail if WebGL isn't supported)
try {
Expand All @@ -46,7 +46,7 @@ module.exports = function DoNothing(options, UI) {
}

// convert the image to a texture
var imageEl = document.getElementById(id);
// var imageEl = document.getElementById(id);

var image = new Image();

Expand Down Expand Up @@ -168,13 +168,14 @@ module.exports = function DoNothing(options, UI) {
// keep non-blob version in case we have to fall back:
// image.src = canvas.toDataURL('image/png');
// window.location = canvas.toDataURL('image/png');
imageEl.src = burl;
// imageEl.src = burl;
cb(burl);

}

};

image.src = imageEl.src;
image.src = dataURI;

};

Expand All @@ -190,35 +191,71 @@ module.exports = function DoNothing(options, UI) {
var parseDistortCoordinates = require('../../util/parseDistortCoordinates.js');
var cornerCoordinates = parseDistortCoordinates(options);

if (!options.inBrowser) {
// this.output = input;
// callback();
require('../_nomodule/gl-context')(input, callback, step, options);
function extraManipulation(pixels, setRenderState, generateOutput) {
setRenderState(false);
if (!options.inBrowser) {
// this.output = input;
// callback();
require('../_nomodule/gl-context')(input, callback, step, options);
}
else {
// var image = document.createElement('img');

const getDataUri = require('../../util/getDataUri');
const getPixels = require('get-pixels'),
pixelSetter = require('../../util/pixelSetter.js');
getDataUri(pixels, input.format).then(dataUri => {
rishabhshuklax marked this conversation as resolved.
Show resolved Hide resolved

warpWebGl(
dataUri,
[0, 0, pixels.shape[0], 0, pixels.shape[0], pixels.shape[1], 0, pixels.shape[1]], // matrix 1 (before) corner coordinates, NW, NE, SE, SW
// [0, 100, 1023, -50, 1223, 867, 100, 767] // matrix 2 (after) corner coordinates
cornerCoordinates,
null,
function (burl) {
getPixels(burl, function (err, qrPixels) {
for (let x = 0; x < pixels.shape[0]; x++) {
for (let y = 0; y < pixels.shape[1]; y++) {
pixelSetter(
x,
y,
[
qrPixels.get(x, y, 0),
qrPixels.get(x, y, 1),
qrPixels.get(x, y, 2),
qrPixels.get(x, y, 3)
],
pixels
);
}
}

setRenderState(true);
generateOutput();
});


}
);


});
}
}
else {
var image = document.createElement('img');
image.onload = () => {
warpWebGl(
'img',
[0, 0, image.naturalWidth, 0, image.naturalWidth, image.naturalHeight, 0, image.naturalHeight], // matrix 1 (before) corner coordinates, NW, NE, SE, SW
// [0, 100, 1023, -50, 1223, 867, 100, 767] // matrix 2 (after) corner coordinates
cornerCoordinates
);
image.onload = () => {
var canvas = document.createElement('canvas');
canvas.width = image.naturalWidth; // or 'width' if you want a special/scaled size
canvas.height = image.naturalHeight; // or 'height' if you want a special/scaled size
canvas.getContext('2d').drawImage(image, 0, 0);

step.output = { src: canvas.toDataURL('image/png'), format: 'png' };
image.remove();
callback();
};
};
image.src = input.src;
image.id = 'img';
document.body.appendChild(image);
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,
extraManipulation: extraManipulation,
format: input.format,
image: options.image,
inBrowser: options.inBrowser,
callback: callback,
useWasm:options.useWasm
});
}

return {
Expand Down