public static XPR2 FromImage(string path) { if (!File.Exists(path)) { return(null); } XPR2 xpr = new XPR2(); xpr._image = new MagickImage(path); xpr._name = Path.GetFileNameWithoutExtension(path); return(xpr); }
public static XPR2 FromFile(string path) { if (!File.Exists(path)) { return(null); } XPR2 xpr = new XPR2(); // Opens .xpr texture using (AwesomeReader ar = new AwesomeReader(File.OpenRead(path), true)) { if (ar.ReadInt32() != MAGIC_XPR2) { return(null); } ar.ReadInt32(); // Always 2048? int texSize = ar.ReadInt32(); int imageCount = ar.ReadInt32(); xpr._image = new MagickImage(); for (int i = 0; i < imageCount; i++) { // TX2D Header if (ar.ReadInt32() != MAGIC_TX2D) { continue; } int infoOffset = ar.ReadInt32() + 45; ar.ReadInt32(); // Not needed - Always 52? int nameOffset = ar.ReadInt32() + 12; // Texture name offset // Reads texture name ar.BaseStream.Position = nameOffset; string name = ar.ReadNullString(); // "album" // Reads texture info ar.BaseStream.Position = infoOffset; int imageOffset = ar.ReadUInt16(); int bytesOffset = (imageOffset << 8) + 0x80C; // Where raw image bytes are byte compressionType = ar.ReadByte(); // 0x52 = DXT1 int height = (ar.ReadUInt16() + 1) << 3; int width = (ar.ReadUInt16() + 1) & 0x1FFF; int size = ((texSize >> 8) - imageOffset) << 8; // Reads raw image data ar.BaseStream.Position = bytesOffset; byte[] data = ar.ReadBytes(size); string compression = ""; switch (compressionType) { case 0x52: // DXT1 compression = "DXT1"; break; case 0x53: // DXT3 continue; case 0x54: // DXT5 compression = "DXT5"; break; case 0x71: // DXT5 - Packed normal map case 0x7C: // DXT1 - Packed normal map case 0x86: // Raw (a8r8g8b8) continue; } // TODO: Better implementation for alternate compressions SwapBytes(data); byte[] outData = UntileCompressedXbox360Texture(data, width, height, width, height, 4, 4, compression == "DXT1" ? 8 : 16); byte[] dds = new byte[128 + outData.Length]; // Writes DDS file using (MemoryStream ms = new MemoryStream(dds)) { ms.Write(BuildDDSHeader(compression, width, height, width * 4, 0), 0, 128); ms.Write(outData, 0, outData.Length); } // Converts to bitmap xpr._image = new MagickImage(dds); xpr._name = name; } } return(xpr); }
public static XPR2 FromFile(string path) { if (!File.Exists(path)) { return(null); } XPR2 xpr = new XPR2(); // Opens .xpr texture using (AwesomeReader ar = new AwesomeReader(File.OpenRead(path), true)) { if (ar.ReadInt32() != MAGIC_XPR2) { return(null); } ar.ReadInt32(); // Always 2048? int texSize = ar.ReadInt32(); xpr.images = new Bitmap[ar.ReadInt32()]; for (int i = 0; i < xpr.images.Length; i++) { // TX2D Header if (ar.ReadInt32() != MAGIC_TX2D) { continue; } int infoOffset = ar.ReadInt32() + 45; ar.ReadInt32(); // Not needed - Always 52? int nameOffset = ar.ReadInt32() + 12; // Texture name offset // Reads texture name ar.BaseStream.Position = nameOffset; string name = ar.ReadNullString(); // "album" // Reads texture info ar.BaseStream.Position = infoOffset; int imageOffset = ar.ReadUInt16(); int bytesOffset = (imageOffset << 8) + 0x80C; // Where raw image bytes are byte compressionType = ar.ReadByte(); // 0x52 = DXT1 int height = (ar.ReadUInt16() + 1) << 3; int width = (ar.ReadUInt16() + 1) & 0x1FFF; int size = ((texSize >> 8) - imageOffset) << 8; // Reads raw image data ar.BaseStream.Position = bytesOffset; byte[] data = ar.ReadBytes(size); string compression = ""; switch (compressionType) { case 0x52: // DXT1 compression = "DXT1"; break; case 0x53: // DXT3 case 0x54: // DXT5 case 0x71: // DXT5 - Packed normal map case 0x7C: // DXT1 - PAcked normal map case 0x86: // raw continue; } byte[] dds = new byte[128 + data.Length]; SwapBytes(data); using (MemoryStream ms = new MemoryStream(dds)) { ms.Write(BuildDDSHeader(compression, width, height, data.Length, 0), 0, 128); ms.Write(data, 0, data.Length); ms.Seek(0, SeekOrigin.Begin); DDSImage image = new DDSImage(ms); xpr.images[i] = image.BitmapImage; //xpr.images[i].Save("converted.png"); } //File.WriteAllBytes("test.dds", dds); continue; // Not ready for multiple images yet } } return(xpr); }