Skip to content

Commit

Permalink
Merge pull request #10494 from Snuffleupagus/ColorSpace-isDefaultDecode
Browse files Browse the repository at this point in the history
Reduce unnecessary duplication of the `isDefaultDecode` methods on `ColorSpace` instances
  • Loading branch information
timvandermeij committed Jan 25, 2019
2 parents e2701d5 + 29f36d7 commit 1eee1c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
46 changes: 16 additions & 30 deletions src/core/colorspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ class ColorSpace {
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
* how many alpha components there are in the dest array; it will be either
Expand Down Expand Up @@ -379,18 +386,17 @@ class ColorSpace {
/**
* 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
* 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
* slightly different.
* @param {Array} decode Decode map (usually from an image).
* @param {Number} n Number of components the color space has.
* @param {Array} decode - Decode map (usually from an image).
* @param {Number} numComps - Number of components the color space has.
*/
static isDefaultDecode(decode, n) {
static isDefaultDecode(decode, numComps) {
if (!Array.isArray(decode)) {
return true;
}

if (n * 2 !== decode.length) {
if (numComps * 2 !== decode.length) {
warn('The decode map is not the correct length');
return true;
}
Expand Down Expand Up @@ -491,17 +497,17 @@ class AlternateCS extends ColorSpace {
this.base.numComps / this.numComps,
alpha01);
}

isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
}

class PatternCS extends ColorSpace {
constructor(baseCS) {
super('Pattern', null);
this.base = baseCS;
}

isDefaultDecode(decodeMap, bpc) {
unreachable('Should not call PatternCS.isDefaultDecode');
}
}

/**
Expand Down Expand Up @@ -619,10 +625,6 @@ class DeviceGrayCS extends ColorSpace {
getOutputLength(inputLength, alpha01) {
return inputLength * (3 + alpha01);
}

isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
}

/**
Expand Down Expand Up @@ -671,10 +673,6 @@ class DeviceRgbCS extends ColorSpace {
isPassthrough(bits) {
return bits === 8;
}

isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
}

/**
Expand Down Expand Up @@ -754,10 +752,6 @@ const DeviceCmykCS = (function DeviceCmykCSClosure() {
getOutputLength(inputLength, alpha01) {
return (inputLength / 4 * (3 + alpha01)) | 0;
}

isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
}
return DeviceCmykCS;
})();
Expand Down Expand Up @@ -857,10 +851,6 @@ const CalGrayCS = (function CalGrayCSClosure() {
getOutputLength(inputLength, alpha01) {
return inputLength * (3 + alpha01);
}

isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
}
return CalGrayCS;
})();
Expand Down Expand Up @@ -1139,10 +1129,6 @@ const CalRGBCS = (function CalRGBCSClosure() {
getOutputLength(inputLength, alpha01) {
return (inputLength * (3 + alpha01) / 3) | 0;
}

isDefaultDecode(decodeMap, bpc) {
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
}
}
return CalRGBCS;
})();
Expand Down
3 changes: 2 additions & 1 deletion src/core/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ var PDFImage = (function PDFImageClosure() {
if (this.decode &&
((this.colorSpace &&
!this.colorSpace.isDefaultDecode(this.decode, bitsPerComponent)) ||
(isMask && !ColorSpace.isDefaultDecode(this.decode, 1)))) {
(isMask &&
!ColorSpace.isDefaultDecode(this.decode, /* numComps = */ 1)))) {
this.needsDecode = true;
// Do some preprocessing to avoid more math.
var max = (1 << bitsPerComponent) - 1;
Expand Down

0 comments on commit 1eee1c5

Please sign in to comment.