Esempio n. 1
0
        public Bitmap GetBitmap(int mipLevel = 0)
        {
            MipMap mip = MipMaps[mipLevel];
            int    x, y;

            Bitmap bmp = new Bitmap(mip.Width, mip.Height, PixelFormat.Format32bppArgb);

            byte[] data;
            byte[] dest;

            switch (Format)
            {
            case D3DFormat.PVRTC4:
                data = mip.Data;
                dest = new byte[mip.Width * mip.Height * 4];

                PVTRC.Decompress(data, false, mip.Width, mip.Height, true, dest);

                for (y = 0; y < mip.Height; y++)
                {
                    for (x = 0; x < mip.Width; x++)
                    {
                        int offset = (x + y * mip.Width) * 4;

                        Color c = Color.FromArgb(dest[offset + 3], dest[offset + 0], dest[offset + 1], dest[offset + 2]);
                        bmp.SetPixel(x, y, c);
                    }
                }
                break;

            case D3DFormat.R4G4B4A4:
                data = mip.Data;

                for (y = 0; y < mip.Height; y++)
                {
                    for (x = 0; x < mip.Width; x++)
                    {
                        int offset = (x + y * mip.Width) * 2;
                        int pixel  = BitConverter.ToUInt16(data, offset);
                        bmp.SetPixel(x, y, ColorHelper.R4G4B4A4ToColor(pixel));
                    }
                }
                break;

            case D3DFormat.R5G5B6:
                data = mip.Data;

                for (y = 0; y < mip.Height; y++)
                {
                    for (x = 0; x < mip.Width; x++)
                    {
                        int offset = (x + y * mip.Width) * 2;
                        int pixel  = BitConverter.ToUInt16(data, offset);
                        bmp.SetPixel(x, y, ColorHelper.R5G5B6ToColor(pixel));
                    }
                }
                break;

            case D3DFormat.A8B8G8R8:
                data = mip.Data;

                for (y = 0; y < mip.Height; y++)
                {
                    for (x = 0; x < mip.Width; x++)
                    {
                        int  offset = (x + y * mip.Width) * 4;
                        uint pixel  = BitConverter.ToUInt32(data, offset);
                        bmp.SetPixel(x, y, ColorHelper.A8B8G8R8ToColor(pixel));
                    }
                }
                break;

            case D3DFormat.R5G6B5:
                data = mip.Data;

                for (y = 0; y < mip.Height; y++)
                {
                    for (x = 0; x < mip.Width; x++)
                    {
                        int offset = (x + y * mip.Width) * 2;
                        int pixel  = BitConverter.ToUInt16(data, offset);
                        bmp.SetPixel(x, y, ColorHelper.R5G6B5ToColor(pixel));
                    }
                }
                break;

            case D3DFormat.A8R8G8B8:
                data = mip.Data;

                int pixelSize = Flags.HasFlag(TDXFlags.AlphaNbit) ? 4 : 3;

                for (y = 0; y < mip.Height; y++)
                {
                    for (x = 0; x < mip.Width; x++)
                    {
                        int offset = (x + y * mip.Width) * pixelSize;
                        int a      = pixelSize == 4 ? data[offset + 3] : 255;

                        Color c = Color.FromArgb(a, data[offset + 0], data[offset + 1], data[offset + 2]);
                        bmp.SetPixel(x, y, c);
                    }
                }
                break;

            case D3DFormat.DXT1:
                data = mip.Data;
                dest = new byte[mip.Width * mip.Height * 4];

                Squish.Squish.DecompressImage(dest, mip.Width, mip.Height, ref data, SquishFlags.kDxt1);

                for (y = 0; y < mip.Height; y++)
                {
                    for (x = 0; x < mip.Width; x++)
                    {
                        int offset = (x + y * mip.Width) * 4;

                        Color c = Color.FromArgb(dest[offset + 3], dest[offset + 0], dest[offset + 1], dest[offset + 2]);
                        bmp.SetPixel(x, y, c);
                    }
                }
                break;

            case D3DFormat.DXT5:
                data = mip.Data;
                dest = new byte[mip.Width * mip.Height * 4];

                Squish.Squish.DecompressImage(dest, mip.Width, mip.Height, ref data, SquishFlags.kDxt5);

                for (y = 0; y < mip.Height; y++)
                {
                    for (x = 0; x < mip.Width; x++)
                    {
                        int offset = (x + y * mip.Width) * 4;

                        Color c = Color.FromArgb(dest[offset + 3], dest[offset + 0], dest[offset + 1], dest[offset + 2]);
                        bmp.SetPixel(x, y, c);
                    }
                }
                break;

            case D3DFormat.ATI2:
                data = mip.Data;

                dest = BC5Unorm.Decompress(data, (uint)mip.Width, (uint)mip.Height);

                for (y = 0; y < mip.Height; y++)
                {
                    for (x = 0; x < mip.Width; x++)
                    {
                        int offset = (x + y * mip.Width) * 4;

                        Color c = Color.FromArgb(dest[offset + 3], dest[offset + 2], dest[offset + 1], dest[offset + 0]);
                        bmp.SetPixel(x, y, c);
                    }
                }
                break;
            }

            return(bmp);
        }
Esempio n. 2
0
        public static TDX LoadFromBitmap(Bitmap asset, string name, D3DFormat format)
        {
            TDX tdx = null;

            if (tdx == null)
            {
                tdx = new TDX();

                Bitmap      b     = asset;
                SquishFlags flags = SquishFlags.kDxt1;

                tdx.Name   = name;
                tdx.Format = format;

                switch (tdx.Format)
                {
                case D3DFormat.DXT1:
                    flags = SquishFlags.kDxt1;
                    break;

                case D3DFormat.DXT5:
                    flags = SquishFlags.kDxt5;
                    break;
                }

                List <Bitmap> mipBitmaps = GenerateMips(b, b.Width, b.Height);

                foreach (Bitmap mb in mipBitmaps)
                {
                    MipMap mip = new MipMap()
                    {
                        Width  = mb.Width,
                        Height = mb.Height
                    };

                    byte[] data = new byte[mb.Width * mb.Height * 4];
                    byte[] dest = new byte[Squish.Squish.GetStorageRequirements(mb.Width, mb.Height, flags | SquishFlags.kColourIterativeClusterFit | SquishFlags.kWeightColourByAlpha)];

                    int ii = 0;
                    for (int y = 0; y < mb.Height; y++)
                    {
                        for (int x = 0; x < mb.Width; x++)
                        {
                            Color p = mb.GetPixel(x, y);
                            data[ii + 0] = p.R;
                            data[ii + 1] = p.G;
                            data[ii + 2] = p.B;
                            data[ii + 3] = p.A;

                            ii += 4;
                        }
                    }

                    if (format == D3DFormat.ATI2)
                    {
                        dest = BC5Unorm.Compress(data, (ushort)mb.Width, (ushort)mb.Height, GetMipSize(format, (ushort)mb.Width, (ushort)mb.Height));
                    }
                    else
                    {
                        Squish.Squish.CompressImage(data, mb.Width, mb.Height, ref dest, flags | SquishFlags.kColourClusterFit);
                    }

                    mip.Data = dest;

                    tdx.MipMaps.Add(mip);
                }
            }

            return(tdx);
        }