public void Write(Texture2D texture2D, Stream outputStream) { width = texture2D.Width; height = texture2D.Height; GetColorData(texture2D); // write PNG signature outputStream.Write(HeaderChunk.PngSignature, 0, HeaderChunk.PngSignature.Length); // write header chunk var headerChunk = new HeaderChunk(); headerChunk.Width = (uint)texture2D.Width; headerChunk.Height = (uint)texture2D.Height; headerChunk.BitDepth = 8; headerChunk.ColorType = colorType; headerChunk.CompressionMethod = 0; headerChunk.FilterMethod = 0; headerChunk.InterlaceMethod = 0; var headerChunkBytes = headerChunk.Encode(); outputStream.Write(headerChunkBytes, 0, headerChunkBytes.Length); // write data chunks var encodedPixelData = EncodePixelData(texture2D); var compressedPixelData = new MemoryStream(); ZlibStream deflateStream = null; try { deflateStream = new ZlibStream(compressedPixelData, CompressionMode.Compress); deflateStream.Write(encodedPixelData, 0, encodedPixelData.Length); deflateStream.Finish(); } catch (Exception exception) { throw new Exception("An error occurred during DEFLATE compression.", exception); } var dataChunk = new DataChunk(); dataChunk.Data = compressedPixelData.ToArray(); var dataChunkBytes = dataChunk.Encode(); outputStream.Write(dataChunkBytes, 0, dataChunkBytes.Length); deflateStream.Dispose(); compressedPixelData.Dispose(); // write end chunk var endChunk = new EndChunk(); var endChunkBytes = endChunk.Encode(); outputStream.Write(endChunkBytes, 0, endChunkBytes.Length); }
public Stream Write(Texture2D texture2D) { textureData = new Color[texture2D.Width * texture2D.Height]; texture2D.GetData <Color>(textureData); var outputStream = new MemoryStream(); // write PNG signature outputStream.Write(HeaderChunk.PngSignature, 0, HeaderChunk.PngSignature.Length); // write header chunk var headerChunk = new HeaderChunk(); headerChunk.Width = (uint)texture2D.Width; headerChunk.Height = (uint)texture2D.Height; headerChunk.BitDepth = 8; headerChunk.ColorType = colorType; headerChunk.CompressionMethod = 0; headerChunk.FilterMethod = 0; headerChunk.InterlaceMethod = 0; var headerChunkBytes = headerChunk.Encode(); outputStream.Write(headerChunkBytes, 0, headerChunkBytes.Length); // write data chunks var encodedPixelData = EncodePixelData(texture2D); var compressedPixelData = new MemoryStream(); try { using (var deflateStream = new ZlibStream(new MemoryStream(encodedPixelData), CompressionMode.Compress)) { deflateStream.CopyTo(compressedPixelData); } } catch (Exception exception) { throw new Exception("An error occurred during DEFLATE compression.", exception); } var dataChunk = new DataChunk(); dataChunk.Data = compressedPixelData.ToArray(); var dataChunkBytes = dataChunk.Encode(); outputStream.Write(dataChunkBytes, 0, dataChunkBytes.Length); // write end chunk var endChunk = new EndChunk(); var endChunkBytes = endChunk.Encode(); outputStream.Write(endChunkBytes, 0, endChunkBytes.Length); return(outputStream); }
public Stream Write(Texture2D texture2D) { textureData = new Color[texture2D.Width * texture2D.Height]; texture2D.GetData<Color>(textureData); var outputStream = new MemoryStream(); // write PNG signature outputStream.Write(HeaderChunk.PngSignature, 0, HeaderChunk.PngSignature.Length); // write header chunk var headerChunk = new HeaderChunk(); headerChunk.Width = (uint)texture2D.Width; headerChunk.Height = (uint)texture2D.Height; headerChunk.BitDepth = 8; headerChunk.ColorType = colorType; headerChunk.CompressionMethod = 0; headerChunk.FilterMethod = 0; headerChunk.InterlaceMethod = 0; var headerChunkBytes = headerChunk.Encode(); outputStream.Write(headerChunkBytes, 0, headerChunkBytes.Length); // write data chunks var encodedPixelData = EncodePixelData(texture2D); var compressedPixelData = new MemoryStream(); try { using (var deflateStream = new ZlibStream(new MemoryStream(encodedPixelData), CompressionMode.Compress)) { deflateStream.CopyTo(compressedPixelData); } } catch (Exception exception) { throw new Exception("An error occurred during DEFLATE compression.", exception); } var dataChunk = new DataChunk(); dataChunk.Data = compressedPixelData.ToArray(); var dataChunkBytes = dataChunk.Encode(); outputStream.Write(dataChunkBytes, 0, dataChunkBytes.Length); // write end chunk var endChunk = new EndChunk(); var endChunkBytes = endChunk.Encode(); outputStream.Write(endChunkBytes, 0, endChunkBytes.Length); return outputStream; }