image-file.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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, size_read;
  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. size_read = fread(image->gif_data, 1, size, file);
  73. if (size_read != size) {
  74. blog(LOG_WARNING, "Failed to fully read gif file '%s'.", path);
  75. goto fail;
  76. }
  77. do {
  78. result = gif_initialise(&image->gif, size, image->gif_data);
  79. if (result < 0) {
  80. blog(LOG_WARNING, "Failed to initialize gif '%s', "
  81. "possible file corruption", path);
  82. goto fail;
  83. }
  84. } while (result != GIF_OK);
  85. if (image->gif.width > 4096 || image->gif.height > 4096) {
  86. blog(LOG_WARNING, "Bad texture dimensions (%dx%d) in '%s'",
  87. image->gif.width, image->gif.height, path);
  88. goto fail;
  89. }
  90. max_size = (uint64_t)image->gif.width * (uint64_t)image->gif.height *
  91. (uint64_t)image->gif.frame_count * 4LLU;
  92. if ((uint64_t)get_full_decoded_gif_size(image) != max_size) {
  93. blog(LOG_WARNING, "Gif '%s' overflowed maximum pointer size",
  94. path);
  95. goto fail;
  96. }
  97. image->is_animated_gif = (image->gif.frame_count > 1 && result >= 0);
  98. if (image->is_animated_gif) {
  99. gif_decode_frame(&image->gif, 0);
  100. image->animation_frame_cache = bzalloc(
  101. image->gif.frame_count * sizeof(uint8_t*));
  102. image->animation_frame_data = bzalloc(
  103. get_full_decoded_gif_size(image));
  104. for (unsigned int i = 0; i < image->gif.frame_count; i++) {
  105. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  106. blog(LOG_WARNING, "Couldn't decode frame %u "
  107. "of '%s'", i, path);
  108. }
  109. gif_decode_frame(&image->gif, 0);
  110. image->cx = (uint32_t)image->gif.width;
  111. image->cy = (uint32_t)image->gif.height;
  112. image->format = GS_RGBA;
  113. } else {
  114. gif_finalise(&image->gif);
  115. bfree(image->gif_data);
  116. image->gif_data = NULL;
  117. is_animated_gif = false;
  118. goto not_animated;
  119. }
  120. image->loaded = true;
  121. fail:
  122. if (!image->loaded)
  123. gs_image_file_free(image);
  124. not_animated:
  125. if (file)
  126. fclose(file);
  127. return is_animated_gif;
  128. }
  129. void gs_image_file_init(gs_image_file_t *image, const char *file)
  130. {
  131. size_t len;
  132. if (!image)
  133. return;
  134. memset(image, 0, sizeof(*image));
  135. if (!file)
  136. return;
  137. len = strlen(file);
  138. if (len > 4 && strcmp(file + len - 4, ".gif") == 0) {
  139. if (init_animated_gif(image, file))
  140. return;
  141. }
  142. image->texture_data = gs_create_texture_file_data(file,
  143. &image->format, &image->cx, &image->cy);
  144. image->loaded = !!image->texture_data;
  145. if (!image->loaded) {
  146. blog(LOG_WARNING, "Failed to load file '%s'", file);
  147. gs_image_file_free(image);
  148. }
  149. }
  150. void gs_image_file_free(gs_image_file_t *image)
  151. {
  152. if (!image)
  153. return;
  154. if (image->loaded) {
  155. if (image->is_animated_gif) {
  156. gif_finalise(&image->gif);
  157. bfree(image->animation_frame_cache);
  158. bfree(image->animation_frame_data);
  159. }
  160. gs_texture_destroy(image->texture);
  161. }
  162. bfree(image->texture_data);
  163. bfree(image->gif_data);
  164. memset(image, 0, sizeof(*image));
  165. }
  166. void gs_image_file_init_texture(gs_image_file_t *image)
  167. {
  168. if (!image->loaded)
  169. return;
  170. if (image->is_animated_gif) {
  171. image->texture = gs_texture_create(
  172. image->cx, image->cy, image->format, 1,
  173. (const uint8_t**)&image->gif.frame_image,
  174. GS_DYNAMIC);
  175. } else {
  176. image->texture = gs_texture_create(
  177. image->cx, image->cy, image->format, 1,
  178. (const uint8_t**)&image->texture_data, 0);
  179. bfree(image->texture_data);
  180. image->texture_data = NULL;
  181. }
  182. }
  183. static inline uint64_t get_time(gs_image_file_t *image, int i)
  184. {
  185. uint64_t val = (uint64_t)image->gif.frames[i].frame_delay * 10000000ULL;
  186. if (!val)
  187. val = 100000000;
  188. return val;
  189. }
  190. static inline int calculate_new_frame(gs_image_file_t *image,
  191. uint64_t elapsed_time_ns, int loops)
  192. {
  193. int new_frame = image->cur_frame;
  194. image->cur_time += elapsed_time_ns;
  195. for (;;) {
  196. uint64_t t = get_time(image, new_frame);
  197. if (image->cur_time <= t)
  198. break;
  199. image->cur_time -= t;
  200. if ((unsigned int)++new_frame == image->gif.frame_count) {
  201. if (!loops || ++image->cur_loop < loops) {
  202. new_frame = 0;
  203. } else if (image->cur_loop == loops) {
  204. new_frame--;
  205. break;
  206. }
  207. }
  208. }
  209. return new_frame;
  210. }
  211. static void decode_new_frame(gs_image_file_t *image, int new_frame)
  212. {
  213. if (!image->animation_frame_cache[new_frame]) {
  214. int last_frame;
  215. /* if looped, decode frame 0 */
  216. last_frame = (new_frame < image->last_decoded_frame) ?
  217. 0 : image->last_decoded_frame + 1;
  218. /* decode missed frames */
  219. for (int i = last_frame; i < new_frame; i++) {
  220. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  221. return;
  222. }
  223. /* decode actual desired frame */
  224. if (gif_decode_frame(&image->gif, new_frame) == GIF_OK) {
  225. size_t pos = new_frame * image->gif.width *
  226. image->gif.height * 4;
  227. image->animation_frame_cache[new_frame] =
  228. image->animation_frame_data + pos;
  229. memcpy(image->animation_frame_cache[new_frame],
  230. image->gif.frame_image,
  231. image->gif.width *
  232. image->gif.height * 4);
  233. image->last_decoded_frame = new_frame;
  234. }
  235. }
  236. image->cur_frame = new_frame;
  237. }
  238. bool gs_image_file_tick(gs_image_file_t *image, uint64_t elapsed_time_ns)
  239. {
  240. int loops;
  241. if (!image->is_animated_gif || !image->loaded)
  242. return false;
  243. loops = image->gif.loop_count;
  244. if (loops >= 0xFFFF)
  245. loops = 0;
  246. if (!loops || image->cur_loop < loops) {
  247. int new_frame = calculate_new_frame(image, elapsed_time_ns,
  248. loops);
  249. if (new_frame != image->cur_frame) {
  250. decode_new_frame(image, new_frame);
  251. return true;
  252. }
  253. }
  254. return false;
  255. }
  256. void gs_image_file_update_texture(gs_image_file_t *image)
  257. {
  258. if (!image->is_animated_gif || !image->loaded)
  259. return;
  260. if (!image->animation_frame_cache[image->cur_frame])
  261. decode_new_frame(image, image->cur_frame);
  262. gs_texture_set_image(image->texture,
  263. image->animation_frame_cache[image->cur_frame],
  264. image->gif.width * 4, false);
  265. }