/// <summary> /// Get Texture2D converted to 32bit color /// </summary> /// <param name="forcePalette">Desired Palette, see texture.NumOfPalettes. -1 is default.</param> /// <param name="colors">Override colors of palette; Array size must match texture.NumOfColorsPerPalette</param> /// <returns>32bit Texture2D</returns> /// <remarks> /// Some paletts are 256 but the game only uses 16 colors might need to make the restriction /// more lax and allow any size array and only throw errors if the colorkey is greater than /// size of array. Or we could treat any of those bad matches as transparent. /// </remarks> public override Texture2D GetTexture(Color[] colors) { if (Memory.graphics.GraphicsDevice == null) { return(null); } if (texture.PaletteFlag != 0) { if (colors == null) { throw new ArgumentNullException(nameof(colors)); } //if (colors != null && colors.Length != texture.NumOfColours) //{ // //if (colors.Length > texture.NumOfColours) //truncate colors to the correct amount. in some // // colors = colors.Take(texture.NumOfColours).ToArray(); // //else // might need to expand the array the other way if we get more mismatches. // //Array.Resize(ref colors,texture.NumOfColours); // //throw new Exception($" custom colors parameter set but array size to match palette size: {texture.NumOfColours}"); //} MemoryStream ms; using (BinaryReader br = new BinaryReader(ms = new MemoryStream(buffer))) { ms.Seek(TextureLocator, SeekOrigin.Begin); TextureBuffer convertBuffer = new TextureBuffer(texture.Width, texture.Height); for (int i = 0; i < convertBuffer.Length && ms.Position < ms.Length; i++) { byte colorKey = br.ReadByte(); if (colorKey > colors.Length) { continue; } convertBuffer[i] = colors[colorKey]; } return(convertBuffer.GetTexture()); } } else { if (texture.bytesPerPixel == 2) { MemoryStream ms; using (BinaryReader br = new BinaryReader(ms = new MemoryStream(buffer))) { ms.Seek(TextureLocator, SeekOrigin.Begin); TextureBuffer convertBuffer = new TextureBuffer(texture.Width, texture.Height); for (int i = 0; ms.Position + 2 < ms.Length; i++) { convertBuffer[i] = ABGR1555toRGBA32bit(br.ReadUInt16()); } return(convertBuffer.GetTexture()); } } else if (texture.bytesPerPixel == 3) { // not tested but vincent tim had support for it so i guess it's possible RGB or BGR MemoryStream ms; using (BinaryReader br = new BinaryReader(ms = new MemoryStream(buffer))) { ms.Seek(TextureLocator, SeekOrigin.Begin); TextureBuffer convertBuffer = new TextureBuffer(texture.Width, texture.Height); Color color; color.A = 0xFF; for (int i = 0; ms.Position + 3 < ms.Length; i++) { //RGB or BGR so might need to reorder things to RGB color.B = br.ReadByte(); color.G = br.ReadByte(); color.R = br.ReadByte(); convertBuffer[i] = color; } return(convertBuffer.GetTexture()); } } } return(null); }
/// <summary> /// Get Texture2D converted to 32bit color /// </summary> /// <param name="forcePalette">Desired Palette, see texture.NumOfPalettes. -1 is default.</param> /// <param name="colors">Override colors of palette; Array size must match texture.NumOfColorsPerPalette</param> /// <returns>32bit Texture2D</returns> /// <remarks> /// Some paletts are 256 but the game only uses 16 colors might need to make the restriction /// more lax and allow any size array and only throw errors if the colorkey is greater than /// size of array. Or we could treat any of those bad matches as transparent. /// </remarks> public override Texture2D GetTexture(Color[] colors = null) { if (Memory.graphics.GraphicsDevice != null) { if (texture.PaletteFlag != 0) { if (colors != null && colors.Length != texture.NumOfColours) { throw new Exception($" custom colors parameter set but array size to match palette size: {texture.NumOfColours}"); } MemoryStream ms; using (BinaryReader br = new BinaryReader(ms = new MemoryStream(buffer))) { ms.Seek(TextureLocator, SeekOrigin.Begin); TextureBuffer convertBuffer = new TextureBuffer(texture.Width, texture.Height); for (int i = 0; i < convertBuffer.Length && ms.Position < ms.Length; i++) { convertBuffer[i] = colors[br.ReadByte()]; //colorkey } return(convertBuffer.GetTexture()); } } else if (texture.bytesPerPixel == 2) { MemoryStream ms; using (BinaryReader br = new BinaryReader(ms = new MemoryStream(buffer))) { ms.Seek(TextureLocator, SeekOrigin.Begin); TextureBuffer convertBuffer = new TextureBuffer(texture.Width, texture.Height); for (int i = 0; ms.Position + 2 < ms.Length; i++) { convertBuffer[i] = ABGR1555toRGBA32bit(br.ReadUInt16()); } return(convertBuffer.GetTexture()); } } else if (texture.bytesPerPixel == 3) { // not tested but vincent tim had support for it so i guess it's possible RGB or BGR MemoryStream ms; using (BinaryReader br = new BinaryReader(ms = new MemoryStream(buffer))) { ms.Seek(TextureLocator, SeekOrigin.Begin); TextureBuffer convertBuffer = new TextureBuffer(texture.Width, texture.Height); Color color; color.A = 0xFF; for (int i = 0; ms.Position + 3 < ms.Length; i++) { //RGB or BGR so might need to reorder things to RGB color.B = br.ReadByte(); color.G = br.ReadByte(); color.R = br.ReadByte(); convertBuffer[i] = color; } return(convertBuffer.GetTexture()); } } } return(null); }