Merge pull request #21040 from Snuffleupagus/constructSampled-flat-arrays

Use the original array-data when parsing Type 0 (Sampled) Functions
This commit is contained in:
calixteman 2026-04-04 19:56:54 +02:00 committed by GitHub
commit b5dd450041
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -160,22 +160,13 @@ class PDFFunction {
} }
static constructSampled(factory, fn, dict) { static constructSampled(factory, fn, dict) {
function toMultiArray(arr) {
const inputLength = arr.length;
const out = [];
let index = 0;
for (let i = 0; i < inputLength; i += 2) {
out[index++] = [arr[i], arr[i + 1]];
}
return out;
}
// See chapter 3, page 109 of the PDF reference // See chapter 3, page 109 of the PDF reference
function interpolate(x, xmin, xmax, ymin, ymax) { function interpolate(x, xmin, xmax, ymin, ymax) {
return ymin + (x - xmin) * ((ymax - ymin) / (xmax - xmin)); return ymin + (x - xmin) * ((ymax - ymin) / (xmax - xmin));
} }
let domain = toNumberArray(dict.getArray("Domain")); const domain = toNumberArray(dict.getArray("Domain"));
let range = toNumberArray(dict.getArray("Range")); const range = toNumberArray(dict.getArray("Range"));
if (!domain || !range) { if (!domain || !range) {
throw new FormatError("No domain or range"); throw new FormatError("No domain or range");
@ -184,9 +175,6 @@ class PDFFunction {
const inputSize = domain.length / 2; const inputSize = domain.length / 2;
const outputSize = range.length / 2; const outputSize = range.length / 2;
domain = toMultiArray(domain);
range = toMultiArray(range);
const size = toNumberArray(dict.getArray("Size")); const size = toNumberArray(dict.getArray("Size"));
const bps = dict.get("BitsPerSample"); const bps = dict.get("BitsPerSample");
const order = dict.get("Order") || 1; const order = dict.get("Order") || 1;
@ -200,14 +188,11 @@ class PDFFunction {
if (!encode) { if (!encode) {
encode = []; encode = [];
for (let i = 0; i < inputSize; ++i) { for (let i = 0; i < inputSize; ++i) {
encode.push([0, size[i] - 1]); encode.push(0, size[i] - 1);
} }
} else {
encode = toMultiArray(encode);
} }
let decode = toNumberArray(dict.getArray("Decode")); const decode = toNumberArray(dict.getArray("Decode")) || range;
decode = !decode ? range : toMultiArray(decode);
const samples = this.getSampleArray(size, outputSize, bps, fn); const samples = this.getSampleArray(size, outputSize, bps, fn);
// const mask = 2 ** bps - 1; // const mask = 2 ** bps - 1;
@ -227,8 +212,8 @@ class PDFFunction {
// Map x_i to y_j for 0 <= i < m using the sampled function. // Map x_i to y_j for 0 <= i < m using the sampled function.
for (i = 0; i < inputSize; ++i) { for (i = 0; i < inputSize; ++i) {
// x_i' = min(max(x_i, Domain_2i), Domain_2i+1) // x_i' = min(max(x_i, Domain_2i), Domain_2i+1)
const domain_2i = domain[i][0]; const domain_2i = domain[2 * i];
const domain_2i_1 = domain[i][1]; const domain_2i_1 = domain[2 * i + 1];
const xi = MathClamp(src[srcOffset + i], domain_2i, domain_2i_1); const xi = MathClamp(src[srcOffset + i], domain_2i, domain_2i_1);
// e_i = Interpolate(x_i', Domain_2i, Domain_2i+1, // e_i = Interpolate(x_i', Domain_2i, Domain_2i+1,
@ -237,8 +222,8 @@ class PDFFunction {
xi, xi,
domain_2i, domain_2i,
domain_2i_1, domain_2i_1,
encode[i][0], encode[2 * i],
encode[i][1] encode[2 * i + 1]
); );
// e_i' = min(max(e_i, 0), Size_i - 1) // e_i' = min(max(e_i, 0), Size_i - 1)
@ -274,10 +259,10 @@ class PDFFunction {
// r_j' = Interpolate(r_j, 0, 2^BitsPerSample - 1, // r_j' = Interpolate(r_j, 0, 2^BitsPerSample - 1,
// Decode_2j, Decode_2j+1) // Decode_2j, Decode_2j+1)
rj = interpolate(rj, 0, 1, decode[j][0], decode[j][1]); rj = interpolate(rj, 0, 1, decode[2 * j], decode[2 * j + 1]);
// y_j = min(max(r_j, range_2j), range_2j+1) // y_j = min(max(r_j, range_2j), range_2j+1)
dest[destOffset + j] = MathClamp(rj, range[j][0], range[j][1]); dest[destOffset + j] = MathClamp(rj, range[2 * j], range[2 * j + 1]);
} }
}; };
} }