public void InsertData(uint width, uint height, byte[] data) { int bufferSize = (int)((width + 1) * height); MemoryStream idatRawStream = new MemoryStream(bufferSize); for (uint row = 0; row < height; row++) { // Filter type byte idatRawStream.WriteByte(0); // Assuming data is row-major indices idatRawStream.Write(data, (int)(row * width), (int)width); } MemoryStream idatStream = new MemoryStream(4); using (DeflateStream compressStream = new DeflateStream(idatStream, CompressionLevel.NoCompression)) { byte[] rawData = idatRawStream.GetBuffer(); compressStream.Write(rawData, 0, rawData.Length); } byte[] idatData = idatStream.GetBuffer(); Console.WriteLine($"Compressed {bufferSize} bytes to {idatData.Length} bytes."); PNGChunk idatChunk = new PNGChunk("IDAT", idatData); chunks.AddLast(idatChunk); }
public void InsertTransparency(byte transparentColour = 0) { LinkedListNode <PNGChunk> palChunk; foreach (var chunk in chunks) { if (chunk.Type == "PLTE") { palChunk = chunks.Find(chunk); uint palLength = chunk.Length / 3; byte[] transData = new byte[palLength]; for (int i = 0; i < palLength; i++) { if (i == transparentColour) { transData[i] = 0; } else { transData[i] = 255; } } PNGChunk transChunk = new PNGChunk("tRNS", transData); chunks.AddAfter(palChunk, transChunk); break; } } }
private void WriteChunk(Stream stream, PNGChunk chunk) { WriteBigEndian(stream, chunk.Length); byte[] typeBytes = Encoding.ASCII.GetBytes(chunk.Type); stream.Write(typeBytes, 0, 4); if (chunk.Length > 0) { stream.Write(chunk.Data, 0, (int)chunk.Length); } WriteBigEndian(stream, chunk.CRC); }
public void InsertGrabChunk(int xOffset, int yOffset) { byte[] grabData = new byte[8]; MemoryStream stream = new MemoryStream(grabData); WriteBigEndian(stream, xOffset); WriteBigEndian(stream, yOffset); stream.Close(); PNGChunk grabChunk = new PNGChunk("grAb", grabData); if (grabChunkNode == null) { grabChunkNode = chunks.AddAfter(ihdrChunkNode, grabChunk); } else { grabChunkNode.Value = grabChunk; } }
public void InsertHeader(uint width, uint height, byte depth = 8, byte colourType = 3, byte compression = 0, byte filter = 0, byte interlace = 0) { string ihdrType = "IHDR"; byte[] data = new byte[13]; MemoryStream stream = new MemoryStream(data); WriteBigEndian(stream, width); WriteBigEndian(stream, height); stream.WriteByte(depth); stream.WriteByte(colourType); stream.WriteByte(compression); stream.WriteByte(filter); stream.WriteByte(interlace); stream.Close(); PNGChunk ihdrChunk = new PNGChunk(ihdrType, data); ihdrChunkNode = chunks.AddFirst(ihdrChunk); }
public void InsertPalette(Color[] Palette, byte transparentColour = 0, LinkedListNode <PNGChunk> after = null) { byte[] plteData = new byte[Palette.Length * 3]; byte[] trnsData = new byte[Palette.Length]; MemoryStream palStream = new MemoryStream(plteData); MemoryStream transStream = new MemoryStream(trnsData); foreach (Color colour in Palette) { palStream.WriteByte(colour.R); palStream.WriteByte(colour.G); palStream.WriteByte(colour.B); } palStream.Close(); for (int i = 0; i < Palette.Length; i++) { if (i == transparentColour) { transStream.WriteByte(0); // Transparent } else { transStream.WriteByte(255); // Opaque } } transStream.Close(); PNGChunk plteChunk = new PNGChunk("PLTE", plteData); PNGChunk trnsChunk = new PNGChunk("tRNS", trnsData); if (after != null) { chunks.AddAfter(after, trnsChunk); // PLTE will go first chunks.AddAfter(after, plteChunk); } else { chunks.AddLast(plteChunk); chunks.AddLast(trnsChunk); } }
public bool Read() { byte[] header = binaryReader.ReadBytes(8); int comparison = 0; for (int i = 0; i < PNGHead.Length; i++) { comparison += PNGHead[i] ^ header[i]; } if (comparison > 0) { return(false); } while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length) { uint length = ReadBigEndian(); string type = Encoding.ASCII.GetString(binaryReader.ReadBytes(4)); byte[] data; if (length > 0) { data = binaryReader.ReadBytes((int)length); } else { data = new byte[] { }; } uint crc = ReadBigEndian(); PNGChunk chunk = new PNGChunk(length, type, crc, data); chunks.AddLast(chunk); if (type == "IHDR") { ihdrChunkNode = chunks.Last; } } binaryReader.Close(); return(true); }
public void InsertEnd() { PNGChunk endChunk = new PNGChunk("IEND"); chunks.AddLast(endChunk); }