Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions PDFSharp.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="BitMiracle.LibTiff.NET, Version=2.3.642.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\BitMiracle.LibTiff.NET.2.3.642.0\lib\net20\BitMiracle.LibTiff.NET.dll</HintPath>
<Reference Include="BitMiracle.LibTiff.NET40, Version=2.4.584.0, Culture=neutral, PublicKeyToken=53879b3e20e7a7d6, processorArchitecture=MSIL">
<HintPath>packages\BitMiracle.LibTiff.NET.2.4.584.1\lib\net40\BitMiracle.LibTiff.NET40.dll</HintPath>
</Reference>
<Reference Include="PdfSharp, Version=1.32.3057.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<HintPath>packages\PDFsharp.1.32.3057.0\lib\net20\PdfSharp.dll</HintPath>
Expand Down
26 changes: 22 additions & 4 deletions Pdf/PdfDictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -155,17 +155,31 @@ private static byte[] GetTiffImageBufferFromCCITTFaxDecode(PdfDictionaryImageMet
/// <returns>The image retrieve from the dictionary. If not found or an invalid image, then null is returned.</returns>
private static Image ImageFromCCITTFaxDecode(PdfDictionary dictionary)
{
Image image = null;
PdfDictionaryImageMetaData imageData = new PdfDictionaryImageMetaData(dictionary);

PixelFormat format = GetPixelFormat(imageData.ColorSpace, imageData.BitsPerPixel, true);
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);

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("<INLINE>", "r", stream, new TiffStream())) {
if (tiff == null) return (null);
Expand Down Expand Up @@ -641,7 +655,6 @@ public static PdfColorSpace Parse(PdfItem colorSpace)
// Standard CMYK Colorspace
{ "/DeviceCMYK", (a) => {
throw new NotImplementedException("CMYK encoded images are not supported.");
return (new PdfCMYKColorSpace());
} },
};

Expand Down Expand Up @@ -673,6 +686,9 @@ class PdfDictionaryImageMetaData
/// <summary>The colorspace information for the image.</summary>
public PdfColorSpace ColorSpace { get; set; }

/// <summary>The Compression for the image.</summary>
public Compression Compression { get; set; }

/// <param name="dictionary">The dictionary object o parse.</param>
public PdfDictionaryImageMetaData(PdfDictionary dictionary)
{
Expand All @@ -698,6 +714,8 @@ private void Initialize(PdfDictionary dictionary)
ColorSpace = PdfDictionaryColorSpace.Parse(colorSpace);
}
else ColorSpace = new PdfRGBColorSpace(); // Default to RGB Color Space

Compression = Compression.CCITTFAX4;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BitMiracle.LibTiff.NET" version="2.3.642.0" targetFramework="net40" />
<package id="BitMiracle.LibTiff.NET" version="2.4.584.1" targetFramework="net40" />
<package id="PDFsharp" version="1.32.3057.0" targetFramework="net40" />
</packages>