47 #include <config_auto.h>
50 #include "allheaders.h"
55 #include "webp/decode.h"
56 #include "webp/encode.h"
68 pixReadStreamWebP(FILE *fp)
75 return (
PIX *)ERROR_PTR(
"fp not defined", __func__, NULL);
80 return (
PIX *)ERROR_PTR(
"filedata not read", __func__, NULL);
82 pix = pixReadMemWebP(filedata, filesize);
108 pixReadMemWebP(
const l_uint8 *filedata,
112 l_int32 w, h, has_alpha, wpl, stride;
116 WebPBitstreamFeatures 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;
129 pixSetInputFormat(pix, IFF_WEBP);
130 if (has_alpha) pixSetSpp(pix, 4);
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);
162 readHeaderWebP(
const char *filename,
168 l_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);
180 return ERROR_INT(
"no file size found", __func__, 1);
182 L_WARNING(
"very small webp file\n", __func__);
183 nbytes = L_MIN(filesize, 100);
185 return ERROR_INT(
"image file not found", __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);
206 readHeaderMemWebP(
const l_uint8 *data,
212 WebPBitstreamFeatures 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;
249 pixWriteWebP(
const char *filename,
258 return ERROR_INT(
"pixs not defined", __func__, 1);
260 return ERROR_INT(
"filename not defined", __func__, 1);
263 return ERROR_INT(
"stream not opened", __func__, 1);
264 ret = pixWriteStreamWebP(fp, pixs, quality, lossless);
267 return ERROR_INT(
"pixs not compressed to stream", __func__, 1);
289 pixWriteStreamWebP(FILE *fp,
295 size_t filebytes, nbytes;
298 return ERROR_INT(
"stream not open", __func__, 1);
300 return ERROR_INT(
"pixs not defined", __func__, 1);
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);
334 pixWriteMemWebP(l_uint8 **pencdata,
340 l_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)
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)
380 wpl = pixGetWpl(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);
l_uint32 * pixGetData(PIX *pix)
pixGetData()
void pixDestroy(PIX **ppix)
pixDestroy()
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
l_ok pixSetPadBits(PIX *pix, l_int32 val)
pixSetPadBits()
l_ok pixEndianByteSwap(PIX *pixs)
pixEndianByteSwap()
l_ok pixSetComponentArbitrary(PIX *pix, l_int32 comp, l_int32 val)
pixSetComponentArbitrary()
@ REMOVE_CMAP_TO_FULL_COLOR
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
size_t nbytesInFile(const char *filename)
nbytesInFile()
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
FILE * fopenReadStream(const char *filename)
fopenReadStream()