LibGfx/JBIG2: Make SLTP contexts bit order match compute_context() order

The SLTP contexts must match the bit order used by compute_context().
compute_context() puts the adaptive template pixels in the leftmost
bit(s). The SLTP contexts used to not do this. Now they do.

See Figure 3(a), 4 - 6, 8 - 11 in the JBIG2 spec (ITU-T T.88).

With this, the generated jbig2 files (when embedded in PDFs via
Meta/jbig2_to_pdf.py) decode fine in other PDF viewers, and we
now correctly decode the PDF attached to
https://github.com/mozilla/pdf.js/issues/6267
This commit is contained in:
Nico Weber committed 2025-11-28 18:09:05 -05:00
1 parent 5d8baf7691
commit d1ad0b5bfe
6 files changed
+8 -8

No files matched your search

Binary file not shown.
@@ -1123,16 +1123,16 @@ static ErrorOr<NonnullRefPtr<BilevelImage>> generic_region_decoding_procedure(Ge
// independent from other contexts in the spec. They are passed in to this function.
// Figure 8 – Reused context for coding the SLTP value when GBTEMPLATE is 0
constexpr u16 sltp_context_for_template_0 = 0b10011'0110010'0101;
constexpr u16 sltp_context_for_template_0 = 0b0011'001'11001'0101;
// Figure 9 – Reused context for coding the SLTP value when GBTEMPLATE is 1
constexpr u16 sltp_context_for_template_1 = 0b0011'110010'101;
constexpr u16 sltp_context_for_template_1 = 0b0'0011'11001'101;
// Figure 10 – Reused context for coding the SLTP value when GBTEMPLATE is 2
constexpr u16 sltp_context_for_template_2 = 0b001'11001'01;
constexpr u16 sltp_context_for_template_2 = 0b1'001'1100'01;
// Figure 11 – Reused context for coding the SLTP value when GBTEMPLATE is 3
constexpr u16 sltp_context_for_template_3 = 0b011001'0101;
constexpr u16 sltp_context_for_template_3 = 0b1'01100'0101;
u16 sltp_context = [](u8 gb_template) {
if (gb_template == 0)
@@ -257,16 +257,16 @@ static ErrorOr<void> generic_region_encoding_procedure(GenericRegionEncodingInpu
// independent from other contexts in the spec. They are passed in to this function.
// Figure 8 – Reused context for coding the SLTP value when GBTEMPLATE is 0
constexpr u16 sltp_context_for_template_0 = 0b10011'0110010'0101;
constexpr u16 sltp_context_for_template_0 = 0b0011'001'11001'0101;
// Figure 9 – Reused context for coding the SLTP value when GBTEMPLATE is 1
constexpr u16 sltp_context_for_template_1 = 0b0011'110010'101;
constexpr u16 sltp_context_for_template_1 = 0b0'0011'11001'101;
// Figure 10 – Reused context for coding the SLTP value when GBTEMPLATE is 2
constexpr u16 sltp_context_for_template_2 = 0b001'11001'01;
constexpr u16 sltp_context_for_template_2 = 0b1'001'1100'01;
// Figure 11 – Reused context for coding the SLTP value when GBTEMPLATE is 3
constexpr u16 sltp_context_for_template_3 = 0b011001'0101;
constexpr u16 sltp_context_for_template_3 = 0b1'01100'0101;
u16 sltp_context = [](u8 gb_template) {
if (gb_template == 0)