示例#1
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());
                }
        }