Remove the unused inline parameter from various methods/functions in PDFImage, and change a couple of methods to use Objects rather than plain parameters

The `inline` parameter is passed to a number of methods/functions in `PDFImage`, despite not actually being used. Its value is never checked, nor is it ever assigned to the current `PDFImage` instance (i.e. no `this.inline = inline` exists).
Looking briefly at the history of this code, I was also unable to find a point in time where `inline` was being used.

As far as I'm concerned, `inline` does nothing more than add clutter to already very unwieldy method/function signatures, hence why I'm proposing that we just remove it.
To further simplify call-sites using `PDFImage`/`NativeImageDecoder`, a number of methods/functions are changed to take Objects rather than a bunch of (somewhat) randomly ordered parameters.
This commit is contained in:
Jonas Jenwald 2017-09-21 12:14:05 +02:00
parent f206ee56bf
commit 5c961c76bb
2 changed files with 61 additions and 39 deletions

View File

@ -57,7 +57,8 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
isEvalSupported: true, isEvalSupported: true,
}; };
function NativeImageDecoder(xref, resources, handler, forceDataSchema) { function NativeImageDecoder({ xref, resources, handler,
forceDataSchema = false, }) {
this.xref = xref; this.xref = xref;
this.resources = resources; this.resources = resources;
this.handler = handler; this.handler = handler;
@ -86,8 +87,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
* Checks if the image can be decoded and displayed by the browser without any * Checks if the image can be decoded and displayed by the browser without any
* further processing such as color space conversions. * further processing such as color space conversions.
*/ */
NativeImageDecoder.isSupported = NativeImageDecoder.isSupported = function(image, xref, res) {
function NativeImageDecoder_isSupported(image, xref, res) {
var dict = image.dict; var dict = image.dict;
if (dict.has('DecodeParms') || dict.has('DP')) { if (dict.has('DecodeParms') || dict.has('DP')) {
return false; return false;
@ -99,8 +99,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
/** /**
* Checks if the image can be decoded by the browser. * Checks if the image can be decoded by the browser.
*/ */
NativeImageDecoder.isDecodable = NativeImageDecoder.isDecodable = function(image, xref, res) {
function NativeImageDecoder_isDecodable(image, xref, res) {
var dict = image.dict; var dict = image.dict;
if (dict.has('DecodeParms') || dict.has('DP')) { if (dict.has('DecodeParms') || dict.has('DP')) {
return false; return false;
@ -368,11 +367,14 @@ 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.getArray('Decode', 'D'); var decode = dict.getArray('Decode', 'D');
var inverseDecode = (!!decode && decode[0] > 0);
imgData = PDFImage.createMask(imgArray, width, height, imgData = PDFImage.createMask({
image instanceof DecodeStream, imgArray,
inverseDecode); width,
height,
imageIsFromDecodeStream: image instanceof DecodeStream,
inverseDecode: (!!decode && decode[0] > 0),
});
imgData.cached = true; imgData.cached = true;
args = [imgData]; args = [imgData];
operatorList.addOp(OPS.paintImageMaskXObject, args); operatorList.addOp(OPS.paintImageMaskXObject, args);
@ -392,8 +394,11 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
// Inlining small images into the queue as RGB data // Inlining small images into the queue as RGB data
if (inline && !softMask && !mask && !(image instanceof JpegStream) && if (inline && !softMask && !mask && !(image instanceof JpegStream) &&
(w + h) < SMALL_IMAGE_DIMENSIONS) { (w + h) < SMALL_IMAGE_DIMENSIONS) {
var imageObj = new PDFImage(this.xref, resources, image, let imageObj = new PDFImage({
inline, null, null); xref: this.xref,
res: resources,
image,
});
// We force the use of RGBA_32BPP images here, because we can't handle // We force the use of RGBA_32BPP images here, because we can't handle
// any other kind. // any other kind.
imgData = imageObj.createImageData(/* forceRGBA = */ true); imgData = imageObj.createImageData(/* forceRGBA = */ true);
@ -429,12 +434,21 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
if (nativeImageDecoderSupport === NativeImageDecoding.DECODE && if (nativeImageDecoderSupport === NativeImageDecoding.DECODE &&
(image instanceof JpegStream || mask instanceof JpegStream || (image instanceof JpegStream || mask instanceof JpegStream ||
softMask instanceof JpegStream)) { softMask instanceof JpegStream)) {
nativeImageDecoder = new NativeImageDecoder(this.xref, resources, nativeImageDecoder = new NativeImageDecoder({
this.handler, this.options.forceDataSchema); xref: this.xref,
resources,
handler: this.handler,
forceDataSchema: this.options.forceDataSchema,
});
} }
PDFImage.buildImage(this.handler, this.xref, resources, image, inline, PDFImage.buildImage({
nativeImageDecoder).then((imageObj) => { handler: this.handler,
xref: this.xref,
res: resources,
image,
nativeDecoder: nativeImageDecoder,
}).then((imageObj) => {
var imgData = imageObj.createImageData(/* forceRGBA = */ false); var imgData = imageObj.createImageData(/* forceRGBA = */ false);
this.handler.send('obj', [objId, this.pageIndex, 'Image', imgData], this.handler.send('obj', [objId, this.pageIndex, 'Image', imgData],
[imgData.data.buffer]); [imgData.data.buffer]);

View File

@ -74,7 +74,8 @@ var PDFImage = (function PDFImageClosure() {
return dest; return dest;
} }
function PDFImage(xref, res, image, inline, smask, mask, isMask) { function PDFImage({ xref, res, image, smask = null, mask = null,
isMask = false, }) {
this.image = image; this.image = image;
var dict = image.dict; var dict = image.dict;
if (dict.has('Filter')) { if (dict.has('Filter')) {
@ -160,14 +161,23 @@ var PDFImage = (function PDFImageClosure() {
} }
if (smask) { if (smask) {
this.smask = new PDFImage(xref, res, smask, false); this.smask = new PDFImage({
xref,
res,
image: smask,
});
} else if (mask) { } else if (mask) {
if (isStream(mask)) { if (isStream(mask)) {
var maskDict = mask.dict, imageMask = maskDict.get('ImageMask', 'IM'); var maskDict = mask.dict, imageMask = maskDict.get('ImageMask', 'IM');
if (!imageMask) { if (!imageMask) {
warn('Ignoring /Mask in image without /ImageMask.'); warn('Ignoring /Mask in image without /ImageMask.');
} else { } else {
this.mask = new PDFImage(xref, res, mask, false, null, null, true); this.mask = new PDFImage({
xref,
res,
image: mask,
isMask: true,
});
} }
} else { } else {
// Color key mask (just an array). // Color key mask (just an array).
@ -179,9 +189,8 @@ var PDFImage = (function PDFImageClosure() {
* Handles processing of image data and returns the Promise that is resolved * Handles processing of image data and returns the Promise that is resolved
* with a PDFImage when the image is ready to be used. * with a PDFImage when the image is ready to be used.
*/ */
PDFImage.buildImage = function PDFImage_buildImage(handler, xref, PDFImage.buildImage = function({ handler, xref, res, image,
res, image, inline, nativeDecoder = null, }) {
nativeDecoder) {
var imagePromise = handleImageData(image, nativeDecoder); var imagePromise = handleImageData(image, nativeDecoder);
var smaskPromise; var smaskPromise;
var maskPromise; var maskPromise;
@ -208,17 +217,19 @@ var PDFImage = (function PDFImageClosure() {
} }
} }
return Promise.all([imagePromise, smaskPromise, maskPromise]).then( return Promise.all([imagePromise, smaskPromise, maskPromise]).then(
function(results) { function([imageData, smaskData, maskData]) {
var imageData = results[0]; return new PDFImage({
var smaskData = results[1]; xref,
var maskData = results[2]; res,
return new PDFImage(xref, res, imageData, inline, smaskData, maskData); image: imageData,
smask: smaskData,
mask: maskData,
});
}); });
}; };
PDFImage.createMask = PDFImage.createMask = function({ imgArray, width, height,
function PDFImage_createMask(imgArray, width, height, imageIsFromDecodeStream, inverseDecode, }) {
imageIsFromDecodeStream, inverseDecode) {
// |imgArray| might not contain full data for every pixel of the mask, so // |imgArray| might not contain full data for every pixel of the mask, so
// we need to distinguish between |computedLength| and |actualLength|. // we need to distinguish between |computedLength| and |actualLength|.
@ -271,7 +282,7 @@ var PDFImage = (function PDFImageClosure() {
this.mask && this.mask.height || 0); this.mask && this.mask.height || 0);
}, },
decodeBuffer: function PDFImage_decodeBuffer(buffer) { decodeBuffer(buffer) {
var bpc = this.bpc; var bpc = this.bpc;
var numComps = this.numComps; var numComps = this.numComps;
@ -297,7 +308,7 @@ var PDFImage = (function PDFImageClosure() {
} }
}, },
getComponents: function PDFImage_getComponents(buffer) { getComponents(buffer) {
var bpc = this.bpc; var bpc = this.bpc;
// This image doesn't require any extra work. // This image doesn't require any extra work.
@ -374,8 +385,7 @@ var PDFImage = (function PDFImageClosure() {
return output; return output;
}, },
fillOpacity: function PDFImage_fillOpacity(rgbaBuf, width, height, fillOpacity(rgbaBuf, width, height, actualHeight, image) {
actualHeight, image) {
var smask = this.smask; var smask = this.smask;
var mask = this.mask; var mask = this.mask;
var alphaBuf, sw, sh, i, ii, j; var alphaBuf, sw, sh, i, ii, j;
@ -441,7 +451,7 @@ var PDFImage = (function PDFImageClosure() {
} }
}, },
undoPreblend: function PDFImage_undoPreblend(buffer, width, height) { undoPreblend(buffer, width, height) {
var matte = this.smask && this.smask.matte; var matte = this.smask && this.smask.matte;
if (!matte) { if (!matte) {
return; return;
@ -472,7 +482,7 @@ var PDFImage = (function PDFImageClosure() {
} }
}, },
createImageData: function PDFImage_createImageData(forceRGBA) { createImageData(forceRGBA = false) {
var drawWidth = this.drawWidth; var drawWidth = this.drawWidth;
var drawHeight = this.drawHeight; var drawHeight = this.drawHeight;
var imgData = { // other fields are filled in below var imgData = { // other fields are filled in below
@ -581,7 +591,7 @@ var PDFImage = (function PDFImageClosure() {
return imgData; return imgData;
}, },
fillGrayBuffer: function PDFImage_fillGrayBuffer(buffer) { fillGrayBuffer(buffer) {
var numComps = this.numComps; var numComps = this.numComps;
if (numComps !== 1) { if (numComps !== 1) {
throw new FormatError( throw new FormatError(
@ -627,9 +637,7 @@ var PDFImage = (function PDFImageClosure() {
} }
}, },
getImageBytes: function PDFImage_getImageBytes(length, getImageBytes(length, drawWidth, drawHeight, forceRGB = false) {
drawWidth, drawHeight,
forceRGB) {
this.image.reset(); this.image.reset();
this.image.drawWidth = drawWidth || this.width; this.image.drawWidth = drawWidth || this.width;
this.image.drawHeight = drawHeight || this.height; this.image.drawHeight = drawHeight || this.height;