Merge pull request #326 from kasperdaff/ccitt-fax-filter-fixes

Fixes for recent CCITT Fax Filter implementation
This commit is contained in:
Eliot Jones 2021-05-15 12:26:02 -04:00 committed by GitHub
commit 636936330a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 29 deletions

View File

@ -6,16 +6,20 @@
internal enum CcittFaxCompressionType internal enum CcittFaxCompressionType
{ {
/// <summary> /// <summary>
/// Modified Huffman - Group 3 (T4) /// Modified Huffman (MH) - Group 3 variation (T2)
/// </summary> /// </summary>
ModifiedHuffman, ModifiedHuffman,
/// <summary> /// <summary>
/// Modified Read - Group 3 (optional T4) /// Modified Huffman (MH) - Group 3 (T4)
/// </summary> /// </summary>
T4, Group3_1D,
/// <summary> /// <summary>
/// Modified Modified Read - Group 4 (T6) /// Modified Read (MR) - Group 3 (T4)
/// </summary> /// </summary>
T6 Group3_2D,
/// <summary>
/// Modified Modified Read (MMR) - Group 4 (T6)
/// </summary>
Group4_2D
} }
} }

View File

@ -60,7 +60,7 @@
{ {
if (k == 0) if (k == 0)
{ {
var compressionType = CcittFaxCompressionType.T4; // Group 3 1D var compressionType = CcittFaxCompressionType.Group3_1D; // Group 3 1D
if (input.Count < 20) if (input.Count < 20)
{ {
@ -78,7 +78,7 @@
b = (short)((b << 1) + ((input[(i / 8)] >> (7 - (i % 8))) & 0x01)); b = (short)((b << 1) + ((input[(i / 8)] >> (7 - (i % 8))) & 0x01));
if ((b & 0xFFF) == 1) if ((b & 0xFFF) == 1)
{ {
return CcittFaxCompressionType.T4; return CcittFaxCompressionType.Group3_1D;
} }
} }
} }
@ -88,11 +88,11 @@
else if (k > 0) else if (k > 0)
{ {
// Group 3 2D // Group 3 2D
return CcittFaxCompressionType.T4; return CcittFaxCompressionType.Group3_2D;
} }
else else
{ {
return CcittFaxCompressionType.T6; return CcittFaxCompressionType.Group4_2D;
} }
} }

View File

@ -17,7 +17,6 @@
private readonly int columns; private readonly int columns;
private readonly byte[] decodedRow; private readonly byte[] decodedRow;
private readonly bool optionG32D;
private readonly bool optionByteAligned; private readonly bool optionByteAligned;
private readonly CcittFaxCompressionType type; private readonly CcittFaxCompressionType type;
@ -52,20 +51,6 @@
changesCurrentRow = new int[columns + 2]; changesCurrentRow = new int[columns + 2];
optionByteAligned = byteAligned; 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() private void Fetch()
@ -252,7 +237,7 @@
} }
done: done:
if (!optionG32D || ReadBit()) if (type == CcittFaxCompressionType.Group3_1D || ReadBit())
{ {
Decode1D(); Decode1D();
} }
@ -279,10 +264,11 @@
case CcittFaxCompressionType.ModifiedHuffman: case CcittFaxCompressionType.ModifiedHuffman:
DecodeRowType2(); DecodeRowType2();
break; break;
case CcittFaxCompressionType.T4: case CcittFaxCompressionType.Group3_1D:
case CcittFaxCompressionType.Group3_2D:
DecodeRowType4(); DecodeRowType4();
break; break;
case CcittFaxCompressionType.T6: case CcittFaxCompressionType.Group4_2D:
DecodeRowType6(); DecodeRowType6();
break; break;
default: default:

View File

@ -11,6 +11,7 @@
using Graphics.Core; using Graphics.Core;
using Tokenization.Scanner; using Tokenization.Scanner;
using Tokens; using Tokens;
using UglyToad.PdfPig.Parser.Parts;
using Util; using Util;
internal static class XObjectFactory internal static class XObjectFactory
@ -99,7 +100,15 @@
} }
} }
var decodedBytes = supportsFilters ? new Lazy<IReadOnlyList<byte>>(() => 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<IReadOnlyList<byte>>(() => streamToken.Decode(filterProvider, pdfScanner))
: null; : null;
var decode = EmptyArray<decimal>.Instance; var decode = EmptyArray<decimal>.Instance;