Beispiel #1
0
        internal static Bitmap GetIMG_XY7(Clim bclim)
        {
            Bitmap img = new Bitmap(bclim.BaseSize, bclim.BaseSize);

            using (Stream bitmapStream = new MemoryStream(bclim.Data))
                using (BinaryReader br = new BinaryReader(bitmapStream))
                {
                    // Fetch Color stuff.
                    if (br.ReadUInt16() != 2)
                    {
                        return(null);
                    }
                    ushort  colors = br.ReadUInt16();
                    Color[] ca     = new Color[colors];
                    for (int i = 0; i < colors; i++)
                    {
                        ca[i] = Bclim.DecodeColor(br.ReadUInt16(), 7);
                    }

                    // Coordinates
                    // Colors
                    // Tiles Per Width
                    int p = Bclim.Gcm(img.Width, 8) / 8;
                    if (p == 0)
                    {
                        p = 1;
                    }

                    for (uint i = 0; i < bclim.BaseSize * bclim.BaseSize; i++)               // for every pixel
                    {
                        Bclim.D2Xy(i % 64, out uint x, out uint y);
                        uint tile = i / 64;

                        // Shift Tile Coordinate into Tilemap
                        x += (uint)(tile % p) * 8;
                        y += (uint)(tile / p) * 8;

                        byte val = br.ReadByte();
                        if (colors <= 0x10)                   // Handle 2 pixels at a time
                        {
                            img.SetPixel((int)x, (int)y, ca[val >> 4]);
                            x++;
                            i++;
                            val &= 0xF;
                            img.SetPixel((int)x, (int)y, ca[val]);
                        }
                        else                 //1bpp instead of .5, handle 2 pixels at a time the same way for no reason
                        {
                            img.SetPixel((int)x, (int)y, ca[val]);
                            x++;
                            i++;
                            val = br.ReadByte();
                            img.SetPixel((int)x, (int)y, ca[val]);
                        }
                    }
                }
            return(img);
        }
Beispiel #2
0
        // Bitmap Data Writing
        internal static Bitmap GetImg(int width, int height, byte[] bytes, int f)
        {
            Bitmap img  = new Bitmap(width, height);
            int    area = img.Width * img.Height;
            // Tiles Per Width
            int p = Bclim.Gcm(img.Width, 8) / 8;

            if (p == 0)
            {
                p = 1;
            }
            using (Stream bitmapStream = new MemoryStream(bytes))
                using (BinaryReader br = new BinaryReader(bitmapStream))
                    for (uint i = 0; i < area; i++)               // for every pixel
                    {
                        Bclim.D2Xy(i % 64, out uint x, out uint y);
                        uint tile = i / 64;

                        // Shift Tile Coordinate into Tilemap
                        x += (uint)(tile % p) * 8;
                        y += (uint)(tile / p) * 8;

                        // Get Color
                        Color c;
                        switch (f)
                        {
                        case 0x0:                         // L8        // 8bit/1 byte
                        case 0x1:                         // A8
                        case 0x2:                         // LA4
                            c = Bclim.DecodeColor(br.ReadByte(), f);
                            break;

                        case 0x3:                         // LA8       // 16bit/2 byte
                        case 0x4:                         // HILO8
                        case 0x5:                         // RGB565
                        case 0x8:                         // RGBA4444
                        case 0x7:                         // RGBA5551
                            c = Bclim.DecodeColor(br.ReadUInt16(), f);
                            break;

                        case 0x6:                         // RGB8:     // 24bit
                            byte[] data = br.ReadBytes(3);
                            Array.Resize(ref data, 4);
                            c = Bclim.DecodeColor(BitConverter.ToUInt32(data, 0), f);
                            break;

                        case 0x9:                         // RGBA8888
                            c = Bclim.DecodeColor(br.ReadUInt32(), f);
                            break;

                        case 0xC:                                                          // L4
                        case 0xD:                                                          // A4        // 4bit - Do 2 pixels at a time.
                            uint val = br.ReadByte();
                            img.SetPixel((int)x, (int)y, Bclim.DecodeColor(val & 0xF, f)); // lowest bits for the low pixel
                            i++;
                            x++;
                            c = Bclim.DecodeColor(val >> 4, f);                               // highest bits for the high pixel
                            break;

                        default: throw new Exception("Invalid FileFormat.");
                        }
                        img.SetPixel((int)x, (int)y, c);
                    }
            return(img);
        }