47#include <config_auto.h>
50#include "allheaders.h"
55#include "webp/decode.h"
56#include "webp/encode.h"
68pixReadStreamWebP(FILE *fp)
75 return (
PIX *)ERROR_PTR(
"fp not defined", __func__, NULL);
79 if ((filedata = l_binaryReadStream(fp, &filesize)) == NULL)
80 return (
PIX *)ERROR_PTR(
"filedata not read", __func__, NULL);
82 pix = pixReadMemWebP(filedata, filesize);
108pixReadMemWebP(
const l_uint8 *filedata,
112l_int32 w, h, has_alpha, wpl, stride;
116WebPBitstreamFeatures features;
119 return (
PIX *)ERROR_PTR(
"filedata not defined", __func__, NULL);
121 if (WebPGetFeatures(filedata, filesize, &features))
122 return (
PIX *)ERROR_PTR(
"Invalid WebP file", __func__, NULL);
125 has_alpha = features.has_alpha;
128 pix = pixCreate(w, h, 32);
129 pixSetInputFormat(pix, IFF_WEBP);
130 if (has_alpha) pixSetSpp(pix, 4);
131 data = pixGetData(pix);
132 wpl = pixGetWpl(pix);
134 size = (size_t)stride * h;
135 out = WebPDecodeRGBAInto(filedata, filesize, (uint8_t *)data, size,
139 return (
PIX *)ERROR_PTR(
"WebP decode failed", __func__, NULL);
147 pixEndianByteSwap(pix);
162readHeaderWebP(
const char *filename,
168l_int32 nbytes, bytesread;
172 if (!pw || !ph || !pspp)
173 return ERROR_INT(
"input ptr(s) not defined", __func__, 1);
174 *pw = *ph = *pspp = 0;
176 return ERROR_INT(
"filename not defined", __func__, 1);
179 if ((filesize = nbytesInFile(filename)) == 0)
180 return ERROR_INT_1(
"no file size found", filename, __func__, 1);
182 L_WARNING(
"very small webp file: %s\n", __func__, filename);
183 nbytes = L_MIN(filesize, 100);
184 if ((fp = fopenReadStream(filename)) == NULL)
185 return ERROR_INT_1(
"image file not found", filename, __func__, 1);
186 bytesread = fread(data, 1, nbytes, fp);
188 if (bytesread != nbytes)
189 return ERROR_INT(
"failed to read requested data", __func__, 1);
191 return readHeaderMemWebP(data, nbytes, pw, ph, pspp);
206readHeaderMemWebP(
const l_uint8 *data,
212WebPBitstreamFeatures features;
218 return ERROR_INT(
"data not defined", __func__, 1);
219 if (!pw || !ph || !pspp)
220 return ERROR_INT(
"input ptr(s) not defined", __func__, 1);
222 if (WebPGetFeatures(data, (l_int32)size, &features))
223 return ERROR_INT(
"invalid WebP file", __func__, 1);
224 *pw = features.width;
225 *ph = features.height;
226 *pspp = (features.has_alpha) ? 4 : 3;
249pixWriteWebP(
const char *filename,
258 return ERROR_INT(
"pixs not defined", __func__, 1);
260 return ERROR_INT(
"filename not defined", __func__, 1);
262 if ((fp = fopenWriteStream(filename,
"wb+")) == NULL)
263 return ERROR_INT_1(
"stream not opened", filename, __func__, 1);
264 ret = pixWriteStreamWebP(fp, pixs, quality, lossless);
267 return ERROR_INT_1(
"pixs not compressed to stream", filename, __func__, 1);
289pixWriteStreamWebP(FILE *fp,
295size_t filebytes, nbytes;
298 return ERROR_INT(
"stream not open", __func__, 1);
300 return ERROR_INT(
"pixs not defined", __func__, 1);
302 pixSetPadBits(pixs, 0);
303 pixWriteMemWebP(&filedata, &filebytes, pixs, quality, lossless);
305 nbytes = fwrite(filedata, 1, filebytes, fp);
307 if (nbytes != filebytes)
308 return ERROR_INT(
"Write error", __func__, 1);
334pixWriteMemWebP(l_uint8 **pencdata,
340l_int32 w, h, d, wpl, stride;
345 return ERROR_INT(
"&encdata not defined", __func__, 1);
348 return ERROR_INT(
"&encsize not defined", __func__, 1);
351 return ERROR_INT(
"&pixs not defined", __func__, 1);
352 if (lossless == 0 && (quality < 0 || quality > 100))
353 return ERROR_INT(
"quality not in [0 ... 100]", __func__, 1);
356 return ERROR_INT(
"failure to remove color map", __func__, 1);
359 if (pixGetDepth(pix1) != 32)
360 pix2 = pixConvertTo32(pix1);
362 pix2 = pixCopy(NULL, pix1);
364 pixGetDimensions(pix2, &w, &h, &d);
365 if (w <= 0 || h <= 0 || d != 32) {
367 return ERROR_INT(
"pix2 not 32 bpp or of 0 size", __func__, 1);
371 if (pixGetSpp(pix2) == 3)
379 pixEndianByteSwap(pix2);
380 wpl = pixGetWpl(pix2);
381 data = pixGetData(pix2);
384 *pencsize = WebPEncodeLosslessRGBA((uint8_t *)data, w, h,
387 *pencsize = WebPEncodeRGBA((uint8_t *)data, w, h, stride,
392 if (*pencsize == 0) {
395 return ERROR_INT(
"webp encoding failed", __func__, 1);
@ REMOVE_CMAP_TO_FULL_COLOR