public static Image GetImagePS3(byte[] data, byte[] extraData) { if (extraData != null && data.Length == 48) { try { MemoryStream ms = new MemoryStream(data); BinaryReader br = new BinaryReader(ms); byte compression = br.ReadByte(); byte[] unknown1 = br.ReadBytes(3); CompressionType type = CompressionType.UNKNOWN; if (compression == 0x85) { type = CompressionType.ARGB; } else if (compression == 0x86) { type = CompressionType.DXT1; } else if (compression == 0x88) { type = CompressionType.DXT5; } int unknown2 = Util.ReverseBytes(br.ReadInt32()); int width = Util.ReverseBytes(br.ReadInt16()); int height = Util.ReverseBytes(br.ReadInt16()); br.Close(); byte[] pixels = extraData; if (type == CompressionType.DXT1) { pixels = ImageUtil.DecompressImage(pixels, width, height, DXTCompression.DXT1); } else if (type == CompressionType.DXT3) { pixels = ImageUtil.DecompressImage(pixels, width, height, DXTCompression.DXT3); } else if (type == CompressionType.DXT5) { pixels = ImageUtil.DecompressImage(pixels, width, height, DXTCompression.DXT5); } DirectBitmap bitmap = new DirectBitmap(width, height); int index = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { byte red; byte green; byte blue; byte alpha; if (type == CompressionType.BGRA) { blue = pixels[index + 0]; green = pixels[index + 1]; red = pixels[index + 2]; alpha = pixels[index + 3]; } else { alpha = pixels[index + 0]; red = pixels[index + 1]; green = pixels[index + 2]; blue = pixels[index + 3]; } Color color = Color.FromArgb(alpha, red, green, blue); bitmap.Bits[x + y * width] = color.ToArgb(); bitmap.SetPixel(x, y, color); index += 4; } } return(bitmap.Bitmap); } catch { return(null); } } else { return(null); } }
public static Image GetImage(byte[] data, byte[] extraData) { try { ImageHeader header = GetImageHeader(data); byte[] pixels = extraData; if (header.CompressionType == CompressionType.DXT1) { pixels = ImageUtil.DecompressImage(pixels, header.Width, header.Height, DXTCompression.DXT1); } else if (header.CompressionType == CompressionType.DXT3) { pixels = ImageUtil.DecompressImage(pixels, header.Width, header.Height, DXTCompression.DXT3); } else if (header.CompressionType == CompressionType.DXT5) { pixels = ImageUtil.DecompressImage(pixels, header.Width, header.Height, DXTCompression.DXT5); } DirectBitmap bitmap = new DirectBitmap(header.Width, header.Height); int index = 0; for (int y = 0; y < header.Height; y++) { for (int x = 0; x < header.Width; x++) { byte red; byte green; byte blue; byte alpha; if (header.CompressionType == CompressionType.BGRA) { blue = pixels[index + 0]; green = pixels[index + 1]; red = pixels[index + 2]; alpha = pixels[index + 3]; } else if (header.CompressionType == CompressionType.RGBA) { red = pixels[index + 0]; green = pixels[index + 1]; blue = pixels[index + 2]; alpha = pixels[index + 3]; } else { alpha = pixels[index + 0]; red = pixels[index + 1]; green = pixels[index + 2]; blue = pixels[index + 3]; } Color color = Color.FromArgb(alpha, red, green, blue); bitmap.Bits[x + y * header.Width] = color.ToArgb(); index += 4; } } return(bitmap.Bitmap); } catch (Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); return(null); } }