// Indexed Image with Bit Depth == 8 private byte[] GetImageColorType3BitDepth8(byte[] buf) { byte[] image = new byte[3 * (this.w * this.h)]; byte[] filters = new byte[this.h]; byte[] alpha = null; if (tRNS != null) { alpha = new byte[this.w * this.h]; for (int i = 0; i < alpha.Length; i++) { alpha[i] = (byte)0xff; } } int bytesPerLine = this.w + 1; int m = 0; int n = 0; int j = 0; for (int i = 0; i < buf.Length; i++) { if (i % bytesPerLine == 0) { filters[m++] = buf[i]; } else { int k = ((int)buf[i]) & 0xff; if (tRNS != null && k < tRNS.Length) { alpha[n] = tRNS[k]; } n++; image[j++] = pLTE[3 * k]; image[j++] = pLTE[3 * k + 1]; image[j++] = pLTE[3 * k + 2]; } } ApplyFilters(filters, image, this.w, this.h, 3); if (tRNS != null) { deflatedAlphaData = Compressor.deflate(alpha); } return(image); }
// Truecolor Image with Alpha Transparency private byte[] GetImageColorType6BitDepth8(byte[] buf) { byte[] idata = new byte[3 * this.w * this.h]; // Image data byte[] alpha = new byte[this.w * this.h]; // Alpha values byte[] image = new byte[4 * this.w * this.h]; byte[] filters = new byte[this.h]; int bytesPerLine = 4 * this.w + 1; int k = 0; int j = 0; int i = 0; for (; i < buf.Length; i++) { if (i % bytesPerLine == 0) { filters[j++] = buf[i]; } else { image[k++] = buf[i]; } } ApplyFilters(filters, image, this.w, this.h, 4); k = 0; j = 0; i = 0; while (i < image.Length) { idata[j++] = image[i++]; idata[j++] = image[i++]; idata[j++] = image[i++]; alpha[k++] = image[i++]; } deflatedAlphaData = Compressor.deflate(alpha); return(idata); }
/** * Used to embed PNG images in the PDF document. * */ public PNGImage(Stream inputStream) { ValidatePNG(inputStream); List <Chunk> chunks = ProcessPNG(inputStream); for (int i = 0; i < chunks.Count; i++) { Chunk chunk = chunks[i]; String chunkType = System.Text.Encoding.UTF8.GetString(chunk.type); if (chunkType.Equals("IHDR")) { this.w = (int)ToUInt32(chunk.GetData(), 0); // Width this.h = (int)ToUInt32(chunk.GetData(), 4); // Height this.bitDepth = chunk.GetData()[8]; // Bit Depth this.colorType = chunk.GetData()[9]; // Color Type // Console.WriteLine( // "Bit Depth == " + chunk.GetData()[8]); // Console.WriteLine( // "Color Type == " + chunk.GetData()[9]); // Console.WriteLine(chunk.GetData()[10]); // Console.WriteLine(chunk.GetData()[11]); // Console.WriteLine(chunk.GetData()[12]); if (chunk.GetData()[12] == 1) { Console.WriteLine("Interlaced PNG images are not supported."); Console.WriteLine("Convert the image using OptiPNG:\noptipng -i0 -o7 myimage.png\n"); } } else if (chunkType.Equals("IDAT")) { iDAT = AppendIdatChunk(iDAT, chunk.GetData()); } else if (chunkType.Equals("PLTE")) { pLTE = chunk.GetData(); if (pLTE.Length % 3 != 0) { throw new Exception("Incorrect palette length."); } } else if (chunkType.Equals("gAMA")) { // TODO: // Console.WriteLine("gAMA chunk found!"); } else if (chunkType.Equals("tRNS")) { // Console.WriteLine("tRNS chunk found!"); if (colorType == 3) { tRNS = chunk.GetData(); } } else if (chunkType.Equals("cHRM")) { // TODO: // Console.WriteLine("cHRM chunk found!"); } else if (chunkType.Equals("sBIT")) { // TODO: // Console.WriteLine("sBIT chunk found!"); } else if (chunkType.Equals("bKGD")) { // TODO: // Console.WriteLine("bKGD chunk found!"); } } byte[] inflatedImageData = Decompressor.inflate(iDAT); byte[] imageData; if (colorType == 0) { // Grayscale Image if (bitDepth == 16) { imageData = GetImageColorType0BitDepth16(inflatedImageData); } else if (bitDepth == 8) { imageData = GetImageColorType0BitDepth8(inflatedImageData); } else if (bitDepth == 4) { imageData = GetImageColorType0BitDepth4(inflatedImageData); } else if (bitDepth == 2) { imageData = GetImageColorType0BitDepth2(inflatedImageData); } else if (bitDepth == 1) { imageData = GetImageColorType0BitDepth1(inflatedImageData); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } else if (colorType == 6) { if (bitDepth == 8) { imageData = GetImageColorType6BitDepth8(inflatedImageData); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } else { // Color Image if (pLTE == null) { // Trucolor Image if (bitDepth == 16) { imageData = GetImageColorType2BitDepth16(inflatedImageData); } else { imageData = GetImageColorType2BitDepth8(inflatedImageData); } } else { // Indexed Image if (bitDepth == 8) { imageData = GetImageColorType3BitDepth8(inflatedImageData); } else if (bitDepth == 4) { imageData = GetImageColorType3BitDepth4(inflatedImageData); } else if (bitDepth == 2) { imageData = GetImageColorType3BitDepth2(inflatedImageData); } else if (bitDepth == 1) { imageData = GetImageColorType3BitDepth1(inflatedImageData); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } } deflatedImageData = Compressor.deflate(imageData); }