image-file.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 *
  48. image->gif.frame_count;
  49. }
  50. static inline void *alloc_mem(gs_image_file_t *image, uint64_t *mem_usage,
  51. size_t size)
  52. {
  53. UNUSED_PARAMETER(image);
  54. if (mem_usage)
  55. *mem_usage += size;
  56. return bzalloc(size);
  57. }
  58. static bool init_animated_gif(gs_image_file_t *image, const char *path,
  59. uint64_t *mem_usage)
  60. {
  61. bool is_animated_gif = true;
  62. gif_result result;
  63. uint64_t max_size;
  64. size_t size, size_read;
  65. FILE *file;
  66. image->bitmap_callbacks.bitmap_create = bi_def_bitmap_create;
  67. image->bitmap_callbacks.bitmap_destroy = bi_def_bitmap_destroy;
  68. image->bitmap_callbacks.bitmap_get_buffer = bi_def_bitmap_get_buffer;
  69. image->bitmap_callbacks.bitmap_modified = bi_def_bitmap_modified;
  70. image->bitmap_callbacks.bitmap_set_opaque = bi_def_bitmap_set_opaque;
  71. image->bitmap_callbacks.bitmap_test_opaque = bi_def_bitmap_test_opaque;
  72. gif_create(&image->gif, &image->bitmap_callbacks);
  73. file = os_fopen(path, "rb");
  74. if (!file) {
  75. blog(LOG_WARNING, "Failed to open file '%s'", path);
  76. goto fail;
  77. }
  78. fseek(file, 0, SEEK_END);
  79. size = (size_t)os_ftelli64(file);
  80. fseek(file, 0, SEEK_SET);
  81. image->gif_data = bmalloc(size);
  82. size_read = fread(image->gif_data, 1, size, file);
  83. if (size_read != size) {
  84. blog(LOG_WARNING, "Failed to fully read gif file '%s'.", path);
  85. goto fail;
  86. }
  87. do {
  88. result = gif_initialise(&image->gif, size, image->gif_data);
  89. if (result < 0) {
  90. blog(LOG_WARNING,
  91. "Failed to initialize gif '%s', "
  92. "possible file corruption",
  93. path);
  94. goto fail;
  95. }
  96. } while (result != GIF_OK);
  97. if (image->gif.width > 4096 || image->gif.height > 4096) {
  98. blog(LOG_WARNING, "Bad texture dimensions (%dx%d) in '%s'",
  99. image->gif.width, image->gif.height, path);
  100. goto fail;
  101. }
  102. max_size = (uint64_t)image->gif.width * (uint64_t)image->gif.height *
  103. (uint64_t)image->gif.frame_count * 4LLU;
  104. if ((uint64_t)get_full_decoded_gif_size(image) != max_size) {
  105. blog(LOG_WARNING, "Gif '%s' overflowed maximum pointer size",
  106. path);
  107. goto fail;
  108. }
  109. image->is_animated_gif = (image->gif.frame_count > 1 && result >= 0);
  110. if (image->is_animated_gif) {
  111. gif_decode_frame(&image->gif, 0);
  112. image->animation_frame_cache =
  113. alloc_mem(image, mem_usage,
  114. image->gif.frame_count * sizeof(uint8_t *));
  115. image->animation_frame_data = alloc_mem(
  116. image, mem_usage, get_full_decoded_gif_size(image));
  117. for (unsigned int i = 0; i < image->gif.frame_count; i++) {
  118. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  119. blog(LOG_WARNING,
  120. "Couldn't decode frame %u "
  121. "of '%s'",
  122. i, path);
  123. }
  124. gif_decode_frame(&image->gif, 0);
  125. image->cx = (uint32_t)image->gif.width;
  126. image->cy = (uint32_t)image->gif.height;
  127. image->format = GS_RGBA;
  128. if (mem_usage) {
  129. *mem_usage += image->cx * image->cy * 4;
  130. *mem_usage += size;
  131. }
  132. } else {
  133. gif_finalise(&image->gif);
  134. bfree(image->gif_data);
  135. image->gif_data = NULL;
  136. is_animated_gif = false;
  137. goto not_animated;
  138. }
  139. image->loaded = true;
  140. fail:
  141. if (!image->loaded)
  142. gs_image_file_free(image);
  143. not_animated:
  144. if (file)
  145. fclose(file);
  146. return is_animated_gif;
  147. }
  148. static void gs_image_file_init_internal(gs_image_file_t *image,
  149. const char *file, uint64_t *mem_usage)
  150. {
  151. size_t len;
  152. if (!image)
  153. return;
  154. memset(image, 0, sizeof(*image));
  155. if (!file)
  156. return;
  157. len = strlen(file);
  158. if (len > 4 && strcmp(file + len - 4, ".gif") == 0) {
  159. if (init_animated_gif(image, file, mem_usage))
  160. return;
  161. }
  162. image->texture_data = gs_create_texture_file_data(
  163. file, &image->format, &image->cx, &image->cy);
  164. if (mem_usage) {
  165. *mem_usage += image->cx * image->cy *
  166. gs_get_format_bpp(image->format) / 8;
  167. }
  168. image->loaded = !!image->texture_data;
  169. if (!image->loaded) {
  170. blog(LOG_WARNING, "Failed to load file '%s'", file);
  171. gs_image_file_free(image);
  172. }
  173. }
  174. void gs_image_file_init(gs_image_file_t *image, const char *file)
  175. {
  176. gs_image_file_init_internal(image, file, NULL);
  177. }
  178. void gs_image_file_free(gs_image_file_t *image)
  179. {
  180. if (!image)
  181. return;
  182. if (image->loaded) {
  183. if (image->is_animated_gif) {
  184. gif_finalise(&image->gif);
  185. bfree(image->animation_frame_cache);
  186. bfree(image->animation_frame_data);
  187. }
  188. gs_texture_destroy(image->texture);
  189. }
  190. bfree(image->texture_data);
  191. bfree(image->gif_data);
  192. memset(image, 0, sizeof(*image));
  193. }
  194. void gs_image_file2_init(gs_image_file2_t *if2, const char *file)
  195. {
  196. gs_image_file_init_internal(&if2->image, file, &if2->mem_usage);
  197. }
  198. void gs_image_file_init_texture(gs_image_file_t *image)
  199. {
  200. if (!image->loaded)
  201. return;
  202. if (image->is_animated_gif) {
  203. image->texture = gs_texture_create(
  204. image->cx, image->cy, image->format, 1,
  205. (const uint8_t **)&image->gif.frame_image, GS_DYNAMIC);
  206. } else {
  207. image->texture = gs_texture_create(
  208. image->cx, image->cy, image->format, 1,
  209. (const uint8_t **)&image->texture_data, 0);
  210. bfree(image->texture_data);
  211. image->texture_data = NULL;
  212. }
  213. }
  214. static inline uint64_t get_time(gs_image_file_t *image, int i)
  215. {
  216. uint64_t val = (uint64_t)image->gif.frames[i].frame_delay * 10000000ULL;
  217. if (!val)
  218. val = 100000000;
  219. return val;
  220. }
  221. static inline int calculate_new_frame(gs_image_file_t *image,
  222. uint64_t elapsed_time_ns, int loops)
  223. {
  224. int new_frame = image->cur_frame;
  225. image->cur_time += elapsed_time_ns;
  226. for (;;) {
  227. uint64_t t = get_time(image, new_frame);
  228. if (image->cur_time <= t)
  229. break;
  230. image->cur_time -= t;
  231. if ((unsigned int)++new_frame == image->gif.frame_count) {
  232. if (!loops || ++image->cur_loop < loops) {
  233. new_frame = 0;
  234. } else if (image->cur_loop == loops) {
  235. new_frame--;
  236. break;
  237. }
  238. }
  239. }
  240. return new_frame;
  241. }
  242. static void decode_new_frame(gs_image_file_t *image, int new_frame)
  243. {
  244. if (!image->animation_frame_cache[new_frame]) {
  245. int last_frame;
  246. /* if looped, decode frame 0 */
  247. last_frame = (new_frame < image->last_decoded_frame)
  248. ? 0
  249. : image->last_decoded_frame + 1;
  250. /* decode missed frames */
  251. for (int i = last_frame; i < new_frame; i++) {
  252. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  253. return;
  254. }
  255. /* decode actual desired frame */
  256. if (gif_decode_frame(&image->gif, new_frame) == GIF_OK) {
  257. size_t pos = new_frame * image->gif.width *
  258. image->gif.height * 4;
  259. image->animation_frame_cache[new_frame] =
  260. image->animation_frame_data + pos;
  261. memcpy(image->animation_frame_cache[new_frame],
  262. image->gif.frame_image,
  263. image->gif.width * image->gif.height * 4);
  264. image->last_decoded_frame = new_frame;
  265. }
  266. }
  267. image->cur_frame = new_frame;
  268. }
  269. bool gs_image_file_tick(gs_image_file_t *image, uint64_t elapsed_time_ns)
  270. {
  271. int loops;
  272. if (!image->is_animated_gif || !image->loaded)
  273. return false;
  274. loops = image->gif.loop_count;
  275. if (loops >= 0xFFFF)
  276. loops = 0;
  277. if (!loops || image->cur_loop < loops) {
  278. int new_frame =
  279. calculate_new_frame(image, elapsed_time_ns, loops);
  280. if (new_frame != image->cur_frame) {
  281. decode_new_frame(image, new_frame);
  282. return true;
  283. }
  284. }
  285. return false;
  286. }
  287. void gs_image_file_update_texture(gs_image_file_t *image)
  288. {
  289. if (!image->is_animated_gif || !image->loaded)
  290. return;
  291. if (!image->animation_frame_cache[image->cur_frame])
  292. decode_new_frame(image, image->cur_frame);
  293. gs_texture_set_image(image->texture,
  294. image->animation_frame_cache[image->cur_frame],
  295. image->gif.width * 4, false);
  296. }