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 ImageInfo SetImage(Image newImage, CompressionType compression) { int width = newImage.Width; int height = newImage.Height; byte[] header = null; byte[] data = null; if (compression == CompressionType.BGRA) { Bitmap image = new Bitmap(newImage); MemoryStream mspixels = new MemoryStream(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Color pixel = image.GetPixel(j, i); mspixels.WriteByte(pixel.B); mspixels.WriteByte(pixel.G); mspixels.WriteByte(pixel.R); mspixels.WriteByte(pixel.A); } } data = mspixels.ToArray(); MemoryStream msx = new MemoryStream(); BinaryWriter bw = new BinaryWriter(msx); bw.Write((int)0); bw.Write((int)0); bw.Write((int)0); bw.Write((int)1); bw.Write((int)0x15); bw.Write((short)width); bw.Write((short)height); bw.Write((int)0x15); bw.Write((int)0); bw.Flush(); header = msx.ToArray(); bw.Close(); } else { DXTCompression dxt = DXTCompression.DXT1; if (compression == CompressionType.DXT3) { dxt = DXTCompression.DXT1; } else if (compression == CompressionType.DXT5) { dxt = DXTCompression.DXT5; } data = ImageUtil.CompressImage(newImage, dxt); MemoryStream msx = new MemoryStream(); BinaryWriter bw = new BinaryWriter(msx); bw.Write((int)0); bw.Write((int)0); bw.Write((int)0); bw.Write((int)1); bw.Write(Encoding.ASCII.GetBytes(compression.ToString())); bw.Write((short)width); bw.Write((short)height); bw.Write((int)0x15); bw.Write((int)0); bw.Flush(); header = msx.ToArray(); bw.Close(); } return(new ImageInfo(header, data)); }
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); } }