Ejemplo n.º 1
0
        private static AnimationChainListSave DeserializeManually(string fileName)
        {
            AnimationChainListSave toReturn = new AnimationChainListSave();

            System.Xml.Linq.XDocument xDocument = null;

            using (var stream = FileManager.GetStreamForFile(fileName))
            {
                xDocument = System.Xml.Linq.XDocument.Load(stream);
            }

            System.Xml.Linq.XElement foundElement = null;

            foreach (var element in xDocument.Elements())
            {
                if (element.Name.LocalName == "AnimationChainArraySave")
                {
                    foundElement = element;
                    break;
                }
            }

            LoadFromElement(toReturn, foundElement);

            return(toReturn);
        }
Ejemplo n.º 2
0
        private static byte[] LoadFile(string fileName)
        {
            // September 14, 2010:
            // I think we can just standardize the reading of the file by simply by using
            // the GetStreamForFile method instead of branching.

//#if !WINDOWS_PHONE
//            System.Diagnostics.Trace.Write(Path.Combine(Microsoft.Xna.Framework.Storage.StorageContainer.TitleLocation, fileName.Replace("/", "\\")));


//            FileStream file = File.OpenRead(Path.Combine(Microsoft.Xna.Framework.Storage.StorageContainer.TitleLocation, fileName.Replace("/", "\\")));
//            byte[] source = ReadFile(file);

//            return source;
//#else


            Stream stream = FileManager.GetStreamForFile(fileName);

            byte[] source = ReadFile(stream);
            FileManager.Close(stream);

            return(source);
//#endif
        }
Ejemplo n.º 3
0
        public static ImageDataList GetImageDataList(string gifFileName)
        {
            ImageDataList imageDatas = new ImageDataList();

#if XBOX360 || SILVERLIGHT
            if (gifFileName.StartsWith(@".\") || gifFileName.StartsWith(@"./"))
            {
                gifFileName = gifFileName.Substring(2);
            }
#endif

#if SILVERLIGHT || WINDOWS_8
            using (Stream stream = FileManager.GetStreamForFile(gifFileName))
#else
            using (FileStream stream = System.IO.File.OpenRead(gifFileName))
#endif

            {
                mGifInfo            = new GifInfo();
                mGifInfo.DelayTimes = new List <short>();

                BinaryReader reader = new BinaryReader(stream);

                #region header
                // Example:  GIF89a
                byte[] buffer = reader.ReadBytes(6);
                // set the header here
                char[] charArray = new char[buffer.Length];
                for (int i = 0; i < buffer.Length; i++)
                {
                    charArray[i] = (char)buffer[i];
                }

                mGifInfo.Header = new string(charArray);
                #endregion

                #region Width/Height

                mGifInfo.Width  = reader.ReadInt16();
                mGifInfo.Height = reader.ReadInt16();

                #endregion

                #region Packed information

                // The packed byte has the following info:
                // Bits 0-2     Size of the Global Color Table
                // Bit 3        Color Table Sort Flag
                // Bits 4-6     Color Resolution
                // Bit 7        Global Color Table Flag

                Byte packedByte = reader.ReadByte();

                int sizeOfGlobalColorTable = (7 & packedByte);

                mGifInfo.NumberOfColorEntries = 1 * (1 << (sizeOfGlobalColorTable + 1));

                mGifInfo.HasGlobalColorTable = (128 & packedByte) != 0;

                #endregion

                #region background color
                reader.ReadByte();
                #endregion

                #region default aspect ratio

                reader.ReadByte();

                #endregion

                TryReadGlobalColorTable(reader);


                byte separator = reader.ReadByte();

                while (separator != 0x3b) // Extension
                {
                    switch (separator)
                    {
                        #region Extensions

                    case 0x21:

                        Byte label = reader.ReadByte();

                        switch (label)
                        {
                        case 0xf9:
                            ReadGraphicsControlExtension(reader);
                            break;

                        case 0xff:
                            ReadApplicationExtensionBlock(reader);
                            break;

                        case 0xfe:
                            ReadCommonExtensionBlock(reader);
                            break;
                        }
                        break;
                        #endregion

                        #region Image Data
                    case 0x2c:

                        ReadImageDescriptor(reader);


                        Color[] color = new Color[mGifInfo.Width * mGifInfo.Height];

                        int transparentIndex = mGifInfo.TransparentIndex;

                        #region Interlaced
                        if (mGifInfo.IsInterlaced)
                        {
                            int i = 0;
                            for (int pass = 0; pass < 4; ++pass)
                            {
                                int row       = 0;
                                int increment = 0;

                                switch (pass)
                                {
                                case 0: row = 0; increment = 8; break;

                                case 1: row = 4; increment = 8; break;

                                case 2: row = 2; increment = 4; break;

                                case 3: row = 1; increment = 2; break;
                                }
                                for (; row < mGifInfo.Height; row += increment)
                                {
                                    for (int col = 0; col < mGifInfo.Width; ++col)
                                    {
                                        int position = (row * mGifInfo.Width) + col;

                                        byte index = mUncompressedColorIndexBuffer[i++];

                                        byte alpha = 255;

                                        if (mGifInfo.HasTransparency && index == transparentIndex)
                                        {
                                            alpha = 0;
                                        }

#if FRB_XNA || SILVERLIGHT || WINDOWS_PHONE
                                        color[position] = new Color(
                                            (byte)mGifInfo.PaletteInfo.Entries[index].R,
                                            (byte)mGifInfo.PaletteInfo.Entries[index].G,
                                            (byte)mGifInfo.PaletteInfo.Entries[index].B,
                                            (byte)alpha);
#elif FRB_MDX
                                        color[position] = Color.FromArgb(
                                            alpha,
                                            (byte)mGifInfo.PaletteInfo.Entries[index].R,
                                            (byte)mGifInfo.PaletteInfo.Entries[index].G,
                                            (byte)mGifInfo.PaletteInfo.Entries[index].B);
#endif
                                    }
                                }
                            }
                        }
                        #endregion
                        else
                        {
                            #region NonInterlaced

                            for (int i = 0; i < mGifInfo.PaletteInfo.Entries.Length; i++)
                            {
                                mGifInfo.PaletteInfo.Entries[transparentIndex].A = 255;
                            }
                            if (mGifInfo.HasTransparency)
                            {
                                mGifInfo.PaletteInfo.Entries[transparentIndex].A = 0;
                            }

                            int x = 0;
                            int y = 0;

                            int colorIndex;

                            for (int i = 0; i < mUncompressedColorIndexBuffer.Count; i++)
                            {
                                byte index = mUncompressedColorIndexBuffer[i];

                                // Let's see if we can avoid an if statement
                                x = mGifInfo.CurrentBlockLeft + i % mGifInfo.CurrentBlockWidth;
                                y = mGifInfo.CurrentBlockTop + i / mGifInfo.CurrentBlockWidth;

                                colorIndex = x + y * mGifInfo.Width;

#if FRB_XNA || SILVERLIGHT || WINDOWS_PHONE
                                color[colorIndex] = new Color(
                                    (byte)mGifInfo.PaletteInfo.Entries[index].R,
                                    (byte)mGifInfo.PaletteInfo.Entries[index].G,
                                    (byte)mGifInfo.PaletteInfo.Entries[index].B,
                                    (byte)mGifInfo.PaletteInfo.Entries[index].A);
#elif FRB_MDX
                                color[colorIndex] = Color.FromArgb(
                                    (byte)mGifInfo.PaletteInfo.Entries[index].A,
                                    (byte)mGifInfo.PaletteInfo.Entries[index].R,
                                    (byte)mGifInfo.PaletteInfo.Entries[index].G,
                                    (byte)mGifInfo.PaletteInfo.Entries[index].B);
#endif
                            }
                            #endregion
                        }
                        ImageData imageData = new ImageData(
                            mGifInfo.Width, mGifInfo.Height, color);

                        imageDatas.Add(imageData);

                        mUncompressedColorIndexBuffer.Clear();

                        break;
                        #endregion

                        #region End of file
                    case 0x3b:

                        // end of file
                        break;

                        #endregion
                    }

                    separator = reader.ReadByte();
                }
            }

            // Fill the imageDatas with the frame times
            foreach (short s in mGifInfo.DelayTimes)
            {
                imageDatas.FrameTimes.Add(s / 100.0);
            }

            return(imageDatas);
        }