Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CellChunk.cs 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System.Collections.ObjectModel;
  2. using System.IO;
  3. using Unity.Collections;
  4. using UnityEngine;
  5. using UnityEngine.Assertions;
  6. namespace UnityEditor.U2D.Aseprite
  7. {
  8. /// <summary>
  9. /// Aseprite cell types.
  10. /// </summary>
  11. public enum CellTypes
  12. {
  13. RawImage = 0,
  14. LinkedCell = 1,
  15. CompressedImage = 2,
  16. CompressedTileMap = 3
  17. }
  18. /// <summary>
  19. /// Parsed representation of an Aseprite Cell chunk.
  20. /// </summary>
  21. public class CellChunk : BaseChunk
  22. {
  23. public override ChunkTypes chunkType => ChunkTypes.Cell;
  24. internal CellChunk(uint chunkSize, ushort colorDepth, ReadOnlyCollection<PaletteEntry> paletteEntries, byte alphaPaletteEntry) : base(chunkSize)
  25. {
  26. m_ColorDepth = colorDepth;
  27. m_PaletteEntries = paletteEntries;
  28. m_AlphaPaletteEntry = alphaPaletteEntry;
  29. }
  30. readonly ushort m_ColorDepth;
  31. readonly ReadOnlyCollection<PaletteEntry> m_PaletteEntries;
  32. readonly byte m_AlphaPaletteEntry;
  33. /// <summary>
  34. /// The layer index is a number to identify a layer in the sprite.
  35. /// Layers are numbered in the same order as Layer Chunks appear in the file.
  36. /// </summary>
  37. public ushort layerIndex { get; private set; }
  38. /// <summary>
  39. /// The Cell's X position on the canvas.
  40. /// </summary>
  41. public short posX { get; private set; }
  42. /// <summary>
  43. /// The Cell's Y position on the canvas.
  44. /// </summary>
  45. public short posY { get; private set; }
  46. /// <summary>
  47. /// Opacity level of the cell (0 = transparent, 255 = opaque).
  48. /// </summary>
  49. public byte opacity { get; private set; }
  50. /// <summary>
  51. /// The type of cell.
  52. /// </summary>
  53. public CellTypes cellType { get; private set; }
  54. /// <summary>
  55. /// A cell's draw order. Higher number means towards the front.
  56. /// </summary>
  57. internal short zIndex { get; private set; }
  58. /// <summary>
  59. /// The frame index of the cell (Only available for Linked Cells).
  60. /// </summary>
  61. public int linkedToFrame { get; private set; } = -1;
  62. /// <summary>
  63. /// The width of the cell in pixels.
  64. /// </summary>
  65. public ushort width { get; private set; }
  66. /// <summary>
  67. /// The height of the cell in pixels.
  68. /// </summary>
  69. public ushort height { get; private set; }
  70. /// <summary>
  71. /// The image data of the cell.
  72. /// </summary>
  73. public NativeArray<Color32> image { get; private set; }
  74. /// <summary>
  75. /// User data associated with the cell.
  76. /// </summary>
  77. public UserDataChunk dataChunk { get; set; }
  78. protected override void InternalRead(BinaryReader reader)
  79. {
  80. layerIndex = reader.ReadUInt16();
  81. posX = reader.ReadInt16();
  82. posY = reader.ReadInt16();
  83. opacity = reader.ReadByte();
  84. cellType = (CellTypes)reader.ReadUInt16();
  85. zIndex = reader.ReadInt16();
  86. // Not in use bytes
  87. for (var i = 0; i < 5; ++i)
  88. {
  89. var miscVal = reader.ReadByte();
  90. Assert.IsTrue(miscVal == 0);
  91. }
  92. if (cellType == CellTypes.RawImage)
  93. {
  94. width = reader.ReadUInt16();
  95. height = reader.ReadUInt16();
  96. byte[] imageData = null;
  97. if (m_ColorDepth == 32)
  98. imageData = reader.ReadBytes(width * height * 4);
  99. else if (m_ColorDepth == 16)
  100. imageData = reader.ReadBytes(width * height * 2);
  101. else if (m_ColorDepth == 8)
  102. imageData = reader.ReadBytes(width * height);
  103. if (imageData != null)
  104. image = AsepriteUtilities.GenerateImageData(m_ColorDepth, imageData, m_PaletteEntries, m_AlphaPaletteEntry);
  105. }
  106. else if (cellType == CellTypes.LinkedCell)
  107. {
  108. linkedToFrame = reader.ReadUInt16();
  109. }
  110. else if (cellType == CellTypes.CompressedImage)
  111. {
  112. width = reader.ReadUInt16();
  113. height = reader.ReadUInt16();
  114. var dataSize = (int)m_ChunkSize - ChunkHeader.stride - 20;
  115. var decompressedData = AsepriteUtilities.ReadAndDecompressedData(reader, dataSize);
  116. image = AsepriteUtilities.GenerateImageData(m_ColorDepth, decompressedData, m_PaletteEntries, m_AlphaPaletteEntry);
  117. }
  118. else if (cellType == CellTypes.CompressedTileMap) // Not implemented yet.
  119. {
  120. width = reader.ReadUInt16();
  121. height = reader.ReadUInt16();
  122. var bitsPerTile = reader.ReadUInt16();
  123. var tileIdMask = reader.ReadUInt32();
  124. var xFlipMask = reader.ReadUInt32();
  125. var yFlipMask = reader.ReadUInt32();
  126. var rotation90Mask = reader.ReadUInt32();
  127. // Not in use bytes
  128. for (var i = 0; i < 10; ++i)
  129. reader.ReadByte();
  130. var dataSize = (int)m_ChunkSize - ChunkHeader.stride - 48;
  131. var decompressedData = AsepriteUtilities.ReadAndDecompressedData(reader, dataSize);
  132. var bytesPerTile = bitsPerTile / 8;
  133. var noOfTiles = decompressedData.Length / bytesPerTile;
  134. using var memoryStream = new MemoryStream(decompressedData);
  135. using var binaryReader = new BinaryReader(memoryStream);
  136. for (var i = 0; i < noOfTiles; ++i)
  137. {
  138. uint tileIndex = 0;
  139. if (bitsPerTile == 32)
  140. tileIndex = binaryReader.ReadUInt32();
  141. else if (bitsPerTile == 16)
  142. tileIndex = binaryReader.ReadUInt16();
  143. else if (bitsPerTile == 8)
  144. tileIndex = binaryReader.ReadByte();
  145. }
  146. }
  147. }
  148. public override void Dispose()
  149. {
  150. image.DisposeIfCreated();
  151. }
  152. }
  153. }