Transfer image masks when when possible, instead of copying.

This commit is contained in:
Nicholas Nethercote 2014-03-06 21:27:32 -08:00
parent cb5bb0cec7
commit 00c1cff405
2 changed files with 23 additions and 15 deletions

View File

@ -14,10 +14,10 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals assert, assertWellFormed, ColorSpace, Dict, Encodings, error, /* globals assert, assertWellFormed, ColorSpace, DecodeStream, Dict, Encodings,
ErrorFont, Font, FONT_IDENTITY_MATRIX, fontCharsToUnicode, FontFlags, error, ErrorFont, Font, FONT_IDENTITY_MATRIX, fontCharsToUnicode,
ImageKind, info, isArray, isCmd, isDict, isEOF, isName, isNum, FontFlags, ImageKind, info, isArray, isCmd, isDict, isEOF, isName,
isStream, isString, JpegStream, Lexer, Metrics, Name, Parser, isNum, isStream, isString, JpegStream, Lexer, Metrics, Name, Parser,
Pattern, PDFImage, PDFJS, serifFonts, stdFontMap, symbolsFonts, Pattern, PDFImage, PDFJS, serifFonts, stdFontMap, symbolsFonts,
getTilingPatternIR, warn, Util, Promise, LegacyPromise, getTilingPatternIR, warn, Util, Promise, LegacyPromise,
RefSetCache, isRef, TextRenderingMode, CMapFactory, OPS, RefSetCache, isRef, TextRenderingMode, CMapFactory, OPS,
@ -146,10 +146,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var bitStrideLength = (width + 7) >> 3; var bitStrideLength = (width + 7) >> 3;
var imgArray = image.getBytes(bitStrideLength * height); var imgArray = image.getBytes(bitStrideLength * height);
var decode = dict.get('Decode', 'D'); var decode = dict.get('Decode', 'D');
var canTransfer = image instanceof DecodeStream;
var inverseDecode = !!decode && decode[0] > 0; var inverseDecode = !!decode && decode[0] > 0;
operatorList.addOp(OPS.paintImageMaskXObject, operatorList.addOp(OPS.paintImageMaskXObject,
[PDFImage.createMask(imgArray, width, height, inverseDecode)] [PDFImage.createMask(imgArray, width, height, canTransfer,
inverseDecode)]
); );
return; return;
} }

View File

@ -209,19 +209,25 @@ var PDFImage = (function PDFImageClosure() {
return temp; return temp;
}; };
PDFImage.createMask = function PDFImage_createMask(imgArray, width, height, PDFImage.createMask =
inverseDecode) { function PDFImage_createMask(imgArray, width, height, canTransfer,
// Copy imgArray into a typed array (inverting if necessary) so it can be inverseDecode) {
// transferred to the main thread. // If imgArray came from a DecodeStream, we're safe to transfer it.
// Otherwise, copy it.
var actualLength = imgArray.byteLength; var actualLength = imgArray.byteLength;
var data = new Uint8Array(actualLength); var data;
if (canTransfer) {
data = imgArray;
} else {
data = new Uint8Array(actualLength);
data.set(imgArray);
}
// Invert if necessary. It's safe to modify the array -- whether it's the
// original or a copy, we're about to transfer it anyway, so nothing else
// in this thread can be relying on its contents.
if (inverseDecode) { if (inverseDecode) {
for (var i = 0; i < actualLength; i++) { for (var i = 0; i < actualLength; i++) {
data[i] = ~imgArray[i]; data[i] = ~data[i];
}
} else {
for (var i = 0; i < actualLength; i++) {
data[i] = imgArray[i];
} }
} }