Skip to content

Instantly share code, notes, and snippets.

@tsutsui
Created August 15, 2023 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsutsui/313767b1aa7a5af96d2a7c1058e187fc to your computer and use it in GitHub Desktop.
Save tsutsui/313767b1aa7a5af96d2a7c1058e187fc to your computer and use it in GitHub Desktop.
libwebp MODE_RGB decoding doesn't handle Alpha value
PROG= webp-test-MODE_RGB
SRC= webp-test-MODE_RGB.c
#PREFIX= /usr/pkg
PREFIX= /usr
CFLAGS= -I${PREFIX}/include
LDFLAGS= -L${PREFIX}/lib -Wl,-R${PREFIX}/lib -lwebp
WEBPIMG= 3_webp_ll.webp
all: ${PROG}
${PROG}: ${SRC}
cc ${SRC} -o ${PROG} ${CFLAGS} ${LDFLAGS}
test: ${PROG} ${WEBPIMG}
./${PROG}
${WEBPIMG}:
wget https://www.gstatic.com/webp/gallery3/${WEBPIMG}
clean:
rm -f ${PROG}
rm -f 3_webp_ll_RGB.webp
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <err.h>
#include <sys/stat.h>
#include <webp/types.h>
#include <webp/encode.h>
#include <webp/decode.h>
#define IFNAME "3_webp_ll.webp"
#define OFNAME "3_webp_ll_RGB.webp"
int
main(int argc, char *argv[])
{
FILE *ifp, *ofp;
struct stat sb;
int fd;
int width, height, stride;
size_t filesize, readsize, outsize, writesize;
uint8_t *filebuf, *ipic, *opic;
fd = open(IFNAME, O_RDONLY);
if (fd == -1) {
errx(EXIT_FAILURE, "failed to open input file");
}
if (fstat(fd, &sb) == -1) {
errx(EXIT_FAILURE, "failed to stat input file");
}
filesize = sb.st_size;
filebuf = calloc(filesize, 1);
if (filebuf == NULL)
errx(EXIT_FAILURE, "cannot allocate input buffer");
ifp = fdopen(fd, "r");
if (ifp == NULL) {
errx(EXIT_FAILURE, "failed to open input stream");
}
readsize = fread(filebuf, 1, filesize, ifp);
if (readsize != filesize) {
errx(EXIT_FAILURE, "failed to read input file");
}
fclose(ifp);
close(fd);
/* decode transparent webp using MODE_RGB */
ipic = WebPDecodeRGB(filebuf, filesize, &width, &height);
free(filebuf);
stride = width * 3; /* Decoded ipic has RGB without A */
outsize = WebPEncodeLosslessRGB(ipic, width, height, stride, &opic);
WebPFree(ipic);
ofp = fopen(OFNAME, "w");
if (ofp == NULL) {
errx(EXIT_FAILURE, "failed to open output file");
}
writesize = fwrite(opic, 1, outsize, ofp);
WebPFree(opic);
if (writesize != outsize) {
errx(EXIT_FAILURE, "failed to write output file");
}
fclose(ofp);
exit(EXIT_SUCCESS);
}
@tsutsui
Copy link
Author

tsutsui commented Aug 15, 2023

webp-trans
webp-rgb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment