public static void WriteColumn(BinaryWriter bw, TxmPixelFormat format, int bytesPerLine, byte[] data) { ColumnParams colParams = GetColumnParams(format); int bytesPerDestLine = colParams.Width * colParams.BitsPerPixel / 8; int skipLength = bytesPerLine - bytesPerDestLine; for (int i = 0; i < colParams.Height; ++i) { bw.Write(data, i * bytesPerDestLine, bytesPerDestLine); bw.Seek(skipLength, SeekOrigin.Current); } }
public static byte[] ReadColumn(BinaryReader br, TxmPixelFormat format, int bytesPerLine) { ColumnParams colParams = GetColumnParams(format); byte[] data = new byte[colParams.Width * colParams.Height * colParams.BitsPerPixel / 8]; int bytesPerDestLine = colParams.Width * colParams.BitsPerPixel / 8; int skipLength = bytesPerLine - bytesPerDestLine; for (int i = 0; i < colParams.Height; ++i) { byte[] lineData = br.ReadBytes(bytesPerDestLine); br.BaseStream.Seek(skipLength, SeekOrigin.Current); Buffer.BlockCopy(lineData, 0, data, i * bytesPerDestLine, lineData.Length); } return(data); }
int GetImageMemSize(TxmPixelFormat format, int width, int height) { switch (format) { case TxmPixelFormat.PSMT4: return(width * height / 2); case TxmPixelFormat.PSMT8: return(width * height); case TxmPixelFormat.PSMCT32: return(width * height * 4); default: return(0); } }
public static ColumnParams GetColumnParams(TxmPixelFormat format) { switch (format) { case TxmPixelFormat.PSMCT32: case TxmPixelFormat.PSMCT24: return(PSMCT32_COLUMN_PARAMS); case TxmPixelFormat.PSMT8: return(PSMT8_COLUMN_PARAMS); case TxmPixelFormat.PSMT4: return(PSMT4_COLUMN_PARAMS); default: throw new NotSupportedException($"{format} not supported."); } }
public static byte[] MixColumn(byte[] column, TxmPixelFormat srcFormat, TxmPixelFormat destFormat, bool isOdd) { if (srcFormat != TxmPixelFormat.PSMCT32) { throw new NotSupportedException("Only PSMCT32 supported as source format."); } switch (destFormat) { case TxmPixelFormat.PSMT4: return(MixColumn32To4(column, isOdd)); case TxmPixelFormat.PSMT8: return(MixColumn32To8(column, isOdd)); default: throw new NotSupportedException($"{destFormat} not supported as destination format."); } }
public static int CalcBlockMemoryOffset(TxmPixelFormat format, int index) { ColumnParams colParams = GetColumnParams(format); switch (format) { case TxmPixelFormat.PSMCT32: return(CalcBlockMemoryOffset(colParams, PSMCT32_BLOCK_REVERSE_LOOKUP, index)); case TxmPixelFormat.PSMT8: return(CalcBlockMemoryOffset(colParams, PSMT8_BLOCK_REVERSE_LOOKUP, index)); case TxmPixelFormat.PSMT4: return(CalcBlockMemoryOffset(colParams, PSMT4_BLOCK_REVERSE_LOOKUP, index)); default: throw new NotSupportedException($"{format} not supported"); } }
public static int CalcBlockNumber(TxmPixelFormat format, int blockX, int blockY, int texBufWidth) { ColumnParams colParams = GetColumnParams(format); switch (format) { case TxmPixelFormat.PSMCT32: return(CalcBlockNumber(colParams, PSMCT32_BLOCK_LOOKUP, blockX, blockY, texBufWidth)); case TxmPixelFormat.PSMT8: return(CalcBlockNumber(colParams, PSMT8_BLOCK_LOOKUP, blockX, blockY, texBufWidth)); case TxmPixelFormat.PSMT4: return(CalcBlockNumber(colParams, PSMT4_BLOCK_LOOKUP, blockX, blockY, texBufWidth)); default: throw new NotSupportedException($"{format} not supported"); } }
public static void ConvertImageToTxm(string inPath, Stream outStream, byte level = 1, ushort bufferBase = 0, ushort paletteBufferBase = 0) { using (var image = Image.Load <Rgba32>(inPath)) { // Gather all colors to see if it would fit in PSMT8 TxmPixelFormat pixelFormat = TxmPixelFormat.None; HashSet <Rgba32> colorSet = new HashSet <Rgba32>(); List <Rgba32> palette = null; for (int y = 0; y < image.Height; ++y) { var row = image.GetPixelRowSpan(y); for (int x = 0; x < image.Width; ++x) { colorSet.Add(row[x]); if (colorSet.Count > 256) { pixelFormat = TxmPixelFormat.PSMCT32; y = image.Height; break; } } } short paletteWidth = 0; short paletteHeight = 0; if (pixelFormat == TxmPixelFormat.None) { // Palette check passed, assign palettized pixel format if (colorSet.Count > 16) { pixelFormat = TxmPixelFormat.PSMT8; paletteWidth = 16; paletteHeight = 16; } else { pixelFormat = TxmPixelFormat.PSMT4; paletteWidth = 8; paletteHeight = 2; } palette = new List <Rgba32>(colorSet); } // Write header BinaryWriter bw = new BinaryWriter(outStream); TxmHeader txmHeader = new TxmHeader { ImageSourcePixelFormat = pixelFormat, ImageVideoPixelFormat = pixelFormat, ImageWidth = (short)image.Width, ImageHeight = (short)image.Height, ImageBufferBase = bufferBase, ClutPixelFormat = palette != null ? TxmPixelFormat.PSMCT32 : TxmPixelFormat.None, Misc = (byte)(level & 0x0f), ClutWidth = paletteWidth, ClutHeight = paletteHeight, ClutBufferBase = paletteBufferBase }; txmHeader.Write(bw); // Write palette int palettePixelsWritten = 0; if (pixelFormat == TxmPixelFormat.PSMT4) { foreach (var color in palette) { bw.Write(color.R); bw.Write(color.G); bw.Write(color.B); bw.Write((byte)((color.A + 1) >> 1)); ++palettePixelsWritten; } } else if (pixelFormat == TxmPixelFormat.PSMT8) { int baseOffset = 0; Rgba32 black = new Rgba32(); int[] order = new int[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, }; while (palettePixelsWritten < palette.Count) { foreach (var offset in order) { var palOffset = baseOffset + offset; var color = palOffset < palette.Count ? palette[palOffset] : black; bw.Write(color.R); bw.Write(color.G); bw.Write(color.B); bw.Write((byte)((color.A + 1) >> 1)); ++palettePixelsWritten; } baseOffset += order.Length; } } // Pad out rest of palette int targetOffset = 16 + (txmHeader.GetClutByteSize() + 15) / 16 * 16; while (outStream.Position < targetOffset) { bw.Write((byte)0); } // Write main image data byte pal4BppBuffer = 0; bool odd = false; for (int y = 0; y < image.Height; ++y) { var row = image.GetPixelRowSpan(y); for (int x = 0; x < image.Width; ++x) { var pixel = row[x]; if (pixelFormat == TxmPixelFormat.PSMCT32) { bw.Write(pixel.R); bw.Write(pixel.G); bw.Write(pixel.B); bw.Write(pixel.A); // Should be halved, but full range is used on PC } else { var palIndex = palette.IndexOf(pixel); if (pixelFormat == TxmPixelFormat.PSMT4) { pal4BppBuffer <<= 4; pal4BppBuffer |= (byte)(palIndex & 0x0f); odd = !odd; if (!odd) { bw.Write(pal4BppBuffer); pal4BppBuffer = 0; } } else { bw.Write((byte)palIndex); } } } } } }