39#include <config_auto.h>
42#include "allheaders.h"
47#include "webp/decode.h"
48#include "webp/encode.h"
50#include "webp/demux.h"
72pixaWriteWebPAnim(
const char *filename,
83 return ERROR_INT(
"filename not defined", __func__, 1);
85 return ERROR_INT(
"pixa not defined", __func__, 1);
87 if ((fp = fopenWriteStream(filename,
"wb+")) == NULL)
88 return ERROR_INT(
"stream not opened", __func__, 1);
89 ret = pixaWriteStreamWebPAnim(fp, pixa, loopcount, duration,
93 return ERROR_INT(
"pixs not compressed to stream", __func__, 1);
117pixaWriteStreamWebPAnim(FILE *fp,
125size_t filebytes, nbytes;
128 return ERROR_INT(
"stream not open", __func__, 1);
130 return ERROR_INT(
"pixa not defined", __func__, 1);
133 pixaWriteMemWebPAnim(&filedata, &filebytes, pixa, loopcount,
134 duration, quality, lossless);
137 return ERROR_INT(
"filedata not made", __func__, 1);
138 nbytes = fwrite(filedata, 1, filebytes, fp);
140 if (nbytes != filebytes)
141 return ERROR_INT(
"Write error", __func__, 1);
164pixaWriteMemWebPAnim(l_uint8 **pencdata,
172l_int32 i, n, same, w, h, wpl, ret;
176WebPAnimEncoderOptions enc_options;
180WebPMuxAnimParams newparams;
184 return ERROR_INT(
"&encdata not defined", __func__, 1);
187 return ERROR_INT(
"&encsize not defined", __func__, 1);
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);
197 pixaVerifyDimensions(pixa, &same, &w, &h);
199 return ERROR_INT(
"sizes of all pix are not the same", __func__, 1);
202 WebPAnimEncoderOptionsInit(&enc_options);
203 enc = WebPAnimEncoderNew(w, h, &enc_options);
205 for (i = 0; i < n; i++) {
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);
217 WebPPictureImportRGBA(&frame, data, 4 * wpl);
222 WebPConfigInit(&config);
223 config.lossless = lossless;
224 config.quality = quality;
225 WebPAnimEncoderAdd(enc, &frame, duration * i, &config);
226 WebPPictureFree(&frame);
228 WebPAnimEncoderAdd(enc, NULL, duration * i, NULL);
229 WebPAnimEncoderAssemble(enc, &webp_data);
230 WebPAnimEncoderDelete(enc);
239 mux = WebPMuxCreate(&webp_data, 1);
241 L_ERROR(
"could not re-mux to add loop count\n", __func__);
243 ret = WebPMuxGetAnimationParams(mux, &newparams);
244 if (ret != WEBP_MUX_OK) {
245 L_ERROR(
"failed to get loop count\n", __func__);
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__);
252 WebPDataClear(&webp_data);
253 WebPMuxAssemble(mux, &webp_data);
258 *pencdata = (l_uint8 *)webp_data.bytes;
259 *pencsize = webp_data.size;
260 L_INFO(
"data size = %zu\n", __func__, webp_data.size);