private Chunk getChunk(System.IO.Stream inputStream) { Chunk chunk = new Chunk(); chunk.SetLength(GetLong(inputStream)); // The length of the data chunk. chunk.SetType(GetBytes(inputStream, 4)); // The chunk type. chunk.SetData(GetBytes(inputStream, chunk.GetLength())); // The chunk data. chunk.SetCrc(GetLong(inputStream)); // CRC of the type and data chunks. if (!chunk.hasGoodCRC()) { throw new Exception("Chunk has bad CRC."); } return chunk; }
public PNGImage(Stream inputStream) { this.ValidatePNG(inputStream); List <Chunk> list = new List <Chunk>(); this.ProcessPNG(list, inputStream); for (int i = 0; i < list.Count; i++) { Chunk chunk = list[i]; if (chunk.type[0] == 73 && chunk.type[1] == 72 && chunk.type[2] == 68 && chunk.type[3] == 82) { this.w = this.ToIntValue(chunk.GetData(), 0); this.h = this.ToIntValue(chunk.GetData(), 4); this.bitDepth = chunk.GetData()[8]; this.colorType = (int)chunk.GetData()[9]; } else { if (chunk.type[0] == 73 && chunk.type[1] == 68 && chunk.type[2] == 65 && chunk.type[3] == 84) { this.data = this.AppendIdatChunk(this.data, chunk.GetData()); } else { if (chunk.type[0] == 80 && chunk.type[1] == 76 && chunk.type[2] == 84 && chunk.type[3] == 69) { this.rgb = chunk.GetData(); if (this.rgb.Length % 3 != 0) { throw new Exception("Incorrect palette length."); } } else { if (chunk.type[0] != 103 || chunk.type[1] != 65 || chunk.type[2] != 77 || chunk.type[3] != 65) { if (chunk.type[0] == 116 && chunk.type[1] == 82 && chunk.type[2] == 78 && chunk.type[3] == 83) { if (this.colorType == 3) { this.alphaForPalette = new byte[this.w * this.h]; for (int j = 0; j < this.alphaForPalette.Length; j++) { this.alphaForPalette[j] = 255; } byte[] array = chunk.GetData(); for (int k = 0; k < array.Length; k++) { this.alphaForPalette[k] = array[k]; } } } else { if ((chunk.type[0] != 99 || chunk.type[1] != 72 || chunk.type[2] != 82 || chunk.type[3] != 77) && (chunk.type[0] != 115 || chunk.type[1] != 66 || chunk.type[2] != 73 || chunk.type[3] != 84) && chunk.type[0] == 98 && chunk.type[1] == 75 && chunk.type[2] == 71) { byte arg_2B6_0 = chunk.type[3]; } } } } } } } this.inflated = this.GetDecompressedData(); if (this.colorType == 0) { if (this.bitDepth == 16) { this.image = this.getImageColorType0BitDepth16(); } else { if (this.bitDepth == 8) { this.image = this.getImageColorType0BitDepth8(); } else { if (this.bitDepth == 4) { this.image = this.getImageColorType0BitDepth4(); } else { if (this.bitDepth == 2) { this.image = this.getImageColorType0BitDepth2(); } else { if (this.bitDepth != 1) { throw new Exception("Image with unsupported bit depth == " + this.bitDepth); } this.image = this.getImageColorType0BitDepth1(); } } } } } else { if (this.colorType == 6) { this.image = this.getImageColorType6BitDepth8(); } else { if (this.rgb == null) { if (this.bitDepth == 16) { this.image = this.getImageColorType2BitDepth16(); } else { this.image = this.getImageColorType2BitDepth8(); } } else { if (this.bitDepth == 8) { this.image = this.getImageColorType3BitDepth8(); } else { if (this.bitDepth == 4) { this.image = this.getImageColorType3BitDepth4(); } else { if (this.bitDepth == 2) { this.image = this.getImageColorType3BitDepth2(); } else { if (this.bitDepth != 1) { throw new Exception("Image with unsupported bit depth == " + this.bitDepth); } this.image = this.getImageColorType3BitDepth1(); } } } } } } this.deflated = this.DeflateReconstructedData(); }
/** * Used to embed PNG images in the PDF document. * */ public PNGImage(Stream inputStream) { ValidatePNG(inputStream); List <Chunk> chunks = new List <Chunk>(); ProcessPNG(chunks, inputStream); for (int i = 0; i < chunks.Count; i++) { Chunk chunk = chunks[i]; if (chunk.type[0] == 'I' && chunk.type[1] == 'H' && chunk.type[2] == 'D' && chunk.type[3] == 'R') { this.w = ToIntValue(chunk.GetData(), 0); // Width this.h = ToIntValue(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) { // TODO: // Console.WriteLine("Interlaced PNG images are not supported.\nConvert the image using OptiPNG:\noptipng -i0 -o7 myimage.png\n"); } } else if (chunk.type[0] == 'I' && chunk.type[1] == 'D' && chunk.type[2] == 'A' && chunk.type[3] == 'T') { data = AppendIdatChunk(data, chunk.GetData()); } else if (chunk.type[0] == 'P' && chunk.type[1] == 'L' && chunk.type[2] == 'T' && chunk.type[3] == 'E') { rgb = chunk.GetData(); if (rgb.Length % 3 != 0) { throw new Exception("Incorrect palette length."); } } else if (chunk.type[0] == 'g' && chunk.type[1] == 'A' && chunk.type[2] == 'M' && chunk.type[3] == 'A') { // TODO: // Console.WriteLine("gAMA chunk found!"); } else if (chunk.type[0] == 't' && chunk.type[1] == 'R' && chunk.type[2] == 'N' && chunk.type[3] == 'S') { // Console.WriteLine("tRNS chunk found!"); if (colorType == 3) { alphaForPalette = new byte[this.w * this.h]; for (int j = 0; j < alphaForPalette.Length; j++) { alphaForPalette[j] = (byte)0xff; } byte[] alpha = chunk.GetData(); for (int j = 0; j < alpha.Length; j++) { alphaForPalette[j] = alpha[j]; } } } else if (chunk.type[0] == 'c' && chunk.type[1] == 'H' && chunk.type[2] == 'R' && chunk.type[3] == 'M') { // TODO: // Console.WriteLine("cHRM chunk found!"); } else if (chunk.type[0] == 's' && chunk.type[1] == 'B' && chunk.type[2] == 'I' && chunk.type[3] == 'T') { // TODO: // Console.WriteLine("sBIT chunk found!"); } else if (chunk.type[0] == 'b' && chunk.type[1] == 'K' && chunk.type[2] == 'G' && chunk.type[3] == 'D') { // TODO: // Console.WriteLine("bKGD chunk found!"); } } inflated = GetDecompressedData(); if (colorType == 0) { // Grayscale Image if (bitDepth == 16) { image = getImageColorType0BitDepth16(); } else if (bitDepth == 8) { image = getImageColorType0BitDepth8(); } else if (bitDepth == 4) { image = getImageColorType0BitDepth4(); } else if (bitDepth == 2) { image = getImageColorType0BitDepth2(); } else if (bitDepth == 1) { image = getImageColorType0BitDepth1(); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } else if (colorType == 6) { if (bitDepth == 8) { image = getImageColorType6BitDepth8(); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } else { // Color Image if (rgb == null) { // Trucolor Image if (bitDepth == 16) { image = getImageColorType2BitDepth16(); } else { image = getImageColorType2BitDepth8(); } } else { // Indexed Image if (bitDepth == 8) { image = getImageColorType3BitDepth8(); } else if (bitDepth == 4) { image = getImageColorType3BitDepth4(); } else if (bitDepth == 2) { image = getImageColorType3BitDepth2(); } else if (bitDepth == 1) { image = getImageColorType3BitDepth1(); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } } deflated = DeflateReconstructedData(); }
public PNGImage(System.IO.Stream inputStream) { validatePNG(inputStream); List <Chunk> chunks = new List <Chunk>(); processPNG(chunks, inputStream); for (int i = 0; i < chunks.Count; i++) { Chunk chunk = chunks[i]; if (chunk.type[0] == 'I' && chunk.type[1] == 'H' && chunk.type[2] == 'D' && chunk.type[3] == 'R') { this.w = toIntValue(chunk.GetData(), 0); // Width this.h = toIntValue(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 ] ); } else if (chunk.type[0] == 'I' && chunk.type[1] == 'D' && chunk.type[2] == 'A' && chunk.type[3] == 'T') { data = appendIdatChunk(data, chunk.GetData()); } else if (chunk.type[0] == 'P' && chunk.type[1] == 'L' && chunk.type[2] == 'T' && chunk.type[3] == 'E') { rgb = chunk.GetData(); if (rgb.Length % 3 != 0) { throw new Exception("Incorrect palette length."); } } } inflated = getDecompressedData(); if (colorType == 0) { // Grayscale Image if (bitDepth == 16) { image = getImageColorType0BitDepth16(); } else if (bitDepth == 8) { image = getImageColorType0BitDepth8(); } else if (bitDepth == 4) { image = getImageColorType0BitDepth4(); } else if (bitDepth == 2) { image = getImageColorType0BitDepth2(); } else if (bitDepth == 1) { image = getImageColorType0BitDepth1(); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } else if (colorType == 6) { image = getImageColorType6BitDepth8(); } else { // Color Image if (rgb == null) { // Trucolor Image if (bitDepth == 16) { image = getImageColorType2BitDepth16(); } else { image = getImageColorType2BitDepth8(); } } else { // Indexed Image if (bitDepth == 8) { image = getImageColorType3BitDepth8(); } else if (bitDepth == 4) { image = getImageColorType3BitDepth4(); } else if (bitDepth == 2) { image = getImageColorType3BitDepth2(); } else if (bitDepth == 1) { image = getImageColorType3BitDepth1(); } else { throw new Exception("Image with unsupported bit depth == " + bitDepth); } } } deflated = deflateReconstructedData(); }