private static MemoryStream BLP2toDXT1(byte[] blp2) { MemoryStream dxt1; using (var src = new MemoryStream(blp2)) { using (var bsrc = new BinaryReader(src)) { BLP2_HEADER header; header.ident = bsrc.ReadBytes(4); header.compress = bsrc.ReadInt32(); header.flags = bsrc.ReadBytes(4); header.width = bsrc.ReadInt32(); header.height = bsrc.ReadInt32(); header.mipmapOffsets = new int[16]; for (int i = 0; i < 16; i++) header.mipmapOffsets[i] = bsrc.ReadInt32(); header.mipmapLengths = new int[16]; for (int i = 0; i < 16; i++) header.mipmapLengths[i] = bsrc.ReadInt32(); int nPixels = header.width * header.height; if (header.compress == 1) { RGBA_PIXEL[] pal = new RGBA_PIXEL[256]; for (int p = 0; p < 256; p++) { pal[p].R = bsrc.ReadByte(); pal[p].G = bsrc.ReadByte(); pal[p].B = bsrc.ReadByte(); pal[p].A = bsrc.ReadByte(); } src.Seek(header.mipmapOffsets[0], SeekOrigin.Begin); if (header.flags[0] == 1) { return null; } else if (header.flags[0] == 2) { if (header.flags[1] == 8) { return null; } else { // Ugly way to write the DXT1 header dxt1 = new MemoryStream(); dxt1.WriteByte((byte)'D'); dxt1.WriteByte((byte)'D'); dxt1.WriteByte((byte)'S'); dxt1.WriteByte((byte)' '); dxt1.WriteByte((byte)'|'); for (int i = 0; i < 3; i++) dxt1.WriteByte(0); dxt1.WriteByte(7); dxt1.WriteByte(0x10); for (int i = 0; i < 3; i++) dxt1.WriteByte(0); dxt1.WriteByte(1); for (int i = 0; i < 3; i++) dxt1.WriteByte(0); dxt1.WriteByte(1); for (int i = 0; i < 58; i++) dxt1.WriteByte(0); dxt1.WriteByte((byte)' '); for (int i = 0; i < 3; i++) dxt1.WriteByte(0); dxt1.WriteByte(4); for (int i = 0; i < 3; i++) dxt1.WriteByte(0); dxt1.WriteByte((byte)'D'); dxt1.WriteByte((byte)'X'); dxt1.WriteByte((byte)'T'); dxt1.WriteByte((byte)'1'); for (int i = 0; i < 21; i++) dxt1.WriteByte(0); dxt1.WriteByte(0x10); for (int i = 0; i < 18; i++) dxt1.WriteByte(0); dxt1.Write(blp2, header.mipmapOffsets[0], header.mipmapLengths[0]); return dxt1; } } } } } return null; }
private static MemoryStream BLP2toTGA(byte[] blp2) { MemoryStream tga = new MemoryStream(); using (var src = new MemoryStream(blp2)) { using (var bsrc = new BinaryReader(src)) { BLP2_HEADER header; header.ident = bsrc.ReadBytes(4); header.compress = bsrc.ReadInt32(); header.flags = bsrc.ReadBytes(4); header.width = bsrc.ReadInt32(); header.height = bsrc.ReadInt32(); header.mipmapOffsets = new int[16]; for (int i = 0; i < 16; i++) header.mipmapOffsets[i] = bsrc.ReadInt32(); header.mipmapLengths = new int[16]; for (int i = 0; i < 16; i++) header.mipmapLengths[i] = bsrc.ReadInt32(); int nPixels = header.width * header.height; // Ugly way to write the tga header tga.WriteByte(0); tga.WriteByte(0); tga.WriteByte(2); tga.WriteByte(0); tga.WriteByte(0); tga.WriteByte(0); tga.WriteByte(0); tga.WriteByte(0); tga.WriteByte(0); tga.WriteByte(0); tga.WriteByte(0); tga.WriteByte(0); tga.WriteByte(0); tga.WriteByte(1); tga.WriteByte(0); tga.WriteByte(1); tga.WriteByte(32); tga.WriteByte(8); if (header.compress == 1) { RGBA_PIXEL[] pal = new RGBA_PIXEL[256]; for (int p = 0; p < 256; p++) { pal[p].R = bsrc.ReadByte(); pal[p].G = bsrc.ReadByte(); pal[p].B = bsrc.ReadByte(); pal[p].A = bsrc.ReadByte(); } src.Seek(header.mipmapOffsets[0], SeekOrigin.Begin); if (header.flags[0] == 1) { // Straks, volgens mij is het DXT1 formaat } else if (header.flags[0] == 2) { if (header.flags[1] == 8) { DecodeDDSFormat((int)DDS_Format.DDS_DXT3, src, header.width, header.height, tga); } else { DecodeDDSFormat((int)DDS_Format.DDS_DXT1, src, header.width, header.height, tga); } } } else if (header.compress == 0) { // This is JPEG, dunno, just output? } } } return tga; }