Add feature detection for using Uint8Array as imageData
This commit is contained in:
parent
5bae370a90
commit
01d2929401
@ -3,6 +3,29 @@
|
|||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
// Some browsers can use UInt8Array directly as imageData.
|
||||||
|
PDFJS.FEATURE_CANVAS_UINT_IMAGE_DATA = false;
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
window.addEventListener('load', function featureCanvasUIntImgaData() {
|
||||||
|
var canvas = document.createElement('canvas');
|
||||||
|
canvas.width = 1;
|
||||||
|
canvas.height = 1;
|
||||||
|
var ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
|
try {
|
||||||
|
ctx.putImageData({
|
||||||
|
width: 1,
|
||||||
|
height: 1,
|
||||||
|
data: new Uint8Array(4)
|
||||||
|
}, 0, 0);
|
||||||
|
} catch (e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
PDFJS.FEATURE_CANVAS_UINT_IMAGE_DATA = true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// <canvas> contexts store most of the state we need natively.
|
// <canvas> contexts store most of the state we need natively.
|
||||||
// However, PDF needs a bit more state, which we store here.
|
// However, PDF needs a bit more state, which we store here.
|
||||||
|
|
||||||
@ -789,17 +812,18 @@ var CanvasGraphics = (function canvasGraphics() {
|
|||||||
var tmpCtx = tmpCanvas.getContext('2d');
|
var tmpCtx = tmpCanvas.getContext('2d');
|
||||||
var tmpImgData;
|
var tmpImgData;
|
||||||
|
|
||||||
// Some browsers can set an UInt8Array directly as imageData, some
|
if (PDFJS.FEATURE_CANVAS_UINT_IMAGE_DATA) {
|
||||||
// can't. As long as we don't have proper feature detection, just
|
tmpImgData = imgData;
|
||||||
// copy over each pixel and set the imageData that way.
|
} else {
|
||||||
tmpImgData = tmpCtx.getImageData(0, 0, w, h);
|
tmpImgData = tmpCtx.getImageData(0, 0, w, h);
|
||||||
|
|
||||||
// Copy over the imageData.
|
// Copy over the imageData pixel by pixel.
|
||||||
var tmpImgDataPixels = tmpImgData.data;
|
var tmpImgDataPixels = tmpImgData.data;
|
||||||
var len = tmpImgDataPixels.length;
|
var len = tmpImgDataPixels.length;
|
||||||
|
|
||||||
while (len--) {
|
while (len--) {
|
||||||
tmpImgDataPixels[len] = imgData.data[len];
|
tmpImgDataPixels[len] = imgData.data[len];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpCtx.putImageData(tmpImgData, 0, 0);
|
tmpCtx.putImageData(tmpImgData, 0, 0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user