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.CommonRGBA8: Rgba2Argb(ref result); break; case PsbPixelFormat.DXT5: //Rgba2Argb(ref result); result = DxtUtil.Dxt5Encode(result, bmp.Width, bmp.Height); break; } return(result); }
public static Bitmap ConvertToImage(byte[] data, int height, int width, PsbPixelFormat colorFormat = PsbPixelFormat.None) { Bitmap bmp; BitmapData bmpData; { bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb); bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, bmp.PixelFormat); } switch (colorFormat) { case PsbPixelFormat.WinRGBA4444: data = Rgba428(data); break; case PsbPixelFormat.CommonRGBA8: Rgba2Argb(ref data); break; case PsbPixelFormat.DXT5: //MARK: RL seems compatible to DXT5 compress? data = DxtUtil.DecompressDxt5(data, width, height); Rgba2Argb(ref data); //DXT5(for win) need conversion 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"); }
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"); }
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); }