TIM2.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace FF8
  6. {
  7. //upgraded TIM class, because that first one is a trash
  8. public class TIM2
  9. {
  10. public struct Texture
  11. {
  12. public ushort PaletteX;
  13. public ushort PaletteY;
  14. public ushort NumOfColours;
  15. public ushort NumOfCluts;
  16. public uint clutSize;
  17. public byte[] ClutData;
  18. public ushort ImageOrgX;
  19. public ushort ImageOrgY;
  20. public ushort Width;
  21. public ushort Height;
  22. }
  23. public struct Color
  24. {
  25. public byte Red;
  26. public byte Green;
  27. public byte Blue;
  28. public byte Alpha;
  29. }
  30. private int bpp = -1;
  31. private Texture texture;
  32. private PseudoBufferedStream pbs;
  33. private uint textureDataPointer;
  34. private uint timOffset;
  35. public TIM2(byte[] buffer, uint offset = 0)
  36. {
  37. pbs = new PseudoBufferedStream(buffer);
  38. timOffset = offset;
  39. pbs.Seek(offset, System.IO.SeekOrigin.Begin);
  40. pbs.Seek(4, System.IO.SeekOrigin.Current); //clutID
  41. byte bppIndicator = pbs.ReadByte();
  42. bppIndicator = (byte)(bppIndicator == 0x08 ? 4 :
  43. bppIndicator == 0x09 ? 8 :
  44. bppIndicator == 0x02 ? 16 :
  45. bppIndicator == 0x03 ? 24 : 8);
  46. bpp = bppIndicator;
  47. texture = new Texture();
  48. ReadParameters(bppIndicator);
  49. }
  50. public void KillStreams() => pbs.DisposeAll();
  51. private void ReadParameters(byte _bpp)
  52. {
  53. pbs.Seek(3, System.IO.SeekOrigin.Current);
  54. if (_bpp == 4)
  55. {
  56. texture.clutSize = pbs.ReadUInt() - 12;
  57. texture.PaletteX = pbs.ReadUShort();
  58. texture.PaletteY = pbs.ReadUShort();
  59. texture.NumOfColours = pbs.ReadUShort();
  60. texture.NumOfCluts = pbs.ReadUShort();
  61. int bppMultiplier = 16;
  62. if (texture.NumOfColours != 16 || texture.clutSize != (texture.NumOfCluts * bppMultiplier)) //wmsetus uses 4BPP, but sets 256 colours, but actually is 16, but num of clut is 2* 256/16 WTF?
  63. {
  64. texture.NumOfCluts = (ushort)(texture.NumOfColours / 16 * texture.NumOfCluts);
  65. bppMultiplier = 32;
  66. }
  67. byte[] buffer = new byte[texture.NumOfCluts * bppMultiplier];
  68. for (int i = 0; i != buffer.Length; i++)
  69. buffer[i] = pbs.ReadByte();
  70. texture.ClutData = buffer;
  71. pbs.Seek(4, System.IO.SeekOrigin.Current);
  72. texture.ImageOrgX = pbs.ReadUShort();
  73. texture.ImageOrgY = pbs.ReadUShort();
  74. texture.Width = (ushort)(pbs.ReadUShort() * 4);
  75. texture.Height = pbs.ReadUShort();
  76. textureDataPointer = (uint)pbs.Tell();
  77. return;
  78. }
  79. if (_bpp == 8)
  80. {
  81. pbs.Seek(4, System.IO.SeekOrigin.Current);
  82. texture.PaletteX = pbs.ReadUShort();
  83. texture.PaletteY = pbs.ReadUShort();
  84. pbs.Seek(2, System.IO.SeekOrigin.Current);
  85. texture.NumOfCluts = pbs.ReadUShort();
  86. byte[] buffer = new byte[texture.NumOfCluts * 512];
  87. for (int i = 0; i != buffer.Length; i++)
  88. buffer[i] = pbs.ReadByte();
  89. texture.ClutData = buffer;
  90. pbs.Seek(4, System.IO.SeekOrigin.Current);
  91. texture.ImageOrgX = pbs.ReadUShort();
  92. texture.ImageOrgY = pbs.ReadUShort();
  93. texture.Width = (ushort)(pbs.ReadUShort() * 2);
  94. texture.Height = pbs.ReadUShort();
  95. textureDataPointer = (uint)pbs.Tell();
  96. return;
  97. }
  98. if (_bpp == 16)
  99. {
  100. pbs.Seek(4, System.IO.SeekOrigin.Current);
  101. texture.ImageOrgX = pbs.ReadUShort();
  102. texture.ImageOrgY = pbs.ReadUShort();
  103. texture.Width = pbs.ReadUShort();
  104. texture.Height = pbs.ReadUShort();
  105. textureDataPointer = (uint)pbs.Tell();
  106. return;
  107. }
  108. if (_bpp != 24) return;
  109. pbs.Seek(4, System.IO.SeekOrigin.Current);
  110. texture.ImageOrgX = pbs.ReadUShort();
  111. texture.ImageOrgY = pbs.ReadUShort();
  112. texture.Width = (ushort)(pbs.ReadUShort() / 1.5);
  113. texture.Height = pbs.ReadUShort();
  114. textureDataPointer = (uint)pbs.Tell();
  115. }
  116. public Color[] GetClutColors(Font.ColorID clut) => GetClutColors((ushort)clut);
  117. public Color[] GetClutColors(int clut) => GetClutColors((ushort)clut);
  118. public Color[] GetClutColors(ushort clut)
  119. {
  120. if (clut > texture.NumOfCluts)
  121. throw new Exception("TIM_v2::GetClutColors::given clut is bigger than texture number of cluts");
  122. List<Color> colorPixels = new List<Color>();
  123. if (bpp == 8)
  124. {
  125. pbs.Seek(timOffset + 20 + (512 * clut), System.IO.SeekOrigin.Begin);
  126. for (int i = 0; i < 512 / 2; i++)
  127. {
  128. ushort clutPixel = pbs.ReadUShort();
  129. byte red = (byte)((clutPixel) & 0x1F);
  130. byte green = (byte)((clutPixel >> 5) & 0x1F);
  131. byte blue = (byte)((clutPixel >> 10) & 0x1F);
  132. red = (byte)MathHelper.Clamp((red * bpp), 0, 255);
  133. green = (byte)MathHelper.Clamp((green * bpp), 0, 255);
  134. blue = (byte)MathHelper.Clamp((blue * bpp), 0, 255);
  135. colorPixels.Add(new Color() { Red = red, Green = green, Blue = blue });
  136. }
  137. }
  138. if (bpp == 4)
  139. {
  140. pbs.Seek(timOffset + 20 + (32 * clut), System.IO.SeekOrigin.Begin);
  141. for (int i = 0; i < 16; i++)
  142. {
  143. ushort clutPixel = pbs.ReadUShort();
  144. byte red = (byte)((clutPixel) & 0x1F);
  145. byte green = (byte)((clutPixel >> 5) & 0x1F);
  146. byte blue = (byte)((clutPixel >> 10) & 0x1F);
  147. byte alpha = (byte)(clutPixel >> 11 & 1);
  148. red = (byte)MathHelper.Clamp((red * bpp * 4), 0, 255);
  149. green = (byte)MathHelper.Clamp((green * bpp * 4), 0, 255);
  150. blue = (byte)MathHelper.Clamp((blue * bpp * 4), 0, 255);
  151. colorPixels.Add(new Color { Red = red, Green = green, Blue = blue, Alpha = alpha });
  152. }
  153. }
  154. if (bpp > 8) throw new Exception("TIM that has bpp mode higher than 8 has no clut data!");
  155. return colorPixels.ToArray();
  156. }
  157. public byte[] CreateImageBuffer(Color[] palette = null, bool bIgnoreSize = false)
  158. {
  159. pbs.Seek(textureDataPointer, System.IO.SeekOrigin.Begin);
  160. byte[] buffer = new byte[texture.Width * texture.Height * 4]; //ARGB
  161. if (bpp == 8)
  162. {
  163. if (!bIgnoreSize)
  164. if ((buffer.Length) / 4 != pbs.Length - pbs.Tell())
  165. throw new Exception("TIM_v2::CreateImageBuffer::TIM texture buffer has size incosistency.");
  166. for (int i = 0; i < buffer.Length; i++)
  167. {
  168. byte pixel = pbs.ReadByte();
  169. Color ColoredPixel = palette[pixel];
  170. buffer[i] = ColoredPixel.Red;
  171. buffer[++i] = ColoredPixel.Green;
  172. buffer[++i] = ColoredPixel.Blue;
  173. buffer[++i] = (byte)((ColoredPixel.Red == 0 && ColoredPixel.Green == 0 && ColoredPixel.Blue == 0) ? 0x00 : 0xFF);
  174. }
  175. }
  176. if (bpp == 4)
  177. {
  178. if (!bIgnoreSize)
  179. if ((buffer.Length) / 8 != pbs.Length - pbs.Tell())
  180. throw new Exception("TIM_v2::CreateImageBuffer::TIM texture buffer has size incosistency.");
  181. for (int i = 0; i < buffer.Length; i++)
  182. {
  183. byte pixel = pbs.ReadByte();
  184. Color ColoredPixel = palette[pixel & 0xf];
  185. buffer[i] = ColoredPixel.Red;
  186. buffer[++i] = ColoredPixel.Green;
  187. buffer[++i] = ColoredPixel.Blue;
  188. buffer[++i] = ColoredPixel.Alpha;
  189. ColoredPixel = palette[pixel >> 4];
  190. buffer[++i] = ColoredPixel.Red;
  191. buffer[++i] = ColoredPixel.Green;
  192. buffer[++i] = ColoredPixel.Blue;
  193. buffer[++i] = ColoredPixel.Alpha;
  194. }
  195. }
  196. //Then in bs debug where ReadTexture store for all cluts
  197. //data and then create Texture2D from there. (array of e.g. 15 texture2D)
  198. return buffer;
  199. }
  200. public int GetClutCount => texture.NumOfCluts;
  201. public int GetWidth => texture.Width;
  202. public int GetHeight => texture.Height;
  203. /// <summary>
  204. /// Splash is 640x400 16BPP typical TIM with palette of ggg bbbbb a rrrrr gg
  205. /// </summary>
  206. /// <param name="buffer">raw 16bpp image</param>
  207. /// <returns>Texture2D</returns>
  208. /// <remarks>These files are just the image data with no header and no clut data. So above doesn't work with this.</remarks>
  209. public static Texture2D Overture(byte[] buffer)
  210. {
  211. //var ImageOrgX = BitConverter.ToUInt16(buffer, 0x00);
  212. //var ImageOrgY = BitConverter.ToUInt16(buffer, 0x02);
  213. var Width = BitConverter.ToUInt16(buffer, 0x04);
  214. var Height = BitConverter.ToUInt16(buffer, 0x06);
  215. Texture2D splashTex = new Texture2D(Memory.graphics.GraphicsDevice, Width, Height, false, SurfaceFormat.Color);
  216. lock (splashTex)
  217. {
  218. byte[] rgbBuffer = new byte[splashTex.Width * splashTex.Height * 4];
  219. int innerBufferIndex = 0x08;
  220. for (int i = 0; i < rgbBuffer.Length && innerBufferIndex < buffer.Length; i += 4)
  221. {
  222. if (innerBufferIndex + 1 >= buffer.Length)
  223. {
  224. break;
  225. }
  226. ushort pixel = (ushort)((buffer[innerBufferIndex + 1] << 8) | buffer[innerBufferIndex]);
  227. byte red = (byte)((pixel) & 0x1F);
  228. byte green = (byte)((pixel >> 5) & 0x1F);
  229. byte blue = (byte)((pixel >> 10) & 0x1F);
  230. red = (byte)MathHelper.Clamp((red * 8), 0, 255);
  231. green = (byte)MathHelper.Clamp((green * 8), 0, 255);
  232. blue = (byte)MathHelper.Clamp((blue * 8), 0, 255);
  233. rgbBuffer[i] = red;
  234. rgbBuffer[i + 1] = green;
  235. rgbBuffer[i + 2] = blue;
  236. rgbBuffer[i + 3] = 255;//(byte)(((pixel >> 7) & 0x1) == 1 ? 255 : 0);
  237. innerBufferIndex += 2;
  238. }
  239. splashTex.SetData(rgbBuffer);
  240. }
  241. return splashTex;
  242. }
  243. }
  244. }