image-file.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /******************************************************************************
  2. Copyright (C) 2016 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "image-file.h"
  15. #include "../util/base.h"
  16. #include "../util/platform.h"
  17. #define blog(level, format, ...) \
  18. blog(level, "%s: " format, __FUNCTION__, __VA_ARGS__)
  19. static void *bi_def_bitmap_create(int width, int height)
  20. {
  21. return bmalloc(width * height * 4);
  22. }
  23. static void bi_def_bitmap_set_opaque(void *bitmap, bool opaque)
  24. {
  25. UNUSED_PARAMETER(bitmap);
  26. UNUSED_PARAMETER(opaque);
  27. }
  28. static bool bi_def_bitmap_test_opaque(void *bitmap)
  29. {
  30. UNUSED_PARAMETER(bitmap);
  31. return false;
  32. }
  33. static unsigned char *bi_def_bitmap_get_buffer(void *bitmap)
  34. {
  35. return (unsigned char*)bitmap;
  36. }
  37. static void bi_def_bitmap_destroy(void *bitmap)
  38. {
  39. bfree(bitmap);
  40. }
  41. static void bi_def_bitmap_modified(void *bitmap)
  42. {
  43. UNUSED_PARAMETER(bitmap);
  44. }
  45. static inline int get_full_decoded_gif_size(gs_image_file_t *image)
  46. {
  47. return image->gif.width * image->gif.height * 4 * image->gif.frame_count;
  48. }
  49. static bool init_animated_gif(gs_image_file_t *image, const char *path)
  50. {
  51. bool is_animated_gif = true;
  52. gif_result result;
  53. uint64_t max_size;
  54. size_t size;
  55. FILE *file;
  56. image->bitmap_callbacks.bitmap_create = bi_def_bitmap_create;
  57. image->bitmap_callbacks.bitmap_destroy = bi_def_bitmap_destroy;
  58. image->bitmap_callbacks.bitmap_get_buffer = bi_def_bitmap_get_buffer;
  59. image->bitmap_callbacks.bitmap_modified = bi_def_bitmap_modified;
  60. image->bitmap_callbacks.bitmap_set_opaque = bi_def_bitmap_set_opaque;
  61. image->bitmap_callbacks.bitmap_test_opaque = bi_def_bitmap_test_opaque;
  62. gif_create(&image->gif, &image->bitmap_callbacks);
  63. file = os_fopen(path, "rb");
  64. if (!file) {
  65. blog(LOG_WARNING, "Failed to open file '%s'", path);
  66. goto fail;
  67. }
  68. fseek(file, 0, SEEK_END);
  69. size = (size_t)os_ftelli64(file);
  70. fseek(file, 0, SEEK_SET);
  71. image->gif_data = bmalloc(size);
  72. fread(image->gif_data, 1, size, file);
  73. do {
  74. result = gif_initialise(&image->gif, size, image->gif_data);
  75. if (result < 0) {
  76. blog(LOG_WARNING, "Failed to initialize gif '%s', "
  77. "possible file corruption", path);
  78. goto fail;
  79. }
  80. } while (result != GIF_OK);
  81. if (image->gif.width > 4096 || image->gif.height > 4096) {
  82. blog(LOG_WARNING, "Bad texture dimensions (%dx%d) in '%s'",
  83. image->gif.width, image->gif.height, path);
  84. goto fail;
  85. }
  86. max_size = (uint64_t)image->gif.width * (uint64_t)image->gif.height *
  87. (uint64_t)image->gif.frame_count * 4LLU;
  88. if ((uint64_t)get_full_decoded_gif_size(image) != max_size) {
  89. blog(LOG_WARNING, "Gif '%s' overflowed maximum pointer size",
  90. path);
  91. goto fail;
  92. }
  93. image->is_animated_gif = (image->gif.frame_count > 1 && result >= 0);
  94. if (image->is_animated_gif) {
  95. gif_decode_frame(&image->gif, 0);
  96. image->animation_frame_cache = bzalloc(
  97. image->gif.frame_count * sizeof(uint8_t*));
  98. image->animation_frame_data = bzalloc(
  99. get_full_decoded_gif_size(image));
  100. for (unsigned int i = 0; i < image->gif.frame_count; i++) {
  101. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  102. blog(LOG_WARNING, "Couldn't decode frame %u "
  103. "of '%s'", i, path);
  104. }
  105. gif_decode_frame(&image->gif, 0);
  106. image->cx = (uint32_t)image->gif.width;
  107. image->cy = (uint32_t)image->gif.height;
  108. image->format = GS_RGBA;
  109. } else {
  110. gif_finalise(&image->gif);
  111. bfree(image->gif_data);
  112. image->gif_data = NULL;
  113. is_animated_gif = false;
  114. goto not_animated;
  115. }
  116. image->loaded = true;
  117. fail:
  118. if (!image->loaded)
  119. gs_image_file_free(image);
  120. not_animated:
  121. if (file)
  122. fclose(file);
  123. return is_animated_gif;
  124. }
  125. void gs_image_file_init(gs_image_file_t *image, const char *file)
  126. {
  127. size_t len;
  128. if (!image)
  129. return;
  130. memset(image, 0, sizeof(*image));
  131. if (!file)
  132. return;
  133. len = strlen(file);
  134. if (len > 4 && strcmp(file + len - 4, ".gif") == 0) {
  135. if (init_animated_gif(image, file))
  136. return;
  137. }
  138. image->texture_data = gs_create_texture_file_data(file,
  139. &image->format, &image->cx, &image->cy);
  140. image->loaded = !!image->texture_data;
  141. if (!image->loaded) {
  142. blog(LOG_WARNING, "Failed to load file '%s'", file);
  143. gs_image_file_free(image);
  144. }
  145. }
  146. void gs_image_file_free(gs_image_file_t *image)
  147. {
  148. if (!image)
  149. return;
  150. if (image->loaded) {
  151. if (image->is_animated_gif) {
  152. gif_finalise(&image->gif);
  153. bfree(image->animation_frame_cache);
  154. bfree(image->animation_frame_data);
  155. }
  156. gs_texture_destroy(image->texture);
  157. }
  158. bfree(image->texture_data);
  159. bfree(image->gif_data);
  160. memset(image, 0, sizeof(*image));
  161. }
  162. void gs_image_file_init_texture(gs_image_file_t *image)
  163. {
  164. if (!image->loaded)
  165. return;
  166. if (image->is_animated_gif) {
  167. image->texture = gs_texture_create(
  168. image->cx, image->cy, image->format, 1,
  169. (const uint8_t**)&image->gif.frame_image,
  170. GS_DYNAMIC);
  171. } else {
  172. image->texture = gs_texture_create(
  173. image->cx, image->cy, image->format, 1,
  174. (const uint8_t**)&image->texture_data, 0);
  175. bfree(image->texture_data);
  176. image->texture_data = NULL;
  177. }
  178. }
  179. static inline uint64_t get_time(gs_image_file_t *image, int i)
  180. {
  181. uint64_t val = (uint64_t)image->gif.frames[i].frame_delay * 10000000ULL;
  182. if (!val)
  183. val = 100000000;
  184. return val;
  185. }
  186. static inline int calculate_new_frame(gs_image_file_t *image,
  187. uint64_t elapsed_time_ns, int loops)
  188. {
  189. int new_frame = image->cur_frame;
  190. image->cur_time += elapsed_time_ns;
  191. for (;;) {
  192. uint64_t t = get_time(image, new_frame);
  193. if (image->cur_time <= t)
  194. break;
  195. image->cur_time -= t;
  196. if ((unsigned int)++new_frame == image->gif.frame_count) {
  197. if (!loops || ++image->cur_loop < loops) {
  198. new_frame = 0;
  199. } else if (image->cur_loop == loops) {
  200. new_frame--;
  201. break;
  202. }
  203. }
  204. }
  205. return new_frame;
  206. }
  207. static void decode_new_frame(gs_image_file_t *image, int new_frame)
  208. {
  209. if (!image->animation_frame_cache[new_frame]) {
  210. int last_frame;
  211. /* if looped, decode frame 0 */
  212. last_frame = (new_frame < image->last_decoded_frame) ?
  213. 0 : image->last_decoded_frame + 1;
  214. /* decode missed frames */
  215. for (int i = last_frame; i < new_frame; i++) {
  216. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  217. return;
  218. }
  219. /* decode actual desired frame */
  220. if (gif_decode_frame(&image->gif, new_frame) == GIF_OK) {
  221. size_t pos = new_frame * image->gif.width *
  222. image->gif.height * 4;
  223. image->animation_frame_cache[new_frame] =
  224. image->animation_frame_data + pos;
  225. memcpy(image->animation_frame_cache[new_frame],
  226. image->gif.frame_image,
  227. image->gif.width *
  228. image->gif.height * 4);
  229. image->last_decoded_frame = new_frame;
  230. }
  231. }
  232. image->cur_frame = new_frame;
  233. }
  234. bool gs_image_file_tick(gs_image_file_t *image, uint64_t elapsed_time_ns)
  235. {
  236. int loops;
  237. if (!image->is_animated_gif || !image->loaded)
  238. return false;
  239. loops = image->gif.loop_count;
  240. if (loops >= 0xFFFF)
  241. loops = 0;
  242. if (!loops || image->cur_loop < loops) {
  243. int new_frame = calculate_new_frame(image, elapsed_time_ns,
  244. loops);
  245. if (new_frame != image->cur_frame) {
  246. decode_new_frame(image, new_frame);
  247. return true;
  248. }
  249. }
  250. return false;
  251. }
  252. void gs_image_file_update_texture(gs_image_file_t *image)
  253. {
  254. if (!image->is_animated_gif || !image->loaded)
  255. return;
  256. if (image->animation_frame_cache[image->cur_frame]) {
  257. gs_texture_set_image(image->texture,
  258. image->animation_frame_cache[image->cur_frame],
  259. image->gif.width * 4, false);
  260. }
  261. }