| @@ -33,12 +33,21 @@ |
| 33 | 33 | | #include <libexif/exif-data.h> |
| 34 | 34 | | #endif |
| 35 | 35 | | |
| 36 | | − | #if HAVE_LIBGIF |
| 36 | + | #ifdef IMLIB2_VERSION |
| 37 | + | #if IMLIB2_VERSION >= IMLIB2_VERSION_(1, 8, 0) |
| 38 | + | #define HAVE_IMLIB2_MULTI_FRAME 1 |
| 39 | + | #endif |
| 40 | + | #endif |
| 41 | + | #ifndef HAVE_IMLIB2_MULTI_FRAME |
| 42 | + | #define HAVE_IMLIB2_MULTI_FRAME 0 |
| 43 | + | #endif |
| 44 | + | |
| 45 | + | #if HAVE_LIBGIF && !HAVE_IMLIB2_MULTI_FRAME |
| 37 | 46 | | #include <gif_lib.h> |
| 38 | 47 | | enum { DEF_GIF_DELAY = 75 }; |
| 39 | 48 | | #endif |
| 40 | 49 | | |
| 41 | | − | #if HAVE_LIBWEBP |
| 50 | + | #if HAVE_LIBWEBP && !HAVE_IMLIB2_MULTI_FRAME |
| 42 | 51 | | #include <stdio.h> |
| 43 | 52 | | #include <webp/decode.h> |
| 44 | 53 | | #include <webp/demux.h> |
| @@ -139,7 +148,7 @@ void exif_auto_orientate(const fileinfo_t *file) |
| 139 | 148 | | } |
| 140 | 149 | | #endif |
| 141 | 150 | | |
| 142 | | − | #if HAVE_LIBGIF || HAVE_LIBWEBP |
| 151 | + | #if HAVE_LIBGIF || HAVE_LIBWEBP || HAVE_IMLIB2_MULTI_FRAME |
| 143 | 152 | | static void img_multiframe_context_set(img_t *img) |
| 144 | 153 | | { |
| 145 | 154 | | if (img->multi.cnt > 1) { |
| @@ -156,7 +165,25 @@ static void img_multiframe_context_set(img_t *img) |
| 156 | 165 | | } |
| 157 | 166 | | #endif |
| 158 | 167 | | |
| 159 | | − | #if HAVE_LIBGIF |
| 168 | + | #if (HAVE_LIBGIF || HAVE_LIBWEBP) && !HAVE_IMLIB2_MULTI_FRAME |
| 169 | + | static void img_multiframe_deprecation_notice(void) |
| 170 | + | { |
| 171 | + | static bool warned; |
| 172 | + | if (!warned) { |
| 173 | + | error(0, 0, "\n" |
| 174 | + | "################################################################\n" |
| 175 | + | "# DEPRECATION NOTICE #\n" |
| 176 | + | "################################################################\n" |
| 177 | + | "# Internal multi-frame gif and webp loaders are deprecated and #\n" |
| 178 | + | "# will be removed soon. Please upgrade to Imlib2 v1.8.0 for #\n" |
| 179 | + | "# multi-frame/animated image support. #\n" |
| 180 | + | "################################################################"); |
| 181 | + | warned = true; |
| 182 | + | } |
| 183 | + | } |
| 184 | + | #endif |
| 185 | + | |
| 186 | + | #if HAVE_LIBGIF && !HAVE_IMLIB2_MULTI_FRAME |
| 160 | 187 | | static bool img_load_gif(img_t *img, const fileinfo_t *file) |
| 161 | 188 | | { |
| 162 | 189 | | GifFileType *gif; |
| @@ -177,6 +204,8 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file) |
| 177 | 204 | | bool err = false; |
| 178 | 205 | | multi_img_t *m = &img->multi; |
| 179 | 206 | | |
| 207 | + | img_multiframe_deprecation_notice(); |
| 208 | + | |
| 180 | 209 | | #if defined(GIFLIB_MAJOR) && GIFLIB_MAJOR >= 5 |
| 181 | 210 | | gif = DGifOpenFileName(file->path, NULL); |
| 182 | 211 | | #else |
| @@ -321,8 +350,7 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file) |
| 321 | 350 | | } |
| 322 | 351 | | #endif /* HAVE_LIBGIF */ |
| 323 | 352 | | |
| 324 | | − | |
| 325 | | − | #if HAVE_LIBWEBP |
| 353 | + | #if HAVE_LIBWEBP && !HAVE_IMLIB2_MULTI_FRAME |
| 326 | 354 | | static bool img_load_webp(img_t *img, const fileinfo_t *file) |
| 327 | 355 | | { |
| 328 | 356 | | FILE *webp_file; |
| @@ -340,6 +368,8 @@ static bool img_load_webp(img_t *img, const fileinfo_t *file) |
| 340 | 368 | | bool err = false; |
| 341 | 369 | | multi_img_t *m = &img->multi; |
| 342 | 370 | | |
| 371 | + | img_multiframe_deprecation_notice(); |
| 372 | + | |
| 343 | 373 | | if ((webp_file = fopen(file->path, "rb")) == NULL) { |
| 344 | 374 | | error(0, errno, "%s: Error opening webp image", file->name); |
| 345 | 375 | | return false; |
| @@ -408,6 +438,118 @@ fail: |
| 408 | 438 | | } |
| 409 | 439 | | #endif /* HAVE_LIBWEBP */ |
| 410 | 440 | | |
| 441 | + | #if HAVE_IMLIB2_MULTI_FRAME |
| 442 | + | static void img_area_clear(int x, int y, int w, int h) |
| 443 | + | { |
| 444 | + | assert(x >= 0 && y >= 0); |
| 445 | + | assert(w > 0 && h > 0); |
| 446 | + | imlib_image_set_has_alpha(1); |
| 447 | + | imlib_context_set_blend(0); |
| 448 | + | imlib_context_set_color(0, 0, 0, 0); |
| 449 | + | imlib_image_fill_rectangle(x, y, w, h); |
| 450 | + | } |
| 451 | + | |
| 452 | + | static bool img_load_multiframe(img_t *img, const fileinfo_t *file) |
| 453 | + | { |
| 454 | + | unsigned int n, fcnt; |
| 455 | + | Imlib_Image blank; |
| 456 | + | Imlib_Frame_Info finfo; |
| 457 | + | int px, py, pw, ph, pflag; |
| 458 | + | multi_img_t *m = &img->multi; |
| 459 | + | |
| 460 | + | imlib_context_set_image(img->im); |
| 461 | + | imlib_image_get_frame_info(&finfo); |
| 462 | + | if ((fcnt = finfo.frame_count) <= 1 || !(finfo.frame_flags & IMLIB_IMAGE_ANIMATED)) |
| 463 | + | return false; |
| 464 | + | img->w = finfo.canvas_w; |
| 465 | + | img->h = finfo.canvas_h; |
| 466 | + | |
| 467 | + | if (fcnt > m->cap) { |
| 468 | + | m->cap = fcnt; |
| 469 | + | m->frames = erealloc(m->frames, m->cap * sizeof(*m->frames)); |
| 470 | + | } |
| 471 | + | |
| 472 | + | imlib_context_set_dither(0); |
| 473 | + | imlib_context_set_anti_alias(0); |
| 474 | + | imlib_context_set_color_modifier(NULL); |
| 475 | + | imlib_context_set_operation(IMLIB_OP_COPY); |
| 476 | + | |
| 477 | + | if ((blank = imlib_create_image(img->w, img->h)) == NULL) { |
| 478 | + | error(0, 0, "%s: couldn't create image", file->name); |
| 479 | + | return false; |
| 480 | + | } |
| 481 | + | imlib_context_set_image(blank); |
| 482 | + | img_area_clear(0, 0, img->w, img->h); |
| 483 | + | |
| 484 | + | /* |
| 485 | + | * Imlib2 gives back a "raw frame", we need to blend it on top of the |
| 486 | + | * previous frame ourselves if necessary to get the fully decoded frame. |
| 487 | + | */ |
| 488 | + | pflag = m->length = m->cnt = m->sel = 0; |
| 489 | + | px = py = pw = ph = 0; |
| 490 | + | for (n = 1; n <= fcnt; ++n) { |
| 491 | + | Imlib_Image frame, canvas; |
| 492 | + | int sx, sy, sw, sh; |
| 493 | + | bool has_alpha; |
| 494 | + | |
| 495 | + | imlib_context_set_image(m->cnt < 1 ? blank : m->frames[m->cnt - 1].im); |
| 496 | + | if ((canvas = imlib_clone_image()) == NULL || |
| 497 | + | (frame = imlib_load_image_frame(file->path, n)) == NULL) |
| 498 | + | { |
| 499 | + | if (canvas != NULL) { |
| 500 | + | imlib_context_set_image(canvas); |
| 501 | + | imlib_free_image(); |
| 502 | + | } |
| 503 | + | error(0, 0, "%s: failed to load frame %d", file->name, n); |
| 504 | + | break; |
| 505 | + | } |
| 506 | + | |
| 507 | + | imlib_context_set_image(frame); |
| 508 | + | imlib_image_get_frame_info(&finfo); |
| 509 | + | assert(finfo.frame_count == (int)fcnt); |
| 510 | + | assert(finfo.canvas_w == img->w && finfo.canvas_h == img->h); |
| 511 | + | sx = finfo.frame_x; |
| 512 | + | sy = finfo.frame_y; |
| 513 | + | sw = finfo.frame_w; |
| 514 | + | sh = finfo.frame_h; |
| 515 | + | has_alpha = imlib_image_has_alpha(); |
| 516 | + | |
| 517 | + | imlib_context_set_image(canvas); |
| 518 | + | /* the dispose flags are explained in Imlib2's header */ |
| 519 | + | if (pflag & IMLIB_FRAME_DISPOSE_CLEAR) { |
| 520 | + | img_area_clear(px, py, pw, ph); |
| 521 | + | } else if (pflag & IMLIB_FRAME_DISPOSE_PREV) { |
| 522 | + | Imlib_Image p = m->cnt < 2 ? blank : m->frames[m->cnt - 2].im; |
| 523 | + | assert(m->cnt > 0); |
| 524 | + | img_area_clear(0, 0, img->w, img->h); |
| 525 | + | imlib_blend_image_onto_image(p, 1, px, py, pw, ph, px, py, pw, ph); |
| 526 | + | } |
| 527 | + | pflag = finfo.frame_flags; |
| 528 | + | if (pflag & (IMLIB_FRAME_DISPOSE_CLEAR | IMLIB_FRAME_DISPOSE_PREV)) { |
| 529 | + | /* remember these so we can "dispose" them before blending next frame */ |
| 530 | + | px = sx; |
| 531 | + | py = sy; |
| 532 | + | pw = sw; |
| 533 | + | ph = sh; |
| 534 | + | } |
| 535 | + | assert(imlib_context_get_operation() == IMLIB_OP_COPY); |
| 536 | + | imlib_image_set_has_alpha(has_alpha); |
| 537 | + | imlib_context_set_blend(!!(finfo.frame_flags & IMLIB_FRAME_BLEND)); |
| 538 | + | imlib_blend_image_onto_image(frame, has_alpha, 0, 0, sw, sh, sx, sy, sw, sh); |
| 539 | + | m->frames[m->cnt].im = canvas; |
| 540 | + | m->frames[m->cnt].delay = finfo.frame_delay; |
| 541 | + | m->length += m->frames[m->cnt].delay; |
| 542 | + | m->cnt++; |
| 543 | + | imlib_context_set_image(frame); |
| 544 | + | imlib_free_image(); |
| 545 | + | } |
| 546 | + | imlib_context_set_image(blank); |
| 547 | + | imlib_free_image(); |
| 548 | + | img_multiframe_context_set(img); |
| 549 | + | return m->cnt > 0; |
| 550 | + | } |
| 551 | + | #endif /* HAVE_IMLIB2_MULTI_FRAME */ |
| 552 | + | |
| 411 | 553 | | Imlib_Image img_open(const fileinfo_t *file) |
| 412 | 554 | | { |
| 413 | 555 | | struct stat st; |
| @@ -415,7 +557,11 @@ Imlib_Image img_open(const fileinfo_t *file) |
| 415 | 557 | | |
| 416 | 558 | | if (access(file->path, R_OK) == 0 && |
| 417 | 559 | | stat(file->path, &st) == 0 && S_ISREG(st.st_mode) && |
| 560 | + | #if HAVE_IMLIB2_MULTI_FRAME |
| 561 | + | (im = imlib_load_image_frame(file->path, 1)) != NULL) |
| 562 | + | #else |
| 418 | 563 | | (im = imlib_load_image_immediately(file->path)) != NULL) |
| 564 | + | #endif |
| 419 | 565 | | { |
| 420 | 566 | | imlib_context_set_image(im); |
| 421 | 567 | | } |
| @@ -427,6 +573,7 @@ Imlib_Image img_open(const fileinfo_t *file) |
| 427 | 573 | | bool img_load(img_t *img, const fileinfo_t *file) |
| 428 | 574 | | { |
| 429 | 575 | | const char *fmt; |
| 576 | + | bool animated = false; |
| 430 | 577 | | |
| 431 | 578 | | if ((img->im = img_open(file)) == NULL) |
| 432 | 579 | | return false; |
| @@ -441,12 +588,16 @@ bool img_load(img_t *img, const fileinfo_t *file) |
| 441 | 588 | | exif_auto_orientate(file); |
| 442 | 589 | | #endif |
| 443 | 590 | | |
| 591 | + | #if HAVE_IMLIB2_MULTI_FRAME |
| 592 | + | animated = img_load_multiframe(img, file); |
| 593 | + | #endif |
| 594 | + | |
| 444 | 595 | | if ((fmt = imlib_image_format()) != NULL) { /* NOLINT: fmt might be unused, not worth fixing */ |
| 445 | | − | #if HAVE_LIBGIF |
| 596 | + | #if HAVE_LIBGIF && !HAVE_IMLIB2_MULTI_FRAME |
| 446 | 597 | | if (STREQ(fmt, "gif")) |
| 447 | 598 | | img_load_gif(img, file); |
| 448 | 599 | | #endif |
| 449 | | − | #if HAVE_LIBWEBP |
| 600 | + | #if HAVE_LIBWEBP && !HAVE_IMLIB2_MULTI_FRAME |
| 450 | 601 | | if (STREQ(fmt, "webp")) |
| 451 | 602 | | img_load_webp(img, file); |
| 452 | 603 | | #endif |
| @@ -455,8 +606,13 @@ bool img_load(img_t *img, const fileinfo_t *file) |
| 455 | 606 | | exif_auto_orientate(file); |
| 456 | 607 | | #endif |
| 457 | 608 | | } |
| 458 | | − | img->w = imlib_image_get_width(); |
| 459 | | − | img->h = imlib_image_get_height(); |
| 609 | + | /* for animated images, we want the _canvas_ width/height, which |
| 610 | + | * img_load_multiframe() sets already. |
| 611 | + | */ |
| 612 | + | if (!animated) { |
| 613 | + | img->w = imlib_image_get_width(); |
| 614 | + | img->h = imlib_image_get_height(); |
| 615 | + | } |
| 460 | 616 | | img->checkpan = true; |
| 461 | 617 | | img->dirty = true; |
| 462 | 618 | | |
| @@ -466,20 +622,18 @@ bool img_load(img_t *img, const fileinfo_t *file) |
| 466 | 622 | | CLEANUP void img_close(img_t *img, bool decache) |
| 467 | 623 | | { |
| 468 | 624 | | unsigned int i; |
| 625 | + | void (*free_img)(void) = decache ? imlib_free_image_and_decache : imlib_free_image; |
| 469 | 626 | | |
| 470 | 627 | | if (img->multi.cnt > 0) { |
| 471 | 628 | | for (i = 0; i < img->multi.cnt; i++) { |
| 472 | 629 | | imlib_context_set_image(img->multi.frames[i].im); |
| 473 | | − | imlib_free_image(); |
| 630 | + | free_img(); |
| 474 | 631 | | } |
| 475 | 632 | | img->multi.cnt = 0; |
| 476 | 633 | | img->im = NULL; |
| 477 | 634 | | } else if (img->im != NULL) { |
| 478 | 635 | | imlib_context_set_image(img->im); |
| 479 | | − | if (decache) |
| 480 | | − | imlib_free_image_and_decache(); |
| 481 | | − | else |
| 482 | | − | imlib_free_image(); |
| 636 | + | free_img(); |
| 483 | 637 | | img->im = NULL; |
| 484 | 638 | | } |
| 485 | 639 | | } |
| @@ -629,6 +783,8 @@ void img_render(img_t *img) |
| 629 | 783 | | imlib_context_set_color(c.red >> 8, c.green >> 8, c.blue >> 8, 0xFF); |
| 630 | 784 | | imlib_image_fill_rectangle(0, 0, dw, dh); |
| 631 | 785 | | } |
| 786 | + | imlib_context_set_blend(1); |
| 787 | + | imlib_context_set_operation(IMLIB_OP_COPY); |
| 632 | 788 | | imlib_blend_image_onto_image(img->im, 0, sx, sy, sw, sh, 0, 0, dw, dh); |
| 633 | 789 | | imlib_context_set_color_modifier(NULL); |
| 634 | 790 | | imlib_render_image_on_drawable(dx, dy); |