Skip to content

Commit 1cc10a6

Browse files
LucasCholletawesomekling
authored andcommitted
LibGfx/CCITT: Extract the code to decode a single CCITT3 1D line
This will be handy for the pure CCITT Group 3 decoder too :^)
1 parent 6a94b09 commit 1cc10a6

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -264,35 +264,21 @@ Optional<Code> get_terminal_code(Color color, u16 code_word, u8 code_size)
264264
return get_code_from_table(black_terminating_codes, code_word, code_size);
265265
}
266266

267-
}
268-
269-
ErrorOr<ByteBuffer> decode_ccitt_rle(ReadonlyBytes bytes, u32 image_width, u32 image_height)
267+
ErrorOr<void> decode_single_ccitt3_1d_line(BigEndianInputBitStream& input_bit_stream, BigEndianOutputBitStream& decoded_bits, u32 image_width)
270268
{
271-
auto strip_stream = make<FixedMemoryStream>(bytes);
272-
auto bit_stream = make<BigEndianInputBitStream>(MaybeOwned<Stream>(*strip_stream));
273-
274-
ByteBuffer decoded_bytes = TRY(ByteBuffer::create_zeroed(ceil_div(image_width * image_height, 8)));
275-
auto output_stream = make<FixedMemoryStream>(decoded_bytes.bytes());
276-
auto decoded_bits = make<BigEndianOutputBitStream>(MaybeOwned<Stream>(*output_stream));
277269
auto const ccitt_white = Color::NamedColor::White;
278270
auto const ccitt_black = Color::NamedColor::Black;
279271

280-
Color current_color { ccitt_black };
281-
u32 run_length {};
282-
272+
Color current_color { ccitt_white };
273+
u32 run_length = 0;
283274
u32 column = 0;
284275

285-
while (!bit_stream->is_eof() || run_length > 0) {
276+
while (column < image_width) {
286277
if (run_length > 0) {
287278
run_length--;
288-
TRY(decoded_bits->write_bits(current_color == ccitt_white ? 0u : 1u, 1));
279+
TRY(decoded_bits.write_bits(current_color == ccitt_white ? 0u : 1u, 1));
289280

290281
++column;
291-
if (column == image_width) {
292-
column = 0;
293-
bit_stream->align_to_byte_boundary();
294-
TRY(decoded_bits->align_to_byte_boundary());
295-
}
296282
continue;
297283
}
298284

@@ -306,7 +292,7 @@ ErrorOr<ByteBuffer> decode_ccitt_rle(ReadonlyBytes bytes, u32 image_width, u32 i
306292
u16 potential_code {};
307293
while (size < 14) {
308294
potential_code <<= 1;
309-
potential_code |= TRY(bit_stream->read_bit());
295+
potential_code |= TRY(input_bit_stream.read_bit());
310296
size++;
311297

312298
if (auto const maybe_markup = get_markup_code(current_color, potential_code, size); maybe_markup.has_value()) {
@@ -327,6 +313,27 @@ ErrorOr<ByteBuffer> decode_ccitt_rle(ReadonlyBytes bytes, u32 image_width, u32 i
327313
return Error::from_string_literal("TIFFImageDecoderPlugin: CCITT codes encode for more that a line");
328314
}
329315

316+
return {};
317+
}
318+
319+
}
320+
321+
ErrorOr<ByteBuffer> decode_ccitt_rle(ReadonlyBytes bytes, u32 image_width, u32 image_height)
322+
{
323+
auto strip_stream = make<FixedMemoryStream>(bytes);
324+
auto bit_stream = make<BigEndianInputBitStream>(MaybeOwned<Stream>(*strip_stream));
325+
326+
ByteBuffer decoded_bytes = TRY(ByteBuffer::create_zeroed(ceil_div(image_width * image_height, 8)));
327+
auto output_stream = make<FixedMemoryStream>(decoded_bytes.bytes());
328+
auto decoded_bits = make<BigEndianOutputBitStream>(MaybeOwned<Stream>(*output_stream));
329+
330+
while (!bit_stream->is_eof()) {
331+
TRY(decode_single_ccitt3_1d_line(*bit_stream, *decoded_bits, image_width));
332+
333+
bit_stream->align_to_byte_boundary();
334+
TRY(decoded_bits->align_to_byte_boundary());
335+
}
336+
330337
return decoded_bytes;
331338
}
332339

0 commit comments

Comments
 (0)