f648a8753671888c03d5fcb890ff423a687b676a
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / package / gd / 0005-webp.patch
1 Patch committed upstream
2 https://bitbucket.org/libgd/gd-libgd/commits/a79232c5fa692c3b6e3f5bc95ecfc455424c3f54?at=master
3
4 Downloaded from Gentoo
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
6
7 Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
8
9 ---
10 https://bugs.gentoo.org/545956
11
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
16
17 ---
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(-)
23
24 diff --git a/configure.ac b/configure.ac
25 index 1024a3a..8923186 100644
26 --- a/configure.ac
27 +++ b/configure.ac
28 @@ -317,63 +317,6 @@ if test "$with_xpm" != no; then
29  fi
30  AM_CONDITIONAL([HAVE_LIBXPM], test "$with_xpm" = yes)
31  
32 -dnl check for libvpx by default
33 -AC_ARG_WITH(vpx,dnl
34 -[  --with-vpx=DIR         where to find the vpx library])
35 -
36 -case $with_vpx in
37 -no) ;;
38 -yes|"")
39 -  PKG_CHECK_MODULES([LIBVPX], vpx, [with_vpx=yes],
40 -  [
41 -    PKG_CHECK_MODULES([LIBVPX], libvpx, [with_vpx=yes],
42 -    [
43 -      if test "$with_vpx" = yes; then
44 -        AC_MSG_ERROR([VPX support requested, but not found])
45 -      fi
46 -      with_vpx=no
47 -    ])
48 -  ])
49 -  ;;
50 -*)
51 -  save_LIBS="$LIBS"
52 -  save_CPPFLAGS="$CPPFLAGS"
53 -
54 -  if test -d "$with_vpx"; then
55 -    LIBVPX_CFLAGS="-I$with_vpx/include"
56 -    LIBVPX_LIBS="-L$with_vpx/lib -lvpx"
57 -  fi
58 -
59 -  CPPFLAGS="$CPPFLAGS $LIBVPX_CFLAGS"
60 -  LIBS="$LIBS $LIBVPX_LIBS"
61 -
62 -  AC_CHECK_LIB(vpx,vpx_codec_destroy,
63 -    [
64 -      if test -z "$LIBVPX_LIBS"; then
65 -        LIBVPX_LIBS="-lvpx"
66 -      fi
67 -      with_vpx=yes
68 -    ],[
69 -      if test "$with_vpx" != ""; then
70 -        AC_MSG_ERROR([vpx support requested, but not found])
71 -      else
72 -        with_vpx=no
73 -      fi
74 -    ])
75 -
76 -  CPPFLAGS="$save_CPPFLAGS"
77 -  LIBS="$save_LIBS"
78 -  ;;
79 -esac
80 -
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. ])
86 -fi
87 -AM_CONDITIONAL([HAVE_LIBVPX], test "$with_vpx" = yes)
88 -
89  dnl check for libtiff by default
90  AC_ARG_WITH(tiff,dnl
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])
94  fi
95  
96 +
97 +dnl check for libwebp by default
98 +AC_ARG_WITH(webp,dnl
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"
103 +   fi],
104 +  withval=yes)
105 +
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. ])])
111 +     with_webp=yes
112 +else
113 +  with_webp=no
114 +fi
115 +AM_CONDITIONAL([HAVE_LIBWEBP], test "$with_webp" = yes)
116 +
117  dnl report configuration
118  AC_MSG_RESULT([
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
131 --- a/src/gd_webp.c
132 +++ b/src/gd_webp.c
133 @@ -2,33 +2,21 @@
134  #include "config.h"
135  #endif /* HAVE_CONFIG_H */
136  
137 +
138 +#ifdef HAVE_LIBWEBP
139  #include <stdio.h>
140  #include <math.h>
141  #include <string.h>
142  #include <stdlib.h>
143  #include "gd.h"
144  #include "gd_errors.h"
145 -
146 -#ifdef HAVE_LIBVPX
147 -#include "webpimg.h"
148  #include "gdhelpers.h"
149 +#include "webp/decode.h"
150 +#include "webp/encode.h"
151  
152 -extern void gd_YUV420toRGBA(uint8* Y,
153 -                                 uint8* U,
154 -                                 uint8* V,
155 -                                 gdImagePtr im);
156 -
157 -extern void gd_RGBAToYUV420(gdImagePtr im2,
158 -                                 uint8* Y,
159 -                                 uint8* U,
160 -                                 uint8* V);
161 -
162 -const char * gdWebpGetVersionString()
163 -{
164 -       return "not defined";
165 -}
166 +#define GD_WEBP_ALLOC_STEP (4*1024)
167  
168 -BGD_DECLARE(gdImagePtr) gdImageCreateFromWebp (FILE * inFile)
169 +gdImagePtr gdImageCreateFromWebp (FILE * inFile)
170  {
171         gdImagePtr im;
172         gdIOCtx *in = gdNewFileCtx(inFile);
173 @@ -38,42 +26,16 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromWebp (FILE * inFile)
174         return im;
175  }
176  
177 -BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpPtr (int size, void *data)
178 +gdImagePtr gdImageCreateFromWebpCtx (gdIOCtx * infile)
179  {
180 -       int     width, height, ret;
181 -       unsigned char   *Y = NULL;
182 -       unsigned char   *U = NULL;
183 -       unsigned char   *V = NULL;
184 -       gdImagePtr im;
185 -
186 -       ret = WebPDecode(data, size, &Y, &U, &V, &width, &height);
187 -       if (ret != webp_success) {
188 -               if (Y) free(Y);
189 -               if (U) free(U);
190 -               if (V) free(V);
191 -               gd_error("WebP decode: fail to decode input data");
192 -               return NULL;
193 -       }
194 -       im = gdImageCreateTrueColor(width, height);
195 -       if (!im) {
196 -               return NULL;
197 -       }
198 -       gd_YUV420toRGBA(Y, U, V, im);
199 -       return im;
200 -}
201 -
202 -#define GD_WEBP_ALLOC_STEP (4*1024)
203 -
204 -BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpCtx (gdIOCtx * infile)
205 -{
206 -       int     width, height, ret;
207 -       unsigned char   *filedata = NULL;
208 +       int    width, height;
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;
215         size_t size = 0, n;
216         gdImagePtr im;
217 +       int x, y;
218 +       uint8_t *p;
219  
220         do {
221                 temp = gdRealloc(filedata, size+GD_WEBP_ALLOC_STEP);
222 @@ -89,23 +51,97 @@ BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpCtx (gdIOCtx * infile)
223                 }
224  
225                 n = gdGetBuf(read, GD_WEBP_ALLOC_STEP, infile);
226 -               size += n;
227 -       } while (n>0);
228 +               if (n>0 && n!=EOF) {
229 +                       size += n;
230 +               }
231 +       } while (n>0 && n!=EOF);
232  
233 -       ret = WebPDecode(filedata, size, &Y, &U, &V, &width, &height);
234 -       gdFree(filedata);
235 -       if (ret != webp_success) {
236 -               if (Y) free(Y);
237 -               if (U) free(U);
238 -               if (V) free(V);
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");
242                 return NULL;
243         }
244 +
245         im = gdImageCreateTrueColor(width, height);
246 -       gd_YUV420toRGBA(Y, U, V, im);
247 +       if (!im) {
248 +               return NULL;
249 +       }
250 +       argb = WebPDecodeARGB(filedata, size, &width, &height);
251 +       if (!argb) {
252 +               gd_error("gd-webp cannot allocate temporary buffer");
253 +               gdFree(argb);
254 +               return NULL;
255 +       }
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);
263 +               }
264 +       }
265 +       gdFree(filedata);
266 +       free(argb);
267 +       im->saveAlphaFlag = 1;
268         return im;
269  }
270  
271 +void gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quantization)
272 +{
273 +       uint8_t *argb;
274 +       int x, y;
275 +       uint8_t *p;
276 +       uint8_t *out;
277 +       size_t out_size;
278 +
279 +       if (im == NULL) {
280 +               return;
281 +       }
282 +
283 +       if (!gdImageTrueColor(im)) {
284 +               gd_error("Paletter image not supported by webp");
285 +               return;
286 +       }
287 +
288 +       if (quantization == -1) {
289 +               quantization = 80;
290 +       }
291 +
292 +       argb = (uint8_t *)gdMalloc(gdImageSX(im) * 4 * gdImageSY(im));
293 +       if (!argb) {
294 +               return;
295 +       }
296 +       p = argb;
297 +       for (y = 0; y < gdImageSY(im); y++) {
298 +               for (x = 0; x < gdImageSX(im); x++) {
299 +                       register int c;
300 +                       register char a;
301 +                       c = im->tpixels[y][x];
302 +                       a = gdTrueColorGetAlpha(c);
303 +                       if (a == 127) {
304 +                               a = 0;
305 +                       } else {
306 +                               a = 255 - ((a << 1) + (a >> 6));
307 +                       }
308 +                       *(p++) = gdTrueColorGetRed(c);
309 +                       *(p++) = gdTrueColorGetGreen(c);
310 +                       *(p++) = gdTrueColorGetBlue(c); 
311 +                       *(p++) = a;
312 +               }
313 +       }
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");
318 +               goto freeargb;
319 +       }
320 +       gdPutBuf(out, out_size, outfile);
321 +       free(out);
322 +
323 +freeargb:
324 +       gdFree(argb);
325 +}
326 +
327  BGD_DECLARE(void) gdImageWebpEx (gdImagePtr im, FILE * outFile, int quantization)
328  {
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)
332  {
333         gdIOCtx *out = gdNewFileCtx(outFile);
334 -       gdImageWebpCtx(im, out, -1);
335 +       gdImageWebpCtx(im, out, -1);
336         out->gd_free(out);
337  }
338  
339 @@ -140,75 +176,4 @@ BGD_DECLARE(void *) gdImageWebpPtrEx (gdImagePtr im, int *size, int quantization
340         out->gd_free(out);
341         return rv;
342  }
343 -
344 -/*
345 - * Maps normalized QP (quality) to VP8 QP
346 - */
347 -int mapQualityToVP8QP(int quality) {
348 -#define MIN_QUALITY 0
349 -#define MAX_QUALITY 100
350 -#define MIN_VP8QP 1
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);
357 -               return -1;
358 -       }
359 -
360 -       return (int)(vp8qp + 0.5);
361 -}
362 -
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).
366 - */
367 -BGD_DECLARE(void) gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quantization)
368 -{
369 -       int width = im->sx;
370 -       int height = im->sy;
371 -
372 -       int  yuv_width, yuv_height, yuv_nbytes, ret;
373 -       int vp8_quality;
374 -       unsigned char *Y = NULL,
375 -                                 *U = NULL,
376 -                                 *V = NULL;
377 -       unsigned char *filedata = NULL;
378 -
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;
383 -
384 -       if ((Y = (unsigned char *)gdCalloc(yuv_nbytes, sizeof(unsigned char))) == NULL) {
385 -               gd_error("gd-webp error: cannot allocate Y buffer");
386 -               return;
387 -       }
388 -       if (quantization == -1) {
389 -               quantization = 80;
390 -       }
391 -       vp8_quality = mapQualityToVP8QP(quantization);
392 -
393 -       U = Y + width * height;
394 -       V = U + yuv_width * yuv_height;
395 -       gd_RGBAToYUV420(im, Y, U, V);
396 -
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);
400 -       gdFree(Y);
401 -
402 -       if (ret != webp_success) {
403 -               if (filedata) {
404 -                       free(filedata);
405 -               }
406 -               gd_error("gd-webp error: WebP Encoder failed");
407 -               return;
408 -       }
409 -
410 -       gdPutBuf (filedata, yuv_nbytes, outfile);
411 -       free(filedata);
412 -}
413 -
414 -#endif /* HAVE_LIBVPX */
415 +#endif /* HAVE_LIBWEBP */
416 -- 
417 2.3.5
418