image-file.c 8.8 KB

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