Merge pull request #10494 from Snuffleupagus/ColorSpace-isDefaultDecode

Reduce unnecessary duplication of the `isDefaultDecode` methods on `ColorSpace` instances
This commit is contained in:
Tim van der Meij 2019-01-25 22:43:28 +01:00 committed by GitHub
commit 1eee1c5d2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 31 deletions

View File

@ -109,6 +109,13 @@ class ColorSpace {
return false; return false;
} }
/**
* Refer to the static `ColorSpace.isDefaultDecode` method below.
*/
isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
/** /**
* Fills in the RGB colors in the destination buffer. alpha01 indicates * Fills in the RGB colors in the destination buffer. alpha01 indicates
* how many alpha components there are in the dest array; it will be either * how many alpha components there are in the dest array; it will be either
@ -379,18 +386,17 @@ class ColorSpace {
/** /**
* Checks if a decode map matches the default decode map for a color space. * Checks if a decode map matches the default decode map for a color space.
* This handles the general decode maps where there are two values per * This handles the general decode maps where there are two values per
* component. e.g. [0, 1, 0, 1, 0, 1] for a RGB color. * component, e.g. [0, 1, 0, 1, 0, 1] for a RGB color.
* This does not handle Lab, Indexed, or Pattern decode maps since they are * This does not handle Lab, Indexed, or Pattern decode maps since they are
* slightly different. * slightly different.
* @param {Array} decode Decode map (usually from an image). * @param {Array} decode - Decode map (usually from an image).
* @param {Number} n Number of components the color space has. * @param {Number} numComps - Number of components the color space has.
*/ */
static isDefaultDecode(decode, n) { static isDefaultDecode(decode, numComps) {
if (!Array.isArray(decode)) { if (!Array.isArray(decode)) {
return true; return true;
} }
if (numComps * 2 !== decode.length) {
if (n * 2 !== decode.length) {
warn('The decode map is not the correct length'); warn('The decode map is not the correct length');
return true; return true;
} }
@ -491,10 +497,6 @@ class AlternateCS extends ColorSpace {
this.base.numComps / this.numComps, this.base.numComps / this.numComps,
alpha01); alpha01);
} }
isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
} }
class PatternCS extends ColorSpace { class PatternCS extends ColorSpace {
@ -502,6 +504,10 @@ class PatternCS extends ColorSpace {
super('Pattern', null); super('Pattern', null);
this.base = baseCS; this.base = baseCS;
} }
isDefaultDecode(decodeMap, bpc) {
unreachable('Should not call PatternCS.isDefaultDecode');
}
} }
/** /**
@ -619,10 +625,6 @@ class DeviceGrayCS extends ColorSpace {
getOutputLength(inputLength, alpha01) { getOutputLength(inputLength, alpha01) {
return inputLength * (3 + alpha01); return inputLength * (3 + alpha01);
} }
isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
} }
/** /**
@ -671,10 +673,6 @@ class DeviceRgbCS extends ColorSpace {
isPassthrough(bits) { isPassthrough(bits) {
return bits === 8; return bits === 8;
} }
isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
} }
/** /**
@ -754,10 +752,6 @@ const DeviceCmykCS = (function DeviceCmykCSClosure() {
getOutputLength(inputLength, alpha01) { getOutputLength(inputLength, alpha01) {
return (inputLength / 4 * (3 + alpha01)) | 0; return (inputLength / 4 * (3 + alpha01)) | 0;
} }
isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
} }
return DeviceCmykCS; return DeviceCmykCS;
})(); })();
@ -857,10 +851,6 @@ const CalGrayCS = (function CalGrayCSClosure() {
getOutputLength(inputLength, alpha01) { getOutputLength(inputLength, alpha01) {
return inputLength * (3 + alpha01); return inputLength * (3 + alpha01);
} }
isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
} }
return CalGrayCS; return CalGrayCS;
})(); })();
@ -1139,10 +1129,6 @@ const CalRGBCS = (function CalRGBCSClosure() {
getOutputLength(inputLength, alpha01) { getOutputLength(inputLength, alpha01) {
return (inputLength * (3 + alpha01) / 3) | 0; return (inputLength * (3 + alpha01) / 3) | 0;
} }
isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
} }
return CalRGBCS; return CalRGBCS;
})(); })();

View File

@ -171,7 +171,8 @@ var PDFImage = (function PDFImageClosure() {
if (this.decode && if (this.decode &&
((this.colorSpace && ((this.colorSpace &&
!this.colorSpace.isDefaultDecode(this.decode, bitsPerComponent)) || !this.colorSpace.isDefaultDecode(this.decode, bitsPerComponent)) ||
(isMask && !ColorSpace.isDefaultDecode(this.decode, 1)))) { (isMask &&
!ColorSpace.isDefaultDecode(this.decode, /* numComps = */ 1)))) {
this.needsDecode = true; this.needsDecode = true;
// Do some preprocessing to avoid more math. // Do some preprocessing to avoid more math.
var max = (1 << bitsPerComponent) - 1; var max = (1 << bitsPerComponent) - 1;