Leptonica  1.83.1
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  if (!fp)
75  return (PIX *)ERROR_PTR("fp not defined", __func__, NULL);
76 
77  /* Read data from file and decode into Y,U,V arrays */
78  rewind(fp);
79  if ((filedata = l_binaryReadStream(fp, &filesize)) == NULL)
80  return (PIX *)ERROR_PTR("filedata not read", __func__, NULL);
81 
82  pix = pixReadMemWebP(filedata, filesize);
83  LEPT_FREE(filedata);
84  return pix;
85 }
86 
87 
107 PIX *
108 pixReadMemWebP(const l_uint8 *filedata,
109  size_t filesize)
110 {
111 l_uint8 *out = NULL;
112 l_int32 w, h, has_alpha, wpl, stride;
113 l_uint32 *data;
114 size_t size;
115 PIX *pix;
116 WebPBitstreamFeatures features;
117 
118  if (!filedata)
119  return (PIX *)ERROR_PTR("filedata not defined", __func__, NULL);
120 
121  if (WebPGetFeatures(filedata, filesize, &features))
122  return (PIX *)ERROR_PTR("Invalid WebP file", __func__, NULL);
123  w = features.width;
124  h = features.height;
125  has_alpha = features.has_alpha;
126 
127  /* Write from compressed Y,U,V arrays to pix raster data */
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);
133  stride = wpl * 4;
134  size = (size_t)stride * h;
135  out = WebPDecodeRGBAInto(filedata, filesize, (uint8_t *)data, size,
136  stride);
137  if (out == NULL) { /* error: out should also point to data */
138  pixDestroy(&pix);
139  return (PIX *)ERROR_PTR("WebP decode failed", __func__, NULL);
140  }
141 
142  /* The WebP API expects data in RGBA order. The pix stores
143  * in host-dependent order with R as the MSB and A as the LSB.
144  * On little-endian machines, the bytes in the word must
145  * be swapped; e.g., R goes from byte 0 (LSB) to byte 3 (MSB).
146  * No swapping is necessary for big-endians. */
147  pixEndianByteSwap(pix);
148  return pix;
149 }
150 
151 
161 l_ok
162 readHeaderWebP(const char *filename,
163  l_int32 *pw,
164  l_int32 *ph,
165  l_int32 *pspp)
166 {
167 l_uint8 data[100]; /* expect size info within the first 50 bytes or so */
168 l_int32 nbytes, bytesread;
169 size_t filesize;
170 FILE *fp;
171 
172  if (!pw || !ph || !pspp)
173  return ERROR_INT("input ptr(s) not defined", __func__, 1);
174  *pw = *ph = *pspp = 0;
175  if (!filename)
176  return ERROR_INT("filename not defined", __func__, 1);
177 
178  /* Read no more than 100 bytes from the file */
179  if ((filesize = nbytesInFile(filename)) == 0)
180  return ERROR_INT("no file size found", __func__, 1);
181  if (filesize < 100)
182  L_WARNING("very small webp file\n", __func__);
183  nbytes = L_MIN(filesize, 100);
184  if ((fp = fopenReadStream(filename)) == NULL)
185  return ERROR_INT("image file not found", __func__, 1);
186  bytesread = fread(data, 1, nbytes, fp);
187  fclose(fp);
188  if (bytesread != nbytes)
189  return ERROR_INT("failed to read requested data", __func__, 1);
190 
191  return readHeaderMemWebP(data, nbytes, pw, ph, pspp);
192 }
193 
194 
205 l_ok
206 readHeaderMemWebP(const l_uint8 *data,
207  size_t size,
208  l_int32 *pw,
209  l_int32 *ph,
210  l_int32 *pspp)
211 {
212 WebPBitstreamFeatures features;
213 
214  if (pw) *pw = 0;
215  if (ph) *ph = 0;
216  if (pspp) *pspp = 0;
217  if (!data)
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);
221 
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;
227  return 0;
228 }
229 
230 
231 /*---------------------------------------------------------------------*
232  * Writing WebP *
233  *---------------------------------------------------------------------*/
248 l_ok
249 pixWriteWebP(const char *filename,
250  PIX *pixs,
251  l_int32 quality,
252  l_int32 lossless)
253 {
254 l_int32 ret;
255 FILE *fp;
256 
257  if (!pixs)
258  return ERROR_INT("pixs not defined", __func__, 1);
259  if (!filename)
260  return ERROR_INT("filename not defined", __func__, 1);
261 
262  if ((fp = fopenWriteStream(filename, "wb+")) == NULL)
263  return ERROR_INT("stream not opened", __func__, 1);
264  ret = pixWriteStreamWebP(fp, pixs, quality, lossless);
265  fclose(fp);
266  if (ret)
267  return ERROR_INT("pixs not compressed to stream", __func__, 1);
268  return 0;
269 }
270 
271 
288 l_ok
289 pixWriteStreamWebP(FILE *fp,
290  PIX *pixs,
291  l_int32 quality,
292  l_int32 lossless)
293 {
294 l_uint8 *filedata;
295 size_t filebytes, nbytes;
296 
297  if (!fp)
298  return ERROR_INT("stream not open", __func__, 1);
299  if (!pixs)
300  return ERROR_INT("pixs not defined", __func__, 1);
301 
302  pixSetPadBits(pixs, 0);
303  pixWriteMemWebP(&filedata, &filebytes, pixs, quality, lossless);
304  rewind(fp);
305  nbytes = fwrite(filedata, 1, filebytes, fp);
306  free(filedata);
307  if (nbytes != filebytes)
308  return ERROR_INT("Write error", __func__, 1);
309  return 0;
310 }
311 
312 
333 l_ok
334 pixWriteMemWebP(l_uint8 **pencdata,
335  size_t *pencsize,
336  PIX *pixs,
337  l_int32 quality,
338  l_int32 lossless)
339 {
340 l_int32 w, h, d, wpl, stride;
341 l_uint32 *data;
342 PIX *pix1, *pix2;
343 
344  if (!pencdata)
345  return ERROR_INT("&encdata not defined", __func__, 1);
346  *pencdata = NULL;
347  if (!pencsize)
348  return ERROR_INT("&encsize not defined", __func__, 1);
349  *pencsize = 0;
350  if (!pixs)
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);
354 
355  if ((pix1 = pixRemoveColormap(pixs, REMOVE_CMAP_TO_FULL_COLOR)) == NULL)
356  return ERROR_INT("failure to remove color map", __func__, 1);
357 
358  /* Convert to rgb if not 32 bpp; pix2 must not be a clone of pixs. */
359  if (pixGetDepth(pix1) != 32)
360  pix2 = pixConvertTo32(pix1);
361  else
362  pix2 = pixCopy(NULL, pix1);
363  pixDestroy(&pix1);
364  pixGetDimensions(pix2, &w, &h, &d);
365  if (w <= 0 || h <= 0 || d != 32) {
366  pixDestroy(&pix2);
367  return ERROR_INT("pix2 not 32 bpp or of 0 size", __func__, 1);
368  }
369 
370  /* If spp == 3, need to set alpha layer to opaque (all 1s). */
371  if (pixGetSpp(pix2) == 3)
373 
374  /* The WebP API expects data in RGBA order. The pix stores
375  * in host-dependent order with R as the MSB and A as the LSB.
376  * On little-endian machines, the bytes in the word must
377  * be swapped; e.g., R goes from byte 0 (LSB) to byte 3 (MSB).
378  * No swapping is necessary for big-endians. */
379  pixEndianByteSwap(pix2);
380  wpl = pixGetWpl(pix2);
381  data = pixGetData(pix2);
382  stride = wpl * 4;
383  if (lossless) {
384  *pencsize = WebPEncodeLosslessRGBA((uint8_t *)data, w, h,
385  stride, pencdata);
386  } else {
387  *pencsize = WebPEncodeRGBA((uint8_t *)data, w, h, stride,
388  quality, pencdata);
389  }
390  pixDestroy(&pix2);
391 
392  if (*pencsize == 0) {
393  free(*pencdata);
394  *pencdata = NULL;
395  return ERROR_INT("webp encoding failed", __func__, 1);
396  }
397 
398  return 0;
399 }
400 
401 /* --------------------------------------------*/
402 #endif /* HAVE_LIBWEBP */
403 /* --------------------------------------------*/
l_uint32 * pixGetData(PIX *pix)
pixGetData()
Definition: pix1.c:1642
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:608
l_ok pixGetDimensions(const PIX *pix, l_int32 *pw, l_int32 *ph, l_int32 *pd)
pixGetDimensions()
Definition: pix1.c:1074
PIX * pixCopy(PIX *pixd, const PIX *pixs)
pixCopy()
Definition: pix1.c:689
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:1346
l_ok pixEndianByteSwap(PIX *pixs)
pixEndianByteSwap()
Definition: pix2.c:3041
l_ok pixSetComponentArbitrary(PIX *pix, l_int32 comp, l_int32 val)
pixSetComponentArbitrary()
Definition: pix2.c:1042
@ L_ALPHA_CHANNEL
Definition: pix.h:331
@ REMOVE_CMAP_TO_FULL_COLOR
Definition: pix.h:382
PIX * pixRemoveColormap(PIX *pixs, l_int32 type)
pixRemoveColormap()
Definition: pixconv.c:324
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
Definition: pixconv.c:3246
size_t nbytesInFile(const char *filename)
nbytesInFile()
Definition: utils2.c:1559
l_uint8 * l_binaryReadStream(FILE *fp, size_t *pnbytes)
l_binaryReadStream()
Definition: utils2.c:1358
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1905
FILE * fopenReadStream(const char *filename)
fopenReadStream()
Definition: utils2.c:1864