From 454a10774ee3ba1286da72f4e7d55ac2341db2fe Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 12 Feb 2024 19:35:31 -0500 Subject: [PATCH] LibPDF: Let Filter::handle_lzw_and_flate_parameters() read decode params ...instead of reading them in Filter::decode() for all filters and then passing them around to only the LZW and flate filters. (EarlyChange is LZWDecode-only, so that's read there instead.) No behavior change. --- Userland/Libraries/LibPDF/Filter.cpp | 53 ++++++++++++++-------------- Userland/Libraries/LibPDF/Filter.h | 6 ++-- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/Userland/Libraries/LibPDF/Filter.cpp b/Userland/Libraries/LibPDF/Filter.cpp index 956bd70022..4264887517 100644 --- a/Userland/Libraries/LibPDF/Filter.cpp +++ b/Userland/Libraries/LibPDF/Filter.cpp @@ -19,33 +19,14 @@ namespace PDF { PDFErrorOr Filter::decode(ReadonlyBytes bytes, DeprecatedFlyString const& encoding_type, RefPtr decode_parms) { - int predictor = 1; - int columns = 1; - int colors = 1; - int bits_per_component = 8; - int early_change = 1; - - if (decode_parms) { - if (decode_parms->contains(CommonNames::Predictor)) - predictor = decode_parms->get_value(CommonNames::Predictor).get(); - if (decode_parms->contains(CommonNames::Columns)) - columns = decode_parms->get_value(CommonNames::Columns).get(); - if (decode_parms->contains(CommonNames::Colors)) - colors = decode_parms->get_value(CommonNames::Colors).get(); - if (decode_parms->contains(CommonNames::BitsPerComponent)) - bits_per_component = decode_parms->get_value(CommonNames::BitsPerComponent).get(); - if (decode_parms->contains(CommonNames::EarlyChange)) - early_change = decode_parms->get_value(CommonNames::EarlyChange).get(); - } - if (encoding_type == CommonNames::ASCIIHexDecode) return decode_ascii_hex(bytes); if (encoding_type == CommonNames::ASCII85Decode) return decode_ascii85(bytes); if (encoding_type == CommonNames::LZWDecode) - return decode_lzw(bytes, predictor, columns, colors, bits_per_component, early_change); + return decode_lzw(bytes, decode_parms); if (encoding_type == CommonNames::FlateDecode) - return decode_flate(bytes, predictor, columns, colors, bits_per_component); + return decode_flate(bytes, decode_parms); if (encoding_type == CommonNames::RunLengthDecode) return decode_run_length(bytes); if (encoding_type == CommonNames::CCITTFaxDecode) @@ -228,9 +209,24 @@ PDFErrorOr Filter::decode_tiff_prediction(Bytes bytes, int columns, return decoded; } -PDFErrorOr Filter::handle_lzw_and_flate_parameters(ByteBuffer buffer, int predictor, int columns, int colors, int bits_per_component) +PDFErrorOr Filter::handle_lzw_and_flate_parameters(ByteBuffer buffer, RefPtr decode_parms) { // Table 3.7 Optional parameters for LZWDecode and FlateDecode filters + int predictor = 1; + int colors = 1; + int bits_per_component = 8; + int columns = 1; + + if (decode_parms) { + if (decode_parms->contains(CommonNames::Predictor)) + predictor = decode_parms->get_value(CommonNames::Predictor).get(); + if (decode_parms->contains(CommonNames::Colors)) + colors = decode_parms->get_value(CommonNames::Colors).get(); + if (decode_parms->contains(CommonNames::BitsPerComponent)) + bits_per_component = decode_parms->get_value(CommonNames::BitsPerComponent).get(); + if (decode_parms->contains(CommonNames::Columns)) + columns = decode_parms->get_value(CommonNames::Columns).get(); + } if (predictor == 1) return buffer; @@ -257,16 +253,21 @@ PDFErrorOr Filter::handle_lzw_and_flate_parameters(ByteBuffer buffer return decode_png_prediction(buffer_bytes, bytes_per_row, bytes_per_pixel); } -PDFErrorOr Filter::decode_lzw(ReadonlyBytes bytes, int predictor, int columns, int colors, int bits_per_component, int early_change) +PDFErrorOr Filter::decode_lzw(ReadonlyBytes bytes, RefPtr decode_parms) { + // Table 3.7 Optional parameters for LZWDecode and FlateDecode filters + int early_change = 1; + if (decode_parms && decode_parms->contains(CommonNames::EarlyChange)) + early_change = decode_parms->get_value(CommonNames::EarlyChange).get(); + auto decoded = TRY(Compress::LZWDecoder::decode_all(bytes, 8, -early_change)); - return handle_lzw_and_flate_parameters(move(decoded), predictor, columns, colors, bits_per_component); + return handle_lzw_and_flate_parameters(move(decoded), decode_parms); } -PDFErrorOr Filter::decode_flate(ReadonlyBytes bytes, int predictor, int columns, int colors, int bits_per_component) +PDFErrorOr Filter::decode_flate(ReadonlyBytes bytes, RefPtr decode_parms) { auto buff = TRY(Compress::DeflateDecompressor::decompress_all(bytes.slice(2))); - return handle_lzw_and_flate_parameters(move(buff), predictor, columns, colors, bits_per_component); + return handle_lzw_and_flate_parameters(move(buff), decode_parms); } PDFErrorOr Filter::decode_run_length(ReadonlyBytes bytes) diff --git a/Userland/Libraries/LibPDF/Filter.h b/Userland/Libraries/LibPDF/Filter.h index 16f51ce3bd..bb8281a6c6 100644 --- a/Userland/Libraries/LibPDF/Filter.h +++ b/Userland/Libraries/LibPDF/Filter.h @@ -22,8 +22,8 @@ private: static PDFErrorOr decode_ascii85(ReadonlyBytes bytes); static PDFErrorOr decode_png_prediction(Bytes bytes, size_t bytes_per_row, size_t bytes_per_pixel); static PDFErrorOr decode_tiff_prediction(Bytes bytes, int columns, int colors, int bits_per_component); - static PDFErrorOr decode_lzw(ReadonlyBytes bytes, int predictor, int columns, int colors, int bits_per_component, int early_change); - static PDFErrorOr decode_flate(ReadonlyBytes bytes, int predictor, int columns, int colors, int bits_per_component); + static PDFErrorOr decode_lzw(ReadonlyBytes bytes, RefPtr decode_parms); + static PDFErrorOr decode_flate(ReadonlyBytes bytes, RefPtr decode_parms); static PDFErrorOr decode_run_length(ReadonlyBytes bytes); static PDFErrorOr decode_ccitt(ReadonlyBytes bytes); static PDFErrorOr decode_jbig2(ReadonlyBytes bytes); @@ -31,7 +31,7 @@ private: static PDFErrorOr decode_jpx(ReadonlyBytes bytes); static PDFErrorOr decode_crypt(ReadonlyBytes bytes); - static PDFErrorOr handle_lzw_and_flate_parameters(ByteBuffer buffer, int predictor, int columns, int colors, int bits_per_component); + static PDFErrorOr handle_lzw_and_flate_parameters(ByteBuffer buffer, RefPtr decode_parms); }; }