Skip to content

Commit 220a289

Browse files
Merge pull request #19791 from Snuffleupagus/IndexedCS-round-clamp
Handle non-integer and out-of-range values correctly in Indexed color spaces
2 parents 22657e2 + fbc4f4b commit 220a289

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

src/core/colorspace.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ class IndexedCS extends ColorSpace {
429429
constructor(base, highVal, lookup) {
430430
super("Indexed", 1);
431431
this.base = base;
432+
this.highVal = highVal;
432433

433434
const length = base.numComps * (highVal + 1);
434435
this.lookup = new Uint8Array(length);
@@ -452,9 +453,10 @@ class IndexedCS extends ColorSpace {
452453
'IndexedCS.getRgbItem: Unsupported "dest" type.'
453454
);
454455
}
455-
const numComps = this.base.numComps;
456-
const start = src[srcOffset] * numComps;
457-
this.base.getRgbBuffer(this.lookup, start, 1, dest, destOffset, 8, 0);
456+
const { base, highVal, lookup } = this;
457+
const start =
458+
MathClamp(Math.round(src[srcOffset]), 0, highVal) * base.numComps;
459+
base.getRgbBuffer(lookup, start, 1, dest, destOffset, 8, 0);
458460
}
459461

460462
getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
@@ -464,13 +466,13 @@ class IndexedCS extends ColorSpace {
464466
'IndexedCS.getRgbBuffer: Unsupported "dest" type.'
465467
);
466468
}
467-
const base = this.base;
468-
const numComps = base.numComps;
469+
const { base, highVal, lookup } = this;
470+
const { numComps } = base;
469471
const outputDelta = base.getOutputLength(numComps, alpha01);
470-
const lookup = this.lookup;
471472

472473
for (let i = 0; i < count; ++i) {
473-
const lookupPos = src[srcOffset++] * numComps;
474+
const lookupPos =
475+
MathClamp(Math.round(src[srcOffset++]), 0, highVal) * numComps;
474476
base.getRgbBuffer(lookup, lookupPos, 1, dest, destOffset, 8, alpha01);
475477
destOffset += outputDelta;
476478
}

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
!rotation.pdf
192192
!simpletype3font.pdf
193193
!Type3WordSpacing.pdf
194+
!IndexedCS_negative_and_high.pdf
194195
!sizes.pdf
195196
!javauninstall-7r.pdf
196197
!file_url_link.pdf
1.76 KB
Binary file not shown.

test/test_manifest.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,6 +2851,14 @@
28512851
"rounds": 1,
28522852
"type": "eq"
28532853
},
2854+
{
2855+
"id": "IndexedCS_negative_and_high",
2856+
"file": "pdfs/IndexedCS_negative_and_high.pdf",
2857+
"md5": "3bbf5e6f1cf49acb075fabb0726b5fe4",
2858+
"link": false,
2859+
"rounds": 1,
2860+
"type": "eq"
2861+
},
28542862
{
28552863
"id": "issue13372",
28562864
"file": "pdfs/issue13372.pdf",

0 commit comments

Comments
 (0)