Ejemplo n.º 1
0
        private static byte[] PixelBytesFromImage(Bitmap bmp, PsbPixelFormat pixelFormat = PsbPixelFormat.None)
        {
            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                                              ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            int    stride    = bmpData.Stride;      // 扫描线的宽度
            int    offset    = stride - bmp.Width;  // 显示宽度与扫描线宽度的间隙
            IntPtr iPtr      = bmpData.Scan0;       // 获取bmpData的内存起始位置
            int    scanBytes = stride * bmp.Height; // 用stride宽度,表示这是内存区域的大小

            var result = new byte[scanBytes];

            System.Runtime.InteropServices.Marshal.Copy(iPtr, result, 0, scanBytes);
            bmp.UnlockBits(bmpData); // 解锁内存区域

            switch (pixelFormat)
            {
            case PsbPixelFormat.WinRGBA4444:
                result = Rgba428(result, false);
                break;

            case PsbPixelFormat.CommonRGBA4444:
                Bgra2Abgr(ref result, true);
                result = Rgba428(result, false);
                break;

            case PsbPixelFormat.CommonRGBA8:
                Abgr2Argb(ref result);
                break;

            case PsbPixelFormat.A8L8:
                result = Rgba2A8L8(result);
                break;

            case PsbPixelFormat.DXT5:
                //Abgr2Argb(ref result);
                result = DxtUtil.Dxt5Encode(result, bmp.Width, bmp.Height);
                break;

            case PsbPixelFormat.RGBA8_SW:
                result = PostProcessing.TileTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                break;

            case PsbPixelFormat.L8:
                result = Rgba2L8(result);
                break;

            case PsbPixelFormat.A8_SW:
                result = PostProcessing.TileTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                result = Rgba2A8(result);
                break;
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static Bitmap ConvertToImageWithPalette(byte[] data, byte[] palette, int height, int width,
                                                       PsbPixelFormat colorFormat = PsbPixelFormat.None, PsbPixelFormat paletteColorFormat = PsbPixelFormat.None)
        {
            Bitmap     bmp;
            BitmapData bmpData;

            switch (paletteColorFormat)
            {
            case PsbPixelFormat.CommonRGBA8:
                Switch_0_2(ref palette);
                break;
            }

            switch (colorFormat)
            {
            case PsbPixelFormat.CI8_SW:
                bmp     = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
                bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
                                       ImageLockMode.WriteOnly, bmp.PixelFormat);
                ColorPalette pal = bmp.Palette;
                for (int i = 0; i < 256; i++)
                {
                    pal.Entries[i] = Color.FromArgb(BitConverter.ToInt32(palette, i * 4));
                }
                // Assign the edited palette to the bitmap.
                bmp.Palette = pal;

                data = PostProcessing.UnswizzleTexture(data, bmp.Width, bmp.Height, bmp.PixelFormat);
                //Switch_0_2(ref data);
                break;

            default:
                return(ConvertToImage(data, height, width, colorFormat));
            }

            int    stride    = bmpData.Stride;  // 扫描线的宽度
            int    offset    = stride - width;  // 显示宽度与扫描线宽度的间隙
            IntPtr iptr      = bmpData.Scan0;   // 获取bmpData的内存起始位置
            int    scanBytes = stride * height; // 用stride宽度,表示这是内存区域的大小

            if (scanBytes >= data.Length)
            {
                System.Runtime.InteropServices.Marshal.Copy(data, 0, iptr, data.Length);
                bmp.UnlockBits(bmpData); // 解锁内存区域
                return(bmp);
            }

            throw new BadImageFormatException("data may not corresponding");
        }
Ejemplo n.º 3
0
        public static Bitmap ConvertToImage(byte[] data, int height, int width,
                                            PsbPixelFormat colorFormat = PsbPixelFormat.None)
        {
            var bmp     = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
                                       ImageLockMode.WriteOnly, bmp.PixelFormat);

            switch (colorFormat)
            {
            case PsbPixelFormat.BeRGBA4444:
                data = Argb428(data);
                Argb2Rgba(ref data);
                break;

            case PsbPixelFormat.LeRGBA4444:
                data = Argb428(data);
                break;

            case PsbPixelFormat.BeRGBA8:
                Switch_0_2(ref data);
                break;

            case PsbPixelFormat.A8L8:
                data = ReadA8L8(data, width, height);
                break;

            case PsbPixelFormat.A8L8_SW:
                data = ReadA8L8(data, width, height);
                data = PostProcessing.UnswizzleTexture(data, width, height, PixelFormat.Format32bppArgb);
                break;

            case PsbPixelFormat.DXT5:
                data = DxtUtil.DecompressDxt5(data, width, height);
                Switch_0_2(ref data);     //DXT5(for win) need conversion
                break;

            case PsbPixelFormat.RGBA8_SW:
                data = PostProcessing.UnswizzleTexture(data, bmp.Width, bmp.Height, bmp.PixelFormat);
                Switch_0_2(ref data);
                break;

            case PsbPixelFormat.LeRGBA4444_SW:
                data = Argb428(data);
                //Rgba2Argb(ref data);
                data = PostProcessing.UnswizzleTexture(data, bmp.Width, bmp.Height, bmp.PixelFormat);
                break;

            case PsbPixelFormat.TileRGBA8_SW:
                data = PostProcessing.UntileTexture(data, bmp.Width, bmp.Height, bmp.PixelFormat);
                break;

            case PsbPixelFormat.A8:
                data = ReadA8(data, height, width);
                break;

            case PsbPixelFormat.A8_SW:
                data = ReadA8(data, height, width);
                data = PostProcessing.UnswizzleTexture(data, width, height, PixelFormat.Format32bppArgb);
                break;

            case PsbPixelFormat.TileA8_SW:
                data = ReadA8(data, height, width);
                data = PostProcessing.UntileTexture(data, width, height, PixelFormat.Format32bppArgb);
                break;

            case PsbPixelFormat.L8:
                data = ReadL8(data, height, width);
                break;

            case PsbPixelFormat.L8_SW:
                data = ReadL8(data, height, width);
                data = PostProcessing.UnswizzleTexture(data, width, height, PixelFormat.Format32bppArgb);
                break;

            case PsbPixelFormat.RGBA5650:
                data = ReadRgba5650(data);
                break;

            case PsbPixelFormat.RGBA5650_SW:
                data = ReadRgba5650(data);
                data = PostProcessing.UnswizzleTexture(data, width, height, PixelFormat.Format32bppArgb);
                break;
            }

            int    stride    = bmpData.Stride;  // 扫描线的宽度
            int    offset    = stride - width;  // 显示宽度与扫描线宽度的间隙
            IntPtr iptr      = bmpData.Scan0;   // 获取bmpData的内存起始位置
            int    scanBytes = stride * height; // 用stride宽度,表示这是内存区域的大小

            if (scanBytes >= data.Length)
            {
                Marshal.Copy(data, 0, iptr, data.Length);
                bmp.UnlockBits(bmpData); // 解锁内存区域
                return(bmp);
            }

            throw new BadImageFormatException("data may not corresponding");
        }
Ejemplo n.º 4
0
        private static byte[] PixelBytesFromImage(Bitmap bmp, PsbPixelFormat pixelFormat = PsbPixelFormat.None)
        {
            BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                                              ImageLockMode.ReadOnly, bmp.PixelFormat);

            int    stride    = bmpData.Stride;      // 扫描线的宽度
            int    offset    = stride - bmp.Width;  // 显示宽度与扫描线宽度的间隙
            IntPtr iPtr      = bmpData.Scan0;       // 获取bmpData的内存起始位置
            int    scanBytes = stride * bmp.Height; // 用stride宽度,表示这是内存区域的大小

            var result = new byte[scanBytes];

            Marshal.Copy(iPtr, result, 0, scanBytes);
            bmp.UnlockBits(bmpData); // 解锁内存区域

            switch (pixelFormat)
            {
            case PsbPixelFormat.LeRGBA4444:
                result = Argb428(result, false);
                break;

            case PsbPixelFormat.BeRGBA4444:
                Argb2Rgba(ref result, true);
                result = Argb428(result, false);
                break;

            case PsbPixelFormat.BeRGBA8:
                Switch_0_2(ref result);
                break;

            case PsbPixelFormat.A8L8:
                result = Argb2A8L8(result);
                break;

            case PsbPixelFormat.A8L8_SW:
                result = PostProcessing.SwizzleTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                result = Argb2A8L8(result);
                break;

            case PsbPixelFormat.DXT5:
                //Switch_0_2(ref result);
                result = DxtUtil.Dxt5Encode(result, bmp.Width, bmp.Height);
                break;

            case PsbPixelFormat.RGBA8_SW:
                result = PostProcessing.SwizzleTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                Switch_0_2(ref result);
                break;

            case PsbPixelFormat.TileRGBA8_SW:
                result = PostProcessing.TileTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                break;

            case PsbPixelFormat.L8:
                result = Argb2L8(result);
                break;

            case PsbPixelFormat.A8_SW:
                result = PostProcessing.SwizzleTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                result = Argb2A8(result);
                break;

            case PsbPixelFormat.TileA8_SW:
                result = PostProcessing.TileTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                result = Argb2A8(result);
                break;

            case PsbPixelFormat.A8:
                result = Argb2A8(result);
                break;

            case PsbPixelFormat.CI8_SW:
                result = PostProcessing.SwizzleTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                break;

            case PsbPixelFormat.CI4_SW:
                result = PostProcessing.SwizzleTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                break;

            case PsbPixelFormat.L8_SW:
                result = PostProcessing.SwizzleTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                result = Argb2L8(result);
                break;

            case PsbPixelFormat.LeRGBA4444_SW:
                result = PostProcessing.SwizzleTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                result = Argb428(result, false);
                break;

            case PsbPixelFormat.RGBA5650:
                result = Argb2Rgba5650(result);
                break;

            case PsbPixelFormat.RGBA5650_SW:
                result = PostProcessing.SwizzleTexture(result, bmp.Width, bmp.Height, bmp.PixelFormat);
                result = Argb2Rgba5650(result);
                break;
            }

            return(result);
        }
Ejemplo n.º 5
0
        public static Bitmap ConvertToImage(byte[] data, int height, int width,
                                            PsbPixelFormat colorFormat = PsbPixelFormat.None)
        {
            var bmp     = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
                                       ImageLockMode.WriteOnly, bmp.PixelFormat);

            switch (colorFormat)
            {
            case PsbPixelFormat.CommonRGBA4444:
                data = Rgba428(data);
                Bgra2Abgr(ref data);
                break;

            case PsbPixelFormat.WinRGBA4444:
                data = Rgba428(data);
                break;

            case PsbPixelFormat.CommonRGBA8:
                Abgr2Argb(ref data);
                break;

            case PsbPixelFormat.A8L8:
                data = ReadA8L8(data, width, height);
                break;

            case PsbPixelFormat.DXT5:    //MARK: RL seems compatible to DXT5 compress?
                data = DxtUtil.DecompressDxt5(data, width, height);
                Abgr2Argb(ref data);     //DXT5(for win) need conversion
                break;

            case PsbPixelFormat.RGBA8_SW:
                data = PostProcessing.UntileTexture(data, width, height, PixelFormat.Format32bppArgb);
                break;

            case PsbPixelFormat.A8:
                data = ReadA8(data, height, width);
                break;

            case PsbPixelFormat.A8_SW:
                data = ReadA8(data, height, width);
                data = PostProcessing.UntileTexture(data, width, height, PixelFormat.Format32bppArgb);
                break;

            case PsbPixelFormat.L8:
                data = ReadL8(data, height, width);
                break;
            }

            int    stride    = bmpData.Stride;  // 扫描线的宽度
            int    offset    = stride - width;  // 显示宽度与扫描线宽度的间隙
            IntPtr iptr      = bmpData.Scan0;   // 获取bmpData的内存起始位置
            int    scanBytes = stride * height; // 用stride宽度,表示这是内存区域的大小

            if (scanBytes >= data.Length)
            {
                System.Runtime.InteropServices.Marshal.Copy(data, 0, iptr, data.Length);
                bmp.UnlockBits(bmpData); // 解锁内存区域
                return(bmp);
            }

            throw new BadImageFormatException("data may not corresponding");
        }