1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibPDF: Factorize flate parameters handling to its own function

This part will be shared with the LZW filter, so let's factorize it.
This commit is contained in:
Lucas CHOLLET 2023-11-11 15:48:58 -05:00 committed by Andreas Kling
parent e8c0cea5f9
commit 048ef11136
2 changed files with 19 additions and 10 deletions

View file

@ -206,16 +206,12 @@ PDFErrorOr<ByteBuffer> Filter::decode_png_prediction(Bytes bytes, int bytes_per_
return decoded;
}
PDFErrorOr<ByteBuffer> Filter::decode_lzw(ReadonlyBytes)
PDFErrorOr<ByteBuffer> Filter::handle_lzw_and_flate_parameters(ByteBuffer buffer, int predictor, int columns, int colors, int bits_per_component)
{
return Error::rendering_unsupported_error("LZW Filter is not supported");
}
// Table 3.7 Optional parameters for LZWDecode and FlateDecode filters
PDFErrorOr<ByteBuffer> Filter::decode_flate(ReadonlyBytes bytes, int predictor, int columns, int colors, int bits_per_component)
{
auto buff = TRY(Compress::DeflateDecompressor::decompress_all(bytes.slice(2)));
if (predictor == 1)
return buff;
return buffer;
// Check if we are dealing with a PNG prediction
if (predictor == 2)
@ -224,11 +220,22 @@ PDFErrorOr<ByteBuffer> Filter::decode_flate(ReadonlyBytes bytes, int predictor,
return AK::Error::from_string_literal("Invalid predictor value");
// Rows are always a whole number of bytes long, starting with an algorithm tag
int bytes_per_row = AK::ceil_div(columns * colors * bits_per_component, 8) + 1;
if (buff.size() % bytes_per_row)
int const bytes_per_row = AK::ceil_div(columns * colors * bits_per_component, 8) + 1;
if (buffer.size() % bytes_per_row)
return AK::Error::from_string_literal("Flate input data is not divisible into columns");
return decode_png_prediction(buff, bytes_per_row);
return decode_png_prediction(buffer, bytes_per_row);
}
PDFErrorOr<ByteBuffer> Filter::decode_lzw(ReadonlyBytes)
{
return Error::rendering_unsupported_error("LZW Filter is not supported");
}
PDFErrorOr<ByteBuffer> Filter::decode_flate(ReadonlyBytes bytes, int predictor, int columns, int colors, int bits_per_component)
{
auto buff = TRY(Compress::DeflateDecompressor::decompress_all(bytes.slice(2)));
return handle_lzw_and_flate_parameters(move(buff), predictor, columns, colors, bits_per_component);
}
PDFErrorOr<ByteBuffer> Filter::decode_run_length(ReadonlyBytes bytes)

View file

@ -29,6 +29,8 @@ private:
static PDFErrorOr<ByteBuffer> decode_dct(ReadonlyBytes bytes);
static PDFErrorOr<ByteBuffer> decode_jpx(ReadonlyBytes bytes);
static PDFErrorOr<ByteBuffer> decode_crypt(ReadonlyBytes bytes);
static PDFErrorOr<ByteBuffer> handle_lzw_and_flate_parameters(ByteBuffer buffer, int predictor, int columns, int colors, int bits_per_component);
};
}