From 30039aa6d35cf5aa15c5ea24cd35081834894d24 Mon Sep 17 00:00:00 2001 From: Kasper Frank Date: Sun, 9 May 2021 18:45:26 +0200 Subject: [PATCH 1/2] Decode fix for CCITTFax Group 3 (1 dimensional version) --- .../Filters/CcittFaxCompressionType.cs | 14 +++++++----- .../Filters/CcittFaxDecodeFilter.cs | 8 +++---- .../Filters/CcittFaxDecoderStream.cs | 22 ++++--------------- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/UglyToad.PdfPig/Filters/CcittFaxCompressionType.cs b/src/UglyToad.PdfPig/Filters/CcittFaxCompressionType.cs index da388445..f0f372de 100644 --- a/src/UglyToad.PdfPig/Filters/CcittFaxCompressionType.cs +++ b/src/UglyToad.PdfPig/Filters/CcittFaxCompressionType.cs @@ -6,16 +6,20 @@ internal enum CcittFaxCompressionType { /// - /// Modified Huffman - Group 3 (T4) + /// Modified Huffman (MH) - Group 3 variation (T2) /// ModifiedHuffman, /// - /// Modified Read - Group 3 (optional T4) + /// Modified Huffman (MH) - Group 3 (T4) /// - T4, + Group3_1D, /// - /// Modified Modified Read - Group 4 (T6) + /// Modified Read (MR) - Group 3 (T4) /// - T6 + Group3_2D, + /// + /// Modified Modified Read (MMR) - Group 4 (T6) + /// + Group4_2D } } diff --git a/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs b/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs index 9fe84246..8cae7dd7 100644 --- a/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs +++ b/src/UglyToad.PdfPig/Filters/CcittFaxDecodeFilter.cs @@ -60,7 +60,7 @@ { if (k == 0) { - var compressionType = CcittFaxCompressionType.T4; // Group 3 1D + var compressionType = CcittFaxCompressionType.Group3_1D; // Group 3 1D if (input.Count < 20) { @@ -78,7 +78,7 @@ b = (short)((b << 1) + ((input[(i / 8)] >> (7 - (i % 8))) & 0x01)); if ((b & 0xFFF) == 1) { - return CcittFaxCompressionType.T4; + return CcittFaxCompressionType.Group3_1D; } } } @@ -88,11 +88,11 @@ else if (k > 0) { // Group 3 2D - return CcittFaxCompressionType.T4; + return CcittFaxCompressionType.Group3_2D; } else { - return CcittFaxCompressionType.T6; + return CcittFaxCompressionType.Group4_2D; } } diff --git a/src/UglyToad.PdfPig/Filters/CcittFaxDecoderStream.cs b/src/UglyToad.PdfPig/Filters/CcittFaxDecoderStream.cs index 71aff5d8..89982af1 100644 --- a/src/UglyToad.PdfPig/Filters/CcittFaxDecoderStream.cs +++ b/src/UglyToad.PdfPig/Filters/CcittFaxDecoderStream.cs @@ -17,7 +17,6 @@ private readonly int columns; private readonly byte[] decodedRow; - private readonly bool optionG32D; private readonly bool optionByteAligned; private readonly CcittFaxCompressionType type; @@ -52,20 +51,6 @@ changesCurrentRow = new int[columns + 2]; optionByteAligned = byteAligned; - switch (type) - { - case CcittFaxCompressionType.ModifiedHuffman: - optionG32D = false; - break; - case CcittFaxCompressionType.T4: - optionG32D = true; - break; - case CcittFaxCompressionType.T6: - optionG32D = false; - break; - default: - throw new ArgumentOutOfRangeException(nameof(type), type, "Illegal parameter"); - } } private void Fetch() @@ -252,7 +237,7 @@ } done: - if (!optionG32D || ReadBit()) + if (type == CcittFaxCompressionType.Group3_1D || ReadBit()) { Decode1D(); } @@ -279,10 +264,11 @@ case CcittFaxCompressionType.ModifiedHuffman: DecodeRowType2(); break; - case CcittFaxCompressionType.T4: + case CcittFaxCompressionType.Group3_1D: + case CcittFaxCompressionType.Group3_2D: DecodeRowType4(); break; - case CcittFaxCompressionType.T6: + case CcittFaxCompressionType.Group4_2D: DecodeRowType6(); break; default: From 9f6dd5375d4ee60b76e7220077fbbbda15558dd5 Mon Sep 17 00:00:00 2001 From: Kasper Frank Date: Mon, 10 May 2021 09:01:57 +0200 Subject: [PATCH 2/2] DecodeParms might be an indirect reference --- src/UglyToad.PdfPig/XObjects/XObjectFactory.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/UglyToad.PdfPig/XObjects/XObjectFactory.cs b/src/UglyToad.PdfPig/XObjects/XObjectFactory.cs index 57c487f0..2df928ed 100644 --- a/src/UglyToad.PdfPig/XObjects/XObjectFactory.cs +++ b/src/UglyToad.PdfPig/XObjects/XObjectFactory.cs @@ -10,7 +10,8 @@ using Graphics.Colors; using Graphics.Core; using Tokenization.Scanner; - using Tokens; + using Tokens; + using UglyToad.PdfPig.Parser.Parts; using Util; internal static class XObjectFactory @@ -99,7 +100,15 @@ } } - var decodedBytes = supportsFilters ? new Lazy>(() => xObject.Stream.Decode(filterProvider, pdfScanner)) + var decodeParams = dictionary.GetObjectOrDefault(NameToken.DecodeParms, NameToken.Dp); + if (decodeParams is IndirectReferenceToken refToken) + { + dictionary = dictionary.With(NameToken.DecodeParms, pdfScanner.Get(refToken.Data).Data); + } + + var streamToken = new StreamToken(dictionary, xObject.Stream.Data); + + var decodedBytes = supportsFilters ? new Lazy>(() => streamToken.Decode(filterProvider, pdfScanner)) : null; var decode = EmptyArray.Instance;