/// <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> /// Reads next frame image /// </summary> protected void ReadImage() { this.ix = this.ReadShort(); this.iy = this.ReadShort(); this.iw = this.ReadShort(); this.ih = this.ReadShort(); int num = this.inp.ReadByte(); this.lctFlag = (num & 0x80) != 0; this.interlace = (num & 0x40) != 0; this.lctSize = ((int)2) << (num & 7); this.m_bpc = NewBpc(this.m_gbpc); if (this.lctFlag) { this.m_curr_table = this.ReadColorTable((num & 7) + 1); this.m_bpc = NewBpc((num & 7) + 1); } else { this.m_curr_table = this.m_global_table; } if (this.transparency && (this.transIndex >= (this.m_curr_table.Length / 3))) { this.transparency = false; } if (this.transparency && (this.m_bpc == 1)) { byte[] destinationArray = new byte[12]; Array.Copy(this.m_curr_table, 0, destinationArray, 0, 6); this.m_curr_table = destinationArray; this.m_bpc = 2; } if (!this.DecodeImageData()) { this.Skip(); } Image image = new RawImage(this.iw, this.ih, 1, this.m_bpc, this.m_out); int length = this.m_curr_table.Length; PdfIndexedColorSpace space = new PdfIndexedColorSpace(PdfName.DeviceRGB, (length / 3) - 1, this.m_curr_table); PdfDictionary dictionary2 = new PdfDictionary(); dictionary2.Add(PdfName.ColorSpace, space); PdfDictionary dictionary = dictionary2; image.Properties = dictionary; if (this.transparency) { image.Transparency = new int[] { this.transIndex, this.transIndex }; } image.OriginalType = ImageType.GIF; image.OriginalData = this.fromData; GifFrame item = new GifFrame { Image = image, X = this.ix, Y = this.iy }; this.frames.Add(item); }
/// <summary> /// Indexeds the model. /// </summary> /// <param name="bdata">The bdata.</param> /// <param name="bpc">The BPC.</param> /// <param name="paletteEntries">The palette entries.</param> /// <returns></returns> private Image IndexedModel(byte[] bdata, int bpc, int paletteEntries) { Image image = new RawImage(this.width, this.height, 1, bpc, bdata); byte[] palette = this.GetPalette(paletteEntries); int length = palette.Length; PdfIndexedColorSpace space = new PdfIndexedColorSpace(PdfName.DeviceRGB, (length / 3) - 1, palette); PdfDictionary dictionary2 = new PdfDictionary(); dictionary2.Add(PdfName.ColorSpace, space); PdfDictionary dictionary = dictionary2; image.Properties = dictionary; return(image); }
private static void DrawColorsAndColorSpaces(PdfPage page, PdfFont titleFont, PdfFont sectionFont, Stream iccStream) { PdfBrush brush = new PdfBrush(); page.Graphics.DrawString("Colors and colorspaces", titleFont, brush, 20, 50); page.Graphics.DrawString("DeviceRGB", sectionFont, brush, 20, 70); PdfPen rgbPen = new PdfPen(PdfRgbColor.DarkRed, 4); PdfBrush rgbBrush = new PdfBrush(PdfRgbColor.LightGoldenrodYellow); page.Graphics.DrawRectangle(rgbPen, rgbBrush, 20, 85, 250, 100); page.Graphics.DrawString("DeviceCMYK", sectionFont, brush, 340, 70); PdfPen cmykPen = new PdfPen(new PdfCmykColor(1, 0.5, 0, 0.1), 4); PdfBrush cmykBrush = new PdfBrush(new PdfCmykColor(0, 0.5, 0.43, 0)); page.Graphics.DrawRectangle(cmykPen, cmykBrush, 340, 85, 250, 100); page.Graphics.DrawString("DeviceGray", sectionFont, brush, 20, 200); PdfPen grayPen = new PdfPen(new PdfGrayColor(0.1), 4); PdfBrush grayBrush = new PdfBrush(new PdfGrayColor(0.75)); page.Graphics.DrawRectangle(grayPen, grayBrush, 20, 215, 250, 100); page.Graphics.DrawString("Indexed", sectionFont, brush, 340, 200); PdfIndexedColorSpace indexedColorSpace = new PdfIndexedColorSpace(); indexedColorSpace.ColorCount = 2; indexedColorSpace.BaseColorSpace = new PdfRgbColorSpace(); indexedColorSpace.ColorTable = new byte[] { 192, 0, 0, 0, 0, 128 }; PdfIndexedColor indexedColor0 = new PdfIndexedColor(indexedColorSpace); indexedColor0.ColorIndex = 0; PdfIndexedColor indexedColor1 = new PdfIndexedColor(indexedColorSpace); indexedColor1.ColorIndex = 1; PdfPen indexedPen = new PdfPen(indexedColor0, 4); PdfBrush indexedBrush = new PdfBrush(indexedColor1); page.Graphics.DrawRectangle(indexedPen, indexedBrush, 340, 215, 250, 100); page.Graphics.DrawString("CalGray", sectionFont, brush, 20, 330); PdfCalGrayColorSpace calGrayColorSpace = new PdfCalGrayColorSpace(); PdfCalGrayColor calGrayColor1 = new PdfCalGrayColor(calGrayColorSpace); calGrayColor1.Gray = 0.6; PdfCalGrayColor calGrayColor2 = new PdfCalGrayColor(calGrayColorSpace); calGrayColor2.Gray = 0.2; PdfPen calGrayPen = new PdfPen(calGrayColor1, 4); PdfBrush calGrayBrush = new PdfBrush(calGrayColor2); page.Graphics.DrawRectangle(calGrayPen, calGrayBrush, 20, 345, 250, 100); page.Graphics.DrawString("CalRGB", sectionFont, brush, 340, 330); PdfCalRgbColorSpace calRgbColorSpace = new PdfCalRgbColorSpace(); PdfCalRgbColor calRgbColor1 = new PdfCalRgbColor(calRgbColorSpace); calRgbColor1.Red = 0.1; calRgbColor1.Green = 0.5; calRgbColor1.Blue = 0.25; PdfCalRgbColor calRgbColor2 = new PdfCalRgbColor(calRgbColorSpace); calRgbColor2.Red = 0.6; calRgbColor2.Green = 0.1; calRgbColor2.Blue = 0.9; PdfPen calRgbPen = new PdfPen(calRgbColor1, 4); PdfBrush calRgbBrush = new PdfBrush(calRgbColor2); page.Graphics.DrawRectangle(calRgbPen, calRgbBrush, 340, 345, 250, 100); page.Graphics.DrawString("L*a*b", sectionFont, brush, 20, 460); PdfLabColorSpace labColorSpace = new PdfLabColorSpace(); PdfLabColor labColor1 = new PdfLabColor(labColorSpace); labColor1.L = 90; labColor1.A = -40; labColor1.B = 120; PdfLabColor labColor2 = new PdfLabColor(labColorSpace); labColor2.L = 45; labColor2.A = 90; labColor2.B = -34; PdfPen labPen = new PdfPen(labColor1, 4); PdfBrush labBrush = new PdfBrush(labColor2); page.Graphics.DrawRectangle(labPen, labBrush, 20, 475, 250, 100); page.Graphics.DrawString("Icc", sectionFont, brush, 340, 460); PdfIccColorSpace iccColorSpace = new PdfIccColorSpace(); byte[] iccData = new byte[iccStream.Length]; iccStream.Read(iccData, 0, iccData.Length); iccColorSpace.IccProfile = iccData; iccColorSpace.AlternateColorSpace = new PdfRgbColorSpace(); iccColorSpace.ColorComponents = 3; PdfIccColor iccColor1 = new PdfIccColor(iccColorSpace); iccColor1.ColorComponents = new double[] { 0.45, 0.1, 0.22 }; PdfIccColor iccColor2 = new PdfIccColor(iccColorSpace); iccColor2.ColorComponents = new double[] { 0.21, 0.76, 0.31 }; PdfPen iccPen = new PdfPen(iccColor1, 4); PdfBrush iccBrush = new PdfBrush(iccColor2); page.Graphics.DrawRectangle(iccPen, iccBrush, 340, 475, 250, 100); page.Graphics.DrawString("Separation", sectionFont, brush, 20, 590); PdfExponentialFunction tintTransform = new PdfExponentialFunction(); tintTransform.Domain = new double[] { 0, 1 }; tintTransform.Range = new double[] { 0, 1, 0, 1, 0, 1, 0, 1 }; tintTransform.Exponent = 1; tintTransform.C0 = new double[] { 0, 0, 0, 0 }; tintTransform.C1 = new double[] { 1, 0.5, 0, 0.1 }; PdfSeparationColorSpace separationColorSpace = new PdfSeparationColorSpace(); separationColorSpace.AlternateColorSpace = new PdfCmykColorSpace(); separationColorSpace.Colorant = "Custom Blue"; separationColorSpace.TintTransform = tintTransform; PdfSeparationColor separationColor1 = new PdfSeparationColor(separationColorSpace); separationColor1.Tint = 0.23; PdfSeparationColor separationColor2 = new PdfSeparationColor(separationColorSpace); separationColor2.Tint = 0.74; PdfPen separationPen = new PdfPen(separationColor1, 4); PdfBrush separationBrush = new PdfBrush(separationColor2); page.Graphics.DrawRectangle(separationPen, separationBrush, 20, 605, 250, 100); page.Graphics.DrawString("Pantone", sectionFont, brush, 340, 590); PdfPen pantonePen = new PdfPen(PdfPantoneColor.ReflexBlue, 4); PdfBrush pantoneBrush = new PdfBrush(PdfPantoneColor.RhodamineRed); page.Graphics.DrawRectangle(pantonePen, pantoneBrush, 340, 605, 250, 100); page.Graphics.CompressAndClose(); }
/// <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); }
/// <summary> /// Loads the image. /// </summary> private void LoadImage() { for (int i = 0; i < PNGID.Length; i++) { if (PNGID[i] != this.stream.ReadByte()) { throw new PdfUnrecognizedImageFormatException("File is not a valid PNG."); } } byte[] buffer = new byte[0x1000]; while (true) { int @int = GetInt(this.stream); string s = GetString(this.stream); if ((@int < 0) || !CheckMarker(s)) { throw new PdfUnrecognizedImageFormatException("Corrupted PNG file."); } if ("IDAT".Equals(s)) { while (@int != 0) { int count = this.stream.Read(buffer, 0, Math.Min(@int, 0x1000)); if (count <= 0) { return; } this.idat.Write(buffer, 0, count); @int -= count; } } else if (!"tRNS".Equals(s)) { if ("IHDR".Equals(s)) { this.width = GetInt(this.stream); this.height = GetInt(this.stream); this.bitDepth = this.stream.ReadByte(); this.colorType = this.stream.ReadByte(); this.stream.ReadByte(); this.stream.ReadByte(); this.interlaceMethod = this.stream.ReadByte(); } else if ("PLTE".Equals(s)) { if (this.colorType == 3) { List <byte> list = new List <byte>(); while (@int-- > 0) { list.Add((byte)this.stream.ReadByte()); } list.ToArray(); PdfIndexedColorSpace space = new PdfIndexedColorSpace(this.GetColorspace(), (@int / 3) - 1, list.ToArray()); this.properties.Add(PdfName.ColorSpace, space); } else { Image.Skip(this.stream, @int); } } else if ("pHYs".Equals(s)) { int num9 = GetInt(this.stream); int num10 = GetInt(this.stream); if (this.stream.ReadByte() == 1) { this.dpiX = (int)((num9 * 0.0254f) + 0.5f); this.dpiY = (int)((num10 * 0.0254f) + 0.5f); } else if (num10 != 0) { this.XYRatio = ((float)num9) / ((float)num10); } } else if ("cHRM".Equals(s)) { this.xW = ((float)GetInt(this.stream)) / 100000f; this.yW = ((float)GetInt(this.stream)) / 100000f; this.xR = ((float)GetInt(this.stream)) / 100000f; this.yR = ((float)GetInt(this.stream)) / 100000f; this.xG = ((float)GetInt(this.stream)) / 100000f; this.yG = ((float)GetInt(this.stream)) / 100000f; this.xB = ((float)GetInt(this.stream)) / 100000f; this.yB = ((float)GetInt(this.stream)) / 100000f; this.hasCHRM = ((((Math.Abs(this.xW) >= 0.0001f) && (Math.Abs(this.yW) >= 0.0001f)) && ((Math.Abs(this.xR) >= 0.0001f) && (Math.Abs(this.yR) >= 0.0001f))) && (((Math.Abs(this.xG) >= 0.0001f) && (Math.Abs(this.yG) >= 0.0001f)) && (Math.Abs(this.xB) >= 0.0001f))) && (Math.Abs(this.yB) >= 0.0001f); } else if ("sRGB".Equals(s)) { int index = this.stream.ReadByte(); this.intent = intents[index]; this.gamma = 2.2f; this.xW = 0.3127f; this.yW = 0.329f; this.xR = 0.64f; this.yR = 0.33f; this.xG = 0.3f; this.yG = 0.6f; this.xB = 0.15f; this.yB = 0.06f; this.hasCHRM = true; } else if ("gAMA".Equals(s)) { int num13 = GetInt(this.stream); if (num13 != 0) { this.gamma = 100000f / ((float)num13); if (!this.hasCHRM) { this.xW = 0.3127f; this.yW = 0.329f; this.xR = 0.64f; this.yR = 0.33f; this.xG = 0.3f; this.yG = 0.6f; this.xB = 0.15f; this.yB = 0.06f; this.hasCHRM = true; } } } else if (!"iCCP".Equals(s)) { if ("IEND".Equals(s)) { return; } Image.Skip(this.stream, @int); } } else { switch (this.colorType) { case 0: if (@int >= 2) { @int -= 2; int word = GetWord(this.stream); if (this.bitDepth != 0x10) { this.properties.Add(PdfName.Mask, PdfArray.Convert(new int[] { word, word })); break; } this.transRedGray = word; } break; case 2: if (@int >= 6) { @int -= 6; int num5 = GetWord(this.stream); int num6 = GetWord(this.stream); int num7 = GetWord(this.stream); if (this.bitDepth != 0x10) { this.properties.Add(PdfName.Mask, PdfArray.Convert(new int[] { num5, num5, num6, num6, num7, num7 })); break; } this.transRedGray = num5; this.transGreen = num6; this.transBlue = num7; } break; case 3: if (@int > 0) { this.trans = new byte[@int]; for (int j = 0; j < @int; j++) { this.trans[j] = (byte)this.stream.ReadByte(); } @int = 0; } break; } Image.Skip(this.stream, @int); } Image.Skip(this.stream, 4); } }