Merge pull request #10595 from Snuffleupagus/JpegDecode-zero-tmpCanvas

Zero the width/height of the temporary canvas used during `JpegDecode` (issue 10594)
This commit is contained in:
Tim van der Meij 2019-02-28 23:41:22 +01:00 committed by GitHub
commit 9559d57636
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2068,15 +2068,14 @@ class WorkerTransport {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
const img = new Image(); const img = new Image();
img.onload = function() { img.onload = function() {
const width = img.width; const { width, height, } = img;
const height = img.height;
const size = width * height; const size = width * height;
const rgbaLength = size * 4; const rgbaLength = size * 4;
const buf = new Uint8ClampedArray(size * components); const buf = new Uint8ClampedArray(size * components);
const tmpCanvas = document.createElement('canvas'); let tmpCanvas = document.createElement('canvas');
tmpCanvas.width = width; tmpCanvas.width = width;
tmpCanvas.height = height; tmpCanvas.height = height;
const tmpCtx = tmpCanvas.getContext('2d'); let tmpCtx = tmpCanvas.getContext('2d');
tmpCtx.drawImage(img, 0, 0); tmpCtx.drawImage(img, 0, 0);
const data = tmpCtx.getImageData(0, 0, width, height).data; const data = tmpCtx.getImageData(0, 0, width, height).data;
@ -2092,6 +2091,13 @@ class WorkerTransport {
} }
} }
resolve({ data: buf, width, height, }); resolve({ data: buf, width, height, });
// Zeroing the width and height cause Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption.
tmpCanvas.width = 0;
tmpCanvas.height = 0;
tmpCanvas = null;
tmpCtx = null;
}; };
img.onerror = function() { img.onerror = function() {
reject(new Error('JpegDecode failed to load image')); reject(new Error('JpegDecode failed to load image'));