Remove the closure from the LabCS 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:30 +02:00
parent 52fa66a98b
commit 8cb5d01acd

View File

@ -1280,76 +1280,7 @@ class CalRGBCS extends ColorSpace {
*
* The default color is `new Float32Array([0, 0, 0])`.
*/
const LabCS = (function LabCSClosure() {
// Function g(x) from spec
function fn_g(x) {
return x >= 6 / 29 ? x ** 3 : (108 / 841) * (x - 4 / 29);
}
function decode(value, high1, low2, high2) {
return low2 + (value * (high2 - low2)) / high1;
}
// If decoding is needed maxVal should be 2^bits per component - 1.
function convertToRgb(cs, src, srcOffset, maxVal, dest, destOffset) {
// XXX: Lab input is in the range of [0, 100], [amin, amax], [bmin, bmax]
// not the usual [0, 1]. If a command like setFillColor is used the src
// values will already be within the correct range. However, if we are
// converting an image we have to map the values to the correct range given
// above.
// Ls,as,bs <---> L*,a*,b* in the spec
let Ls = src[srcOffset];
let as = src[srcOffset + 1];
let bs = src[srcOffset + 2];
if (maxVal !== false) {
Ls = decode(Ls, maxVal, 0, 100);
as = decode(as, maxVal, cs.amin, cs.amax);
bs = decode(bs, maxVal, cs.bmin, cs.bmax);
}
// Adjust limits of 'as' and 'bs'
if (as > cs.amax) {
as = cs.amax;
} else if (as < cs.amin) {
as = cs.amin;
}
if (bs > cs.bmax) {
bs = cs.bmax;
} else if (bs < cs.bmin) {
bs = cs.bmin;
}
// Computes intermediate variables X,Y,Z as per spec
const M = (Ls + 16) / 116;
const L = M + as / 500;
const N = M - bs / 200;
const X = cs.XW * fn_g(L);
const Y = cs.YW * fn_g(M);
const Z = cs.ZW * fn_g(N);
let r, g, b;
// Using different conversions for D50 and D65 white points,
// per http://www.color.org/srgb.pdf
if (cs.ZW < 1) {
// Assuming D50 (X=0.9642, Y=1.00, Z=0.8249)
r = X * 3.1339 + Y * -1.617 + Z * -0.4906;
g = X * -0.9785 + Y * 1.916 + Z * 0.0333;
b = X * 0.072 + Y * -0.229 + Z * 1.4057;
} else {
// Assuming D65 (X=0.9505, Y=1.00, Z=1.0888)
r = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
g = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
b = X * 0.0557 + Y * -0.204 + Z * 1.057;
}
// Convert the color values to the [0,255] range (clamping is automatic).
dest[destOffset] = Math.sqrt(r) * 255;
dest[destOffset + 1] = Math.sqrt(g) * 255;
dest[destOffset + 2] = Math.sqrt(b) * 255;
}
// eslint-disable-next-line no-shadow
class LabCS extends ColorSpace {
class LabCS extends ColorSpace {
constructor(whitePoint, blackPoint, range) {
super("Lab", 3);
@ -1358,23 +1289,15 @@ const LabCS = (function LabCSClosure() {
"WhitePoint missing - required for color space Lab"
);
}
blackPoint ||= [0, 0, 0];
range ||= [-100, 100, -100, 100];
// Translate args to spec variables
this.XW = whitePoint[0];
this.YW = whitePoint[1];
this.ZW = whitePoint[2];
this.amin = range[0];
this.amax = range[1];
this.bmin = range[2];
this.bmax = range[3];
[this.XW, this.YW, this.ZW] = whitePoint;
[this.amin, this.amax, this.bmin, this.bmax] = range || [
-100, 100, -100, 100,
];
// These are here just for completeness - the spec doesn't offer any
// formulas that use BlackPoint in Lab
this.XB = blackPoint[0];
this.YB = blackPoint[1];
this.ZB = blackPoint[2];
[this.XB, this.YB, this.ZB] = blackPoint || [0, 0, 0];
// Validate vars as per spec
if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
@ -1397,6 +1320,73 @@ const LabCS = (function LabCSClosure() {
}
}
// Function g(x) from spec
#fn_g(x) {
return x >= 6 / 29 ? x ** 3 : (108 / 841) * (x - 4 / 29);
}
#decode(value, high1, low2, high2) {
return low2 + (value * (high2 - low2)) / high1;
}
// If decoding is needed maxVal should be 2^bits per component - 1.
#toRgb(src, srcOffset, maxVal, dest, destOffset) {
// XXX: Lab input is in the range of [0, 100], [amin, amax], [bmin, bmax]
// not the usual [0, 1]. If a command like setFillColor is used the src
// values will already be within the correct range. However, if we are
// converting an image we have to map the values to the correct range given
// above.
// Ls,as,bs <---> L*,a*,b* in the spec
let Ls = src[srcOffset];
let as = src[srcOffset + 1];
let bs = src[srcOffset + 2];
if (maxVal !== false) {
Ls = this.#decode(Ls, maxVal, 0, 100);
as = this.#decode(as, maxVal, this.amin, this.amax);
bs = this.#decode(bs, maxVal, this.bmin, this.bmax);
}
// Adjust limits of 'as' and 'bs'
if (as > this.amax) {
as = this.amax;
} else if (as < this.amin) {
as = this.amin;
}
if (bs > this.bmax) {
bs = this.bmax;
} else if (bs < this.bmin) {
bs = this.bmin;
}
// Computes intermediate variables X,Y,Z as per spec
const M = (Ls + 16) / 116;
const L = M + as / 500;
const N = M - bs / 200;
const X = this.XW * this.#fn_g(L);
const Y = this.YW * this.#fn_g(M);
const Z = this.ZW * this.#fn_g(N);
let r, g, b;
// Using different conversions for D50 and D65 white points,
// per http://www.color.org/srgb.pdf
if (this.ZW < 1) {
// Assuming D50 (X=0.9642, Y=1.00, Z=0.8249)
r = X * 3.1339 + Y * -1.617 + Z * -0.4906;
g = X * -0.9785 + Y * 1.916 + Z * 0.0333;
b = X * 0.072 + Y * -0.229 + Z * 1.4057;
} else {
// Assuming D65 (X=0.9505, Y=1.00, Z=1.0888)
r = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
g = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
b = X * 0.0557 + Y * -0.204 + Z * 1.057;
}
// Convert the color values to the [0,255] range (clamping is automatic).
dest[destOffset] = Math.sqrt(r) * 255;
dest[destOffset + 1] = Math.sqrt(g) * 255;
dest[destOffset + 2] = Math.sqrt(b) * 255;
}
getRgbItem(src, srcOffset, dest, destOffset) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert(
@ -1404,7 +1394,7 @@ const LabCS = (function LabCSClosure() {
'LabCS.getRgbItem: Unsupported "dest" type.'
);
}
convertToRgb(this, src, srcOffset, false, dest, destOffset);
this.#toRgb(src, srcOffset, false, dest, destOffset);
}
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
@ -1416,7 +1406,7 @@ const LabCS = (function LabCSClosure() {
}
const maxVal = (1 << bits) - 1;
for (let i = 0; i < count; i++) {
convertToRgb(this, src, srcOffset, maxVal, dest, destOffset);
this.#toRgb(src, srcOffset, maxVal, dest, destOffset);
srcOffset += 3;
destOffset += 3 + alpha01;
}
@ -1435,8 +1425,6 @@ const LabCS = (function LabCSClosure() {
get usesZeroToOneRange() {
return shadow(this, "usesZeroToOneRange", false);
}
}
return LabCS;
})();
}
export { ColorSpace };