Leptonica  1.82.0
Image processing and image analysis suite
webpio.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  -
4  - Redistribution and use in source and binary forms, with or without
5  - modification, are permitted provided that the following conditions
6  - are met:
7  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
46 #ifdef HAVE_CONFIG_H
47 #include <config_auto.h>
48 #endif /* HAVE_CONFIG_H */
49 
50 #include "allheaders.h"
51 
52 /* --------------------------------------------*/
53 #if HAVE_LIBWEBP /* defined in environ.h */
54 /* --------------------------------------------*/
55 #include "webp/decode.h"
56 #include "webp/encode.h"
57 
58 /*---------------------------------------------------------------------*
59  * Reading WebP *
60  *---------------------------------------------------------------------*/
67 PIX *
68 pixReadStreamWebP(FILE *fp)
69 {
70 l_uint8 *filedata;
71 size_t filesize;
72 PIX *pix;
73 
74  PROCNAME("pixReadStreamWebP");
75 
76  if (!fp)
77  return (PIX *)ERROR_PTR("fp not defined", procName, NULL);
78 
79  /* Read data from file and decode into Y,U,V arrays */
80  rewind(fp);
81  if ((filedata = l_binaryReadStream(fp, &filesize)) == NULL)
82  return (PIX *)ERROR_PTR("filedata not read", procName, NULL);
83 
84  pix = pixReadMemWebP(filedata, filesize);
85  LEPT_FREE(filedata);
86  return pix;
87 }
88 
89 
109 PIX *
110 pixReadMemWebP(const l_uint8 *filedata,
111  size_t filesize)
112 {
113 l_uint8 *out = NULL;
114 l_int32 w, h, has_alpha, wpl, stride;
115 l_uint32 *data;
116 size_t size;
117 PIX *pix;
118 WebPBitstreamFeatures features;
119 
120  PROCNAME("pixReadMemWebP");
121 
122  if (!filedata)
123  return (PIX *)ERROR_PTR("filedata not defined", procName, NULL);
124 
125  if (WebPGetFeatures(filedata, filesize, &features))
126  return (PIX *)ERROR_PTR("Invalid WebP file", procName, NULL);
127  w = features.width;
128  h = features.height;
129  has_alpha = features.has_alpha;
130 
131  /* Write from compressed Y,U,V arrays to pix raster data */
132  pix = pixCreate(w, h, 32);
133  pixSetInputFormat(pix, IFF_WEBP);
134  if (has_alpha) pixSetSpp(pix, 4);
135  data = pixGetData(pix);
136  wpl = pixGetWpl(pix);
137  stride = wpl * 4;
138  size = (size_t)stride * h;
139  out = WebPDecodeRGBAInto(filedata, filesize, (uint8_t *)data, size,
140  stride);
141  if (out == NULL) { /* error: out should also point to data */
142  pixDestroy(&pix);
143  return (PIX *)ERROR_PTR("WebP decode failed", procName, NULL);
144  }
145 
146  /* The WebP API expects data in RGBA order. The pix stores
147  * in host-dependent order with R as the MSB and A as the LSB.
148  * On little-endian machines, the bytes in the word must
149  * be swapped; e.g., R goes from byte 0 (LSB) to byte 3 (MSB).
150  * No swapping is necessary for big-endians. */
151  pixEndianByteSwap(pix);
152  return pix;
153 }
154 
155 
165 l_ok
166 readHeaderWebP(const char *filename,
167  l_int32 *pw,
168  l_int32 *ph,
169  l_int32 *pspp)
170 {
171 l_uint8 data[100]; /* expect size info within the first 50 bytes or so */
172 l_int32 nbytes, bytesread;
173 size_t filesize;
174 FILE *fp;
175 
176  PROCNAME("readHeaderWebP");
177 
178  if (!pw || !ph || !pspp)
179  return ERROR_INT("input ptr(s) not defined", procName, 1);
180  *pw = *ph = *pspp = 0;
181  if (!filename)
182  return ERROR_INT("filename not defined", procName, 1);
183 
184  /* Read no more than 100 bytes from the file */
185  if ((filesize = nbytesInFile(filename)) == 0)
186  return ERROR_INT("no file size found", procName, 1);
187  if (filesize < 100)
188  L_WARNING("very small webp file\n", procName);
189  nbytes = L_MIN(filesize, 100);
190  if ((fp = fopenReadStream(filename)) == NULL)
191  return ERROR_INT("image file not found", procName, 1);
192  bytesread = fread(data, 1, nbytes, fp);
193  fclose(fp);
194  if (bytesread != nbytes)
195  return ERROR_INT("failed to read requested data", procName, 1);
196 
197  return readHeaderMemWebP(data, nbytes, pw, ph, pspp);
198 }
199 
200 
211 l_ok
212 readHeaderMemWebP(const l_uint8 *data,
213  size_t size,
214  l_int32 *pw,
215  l_int32 *ph,
216  l_int32 *pspp)
217 {
218 WebPBitstreamFeatures features;
219 
220  PROCNAME("readHeaderWebP");
221 
222  if (pw) *pw = 0;
223  if (ph) *ph = 0;
224  if (pspp) *pspp = 0;
225  if (!data)
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);
229 
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;
235  return 0;
236 }
237 
238 
239 /*---------------------------------------------------------------------*
240  * Writing WebP *
241  *---------------------------------------------------------------------*/
256 l_ok
257 pixWriteWebP(const char *filename,
258  PIX *pixs,
259  l_int32 quality,
260  l_int32 lossless)
261 {
262 l_int32 ret;
263 FILE *fp;
264 
265  PROCNAME("pixWriteWebP");
266 
267  if (!pixs)
268  return ERROR_INT("pixs not defined", procName, 1);
269  if (!filename)
270  return ERROR_INT("filename not defined", procName, 1);
271 
272  if ((fp = fopenWriteStream(filename, "wb+")) == NULL)
273  return ERROR_INT("stream not opened", procName, 1);
274  ret = pixWriteStreamWebP(fp, pixs, quality, lossless);
275  fclose(fp);
276  if (ret)
277  return ERROR_INT("pixs not compressed to stream", procName, 1);
278  return 0;
279 }
280 
281 
298 l_ok
299 pixWriteStreamWebP(FILE *fp,
300  PIX *pixs,
301  l_int32 quality,
302  l_int32 lossless)
303 {
304 l_uint8 *filedata;
305 size_t filebytes, nbytes;
306 
307  PROCNAME("pixWriteStreamWebP");
308 
309  if (!fp)
310  return ERROR_INT("stream not open", procName, 1);
311  if (!pixs)
312  return ERROR_INT("pixs not defined", procName, 1);
313 
314  pixSetPadBits(pixs, 0);
315  pixWriteMemWebP(&filedata, &filebytes, pixs, quality, lossless);
316  rewind(fp);
317  nbytes = fwrite(filedata, 1, filebytes, fp);
318  free(filedata);
319  if (nbytes != filebytes)
320  return ERROR_INT("Write error", procName, 1);
321  return 0;
322 }
323 
324 
345 l_ok
346 pixWriteMemWebP(l_uint8 **pencdata,
347  size_t *pencsize,
348  PIX *pixs,
349  l_int32 quality,
350  l_int32 lossless)
351 {
352 l_int32 w, h, d, wpl, stride;
353 l_uint32 *data;
354 PIX *pix1, *pix2;
355 
356  PROCNAME("pixWriteMemWebP");
357 
358  if (!pencdata)
359  return ERROR_INT("&encdata not defined", procName, 1);
360  *pencdata = NULL;
361  if (!pencsize)
362  return ERROR_INT("&encsize not defined", procName, 1);
363  *pencsize = 0;
364  if (!pixs)
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);
368 
369  if ((pix1 = pixRemoveColormap(pixs, REMOVE_CMAP_TO_FULL_COLOR)) == NULL)
370  return ERROR_INT("failure to remove color map", procName, 1);
371 
372  /* Convert to rgb if not 32 bpp; pix2 must not be a clone of pixs. */
373  if (pixGetDepth(pix1) != 32)
374  pix2 = pixConvertTo32(pix1);
375  else
376  pix2 = pixCopy(NULL, pix1);
377  pixDestroy(&pix1);
378  pixGetDimensions(pix2, &w, &h, &d);
379  if (w <= 0 || h <= 0 || d != 32) {
380  pixDestroy(&pix2);
381  return ERROR_INT("pix2 not 32 bpp or of 0 size", procName, 1);
382  }
383 
384  /* If spp == 3, need to set alpha layer to opaque (all 1s). */
385  if (pixGetSpp(pix2) == 3)
387 
388  /* The WebP API expects data in RGBA order. The pix stores
389  * in host-dependent order with R as the MSB and A as the LSB.
390  * On little-endian machines, the bytes in the word must
391  * be swapped; e.g., R goes from byte 0 (LSB) to byte 3 (MSB).
392  * No swapping is necessary for big-endians. */
393  pixEndianByteSwap(pix2);
394  wpl = pixGetWpl(pix2);
395  data = pixGetData(pix2);
396  stride = wpl * 4;
397  if (lossless) {
398  *pencsize = WebPEncodeLosslessRGBA((uint8_t *)data, w, h,
399  stride, pencdata);
400  } else {
401  *pencsize = WebPEncodeRGBA((uint8_t *)data, w, h, stride,
402  quality, pencdata);
403  }
404  pixDestroy(&pix2);
405 
406  if (*pencsize == 0) {
407  free(*pencdata);
408  *pencdata = NULL;
409  return ERROR_INT("webp encoding failed", procName, 1);
410  }
411 
412  return 0;
413 }
414 
415 /* --------------------------------------------*/
416 #endif /* HAVE_LIBWEBP */
417 /* --------------------------------------------*/
l_uint32 * pixGetData(PIX *pix)
pixGetData()
Definition: pix1.c:1763
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:621
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1113
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
Definition: pix1.c:705
PIX * pixCreate(l_int32 width, l_int32 height, l_int32 depth)
pixCreate()
Definition: pix1.c:315
l_ok pixSetPadBits(PIX *pix, l_int32 val)
pixSetPadBits()
Definition: pix2.c:1382
l_ok pixEndianByteSwap(PIX *pixs)
pixEndianByteSwap()
Definition: pix2.c:3074
l_ok pixSetComponentArbitrary(PIX *pix, l_int32 comp, l_int32 val)
pixSetComponentArbitrary()
Definition: pix2.c:1068
@ L_ALPHA_CHANNEL
Definition: pix.h:207
@ REMOVE_CMAP_TO_FULL_COLOR
Definition: pix.h:258
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
Definition: pixconv.c:328
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
Definition: pixconv.c:3332
Definition: pix.h:139
size_t nbytesInFile(const char *filename)
nbytesInFile()
Definition: utils2.c:1611
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
Definition: utils2.c:1402
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1975
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1932