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

View File

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