image-file.c 11 KB

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