Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/core/jbig2.js
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,20 @@ class SimpleSegmentVisitor {
this.buffer = buffer;
}

getRetainedBitmapContexts(segmentNumber) {
if (!this.retainedBitmapContexts) {
return null;
}
return this.retainedBitmapContexts[segmentNumber] || null;
}

setRetainedBitmapContexts(segmentNumber, contexts) {
if (!this.retainedBitmapContexts) {
this.retainedBitmapContexts = {};
}
this.retainedBitmapContexts[segmentNumber] = contexts;
}

drawBitmap(regionInfo, bitmap) {
const pageInfo = this.currentPageInfo;
const width = regionInfo.width,
Expand Down Expand Up @@ -1683,16 +1697,64 @@ class SimpleSegmentVisitor {
}

const inputSymbols = [];
let lastReferredToSymbolDictionary = null;
for (const referredSegment of referredSegments) {
const referredSymbols = symbols[referredSegment];
// referredSymbols is undefined when we have a reference to a Tables
// segment instead of a SymbolDictionary.
if (referredSymbols) {
inputSymbols.push(...referredSymbols);
lastReferredToSymbolDictionary = referredSegment;
}
}

const decodingContext = new DecodingContext(data, start, end);

// Handle bitmap coding context reuse (7.4.2.2 step 3)
if (dictionary.bitmapCodingContextUsed) {
if (lastReferredToSymbolDictionary === null) {
throw new Jbig2Error(
"symbol dictionary uses bitmap coding context, but has no referred-to symbol dictionary"
);
}

const retainedContexts = this.getRetainedBitmapContexts(
lastReferredToSymbolDictionary
);
if (!retainedContexts) {
throw new Jbig2Error(
"symbol dictionary uses bitmap coding context, but referred-to dictionary did not retain contexts"
);
}

// Validate that parameters match (7.4.2.2 step 3)
if (retainedContexts.huffman !== dictionary.huffman) {
throw new Jbig2Error(
"symbol dictionary bitmap coding context: SDHUFF values do not match"
);
}
if (retainedContexts.refinement !== dictionary.refinement) {
throw new Jbig2Error(
"symbol dictionary bitmap coding context: SDREFAGG values do not match"
);
}
if (retainedContexts.template !== dictionary.template) {
throw new Jbig2Error(
"symbol dictionary bitmap coding context: SDTEMPLATE values do not match"
);
}
if (
retainedContexts.refinementTemplate !== dictionary.refinementTemplate
) {
throw new Jbig2Error(
"symbol dictionary bitmap coding context: SDRTEMPLATE values do not match"
);
}

// Reuse the arithmetic coding contexts
decodingContext.contextCache = retainedContexts.contextCache;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I open the PDF file from this commit with the preview build at http://54.241.84.105:8877/070ff3f62b453a4/web/viewer.html there is no infinite loop anymore, but I also don't see any image rendering, and the console shows this:

Warning: Unable to decode image "img_p0_1": "TypeError: setting getter-only property "contextCache"". pdf.worker.mjs:357:13
Warning: Dependent image isn't ready yet

That seems to relate to this line, so it looks like this may not actually work as expected?

}

symbols[currentSegment] = decodeSymbolDictionary(
dictionary.huffman,
dictionary.refinement,
Expand All @@ -1707,6 +1769,17 @@ class SimpleSegmentVisitor {
decodingContext,
huffmanInput
);

// Handle bitmap coding context retention (7.4.2.2 step 7)
if (dictionary.bitmapCodingContextRetained) {
this.setRetainedBitmapContexts(currentSegment, {
huffman: dictionary.huffman,
refinement: dictionary.refinement,
template: dictionary.template,
refinementTemplate: dictionary.refinementTemplate,
contextCache: decodingContext.contextCache,
});
}
}

onImmediateTextRegion(region, referredSegments, data, start, end) {
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@
!issue20319_1.pdf
!issue20319_2.pdf
!issue20439.pdf
!bitmap-symbol-context-reuse.pdf
!bug1992868.pdf
!bug1937438_af_from_latex.pdf
!bug1937438_from_word.pdf
Expand Down
Binary file added test/pdfs/bitmap-symbol-context-reuse.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2819,6 +2819,13 @@
"rounds": 1,
"type": "eq"
},
{
"id": "issue20461",
"file": "pdfs/bitmap-symbol-context-reuse.pdf",
"md5": "3d79e2d087515c2fdbed6fec0ad86e91",
"rounds": 1,
"type": "eq"
},
{
"id": "issue20439",
"file": "pdfs/issue20439.pdf",
Expand Down