From 37666ae63bcf46dc7b0402fcdf8a99ebec8ce024 Mon Sep 17 00:00:00 2001 From: John Caprez Date: Sat, 11 Nov 2017 10:38:07 +0100 Subject: [PATCH 1/4] added CCITTFAX4 compression --- Pdf/PdfDictionaryExtensions.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Pdf/PdfDictionaryExtensions.cs b/Pdf/PdfDictionaryExtensions.cs index 23b203f..08181dd 100644 --- a/Pdf/PdfDictionaryExtensions.cs +++ b/Pdf/PdfDictionaryExtensions.cs @@ -133,7 +133,7 @@ private static byte[] GetTiffImageBufferFromCCITTFaxDecode(PdfDictionaryImageMet WriteTiffTag(buffer, TiffTag.IMAGEWIDTH, TiffType.LONG, 1, (uint)imageData.Width); WriteTiffTag(buffer, TiffTag.IMAGELENGTH, TiffType.LONG, 1, (uint)imageData.Height); WriteTiffTag(buffer, TiffTag.BITSPERSAMPLE, TiffType.SHORT, 1, (uint)imageData.BitsPerPixel); - WriteTiffTag(buffer, TiffTag.COMPRESSION, TiffType.SHORT, 1, (uint) Compression.CCITTFAX4); // CCITT Group 4 fax encoding. + WriteTiffTag(buffer, TiffTag.COMPRESSION, TiffType.SHORT, 1, (uint)imageData.Compression); WriteTiffTag(buffer, TiffTag.PHOTOMETRIC, TiffType.SHORT, 1, 0); // WhiteIsZero WriteTiffTag(buffer, TiffTag.STRIPOFFSETS, TiffType.LONG, 1, header_length); WriteTiffTag(buffer, TiffTag.SAMPLESPERPIXEL, TiffType.SHORT, 1, 1); @@ -166,6 +166,11 @@ private static Image ImageFromCCITTFaxDecode(PdfDictionary dictionary) if (ccittFaxDecodeParameters.BlackIs1) bitmap.Palette = PdfIndexedColorSpace.CreateColorPalette(Color.Black, Color.White); else bitmap.Palette = PdfIndexedColorSpace.CreateColorPalette(Color.White, Color.Black); + if (ccittFaxDecodeParameters.K == 0 || ccittFaxDecodeParameters.K > 0) + imageData.Compression = Compression.CCITTFAX3; + else if (ccittFaxDecodeParameters.K < 0) + imageData.Compression = Compression.CCITTFAX4; + using (MemoryStream stream = new MemoryStream(GetTiffImageBufferFromCCITTFaxDecode(imageData, dictionary.Stream.Value))) { using (Tiff tiff = Tiff.ClientOpen("", "r", stream, new TiffStream())) { if (tiff == null) return (null); @@ -673,6 +678,9 @@ class PdfDictionaryImageMetaData /// The colorspace information for the image. public PdfColorSpace ColorSpace { get; set; } + /// The Compression for the image. + public Compression Compression { get; set; } + /// The dictionary object o parse. public PdfDictionaryImageMetaData(PdfDictionary dictionary) { @@ -698,6 +706,8 @@ private void Initialize(PdfDictionary dictionary) ColorSpace = PdfDictionaryColorSpace.Parse(colorSpace); } else ColorSpace = new PdfRGBColorSpace(); // Default to RGB Color Space + + Compression = Compression.CCITTFAX4; } /// From 2ab77a7aabf912c2f7b9cdfa474e3cd5adc68bd9 Mon Sep 17 00:00:00 2001 From: John Caprez Date: Sat, 11 Nov 2017 10:38:47 +0100 Subject: [PATCH 2/4] fixed compiler warnings unreachable code and unused variable --- Pdf/PdfDictionaryExtensions.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Pdf/PdfDictionaryExtensions.cs b/Pdf/PdfDictionaryExtensions.cs index 08181dd..b3ba277 100644 --- a/Pdf/PdfDictionaryExtensions.cs +++ b/Pdf/PdfDictionaryExtensions.cs @@ -155,7 +155,6 @@ private static byte[] GetTiffImageBufferFromCCITTFaxDecode(PdfDictionaryImageMet /// The image retrieve from the dictionary. If not found or an invalid image, then null is returned. private static Image ImageFromCCITTFaxDecode(PdfDictionary dictionary) { - Image image = null; PdfDictionaryImageMetaData imageData = new PdfDictionaryImageMetaData(dictionary); PixelFormat format = GetPixelFormat(imageData.ColorSpace, imageData.BitsPerPixel, true); @@ -646,7 +645,6 @@ public static PdfColorSpace Parse(PdfItem colorSpace) // Standard CMYK Colorspace { "/DeviceCMYK", (a) => { throw new NotImplementedException("CMYK encoded images are not supported."); - return (new PdfCMYKColorSpace()); } }, }; From 53e5f7fa4ce26ae84969838568d809ba011530fb Mon Sep 17 00:00:00 2001 From: John Caprez Date: Sat, 11 Nov 2017 10:44:33 +0100 Subject: [PATCH 3/4] updated TIFF library package --- PDFSharp.Extensions.csproj | 4 ++-- packages.config | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/PDFSharp.Extensions.csproj b/PDFSharp.Extensions.csproj index 635a1c0..0deff53 100644 --- a/PDFSharp.Extensions.csproj +++ b/PDFSharp.Extensions.csproj @@ -31,8 +31,8 @@ 4 - - packages\BitMiracle.LibTiff.NET.2.3.642.0\lib\net20\BitMiracle.LibTiff.NET.dll + + packages\BitMiracle.LibTiff.NET.2.4.584.1\lib\net40\BitMiracle.LibTiff.NET40.dll packages\PDFsharp.1.32.3057.0\lib\net20\PdfSharp.dll diff --git a/packages.config b/packages.config index b2ff103..bf5531b 100644 --- a/packages.config +++ b/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file From 759b194ff43c15d0648da56dfe1e73403d6969c2 Mon Sep 17 00:00:00 2001 From: John Caprez Date: Wed, 30 May 2018 10:13:59 +0200 Subject: [PATCH 4/4] Handle the case when CCITTFaxDecode \DecodeParms are stored as an array instead of a dictionary --- Pdf/PdfDictionaryExtensions.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Pdf/PdfDictionaryExtensions.cs b/Pdf/PdfDictionaryExtensions.cs index b3ba277..6c814df 100644 --- a/Pdf/PdfDictionaryExtensions.cs +++ b/Pdf/PdfDictionaryExtensions.cs @@ -161,7 +161,17 @@ private static Image ImageFromCCITTFaxDecode(PdfDictionary dictionary) Bitmap bitmap = new Bitmap(imageData.Width, imageData.Height, format); // Determine if BLACK=1, create proper indexed color palette. - CCITTFaxDecodeParameters ccittFaxDecodeParameters = new CCITTFaxDecodeParameters(dictionary.Elements["/DecodeParms"].Get() as PdfDictionary); + + PdfDictionary decodeParams; + var decodeParamsObject = dictionary.Elements["/DecodeParms"].Get(); + if (decodeParamsObject is PdfArray) + decodeParams = (decodeParamsObject as PdfArray).First() as PdfDictionary; + else if (decodeParamsObject is PdfDictionary) + decodeParams = decodeParamsObject as PdfDictionary; + else + throw new NotSupportedException("Unknown format of CCITTFaxDecode params."); + + CCITTFaxDecodeParameters ccittFaxDecodeParameters = new CCITTFaxDecodeParameters(decodeParams); if (ccittFaxDecodeParameters.BlackIs1) bitmap.Palette = PdfIndexedColorSpace.CreateColorPalette(Color.Black, Color.White); else bitmap.Palette = PdfIndexedColorSpace.CreateColorPalette(Color.White, Color.Black);