Ingen beskrivning
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.

AsepriteUtilities.cs 3.4KB

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