Leptonica  1.83.1
Image processing and image analysis suite
webpanimio.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 
38 #ifdef HAVE_CONFIG_H
39 #include <config_auto.h>
40 #endif /* HAVE_CONFIG_H */
41 
42 #include "allheaders.h"
43 
44 /* -----------------------------------------------*/
45 #if HAVE_LIBWEBP_ANIM /* defined in environ.h */
46 /* -----------------------------------------------*/
47 #include "webp/decode.h"
48 #include "webp/encode.h"
49 #include "webp/mux.h"
50 #include "webp/demux.h"
51 
52 /*---------------------------------------------------------------------*
53  * Writing animated WebP *
54  *---------------------------------------------------------------------*/
71 l_ok
72 pixaWriteWebPAnim(const char *filename,
73  PIXA *pixa,
74  l_int32 loopcount,
75  l_int32 duration,
76  l_int32 quality,
77  l_int32 lossless)
78 {
79 l_int32 ret;
80 FILE *fp;
81 
82  if (!filename)
83  return ERROR_INT("filename not defined", __func__, 1);
84  if (!pixa)
85  return ERROR_INT("pixa not defined", __func__, 1);
86 
87  if ((fp = fopenWriteStream(filename, "wb+")) == NULL)
88  return ERROR_INT("stream not opened", __func__, 1);
89  ret = pixaWriteStreamWebPAnim(fp, pixa, loopcount, duration,
90  quality, lossless);
91  fclose(fp);
92  if (ret)
93  return ERROR_INT("pixs not compressed to stream", __func__, 1);
94  return 0;
95 }
96 
97 
116 l_ok
117 pixaWriteStreamWebPAnim(FILE *fp,
118  PIXA *pixa,
119  l_int32 loopcount,
120  l_int32 duration,
121  l_int32 quality,
122  l_int32 lossless)
123 {
124 l_uint8 *filedata;
125 size_t filebytes, nbytes;
126 
127  if (!fp)
128  return ERROR_INT("stream not open", __func__, 1);
129  if (!pixa)
130  return ERROR_INT("pixa not defined", __func__, 1);
131 
132  filedata = NULL;
133  pixaWriteMemWebPAnim(&filedata, &filebytes, pixa, loopcount,
134  duration, quality, lossless);
135  rewind(fp);
136  if (!filedata)
137  return ERROR_INT("filedata not made", __func__, 1);
138  nbytes = fwrite(filedata, 1, filebytes, fp);
139  free(filedata);
140  if (nbytes != filebytes)
141  return ERROR_INT("Write error", __func__, 1);
142  return 0;
143 }
144 
145 
163 l_ok
164 pixaWriteMemWebPAnim(l_uint8 **pencdata,
165  size_t *pencsize,
166  PIXA *pixa,
167  l_int32 loopcount,
168  l_int32 duration,
169  l_int32 quality,
170  l_int32 lossless)
171 {
172 l_int32 i, n, same, w, h, wpl, ret;
173 l_uint8 *data;
174 PIX *pix1, *pix2;
175 WebPAnimEncoder *enc;
176 WebPAnimEncoderOptions enc_options;
177 WebPConfig config;
178 WebPData webp_data;
179 WebPMux *mux = NULL;
180 WebPMuxAnimParams newparams;
181 WebPPicture frame;
182 
183  if (!pencdata)
184  return ERROR_INT("&encdata not defined", __func__, 1);
185  *pencdata = NULL;
186  if (!pencsize)
187  return ERROR_INT("&encsize not defined", __func__, 1);
188  *pencsize = 0;
189  if (!pixa)
190  return ERROR_INT("&pixa not defined", __func__, 1);
191  if ((n = pixaGetCount(pixa)) == 0)
192  return ERROR_INT("no images in pixa", __func__, 1);
193  if (loopcount < 0) loopcount = 0;
194  if (lossless == 0 && (quality < 0 || quality > 100))
195  return ERROR_INT("quality not in [0 ... 100]", __func__, 1);
196 
197  pixaVerifyDimensions(pixa, &same, &w, &h);
198  if (!same)
199  return ERROR_INT("sizes of all pix are not the same", __func__, 1);
200 
201  /* Set up the encoder */
202  WebPAnimEncoderOptionsInit(&enc_options);
203  enc = WebPAnimEncoderNew(w, h, &enc_options);
204 
205  for (i = 0; i < n; i++) {
206  /* Make a frame for each image. Convert the pix to RGBA with
207  * an opaque alpha layer, and put the raster data in the frame. */
208  pix1 = pixaGetPix(pixa, i, L_CLONE);
209  pix2 = pixConvertTo32(pix1);
211  pixEndianByteSwap(pix2);
212  data = (l_uint8 *)pixGetData(pix2);
213  wpl = pixGetWpl(pix2);
214  WebPPictureInit(&frame);
215  frame.width = w;
216  frame.height = h;
217  WebPPictureImportRGBA(&frame, data, 4 * wpl);
218  pixDestroy(&pix1);
219  pixDestroy(&pix2);
220 
221  /* Add the frame data to the encoder, and clear its memory */
222  WebPConfigInit(&config);
223  config.lossless = lossless;
224  config.quality = quality;
225  WebPAnimEncoderAdd(enc, &frame, duration * i, &config);
226  WebPPictureFree(&frame);
227  }
228  WebPAnimEncoderAdd(enc, NULL, duration * i, NULL); /* add a blank frame */
229  WebPAnimEncoderAssemble(enc, &webp_data); /* encode the data */
230  WebPAnimEncoderDelete(enc);
231 
232  /* Set the loopcount if requested. Note that when you make a mux,
233  * it imports the webp_data that was previously made, including
234  * the webp encoded images. Before you re-export that data using
235  * WebPMuxAssemble(), free the heap data in webp_data. There is an
236  * example for setting the loop count in the webp distribution;
237  * see gif2webp.c. */
238  if (loopcount > 0) {
239  mux = WebPMuxCreate(&webp_data, 1);
240  if (!mux) {
241  L_ERROR("could not re-mux to add loop count\n", __func__);
242  } else {
243  ret = WebPMuxGetAnimationParams(mux, &newparams);
244  if (ret != WEBP_MUX_OK) {
245  L_ERROR("failed to get loop count\n", __func__);
246  } else {
247  newparams.loop_count = loopcount;
248  ret = WebPMuxSetAnimationParams(mux, &newparams);
249  if (ret != WEBP_MUX_OK)
250  L_ERROR("failed to set loop count\n", __func__);
251  }
252  WebPDataClear(&webp_data);
253  WebPMuxAssemble(mux, &webp_data);
254  WebPMuxDelete(mux);
255  }
256  }
257 
258  *pencdata = (l_uint8 *)webp_data.bytes;
259  *pencsize = webp_data.size;
260  L_INFO("data size = %zu\n", __func__, webp_data.size);
261  return 0;
262 }
263 
264 
265 /* --------------------------------------------*/
266 #endif /* HAVE_LIBWEBP_ANIM */
267 /* --------------------------------------------*/
l_uint32 * pixGetData(PIX *pix)
pixGetData()
Definition: pix1.c:1642
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:608
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
@ L_CLONE
Definition: pix.h:506
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
Definition: pixabasic.c:629
l_ok pixaVerifyDimensions(PIXA *pixa, l_int32 *psame, l_int32 *pmaxw, l_int32 *pmaxh)
pixaVerifyDimensions()
Definition: pixabasic.c:944
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
Definition: pixabasic.c:647
PIX * pixConvertTo32(PIX *pixs)
pixConvertTo32()
Definition: pixconv.c:3246
FILE * fopenWriteStream(const char *filename, const char *modestring)
fopenWriteStream()
Definition: utils2.c:1905