gd_webp.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "gd.h"
  6. #include "gdhelpers.h"
  7. #ifdef HAVE_LIBWEBP
  8. #include "webp/decode.h"
  9. #include "webp/encode.h"
  10. #define GD_WEBP_ALLOC_STEP (4*1024)
  11. gdImagePtr gdImageCreateFromWebp (FILE * inFile)
  12. {
  13. gdImagePtr im;
  14. gdIOCtx *in = gdNewFileCtx(inFile);
  15. if (!in)
  16. return 0;
  17. im = gdImageCreateFromWebpCtx(in);
  18. in->gd_free(in);
  19. return im;
  20. }
  21. gdImagePtr gdImageCreateFromWebpPtr (int size, void *data)
  22. {
  23. gdImagePtr im;
  24. gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
  25. if (!in)
  26. return 0;
  27. im = gdImageCreateFromWebpCtx(in);
  28. in->gd_free(in);
  29. return im;
  30. }
  31. gdImagePtr gdImageCreateFromWebpCtx (gdIOCtx * infile)
  32. {
  33. int width, height;
  34. uint8_t *filedata = NULL;
  35. uint8_t *argb = NULL;
  36. size_t size = 0, n;
  37. gdImagePtr im;
  38. int x, y;
  39. uint8_t *p;
  40. do {
  41. unsigned char *read, *temp;
  42. temp = gdRealloc(filedata, size+GD_WEBP_ALLOC_STEP);
  43. if (temp) {
  44. filedata = temp;
  45. read = temp + size;
  46. } else {
  47. if (filedata) {
  48. gdFree(filedata);
  49. }
  50. zend_error(E_ERROR, "WebP decode: realloc failed");
  51. return NULL;
  52. }
  53. n = gdGetBuf(read, GD_WEBP_ALLOC_STEP, infile);
  54. if (n>0 && n!=EOF) {
  55. size += n;
  56. }
  57. } while (n>0 && n!=EOF);
  58. if (WebPGetInfo(filedata,size, &width, &height) == 0) {
  59. zend_error(E_ERROR, "gd-webp cannot get webp info");
  60. gdFree(filedata);
  61. return NULL;
  62. }
  63. im = gdImageCreateTrueColor(width, height);
  64. if (!im) {
  65. gdFree(filedata);
  66. return NULL;
  67. }
  68. argb = WebPDecodeARGB(filedata, size, &width, &height);
  69. if (!argb) {
  70. zend_error(E_ERROR, "gd-webp cannot allocate temporary buffer");
  71. gdFree(filedata);
  72. gdImageDestroy(im);
  73. return NULL;
  74. }
  75. for (y = 0, p = argb; y < height; y++) {
  76. for (x = 0; x < width; x++) {
  77. register uint8_t a = gdAlphaMax - (*(p++) >> 1);
  78. register uint8_t r = *(p++);
  79. register uint8_t g = *(p++);
  80. register uint8_t b = *(p++);
  81. im->tpixels[y][x] = gdTrueColorAlpha(r, g, b, a);
  82. }
  83. }
  84. gdFree(filedata);
  85. /* do not use gdFree here, in case gdFree/alloc is mapped to something else than libc */
  86. free(argb);
  87. im->saveAlphaFlag = 1;
  88. return im;
  89. }
  90. void gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quality)
  91. {
  92. uint8_t *argb;
  93. int x, y;
  94. uint8_t *p;
  95. uint8_t *out;
  96. size_t out_size;
  97. if (im == NULL) {
  98. return;
  99. }
  100. if (!gdImageTrueColor(im)) {
  101. zend_error(E_ERROR, "Paletter image not supported by webp");
  102. return;
  103. }
  104. if (quality == -1) {
  105. quality = 80;
  106. }
  107. if (overflow2(gdImageSX(im), 4)) {
  108. return;
  109. }
  110. if (overflow2(gdImageSX(im) * 4, gdImageSY(im))) {
  111. return;
  112. }
  113. argb = (uint8_t *)gdMalloc(gdImageSX(im) * 4 * gdImageSY(im));
  114. if (!argb) {
  115. return;
  116. }
  117. p = argb;
  118. for (y = 0; y < gdImageSY(im); y++) {
  119. for (x = 0; x < gdImageSX(im); x++) {
  120. register int c;
  121. register char a;
  122. c = im->tpixels[y][x];
  123. a = gdTrueColorGetAlpha(c);
  124. if (a == 127) {
  125. a = 0;
  126. } else {
  127. a = 255 - ((a << 1) + (a >> 6));
  128. }
  129. *(p++) = gdTrueColorGetRed(c);
  130. *(p++) = gdTrueColorGetGreen(c);
  131. *(p++) = gdTrueColorGetBlue(c);
  132. *(p++) = a;
  133. }
  134. }
  135. if (quality >= gdWebpLossless) {
  136. out_size = WebPEncodeLosslessRGBA(argb, gdImageSX(im), gdImageSY(im), gdImageSX(im) * 4, &out);
  137. } else {
  138. out_size = WebPEncodeRGBA(argb, gdImageSX(im), gdImageSY(im), gdImageSX(im) * 4, quality, &out);
  139. }
  140. if (out_size == 0) {
  141. zend_error(E_ERROR, "gd-webp encoding failed");
  142. goto freeargb;
  143. }
  144. gdPutBuf(out, out_size, outfile);
  145. free(out);
  146. freeargb:
  147. gdFree(argb);
  148. }
  149. void gdImageWebpEx (gdImagePtr im, FILE * outFile, int quality)
  150. {
  151. gdIOCtx *out = gdNewFileCtx(outFile);
  152. gdImageWebpCtx(im, out, quality);
  153. out->gd_free(out);
  154. }
  155. void gdImageWebp (gdImagePtr im, FILE * outFile)
  156. {
  157. gdIOCtx *out = gdNewFileCtx(outFile);
  158. gdImageWebpCtx(im, out, -1);
  159. out->gd_free(out);
  160. }
  161. void * gdImageWebpPtr (gdImagePtr im, int *size)
  162. {
  163. void *rv;
  164. gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
  165. gdImageWebpCtx(im, out, -1);
  166. rv = gdDPExtractData(out, size);
  167. out->gd_free(out);
  168. return rv;
  169. }
  170. void * gdImageWebpPtrEx (gdImagePtr im, int *size, int quality)
  171. {
  172. void *rv;
  173. gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
  174. gdImageWebpCtx(im, out, quality);
  175. rv = gdDPExtractData(out, size);
  176. out->gd_free(out);
  177. return rv;
  178. }
  179. #endif /* HAVE_LIBWEBP */