/// <summary> /// Retrieves the specifed dictionary object as an object encoded with CCITTFaxDecode filter (TIFF). /// </summary> /// <param name="dictionary">The dictionary to extract the object from.</param> /// <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); if (ccittFaxDecodeParameters.BlackIs1) bitmap.Palette = PdfIndexedColorSpace.CreateColorPalette(Color.Black, Color.White); else bitmap.Palette = PdfIndexedColorSpace.CreateColorPalette(Color.White, Color.Black); 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); int stride = tiff.ScanlineSize(); byte[] buffer = new byte[stride]; for (int i = 0; i < imageData.Height; i++) { tiff.ReadScanline(buffer, i); Rectangle imgRect = new Rectangle(0, i, imageData.Width, 1); BitmapData imgData = bitmap.LockBits(imgRect, ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed); Marshal.Copy(buffer, 0, imgData.Scan0, buffer.Length); bitmap.UnlockBits(imgData); } } } return (bitmap); }
/// <summary> /// Retrieves the specifed dictionary object as an object encoded with CCITTFaxDecode filter (TIFF). /// </summary> /// <param name="dictionary">The dictionary to extract the object from.</param> /// <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) { 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. 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); } int stride = tiff.ScanlineSize(); byte[] buffer = new byte[stride]; for (int i = 0; i < imageData.Height; i++) { tiff.ReadScanline(buffer, i); Rectangle imgRect = new Rectangle(0, i, imageData.Width, 1); BitmapData imgData = bitmap.LockBits(imgRect, ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed); Marshal.Copy(buffer, 0, imgData.Scan0, buffer.Length); bitmap.UnlockBits(imgData); } } } return(bitmap); }