Represents the png header chunk.
        /// <summary>
        /// Reads the specified scanline.
        /// </summary>
        /// <param name="scanline">The scanline.</param>
        /// <param name="pixels">The pixels, where the colors should be stored in RGBA format.</param>
        /// <param name="header">The header, which contains information about the png file, like
        /// the width of the image and the height.</param>
        public void ReadScanline(byte[] scanline, byte[] pixels, PngHeader header)
        {
            int offset = 0;

            byte[] newScanline = scanline.ToArrayByBitsLength(header.BitDepth);

            if (_useAlpha)
            {
                Array.Copy(newScanline, 0, pixels, _row * header.Width * 4, newScanline.Length);
            }
            else
            {
                for (int x = 0; x < newScanline.Length / 3; x++)
                {
                    offset = (_row * header.Width + x) * 4;

                    pixels[offset + 0] = newScanline[x * 3];
                    pixels[offset + 1] = newScanline[x * 3 + 1];
                    pixels[offset + 2] = newScanline[x * 3 + 2];
                    pixels[offset + 3] = (byte)255;
                }
            }

            _row++;
        }
Example #2
0
        /// <summary>
        /// Reads the specified scanline.
        /// </summary>
        /// <param name="scanline">The scanline.</param>
        /// <param name="pixels">The pixels, where the colors should be stored in RGBA format.</param>
        /// <param name="header">The header, which contains information about the png file, like
        /// the width of the image and the height.</param>
        public void ReadScanline(byte[] scanline, byte[] pixels, PngHeader header)
        {
            int offset = 0;

            //byte[] newScanline = scanline.ToArrayByBitsLength(header.BitDepth);
            byte[] newScanline = IEnum.ToArrayByBitsLength(scanline, header.BitDepth);
            if (_useAlpha)
            {
                for (int x = 0; x < header.Width / 2; x++)
                {
                    offset = (_row * header.Width + x) * 4;

                    pixels[offset + 0] = newScanline[x * 2];
                    pixels[offset + 1] = newScanline[x * 2];
                    pixels[offset + 2] = newScanline[x * 2];
                    pixels[offset + 3] = newScanline[x * 2 + 1];
                }
            }
            else
            {
                for (int x = 0; x < header.Width; x++)
                {
                    offset = (_row * header.Width + x) * 4;

                    pixels[offset + 0] = newScanline[x];
                    pixels[offset + 1] = newScanline[x];
                    pixels[offset + 2] = newScanline[x];
                    pixels[offset + 3] = (byte)255;
                }
            }

            _row++;
        }
        /// <summary>
        /// Reads the specified scanline.
        /// </summary>
        /// <param name="scanline">The scanline.</param>
        /// <param name="pixels">The pixels, where the colors should be stored in RGBA format.</param>
        /// <param name="header">The header, which contains information about the png file, like
        /// the width of the image and the height.</param>
        public void ReadScanline(byte[] scanline, byte[] pixels, PngHeader header)
        {
            byte[] newScanline = scanline.ToArrayByBitsLength(header.BitDepth);

            int offset = 0, index = 0;

            if (_paletteAlpha != null && _paletteAlpha.Length > 0)
            {
                // If the alpha palette is not null and does one or
                // more entries, this means, that the image contains and alpha
                // channel and we should try to read it.
                for (int i = 0; i < header.Width; i++)
                {
                    index = newScanline[i];

                    offset = (_row * header.Width + i) * 4;

                    pixels[offset + 0] = _palette[index * 3];
                    pixels[offset + 1] = _palette[index * 3 + 1];
                    pixels[offset + 2] = _palette[index * 3 + 2];
                    pixels[offset + 3] = _paletteAlpha.Length > index ? _paletteAlpha[index] : (byte)255;
                }
            }
            else
            {
                for (int i = 0; i < header.Width; i++)
                {
                    index = newScanline[i];

                    offset = (_row * header.Width + i) * 4;

                    pixels[offset + 0] = _palette[index * 3];
                    pixels[offset + 1] = _palette[index * 3 + 1];
                    pixels[offset + 2] = _palette[index * 3 + 2];
                    pixels[offset + 3] = (byte)255;
                }
            }

            _row++;
        }
        private void ReadHeaderChunk(byte[] data)
        {
            _header = new PngHeader();

            Array.Reverse(data, 0, 4);
            Array.Reverse(data, 4, 4);

            _header.Width  = BitConverter.ToInt32(data, 0);
            _header.Height = BitConverter.ToInt32(data, 4);

            _header.BitDepth = data[8];
            _header.ColorType = data[9];
            _header.FilterMethod = data[11];
            _header.InterlaceMethod = data[12];
            _header.CompressionMethod = data[10];
        }
Example #5
0
        private void WriteHeaderChunk(PngHeader header)
        {
            byte[] chunkData = new byte[13];

            WriteInteger(chunkData, 0, header.Width);
            WriteInteger(chunkData, 4, header.Height);

            chunkData[8] = header.BitDepth;
            chunkData[9] = header.ColorType;
            chunkData[10] = header.CompressionMethod;
            chunkData[11] = header.FilterMethod;
            chunkData[12] = header.InterlaceMethod;

            WriteChunk(PngChunkTypes.Header, chunkData);
        }
Example #6
0
        /// <summary>
        /// Encodes the data of the specified image and writes the result to
        /// the specified stream.
        /// </summary>
        /// <param name="image">The image, where the data should be get from.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <param name="stream">The stream, where the image data should be written to.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="image"/> is null (Nothing in Visual Basic).</para>
        /// <para>- or -</para>
        /// <para><paramref name="stream"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public void Encode(ExtendedImage image, Stream stream)
        {
            Guard.NotNull(image, "image");
            Guard.NotNull(stream, "stream");

            _image = image;

            _stream = stream;

            // Write the png header.
            stream.Write(
                new byte[] 
                { 
                    0x89, 0x50, 0x4E, 0x47, 
                    0x0D, 0x0A, 0x1A, 0x0A 
                }, 0, 8);

            PngHeader header = new PngHeader();
            header.Width = image.PixelWidth;
            header.Height = image.PixelHeight;
            header.ColorType = 6;
            header.BitDepth = 8;
            header.FilterMethod = 0;
            header.CompressionMethod = 0;
            header.InterlaceMethod = 0;

            WriteHeaderChunk(header);

            WritePhysicsChunk();
            WriteGammaChunk();

            if (IsWritingUncompressed)
            {
                WriteDataChunksFast();
            }
            else
            {
                WriteDataChunks();
            }
            WriteEndChunk();

            stream.Flush();
        }