Remove the closure from the CalGrayCS class

Now that modern JavaScript is fully supported also in the worker-thread we no longer need to keep old closures, which slightly reduces the size of the code.
This commit is contained in:
Jonas Jenwald 2023-09-15 15:53:16 +02:00
parent d2c8997380
commit 4d615f087f

View File

@ -886,26 +886,7 @@ class DeviceCmykCS extends ColorSpace {
*
* The default color is `new Float32Array([0])`.
*/
const CalGrayCS = (function CalGrayCSClosure() {
function convertToRgb(cs, src, srcOffset, dest, destOffset, scale) {
// A represents a gray component of a calibrated gray space.
// A <---> AG in the spec
const A = src[srcOffset] * scale;
const AG = A ** cs.G;
// Computes L as per spec. ( = cs.YW * AG )
// Except if other than default BlackPoint values are used.
const L = cs.YW * AG;
// http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html, Ch 4.
// Convert values to rgb range [0, 255].
const val = Math.max(295.8 * L ** 0.3333333333333333 - 40.8, 0);
dest[destOffset] = val;
dest[destOffset + 1] = val;
dest[destOffset + 2] = val;
}
// eslint-disable-next-line no-shadow
class CalGrayCS extends ColorSpace {
class CalGrayCS extends ColorSpace {
constructor(whitePoint, blackPoint, gamma) {
super("CalGray", 1);
@ -914,25 +895,15 @@ const CalGrayCS = (function CalGrayCSClosure() {
"WhitePoint missing - required for color space CalGray"
);
}
blackPoint ||= [0, 0, 0];
gamma ||= 1;
// Translate arguments to spec variables.
this.XW = whitePoint[0];
this.YW = whitePoint[1];
this.ZW = whitePoint[2];
this.XB = blackPoint[0];
this.YB = blackPoint[1];
this.ZB = blackPoint[2];
this.G = gamma;
[this.XW, this.YW, this.ZW] = whitePoint;
[this.XB, this.YB, this.ZB] = blackPoint || [0, 0, 0];
this.G = gamma || 1;
// Validate variables as per spec.
if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
throw new FormatError(
`Invalid WhitePoint components for ${this.name}` +
", no fallback available"
`Invalid WhitePoint components for ${this.name}, no fallback available`
);
}
@ -950,13 +921,29 @@ const CalGrayCS = (function CalGrayCSClosure() {
if (this.G < 1) {
info(
`Invalid Gamma: ${this.G} for ${this.name}, ` +
"falling back to default."
`Invalid Gamma: ${this.G} for ${this.name}, falling back to default.`
);
this.G = 1;
}
}
#toRgb(src, srcOffset, dest, destOffset, scale) {
// A represents a gray component of a calibrated gray space.
// A <---> AG in the spec
const A = src[srcOffset] * scale;
const AG = A ** this.G;
// Computes L as per spec. ( = this.YW * AG )
// Except if other than default BlackPoint values are used.
const L = this.YW * AG;
// http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html, Ch 4.
// Convert values to rgb range [0, 255].
const val = Math.max(295.8 * L ** 0.3333333333333333 - 40.8, 0);
dest[destOffset] = val;
dest[destOffset + 1] = val;
dest[destOffset + 2] = val;
}
getRgbItem(src, srcOffset, dest, destOffset) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert(
@ -964,7 +951,7 @@ const CalGrayCS = (function CalGrayCSClosure() {
'CalGrayCS.getRgbItem: Unsupported "dest" type.'
);
}
convertToRgb(this, src, srcOffset, dest, destOffset, 1);
this.#toRgb(src, srcOffset, dest, destOffset, 1);
}
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
@ -977,7 +964,7 @@ const CalGrayCS = (function CalGrayCSClosure() {
const scale = 1 / ((1 << bits) - 1);
for (let i = 0; i < count; ++i) {
convertToRgb(this, src, srcOffset, dest, destOffset, scale);
this.#toRgb(src, srcOffset, dest, destOffset, scale);
srcOffset += 1;
destOffset += 3 + alpha01;
}
@ -986,9 +973,7 @@ const CalGrayCS = (function CalGrayCSClosure() {
getOutputLength(inputLength, alpha01) {
return inputLength * (3 + alpha01);
}
}
return CalGrayCS;
})();
}
/**
* CalRGBCS: Based on "PDF Reference, Sixth Ed", p.247