Exemplo n.º 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);
        }
Exemplo n.º 2
0
        internal static byte[] GetPixelData(Bitmap img, int format, bool rectangle = true)
        {
            int w = img.Width;
            int h = img.Height;

            bool perfect = w == h && (w != 0) && ((w & (w - 1)) == 0);

            if (!perfect)               // Check if square power of two, else resize
            {
                // Square Format Checks
                if (rectangle && Math.Min(img.Width, img.Height) < 32)
                {
                    w = Bclim.Nlpo2(img.Width);
                    h = Bclim.Nlpo2(img.Height);
                }
                else
                {
                    w = h = Math.Max(Bclim.Nlpo2(w), Bclim.Nlpo2(h));                           // else resize
                }
            }

            using (MemoryStream mz = new MemoryStream())
                using (BinaryWriter bz = new BinaryWriter(mz))
                {
                    int p = Bclim.Gcm(w, 8) / 8;
                    if (p == 0)
                    {
                        p = 1;
                    }
                    for (uint i = 0; i < w * h; i++)
                    {
                        Bclim.D2Xy(i % 64, out uint x, out uint y);

                        // Get Shift Tile
                        uint tile = i / 64;

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

                        // Don't write data
                        Color c;
                        if (x >= img.Width || y >= img.Height)
                        {
                            c = Color.FromArgb(0, 0, 0, 0);
                        }
                        else
                        {
                            c = img.GetPixel((int)x, (int)y);
                            if (c.A == 0)
                            {
                                c = Color.FromArgb(0, 86, 86, 86);
                            }
                        }

                        switch (format)
                        {
                        case 0:
                            bz.Write(Bclim.GetL8(c));
                            break;                             // L8

                        case 1:
                            bz.Write(Bclim.GetA8(c));
                            break;                             // A8

                        case 2:
                            bz.Write(Bclim.GetLA4(c));
                            break;                             // LA4(4)

                        case 3:
                            bz.Write(Bclim.GetLA8(c));
                            break;                             // LA8(8)

                        case 4:
                            bz.Write(Bclim.GetHilo8(c));
                            break;                             // HILO8

                        case 5:
                            bz.Write(Bclim.GetRgb565(c));
                            break;                             // RGB565

                        case 6:
                        {
                            bz.Write(c.B);
                            bz.Write(c.G);
                            bz.Write(c.R);
                            break;
                        }

                        case 7:
                            bz.Write(Bclim.GetRgba5551(c));
                            break;                             // RGBA5551

                        case 8:
                            bz.Write(Bclim.GetRgba4444(c));
                            break;                             // RGBA4444

                        case 9:
                            bz.Write(Bclim.GetRgba8888(c));
                            break;                             // RGBA8

                        case 10: throw new Exception("ETC1 not supported.");

                        case 11: throw new Exception("ETC1A4 not supported.");

                        case 12:
                        {
                            byte val = (byte)(Bclim.GetL8(c) / 0x11);                                  // First Pix    // L4
                            {
                                c = img.GetPixel((int)x, (int)y);
                                if (c.A == 0)
                                {
                                    c = Color.FromArgb(0, 0, 0, 0);
                                }
                            }
                            val |= (byte)((Bclim.GetL8(c) / 0x11) << 4);
                            i++;
                            bz.Write(val);
                            break;
                        }

                        case 13:
                        {
                            byte val = (byte)(Bclim.GetA8(c) / 0x11);                                  // First Pix    // L4
                            {
                                c = img.GetPixel((int)x, (int)y);
                            }
                            val |= (byte)((Bclim.GetA8(c) / 0x11) << 4);
                            i++;
                            bz.Write(val);
                            break;
                        }
                        }
                    }
                    if (!perfect)
                    {
                        while (mz.Length < Bclim.Nlpo2((int)mz.Length))                      // pad
                        {
                            bz.Write((byte)0);
                        }
                    }
                    return(mz.ToArray());
                }
        }
Exemplo n.º 3
0
        // BCLIM Data Writing
        internal static int Write16BitColorPalette(Bitmap img, ref MemoryStream ms)
        {
            using (Stream pixelcolors = new MemoryStream())
                using (BinaryWriter bz = new BinaryWriter(pixelcolors))
                {
                    // Set up our basis.
                    bool    under16Colors = false;
                    int     colors        = Bclim.GetColorCount(img);
                    Color[] pcs           = new Color[colors];
                    if (colors < 16)
                    {
                        under16Colors = true;
                    }
                    uint div = 1;
                    if (under16Colors)
                    {
                        div = 2;
                    }

                    if (colors > 70)
                    {
                        throw new Exception("Too many colors");
                    }

                    // Set up a new reverse image to build into.
                    int w = Bclim.Gcm(img.Width, 8);
                    int h = Bclim.Gcm(img.Height, 8);
                    w = Math.Max(Bclim.Nlpo2(w), Bclim.Nlpo2(h));
                    h = w;
                    byte[] pixelarray = new byte[w * h];

                    const int colorformat = 2;
                    int       ctr         = 1;

                    pcs[0] = Color.FromArgb(0, 0xFF, 0xFF, 0xFF);

                    int p = Bclim.Gcm(w, 8) / 8;
                    if (p == 0)
                    {
                        p = 1;
                    }
                    int d = 0;
                    for (uint i = 0; i < pixelarray.Length; i++)
                    {
                        d = (int)(i / div);
                        // Get Tile Coordinate
                        Bclim.D2Xy(i % 64, out uint x, out uint y);

                        // Get Shift Tile
                        uint tile = i / 64;

                        // Shift Tile Coordinate into Tilemap
                        x += (uint)(tile % p) * 8;
                        y += (uint)(tile / p) * 8;
                        if (x >= img.Width || y >= img.Height)                   // Don't try to access any pixel data outside of our bounds.
                        {
                            i++;
                            continue;
                        }                 // Goto next tile.

                        // Get Color of Pixel
                        Color c = img.GetPixel((int)x, (int)y);

                        // Color Table Building Logic
                        int index = Array.IndexOf(pcs, c);
                        if (c.A == 0)
                        {
                            index = 0;
                        }
                        if (index < 0)                   // If new color
                        {
                            pcs[ctr] = c;
                            index    = ctr;
                            ctr++;
                        }                 // Add it to color list

                        // Add pixel to pixeldata
                        if (under16Colors)
                        {
                            index = index << 4;
                        }
                        pixelarray[i / div] = (byte)index;
                        if (!under16Colors)
                        {
                            continue;
                        }

                        c     = img.GetPixel((int)x + 1, (int)y);
                        index = Array.IndexOf(pcs, c);
                        if (c.A == 0)
                        {
                            index = 0;
                        }
                        if (index < 0)                   // If new color
                        {
                            pcs[ctr] = c;
                            index    = ctr;
                            ctr++;
                        }
                        pixelarray[i / div] |= (byte)index;
                        i++;
                    }

                    // Write Intro
                    bz.Write((ushort)colorformat);
                    bz.Write((ushort)ctr);
                    // Write Colors
                    for (int i = 0; i < ctr; i++)
                    {
                        bz.Write(Bclim.GetRgba5551(pcs[i]));                       // Write byte array.
                    }
                    // Write Pixel Data
                    for (uint i = 0; i < d; i++)
                    {
                        bz.Write(pixelarray[i]);
                    }
                    // Write Padding
                    while (pixelcolors.Length < Bclim.Nlpo2((int)pixelcolors.Length))
                    {
                        bz.Write((byte)0);
                    }
                    // Copy to main CLIM.
                    pixelcolors.Position = 0;
                    pixelcolors.CopyTo(ms);
                }
            return(7);
        }
Exemplo n.º 4
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);
        }