47 #include <config_auto.h>
50 #include "allheaders.h"
55 #include "webp/decode.h"
56 #include "webp/encode.h"
68 pixReadStreamWebP(FILE *fp)
74 PROCNAME(
"pixReadStreamWebP");
77 return (
PIX *)ERROR_PTR(
"fp not defined", procName, NULL);
82 return (
PIX *)ERROR_PTR(
"filedata not read", procName, NULL);
84 pix = pixReadMemWebP(filedata, filesize);
110 pixReadMemWebP(
const l_uint8 *filedata,
114 l_int32 w, h, has_alpha, wpl, stride;
118 WebPBitstreamFeatures features;
120 PROCNAME(
"pixReadMemWebP");
123 return (
PIX *)ERROR_PTR(
"filedata not defined", procName, NULL);
125 if (WebPGetFeatures(filedata, filesize, &features))
126 return (
PIX *)ERROR_PTR(
"Invalid WebP file", procName, NULL);
129 has_alpha = features.has_alpha;
133 pixSetInputFormat(pix, IFF_WEBP);
134 if (has_alpha) pixSetSpp(pix, 4);
136 wpl = pixGetWpl(pix);
138 size = (size_t)stride * h;
139 out = WebPDecodeRGBAInto(filedata, filesize, (uint8_t *)data, size,
143 return (
PIX *)ERROR_PTR(
"WebP decode failed", procName, NULL);
166 readHeaderWebP(
const char *filename,
172 l_int32 nbytes, bytesread;
176 PROCNAME(
"readHeaderWebP");
178 if (!pw || !ph || !pspp)
179 return ERROR_INT(
"input ptr(s) not defined", procName, 1);
180 *pw = *ph = *pspp = 0;
182 return ERROR_INT(
"filename not defined", procName, 1);
186 return ERROR_INT(
"no file size found", procName, 1);
188 L_WARNING(
"very small webp file\n", procName);
189 nbytes = L_MIN(filesize, 100);
191 return ERROR_INT(
"image file not found", procName, 1);
192 bytesread = fread(data, 1, nbytes, fp);
194 if (bytesread != nbytes)
195 return ERROR_INT(
"failed to read requested data", procName, 1);
197 return readHeaderMemWebP(data, nbytes, pw, ph, pspp);
212 readHeaderMemWebP(
const l_uint8 *data,
218 WebPBitstreamFeatures features;
220 PROCNAME(
"readHeaderWebP");
226 return ERROR_INT(
"data not defined", procName, 1);
227 if (!pw || !ph || !pspp)
228 return ERROR_INT(
"input ptr(s) not defined", procName, 1);
230 if (WebPGetFeatures(data, (l_int32)size, &features))
231 return ERROR_INT(
"invalid WebP file", procName, 1);
232 *pw = features.width;
233 *ph = features.height;
234 *pspp = (features.has_alpha) ? 4 : 3;
257 pixWriteWebP(
const char *filename,
265 PROCNAME(
"pixWriteWebP");
268 return ERROR_INT(
"pixs not defined", procName, 1);
270 return ERROR_INT(
"filename not defined", procName, 1);
273 return ERROR_INT(
"stream not opened", procName, 1);
274 ret = pixWriteStreamWebP(fp, pixs, quality, lossless);
277 return ERROR_INT(
"pixs not compressed to stream", procName, 1);
299 pixWriteStreamWebP(FILE *fp,
305 size_t filebytes, nbytes;
307 PROCNAME(
"pixWriteStreamWebP");
310 return ERROR_INT(
"stream not open", procName, 1);
312 return ERROR_INT(
"pixs not defined", procName, 1);
315 pixWriteMemWebP(&filedata, &filebytes, pixs, quality, lossless);
317 nbytes = fwrite(filedata, 1, filebytes, fp);
319 if (nbytes != filebytes)
320 return ERROR_INT(
"Write error", procName, 1);
346 pixWriteMemWebP(l_uint8 **pencdata,
352 l_int32 w, h, d, wpl, stride;
356 PROCNAME(
"pixWriteMemWebP");
359 return ERROR_INT(
"&encdata not defined", procName, 1);
362 return ERROR_INT(
"&encsize not defined", procName, 1);
365 return ERROR_INT(
"&pixs not defined", procName, 1);
366 if (lossless == 0 && (quality < 0 || quality > 100))
367 return ERROR_INT(
"quality not in [0 ... 100]", procName, 1);
370 return ERROR_INT(
"failure to remove color map", procName, 1);
373 if (pixGetDepth(pix1) != 32)
379 if (w <= 0 || h <= 0 || d != 32) {
381 return ERROR_INT(
"pix2 not 32 bpp or of 0 size", procName, 1);
385 if (pixGetSpp(pix2) == 3)
394 wpl = pixGetWpl(pix2);
398 *pencsize = WebPEncodeLosslessRGBA((uint8_t *)data, w, h,
401 *pencsize = WebPEncodeRGBA((uint8_t *)data, w, h, stride,
406 if (*pencsize == 0) {
409 return ERROR_INT(
"webp encoding failed", procName, 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()