1
0

image-file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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_color_space *space,
  162. enum gs_image_alpha_mode alpha_mode)
  163. {
  164. size_t len;
  165. if (!image)
  166. return;
  167. memset(image, 0, sizeof(*image));
  168. if (!file)
  169. return;
  170. len = strlen(file);
  171. if (len > 4 && astrcmpi(file + len - 4, ".gif") == 0) {
  172. if (init_animated_gif(image, file, mem_usage, alpha_mode)) {
  173. return;
  174. }
  175. }
  176. image->texture_data =
  177. gs_create_texture_file_data3(file, alpha_mode, &image->format,
  178. &image->cx, &image->cy, space);
  179. if (mem_usage) {
  180. *mem_usage += image->cx * image->cy *
  181. gs_get_format_bpp(image->format) / 8;
  182. }
  183. image->loaded = !!image->texture_data;
  184. if (!image->loaded) {
  185. blog(LOG_WARNING, "Failed to load file '%s'", file);
  186. gs_image_file_free(image);
  187. }
  188. }
  189. void gs_image_file_init(gs_image_file_t *image, const char *file)
  190. {
  191. enum gs_color_space unused;
  192. gs_image_file_init_internal(image, file, NULL, &unused,
  193. GS_IMAGE_ALPHA_STRAIGHT);
  194. }
  195. void gs_image_file_free(gs_image_file_t *image)
  196. {
  197. if (!image)
  198. return;
  199. if (image->loaded) {
  200. if (image->is_animated_gif) {
  201. gif_finalise(&image->gif);
  202. bfree(image->animation_frame_cache);
  203. bfree(image->animation_frame_data);
  204. }
  205. gs_texture_destroy(image->texture);
  206. }
  207. bfree(image->texture_data);
  208. bfree(image->gif_data);
  209. memset(image, 0, sizeof(*image));
  210. }
  211. void gs_image_file2_init(gs_image_file2_t *if2, const char *file)
  212. {
  213. enum gs_color_space unused;
  214. gs_image_file_init_internal(&if2->image, file, &if2->mem_usage, &unused,
  215. GS_IMAGE_ALPHA_STRAIGHT);
  216. }
  217. void gs_image_file3_init(gs_image_file3_t *if3, const char *file,
  218. enum gs_image_alpha_mode alpha_mode)
  219. {
  220. enum gs_color_space unused;
  221. gs_image_file_init_internal(&if3->image2.image, file,
  222. &if3->image2.mem_usage, &unused,
  223. alpha_mode);
  224. if3->alpha_mode = alpha_mode;
  225. }
  226. void gs_image_file4_init(gs_image_file4_t *if4, const char *file,
  227. enum gs_image_alpha_mode alpha_mode)
  228. {
  229. gs_image_file_init_internal(&if4->image3.image2.image, file,
  230. &if4->image3.image2.mem_usage, &if4->space,
  231. alpha_mode);
  232. if4->image3.alpha_mode = alpha_mode;
  233. }
  234. void gs_image_file_init_texture(gs_image_file_t *image)
  235. {
  236. if (!image->loaded)
  237. return;
  238. if (image->is_animated_gif) {
  239. image->texture = gs_texture_create(
  240. image->cx, image->cy, image->format, 1,
  241. (const uint8_t **)&image->gif.frame_image, GS_DYNAMIC);
  242. } else {
  243. image->texture = gs_texture_create(
  244. image->cx, image->cy, image->format, 1,
  245. (const uint8_t **)&image->texture_data, 0);
  246. bfree(image->texture_data);
  247. image->texture_data = NULL;
  248. }
  249. }
  250. static inline uint64_t get_time(gs_image_file_t *image, int i)
  251. {
  252. uint64_t val = (uint64_t)image->gif.frames[i].frame_delay * 10000000ULL;
  253. if (!val)
  254. val = 100000000;
  255. return val;
  256. }
  257. static inline int calculate_new_frame(gs_image_file_t *image,
  258. uint64_t elapsed_time_ns, int loops)
  259. {
  260. int new_frame = image->cur_frame;
  261. image->cur_time += elapsed_time_ns;
  262. for (;;) {
  263. uint64_t t = get_time(image, new_frame);
  264. if (image->cur_time <= t)
  265. break;
  266. image->cur_time -= t;
  267. if ((unsigned int)++new_frame == image->gif.frame_count) {
  268. if (!loops || ++image->cur_loop < loops) {
  269. new_frame = 0;
  270. } else if (image->cur_loop == loops) {
  271. new_frame--;
  272. break;
  273. }
  274. }
  275. }
  276. return new_frame;
  277. }
  278. static void decode_new_frame(gs_image_file_t *image, int new_frame,
  279. enum gs_image_alpha_mode alpha_mode)
  280. {
  281. if (!image->animation_frame_cache[new_frame]) {
  282. int last_frame;
  283. /* if looped, decode frame 0 */
  284. last_frame = (new_frame < image->last_decoded_frame)
  285. ? 0
  286. : image->last_decoded_frame + 1;
  287. /* decode missed frames */
  288. for (int i = last_frame; i < new_frame; i++) {
  289. if (gif_decode_frame(&image->gif, i) != GIF_OK)
  290. return;
  291. }
  292. /* decode actual desired frame */
  293. if (gif_decode_frame(&image->gif, new_frame) == GIF_OK) {
  294. const size_t area =
  295. (size_t)image->gif.width * image->gif.height;
  296. size_t pos = new_frame * area * 4;
  297. image->animation_frame_cache[new_frame] =
  298. image->animation_frame_data + pos;
  299. if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY_SRGB) {
  300. gs_premultiply_xyza_srgb_loop(
  301. image->gif.frame_image, area);
  302. } else if (alpha_mode == GS_IMAGE_ALPHA_PREMULTIPLY) {
  303. gs_premultiply_xyza_loop(image->gif.frame_image,
  304. area);
  305. }
  306. memcpy(image->animation_frame_cache[new_frame],
  307. image->gif.frame_image, area * 4);
  308. image->last_decoded_frame = new_frame;
  309. }
  310. }
  311. image->cur_frame = new_frame;
  312. }
  313. static bool gs_image_file_tick_internal(gs_image_file_t *image,
  314. uint64_t elapsed_time_ns,
  315. enum gs_image_alpha_mode alpha_mode)
  316. {
  317. int loops;
  318. if (!image->is_animated_gif || !image->loaded)
  319. return false;
  320. loops = image->gif.loop_count;
  321. if (loops >= 0xFFFF)
  322. loops = 0;
  323. if (!loops || image->cur_loop < loops) {
  324. int new_frame =
  325. calculate_new_frame(image, elapsed_time_ns, loops);
  326. if (new_frame != image->cur_frame) {
  327. decode_new_frame(image, new_frame, alpha_mode);
  328. return true;
  329. }
  330. }
  331. return false;
  332. }
  333. bool gs_image_file_tick(gs_image_file_t *image, uint64_t elapsed_time_ns)
  334. {
  335. return gs_image_file_tick_internal(image, elapsed_time_ns, false);
  336. }
  337. bool gs_image_file2_tick(gs_image_file2_t *if2, uint64_t elapsed_time_ns)
  338. {
  339. return gs_image_file_tick_internal(&if2->image, elapsed_time_ns, false);
  340. }
  341. bool gs_image_file3_tick(gs_image_file3_t *if3, uint64_t elapsed_time_ns)
  342. {
  343. return gs_image_file_tick_internal(&if3->image2.image, elapsed_time_ns,
  344. if3->alpha_mode);
  345. }
  346. bool gs_image_file4_tick(gs_image_file4_t *if4, uint64_t elapsed_time_ns)
  347. {
  348. return gs_image_file_tick_internal(&if4->image3.image2.image,
  349. elapsed_time_ns,
  350. if4->image3.alpha_mode);
  351. }
  352. static void
  353. gs_image_file_update_texture_internal(gs_image_file_t *image,
  354. enum gs_image_alpha_mode alpha_mode)
  355. {
  356. if (!image->is_animated_gif || !image->loaded)
  357. return;
  358. if (!image->animation_frame_cache[image->cur_frame])
  359. decode_new_frame(image, image->cur_frame, alpha_mode);
  360. gs_texture_set_image(image->texture,
  361. image->animation_frame_cache[image->cur_frame],
  362. image->gif.width * 4, false);
  363. }
  364. void gs_image_file_update_texture(gs_image_file_t *image)
  365. {
  366. gs_image_file_update_texture_internal(image, false);
  367. }
  368. void gs_image_file2_update_texture(gs_image_file2_t *if2)
  369. {
  370. gs_image_file_update_texture_internal(&if2->image, false);
  371. }
  372. void gs_image_file3_update_texture(gs_image_file3_t *if3)
  373. {
  374. gs_image_file_update_texture_internal(&if3->image2.image,
  375. if3->alpha_mode);
  376. }
  377. void gs_image_file4_update_texture(gs_image_file4_t *if4)
  378. {
  379. gs_image_file_update_texture_internal(&if4->image3.image2.image,
  380. if4->image3.alpha_mode);
  381. }