public PNGChunk(PNGChunk c) { length = c.length; signature = c.signature; data = c.data; crc = c.crc; }
public static void Save(Image src, string dFile) { if ((src is Bitmap) && (src.RawFormat.Guid == ImageFormat.Png.Guid)) { Bitmap srcB = (Bitmap)src; // TODO should we premultiply by alpha??? if (srcB.PixelFormat == PixelFormat.Format32bppArgb || srcB.PixelFormat == PixelFormat.Format24bppRgb) { SwapRandB(srcB, srcB.PixelFormat == PixelFormat.Format32bppArgb); } MemoryStream ms = new MemoryStream(); srcB.Save(ms, ImageFormat.Png); //srcB.Save("debug.png", ImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); BinaryReader br = new BinaryReader(ms); List <PNGChunk> chunks = ReadPNG(br); br.Close(); ms.Close(); // add the iPhone chunk PNGChunk iPhoneChunk = new PNGChunk("CgBI"); iPhoneChunk.Data = new byte[] { 0x30, 0x0, 0x20, 0x06 }; // guess? iPhoneChunk.CRC = 0x179e8065; chunks.Insert(0, iPhoneChunk); // fix IDAT chunks foreach (PNGChunk c in chunks) { if (c.Signature == "IDAT") { byte[] orig = c.Data; byte[] newd = new byte[orig.Length - 6]; Array.Copy(orig, 2, newd, 0, orig.Length - 6); // drop zlib header/checksum c.Data = newd; } } FileStream fs = new FileStream(dFile, FileMode.Create, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs); WritePNG(chunks.ToArray(), bw); bw.Close(); fs.Close(); } else { src.Save(dFile); } }
void dIHDR(PNGChunk c) { IHDRChunk ic = new IHDRChunk(c); ind(); wo("Width: " + ic.Width); wo("Height: " + ic.Height); wo("BitDepth: " + ic.BitDepth); wo("ColorType: " + ic.ColorType + " " + decodeColorType(ic)); oud(); }
public IHDRChunk(PNGChunk c) : base(c) { MemoryStream ms = new MemoryStream(c.Data); BinaryReader br = new BinaryReader(ms); width = ReadUInt32BE(br); height = ReadUInt32BE(br); bitdepth = br.ReadByte(); colortype = br.ReadByte(); compressionmethod = br.ReadByte(); filtermethod = br.ReadByte(); interlacemethod = br.ReadByte(); }
public static List <PNGChunk> ReadPNG(BinaryReader br) { List <PNGChunk> chunks = new List <PNGChunk>(); foreach (byte b in PNGSignature) { byte testb = br.ReadByte(); if (b != testb) { throw new OutOfMemoryException(); } } // read all the chunks PNGChunk c1; do { c1 = new PNGChunk(br); chunks.Add(c1); } while (c1.Signature != "IEND"); return(chunks); }
public static void WritePNG(PNGChunk[] chunks, BinaryWriter bw) { bw.Write(PNGSignature); foreach (PNGChunk c in chunks) { c.Write(bw); } }
public static void Save(Image src, string dFile) { if ((src is Bitmap) && (src.RawFormat.Guid == ImageFormat.Png.Guid)) { Bitmap srcB = (Bitmap)src; // TODO should we premultiply by alpha??? if (srcB.PixelFormat == PixelFormat.Format32bppArgb || srcB.PixelFormat == PixelFormat.Format24bppRgb) SwapRandB(srcB, srcB.PixelFormat == PixelFormat.Format32bppArgb); MemoryStream ms = new MemoryStream(); srcB.Save(ms, ImageFormat.Png); //srcB.Save("debug.png", ImageFormat.Png); ms.Seek(0, SeekOrigin.Begin); BinaryReader br = new BinaryReader(ms); List<PNGChunk> chunks = ReadPNG(br); br.Close(); ms.Close(); // add the iPhone chunk PNGChunk iPhoneChunk = new PNGChunk("CgBI"); iPhoneChunk.Data = new byte[] { 0x30, 0x0, 0x20, 0x06 }; // guess? iPhoneChunk.CRC = 0x179e8065; chunks.Insert(0, iPhoneChunk); // fix IDAT chunks foreach (PNGChunk c in chunks) { if (c.Signature == "IDAT") { byte[] orig = c.Data; byte[] newd = new byte[orig.Length - 6]; Array.Copy(orig, 2, newd, 0, orig.Length - 6); // drop zlib header/checksum c.Data = newd; } } FileStream fs = new FileStream(dFile, FileMode.Create, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs); WritePNG(chunks.ToArray(), bw); bw.Close(); fs.Close(); } else src.Save(dFile); }
public static List<PNGChunk> ReadPNG(BinaryReader br) { List<PNGChunk> chunks = new List<PNGChunk>(); foreach (byte b in PNGSignature) { byte testb = br.ReadByte(); if (b != testb) throw new OutOfMemoryException(); } // read all the chunks PNGChunk c1; do { c1 = new PNGChunk(br); chunks.Add(c1); } while (c1.Signature != "IEND"); return chunks; }