image-file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. #include "vec4.h"
  18. #define blog(level, format, ...) \
  19. blog(level, "%s: " format, __FUNCTION__, __VA_ARGS__)
  20. static void *bi_def_bitmap_create(int width, int height)
  21. {
  22. return bmalloc((size_t)4 * width * height);
  23. }
  24. static void bi_def_bitmap_set_opaque(void *bitmap, bool opaque)
  25. {
  26. UNUSED_PARAMETER(bitmap);
  27. UNUSED_PARAMETER(opaque);
  28. }
  29. static bool bi_def_bitmap_test_opaque(void *bitmap)
  30. {
  31. UNUSED_PARAMETER(bitmap);
  32. return false;
  33. }
  34. static unsigned char *bi_def_bitmap_get_buffer(void *bitmap)
  35. {
  36. return (unsigned char *)bitmap;
  37. }
  38. static void bi_def_bitmap_destroy(void *bitmap)
  39. {
  40. bfree(bitmap);
  41. }
  42. static void bi_def_bitmap_modified(void *bitmap)
  43. {
  44. UNUSED_PARAMETER(bitmap);
  45. }
  46. static inline int get_full_decoded_gif_size(gs_image_file_t *image)
  47. {
  48. return image->gif.width * image->gif.height * 4 *
  49. image->gif.frame_count;
  50. }
  51. static inline void *alloc_mem(gs_image_file_t *image, uint64_t *mem_usage,
  52. size_t size)
  53. {
  54. UNUSED_PARAMETER(image);
  55. if (mem_usage)
  56. *mem_usage += size;
  57. return bzalloc(size);
  58. }
  59. static bool init_animated_gif(gs_image_file_t *image, const char *path,
  60. uint64_t *mem_usage,
  61. enum gs_image_alpha_mode alpha_mode)
  62. {
  63. bool is_animated_gif = true;
  64. gif_result result;
  65. uint64_t max_size;
  66. size_t size, size_read;
  67. FILE *file;
  68. image->bitmap_callbacks.bitmap_create = bi_def_bitmap_create;
  69. image->bitmap_callbacks.bitmap_destroy = bi_def_bitmap_destroy;
  70. image->bitmap_callbacks.bitmap_get_buffer = bi_def_bitmap_get_buffer;
  71. image->bitmap_callbacks.bitmap_modified = bi_def_bitmap_modified;
  72. image->bitmap_callbacks.bitmap_set_opaque = bi_def_bitmap_set_opaque;
  73. image->bitmap_callbacks.bitmap_test_opaque = bi_def_bitmap_test_opaque;
  74. gif_create(&image->gif, &image->bitmap_callbacks);
  75. file = os_fopen(path, "rb");
  76. if (!file) {
  77. blog(LOG_WARNING, "Failed to open file '%s'", path);
  78. goto fail;
  79. }
  80. fseek(file, 0, SEEK_END);
  81. size = (size_t)os_ftelli64(file);
  82. fseek(file, 0, SEEK_SET);
  83. image->gif_data = bmalloc(size);
  84. size_read = fread(image->gif_data, 1, size, file);
  85. if (size_read != size) {
  86. blog(LOG_WARNING, "Failed to fully read gif file '%s'.", path);
  87. goto fail;
  88. }
  89. do {
  90. result = gif_initialise(&image->gif, size, image->gif_data);
  91. if (result < 0) {
  92. blog(LOG_WARNING,
  93. "Failed to initialize gif '%s', "
  94. "possible file corruption",
  95. path);
  96. goto fail;
  97. }
  98. } while (result != GIF_OK);
  99. if (image->gif.width > 4096 || image->gif.height > 4096) {
  100. blog(LOG_WARNING, "Bad texture dimensions (%dx%d) in '%s'",
  101. image->gif.width, image->gif.height, path);
  102. goto fail;
  103. }
  104. max_size = (uint64_t)image->gif.width * (uint64_t)image->gif.height *
  105. (uint64_t)image->gif.frame_count * 4LLU;
  106. if ((uint64_t)get_full_decoded_gif_size(image) != max_size) {
  107. blog(LOG_WARNING, "Gif '%s' overflowed maximum pointer size",
  108. path);
  109. goto fail;
  110. }
  111. image->is_animated_gif = (image->gif.frame_count > 1 && result >= 0);
  112. if (image->is_animated_gif) {
  113. gif_decode_frame(&image->gif, 0);
  114. image->animation_frame_cache =
  115. alloc_mem(image, mem_usage,
  116. image->gif.frame_count * sizeof(uint8_t *));
  117. image->animation_frame_data = alloc_mem(
  118. image, mem_usage, get_full_decoded_gif_size(image));
  119. for (unsigned int i = 0; i < image->gif.frame_count; i++) {
  120. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  121. blog(LOG_WARNING,
  122. "Couldn't decode frame %u "
  123. "of '%s'",
  124. i, path);
  125. }
  126. gif_decode_frame(&image->gif, 0);
  127. image->cx = (uint32_t)image->gif.width;
  128. image->cy = (uint32_t)image->gif.height;
  129. image->format = GS_RGBA;
  130. if (mem_usage) {
  131. *mem_usage += (size_t)4 * image->cx * image->cy;
  132. *mem_usage += size;
  133. }
  134. if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB) {
  135. gs_premultiply_xyza_srgb_loop(image->gif.frame_image,
  136. (size_t)image->cx *
  137. image->cy);
  138. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY) {
  139. gs_premultiply_xyza_loop(image->gif.frame_image,
  140. (size_t)image->cx * image->cy);
  141. }
  142. } else {
  143. gif_finalise(&image->gif);
  144. bfree(image->gif_data);
  145. image->gif_data = NULL;
  146. is_animated_gif = false;
  147. goto not_animated;
  148. }
  149. image->loaded = true;
  150. fail:
  151. if (!image->loaded)
  152. gs_image_file_free(image);
  153. not_animated:
  154. if (file)
  155. fclose(file);
  156. return is_animated_gif;
  157. }
  158. static void gs_image_file_init_internal(gs_image_file_t *image,
  159. const char *file, uint64_t *mem_usage,
  160. enum gs_image_alpha_mode alpha_mode)
  161. {
  162. size_t len;
  163. if (!image)
  164. return;
  165. memset(image, 0, sizeof(*image));
  166. if (!file)
  167. return;
  168. len = strlen(file);
  169. if (len > 4 && strcmp(file + len - 4, ".gif") == 0) {
  170. if (init_animated_gif(image, file, mem_usage, alpha_mode)) {
  171. return;
  172. }
  173. }
  174. image->texture_data = gs_create_texture_file_data2(
  175. file, alpha_mode, &image->format, &image->cx, &image->cy);
  176. if (mem_usage) {
  177. *mem_usage += image->cx * image->cy *
  178. gs_get_format_bpp(image->format) / 8;
  179. }
  180. image->loaded = !!image->texture_data;
  181. if (!image->loaded) {
  182. blog(LOG_WARNING, "Failed to load file '%s'", file);
  183. gs_image_file_free(image);
  184. }
  185. }
  186. void gs_image_file_init(gs_image_file_t *image, const char *file)
  187. {
  188. gs_image_file_init_internal(image, file, NULL, GS_IMAGE_ALPHA_STRAIGHT);
  189. }
  190. void gs_image_file_free(gs_image_file_t *image)
  191. {
  192. if (!image)
  193. return;
  194. if (image->loaded) {
  195. if (image->is_animated_gif) {
  196. gif_finalise(&image->gif);
  197. bfree(image->animation_frame_cache);
  198. bfree(image->animation_frame_data);
  199. }
  200. gs_texture_destroy(image->texture);
  201. }
  202. bfree(image->texture_data);
  203. bfree(image->gif_data);
  204. memset(image, 0, sizeof(*image));
  205. }
  206. void gs_image_file2_init(gs_image_file2_t *if2, const char *file)
  207. {
  208. gs_image_file_init_internal(&if2->image, file, &if2->mem_usage,
  209. GS_IMAGE_ALPHA_STRAIGHT);
  210. }
  211. void gs_image_file3_init(gs_image_file3_t *if3, const char *file,
  212. enum gs_image_alpha_mode alpha_mode)
  213. {
  214. gs_image_file_init_internal(&if3->image2.image, file,
  215. &if3->image2.mem_usage, alpha_mode);
  216. if3->alpha_mode = alpha_mode;
  217. }
  218. void gs_image_file_init_texture(gs_image_file_t *image)
  219. {
  220. if (!image->loaded)
  221. return;
  222. if (image->is_animated_gif) {
  223. image->texture = gs_texture_create(
  224. image->cx, image->cy, image->format, 1,
  225. (const uint8_t **)&image->gif.frame_image, GS_DYNAMIC);
  226. } else {
  227. image->texture = gs_texture_create(
  228. image->cx, image->cy, image->format, 1,
  229. (const uint8_t **)&image->texture_data, 0);
  230. bfree(image->texture_data);
  231. image->texture_data = NULL;
  232. }
  233. }
  234. static inline uint64_t get_time(gs_image_file_t *image, int i)
  235. {
  236. uint64_t val = (uint64_t)image->gif.frames[i].frame_delay * 10000000ULL;
  237. if (!val)
  238. val = 100000000;
  239. return val;
  240. }
  241. static inline int calculate_new_frame(gs_image_file_t *image,
  242. uint64_t elapsed_time_ns, int loops)
  243. {
  244. int new_frame = image->cur_frame;
  245. image->cur_time += elapsed_time_ns;
  246. for (;;) {
  247. uint64_t t = get_time(image, new_frame);
  248. if (image->cur_time <= t)
  249. break;
  250. image->cur_time -= t;
  251. if ((unsigned int)++new_frame == image->gif.frame_count) {
  252. if (!loops || ++image->cur_loop < loops) {
  253. new_frame = 0;
  254. } else if (image->cur_loop == loops) {
  255. new_frame--;
  256. break;
  257. }
  258. }
  259. }
  260. return new_frame;
  261. }
  262. static void decode_new_frame(gs_image_file_t *image, int new_frame,
  263. enum gs_image_alpha_mode alpha_mode)
  264. {
  265. if (!image->animation_frame_cache[new_frame]) {
  266. int last_frame;
  267. /* if looped, decode frame 0 */
  268. last_frame = (new_frame < image->last_decoded_frame)
  269. ? 0
  270. : image->last_decoded_frame + 1;
  271. /* decode missed frames */
  272. for (int i = last_frame; i < new_frame; i++) {
  273. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  274. return;
  275. }
  276. /* decode actual desired frame */
  277. if (gif_decode_frame(&image->gif, new_frame) == GIF_OK) {
  278. const size_t area =
  279. (size_t)image->gif.width * image->gif.height;
  280. size_t pos = new_frame * area * 4;
  281. image->animation_frame_cache[new_frame] =
  282. image->animation_frame_data + pos;
  283. if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB) {
  284. gs_premultiply_xyza_srgb_loop(
  285. image->gif.frame_image, area);
  286. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY) {
  287. gs_premultiply_xyza_loop(image->gif.frame_image,
  288. area);
  289. }
  290. memcpy(image->animation_frame_cache[new_frame],
  291. image->gif.frame_image, area * 4);
  292. image->last_decoded_frame = new_frame;
  293. }
  294. }
  295. image->cur_frame = new_frame;
  296. }
  297. static bool gs_image_file_tick_internal(gs_image_file_t *image,
  298. uint64_t elapsed_time_ns,
  299. enum gs_image_alpha_mode alpha_mode)
  300. {
  301. int loops;
  302. if (!image->is_animated_gif || !image->loaded)
  303. return false;
  304. loops = image->gif.loop_count;
  305. if (loops >= 0xFFFF)
  306. loops = 0;
  307. if (!loops || image->cur_loop < loops) {
  308. int new_frame =
  309. calculate_new_frame(image, elapsed_time_ns, loops);
  310. if (new_frame != image->cur_frame) {
  311. decode_new_frame(image, new_frame, alpha_mode);
  312. return true;
  313. }
  314. }
  315. return false;
  316. }
  317. bool gs_image_file_tick(gs_image_file_t *image, uint64_t elapsed_time_ns)
  318. {
  319. return gs_image_file_tick_internal(image, elapsed_time_ns, false);
  320. }
  321. bool gs_image_file2_tick(gs_image_file2_t *if2, uint64_t elapsed_time_ns)
  322. {
  323. return gs_image_file_tick_internal(&if2->image, elapsed_time_ns, false);
  324. }
  325. bool gs_image_file3_tick(gs_image_file3_t *if3, uint64_t elapsed_time_ns)
  326. {
  327. return gs_image_file_tick_internal(&if3->image2.image, elapsed_time_ns,
  328. if3->alpha_mode);
  329. }
  330. static void
  331. gs_image_file_update_texture_internal(gs_image_file_t *image,
  332. enum gs_image_alpha_mode alpha_mode)
  333. {
  334. if (!image->is_animated_gif || !image->loaded)
  335. return;
  336. if (!image->animation_frame_cache[image->cur_frame])
  337. decode_new_frame(image, image->cur_frame, alpha_mode);
  338. gs_texture_set_image(image->texture,
  339. image->animation_frame_cache[image->cur_frame],
  340. image->gif.width * 4, false);
  341. }
  342. void gs_image_file_update_texture(gs_image_file_t *image)
  343. {
  344. gs_image_file_update_texture_internal(image, false);
  345. }
  346. void gs_image_file2_update_texture(gs_image_file2_t *if2)
  347. {
  348. gs_image_file_update_texture_internal(&if2->image, false);
  349. }
  350. void gs_image_file3_update_texture(gs_image_file3_t *if3)
  351. {
  352. gs_image_file_update_texture_internal(&if3->image2.image,
  353. if3->alpha_mode);
  354. }