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.

ref: https://phab.enlightenment.org/T8964
This commit is contained in:
NRK 2021-11-06 20:58:50 +06:00 committed by N-R-K
parent 7a94a5ecc6
commit eccd7de532
2 changed files with 46 additions and 85 deletions

View File

@ -76,6 +76,7 @@ The following dependencies are optional.
* libexif : Used for auto-orientation and exif thumbnails. * libexif : Used for auto-orientation and exif thumbnails.
Disable via `HAVE_LIBEXIF=0` Disable via `HAVE_LIBEXIF=0`
* libwebp : Used for animated webp playback. * libwebp : Used for animated webp playback.
(NOTE: animated webp also requires Imlib2 v1.7.5 or above)
Disabled via `HAVE_LIBWEBP=0`. Disabled via `HAVE_LIBWEBP=0`.
Please make sure to install the corresponding development packages in case that Please make sure to install the corresponding development packages in case that

130
image.c
View File

@ -298,28 +298,7 @@ static bool img_load_gif(img_t *img, const fileinfo_t *file)
#if HAVE_LIBWEBP #if HAVE_LIBWEBP
static bool is_webp(const char *path) static bool img_load_webp(img_t *img, const fileinfo_t *file)
{
/* The size (in bytes) of the largest amount of data required to verify a WebP image. */
enum { max = 30 };
const unsigned char fmt[max];
bool ret = false;
FILE *f;
if ((f = fopen(path, "rb")) != NULL) {
if (fread((unsigned char *) fmt, 1, max, f) == max)
ret = WebPGetInfo(fmt, max, NULL, NULL);
fclose(f);
}
return ret;
}
/* fframe img
* NULL NULL = do nothing
* x NULL = load the first frame as an Imlib_Image
* NULL x = load all frames into img->multi.
*/
static bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *img)
{ {
FILE *webp_file; FILE *webp_file;
WebPData data; WebPData data;
@ -335,10 +314,8 @@ static bool img_load_webp(const fileinfo_t *file, Imlib_Image *fframe, img_t *im
unsigned long flags; unsigned long flags;
unsigned int delay; unsigned int delay;
bool err = false; bool err = false;
data.bytes = NULL;
if ((err = fframe == NULL && img == NULL)) data.bytes = NULL;
goto fail;
if ((err = (webp_file = fopen(file->path, "rb")) == NULL)) { if ((err = (webp_file = fopen(file->path, "rb")) == NULL)) {
error(0, 0, "%s: Error opening webp image", file->name); 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
} }
demux = WebPAnimDecoderGetDemuxer(dec); demux = WebPAnimDecoderGetDemuxer(dec);
if (img == NULL) { /* Only get the first frame and put it into fframe. */ /* Get global information for the image */
if ((err = !WebPAnimDecoderGetNext(dec, &buf, &ts))) { flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);
error(0, 0, "%s: Error loading first frame", file->name); img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
goto fail; img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
}
*fframe = imlib_create_image_using_copied_data(
info.canvas_width, info.canvas_height, (DATA32*)buf);
imlib_context_set_image(*fframe);
imlib_image_set_has_alpha(WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS) & ALPHA_FLAG);
} else {
/* Get global information for the image */
flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);
img->w = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
img->h = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
if (img->multi.cap == 0) { if (img->multi.cap == 0) {
img->multi.cap = info.frame_count; img->multi.cap = info.frame_count;
img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t)); img->multi.frames = emalloc(img->multi.cap * sizeof(img_frame_t));
} else if (info.frame_count > img->multi.cap) { } else if (info.frame_count > img->multi.cap) {
img->multi.cap = info.frame_count; img->multi.cap = info.frame_count;
img->multi.frames = erealloc(img->multi.frames, img->multi.frames = erealloc(img->multi.frames,
img->multi.cap * sizeof(img_frame_t)); img->multi.cap * sizeof(img_frame_t));
}
/* Load and decode frames (also works on images with only 1 frame) */
img->multi.cnt = img->multi.sel = 0;
while (WebPAnimDecoderGetNext(dec, &buf, &ts)) {
im = imlib_create_image_using_copied_data(
info.canvas_width, info.canvas_height, (DATA32*)buf);
imlib_context_set_image(im);
imlib_image_set_format("webp");
/* Get an iterator of this frame - used for frame info (duration, etc.) */
WebPDemuxGetFrame(demux, img->multi.cnt+1, &iter);
imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG);
/* Store info for this frame */
img->multi.frames[img->multi.cnt].im = im;
delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY;
img->multi.frames[img->multi.cnt].delay = delay;
img->multi.length += img->multi.frames[img->multi.cnt].delay;
img->multi.cnt++;
}
WebPDemuxReleaseIterator(&iter);
if (img->multi.cnt > 1) {
imlib_context_set_image(img->im);
imlib_free_image();
img->im = img->multi.frames[0].im;
} else if (img->multi.cnt == 1) {
imlib_context_set_image(img->multi.frames[0].im);
imlib_free_image();
img->multi.cnt = 0;
}
imlib_context_set_image(img->im);
} }
imlib_image_set_format("webp");
/* Load and decode frames (also works on images with only 1 frame) */
img->multi.cnt = img->multi.sel = 0;
while (WebPAnimDecoderGetNext(dec, &buf, &ts)) {
im = imlib_create_image_using_copied_data(
info.canvas_width, info.canvas_height, (DATA32*)buf);
imlib_context_set_image(im);
imlib_image_set_format("webp");
/* Get an iterator of this frame - used for frame info (duration, etc.) */
WebPDemuxGetFrame(demux, img->multi.cnt+1, &iter);
imlib_image_set_has_alpha((flags & ALPHA_FLAG) == ALPHA_FLAG);
/* Store info for this frame */
img->multi.frames[img->multi.cnt].im = im;
delay = iter.duration > 0 ? iter.duration : DEF_WEBP_DELAY;
img->multi.frames[img->multi.cnt].delay = delay;
img->multi.length += img->multi.frames[img->multi.cnt].delay;
img->multi.cnt++;
}
WebPDemuxReleaseIterator(&iter);
if (img->multi.cnt > 1) {
imlib_context_set_image(img->im);
imlib_free_image();
img->im = img->multi.frames[0].im;
} else if (img->multi.cnt == 1) {
imlib_context_set_image(img->multi.frames[0].im);
imlib_free_image();
img->multi.cnt = 0;
}
imlib_context_set_image(img->im);
fail: fail:
if (dec != NULL) if (dec != NULL)
WebPAnimDecoderDelete(dec); WebPAnimDecoderDelete(dec);
@ -439,12 +404,7 @@ Imlib_Image img_open(const fileinfo_t *file)
if (access(file->path, R_OK) == 0 && if (access(file->path, R_OK) == 0 &&
stat(file->path, &st) == 0 && S_ISREG(st.st_mode)) stat(file->path, &st) == 0 && S_ISREG(st.st_mode))
{ {
#if HAVE_LIBWEBP im = imlib_load_image(file->path);
if (is_webp(file->path))
img_load_webp(file, &im, NULL);
else
#endif
im = imlib_load_image(file->path);
if (im != NULL) { if (im != NULL) {
imlib_context_set_image(im); imlib_context_set_image(im);
if (imlib_image_get_data_for_reading_only() == NULL) { if (imlib_image_get_data_for_reading_only() == NULL) {
@ -478,7 +438,7 @@ bool img_load(img_t *img, const fileinfo_t *file)
#endif #endif
#if HAVE_LIBWEBP #if HAVE_LIBWEBP
if (STREQ(fmt, "webp")) if (STREQ(fmt, "webp"))
img_load_webp(file, NULL, img); img_load_webp(img, file);
#endif #endif
} }
img->w = imlib_image_get_width(); img->w = imlib_image_get_width();