Skip to content

Commit 2099ca4

Browse files
nicotrflynn89
authored andcommitted
LibGfx/JBIG2: Pass in decoder and contexts to generic region decoder
The symbol segment decoding procedure will read generic regions that aren't at a byte boundary, and that share contexts across several regions. No behavior change.
1 parent 376b1a2 commit 2099ca4

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

Userland/Libraries/LibGfx/ImageFormats/JBIG2Loader.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ struct AdaptiveTemplatePixel {
769769
};
770770

771771
// 6.2.2 Input parameters
772+
// Table 2 – Parameters for the generic region decoding procedure
772773
struct GenericRegionDecodingInputParameters {
773774
bool is_modified_modified_read { false }; // "MMR" in spec.
774775
u32 region_width { 0 }; // "GBW" in spec.
@@ -780,10 +781,13 @@ struct GenericRegionDecodingInputParameters {
780781

781782
Array<AdaptiveTemplatePixel, 12> adaptive_template_pixels; // "GBATX" / "GBATY" in spec.
782783
// FIXME: GBCOLS, GBCOMBOP, COLEXTFLAG
784+
785+
// If is_modified_modified_read is false, generic_region_decoding_procedure() reads data off this decoder.
786+
JBIG2::ArithmeticDecoder* arithmetic_decoder { nullptr };
783787
};
784788

785789
// 6.2 Generic region decoding procedure
786-
static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_region_decoding_procedure(GenericRegionDecodingInputParameters const& inputs, ReadonlyBytes data)
790+
static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_region_decoding_procedure(GenericRegionDecodingInputParameters const& inputs, ReadonlyBytes data, Vector<JBIG2::ArithmeticDecoder::Context>& contexts)
787791
{
788792
if (inputs.is_modified_modified_read) {
789793
dbgln_if(JBIG2_DEBUG, "JBIG2ImageDecoderPlugin: MMR image data");
@@ -823,7 +827,6 @@ static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_region_decoding_procedure(Gener
823827

824828
auto result = TRY(BitBuffer::create(inputs.region_width, inputs.region_height));
825829

826-
auto decoder = MUST(JBIG2::ArithmeticDecoder::initialize(data));
827830
auto get_pixel = [&inputs](NonnullOwnPtr<BitBuffer> const& buffer, int x, int y) -> bool {
828831
if (x < 0 || x >= (int)inputs.region_width || y < 0)
829832
return false;
@@ -848,16 +851,13 @@ static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_region_decoding_procedure(Gener
848851
// concatenating the label "GB" and the 10-16 pixel values gathered in CONTEXT."
849852
// Implementor's note: What this is supposed to mean is that we have a bunch of independent contexts, and we pick the
850853
// context for the current pixel based on pixel values in the neighborhood. The "GB" part just means this context is
851-
// independent from other contexts in the spec. At the moment, these are the only contexts we have, so we just
852-
// create them on demand here.
853-
// I can't find where the spec says this, but the contexts all start out zero-initialized.
854-
Vector<JBIG2::ArithmeticDecoder::Context> contexts;
855-
contexts.resize(1 << 16);
854+
// independent from other contexts in the spec. They are passed in to this function.
856855

857856
// Figure 8 – Reused context for coding the SLTP value when GBTEMPLATE is 0
858857
constexpr u16 sltp_context_for_template_0 = 0b10011'0110010'0101;
859858

860859
// 6.2.5.7 Decoding the bitmap
860+
JBIG2::ArithmeticDecoder& decoder = *inputs.arithmetic_decoder;
861861
bool ltp = false; // "LTP" in spec. "Line (uses) Typical Prediction" maybe?
862862
for (size_t y = 0; y < inputs.region_height; ++y) {
863863
if (inputs.is_typical_prediction_used) {
@@ -973,7 +973,9 @@ static ErrorOr<void> decode_immediate_generic_region(JBIG2LoadingContext& contex
973973
// "1) Interpret its header, as described in 7.4.6.1"
974974
// Done above.
975975
// "2) As described in E.3.7, reset all the arithmetic coding statistics to zero."
976-
// FIXME: Implement this once we support arithmetic coding.
976+
Vector<JBIG2::ArithmeticDecoder::Context> contexts;
977+
contexts.resize(1 << 16);
978+
977979
// "3) Invoke the generic region decoding procedure described in 6.2, with the parameters to the generic region decoding procedure set as shown in Table 37."
978980
GenericRegionDecodingInputParameters inputs;
979981
inputs.is_modified_modified_read = uses_mmr;
@@ -984,7 +986,14 @@ static ErrorOr<void> decode_immediate_generic_region(JBIG2LoadingContext& contex
984986
inputs.is_extended_reference_template_used = uses_extended_reference_template;
985987
inputs.skip_pattern = OptionalNone {};
986988
inputs.adaptive_template_pixels = adaptive_template_pixels;
987-
auto result = TRY(generic_region_decoding_procedure(inputs, data));
989+
990+
Optional<JBIG2::ArithmeticDecoder> decoder;
991+
if (!uses_mmr) {
992+
decoder = TRY(JBIG2::ArithmeticDecoder::initialize(data));
993+
inputs.arithmetic_decoder = &decoder.value();
994+
}
995+
996+
auto result = TRY(generic_region_decoding_procedure(inputs, data, contexts));
988997

989998
// 8.2 Page image composition step 5)
990999
if (information_field.x_location + information_field.width > (u32)context.page.size.width()

0 commit comments

Comments
 (0)