Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
        private void ProcessChunk(byte[] chunkBytes)
        {
            string chunkType = PngChunk.GetChunkTypeString(chunkBytes.Skip(4).Take(4).ToArray());

            switch (chunkType)
            {
            case "IHDR":

                var headerChunk = new HeaderChunk();
                headerChunk.Decode(chunkBytes);
                width         = (int)headerChunk.Width;
                height        = (int)headerChunk.Height;
                bitsPerSample = (int)headerChunk.BitDepth;
                colorType     = headerChunk.ColorType;
                chunks.Add(headerChunk);

                break;

            case "PLTE":

                var paletteChunk = new PaletteChunk();
                paletteChunk.Decode(chunkBytes);
                palette = paletteChunk.Palette;
                chunks.Add(paletteChunk);

                break;

            case "tRNS":

                var transparencyChunk = new TransparencyChunk();
                transparencyChunk.Decode(chunkBytes);
                palette.AddAlphaToColors(transparencyChunk.PaletteTransparencies);
                break;

            case "IDAT":

                var dataChunk = new DataChunk();
                dataChunk.Decode(chunkBytes);
                dataChunks.Add(dataChunk);

                break;

            default:
                break;
            }
        }
Exemplo n.º 5
0
        private void ProcessChunk(byte[] chunkBytes)
        {
            string chunkType = PngChunk.GetChunkTypeString(chunkBytes.Skip(4).Take(4).ToArray());

            switch (chunkType)
            {
                case "IHDR":

                    var headerChunk = new HeaderChunk();
                    headerChunk.Decode(chunkBytes);
                    width = (int)headerChunk.Width;
                    height = (int)headerChunk.Height;
                    bitsPerSample = (int)headerChunk.BitDepth;
                    colorType = headerChunk.ColorType;
                    chunks.Add(headerChunk);

                    break;

                case "PLTE":

                    var paletteChunk = new PaletteChunk();
                    paletteChunk.Decode(chunkBytes);
                    palette = paletteChunk.Palette;
                    chunks.Add(paletteChunk);

                    break;

                case "tRNS":

                    var transparencyChunk = new TransparencyChunk();
                    transparencyChunk.Decode(chunkBytes);
                    palette.AddAlphaToColors(transparencyChunk.PaletteTransparencies);
                    break;

                case "IDAT":

                    var dataChunk = new DataChunk();
                    dataChunk.Decode(chunkBytes);
                    dataChunks.Add(dataChunk);

                    break;

                default:
                    break;
            }
        }