public Pig(string filename)
        {
            data = File.ReadAllBytes(filename);
            int ofs2 = Bits.GetInt32(data, 0);
            int ofs  = 4;

            if (ofs2 < 65536)
            { // classic pig
                ofs2 = 0;
                ofs  = 0;
            }
            int lvlTexCount = Bits.GetInt32(data, ofs);

            ofs      += 4;
            lvlTexIdx = new int[lvlTexCount];
            for (int i = 0; i < lvlTexCount; i++)
            {
                lvlTexIdx[i] = Bits.GetUInt16(data, ofs);
                ofs         += 2;
            }
            ofs         = ofs2;
            bitmapCount = Bits.GetInt32(data, ofs);
            Debug.WriteLine("bitmapCount " + bitmapCount);
            soundCount      = Bits.GetInt32(data, ofs + 4);
            ofs            += 8;
            dataOfs         = ofs + bitmapCount * 17 + soundCount * 20;
            bitmaps         = new PigBitmap[bitmapCount];
            bitmapIdxByName = new Dictionary <string, int>();
            for (int i = 0; i < bitmapCount; i++)
            {
                PigBitmap bitmap = new PigBitmap(data, ofs);
                ofs       += 17;
                bitmaps[i] = bitmap;
                //if ((bitmap.frame & 31) == 0)
                //    bitmapIdxByName.Add(bitmap.name, i);
            }
            sounds         = new PigSound[soundCount];
            soundIdxByName = new Dictionary <string, int>();
            for (int i = 0; i < soundCount; i++)
            {
                PigSound sound = new PigSound(data, ofs);
                ofs      += 20;
                sounds[i] = sound;
            }
        }
        public byte[] GetBitmap(PigBitmap bmp)
        {
            int ofs = dataOfs + bmp.ofs;

            byte[] ret = new byte[bmp.width * bmp.height];
            //    if (bmp.name.Equals("exit01"))
            //        Debug.Assert(false);
            if ((bmp.flags & (PigFlag.RLE | PigFlag.RLEBig)) != 0)
            {
                int size   = Bits.GetInt32(data, ofs);
                int ofsEnd = ofs + size;
                ofs += 4;
                ofs += (bmp.flags & PigFlag.RLEBig) != 0 ? bmp.height * 2 : bmp.height;
                int retOfs = 0;
                while (ofs < ofsEnd)
                {
                    byte b = data[ofs++];
                    if ((b & 0xe0) == 0xe0)
                    {
                        int c = b & 0x1f;
                        if (c == 0)
                        {
                            continue;
                        }
                        b = data[ofs++];
                        for (int i = 0; i < c; i++)
                        {
                            ret[retOfs++] = b;
                        }
                    }
                    else
                    {
                        ret[retOfs++] = b;
                    }
                }
            }
            else
            {
                Array.Copy(data, ofs, ret, 0, ret.Length);
            }
            return(ret);
        }