2
0

PMP.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System.IO;
  4. using System.Linq;
  5. namespace OpenVIII.Fields
  6. {
  7. /// <summary>
  8. /// Particle Texture
  9. /// </summary>
  10. /// <see cref="http://wiki.ffrtt.ru/index.php?title=FF8/FileFormat_PMP"/>
  11. public sealed class PMP : Texture_Base
  12. {
  13. #region Fields
  14. private byte[] _buffer;
  15. private Cluts _clut;
  16. private int _height;
  17. private int _width;
  18. #endregion Fields
  19. #region Constructors
  20. public PMP(byte[] pmpB) => Load(pmpB);
  21. #endregion Constructors
  22. #region Properties
  23. public override byte GetBytesPerPixel => 4;
  24. public override int GetClutCount => 16;
  25. public override int GetClutSize => 16;
  26. public override int GetColorsCountPerPalette => 16;
  27. public override int GetHeight => _height;
  28. public override int GetOrigX => 0;
  29. public override int GetOrigY => 0;
  30. public override int GetWidth => _width;
  31. public byte[] Unknown { get; set; }
  32. #endregion Properties
  33. #region Methods
  34. public override void ForceSetClutColors(ushort newNumOfColors) => throw new System.NotImplementedException();
  35. public override void ForceSetClutCount(ushort newClut) => throw new System.NotImplementedException();
  36. public override Color[] GetClutColors(ushort clut) => this._clut[(byte)clut];
  37. public override Texture2D GetTexture() => GetTexture(0);
  38. public override Texture2D GetTexture(Color[] colors)
  39. {
  40. var tex = new Texture2D(Memory.Graphics.GraphicsDevice, _width, _height);
  41. var textureBuffer = new TextureBuffer(_width, _height, false);
  42. var i = 0;
  43. foreach (var b in _buffer)
  44. textureBuffer[i++] = colors[b];
  45. textureBuffer.SetData(tex);
  46. return tex;
  47. }
  48. public override Texture2D GetTexture(ushort clut) => GetTexture(GetClutColors(clut));
  49. public override void Load(byte[] buffer, uint offset = 0)
  50. {
  51. if (buffer.Length - offset <= 4) return;
  52. _clut = new Cluts();
  53. MemoryStream ms;
  54. using (var br = new BinaryReader(ms = new MemoryStream(buffer)))
  55. {
  56. ms.Seek(offset, SeekOrigin.Begin);//unknown
  57. Unknown = br.ReadBytes(4);
  58. foreach (var i in Enumerable.Range(0, 16))
  59. {
  60. var colors = Enumerable.Range(0, 16).Select(_ => ABGR1555toRGBA32bit(br.ReadUInt16()))
  61. .ToArray();
  62. _clut.Add((byte)i, colors);
  63. }
  64. var size = ms.Length - ms.Position;
  65. _height = checked((int)(size / 128));
  66. _width = checked((int)(size / _height));
  67. _buffer = br.ReadBytes(checked((int)size));
  68. }
  69. }
  70. public override void Save(string path) => throw new System.NotImplementedException();
  71. public override void SaveCLUT(string path) => _clut.Save(path);
  72. #endregion Methods
  73. }
  74. }