Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AsepriteUtilities.cs 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.IO;
  2. using Unity.Collections;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Aseprite
  5. {
  6. internal static class AsepriteUtilities
  7. {
  8. public static string ReadString(BinaryReader reader)
  9. {
  10. var strLength = reader.ReadUInt16();
  11. var text = "";
  12. for (var m = 0; m < strLength; ++m)
  13. {
  14. var character = (char)reader.ReadByte();
  15. text += character;
  16. }
  17. return text;
  18. }
  19. public static byte[] ReadAndDecompressedData(BinaryReader reader, int dataLength)
  20. {
  21. // 2 bytes of Rfc1950Header that we do not want
  22. var magicBytes = reader.ReadBytes(2);
  23. var compressedData = reader.ReadBytes(dataLength - 2);
  24. var decompressedData = Zlib.Decompress(compressedData);
  25. return decompressedData;
  26. }
  27. public static NativeArray<Color32> GenerateImageData(ushort colorDepth, byte[] imageData, PaletteChunk paletteChunk, byte alphaPaletteEntry)
  28. {
  29. if (colorDepth == 32 || colorDepth == 16)
  30. return ByteToColorArray(imageData, colorDepth);
  31. if (colorDepth == 8)
  32. return ByteToColorArrayUsingPalette(imageData, paletteChunk, alphaPaletteEntry);
  33. return default;
  34. }
  35. static NativeArray<Color32> ByteToColorArray(in byte[] data, ushort colorDepth)
  36. {
  37. NativeArray<Color32> image = default;
  38. if (colorDepth == 32)
  39. {
  40. image = new NativeArray<Color32>(data.Length / 4, Allocator.Persistent);
  41. for (var i = 0; i < image.Length; ++i)
  42. {
  43. var dataIndex = i * 4;
  44. image[i] = new Color32(
  45. data[dataIndex],
  46. data[dataIndex + 1],
  47. data[dataIndex + 2],
  48. data[dataIndex + 3]);
  49. }
  50. }
  51. else if (colorDepth == 16)
  52. {
  53. image = new NativeArray<Color32>(data.Length / 2, Allocator.Persistent);
  54. for (var i = 0; i < image.Length; ++i)
  55. {
  56. var dataIndex = i * 2;
  57. var value = data[dataIndex];
  58. var alpha = data[dataIndex + 1];
  59. image[i] = new Color32(value, value, value, alpha);
  60. }
  61. }
  62. return image;
  63. }
  64. static NativeArray<Color32> ByteToColorArrayUsingPalette(in byte[] data, PaletteChunk paletteChunk, byte alphaPaletteEntry)
  65. {
  66. NativeArray<Color32> image = default;
  67. if (paletteChunk == null)
  68. return default;
  69. var alphaColor = new Color32(0, 0, 0, 0);
  70. image = new NativeArray<Color32>(data.Length, Allocator.Persistent);
  71. for (var i = 0; i < image.Length; ++i)
  72. {
  73. var paletteIndex = data[i];
  74. if (paletteIndex != alphaPaletteEntry)
  75. {
  76. var entry = paletteChunk.entries[paletteIndex];
  77. image[i] = entry.color;
  78. }
  79. else
  80. image[i] = alphaColor;
  81. }
  82. return image;
  83. }
  84. }
  85. }