GifAnimation.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include "GifAnimation.h"
  2. #include <algorithm>
  3. #include <cstring>
  4. #include "ImageDecoder.h"
  5. using namespace WallpaperEngine::Render;
  6. namespace {
  7. bool isGif (const uint8_t* data, size_t size) {
  8. return size >= 6 && data[0] == 'G' && data[1] == 'I' && data[2] == 'F' && data[3] == '8'
  9. && (data[4] == '9' || data[4] == '7') && data[5] == 'a';
  10. }
  11. } // namespace
  12. std::unique_ptr<GifAnimation> GifAnimation::open (const void* data, const size_t size) {
  13. const auto* bytes = static_cast<const uint8_t*> (data);
  14. if (!isGif (bytes, size)) {
  15. return nullptr;
  16. }
  17. auto result = std::unique_ptr<GifAnimation> (new GifAnimation ());
  18. result->m_data.assign (bytes, bytes + size);
  19. return result;
  20. }
  21. bool GifAnimation::canvasSize (const void* data, const size_t size, int& width, int& height) {
  22. const auto* bytes = static_cast<const uint8_t*> (data);
  23. if (!isGif (bytes, size) || size < 10) {
  24. return false;
  25. }
  26. width = bytes[6] | (bytes[7] << 8);
  27. height = bytes[8] | (bytes[9] << 8);
  28. return width > 0 && height > 0;
  29. }
  30. uint8_t GifAnimation::get8 () { return m_cursor < m_data.size () ? m_data[m_cursor++] : 0; }
  31. int GifAnimation::get16le () {
  32. const int low = get8 ();
  33. return low | (get8 () << 8);
  34. }
  35. void GifAnimation::skip (const int count) {
  36. m_cursor = count < 0 ? m_data.size () : std::min (m_data.size (), m_cursor + count);
  37. }
  38. // resourceutil64 sub_180006D90
  39. bool GifAnimation::readHeader () {
  40. if (get8 () != 'G' || get8 () != 'I' || get8 () != 'F' || get8 () != '8') {
  41. return false;
  42. }
  43. const uint8_t version = get8 ();
  44. if ((version != '7' && version != '9') || get8 () != 'a') {
  45. return false;
  46. }
  47. m_width = get16le ();
  48. m_height = get16le ();
  49. m_flags = get8 ();
  50. m_bgIndex = get8 ();
  51. get8 (); // aspect ratio
  52. m_transparent = -1;
  53. // WE only stops at a failed malloc, which overcommit never gives; the canvas becomes a texture, so the GPU limit
  54. // bounds it. loadNext rereads the header while the canvas is empty
  55. if (m_width == 0 || m_height == 0 || exceedsMaxImageSide (m_width, m_height)) {
  56. return false;
  57. }
  58. // unlike stb_image, a background index of 0 or past the global table counts as none
  59. const int count = 2 << (m_flags & 7);
  60. if ((m_flags & 0x80) == 0) {
  61. m_bgIndex = -1;
  62. } else {
  63. readColorTable (m_palette, count, -1);
  64. if (m_bgIndex == 0 || m_bgIndex >= count) {
  65. m_bgIndex = -1;
  66. }
  67. }
  68. return true;
  69. }
  70. // sub_180006C40, entries are stored BGRA
  71. void GifAnimation::readColorTable (uint8_t (*palette)[4], const int count, const int transparent) {
  72. for (int i = 0; i < count; i++) {
  73. palette[i][2] = get8 ();
  74. palette[i][1] = get8 ();
  75. palette[i][0] = get8 ();
  76. palette[i][3] = transparent == i ? 0 : 255;
  77. }
  78. }
  79. // sub_180007230
  80. void GifAnimation::outputCode (const uint16_t code) {
  81. if (m_codes[code].prefix >= 0) {
  82. outputCode (m_codes[code].prefix);
  83. }
  84. if (m_curY >= m_maxY) {
  85. return;
  86. }
  87. const int index = m_curX + m_curY;
  88. uint8_t* pixel = &m_out[index];
  89. m_history[index / 4] = 1;
  90. const uint8_t* color = &m_colorTable[m_codes[code].suffix * 4];
  91. if (color[3] > 128) {
  92. pixel[0] = color[2];
  93. pixel[1] = color[1];
  94. pixel[2] = color[0];
  95. pixel[3] = color[3];
  96. }
  97. m_curX += 4;
  98. if (m_curX >= m_maxX) {
  99. m_curX = m_startX;
  100. m_curY += m_step;
  101. while (m_curY >= m_maxY && m_parse > 0) {
  102. m_step = (1 << m_parse) * m_lineSize;
  103. m_curY = m_startY + (m_step >> 1);
  104. --m_parse;
  105. }
  106. }
  107. }
  108. bool GifAnimation::decodeRaster () {
  109. const uint8_t lzwSize = get8 ();
  110. if (lzwSize > 12) {
  111. return false;
  112. }
  113. const int clear = 1 << lzwSize;
  114. bool first = true;
  115. int codeSize = lzwSize + 1;
  116. int codeMask = (1 << codeSize) - 1;
  117. int bits = 0;
  118. int validBits = 0;
  119. for (int code = 0; code < clear; code++) {
  120. m_codes[code].prefix = -1;
  121. m_codes[code].first = static_cast<uint8_t> (code);
  122. m_codes[code].suffix = static_cast<uint8_t> (code);
  123. }
  124. int avail = clear + 2;
  125. int oldCode = -1;
  126. int length = 0;
  127. for (;;) {
  128. if (validBits < codeSize) {
  129. if (length == 0) {
  130. length = get8 ();
  131. if (length == 0) {
  132. return true;
  133. }
  134. }
  135. --length;
  136. bits |= get8 () << validBits;
  137. validBits += 8;
  138. continue;
  139. }
  140. const int code = bits & codeMask;
  141. bits >>= codeSize;
  142. validBits -= codeSize;
  143. if (code == clear) {
  144. codeSize = lzwSize + 1;
  145. codeMask = (1 << codeSize) - 1;
  146. avail = clear + 2;
  147. oldCode = -1;
  148. first = false;
  149. } else if (code == clear + 1) {
  150. skip (length);
  151. while ((length = get8 ()) > 0) {
  152. skip (length);
  153. }
  154. return true;
  155. } else if (code <= avail) {
  156. if (first) {
  157. return false;
  158. }
  159. if (oldCode >= 0) {
  160. Code& entry = m_codes[avail++];
  161. if (avail > 8192) {
  162. return false;
  163. }
  164. entry.prefix = static_cast<int16_t> (oldCode);
  165. entry.first = m_codes[oldCode].first;
  166. entry.suffix = code == avail ? entry.first : m_codes[code].first;
  167. } else if (code == avail) {
  168. return false;
  169. }
  170. outputCode (static_cast<uint16_t> (code));
  171. if ((avail & codeMask) == 0 && avail <= 0x0FFF) {
  172. codeSize++;
  173. codeMask = (1 << codeSize) - 1;
  174. }
  175. oldCode = code;
  176. } else {
  177. return false;
  178. }
  179. }
  180. }
  181. // sub_180007800, stbi__gif_load_next with a restart flag
  182. GifAnimation::Result GifAnimation::loadNext (const uint8_t* twoBack, const bool restart) {
  183. bool firstFrame = false;
  184. if (m_out.empty () || restart) {
  185. if (!readHeader ()) {
  186. return Result::Error;
  187. }
  188. const size_t count = static_cast<size_t> (m_width) * m_height;
  189. m_out.assign (count * 4, 0);
  190. m_history.assign (count, 0);
  191. firstFrame = true;
  192. } else {
  193. int dispose = (m_eflags >> 2) & 7;
  194. const size_t count = static_cast<size_t> (m_width) * m_height;
  195. if ((m_eflags & 0x1C) == 12 && twoBack == nullptr) {
  196. dispose = 2;
  197. }
  198. if (dispose == 3) {
  199. for (size_t i = 0; i < count; i++) {
  200. if (m_history[i]) {
  201. memcpy (&m_out[i * 4], &twoBack[i * 4], 4);
  202. }
  203. }
  204. } else if (dispose == 2) {
  205. // no saved background like newer stb_image: the palette entry is copied as stored (BGRA)
  206. const uint8_t* fill = nullptr;
  207. if (m_bgIndex >= 0) {
  208. fill = m_transparent < 0 ? m_palette[m_bgIndex] : m_palette[m_transparent];
  209. }
  210. for (size_t i = 0; i < count; i++) {
  211. if (!m_history[i]) {
  212. continue;
  213. }
  214. if (fill == nullptr) {
  215. memset (&m_out[i * 4], 0, 4);
  216. } else {
  217. memcpy (&m_out[i * 4], fill, 4);
  218. }
  219. }
  220. }
  221. }
  222. std::fill (m_history.begin (), m_history.end (), 0);
  223. for (;;) {
  224. if (m_cursor >= m_data.size ()) {
  225. return Result::Error;
  226. }
  227. const uint8_t tag = get8 ();
  228. if (tag == 0x21) {
  229. const uint8_t extension = get8 ();
  230. if (extension == 0xF9) {
  231. const uint8_t length = get8 ();
  232. if (length != 4) {
  233. skip (length);
  234. continue;
  235. }
  236. m_eflags = get8 ();
  237. m_delay = 10 * get16le ();
  238. if (m_transparent >= 0) {
  239. m_palette[m_transparent][3] = 255;
  240. }
  241. if (m_eflags & 1) {
  242. m_transparent = get8 ();
  243. m_palette[m_transparent][3] = 0;
  244. } else {
  245. skip (1);
  246. m_transparent = -1;
  247. }
  248. }
  249. int length;
  250. while ((length = get8 ()) != 0) {
  251. skip (length);
  252. }
  253. continue;
  254. }
  255. if (tag == 0x3B) {
  256. return Result::Trailer;
  257. }
  258. if (tag != 0x2C) {
  259. return Result::Error;
  260. }
  261. const int x = get16le ();
  262. const int y = get16le ();
  263. const int w = get16le ();
  264. const int h = get16le ();
  265. if (x + w > m_width || y + h > m_height) {
  266. return Result::Error;
  267. }
  268. m_lineSize = m_width * 4;
  269. m_startX = x * 4;
  270. m_startY = y * m_lineSize;
  271. m_maxX = m_startX + w * 4;
  272. m_maxY = m_startY + h * m_lineSize;
  273. m_curX = m_startX;
  274. m_curY = w == 0 ? m_maxY : m_startY;
  275. const int localFlags = get8 ();
  276. if (localFlags & 0x40) {
  277. m_step = 8 * m_lineSize;
  278. m_parse = 3;
  279. } else {
  280. m_step = m_lineSize;
  281. m_parse = 0;
  282. }
  283. if (localFlags & 0x80) {
  284. readColorTable (m_localPalette, 2 << (localFlags & 7), m_eflags & 1 ? m_transparent : -1);
  285. m_colorTable = &m_localPalette[0][0];
  286. } else if (m_flags & 0x80) {
  287. m_colorTable = &m_palette[0][0];
  288. } else {
  289. return Result::Error;
  290. }
  291. if (!decodeRaster ()) {
  292. return Result::Error;
  293. }
  294. // first frame without transparency: pixels it doesn't draw get the background entry, alpha as stored
  295. if (firstFrame && m_bgIndex >= 0 && (m_eflags & 1) == 0) {
  296. for (size_t i = 0; i < m_history.size (); i++) {
  297. if (!m_history[i]) {
  298. memcpy (&m_out[i * 4], m_palette[m_bgIndex], 4);
  299. }
  300. }
  301. }
  302. return Result::Frame;
  303. }
  304. }
  305. bool GifAnimation::advance (const float dt) {
  306. if (!m_first && m_timer > 0.0f) {
  307. m_timer -= dt;
  308. if (m_timer > 0.0f) {
  309. return false;
  310. }
  311. }
  312. m_first = false;
  313. const uint8_t* twoBack = m_twoBack.empty () ? nullptr : m_twoBack.data ();
  314. Result result = loadNext (twoBack, false);
  315. if (result == Result::Trailer) {
  316. m_cursor = 0;
  317. result = loadNext (twoBack, true);
  318. }
  319. if (result != Result::Frame) {
  320. return false;
  321. }
  322. const bool sameSize = m_lastWidth == 0 || (m_lastWidth == m_width && m_lastHeight == m_height);
  323. m_lastWidth = m_width;
  324. m_lastHeight = m_height;
  325. m_timer += static_cast<float> (m_delay) / 1000.0f;
  326. const int dispose = m_eflags & 0x1C;
  327. if (dispose == 0 || dispose == 4) {
  328. if (m_twoBack.empty ()) {
  329. m_twoBack.resize (m_out.size ());
  330. }
  331. if (sameSize) {
  332. memcpy (m_twoBack.data (), m_out.data (), m_out.size ());
  333. }
  334. }
  335. return true;
  336. }