Use a more compact typed array to pass the image mask from the worker to the main thread.

This commit is contained in:
Nicholas Nethercote 2014-01-13 20:08:39 -06:00 committed by Yury Delendik
parent dabafb8444
commit 3f533a1cb0
3 changed files with 44 additions and 22 deletions

View File

@ -262,8 +262,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var inverseDecode = !!decode && decode[0] > 0;
operatorList.addOp(OPS.paintImageMaskXObject,
[PDFImage.createMask(imgArray, width, height,
inverseDecode)]
[PDFImage.createMask(imgArray, width, height, inverseDecode)]
);
return;
}

View File

@ -209,26 +209,21 @@ var PDFImage = (function PDFImageClosure() {
PDFImage.createMask = function PDFImage_createMask(imgArray, width, height,
inverseDecode) {
var buffer = new Uint8Array(width * height * 4);
var imgArrayPos = 0;
var i, j, mask, buf;
// removing making non-masked pixels transparent
var bufferPos = 3; // alpha component offset
for (i = 0; i < height; i++) {
mask = 0;
for (j = 0; j < width; j++) {
if (!mask) {
buf = imgArray[imgArrayPos++];
mask = 128;
}
if (!(buf & mask) !== inverseDecode) {
buffer[bufferPos] = 255;
}
bufferPos += 4;
mask >>= 1;
// Copy imgArray into a typed array (inverting if necessary) so it can be
// transferred to the main thread.
var length = ((width + 7) >> 3) * height;
var data = new Uint8Array(length);
if (inverseDecode) {
for (var i = 0; i < length; i++) {
data[i] = ~imgArray[i];
}
} else {
for (var i = 0; i < length; i++) {
data[i] = imgArray[i];
}
}
return {data: buffer, width: width, height: height};
return {data: data, width: width, height: height};
};
PDFImage.prototype = {

View File

@ -431,6 +431,34 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
ctx.putImageData(tmpImgData, 0, 0);
}
function putBinaryImageMask(ctx, imgData) {
var width = imgData.width, height = imgData.height;
var tmpImgData = ctx.createImageData(width, height);
var data = imgData.data;
var tmpImgDataPixels = tmpImgData.data;
var dataPos = 0;
// Expand the mask so it can be used by the canvas. Any required inversion
// has already been handled.
var tmpPos = 3; // alpha component offset
for (var i = 0; i < height; i++) {
var mask = 0;
for (var j = 0; j < width; j++) {
if (!mask) {
var elem = data[dataPos++];
mask = 128;
}
if (!(elem & mask)) {
tmpImgDataPixels[tmpPos] = 255;
}
tmpPos += 4;
mask >>= 1;
}
}
ctx.putImageData(tmpImgData, 0, 0);
}
function copyCtxState(sourceCtx, destCtx) {
var properties = ['strokeStyle', 'fillStyle', 'fillRule', 'globalAlpha',
'lineWidth', 'lineCap', 'lineJoin', 'miterLimit',
@ -1611,7 +1639,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var maskCtx = maskCanvas.context;
maskCtx.save();
putBinaryImageData(maskCtx, img);
putBinaryImageMask(maskCtx, img);
maskCtx.globalCompositeOperation = 'source-in';
@ -1638,7 +1666,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
var maskCtx = maskCanvas.context;
maskCtx.save();
putBinaryImageData(maskCtx, image);
putBinaryImageMask(maskCtx, image);
maskCtx.globalCompositeOperation = 'source-in';