gd_webp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #ifdef HAVE_LIBWEBP
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "gd.h"
  7. #include "gdhelpers.h"
  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 quantization)
  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 (quantization == -1) {
  105. quantization = 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. out_size = WebPEncodeRGBA(argb, gdImageSX(im), gdImageSY(im), gdImageSX(im) * 4, quantization, &out);
  136. if (out_size == 0) {
  137. zend_error(E_ERROR, "gd-webp encoding failed");
  138. goto freeargb;
  139. }
  140. gdPutBuf(out, out_size, outfile);
  141. free(out);
  142. freeargb:
  143. gdFree(argb);
  144. }
  145. void gdImageWebpEx (gdImagePtr im, FILE * outFile, int quantization)
  146. {
  147. gdIOCtx *out = gdNewFileCtx(outFile);
  148. gdImageWebpCtx(im, out, quantization);
  149. out->gd_free(out);
  150. }
  151. void gdImageWebp (gdImagePtr im, FILE * outFile)
  152. {
  153. gdIOCtx *out = gdNewFileCtx(outFile);
  154. gdImageWebpCtx(im, out, -1);
  155. out->gd_free(out);
  156. }
  157. void * gdImageWebpPtr (gdImagePtr im, int *size)
  158. {
  159. void *rv;
  160. gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
  161. gdImageWebpCtx(im, out, -1);
  162. rv = gdDPExtractData(out, size);
  163. out->gd_free(out);
  164. return rv;
  165. }
  166. void * gdImageWebpPtrEx (gdImagePtr im, int *size, int quantization)
  167. {
  168. void *rv;
  169. gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
  170. gdImageWebpCtx(im, out, quantization);
  171. rv = gdDPExtractData(out, size);
  172. out->gd_free(out);
  173. return rv;
  174. }
  175. #endif /* HAVE_LIBWEBP */