1 Patch committed upstream
2 https://bitbucket.org/libgd/gd-libgd/commits/a79232c5fa692c3b6e3f5bc95ecfc455424c3f54?at=master
5 https://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/media-libs/gd/files/gd-2.1.1-webp.patch?revision=1.1&view=markup
7 Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
10 https://bugs.gentoo.org/545956
12 From a79232c5fa692c3b6e3f5bc95ecfc455424c3f54 Mon Sep 17 00:00:00 2001
13 From: Pierre Joye <pierre.php@gmail.com>
14 Date: Tue, 20 Jan 2015 04:55:11 +0100
15 Subject: [PATCH] fix #129, drop VPX usage in favor of libwebp
18 configure.ac | 80 +++++------------
19 src/gd_webp.c | 231 +++++++++++++++++++++-----------------------------
20 tests/Makefile.am | 2 +-
21 tests/webp/bug00111.c | 2 +-
22 4 files changed, 122 insertions(+), 193 deletions(-)
24 diff --git a/configure.ac b/configure.ac
25 index 1024a3a..8923186 100644
28 @@ -317,63 +317,6 @@ if test "$with_xpm" != no; then
30 AM_CONDITIONAL([HAVE_LIBXPM], test "$with_xpm" = yes)
32 -dnl check for libvpx by default
34 -[ --with-vpx=DIR where to find the vpx library])
39 - PKG_CHECK_MODULES([LIBVPX], vpx, [with_vpx=yes],
41 - PKG_CHECK_MODULES([LIBVPX], libvpx, [with_vpx=yes],
43 - if test "$with_vpx" = yes; then
44 - AC_MSG_ERROR([VPX support requested, but not found])
52 - save_CPPFLAGS="$CPPFLAGS"
54 - if test -d "$with_vpx"; then
55 - LIBVPX_CFLAGS="-I$with_vpx/include"
56 - LIBVPX_LIBS="-L$with_vpx/lib -lvpx"
59 - CPPFLAGS="$CPPFLAGS $LIBVPX_CFLAGS"
60 - LIBS="$LIBS $LIBVPX_LIBS"
62 - AC_CHECK_LIB(vpx,vpx_codec_destroy,
64 - if test -z "$LIBVPX_LIBS"; then
69 - if test "$with_vpx" != ""; then
70 - AC_MSG_ERROR([vpx support requested, but not found])
76 - CPPFLAGS="$save_CPPFLAGS"
81 -if test "$with_vpx" != no; then
82 - CPPFLAGS="$CPPFLAGS $LIBVPX_CFLAGS"
83 - LIBS="$LIBS $LIBVPX_LIBS"
84 - FEATURES="GD_VPX $FEATURES"
85 - AC_DEFINE(HAVE_LIBVPX, 1, [ Define if you have the VPX library. ])
87 -AM_CONDITIONAL([HAVE_LIBVPX], test "$with_vpx" = yes)
89 dnl check for libtiff by default
91 [ --with-tiff=DIR where to find the TIFF library])
92 @@ -437,6 +380,27 @@ if test "$mingw_cv_win32_host" = yes; then
93 AC_DEFINE([BGDWIN32], [], [Define is you are building for Win32 API])
97 +dnl check for libwebp by default
99 +[ --with-webp=DIR where to find the webp library],
100 + [if test -d "$withval"; then
101 + LDFLAGS="$LDFLAGS -L$withval/lib"
102 + CFLAGS="$CFLAGS -I$withval/include"
106 +if test "$withval" != no; then
107 + AC_CHECK_LIB(webp,WebPGetInfo,
108 + [LIBS="-lwebp $LIBS"
109 + FEATURES="GD_WEBP $FEATURES"
110 + AC_DEFINE(HAVE_LIBWEBP, 1, [ Define if you have the webp library. ])])
115 +AM_CONDITIONAL([HAVE_LIBWEBP], test "$with_webp" = yes)
117 dnl report configuration
119 ** Configuration summary for $PACKAGE $VERSION:
120 @@ -444,7 +408,7 @@ AC_MSG_RESULT([
121 Support for Zlib: $with_zlib
122 Support for PNG library: $with_png
123 Support for JPEG library: $ac_cv_lib_jpeg_jpeg_set_defaults
124 - Support for VPX library: $with_vpx
125 + Support for WebP library: $with_webp
126 Support for TIFF library: $with_tiff
127 Support for Freetype 2.x library: $with_freetype
128 Support for Fontconfig library: $with_fontconfig
129 diff --git a/src/gd_webp.c b/src/gd_webp.c
130 index a3ae1ac..c44bd80 100644
135 #endif /* HAVE_CONFIG_H */
144 #include "gd_errors.h"
147 -#include "webpimg.h"
148 #include "gdhelpers.h"
149 +#include "webp/decode.h"
150 +#include "webp/encode.h"
152 -extern void gd_YUV420toRGBA(uint8* Y,
157 -extern void gd_RGBAToYUV420(gdImagePtr im2,
162 -const char * gdWebpGetVersionString()
164 - return "not defined";
166 +#define GD_WEBP_ALLOC_STEP (4*1024)
168 -BGD_DECLARE(gdImagePtr) gdImageCreateFromWebp (FILE * inFile)
169 +gdImagePtr gdImageCreateFromWebp (FILE * inFile)
172 gdIOCtx *in = gdNewFileCtx(inFile);
173 @@ -38,42 +26,16 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromWebp (FILE * inFile)
177 -BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpPtr (int size, void *data)
178 +gdImagePtr gdImageCreateFromWebpCtx (gdIOCtx * infile)
180 - int width, height, ret;
181 - unsigned char *Y = NULL;
182 - unsigned char *U = NULL;
183 - unsigned char *V = NULL;
186 - ret = WebPDecode(data, size, &Y, &U, &V, &width, &height);
187 - if (ret != webp_success) {
191 - gd_error("WebP decode: fail to decode input data");
194 - im = gdImageCreateTrueColor(width, height);
198 - gd_YUV420toRGBA(Y, U, V, im);
202 -#define GD_WEBP_ALLOC_STEP (4*1024)
204 -BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpCtx (gdIOCtx * infile)
206 - int width, height, ret;
207 - unsigned char *filedata = NULL;
209 + uint8_t *filedata = NULL;
210 + uint8_t *argb = NULL;
211 unsigned char *read, *temp;
212 - unsigned char *Y = NULL;
213 - unsigned char *U = NULL;
214 - unsigned char *V = NULL;
221 temp = gdRealloc(filedata, size+GD_WEBP_ALLOC_STEP);
222 @@ -89,23 +51,97 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpCtx (gdIOCtx * infile)
225 n = gdGetBuf(read, GD_WEBP_ALLOC_STEP, infile);
228 + if (n>0 && n!=EOF) {
231 + } while (n>0 && n!=EOF);
233 - ret = WebPDecode(filedata, size, &Y, &U, &V, &width, &height);
235 - if (ret != webp_success) {
239 - gd_error("WebP decode: fail to decode input data");
240 + if (WebPGetInfo(filedata,size, &width, &height) == 0) {
241 + gd_error("gd-webp cannot get webp info");
245 im = gdImageCreateTrueColor(width, height);
246 - gd_YUV420toRGBA(Y, U, V, im);
250 + argb = WebPDecodeARGB(filedata, size, &width, &height);
252 + gd_error("gd-webp cannot allocate temporary buffer");
256 + for (y = 0, p = argb; y < height; y++) {
257 + for (x = 0; x < width; x++) {
258 + register uint8_t a = gdAlphaMax - (*(p++) >> 1);
259 + register uint8_t r = *(p++);
260 + register uint8_t g = *(p++);
261 + register uint8_t b = *(p++);
262 + im->tpixels[y][x] = gdTrueColorAlpha(r, g, b, a);
267 + im->saveAlphaFlag = 1;
271 +void gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quantization)
283 + if (!gdImageTrueColor(im)) {
284 + gd_error("Paletter image not supported by webp");
288 + if (quantization == -1) {
292 + argb = (uint8_t *)gdMalloc(gdImageSX(im) * 4 * gdImageSY(im));
297 + for (y = 0; y < gdImageSY(im); y++) {
298 + for (x = 0; x < gdImageSX(im); x++) {
301 + c = im->tpixels[y][x];
302 + a = gdTrueColorGetAlpha(c);
306 + a = 255 - ((a << 1) + (a >> 6));
308 + *(p++) = gdTrueColorGetRed(c);
309 + *(p++) = gdTrueColorGetGreen(c);
310 + *(p++) = gdTrueColorGetBlue(c);
314 + out_size = WebPEncodeRGBA(argb, gdImageSX(im), gdImageSY(im), gdImageSX(im) * 4, quantization, &out);
315 + printf("outsize: %i\n", out_size);
316 + if (out_size == 0) {
317 + gd_error("gd-webp encoding failed");
320 + gdPutBuf(out, out_size, outfile);
327 BGD_DECLARE(void) gdImageWebpEx (gdImagePtr im, FILE * outFile, int quantization)
329 gdIOCtx *out = gdNewFileCtx(outFile);
330 @@ -116,7 +152,7 @@ BGD_DECLARE(void) gdImageWebpEx (gdImagePtr im, FILE * outFile, int quantization
331 BGD_DECLARE(void) gdImageWebp (gdImagePtr im, FILE * outFile)
333 gdIOCtx *out = gdNewFileCtx(outFile);
334 - gdImageWebpCtx(im, out, -1);
335 + gdImageWebpCtx(im, out, -1);
339 @@ -140,75 +176,4 @@ BGD_DECLARE(void *) gdImageWebpPtrEx (gdImagePtr im, int *size, int quantization
345 - * Maps normalized QP (quality) to VP8 QP
347 -int mapQualityToVP8QP(int quality) {
348 -#define MIN_QUALITY 0
349 -#define MAX_QUALITY 100
351 -#define MAX_VP8QP 63
352 - const float scale = MAX_VP8QP - MIN_VP8QP;
353 - const float vp8qp =
354 - scale * (MAX_QUALITY - quality) / (MAX_QUALITY - MIN_QUALITY) + MIN_VP8QP;
355 - if (quality < MIN_QUALITY || quality > MAX_QUALITY) {
356 - gd_error("Wrong quality value %d.", quality);
360 - return (int)(vp8qp + 0.5);
363 -/* This routine is based in part on code from Dale Lutz (Safe Software Inc.)
364 - * and in part on demo code from Chapter 15 of "PNG: The Definitive Guide"
365 - * (http://www.cdrom.com/pub/png/pngbook.html).
367 -BGD_DECLARE(void) gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quantization)
369 - int width = im->sx;
370 - int height = im->sy;
372 - int yuv_width, yuv_height, yuv_nbytes, ret;
374 - unsigned char *Y = NULL,
377 - unsigned char *filedata = NULL;
379 - /* Conversion to Y,U,V buffer */
380 - yuv_width = (width + 1) >> 1;
381 - yuv_height = (height + 1) >> 1;
382 - yuv_nbytes = width * height + 2 * yuv_width * yuv_height;
384 - if ((Y = (unsigned char *)gdCalloc(yuv_nbytes, sizeof(unsigned char))) == NULL) {
385 - gd_error("gd-webp error: cannot allocate Y buffer");
388 - if (quantization == -1) {
391 - vp8_quality = mapQualityToVP8QP(quantization);
393 - U = Y + width * height;
394 - V = U + yuv_width * yuv_height;
395 - gd_RGBAToYUV420(im, Y, U, V);
397 - /* Encode Y,U,V and write data to file */
398 - ret = WebPEncode(Y, U, V, width, height, width, yuv_width, yuv_height, yuv_width,
399 - vp8_quality, &filedata, &yuv_nbytes, NULL);
402 - if (ret != webp_success) {
406 - gd_error("gd-webp error: WebP Encoder failed");
410 - gdPutBuf (filedata, yuv_nbytes, outfile);
414 -#endif /* HAVE_LIBVPX */
415 +#endif /* HAVE_LIBWEBP */