public static void Convert(PngHeader header, byte[] inData, byte[] outData) { int stride = header.Stride; PngFilter decoder = new PngFilter(stride,header.BytesPerPixel); int inOfs=1; int outOfs= 0; for(int row = 0; row < header.Height; row++) { decoder.Decode(inData,outData,inOfs,outOfs); inOfs += stride+1; outOfs += stride; } }
private void WritePng(string fileName, PngHeader header, byte[] rawData) { using (Stream stream = new FileStream(fileName,FileMode.Create)) { using (PngBinaryWriter writer = new PngBinaryWriter(stream)) { writer.WriteSignatur(); writer.WriteHeader(header); using (Stream ms = new MemoryStream()) { PngHelper.WriteRows(rawData,header,ms); writer.WriteData(PngHelper.InflateEncode(ms)); } writer.WriteEnd(); } } }
public void TestWriteCross() { string fileName = Path.Combine(desktop,"cross.png"); int n = 100; PngHeader header = new PngHeader(n,n,1,ColorModel.Gray); byte[] data = new byte[header.Stride*header.Height]; for(int i=0; i<n; i++) { SetPixel(data,i,i,header.Stride); SetPixel(data,n-i,i,header.Stride); SetPixel(data,n/2,i,header.Stride); SetPixel(data,i,n/2,header.Stride); } WritePng(fileName,header,data); }
public void WriteHeader(PngHeader header) { byte[] buffer = new byte[13]; Array.Copy( BitConverter.GetBytes( PngHelper.DWordToInt(header.Width)), 0,buffer,0,4); Array.Copy( BitConverter.GetBytes( PngHelper.DWordToInt(header.Height)), 0,buffer,4,4); buffer[8]=header.BitDepth; buffer[9]=(byte)header.ColorModel; buffer[10]=0; // compress buffer[11]=0; // filter buffer[12]=0; // no interlace Write(new Chunk(buffer.Length,Png.IHDR,buffer)); }
public static byte[] CreateRawImage(PngHeader header) { return new byte[header.Stride*header.Height]; }
public static void WriteRows(byte[] data, PngHeader header, Stream stream) { int stride=header.Stride; if(header.IsMonochrome) { int ofs = 0; for(int row=0; row < header.Height; row++) { stream.WriteByte((byte)Filter.None); stream.Write(data,ofs,stride); ofs+=stride; }; } else { var lines=new Dictionary<Filter, byte[]>(); foreach(Filter filter in Enum.GetValues(typeof(Filter))) lines.Add(filter,new byte[stride]); PngFilter encoder = new PngFilter(stride,header.BytesPerPixel); int ofs = 0; for(int row=0; row < header.Height; row++) { Filter filter = encoder.Encode(data,ofs,lines); stream.WriteByte((byte)filter); stream.Write(lines[filter],0,stride); ofs+=stride; }; } }