Example #1
0
        public Sprite256(String filename)
        {
            try
            {
                using (FileStream fs = File.Open(filename, FileMode.Open))
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        fs.Seek(-4, SeekOrigin.End);
                        uint spriteCount = br.ReadUInt32() & 0x7FFFFFFF;

                        fs.Seek(0, SeekOrigin.Begin);
                        // read in the palette. 256 entries.
                        uint[] spritePalette = new uint[256];
                        for (int i = 0; i < 256; i++)
                        {
                            spritePalette[i] = br.ReadUInt32();
                        }
                        bOwnPalette = new Palette(spritePalette);

                        // read in the frames
                        for (int i = 0; i < spriteCount; i++)
                        {
                            int  fWidth    = br.ReadInt32();
                            int  fHeight   = br.ReadInt32();
                            int  fDataSize = br.ReadInt32();
                            long posAfter  = fs.Position + fDataSize;

                            unsafe
                            {
                                ushort *px = (ushort *)Marshal.AllocHGlobal(fWidth * fHeight * 2);
                                for (int k = 0; k < fWidth * fHeight; k++)
                                {
                                    px[k] = 0;
                                }
                                ushort *px1 = px;

                                while (fDataSize > 0)
                                {
                                    // read in the data
                                    ushort ipx = br.ReadByte();
                                    ipx       |= (ushort)(ipx << 8);
                                    ipx       &= 0xC03F;
                                    fDataSize -= 1;

                                    if ((ipx & 0xC000) > 0)
                                    {
                                        if ((ipx & 0xC000) == 0x4000)
                                        {
                                            px1 += fWidth * (ipx & 0x3F);
                                        }
                                        else
                                        {
                                            px1 += (ipx & 0x3F);
                                        }
                                    }
                                    else
                                    {
                                        for (int j = 0; j < (ipx & 0x3F); j++)
                                        {
                                            byte ss    = br.ReadByte();
                                            *    px1++ = (ushort)((ushort)0xFF00 | ss);
                                        }

                                        fDataSize -= (ipx & 0x3F);
                                    }
                                }

                                SpriteFrame sf = new SpriteFrame();
                                sf.Width  = fWidth;
                                sf.Height = fHeight;
                                sf.Pixels = px;
                                Frames.Add(sf);
                            }

                            fs.Seek(posAfter, SeekOrigin.Begin);
                        }
                    }
            }
            catch (IOException)
            {
                throw;
                //throw new FormatException("Failed to open the image.");
            }
        }
Example #2
0
        public Sprite16(String filename)
        {
            try
            {
                using (FileStream fs = File.Open(filename, FileMode.Open))
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        fs.Seek(-4, SeekOrigin.End);
                        uint spriteCount = br.ReadUInt32() & 0x7FFFFFFF;

                        fs.Seek(0, SeekOrigin.Begin);

                        // read in the frames
                        for (int i = 0; i < spriteCount; i++)
                        {
                            int  fWidth    = br.ReadInt32();
                            int  fHeight   = br.ReadInt32();
                            int  fDataSize = br.ReadInt32();
                            long posAfter  = fs.Position + fDataSize;

                            unsafe
                            {
                                ushort *px = (ushort *)Marshal.AllocHGlobal(fWidth * fHeight * 2);
                                for (int k = 0; k < fWidth * fHeight; k++)
                                {
                                    px[k] = 0;
                                }
                                ushort *px1 = px;

                                while (fDataSize > 0)
                                {
                                    // read in the data
                                    ushort ipx = br.ReadByte();
                                    ipx       |= (ushort)(ipx << 8);
                                    ipx       &= 0xC03F;
                                    fDataSize -= 1;

                                    if ((ipx & 0xC000) > 0)
                                    {
                                        if ((ipx & 0xC000) == 0x4000)
                                        {
                                            px1 += fWidth * (ipx & 0x3F);
                                        }
                                        else
                                        {
                                            px1 += (ipx & 0x3F);
                                        }
                                    }
                                    else
                                    {
                                        byte[] bytes = new byte[(ipx & 0x3F)];
                                        br.Read(bytes, 0, bytes.Length);
                                        for (int j = 0; j < bytes.Length; j++)
                                        {
                                            int alpha2;
                                            alpha2 = (bytes[j] & 0x0F); alpha2 |= alpha2 << 4;
                                            *px1++ = (ushort)((ushort)((alpha2 & 0xFF) << 8) | 0xFF);

                                            if ((j != bytes.Length - 1) || ((bytes[bytes.Length - 1] & 0xF0) > 0))
                                            {
                                                alpha2 = (bytes[j] & 0xF0); alpha2 |= alpha2 >> 4;
                                                *px1++ = (ushort)((ushort)((alpha2 & 0xFF) << 8) | 0xFF);
                                            }
                                        }

                                        fDataSize -= (ipx & 0x3F);
                                    }
                                }

                                SpriteFrame sf = new SpriteFrame();
                                sf.Width  = fWidth;
                                sf.Height = fHeight;
                                sf.Pixels = px;
                                Frames.Add(sf);
                            }

                            fs.Seek(posAfter, SeekOrigin.Begin);
                        }
                    }
            }
            catch (IOException)
            {
                throw;
                //throw new FormatException("Failed to open the image.");
            }
        }