Engines/FlatRedBallXNA/FlatRedBall/IO/PngLoader.cs
/// <summary>
/// Takes a List of all of the Chunks in the .png image being loaded (after IHDR is loaded and removed) 
/// and processes each Chunk appropriately before returning the compressed image. NOTE: Currently only 
/// processes the required Chunks PLTE, IDAT and IEND. Ancillary chunks are currently unsupported.
/// </summary>
/// <param name="chunks">The List of chunks loaded from the .png image file</param>
/// <param name="header">The HeaderInfo loaded from the IHDR chunk</param>
/// <param name="palette">An unused PaletteInfo struct to load the PLTE chunk data into</param>
/// <returns>A byte[] containing the constructed and decompressed image</returns>
Takes a List of all of the Chunks in the .png image being loaded (after IHDR is loaded and removed)
and processes each Chunk appropriately before returning the compressed image. NOTE: Currently only
processes the required Chunks PLTE, IDAT and IEND. Ancillary chunks are currently unsupported.
Takes a List of all of the Chunks in the . png image being loaded ( after IHDR is loaded and removed ) and processes each Chunk appropriately before returning the compressed image . NOTE : Currently only processes the required Chunks PLTE IDAT and IEND . Ancillary chunks are currently unsupported .
private static byte[] ProcessChunks(List<Chunk> chunks, HeaderInfo header, ref PaletteInfo palette)
        {
            int numberOfBytesRead = 0;
            foreach (Chunk currentChunk in chunks)
            {
                switch (currentChunk.Type)
                {
                    case ChunkTypes.IHDR:
                        {
                            throw new Exception(String.Format("List<Chunk> chunks still contains IHDR chunk info."));
                        }
                    case ChunkTypes.PLTE:
                        {   
                            if ((header.ColorType == 0) || (header.ColorType == 4))
                                throw new Exception(String.Format(
                                    "The given ColorType, {0}, does not support the use of a Palette.", header.ColorType));
                            palette.Entries = new RGB[(1 << (header.BitDepth + 1)) - 1];
                            ReadPalette(currentChunk, ref palette);
                            break;
                        }
                    case ChunkTypes.IDAT:
                        {
                            ReadImageData(currentChunk, ref sReadImageDataByteBuffer, ref numberOfBytesRead);
                            break;
                        }
                    case ChunkTypes.IEND:
                        {
                            if (chunks.IndexOf(currentChunk) != chunks.Count - 1)
                                throw new Exception(String.Format(
                                    "IEND chunk does not appear to be the last chunk in the image."));
                            MemoryStream streamForInflater = new MemoryStream(sReadImageDataByteBuffer, 0, numberOfBytesRead);
                            InflaterInputStream decompressionStream = new InflaterInputStream(streamForInflater);
                            ReadStream(ref decompressionStream);
                            break;
                        }
                    case ChunkTypes.tRNS:
                        {
                            mMaxTransIndex = (int)currentChunk.Length - 1;
                            for (int i = 0; i < currentChunk.Length; ++i)
                            {
                                mTransparentEntries[i] = (byte)currentChunk.ChunkData.ReadByte();
                            }
                            break;
                        }
                    case ChunkTypes.UNSUPPORTED:
                        {
                            break;
                        }
                }
            }
            return sByteBuffer;
        }
private static byte [ ] ProcessChunks ( List < Chunk > chunks , HeaderInfo header , ref PaletteInfo palette ) { int numberOfBytesRead = 0 ; foreach ( Chunk currentChunk in chunks ) { switch ( currentChunk . Type ) { case ChunkTypes . IHDR : { throw new Exception ( String . Format ( " " ) ) ; } case ChunkTypes . PLTE : { if ( ( header . ColorType == 0 ) || ( header . ColorType == 4 ) ) throw new Exception ( String . Format ( " " , header . ColorType ) ) ; palette . Entries = new RGB [ ( 1 << ( header . BitDepth + 1 ) ) - 1 ] ; ReadPalette ( currentChunk , ref palette ) ; break ; } case ChunkTypes . IDAT : { ReadImageData ( currentChunk , ref sReadImageDataByteBuffer , ref numberOfBytesRead ) ; break ; } case ChunkTypes . IEND : { if ( chunks . IndexOf ( currentChunk ) != chunks . Count - 1 ) throw new Exception ( String . Format ( " " ) ) ; MemoryStream streamForInflater = new MemoryStream ( sReadImageDataByteBuffer , 0 , numberOfBytesRead ) ; InflaterInputStream decompressionStream = new InflaterInputStream ( streamForInflater ) ; ReadStream ( ref decompressionStream ) ; break ; } case ChunkTypes . tRNS : { mMaxTransIndex = ( int ) currentChunk . Length - 1 ; for ( int i = 0 ; i < currentChunk . Length ; ++ i ) { mTransparentEntries [ i ] = ( byte ) currentChunk . ChunkData . ReadByte ( ) ; } break ; } case ChunkTypes . UNSUPPORTED : { break ; } } } return sByteBuffer ; }
Takes a List of all of the Chunks in the .png image being loaded (after IHDR is loaded and removed)
and processes each Chunk appropriately before returning the compressed image.
Takes a List of all of the Chunks in the . png image being loaded ( after IHDR is loaded and removed ) and processes each Chunk appropriately before returning the compressed image .
//Only one IHDR can exist, and it should have already been removed //break; //Check if a palette can even be used for this image's ColorType //if so, prepare the palette to hold the maximum number of colors for the given bitDepth //Read the PLTE chunk into the given PaletteInfo //Read the IDAT chunk's data into the List for imageData //This should be the last chunk to be loaded, so it's safe now to decompress the image //and do any extra processing caused by ancillary chunks. //Check if there are any chunks after IEND //Load the compressed image into an InflaterInputStream to inflate(decompress) the image //Read the decompressed image through the stream and into a byte[] //Represents an ancillary chunk that isn't yet implemented or an unknown chunk type //Return the image
A byte[] containing the constructed and decompressed image
A byte [] containing the constructed and decompressed image
chunks
List<Chunk>
The List of chunks loaded from the .png image file
The List of chunks loaded from the . png image file
header
HeaderInfo
The HeaderInfo loaded from the IHDR chunk
The HeaderInfo loaded from the IHDR chunk
PaletteInfo
ref
palette
An unused PaletteInfo struct to load the PLTE chunk data into
An unused PaletteInfo struct to load the PLTE chunk data into