Exemple #1
0
        /// <summary>
        /// Attempts to load an MNG from the specified file name
        /// </summary>
        /// <param name="filename">Name of the MNG file to load</param>
        public bool Load(string filename)
        {
            chunks = new List<MNGChunk>();
            pngs = new List<PNG>();
            offsets = new List<Point>();

            // Open the file for reading
            Stream stream = File.OpenRead(filename);

            // Create a new header (should be 1 per file) and
            // read it from the stream
            MNGHeader header = new MNGHeader();
            try
            {
                header.Read(stream);
            }
            catch (Exception)
            {
                stream.Close();
                return false;
                //throw;
            }

            MNGChunk chunk;
            PNG png = null;
            PLTEChunk globalPLTE = null;

            // Read chunks from the stream until we reach the MEND chunk
            do
            {
                // Read a generic Chunk
                chunk = new MNGChunk();
                try
                {
                    chunk.Read(stream);
                }
                catch (Exception)
                {
                    stream.Close();
                    return false;
                    //throw;
                }

                // Take a look at the chunk type and decide what derived class to
                // use to create a specific chunk
                switch (chunk.ChunkType)
                {
                    case MHDRChunk.NAME:
                        if (headerChunk != null)
                            throw new ApplicationException(String.Format(
                                "Only one chunk of type {0} is allowed", chunk.ChunkType));
                        chunk = headerChunk = new MHDRChunk(chunk);

                        break;
                    case MENDChunk.NAME:
                        chunk = new MENDChunk(chunk);
                        break;
                    case TERMChunk.NAME:
                        chunk = new TERMChunk(chunk);
                        break;
                    case BACKChunk.NAME:
                        chunk = new BACKChunk(chunk);
                        break;
                    case BKGDChunk.NAME:
                        chunk = new BKGDChunk(chunk);
                        break;
                    case PLTEChunk.NAME:
                        chunk = new PLTEChunk(chunk);
                        // We can get an PLTE chunk w/o having gotten
                        // an IHDR
                        if (png != null)
                        {
                            png.PLTE = chunk as PLTEChunk;
                            if (png.PLTE.IsEmpty())
                            {
                                if (globalPLTE == null)
                                    throw new ApplicationException(String.Format(
                                        "An empty PLTE chunk was found inside a IHDR/IEND pair but no 'global' PLTE chunk was found"));
                                png.PLTE = globalPLTE;
                            }
                        }
                        else
                            globalPLTE = chunk as PLTEChunk;
                        break;
                    case DEFIChunk.NAME:
                        DEFIChunk dchunk = new DEFIChunk(chunk);
                        Point p = new Point();
                        p.X = (int)dchunk.XLocation;
                        p.Y = (int)dchunk.YLocation;
                        offsets.Add(p);
                        break;
                    case IHDRChunk.NAME:
                        chunk = new IHDRChunk(chunk);
                        // This is the beginning of a new embedded PNG
                        png = new PNG();
                        png.IHDR = chunk as IHDRChunk;
                        break;
                    case IDATChunk.NAME:
                        chunk = new IDATChunk(chunk);
                        // We shouldn't get an IDAT chunk if we haven't yet
                        // gotten an IHDR chunk
                        if (png == null)
                            throw new ArgumentNullException("png");
                        png.IDAT = chunk as IDATChunk;
                        break;
                    case IENDChunk.NAME:
                        chunk = new IENDChunk(chunk);
                        // We can get an IEND chunk w/o having gotten
                        // an IHDR
                        if (png != null)
                        {
                            // However, if we've gotten an IHDR chunk then
                            // this is the end of the embedded PNG
                            png.IEND = chunk as IENDChunk;
                            pngs.Add(png);
                            png = null;
                        }
                        break;
                    default:
                        break;
                }
                // Add the chunk to our list of chunks
                chunks.Add(chunk);
            }
            while (chunk.ChunkType != MENDChunk.NAME);
            return true;
        }
Exemple #2
0
        /// <summary>
        /// Attempts to load an MNG from the specified file name
        /// </summary>
        /// <param name="filename">Name of the MNG file to load</param>
        public bool Load(string filename)
        {
            chunks  = new List <MNGChunk>();
            pngs    = new List <PNG>();
            offsets = new List <Point>();

            // Open the file for reading
            Stream stream = File.OpenRead(filename);

            // Create a new header (should be 1 per file) and
            // read it from the stream
            MNGHeader header = new MNGHeader();

            try
            {
                header.Read(stream);
            }
            catch (Exception)
            {
                stream.Close();
                return(false);
                //throw;
            }

            MNGChunk  chunk;
            PNG       png        = null;
            PLTEChunk globalPLTE = null;

            // Read chunks from the stream until we reach the MEND chunk
            do
            {
                // Read a generic Chunk
                chunk = new MNGChunk();
                try
                {
                    chunk.Read(stream);
                }
                catch (Exception)
                {
                    stream.Close();
                    return(false);
                    //throw;
                }

                // Take a look at the chunk type and decide what derived class to
                // use to create a specific chunk
                switch (chunk.ChunkType)
                {
                case MHDRChunk.NAME:
                    if (headerChunk != null)
                    {
                        throw new ApplicationException(String.Format(
                                                           "Only one chunk of type {0} is allowed", chunk.ChunkType));
                    }
                    chunk = headerChunk = new MHDRChunk(chunk);

                    break;

                case MENDChunk.NAME:
                    chunk = new MENDChunk(chunk);
                    break;

                case TERMChunk.NAME:
                    chunk = new TERMChunk(chunk);
                    break;

                case BACKChunk.NAME:
                    chunk = new BACKChunk(chunk);
                    break;

                case BKGDChunk.NAME:
                    chunk = new BKGDChunk(chunk);
                    break;

                case PLTEChunk.NAME:
                    chunk = new PLTEChunk(chunk);
                    // We can get an PLTE chunk w/o having gotten
                    // an IHDR
                    if (png != null)
                    {
                        png.PLTE = chunk as PLTEChunk;
                        if (png.PLTE.IsEmpty())
                        {
                            if (globalPLTE == null)
                            {
                                throw new ApplicationException(String.Format(
                                                                   "An empty PLTE chunk was found inside a IHDR/IEND pair but no 'global' PLTE chunk was found"));
                            }
                            png.PLTE = globalPLTE;
                        }
                    }
                    else
                    {
                        globalPLTE = chunk as PLTEChunk;
                    }
                    break;

                case DEFIChunk.NAME:
                    DEFIChunk dchunk = new DEFIChunk(chunk);
                    Point     p      = new Point();
                    p.X = (int)dchunk.XLocation;
                    p.Y = (int)dchunk.YLocation;
                    offsets.Add(p);
                    break;

                case IHDRChunk.NAME:
                    chunk = new IHDRChunk(chunk);
                    // This is the beginning of a new embedded PNG
                    png      = new PNG();
                    png.IHDR = chunk as IHDRChunk;
                    break;

                case IDATChunk.NAME:
                    chunk = new IDATChunk(chunk);
                    // We shouldn't get an IDAT chunk if we haven't yet
                    // gotten an IHDR chunk
                    if (png == null)
                    {
                        throw new ArgumentNullException("png");
                    }
                    png.IDAT = chunk as IDATChunk;
                    break;

                case IENDChunk.NAME:
                    chunk = new IENDChunk(chunk);
                    // We can get an IEND chunk w/o having gotten
                    // an IHDR
                    if (png != null)
                    {
                        // However, if we've gotten an IHDR chunk then
                        // this is the end of the embedded PNG
                        png.IEND = chunk as IENDChunk;
                        pngs.Add(png);
                        png = null;
                    }
                    break;

                default:
                    break;
                }
                // Add the chunk to our list of chunks
                chunks.Add(chunk);
            }while (chunk.ChunkType != MENDChunk.NAME);
            return(true);
        }