diff --git a/Userland/Libraries/LibGfx/ImageFormats/ILBMLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/ILBMLoader.cpp index e77e7d8cca..822f151b3c 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/ILBMLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/ILBMLoader.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -246,39 +247,7 @@ static ErrorOr uncompress_byte_run(ReadonlyBytes data, ILBMLoadingCo if (plane_data_size > NumericLimits::max() || ceil_div(plane_data_size, 127ul) > length) return Error::from_string_literal("Uncompressed data size too large"); - auto plane_data = TRY(ByteBuffer::create_uninitialized(plane_data_size)); - - u32 index = 0; - u32 read_bytes = 0; - // Uncompressing is done once we've read all buffer or plane buffer has been fully filled - while (read_bytes < length && index < plane_data_size) { - auto const byte = static_cast(data[read_bytes++]); - if (byte >= -127 && byte <= -1) { - // read next byte - if (read_bytes == data.size()) - return Error::from_string_literal("Malformed compressed data"); - - u8 next_byte = data[read_bytes++]; - if (index + -byte >= plane_data.size()) - return Error::from_string_literal("Malformed compressed data"); - - for (u16 i = 0; i < -byte + 1; ++i) { - plane_data[index++] = next_byte; - } - } else if (byte >= 0) { - if (index + byte >= plane_data_size || read_bytes + byte >= length) - return Error::from_string_literal("Malformed compressed data"); - - for (u16 i = 0; i < byte + 1; ++i) { - plane_data[index] = data[read_bytes]; - read_bytes++; - index++; - } - } - } - - if (index != plane_data_size) - return Error::from_string_literal("Unexpected end of chunk while decompressing data"); + auto plane_data = TRY(Compress::PackBits::decode_all(data, plane_data_size)); return plane_data; } diff --git a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp index e1c96aa05f..4297cd3754 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -284,44 +285,8 @@ private: ByteBuffer decoded_bytes {}; auto decode_packbits_strip = [&](u32 num_bytes) -> ErrorOr { - auto strip_stream = make(TRY(m_stream->read_in_place(num_bytes))); - - decoded_bytes.clear(); - - Optional n {}; - Optional saved_byte {}; - - while (strip_stream->remaining() > 0 || saved_byte.has_value()) { - if (!n.has_value()) - n = TRY(strip_stream->read_value()); - - if (n.value() >= 0 && !saved_byte.has_value()) { - n.value() = n.value() - 1; - if (n.value() == -1) - n.clear(); - - decoded_bytes.append(TRY(strip_stream->read_value())); - continue; - } - - if (n.value() == -128) { - n.clear(); - continue; - } - - if (!saved_byte.has_value()) - saved_byte = TRY(strip_stream->read_value()); - - n.value() = n.value() + 1; - - decoded_bytes.append(*saved_byte); - - if (n == 1) { - saved_byte.clear(); - n.clear(); - } - } - + auto const encoded_bytes = TRY(m_stream->read_in_place(num_bytes)); + decoded_bytes = TRY(Compress::PackBits::decode_all(encoded_bytes)); return decoded_bytes; }; diff --git a/Userland/Libraries/LibPDF/Filter.cpp b/Userland/Libraries/LibPDF/Filter.cpp index 63eebd7bfe..00f4dfe589 100644 --- a/Userland/Libraries/LibPDF/Filter.cpp +++ b/Userland/Libraries/LibPDF/Filter.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -210,30 +211,7 @@ PDFErrorOr Filter::decode_flate(ReadonlyBytes bytes, int predictor, PDFErrorOr Filter::decode_run_length(ReadonlyBytes bytes) { - constexpr size_t END_OF_DECODING = 128; - ByteBuffer buffer {}; - while (true) { - VERIFY(bytes.size() > 0); - auto length = bytes[0]; - bytes = bytes.slice(1); - if (length == END_OF_DECODING) { - VERIFY(bytes.is_empty()); - break; - } - if (length < 128) { - TRY(buffer.try_append(bytes.slice(0, length + 1))); - bytes = bytes.slice(length + 1); - } else { - VERIFY(bytes.size() > 1); - auto byte_to_append = bytes[0]; - bytes = bytes.slice(1); - size_t n_chars = 257 - length; - for (size_t i = 0; i < n_chars; ++i) { - TRY(buffer.try_append(byte_to_append)); - } - } - } - return buffer; + return TRY(Compress::PackBits::decode_all(bytes, OptionalNone {}, Compress::PackBits::CompatibilityMode::PDF)); } PDFErrorOr Filter::decode_ccitt(ReadonlyBytes)