@@ -769,6 +769,7 @@ struct AdaptiveTemplatePixel {
769
769
};
770
770
771
771
// 6.2.2 Input parameters
772
+ // Table 2 – Parameters for the generic region decoding procedure
772
773
struct GenericRegionDecodingInputParameters {
773
774
bool is_modified_modified_read { false }; // "MMR" in spec.
774
775
u32 region_width { 0 }; // "GBW" in spec.
@@ -780,10 +781,13 @@ struct GenericRegionDecodingInputParameters {
780
781
781
782
Array<AdaptiveTemplatePixel, 12 > adaptive_template_pixels; // "GBATX" / "GBATY" in spec.
782
783
// 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 };
783
787
};
784
788
785
789
// 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 )
787
791
{
788
792
if (inputs.is_modified_modified_read ) {
789
793
dbgln_if (JBIG2_DEBUG, " JBIG2ImageDecoderPlugin: MMR image data" );
@@ -823,7 +827,6 @@ static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_region_decoding_procedure(Gener
823
827
824
828
auto result = TRY (BitBuffer::create (inputs.region_width , inputs.region_height ));
825
829
826
- auto decoder = MUST (JBIG2::ArithmeticDecoder::initialize (data));
827
830
auto get_pixel = [&inputs](NonnullOwnPtr<BitBuffer> const & buffer, int x, int y) -> bool {
828
831
if (x < 0 || x >= (int )inputs.region_width || y < 0 )
829
832
return false ;
@@ -848,16 +851,13 @@ static ErrorOr<NonnullOwnPtr<BitBuffer>> generic_region_decoding_procedure(Gener
848
851
// concatenating the label "GB" and the 10-16 pixel values gathered in CONTEXT."
849
852
// Implementor's note: What this is supposed to mean is that we have a bunch of independent contexts, and we pick the
850
853
// 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.
856
855
857
856
// Figure 8 – Reused context for coding the SLTP value when GBTEMPLATE is 0
858
857
constexpr u16 sltp_context_for_template_0 = 0b10011'0110010'0101 ;
859
858
860
859
// 6.2.5.7 Decoding the bitmap
860
+ JBIG2::ArithmeticDecoder& decoder = *inputs.arithmetic_decoder ;
861
861
bool ltp = false ; // "LTP" in spec. "Line (uses) Typical Prediction" maybe?
862
862
for (size_t y = 0 ; y < inputs.region_height ; ++y) {
863
863
if (inputs.is_typical_prediction_used ) {
@@ -973,7 +973,9 @@ static ErrorOr<void> decode_immediate_generic_region(JBIG2LoadingContext& contex
973
973
// "1) Interpret its header, as described in 7.4.6.1"
974
974
// Done above.
975
975
// "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
+
977
979
// "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."
978
980
GenericRegionDecodingInputParameters inputs;
979
981
inputs.is_modified_modified_read = uses_mmr;
@@ -984,7 +986,14 @@ static ErrorOr<void> decode_immediate_generic_region(JBIG2LoadingContext& contex
984
986
inputs.is_extended_reference_template_used = uses_extended_reference_template;
985
987
inputs.skip_pattern = OptionalNone {};
986
988
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));
988
997
989
998
// 8.2 Page image composition step 5)
990
999
if (information_field.x_location + information_field.width > (u32 )context.page .size .width ()
0 commit comments