Skip to content

Commit 4f7f3ba

Browse files
Merge pull request #21587 from calixteman/smask_in_data
Implement SmaskInData == 2 for JPX images
2 parents 8c3941a + f554d0c commit 4f7f3ba

4 files changed

Lines changed: 61 additions & 17 deletions

File tree

src/core/image.js

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class PDFImage {
119119
this.jpxDecoderOptions = {
120120
numComponents: 0,
121121
isIndexedColormap: false,
122-
smaskInData: dict.has("SMaskInData"),
122+
smaskInData: dict.get("SMaskInData") >= 1,
123123
reducePower,
124124
};
125125
if (reducePower) {
@@ -196,6 +196,25 @@ class PDFImage {
196196
if (!this.imageMask) {
197197
let colorSpace = dict.getRaw("CS") || dict.getRaw("ColorSpace");
198198
const hasColorSpace = !!colorSpace;
199+
200+
if (
201+
this.jpxDecoderOptions?.smaskInData &&
202+
dict.get("SMaskInData") === 2
203+
) {
204+
this.jpxPremultiplied = true;
205+
if (this.matte) {
206+
const matteColorSpace = ColorSpaceUtils.parse({
207+
cs: hasColorSpace ? colorSpace : Name.get("DeviceRGB"),
208+
xref,
209+
resources: isInline ? res : null,
210+
pdfFunctionFactory,
211+
globalColorSpaceCache,
212+
localColorSpaceCache,
213+
});
214+
this.preblendMatte = matteColorSpace.getRgb(this.matte, 0);
215+
}
216+
}
217+
199218
if (!hasColorSpace) {
200219
if (this.jpxDecoderOptions) {
201220
colorSpace = Name.get("DeviceRGBA");
@@ -654,22 +673,7 @@ class PDFImage {
654673
});
655674
}
656675

657-
undoPreblend(buffer, width, height) {
658-
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
659-
assert(
660-
buffer instanceof Uint8ClampedArray,
661-
'PDFImage.undoPreblend: Unsupported "buffer" type.'
662-
);
663-
}
664-
const matte = this.smask?.matte;
665-
if (!matte) {
666-
return;
667-
}
668-
const matteRgb = this.colorSpace.getRgb(matte, 0);
669-
const matteR = matteRgb[0];
670-
const matteG = matteRgb[1];
671-
const matteB = matteRgb[2];
672-
const length = width * height * 4;
676+
static #undoPreblend(buffer, length, matteR, matteG, matteB) {
673677
for (let i = 0; i < length; i += 4) {
674678
const alpha = buffer[i + 3];
675679
if (alpha === 0) {
@@ -687,6 +691,27 @@ class PDFImage {
687691
}
688692
}
689693

694+
undoPreblend(buffer, width, height) {
695+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
696+
assert(
697+
buffer instanceof Uint8ClampedArray,
698+
'PDFImage.undoPreblend: Unsupported "buffer" type.'
699+
);
700+
}
701+
const matte = this.smask?.matte;
702+
if (!matte) {
703+
return;
704+
}
705+
const matteRgb = this.colorSpace.getRgb(matte, 0);
706+
PDFImage.#undoPreblend(
707+
buffer,
708+
width * height * 4,
709+
matteRgb[0],
710+
matteRgb[1],
711+
matteRgb[2]
712+
);
713+
}
714+
690715
async createImageData(forceRGBA = false, isOffscreenCanvasSupported = false) {
691716
const drawWidth = this.drawWidth;
692717
const drawHeight = this.drawHeight;
@@ -717,6 +742,17 @@ class PDFImage {
717742
{ internal: isOffscreenCanvasSupported && mustBeResized }
718743
));
719744

745+
if (this.jpxPremultiplied) {
746+
const matteRgb = this.preblendMatte;
747+
PDFImage.#undoPreblend(
748+
imgArray,
749+
imgArray.length,
750+
matteRgb?.[0] ?? 0,
751+
matteRgb?.[1] ?? 0,
752+
matteRgb?.[2] ?? 0
753+
);
754+
}
755+
720756
if (isOffscreenCanvasSupported) {
721757
if (!mustBeResized) {
722758
return this.createBitmap(

test/pdfs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,3 +945,4 @@
945945
!issue20504_skia.pdf
946946
!issue21579.pdf
947947
!saslprep-r6.pdf
948+
!jpx_smaskindata.pdf

test/pdfs/jpx_smaskindata.pdf

2.46 KB
Binary file not shown.

test/test_manifest.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14479,5 +14479,12 @@
1447914479
"rounds": 1,
1448014480
"type": "eq",
1448114481
"password": "S\u00AASL\u00ADprep"
14482+
},
14483+
{
14484+
"id": "jpx_smaskindata",
14485+
"file": "pdfs/jpx_smaskindata.pdf",
14486+
"md5": "f7a8628b14ca176700247f106c16812a",
14487+
"rounds": 1,
14488+
"type": "eq"
1448214489
}
1448314490
]

0 commit comments

Comments
 (0)