From 65a150e7877af228fe05b9b22497709aa4f9133f Mon Sep 17 00:00:00 2001 From: Leonardo Hernández Hernández Date: Sun, 2 Oct 2022 22:49:05 -0500 Subject: webp: intial support for WebP images, using libwebp --- webp.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 webp.c (limited to 'webp.c') diff --git a/webp.c b/webp.c new file mode 100644 index 0000000..82aa493 --- /dev/null +++ b/webp.c @@ -0,0 +1,73 @@ +#include "webp.h" +#include +#include + +#include + +#define LOG_MODULE "webp" +#define LOG_ENABLE_DBG 0 +#include "log.h" +#include "stride.h" + +pixman_image_t * +webp_load(FILE *fp, const char *path) +{ + uint8_t *file_data = NULL; + uint8_t *image_data = NULL; + size_t image_size; + pixman_image_t *pix = NULL; + pixman_format_code_t format; + int width, height, stride; + + if (fseek(fp, 0, SEEK_END) < 0) { + LOG_ERRNO("%s: failed to seek to end of file", path); + return NULL; + } + image_size = ftell(fp); + if (fseek(fp, 0, SEEK_SET) < 0) { + LOG_ERRNO("%s: failed to seek to beginning of file", path); + return NULL; + } + + if (!(file_data = WebPMalloc(image_size + 1))) { + goto err; + } + + clearerr(fp); + fread(file_data, image_size, 1, fp); + if (ferror(fp)) { + LOG_ERRNO("%s: failed to read", path); + goto err; + } + file_data[image_size] = '\0'; + + /* Verify it is a webp image */ + if (!WebPGetInfo(file_data, image_size, NULL, NULL)) { + LOG_ERR("%s: not a WebP file", path); + goto err; + } + + image_data = WebPDecodeRGBA(file_data, image_size, &width, &height); + if (image_data == NULL) { + goto err; + } + format = PIXMAN_x8b8g8r8; + stride = stride_for_format_and_width(format, width); + + pix = pixman_image_create_bits_no_clear( + format, width, height, (uint32_t *)image_data, stride); + + if (pix == NULL) { + LOG_ERR("%s: failed to instantiate pixman image", path); + goto err; + } + + WebPFree(file_data); + return pix; + +err: + WebPFree(file_data); + WebPFree(image_data); + + return NULL; +} -- cgit v1.2.3