Esempio n. 1
0
        public bool Save(string filename, ref Bitmap bmp)
        {
            FileStreamEx f = new FileStreamEx(filename, FileMode.Create, FileAccess.Write);

            head.Write(f);
            SaveRawData(ref bmp, f);
            if (FlagInternalPalette)
            {
                int paletteCount = 0;
                switch (Mode)
                {
                case 1:
                    paletteCount = 2;
                    break;

                case 2:
                    paletteCount = 16;
                    break;

                case 3:
                    paletteCount = 256;
                    break;
                }
                paletteCount = Math.Max(paletteCount, bmp.Palette.Entries.Length);
                for (int i = 0; i < paletteCount; i++)
                {
                    f.WriteByte(bmp.Palette.Entries[i].R);
                    f.WriteByte(bmp.Palette.Entries[i].G);
                    f.WriteByte(bmp.Palette.Entries[i].B);
                    f.WriteByte(bmp.Palette.Entries[i].A);
                }
            }
            f.Close();
            return(true);
        }
Esempio n. 2
0
 public void Write(FileStreamEx f)
 {
     f.WriteByte(headersize);
     f.WriteByte(dstindex);
     f.WriteByte(width);
     f.WriteByte(height);
     f.WriteByte(resolution);
     f.WriteByte(texturecount);
     f.WriteByte(mode);
     f.WriteByte(flags);
 }
Esempio n. 3
0
 public void Read(FileStreamEx f)
 {
     headersize   = f.Read8();
     dstindex     = f.Read8();
     width        = f.Read8();
     height       = f.Read8();
     resolution   = f.Read8();
     texturecount = f.Read8();
     mode         = f.Read8();
     flags        = f.Read8();
 }
Esempio n. 4
0
        public void SaveRawData(ref Bitmap bmp, FileStreamEx f)
        {
            Rectangle  lockRect = new Rectangle(0, 0, bmp.Width & ~3, bmp.Height & ~3);
            BitmapData bmpdata  = bmp.LockBits(lockRect, ImageLockMode.ReadOnly, bmp.PixelFormat);
            int        size     = bmpdata.Stride * bmpdata.Height;

            byte[] data = new byte[size];
            System.Runtime.InteropServices.Marshal.Copy(bmpdata.Scan0, data, 0, size);
            if (Mode == 2)
            {
                for (int i = 0; i < size; i++)
                {
                    data[i] = (byte)(((data[i] & 15) << 4) | ((data[i] >> 4) & 0xF));
                }
            }
            f.Write(data, 0, size);
            bmp.UnlockBits(bmpdata);
        }
Esempio n. 5
0
        public bool Open(string filename, ref Bitmap bmp)
        {
            try
            {
                FileStreamEx f = new FileStreamEx(filename, FileMode.Open, FileAccess.Read);
                head.Read(f);

                bmp = new Bitmap(Size.width * Resolution, Size.height * Resolution * TextureCount, PixelMode);
                if (bmp.Palette.Entries.Length == 0x100)
                {
                    ColorPalette pal = bmp.Palette;
                    for (int i = 0; i < 0x10; i++)
                    {
                        for (int j = 0; j < 0x10; j++)
                        {
                            int r = 0, g = 0, b = 0;
                            switch (i)
                            {
                            case 0:
                                r = 16;
                                g = 16;
                                b = 16;
                                break;

                            case 1:
                                r = 16;
                                break;

                            case 2:
                                g = 16;
                                break;

                            case 3:
                                b = 16;
                                break;

                            case 4:
                                r = 16;
                                g = 16;
                                break;

                            case 5:
                                r = 16;
                                b = 16;
                                break;

                            case 6:
                                g = 16;
                                b = 16;
                                break;

                            case 7:
                                r = 8;
                                g = 8;
                                b = 8;
                                break;

                            case 8:
                                r = 8;
                                break;

                            case 9:
                                g = 8;
                                break;

                            case 10:
                                b = 8;
                                break;

                            case 11:
                                r = 8;
                                g = 8;
                                break;

                            case 12:
                                r = 8;
                                b = 8;
                                break;

                            case 13:
                                g = 8;
                                b = 8;
                                break;

                            case 14:
                                r = 7;
                                g = 3;
                                break;

                            case 15:
                                r = 3;
                                b = 7;
                                break;
                            }
                            r = (r * j) % 0x100;
                            if (r == 0)
                            {
                                r++;
                            }
                            g = (g * j) % 0x100;
                            if (g == 0)
                            {
                                g++;
                            }
                            b = (b * j) % 0x100;
                            if (b == 0)
                            {
                                b++;
                            }
                            pal.Entries[j + i * 0x10] = Color.FromArgb(r, g, b);
                        }
                    }
                    bmp.Palette = pal;
                }

                Rectangle  lockRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                BitmapData bmpdata  = bmp.LockBits(lockRect, ImageLockMode.WriteOnly, bmp.PixelFormat);
                int        size     = bmpdata.Width * bmpdata.Height;
                byte[]     data;
                switch (Mode)
                {
                case 1: size /= 8; break;

                case 2: size /= 2; break;

                case 3: size *= 1; break;

                case 4: size *= 2; break;

                case 5: size *= 3; break;

                case 6: size *= 4; break;
                }
                data = new byte[size];
                f.Read(data, 0, size);
                if (Mode == 2)
                {
                    for (int i = 0; i < size; i++)
                    {
                        data[i] = (byte)(((data[i] & 15) << 4) | ((data[i] >> 4) & 0xF));
                    }
                }
                System.Runtime.InteropServices.Marshal.Copy(data, 0, bmpdata.Scan0, size);
                bmp.UnlockBits(bmpdata);
                f.Close();
            }
            catch (System.Exception)
            {
                return(false);
            }
            return(true);
        }