@@ -2275,13 +2275,19 @@ static ErrorOr<void> encode_generic_refinement_region(JBIG2::GenericRefinementRe
22752275 if (header.referred_to_segments .size () > 1 )
22762276 return Error::from_string_literal (" JBIG2Writer: Generic refinement region must refer to at most one segment" );
22772277
2278- auto collect_related_segments = [&context](u32 page, u32 segment_number) {
2278+ enum class CollectionBehavior {
2279+ ExcludeSelf,
2280+ IncludeSelf,
2281+ };
2282+ auto collect_related_segments = [&context](u32 page, u32 segment_number, CollectionBehavior behavior) {
22792283 Vector<ReadonlyBytes> preceding_segments_on_same_page;
22802284 bool found = false ;
22812285 for (auto const & segment : context.segments ) {
22822286 if (segment.header .page_association == 0 || segment.header .page_association == page) {
22832287 auto const & data = context.segment_data_by_id .get (segment.header .segment_number );
22842288 if (segment.header .segment_number == segment_number) {
2289+ if (behavior == CollectionBehavior::IncludeSelf)
2290+ preceding_segments_on_same_page.append (data->data );
22852291 found = true ;
22862292 break ;
22872293 }
@@ -2303,12 +2309,21 @@ static ErrorOr<void> encode_generic_refinement_region(JBIG2::GenericRefinementRe
23032309 auto const & referred_to_segment = *maybe_segment.value ();
23042310
23052311 return TRY (referred_to_segment.data .visit (
2312+ [&]<OneOf<JBIG2::IntermediateTextRegionSegmentData, JBIG2::IntermediateHalftoneRegionSegmentData> T>(T const &) -> ErrorOr<NonnullRefPtr<BilevelImage>> {
2313+ auto related_segments_on_same_page = collect_related_segments (referred_to_segment.header .page_association , referred_to_segment.header .segment_number , CollectionBehavior::IncludeSelf);
2314+ auto bitmap = TRY (JBIG2ImageDecoderPlugin::decode_embedded_intermediate_region_segment (related_segments_on_same_page, referred_to_segment.header .segment_number ));
2315+ return bitmap;
2316+ },
2317+
2318+ // Optimization: IntermediateGenericRegionSegmentData, IntermediateGenericRefinementRegionSegmentData could also use
2319+ // the implementation above, but since we happen to have the final bitmap at hand for them, just return it immediately.
23062320 [](JBIG2::IntermediateGenericRegionSegmentData const & generic_region_wrapper) -> ErrorOr<NonnullRefPtr<BilevelImage>> {
23072321 return generic_region_wrapper.generic_region .image ;
23082322 },
23092323 [](JBIG2::IntermediateGenericRefinementRegionSegmentData const & generic_refinement_region_wrapper) -> ErrorOr<NonnullRefPtr<BilevelImage>> {
23102324 return generic_refinement_region_wrapper.generic_refinement_region .image ;
23112325 },
2326+
23122327 [](auto const &) -> ErrorOr<NonnullRefPtr<BilevelImage>> {
23132328 return Error::from_string_literal (" JBIG2Writer: Generic refinement region can only refer to intermediate region segments" );
23142329 }))
@@ -2319,7 +2334,7 @@ static ErrorOr<void> encode_generic_refinement_region(JBIG2::GenericRefinementRe
23192334 // contents of the page buffer (see clause 8), restricted to the area of the page buffer specified by this segment's region
23202335 // segment information field."
23212336 VERIFY (header.referred_to_segments .size () == 0 );
2322- auto preceding_segments_on_same_page = collect_related_segments (header.page_association , header.segment_number );
2337+ auto preceding_segments_on_same_page = collect_related_segments (header.page_association , header.segment_number , CollectionBehavior::ExcludeSelf );
23232338 auto bitmap = TRY (JBIG2ImageDecoderPlugin::decode_embedded (preceding_segments_on_same_page));
23242339 return bitmap->subbitmap (generic_refinement_region.region_segment_information .rect ());
23252340 }());
@@ -2550,6 +2565,10 @@ static ErrorOr<SerializedSegmentData> encode_segment(JBIG2::SegmentData const& s
25502565 TRY (encode_text_region (text_region_wrapper.text_region , segment_data.header , context, scratch_buffer));
25512566 return scratch_buffer;
25522567 },
2568+ [&scratch_buffer, &segment_data, &context](JBIG2::IntermediateTextRegionSegmentData const & text_region_wrapper) -> ErrorOr<ReadonlyBytes> {
2569+ TRY (encode_text_region (text_region_wrapper.text_region , segment_data.header , context, scratch_buffer));
2570+ return scratch_buffer;
2571+ },
25532572 [&scratch_buffer](JBIG2::PatternDictionarySegmentData const & pattern_dictionary) -> ErrorOr<ReadonlyBytes> {
25542573 TRY (encode_pattern_dictionary (pattern_dictionary, scratch_buffer));
25552574 return scratch_buffer;
@@ -2562,6 +2581,10 @@ static ErrorOr<SerializedSegmentData> encode_segment(JBIG2::SegmentData const& s
25622581 TRY (encode_halftone_region (halftone_region_wrapper.halftone_region , segment_data.header , context, scratch_buffer));
25632582 return scratch_buffer;
25642583 },
2584+ [&scratch_buffer, &segment_data, &context](JBIG2::IntermediateHalftoneRegionSegmentData const & halftone_region_wrapper) -> ErrorOr<ReadonlyBytes> {
2585+ TRY (encode_halftone_region (halftone_region_wrapper.halftone_region , segment_data.header , context, scratch_buffer));
2586+ return scratch_buffer;
2587+ },
25652588 [&scratch_buffer](JBIG2::ImmediateGenericRegionSegmentData const & generic_region_wrapper) -> ErrorOr<ReadonlyBytes> {
25662589 TRY (encode_generic_region (generic_region_wrapper.generic_region , scratch_buffer));
25672590 return scratch_buffer;
@@ -2619,9 +2642,11 @@ static ErrorOr<SerializedSegmentData> encode_segment(JBIG2::SegmentData const& s
26192642 [](JBIG2::SymbolDictionarySegmentData const &) { return JBIG2::SegmentType::SymbolDictionary; },
26202643 [](JBIG2::ImmediateTextRegionSegmentData const &) { return JBIG2::SegmentType::ImmediateTextRegion; },
26212644 [](JBIG2::ImmediateLosslessTextRegionSegmentData const &) { return JBIG2::SegmentType::ImmediateLosslessTextRegion; },
2645+ [](JBIG2::IntermediateTextRegionSegmentData const &) { return JBIG2::SegmentType::IntermediateTextRegion; },
26222646 [](JBIG2::PatternDictionarySegmentData const &) { return JBIG2::SegmentType::PatternDictionary; },
26232647 [](JBIG2::ImmediateHalftoneRegionSegmentData const &) { return JBIG2::SegmentType::ImmediateHalftoneRegion; },
26242648 [](JBIG2::ImmediateLosslessHalftoneRegionSegmentData const &) { return JBIG2::SegmentType::ImmediateLosslessHalftoneRegion; },
2649+ [](JBIG2::IntermediateHalftoneRegionSegmentData const &) { return JBIG2::SegmentType::IntermediateHalftoneRegion; },
26252650 [](JBIG2::ImmediateGenericRegionSegmentData const &) { return JBIG2::SegmentType::ImmediateGenericRegion; },
26262651 [](JBIG2::ImmediateLosslessGenericRegionSegmentData const &) { return JBIG2::SegmentType::ImmediateLosslessGenericRegion; },
26272652 [](JBIG2::IntermediateGenericRegionSegmentData const &) { return JBIG2::SegmentType::IntermediateGenericRegion; },
0 commit comments