nsxiveccd7de
remove unneeded animated-webp hacks
now that imlib2 (v1.7.5) is able to load the first frame of an animated-webp file, we no longer need the `is_webp` check to bypass imlib2.
+44 −83
2 files changed
| @@ -76,6 +76,7 @@ The following dependencies are optional. | |||
| 76 | 76 | * libexif : Used for auto-orientation and exif thumbnails. | |
| 77 | 77 | Disable via `HAVE_LIBEXIF=0` | |
| 78 | 78 | * libwebp : Used for animated webp playback. | |
| 79 | + | (NOTE: animated webp also requires Imlib2 v1.7.5 or above) | |
| 79 | 80 | Disabled via `HAVE_LIBWEBP=0`. | |
| 80 | 81 | ||
| 81 | 82 | Please make sure to install the corresponding development packages in case that | |
| @@ -298,28 +298,7 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 298 | 298 | ||
| 299 | 299 | ||
| 300 | 300 | #if HAVE_LIBWEBP | |
| 301 | − | static bool is_webp(const char *path) | |
| 302 | − | { | |
| 303 | − | /* The size (in bytes) of the largest amount of data required to verify a WebP image. */ | |
| 304 | − | enum { max = 30 }; | |
| 305 | − | const unsigned char fmt[max]; | |
| 306 | − | bool ret = false; | |
| 307 | − | FILE *f; | |
| 308 | − | ||
| 309 | − | if ((f = fopen(path, "rb")) != NULL) { | |
| 310 | − | if (fread((unsigned char *) fmt, 1, max, f) == max) | |
| 311 | − | ret = WebPGetInfo(fmt, max, NULL, NULL); | |
| 312 | − | fclose(f); | |
| 313 | − | } | |
| 314 | − | return ret; | |
| 315 | − | } | |
| 316 | − | ||
| 317 | − | /* fframe img | |
| 318 | − | * NULL NULL = do nothing | |
| 319 | − | * x NULL = load the first frame as an Imlib_Image | |
| 320 | − | * NULL x = load all frames into img->multi. | |
| 321 | − | */ | |
| 322 | − | static bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img) | |
| 301 | + | static bool img_load_webp(img_t *img, const fileinfo_t *file) | |
| 323 | 302 | { | |
| 324 | 303 | FILE *webp_file; | |
| 325 | 304 | WebPData data; | |
| @@ -335,10 +314,8 @@ static bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *im | |||
| 335 | 314 | unsigned long flags; | |
| 336 | 315 | unsigned int delay; | |
| 337 | 316 | bool err = false; | |
| 338 | − | data.bytes = NULL; | |
| 339 | 317 | ||
| 340 | − | if ((err = fframe == NULL && img == NULL)) | |
| 341 | − | goto fail; | |
| 318 | + | data.bytes = NULL; | |
| 342 | 319 | ||
| 343 | 320 | if ((err = (webp_file = fopen(file->path, "rb")) == NULL)) { | |
| 344 | 321 | error(0, 0, "%s: Error opening webp image", file->name); | |
| @@ -368,61 +345,49 @@ static bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *im | |||
| 368 | 345 | } | |
| 369 | 346 | demux = WebPAnimDecoderGetDemuxer(dec); | |
| 370 | 347 | ||
| 371 | − | if (img == NULL) { /* Only get the first frame and put it into fframe. */ | |
| 372 | − | if ((err = !WebPAnimDecoderGetNext(dec, &buf, &ts))) { | |
| 373 | − | error(0, 0, "%s: Error loading first frame", file->name); | |
| 374 | − | goto fail; | |
| 375 | − | } | |
| 376 | − | *fframe = imlib_create_image_using_copied_data( | |
| 377 | − | info.canvas_width, info.canvas_height, (DATA32*)buf); | |
| 378 | − | imlib_context_set_image(*fframe); | |
| 379 | − | imlib_image_set_has_alpha(WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS) & ALPHA_FLAG); | |
| 380 | − | } else { | |
| 381 | − | /* Get global information for the image */ | |
| 382 | − | flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS); | |
| 383 | − | img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH); | |
| 384 | − | img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT); | |
| 385 | − | ||
| 386 | − | if (img->multi.cap == 0) { | |
| 387 | − | img->multi.cap = info.frame_count; | |
| 388 | − | img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t)); | |
| 389 | − | } else if (info.frame_count > img->multi.cap) { | |
| 390 | − | img->multi.cap = info.frame_count; | |
| 391 | − | img->multi.frames = erealloc(img->multi.frames, | |
| 392 | − | img->multi.cap * sizeof(img_frame_t)); | |
| 393 | − | } | |
| 348 | + | /* Get global information for the image */ | |
| 349 | + | flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS); | |
| 350 | + | img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH); | |
| 351 | + | img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT); | |
| 394 | 352 | ||
| 395 | − | /* Load and decode frames (also works on images with only 1 frame) */ | |
| 396 | − | img->multi.cnt = img->multi.sel = 0; | |
| 397 | − | while (WebPAnimDecoderGetNext(dec, &buf, &ts)) { | |
| 398 | − | im = imlib_create_image_using_copied_data( | |
| 399 | − | info.canvas_width, info.canvas_height, (DATA32*)buf); | |
| 400 | − | imlib_context_set_image(im); | |
| 401 | − | imlib_image_set_format("webp"); | |
| 402 | − | /* Get an iterator of this frame - used for frame info (duration, etc.) */ | |
| 403 | − | WebPDemuxGetFrame(demux, img->multi.cnt+1, &iter); | |
| 404 | − | imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG); | |
| 405 | − | /* Store info for this frame */ | |
| 406 | − | img->multi.frames[img->multi.cnt].im = im; | |
| 407 | − | delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY; | |
| 408 | − | img->multi.frames[img->multi.cnt].delay = delay; | |
| 409 | − | img->multi.length += img->multi.frames[img->multi.cnt].delay; | |
| 410 | − | img->multi.cnt++; | |
| 411 | − | } | |
| 412 | − | WebPDemuxReleaseIterator(&iter); | |
| 353 | + | if (img->multi.cap == 0) { | |
| 354 | + | img->multi.cap = info.frame_count; | |
| 355 | + | img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t)); | |
| 356 | + | } else if (info.frame_count > img->multi.cap) { | |
| 357 | + | img->multi.cap = info.frame_count; | |
| 358 | + | img->multi.frames = erealloc(img->multi.frames, | |
| 359 | + | img->multi.cap * sizeof(img_frame_t)); | |
| 360 | + | } | |
| 413 | 361 | ||
| 414 | − | if (img->multi.cnt > 1) { | |
| 415 | − | imlib_context_set_image(img->im); | |
| 416 | − | imlib_free_image(); | |
| 417 | − | img->im = img->multi.frames[0].im; | |
| 418 | − | } else if (img->multi.cnt == 1) { | |
| 419 | − | imlib_context_set_image(img->multi.frames[0].im); | |
| 420 | − | imlib_free_image(); | |
| 421 | − | img->multi.cnt = 0; | |
| 422 | − | } | |
| 362 | + | /* Load and decode frames (also works on images with only 1 frame) */ | |
| 363 | + | img->multi.cnt = img->multi.sel = 0; | |
| 364 | + | while (WebPAnimDecoderGetNext(dec, &buf, &ts)) { | |
| 365 | + | im = imlib_create_image_using_copied_data( | |
| 366 | + | info.canvas_width, info.canvas_height, (DATA32*)buf); | |
| 367 | + | imlib_context_set_image(im); | |
| 368 | + | imlib_image_set_format("webp"); | |
| 369 | + | /* Get an iterator of this frame - used for frame info (duration, etc.) */ | |
| 370 | + | WebPDemuxGetFrame(demux, img->multi.cnt+1, &iter); | |
| 371 | + | imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG); | |
| 372 | + | /* Store info for this frame */ | |
| 373 | + | img->multi.frames[img->multi.cnt].im = im; | |
| 374 | + | delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY; | |
| 375 | + | img->multi.frames[img->multi.cnt].delay = delay; | |
| 376 | + | img->multi.length += img->multi.frames[img->multi.cnt].delay; | |
| 377 | + | img->multi.cnt++; | |
| 378 | + | } | |
| 379 | + | WebPDemuxReleaseIterator(&iter); | |
| 380 | + | ||
| 381 | + | if (img->multi.cnt > 1) { | |
| 423 | 382 | imlib_context_set_image(img->im); | |
| 383 | + | imlib_free_image(); | |
| 384 | + | img->im = img->multi.frames[0].im; | |
| 385 | + | } else if (img->multi.cnt == 1) { | |
| 386 | + | imlib_context_set_image(img->multi.frames[0].im); | |
| 387 | + | imlib_free_image(); | |
| 388 | + | img->multi.cnt = 0; | |
| 424 | 389 | } | |
| 425 | − | imlib_image_set_format("webp"); | |
| 390 | + | imlib_context_set_image(img->im); | |
| 426 | 391 | fail: | |
| 427 | 392 | if (dec != NULL) | |
| 428 | 393 | WebPAnimDecoderDelete(dec); | |
| @@ -439,12 +404,7 @@ Imlib_Image img_open(const fileinfo_t *file) | |||
| 439 | 404 | if (access(file->path, R_OK) == 0 && | |
| 440 | 405 | stat(file->path, &st) == 0 && S_ISREG(st.st_mode)) | |
| 441 | 406 | { | |
| 442 | − | #if HAVE_LIBWEBP | |
| 443 | − | if (is_webp(file->path)) | |
| 444 | − | img_load_webp(file, &im, NULL); | |
| 445 | − | else | |
| 446 | − | #endif | |
| 447 | − | im = imlib_load_image(file->path); | |
| 407 | + | im = imlib_load_image(file->path); | |
| 448 | 408 | if (im != NULL) { | |
| 449 | 409 | imlib_context_set_image(im); | |
| 450 | 410 | if (imlib_image_get_data_for_reading_only() == NULL) { | |
| @@ -478,7 +438,7 @@ bool img_load(img_t *img, const fileinfo_t *file) | |||
| 478 | 438 | #endif | |
| 479 | 439 | #if HAVE_LIBWEBP | |
| 480 | 440 | if (STREQ(fmt, "webp")) | |
| 481 | − | img_load_webp(file, NULL, img); | |
| 441 | + | img_load_webp(img, file); | |
| 482 | 442 | #endif | |
| 483 | 443 | } | |
| 484 | 444 | img->w = imlib_image_get_width(); | |