private void ReadPaletted(long fileOffset) { var _palette = new List <RGBA>(); _imageData = new List <RGBA>(); int totalColorCount = _header.PaletteData.TotalColorCount; if (totalColorCount == 0) { totalColorCount = _header.PaletteTotalColorCount; } // Read palettes int rgbaSize = Marshal.SizeOf(new RGBA()); int paletteDataSize = totalColorCount * rgbaSize; var content = _fileContainer.Read(fileOffset, paletteDataSize); fileOffset += paletteDataSize; for (int i = 0; i < totalColorCount; ++i) { var palette = ByteToStruct.Convert <RGBA>(content, i * rgbaSize, rgbaSize); _palette.Add(palette); } // Read image indexes (references to palette) int imageSize = _header.ImageData.Width * _header.ImageData.Height; var indexData = _fileContainer.Read(fileOffset, imageSize); fileOffset += imageSize; if (fileOffset - DataOffset != DataSize) { throw new FileLoadException("Data read and data size do not match"); } for (int i = 0; i < indexData.Length; ++i) { var index = indexData[i]; if (index < _palette.Count) { _imageData.Add(_palette[index]); } else { throw new FileLoadException("Palette index out of bounds"); } } }
private void Read() { var headerContent = _fileContainer.Read(DataOffset, Marshal.SizeOf(_header)); var fileOffset = DataOffset + Marshal.SizeOf(_header); _header = ByteToStruct.Convert <Header>(headerContent); if (_header.Version != 1) { throw new FileLoadException($"Unknown tex version: {_header.Version} in file {Name}"); } if (_header.PaletteData.Flag == 1) { ReadPaletted(fileOffset); } else { throw new FileLoadException($"Can't handle tex files without palettedata. file {Name}"); } }