Exemplo n.º 1
0
        public static Bitmap DecodeImage(byte[] data, int width, int height, Tex_Formats type)
        {
            if (type == Tex_Formats.ETC1)
            {
                return(Pixel.decodeETC(data, width, height));
            }
            if (type == Tex_Formats.ETC1a4)
            {
                return(Pixel.decodeAlpha(data, width, height));
            }

            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            int[] pixels = new int[width * height];

            int p = 0;

            for (int h = 0; h < height; h += 8)
            {
                for (int w = 0; w < width; w += 8)
                {
                    // 8x8 block
                    int[] colors = new int[64];
                    for (int i = 0; i < 64; i++)
                    {
                        switch (type)
                        {
                        case Tex_Formats.RGBA8: colors[i] = decode8888(data[p++], data[p++], data[p++], data[p++]); break;

                        case Tex_Formats.RGB8: colors[i] = decode888(data[p++], data[p++], data[p++]); break;

                        case Tex_Formats.RGBA5551: colors[i] = decode5551(data[p++], data[p++]); break;

                        case Tex_Formats.RGB565: colors[i] = decode565(data[p++], data[p++]); break;

                        case Tex_Formats.RGBA4444: colors[i] = decode4444(data[p++], data[p++]); break;

                        case Tex_Formats.LA8: colors[i] = decodeLA8(data[p++], data[p++]); break;

                        case Tex_Formats.HILO8: colors[i] = decodeHILO8(data[p++], data[p++]); break;

                        case Tex_Formats.L8: colors[i] = decodeL8(data[p++]); break;

                        case Tex_Formats.A8: colors[i] = decodeA8(data[p++]); break;

                        case Tex_Formats.LA4: colors[i] = decodeLA4(data[p++]); break;

                        case Tex_Formats.L4:
                        {
                            colors[i++] = decodeL8((data[p] & 0xF) | ((data[p] & 0xF) << 4));
                            colors[i]   = decodeL8((data[p] & 0xF0) | ((data[p] & 0xF0) >> 4));
                            p++;
                            break;
                        }

                        case Tex_Formats.A4:
                        {
                            colors[i++] = decodeA8((data[p] & 0xF) | ((data[p] & 0xF) << 4));
                            colors[i]   = decodeA8((data[p] & 0xF0) | ((data[p] & 0xF0) >> 4));
                            p++;
                            break;
                        }

                        default: throw new Exception("Unsuppored format " + type.ToString("x"));
                        }
                    }

                    for (int bh = 0; bh < 8; bh++)
                    {
                        for (int bw = 0; bw < 8; bw++)
                        {
                            pixels[((w + bw) + (h + bh) * width)] = colors[calcZOrder(bw, bh)];
                        }
                    }
                }
            }

            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);

            Marshal.Copy(pixels, 0, bmpData.Scan0, pixels.Length);
            bmp.UnlockBits(bmpData);

            return(bmp);
        }
Exemplo n.º 2
0
        public static byte[] EncodeImage(Bitmap img, Tex_Formats type)
        {
            if (type == Tex_Formats.ETC1)
            {
                return(RG_ETC1.encodeETC(img));
            }
            if (type == Tex_Formats.ETC1a4)
            {
                return(RG_ETC1.encodeETCa4(img));
            }

            FileOutput o = new FileOutput();

            BitmapData bmpData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, img.PixelFormat);

            int[] pixels = new int[img.Width * img.Height];
            Marshal.Copy(bmpData.Scan0, pixels, 0, pixels.Length);
            img.UnlockBits(bmpData);

            for (int h = 0; h < img.Height; h += 8)
            {
                for (int w = 0; w < img.Width; w += 8)
                {
                    // 8x8 block
                    List <byte[]> colors = new List <byte[]>();
                    for (int bh = 0; bh < 8; bh++)
                    {
                        for (int bw = 0; bw < 8; bw++)
                        {
                            switch (type)
                            {
                            case Tex_Formats.RGBA8: colors.Add(encode8888(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.RGB8: colors.Add(encode8(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.RGBA4444: colors.Add(encode4444(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.RGBA5551: colors.Add(encode5551(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.RGB565: colors.Add(encode565(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.LA8: colors.Add(encodeLA8(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.HILO8: colors.Add(encodeHILO8(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.L8: colors.Add(encodeL8(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.A8: colors.Add(encodeA8(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.LA4: colors.Add(encodeLA4(pixels[(w + bw) + (h + bh) * img.Width])); break;

                            case Tex_Formats.L4:
                            {
                                colors.Add(new byte[] { (byte)((encodeL8(pixels[(w + bw) + (h + bh) * img.Width])[0] / 0x11) & 0xF | ((encodeL8(pixels[(w + bw) + (h + bh) * img.Width + 1])[0] / 0x11) << 4)) });
                                bw++;
                                break;
                            }

                            case Tex_Formats.A4:
                            {
                                colors.Add(new byte[] { (byte)((encodeA8(pixels[(w + bw) + (h + bh) * img.Width])[0] / 0x11) & 0xF | ((encodeA8(pixels[(w + bw) + (h + bh) * img.Width + 1])[0] / 0x11) << 4)) });
                                bw++;
                                break;
                            }
                            }
                        }
                    }

                    for (int bh = 0; bh < 8; bh++)
                    {
                        for (int bw = 0; bw < 8; bw++)
                        {
                            int pos = bw + bh * 8;
                            for (int i = 0; i < zorder.Length; i++)
                            {
                                if (zorder[i] == pos)
                                {
                                    if (type == Tex_Formats.L4 || type == Tex_Formats.A4)
                                    {
                                        i /= 2; bw++;
                                    }
                                    o.WriteBytes(colors[i]);
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(o.GetBytes());
        }