Browse Source

LibPDF: Factorize flate parameters handling to its own function

This part will be shared with the LZW filter, so let's factorize it.
Lucas CHOLLET 1 year ago
parent
commit
048ef11136
2 changed files with 19 additions and 10 deletions
  1. 17 10
      Userland/Libraries/LibPDF/Filter.cpp
  2. 2 0
      Userland/Libraries/LibPDF/Filter.h

+ 17 - 10
Userland/Libraries/LibPDF/Filter.cpp

@@ -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)

+ 2 - 0
Userland/Libraries/LibPDF/Filter.h

@@ -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);
 };
 
 }