protected override void setBMPInternal(int index, ref Bitmap bmp) { if (this.file == null || !this.file.CanWrite) { throw new NotSupportedException("Stream is readonly"); } if (this.compatFixes) { throw new NotSupportedException("Input data had various compatibility fixes applied to be able to load! Cannot save with these."); } parsedImgData imgInfo = this.imgDatas[index]; if (bmp.Width != imgInfo.width || bmp.Height != imgInfo.height) { throw new NotSupportedException("New image has different dimensions"); } { PixelFormat pf; switch (imgInfo.type) { case 19: pf = PixelFormat.Format8bppIndexed; break; case 20: pf = PixelFormat.Format4bppIndexed; break; default: throw new NotSupportedException("Unsupported type"); } if (bmp.PixelFormat != pf) { requestQuantize(ref bmp, pf); } } /*Get the BAR subfile*/ byte[] barFile = this.BAR.fileList[imgInfo.barFile].data; using (BinaryReader br = new BinaryReader(new MemoryStream(barFile, true))) { br.BaseStream.Position = imgInfo.palBOffs; byte[] palData = getData(br, imgInfo.baseP), imgData; { byte[] palette = new byte[1024]; Array.Copy(palData, imgInfo.palOffs, palette, 0, palette.Length); imgData = TexUt2.Encode(bmp, ref palette, imgInfo.palCs); Array.Copy(palette, 0, palData, imgInfo.palOffs, palette.Length); } br.BaseStream.Position = imgInfo.palBOffs; setData(br, imgInfo.baseP, palData); br.BaseStream.Position = imgInfo.imgBOffs; setData(br, imgInfo.baseP, imgData); } this.BAR.fileList[imgInfo.barFile].data = barFile; this.file.Position = 0; this.BAR.save(new Crazycatz00.Utility.Substream(this.file)); }
private void ParseTIM(BinaryReader br, long baseP, int barPos) { br.BaseStream.Position = baseP + 8 + 4; int count = br.ReadInt32(); int offsetTableOff = br.ReadInt32(); int dataOff = br.ReadInt32(); int mkImgOff = br.ReadInt32(); br.BaseStream.Position = baseP + offsetTableOff; int[] offs = new int[count]; for (int i = 0; i < count; ++i) { offs[i] = br.ReadByte(); } long palOffs, palPos = br.BaseStream.Position = baseP + dataOff + 32; byte[] palData = getDataR(br, baseP, out palOffs); #if MDLX_DUMP_RAW_DATA File.WriteAllBytes(string.Format("RAW-palData-{0}.bin", barPos), palData); #endif for (int i = 0; i < count; ++i) { parsedImgData info = new parsedImgData() { barFile = barPos, baseP = baseP, palBOffs = palPos }; br.BaseStream.Position = info.imgBOffs = baseP + dataOff + 32 + 144 + (144 * offs[i]); byte[] imgData = getData(br, baseP); #if MDLX_DUMP_RAW_DATA File.WriteAllBytes(string.Format("RAW-imgData-{0}-{1}[palOffs={2}].bin", barPos, this.bmps.Count, palOffs), imgData); #endif br.BaseStream.Position = baseP + mkImgOff + 32 + (160 * i) + 8; Bitmap img = getImage(br, ref imgData, palData, palOffs, info); this.bmps.Add(img); this.imgDatas.Add(info); Debug.WriteLine(String.Format("Added image #{0}:\n\tSubFile: {1}\n\tiBlockOffset: {2}\n\tpOffset: {3}\n\tWidth: {4}\n\tHeight: {5}\n\tPixelFormat: {6}", this.bmps.Count, barPos, info.imgBOffs, palPos, img.Width, img.Height, img.PixelFormat)); } }
protected override void setBMPInternal(int index, ref Bitmap bmp) { parsedImgData imgInfo = imgDatas[index]; if (bmp.Width != imgInfo.width || bmp.Height != imgInfo.height) { throw new NotSupportedException("New image has different dimensions"); } { PixelFormat pf; switch (imgInfo.type) { case 19: pf = PixelFormat.Format8bppIndexed; break; case 20: pf = PixelFormat.Format4bppIndexed; break; default: throw new NotSupportedException("Unsupported type"); } if (bmp.PixelFormat != pf) { requestQuantize(ref bmp, pf); } } /*Get the BAR subfile*/ byte[] barFile = this.barFile.data; using (var br = new BinaryReader(new MemoryStream(barFile, true))) { br.BaseStream.Position = imgInfo.palBOffs; byte[] palData = getData(br, imgInfo.baseP), imgData; { var palette = new byte[1024]; Array.Copy(palData, imgInfo.palOffs, palette, 0, palette.Length); imgData = TexUt2.Encode(bmp, ref palette, imgInfo.palCs); Array.Copy(palette, 0, palData, imgInfo.palOffs, palette.Length); } br.BaseStream.Position = imgInfo.palBOffs; setData(br, imgInfo.baseP, palData); br.BaseStream.Position = imgInfo.imgBOffs; setData(br, imgInfo.baseP, imgData); } this.barFile.data = barFile; }
private static Bitmap getImage(BinaryReader br, ref byte[] imgData, byte[] palData, long palOffs, parsedImgData info) { #if TRACE Trace.Assert(br.ReadUInt64() == 0x3fL); br.BaseStream.Position += 8; Trace.Assert(br.ReadUInt64() == 0x34L); br.BaseStream.Position += 8; Trace.Assert(br.ReadUInt64() == 0x36L); ulong num = br.ReadUInt64(); Trace.Assert(br.ReadUInt64() == 0x16L); Trace.Assert(((num >> 20) & 0x3f) == 0x13); Trace.Assert(((num >> 0x33) & 15) == 0); Trace.Assert(((num >> 0x37) & 1) == 0); Trace.Assert(((num >> 0x38) & 0x1f) == 0); Trace.Assert(((num >> 0x3d) & 7) == 4); br.BaseStream.Position += 8; Trace.Assert(br.ReadUInt64() == 0x14L); #else br.BaseStream.Position += 8 + 16 + 16 + 16 + 16; #endif num = br.ReadUInt64(); #if TRACE Trace.Assert(br.ReadUInt64() == 0x06L); Trace.Assert(((num >> 0x22) & 1) == 1); Trace.Assert(((num >> 0x33) & 15) == 0); Trace.Assert(((num >> 0x37) & 1) == 0); Trace.Assert(((num >> 0x3d) & 7) == 0); br.BaseStream.Position += 8; Trace.Assert(br.ReadUInt64() == 8L); #else //br.BaseStream.Position += 8 + 16; #endif info.type = (uint)(num >> 20) & 0x3fu; if (info.type != 19 && info.type != 20) { throw new NotSupportedException("Unknown t0PSM: " + info.type); } info.palOffs = (256 * ((uint)(num >> 0x25) & 0x3FFFu)) - palOffs; info.width = (ushort)(1u << ((int)(num >> 0x1A) & 0x0F)); info.height = (ushort)(1u << ((int)(num >> 0x1E) & 0x0F)); info.palCs = (byte)((uint)(num >> 0x38) & 0x1Fu); byte[] palette = new byte[1024]; if (info.palOffs < 0) { throw new NotSupportedException("Image palette located before block address."); } if (info.palOffs + palette.Length > palData.Length) { throw new NotSupportedException("Image palette located after block address."); } Array.Copy(palData, info.palOffs, palette, 0, palette.Length); int size = info.width * info.height; if (info.type == 20) { size /= 2; } if (imgData.Length < size) { Debug.WriteLine("Expected size = " + size + "; got = " + imgData.Length); Array.Resize <byte>(ref imgData, size); } return(TexUt2.Decode(imgData, palette, info.type, info.width, info.height, info.palCs)); }
private void ParseTIM(BinaryReader br, long baseP, int barPos) { br.BaseStream.Position = baseP + 8 + 4; int count = br.ReadInt32(); int offsetTableOff = br.ReadInt32(); int dataOff = br.ReadInt32(); int mkImgOff = br.ReadInt32(); br.BaseStream.Position = baseP + offsetTableOff; var offs = new int[count]; for (int i = 0; i < count; ++i) { offs[i] = br.ReadByte(); } long palOffs, palPos = br.BaseStream.Position = baseP + dataOff + 32; byte[] palData = getDataR(br, baseP, out palOffs); for (int i = 0; i < count; ++i) { var info = new parsedImgData { barFile = barPos, baseP = baseP, palBOffs = palPos }; br.BaseStream.Position = info.imgBOffs = baseP + dataOff + 32 + 144 + (144 * offs[i]); byte[] imgData = getData(br, baseP); br.BaseStream.Position = baseP + mkImgOff + 32 + (160 * i) + 8; byte[] palette = new byte[1024]; Bitmap img = getImage(br, ref imgData, palData, palOffs, info, out palette); bmps.Add(Convert(img)); Palettes.Add(new Color[256]); for (int c = 0; c < 256; c++) { int alpha = palette[c * 4 + 3] * 2; if (alpha > 255) { alpha = 255; } Palettes[Palettes.Count - 1][c] = Color.FromArgb((byte)alpha, palette[c * 4], palette[c * 4 + 1], palette[c * 4 + 2]); } imgDatas.Add(info); } /*br.BaseStream.Position = 0; * * for (int dmyReach = 0; dmyReach+16 < br.BaseStream.Length; dmyReach += 16) * { * br.BaseStream.Position = dmyReach; * * if (br.ReadUInt32() == 0x594D445F) * { * br.BaseStream.Position = dmyReach + 0x0C; * int nextPatch = br.ReadInt32(); * * br.BaseStream.Position = dmyReach + 0x12; * int patchPalette = br.ReadUInt16(); * * br.BaseStream.Position = dmyReach + 0x1E; * int patchCount = br.ReadUInt16(); * * br.BaseStream.Position = dmyReach + 0x20; * int patchX = br.ReadUInt16(); * int patchY = br.ReadUInt16(); * int patchWidth = br.ReadUInt16(); * int patchHeight = br.ReadUInt16(); * * br.BaseStream.Position = dmyReach + 0x30; * int patchStart = br.ReadInt32(); * * nextPatch += 16 + dmyReach; * patchStart += 16 + dmyReach; * * Bitmap patch = new Bitmap(patchWidth, patchHeight * patchCount); * int patchSize = patchWidth * patchHeight * patchCount; * byte[] patchPixels = new byte[patchSize]; * * br.BaseStream.Position = patchStart; * br.BaseStream.Read(patchPixels, 0, patchPixels.Length); * * for (int of = 0; of < patchSize; of++) * { * patch.SetPixel(of % patchWidth, of / patchWidth, Palettes[patchPalette][patchPixels[of]]); * } * * Patches.Add(patch); * PatchesCounts.Add(patchCount); * PatchesSizes.Add(new Size(patchWidth, patchHeight)); * PatchesPositions.Add(new Point(patchX, patchY)); * PatchesDestinations.Add(patchPalette); * } * }*/ }