From d57d67642516d86343e69dc746a3a6da334c35e2 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Thu, 15 Feb 2024 19:46:25 -0500 Subject: [PATCH] LibGfx/CCITT: Add support for Group4 The API is currently pretty raw. Group4 has a bunch of options that we don't support yet. --- .../LibGfx/ImageFormats/CCITTDecoder.cpp | 21 +++++++++++++++++++ .../LibGfx/ImageFormats/CCITTDecoder.h | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp b/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp index 7217f53c325..1bc9fbdc024 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.cpp @@ -563,4 +563,25 @@ ErrorOr decode_ccitt_group3(ReadonlyBytes bytes, u32 image_width, u3 return decoded_bytes; } +ErrorOr decode_ccitt_group4(ReadonlyBytes bytes, u32 image_width, u32 image_height) +{ + auto strip_stream = make(bytes); + auto bit_stream = make(MaybeOwned(*strip_stream)); + + // Note: We put image_height extra-space to handle at most one alignment to byte boundary per line. + ByteBuffer decoded_bytes = TRY(ByteBuffer::create_zeroed(ceil_div(image_width * image_height, 8) + image_height)); + auto output_stream = make(decoded_bytes.bytes()); + auto decoded_bits = make(MaybeOwned(*output_stream)); + + // T.6 2.2.1 Principle of the coding scheme + // The reference line for the first coding line in a page is an imaginary white line. + ReferenceLine reference_line; + TRY(reference_line.try_empend(ccitt_black, image_width)); + + for (u32 i = 0; i < image_height; ++i) + reference_line = TRY(decode_single_ccitt_2d_line(*bit_stream, *decoded_bits, move(reference_line), image_width)); + + return decoded_bytes; +} + } diff --git a/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.h b/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.h index 968a5472d8f..b27754f60e3 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.h +++ b/Userland/Libraries/LibGfx/ImageFormats/CCITTDecoder.h @@ -16,6 +16,9 @@ namespace Gfx::CCITT { // The CCITT3 specification is accessible at this page: // https://www.itu.int/rec/T-REC-T.4/en +// And CCITT4's specification is available here: +// https://www.itu.int/rec/T-REC-T.6/en + // The unidimensional scheme is originally described in: // 4.1 One-dimensional coding scheme // However, this function implements the TIFF variant (see TIFFLoader.h for a spec link), @@ -47,4 +50,6 @@ struct Group3Options { ErrorOr decode_ccitt_group3(ReadonlyBytes bytes, u32 image_width, u32 image_height, Group3Options const& options); +ErrorOr decode_ccitt_group4(ReadonlyBytes bytes, u32 image_width, u32 image_height); + }