Esempio n. 1
0
        /// <summary>
        /// Constructor which takes an existing MNGChunk object and
        /// verifies that its type matches that which is expected
        /// </summary>
        /// <param name="chunk">The MNGChunk to copy</param>
        /// <param name="expectedType">The input MNGChunk expected type</param>
        public MNGChunk(MNGChunk chunk, String expectedType)
        {
            // Copy the existing chunks members
            chunkLength = chunk.chunkLength;
            chunkType   = chunk.chunkType;
            chunkData   = chunk.chunkData;
            chunkCRC    = chunk.chunkCRC;

            // Verify the chunk type is as expected
            if (ChunkType != expectedType)
            {
                throw new ArgumentException(
                          String.Format("Specified chunk type is not {0} as expected", expectedType));
            }

            // Parse the chunk's data
            ParseData(chunkData);
        }
Esempio n. 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 void Load(string filename)
        {
            chunks = new List <MNGChunk>();
            pngs   = new List <PNG>();

            // 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();
                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();
                    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 "FRAM":
                    png = new PNG();
                    string FileName = System.Text.Encoding.GetEncoding("shift_jis").GetString(chunk.ChunkData, 1, GetStrlenInChunk(chunk.ChunkData));
                    System.Console.WriteLine(FileName);
                    if (FileName != null && FileName.Length > 0)
                    {
                        png.SetName(FileName);
                    }

                    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);
        }
Esempio n. 3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="chunk">The MNG chunk containing the data for this specific chunk</param>
 public IDATChunk(MNGChunk chunk)
     : base(chunk, NAME)
 {
 }
Esempio n. 4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="chunk">The MNG chunk containing the data for this specific chunk</param>
 public MHDRChunk(MNGChunk chunk)
     : base(chunk, NAME)
 {
 }
Esempio n. 5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="chunk">The MNG chunk containing the data for this specific chunk</param>
 public IENDChunk(MNGChunk chunk)
     : base(chunk, NAME)
 {
 }
Esempio n. 6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="chunk">The MNG chunk containing the data for this specific chunk</param>
 public PLTEChunk(MNGChunk chunk)
     : base(chunk, NAME)
 {
 }
Esempio n. 7
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="chunk">The MNG chunk containing the data for this specific chunk</param>
 public BACKChunk(MNGChunk chunk)
     : base(chunk, NAME)
 {
 }
Esempio n. 8
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="chunk">The MNG chunk containing the data for this specific chunk</param>
 public BKGDChunk(MNGChunk chunk)
     : base(chunk, NAME)
 {
 }
Esempio n. 9
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="chunk">The MNG chunk containing the data for this specific chunk</param>
 public TERMChunk(MNGChunk chunk)
     : base(chunk, NAME)
 {
 }