Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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