LibGfx/JBIG2Writer+Tests/LibGfx: Refinement of symbol strip refines

This adds writer support for refinement of a symbol dictionary entry
that is itself a strip refinement.

This case is a bit awkward for the writer: To write the refinement, we
need the bitmap that's being refined. But since that's a strip refine,
we don't have that bitmap, we only have the parts it consists of.

As #26411 explains, the approach of calling into the loader to do
the strip decoding doesn't work here: We need the decoded strip
refinement while writing the strip itself. So we'd have to send
a partially encoded strip to the loader, which isn't something we
can currently do.

But since strip refinements only use a limited subset of full text
strips (always a background color of 0, always a reference corner
of topleft, always a composition operator of "or"), reimplementing
the loader's logic for this case, while conceptually not nice, is
not a lot of code. So let's just do that.

Using this, also add a test for this feature. I believe this tests
the last missing refinement case :^)

The test uses regular symbols for the eyes (symbols 0 and 1) and the
mouth (symbol 6).

The nose, 100x100 pixels, is conceptually split into 60x60, 40x60,
60x40, 40x40 tiles. Since symbols are sorted by increasing height, the
latter two are symbols 2 and 3, the former are symbols 4 and 5.

Symbol 3 isn't actually the bottom right part of the nose, but
contains the pixel data of an eye. This is for testing refinement
below.

To test strip background color filling, the 60x60 tile is actually
59x60 pixels. And to test refinement, the top left tile is actually
solid white instead of the top left part of the nose.

The first refinement symbol (id 7) refines the top left tile to a 59x60
tile that's the top left corner of the nose, but shifted several pixels
to the right.

The second refinement symbol (id 8) is a strip refinement. It puts
symbols 7 and 5 next to each other, to produce the top half of the nose
(with the left part being shifted to the side).

The third refinement symbol (id 9) is a simple refinement of symbol 8
refines it to the actual top of the nose (to fix the shift, and to
test refinement of strips in a symbol dictionary).

The fourth refinement symbol (id 10) is a strip refinement that puts
symbol 9 in the first strip, with an offset of 1, and symbols 2 and 3
in a second strip. This means symbol 10 is almost the nose, but its
bottom right contains the pixel data of an eye, since that's what
symbol 3 contained.

Finally, the text region refines symbol 10 to the actual nose pixels,
to test refinement of strips from a text region.

Whew!

Since the text region refines to the nose, it's useful to locally remove
that refinement to test that the reference bitmap looks as expected.
Similarly, it's useful to locally make the final strip refinement put
symbol 8 instead of symbol 9 in the first strip, to make sure the input
to symbol 9 looks as expected.

The test file decodes fine in all viewers I've tried (PDFium,
Preview.app, pdf.js, mutool). (This case is much more awkward for the
writer than for the loader, after all.)
This commit is contained in:
Nico Weber committed 2025-12-29 07:28:27 -05:00
1 parent 0add8d93a5
commit d15f57455a
4 files changed
+388 -7

No files matched your search

+1 -2
View File
@@ -420,6 +420,7 @@ TEST_CASE(test_jbig2_decode)
TEST_INPUT("jbig2/bitmap-symbol-symbolrefineseveral.jbig2"sv),
TEST_INPUT("jbig2/bitmap-symbol-symhuffrefineone.jbig2"sv),
TEST_INPUT("jbig2/bitmap-symbol-symhuffrefineseveral.jbig2"sv),
TEST_INPUT("jbig2/bitmap-symbol-symbolrefine-textrefine.jbig2"sv),
TEST_INPUT("jbig2/bitmap-symbol-textbottomleft.jbig2"sv),
TEST_INPUT("jbig2/bitmap-symbol-textbottomlefttranspose.jbig2"sv),
TEST_INPUT("jbig2/bitmap-symbol-textbottomright.jbig2"sv),
@@ -427,8 +428,6 @@ TEST_CASE(test_jbig2_decode)
TEST_INPUT("jbig2/bitmap-symbol-texttopright.jbig2"sv),
TEST_INPUT("jbig2/bitmap-symbol-texttoprighttranspose.jbig2"sv),
TEST_INPUT("jbig2/bitmap-symbol-texttranspose.jbig2"sv),
// Missing tests, in part because there's no easy way to generate them:
// - symbol refinement referring to symbol in same segment
// Missing tests for things that aren't implemented yet:
// - exttemplate
// - colors
@@ -0,0 +1,369 @@
{
"global_header": {
"organization": "sequential",
"number_of_pages": 1
},
"segments": [
{
"segment_number": 0,
"type": "page_information",
"page_association": 1,
"data": {
"page_width": 399,
"page_height": 400,
"flags": {
"is_eventually_lossless": true
}
}
},
{
"segment_number": 1,
"type": "symbol_dictionary",
"page_association": 1,
"retained": true,
"data": {
"flags": {
"uses_huffman_encoding": false,
"uses_refinement_or_aggregate_coding": false,
"huffman_table_selection_for_height_differences": 0,
"huffman_table_selection_for_width_differences": 0,
"huffman_table_selection_for_bitmap_sizes": 0,
"huffman_table_selection_for_number_of_symbol_instances": 0,
"is_bitmap_coding_context_used": false,
"is_bitmap_coding_context_retained": false,
"template": 0,
"refinement_template": 0
},
"height_classes": [
{
"symbols": [
{
"image_data": {
"from_file": "bitmap.bmp",
"crop": {
"x": 60,
"y": 265,
"width": 30,
"height": 30
}
}
},
{
"image_data": {
"from_file": "bitmap.bmp",
"crop": {
"x": 50,
"y": 108,
"width": 30,
"height": 30
}
}
}
]
},
{
"symbols": [
{
"image_data": {
"from_file": "bitmap.bmp",
"crop": {
"x": 135,
"y": 160,
"width": 60,
"height": 40
}
}
},
{
"image_data": {
"from_file": "bitmap.bmp",
"crop": {
"x": 50,
"y": 108,
"width": 40,
"height": 40
}
}
}
]
},
{
"symbols": [
{
"image_data": {
"from_file": "bitmap.bmp",
"crop": {
"x": 0,
"y": 0,
"width": 59,
"height": 60
}
}
},
{
"image_data": {
"from_file": "bitmap.bmp",
"crop": {
"x": 195,
"y": 100,
"width": 40,
"height": 60
}
}
}
]
},
{
"symbols": [
{
"image_data": {
"from_file": "bitmap.bmp",
"crop": {
"x": 232,
"y": 70,
"width": 120,
"height": 250
}
}
}
]
}
]
}
},
{
"segment_number": 2,
"type": "symbol_dictionary",
"page_association": 1,
"retained": true,
"referred_to_segments": [
{
"segment_number": 1,
"retained": false
}
],
"data": {
"flags": {
"uses_huffman_encoding": false,
"uses_refinement_or_aggregate_coding": true,
"huffman_table_selection_for_height_differences": 0,
"huffman_table_selection_for_width_differences": 0,
"huffman_table_selection_for_bitmap_sizes": 0,
"huffman_table_selection_for_number_of_symbol_instances": 0,
"is_bitmap_coding_context_used": false,
"is_bitmap_coding_context_retained": false,
"template": 0,
"refinement_template": 0
},
"height_classes": [
{
"symbols": [
{
"refines_symbol_to": {
"symbol_id": 4,
"delta_x_offset": 0,
"delta_y_offset": 0,
"image_data": {
"from_file": "bitmap.bmp",
"crop": {
"x": 146,
"y": 100,
"width": 59,
"height": 60
}
}
}
},
{
"width": 99,
"height": 60,
"refines_using_strips": {
"initial_strip_t": -1,
"strips": [
{
"strip_t": 0,
"instances": [
{
"instance_s": 0,
"instance_t": 0,
"symbol_id": 7
},
{
"instance_s": 60,
"instance_t": 0,
"symbol_id": 5
}
]
}
]
}
},
{
"refines_symbol_to": {
"symbol_id": 8,
"delta_x_offset": 0,
"delta_y_offset": 0,
"image_data": {
"from_file": "bitmap.bmp",
"crop": {
"x": 136,
"y": 100,
"width": 99,
"height": 60
}
}
}
}
]
},
{
"symbols": [
{
"width": 100,
"height": 100,
"refines_using_strips": {
"initial_strip_t": -1,
"strips": [
{
"strip_t": 0,
"instances": [
{
"instance_s": 1,
"instance_t": 0,
"symbol_id": 9
}
]
},
{
"strip_t": 60,
"instances": [
{
"instance_s": 0,
"instance_t": 60,
"symbol_id": 2
},
{
"instance_s": 60,
"instance_t": 60,
"symbol_id": 3
}
]
}
]
}
}
]
}
]
}
},
{
"segment_number": 3,
"type": "text_region",
"page_association": 1,
"referred_to_segments": [
{
"segment_number": 2,
"retained": false
}
],
"data": {
"region_segment_information": {
"width": 399,
"height": 400
},
"flags": {
"uses_huffman_encoding": false,
"uses_refinement_coding": true,
"strip_size": 2,
"reference_corner": "top_left",
"is_transposed": false,
"combination_operator": "xor",
"default_pixel_value": "white",
"delta_s_offset": 3,
"refinement_template": 0
},
"huffman_flags": {
"huffman_table_selection_for_first_s": 0,
"huffman_table_selection_for_subsequent_s": 0,
"huffman_table_selection_for_t": 0,
"huffman_table_selection_for_refinement_delta_width": 0,
"huffman_table_selection_for_refinement_delta_height": 0,
"huffman_table_selection_for_refinement_delta_x_offset": 0,
"huffman_table_selection_for_refinement_delta_y_offset": 0,
"huffman_table_selection_for_refinement_size_table": 0
},
"initial_strip_t": -4,
"strips": [
{
"strip_t": 70,
"instances": [
{
"instance_s": 232,
"instance_t": 70,
"symbol_id": 6
}
]
},
{
"strip_t": 100,
"instances": [
{
"instance_s": 135,
"instance_t": 100,
"symbol_id": 10,
"instance_refines_symbol_to": {
"delta_width": 0,
"delta_height": 0,
"delta_x_offset": 0,
"delta_y_offset": 0,
"image_data": {
"from_file": "bitmap.bmp",
"crop": {
"x": 135,
"y": 100,
"width": 100,
"height": 100
}
}
}
}
]
},
{
"strip_t": 108,
"instances": [
{
"instance_s": 50,
"instance_t": 108,
"symbol_id": 1
}
]
},
{
"strip_t": 264,
"instances": [
{
"instance_s": 60,
"instance_t": 265,
"symbol_id": 0
}
]
}
]
}
},
{
"segment_number": 4,
"type": "end_of_page",
"page_association": 1
}
]
}
@@ -486,16 +486,26 @@ static ErrorOr<void> generic_refinement_region_encoding_procedure(GenericRefinem
return {};
}
static ErrorOr<NonnullRefPtr<BilevelImage>> symbol_image(JBIG2::SymbolDictionarySegmentData::HeightClass::Symbol const& symbol)
static ErrorOr<NonnullRefPtr<BilevelImage>> symbol_image(JBIG2::SymbolDictionarySegmentData::HeightClass::Symbol const& symbol, Vector<JBIG2::SymbolDictionarySegmentData::HeightClass::Symbol> const& symbols)
{
if (symbol.image.has<NonnullRefPtr<BilevelImage>>())
return symbol.image.get<NonnullRefPtr<BilevelImage>>();
if (symbol.image.has<JBIG2::SymbolDictionarySegmentData::HeightClass::RefinedSymbol>())
return symbol.image.get<JBIG2::SymbolDictionarySegmentData::HeightClass::RefinedSymbol>().refines_to;
auto image = TRY(BilevelImage::create(symbol.size.width(), symbol.size.height()));
image->fill(false);
auto const& text_strips = symbol.image.get<JBIG2::SymbolDictionarySegmentData::HeightClass::RefinesUsingStrips>();
(void)text_strips;
return Error::from_string_literal("JBIG2Writer: Cannot write refinements of refinements by text strips yet");
for (auto const& strip : text_strips.strips) {
for (auto const& instance : strip.symbol_instances) {
if (instance.symbol_id >= symbols.size())
return Error::from_string_literal("JBIG2Writer: Invalid symbol ID in text strip symbol instance");
auto const& instance_symbol = symbols[instance.symbol_id];
auto instance_image = TRY(symbol_image(instance_symbol, symbols));
instance_image->composite_onto(image, { instance.s, instance.t }, BilevelImage::CompositionType::Or);
}
}
return image;
}
namespace {
@@ -714,7 +724,7 @@ static ErrorOr<void> text_region_encoding_procedure(TextRegionEncodingInputParam
if (static_cast<i32>(symbol.size.height()) + symbol_instance.refinement_data->delta_height < 0)
return Error::from_string_literal("JBIG2Writer: Refinement height out of bounds");
auto reference_bitmap = TRY(symbol_image(symbol));
auto reference_bitmap = TRY(symbol_image(symbol, inputs.symbols));
GenericRefinementRegionEncodingInputParameters refinement_inputs {
.image = symbol_instance.refinement_data->refines_to,
.reference_bitmap = reference_bitmap->as_subbitmap(),
@@ -1108,7 +1118,10 @@ static ErrorOr<ByteBuffer> symbol_dictionary_encoding_procedure(SymbolDictionary
if (refinement_image.symbol_id >= inputs.input_symbols.size() && refinement_image.symbol_id - inputs.input_symbols.size() >= new_symbols.size())
return Error::from_string_literal("JBIG2Writer: Refinement/aggregate symbol ID out of range");
auto const& IBO = TRY(symbol_image(refinement_image.symbol_id < inputs.input_symbols.size() ? inputs.input_symbols[refinement_image.symbol_id] : new_symbols[refinement_image.symbol_id - inputs.input_symbols.size()]));
Vector<JBIG2::SymbolDictionarySegmentData::HeightClass::Symbol> all_symbols;
all_symbols.extend(inputs.input_symbols);
all_symbols.extend(new_symbols);
auto const& IBO = TRY(symbol_image(all_symbols[refinement_image.symbol_id], all_symbols));
MQArithmeticEncoder* refinement_encoder = nullptr;
Optional<MQArithmeticEncoder> huffman_refinement_encoder;