image-file.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
  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 "../util/dstr.h"
  18. #include "vec4.h"
  19. #define blog(level, format, ...) 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 * image->gif.frame_count;
  49. }
  50. static inline void *alloc_mem(gs_image_file_t *image, uint64_t *mem_usage, 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, uint64_t *mem_usage,
  58. enum gs_image_alpha_mode alpha_mode)
  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,
  90. "Failed to initialize gif '%s', "
  91. "possible file corruption",
  92. path);
  93. goto fail;
  94. }
  95. } while (result != GIF_OK);
  96. if (image->gif.width > 4096 || image->gif.height > 4096) {
  97. blog(LOG_WARNING, "Bad texture dimensions (%dx%d) in '%s'", image->gif.width, image->gif.height, path);
  98. goto fail;
  99. }
  100. max_size = (uint64_t)image->gif.width * (uint64_t)image->gif.height * (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", path);
  103. goto fail;
  104. }
  105. image->is_animated_gif = (image->gif.frame_count > 1 && result >= 0);
  106. if (image->is_animated_gif) {
  107. gif_decode_frame(&image->gif, 0);
  108. image->animation_frame_cache = alloc_mem(image, mem_usage, image->gif.frame_count * sizeof(uint8_t *));
  109. image->animation_frame_data = alloc_mem(image, mem_usage, get_full_decoded_gif_size(image));
  110. for (unsigned int i = 0; i < image->gif.frame_count; i++) {
  111. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  112. blog(LOG_WARNING,
  113. "Couldn't decode frame %u "
  114. "of '%s'",
  115. i, path);
  116. }
  117. gif_decode_frame(&image->gif, 0);
  118. image->cx = (uint32_t)image->gif.width;
  119. image->cy = (uint32_t)image->gif.height;
  120. image->format = GS_RGBA;
  121. if (mem_usage) {
  122. *mem_usage += (size_t)4 * image->cx * image->cy;
  123. *mem_usage += size;
  124. }
  125. if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB) {
  126. gs_premultiply_xyza_srgb_loop(image->gif.frame_image, (size_t)image->cx * image->cy);
  127. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY) {
  128. gs_premultiply_xyza_loop(image->gif.frame_image, (size_t)image->cx * image->cy);
  129. }
  130. } else {
  131. gif_finalise(&image->gif);
  132. bfree(image->gif_data);
  133. image->gif_data = NULL;
  134. is_animated_gif = false;
  135. goto not_animated;
  136. }
  137. image->loaded = true;
  138. fail:
  139. if (!image->loaded)
  140. gs_image_file_free(image);
  141. not_animated:
  142. if (file)
  143. fclose(file);
  144. return is_animated_gif;
  145. }
  146. static void gs_image_file_init_internal(gs_image_file_t *image, const char *file, uint64_t *mem_usage,
  147. enum gs_color_space *space, enum gs_image_alpha_mode alpha_mode)
  148. {
  149. size_t len;
  150. if (!image)
  151. return;
  152. memset(image, 0, sizeof(*image));
  153. if (!file)
  154. return;
  155. len = strlen(file);
  156. if (len > 4 && astrcmpi(file + len - 4, ".gif") == 0) {
  157. if (init_animated_gif(image, file, mem_usage, alpha_mode)) {
  158. return;
  159. }
  160. }
  161. image->texture_data =
  162. gs_create_texture_file_data3(file, alpha_mode, &image->format, &image->cx, &image->cy, space);
  163. if (mem_usage) {
  164. *mem_usage += image->cx * image->cy * gs_get_format_bpp(image->format) / 8;
  165. }
  166. image->loaded = !!image->texture_data;
  167. if (!image->loaded) {
  168. blog(LOG_WARNING, "Failed to load file '%s'", file);
  169. gs_image_file_free(image);
  170. }
  171. }
  172. void gs_image_file_init(gs_image_file_t *image, const char *file)
  173. {
  174. enum gs_color_space unused;
  175. gs_image_file_init_internal(image, file, NULL, &unused, GS_IMAGE_ALPHA_STRAIGHT);
  176. }
  177. void gs_image_file_free(gs_image_file_t *image)
  178. {
  179. if (!image)
  180. return;
  181. if (image->loaded) {
  182. if (image->is_animated_gif) {
  183. gif_finalise(&image->gif);
  184. bfree(image->animation_frame_cache);
  185. bfree(image->animation_frame_data);
  186. }
  187. gs_texture_destroy(image->texture);
  188. }
  189. bfree(image->texture_data);
  190. bfree(image->gif_data);
  191. memset(image, 0, sizeof(*image));
  192. }
  193. void gs_image_file2_init(gs_image_file2_t *if2, const char *file)
  194. {
  195. enum gs_color_space unused;
  196. gs_image_file_init_internal(&if2->image, file, &if2->mem_usage, &unused, GS_IMAGE_ALPHA_STRAIGHT);
  197. }
  198. void gs_image_file3_init(gs_image_file3_t *if3, const char *file, enum gs_image_alpha_mode alpha_mode)
  199. {
  200. enum gs_color_space unused;
  201. gs_image_file_init_internal(&if3->image2.image, file, &if3->image2.mem_usage, &unused, alpha_mode);
  202. if3->alpha_mode = alpha_mode;
  203. }
  204. void gs_image_file4_init(gs_image_file4_t *if4, const char *file, enum gs_image_alpha_mode alpha_mode)
  205. {
  206. gs_image_file_init_internal(&if4->image3.image2.image, file, &if4->image3.image2.mem_usage, &if4->space,
  207. alpha_mode);
  208. if4->image3.alpha_mode = alpha_mode;
  209. }
  210. void gs_image_file_init_texture(gs_image_file_t *image)
  211. {
  212. if (!image->loaded)
  213. return;
  214. if (image->is_animated_gif) {
  215. image->texture = gs_texture_create(image->cx, image->cy, image->format, 1,
  216. (const uint8_t **)&image->gif.frame_image, GS_DYNAMIC);
  217. } else {
  218. image->texture = gs_texture_create(image->cx, image->cy, image->format, 1,
  219. (const uint8_t **)&image->texture_data, 0);
  220. bfree(image->texture_data);
  221. image->texture_data = NULL;
  222. }
  223. }
  224. static inline uint64_t get_time(gs_image_file_t *image, int i)
  225. {
  226. uint64_t val = (uint64_t)image->gif.frames[i].frame_delay * 10000000ULL;
  227. if (!val)
  228. val = 100000000;
  229. return val;
  230. }
  231. static inline int calculate_new_frame(gs_image_file_t *image, uint64_t elapsed_time_ns, int loops)
  232. {
  233. int new_frame = image->cur_frame;
  234. image->cur_time += elapsed_time_ns;
  235. for (;;) {
  236. uint64_t t = get_time(image, new_frame);
  237. if (image->cur_time <= t)
  238. break;
  239. image->cur_time -= t;
  240. if ((unsigned int)++new_frame == image->gif.frame_count) {
  241. if (!loops || ++image->cur_loop < loops) {
  242. new_frame = 0;
  243. } else if (image->cur_loop == loops) {
  244. new_frame--;
  245. break;
  246. }
  247. }
  248. }
  249. return new_frame;
  250. }
  251. static void decode_new_frame(gs_image_file_t *image, int new_frame, enum gs_image_alpha_mode alpha_mode)
  252. {
  253. if (!image->animation_frame_cache[new_frame]) {
  254. int last_frame;
  255. /* if looped, decode frame 0 */
  256. last_frame = (new_frame < image->last_decoded_frame) ? 0 : image->last_decoded_frame + 1;
  257. /* decode missed frames */
  258. for (int i = last_frame; i < new_frame; i++) {
  259. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  260. return;
  261. }
  262. /* decode actual desired frame */
  263. if (gif_decode_frame(&image->gif, new_frame) == GIF_OK) {
  264. const size_t area = (size_t)image->gif.width * image->gif.height;
  265. size_t pos = new_frame * area * 4;
  266. image->animation_frame_cache[new_frame] = image->animation_frame_data + pos;
  267. if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB) {
  268. gs_premultiply_xyza_srgb_loop(image->gif.frame_image, area);
  269. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY) {
  270. gs_premultiply_xyza_loop(image->gif.frame_image, area);
  271. }
  272. memcpy(image->animation_frame_cache[new_frame], image->gif.frame_image, area * 4);
  273. image->last_decoded_frame = new_frame;
  274. }
  275. }
  276. image->cur_frame = new_frame;
  277. }
  278. static bool gs_image_file_tick_internal(gs_image_file_t *image, uint64_t elapsed_time_ns,
  279. enum gs_image_alpha_mode alpha_mode)
  280. {
  281. int loops;
  282. if (!image->is_animated_gif || !image->loaded)
  283. return false;
  284. loops = image->gif.loop_count;
  285. if (loops >= 0xFFFF)
  286. loops = 0;
  287. if (!loops || image->cur_loop < loops) {
  288. int new_frame = calculate_new_frame(image, elapsed_time_ns, loops);
  289. if (new_frame != image->cur_frame) {
  290. decode_new_frame(image, new_frame, alpha_mode);
  291. return true;
  292. }
  293. }
  294. return false;
  295. }
  296. bool gs_image_file_tick(gs_image_file_t *image, uint64_t elapsed_time_ns)
  297. {
  298. return gs_image_file_tick_internal(image, elapsed_time_ns, false);
  299. }
  300. bool gs_image_file2_tick(gs_image_file2_t *if2, uint64_t elapsed_time_ns)
  301. {
  302. return gs_image_file_tick_internal(&if2->image, elapsed_time_ns, false);
  303. }
  304. bool gs_image_file3_tick(gs_image_file3_t *if3, uint64_t elapsed_time_ns)
  305. {
  306. return gs_image_file_tick_internal(&if3->image2.image, elapsed_time_ns, if3->alpha_mode);
  307. }
  308. bool gs_image_file4_tick(gs_image_file4_t *if4, uint64_t elapsed_time_ns)
  309. {
  310. return gs_image_file_tick_internal(&if4->image3.image2.image, elapsed_time_ns, if4->image3.alpha_mode);
  311. }
  312. static void gs_image_file_update_texture_internal(gs_image_file_t *image, enum gs_image_alpha_mode alpha_mode)
  313. {
  314. if (!image->is_animated_gif || !image->loaded)
  315. return;
  316. if (!image->animation_frame_cache[image->cur_frame])
  317. decode_new_frame(image, image->cur_frame, alpha_mode);
  318. gs_texture_set_image(image->texture, image->animation_frame_cache[image->cur_frame], image->gif.width * 4,
  319. false);
  320. }
  321. void gs_image_file_update_texture(gs_image_file_t *image)
  322. {
  323. gs_image_file_update_texture_internal(image, false);
  324. }
  325. void gs_image_file2_update_texture(gs_image_file2_t *if2)
  326. {
  327. gs_image_file_update_texture_internal(&if2->image, false);
  328. }
  329. void gs_image_file3_update_texture(gs_image_file3_t *if3)
  330. {
  331. gs_image_file_update_texture_internal(&if3->image2.image, if3->alpha_mode);
  332. }
  333. void gs_image_file4_update_texture(gs_image_file4_t *if4)
  334. {
  335. gs_image_file_update_texture_internal(&if4->image3.image2.image, if4->image3.alpha_mode);
  336. }