TIMOverture.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.IO;
  3. using System.Linq;
  4. namespace OpenVIII
  5. {
  6. /// <summary>
  7. /// TIM data for overture textures.
  8. /// </summary>
  9. /// <remarks>
  10. /// The reason for this is the lack of header, And allows compatibility with other objects that
  11. /// use TIMs.
  12. /// </remarks>
  13. public sealed class TIMOverture : TIM2
  14. {
  15. #region Constructors
  16. public TIMOverture(byte[] buffer, uint offset = 0) => _Init(buffer, offset);
  17. [SuppressMessage("ReSharper", "UnusedMember.Global")]
  18. public TIMOverture(BinaryReader br, uint offset = 0) => _Init(br, offset);
  19. #endregion Constructors
  20. #region Methods
  21. private new void _Init(byte[] buffer, uint offset)
  22. {
  23. this.Buffer = buffer;
  24. using (var br = new BinaryReader(new MemoryStream(buffer)))
  25. {
  26. Init(br, offset);
  27. }
  28. }
  29. private new void _Init(BinaryReader br, uint offset)
  30. {
  31. TrimExcess = true;
  32. br.BaseStream.Seek(offset, SeekOrigin.Begin);
  33. Buffer = br.ReadBytes((int)(br.BaseStream.Length - br.BaseStream.Position));
  34. using (var br2 = new BinaryReader(new MemoryStream(Buffer)))
  35. Init(br2, 0);
  36. }
  37. private new void Init(BinaryReader br, uint offset)
  38. {
  39. BPP = 16;
  40. TIMOffset = offset;
  41. ReadParameters(br);
  42. }
  43. private new void ReadParameters(BinaryReader br)
  44. {
  45. br.BaseStream.Seek(TIMOffset, SeekOrigin.Begin);
  46. Texture.ImageOrgX = br.ReadUInt16();
  47. Texture.ImageOrgY = br.ReadUInt16();
  48. Texture.Width = br.ReadUInt16();
  49. Texture.Height = br.ReadUInt16();
  50. Texture.ImageSize = (uint)(br.BaseStream.Length - TIMOffset);
  51. Texture.ImageDataSize = (int)(br.BaseStream.Length - br.BaseStream.Position);
  52. TextureDataPointer = (uint)br.BaseStream.Position;
  53. br.BaseStream.Seek(TIMOffset, SeekOrigin.Begin);
  54. if (TrimExcess)
  55. Buffer = Buffer.Skip((int)TIMOffset).Take((int)(Texture.ImageDataSize + TextureDataPointer - TIMOffset)).ToArray();
  56. }
  57. #endregion Methods
  58. ///// <summary>
  59. ///// Splash is 640x400 16BPP typical TIM with palette of ggg bbbbb a rrrrr gg
  60. ///// </summary>
  61. ///// <param name="buffer">raw 16bpp image</param>
  62. ///// <returns>Texture2D</returns>
  63. ///// <remarks>
  64. ///// These files are just the image data with no header and no clut data. Tim class doesn't
  65. ///// handle this.
  66. ///// </remarks>
  67. //public static Texture2D Overture(byte[] buffer)
  68. //{
  69. // using (MemoryStream ms = new MemoryStream(buffer))
  70. // using (BinaryReader br = new BinaryReader(ms))
  71. // {
  72. // //var ImageOrgX = BitConverter.ToUInt16(buffer, 0x00);
  73. // //var ImageOrgY = BitConverter.ToUInt16(buffer, 0x02);
  74. // ms.Seek(0x04, SeekOrigin.Begin);
  75. // ushort Width = br.ReadUInt16();
  76. // ushort Height = br.ReadUInt16();
  77. // Texture2D splashTex = new Texture2D(Memory.graphics.GraphicsDevice, Width, Height, false, SurfaceFormat.Color);
  78. // lock (splashTex)
  79. // {
  80. // Color[] rgbBuffer = new Color[Width * Height];
  81. // for (int i = 0; i < rgbBuffer.Length && ms.Position + 2 < ms.Length; i++)
  82. // {
  83. // rgbBuffer[i] = ABGR1555toRGBA32bit(br.ReadUInt16(), true);
  84. // }
  85. // splashTex.SetData(rgbBuffer);
  86. // }
  87. // return splashTex;
  88. // }
  89. //}
  90. }
  91. }