@@ -264,35 +264,21 @@ Optional<Code> get_terminal_code(Color color, u16 code_word, u8 code_size)
264
264
return get_code_from_table (black_terminating_codes, code_word, code_size);
265
265
}
266
266
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)
270
268
{
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));
277
269
auto const ccitt_white = Color::NamedColor::White;
278
270
auto const ccitt_black = Color::NamedColor::Black;
279
271
280
- Color current_color { ccitt_black };
281
- u32 run_length {};
282
-
272
+ Color current_color { ccitt_white };
273
+ u32 run_length = 0 ;
283
274
u32 column = 0 ;
284
275
285
- while (!bit_stream-> is_eof () || run_length > 0 ) {
276
+ while (column < image_width ) {
286
277
if (run_length > 0 ) {
287
278
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 ));
289
280
290
281
++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
- }
296
282
continue ;
297
283
}
298
284
@@ -306,7 +292,7 @@ ErrorOr<ByteBuffer> decode_ccitt_rle(ReadonlyBytes bytes, u32 image_width, u32 i
306
292
u16 potential_code {};
307
293
while (size < 14 ) {
308
294
potential_code <<= 1 ;
309
- potential_code |= TRY (bit_stream-> read_bit ());
295
+ potential_code |= TRY (input_bit_stream. read_bit ());
310
296
size++;
311
297
312
298
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
327
313
return Error::from_string_literal (" TIFFImageDecoderPlugin: CCITT codes encode for more that a line" );
328
314
}
329
315
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
+
330
337
return decoded_bytes;
331
338
}
332
339
0 commit comments