UniGifConst.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. UniGif
  3. Copyright (c) 2015 WestHillApps (Hironari Nishioka)
  4. This software is released under the MIT License.
  5. http://opensource.org/licenses/mit-license.php
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using UnityEngine;
  11. public static partial class UniGif
  12. {
  13. /// <summary>
  14. /// Gif Texture
  15. /// </summary>
  16. public class GifTexture
  17. {
  18. // Texture
  19. public Texture2D m_texture2d;
  20. // Delay time until the next texture.
  21. public float m_delaySec;
  22. public GifTexture(Texture2D texture2d, float delaySec)
  23. {
  24. m_texture2d = texture2d;
  25. m_delaySec = delaySec;
  26. }
  27. }
  28. /// <summary>
  29. /// GIF Data Format
  30. /// </summary>
  31. private struct GifData
  32. {
  33. // Signature
  34. public byte m_sig0, m_sig1, m_sig2;
  35. // Version
  36. public byte m_ver0, m_ver1, m_ver2;
  37. // Logical Screen Width
  38. public ushort m_logicalScreenWidth;
  39. // Logical Screen Height
  40. public ushort m_logicalScreenHeight;
  41. // Global Color Table Flag
  42. public bool m_globalColorTableFlag;
  43. // Color Resolution
  44. public int m_colorResolution;
  45. // Sort Flag
  46. public bool m_sortFlag;
  47. // Size of Global Color Table
  48. public int m_sizeOfGlobalColorTable;
  49. // Background Color Index
  50. public byte m_bgColorIndex;
  51. // Pixel Aspect Ratio
  52. public byte m_pixelAspectRatio;
  53. // Global Color Table
  54. public List<byte[]> m_globalColorTable;
  55. // ImageBlock
  56. public List<ImageBlock> m_imageBlockList;
  57. // GraphicControlExtension
  58. public List<GraphicControlExtension> m_graphicCtrlExList;
  59. // Comment Extension
  60. public List<CommentExtension> m_commentExList;
  61. // Plain Text Extension
  62. public List<PlainTextExtension> m_plainTextExList;
  63. // Application Extension
  64. public ApplicationExtension m_appEx;
  65. // Trailer
  66. public byte m_trailer;
  67. public string signature
  68. {
  69. get
  70. {
  71. char[] c = { (char)m_sig0, (char)m_sig1, (char)m_sig2 };
  72. return new string(c);
  73. }
  74. }
  75. public string version
  76. {
  77. get
  78. {
  79. char[] c = { (char)m_ver0, (char)m_ver1, (char)m_ver2 };
  80. return new string(c);
  81. }
  82. }
  83. public void Dump()
  84. {
  85. Debug.Log("GIF Type: " + signature + "-" + version);
  86. Debug.Log("Image Size: " + m_logicalScreenWidth + "x" + m_logicalScreenHeight);
  87. Debug.Log("Animation Image Count: " + m_imageBlockList.Count);
  88. Debug.Log("Animation Loop Count (0 is infinite): " + m_appEx.loopCount);
  89. if (m_graphicCtrlExList != null && m_graphicCtrlExList.Count > 0)
  90. {
  91. var sb = new StringBuilder("Animation Delay Time (1/100sec)");
  92. for (int i = 0; i < m_graphicCtrlExList.Count; i++)
  93. {
  94. sb.Append(", ");
  95. sb.Append(m_graphicCtrlExList[i].m_delayTime);
  96. }
  97. Debug.Log(sb.ToString());
  98. }
  99. Debug.Log("Application Identifier: " + m_appEx.applicationIdentifier);
  100. Debug.Log("Application Authentication Code: " + m_appEx.applicationAuthenticationCode);
  101. }
  102. }
  103. /// <summary>
  104. /// Image Block
  105. /// </summary>
  106. private struct ImageBlock
  107. {
  108. // Image Separator
  109. public byte m_imageSeparator;
  110. // Image Left Position
  111. public ushort m_imageLeftPosition;
  112. // Image Top Position
  113. public ushort m_imageTopPosition;
  114. // Image Width
  115. public ushort m_imageWidth;
  116. // Image Height
  117. public ushort m_imageHeight;
  118. // Local Color Table Flag
  119. public bool m_localColorTableFlag;
  120. // Interlace Flag
  121. public bool m_interlaceFlag;
  122. // Sort Flag
  123. public bool m_sortFlag;
  124. // Size of Local Color Table
  125. public int m_sizeOfLocalColorTable;
  126. // Local Color Table
  127. public List<byte[]> m_localColorTable;
  128. // LZW Minimum Code Size
  129. public byte m_lzwMinimumCodeSize;
  130. // Block Size & Image Data List
  131. public List<ImageDataBlock> m_imageDataList;
  132. public struct ImageDataBlock
  133. {
  134. // Block Size
  135. public byte m_blockSize;
  136. // Image Data
  137. public byte[] m_imageData;
  138. }
  139. }
  140. /// <summary>
  141. /// Graphic Control Extension
  142. /// </summary>
  143. private struct GraphicControlExtension
  144. {
  145. // Extension Introducer
  146. public byte m_extensionIntroducer;
  147. // Graphic Control Label
  148. public byte m_graphicControlLabel;
  149. // Block Size
  150. public byte m_blockSize;
  151. // Disposal Mothod
  152. public ushort m_disposalMethod;
  153. // Transparent Color Flag
  154. public bool m_transparentColorFlag;
  155. // Delay Time
  156. public ushort m_delayTime;
  157. // Transparent Color Index
  158. public byte m_transparentColorIndex;
  159. // Block Terminator
  160. public byte m_blockTerminator;
  161. }
  162. /// <summary>
  163. /// Comment Extension
  164. /// </summary>
  165. private struct CommentExtension
  166. {
  167. // Extension Introducer
  168. public byte m_extensionIntroducer;
  169. // Comment Label
  170. public byte m_commentLabel;
  171. // Block Size & Comment Data List
  172. public List<CommentDataBlock> m_commentDataList;
  173. public struct CommentDataBlock
  174. {
  175. // Block Size
  176. public byte m_blockSize;
  177. // Image Data
  178. public byte[] m_commentData;
  179. }
  180. }
  181. /// <summary>
  182. /// Plain Text Extension
  183. /// </summary>
  184. private struct PlainTextExtension
  185. {
  186. // Extension Introducer
  187. public byte m_extensionIntroducer;
  188. // Plain Text Label
  189. public byte m_plainTextLabel;
  190. // Block Size
  191. public byte m_blockSize;
  192. // Block Size & Plain Text Data List
  193. public List<PlainTextDataBlock> m_plainTextDataList;
  194. public struct PlainTextDataBlock
  195. {
  196. // Block Size
  197. public byte m_blockSize;
  198. // Plain Text Data
  199. public byte[] m_plainTextData;
  200. }
  201. }
  202. /// <summary>
  203. /// Application Extension
  204. /// </summary>
  205. private struct ApplicationExtension
  206. {
  207. // Extension Introducer
  208. public byte m_extensionIntroducer;
  209. // Extension Label
  210. public byte m_extensionLabel;
  211. // Block Size
  212. public byte m_blockSize;
  213. // Application Identifier
  214. public byte m_appId1, m_appId2, m_appId3, m_appId4, m_appId5, m_appId6, m_appId7, m_appId8;
  215. // Application Authentication Code
  216. public byte m_appAuthCode1, m_appAuthCode2, m_appAuthCode3;
  217. // Block Size & Application Data List
  218. public List<ApplicationDataBlock> m_appDataList;
  219. public struct ApplicationDataBlock
  220. {
  221. // Block Size
  222. public byte m_blockSize;
  223. // Application Data
  224. public byte[] m_applicationData;
  225. }
  226. public string applicationIdentifier
  227. {
  228. get
  229. {
  230. char[] c = { (char)m_appId1, (char)m_appId2, (char)m_appId3, (char)m_appId4, (char)m_appId5, (char)m_appId6, (char)m_appId7, (char)m_appId8 };
  231. return new string(c);
  232. }
  233. }
  234. public string applicationAuthenticationCode
  235. {
  236. get
  237. {
  238. char[] c = { (char)m_appAuthCode1, (char)m_appAuthCode2, (char)m_appAuthCode3 };
  239. return new string(c);
  240. }
  241. }
  242. public int loopCount
  243. {
  244. get
  245. {
  246. if (m_appDataList == null || m_appDataList.Count < 1 ||
  247. m_appDataList[0].m_applicationData.Length < 3 ||
  248. m_appDataList[0].m_applicationData[0] != 0x01)
  249. {
  250. return 0;
  251. }
  252. return BitConverter.ToUInt16(m_appDataList[0].m_applicationData, 1);
  253. }
  254. }
  255. }
  256. }