2013-06-30 01:05:48 +04:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
2013-03-12 18:30:59 +04:00
|
|
|
#include <Python.h>
|
2020-09-16 08:10:28 +03:00
|
|
|
#include "libImaging/Imaging.h"
|
2013-03-12 18:30:59 +04:00
|
|
|
#include <webp/encode.h>
|
|
|
|
#include <webp/decode.h>
|
2013-05-13 20:02:36 +04:00
|
|
|
#include <webp/types.h>
|
|
|
|
|
2013-07-05 00:57:05 +04:00
|
|
|
#ifdef HAVE_WEBPMUX
|
|
|
|
#include <webp/mux.h>
|
2017-09-26 12:58:54 +03:00
|
|
|
#include <webp/demux.h>
|
2013-05-13 20:02:36 +04:00
|
|
|
|
2017-09-28 05:04:24 +03:00
|
|
|
/*
|
|
|
|
* Check the versions from mux.h and demux.h, to ensure the WebPAnimEncoder and
|
2017-10-02 01:23:18 +03:00
|
|
|
* WebPAnimDecoder APIs are present (initial support was added in 0.5.0). The
|
2018-03-10 06:48:01 +03:00
|
|
|
* very early versions had some significant differences, so we require later
|
|
|
|
* versions, before enabling animation support.
|
2017-09-28 05:04:24 +03:00
|
|
|
*/
|
|
|
|
#if WEBP_MUX_ABI_VERSION >= 0x0104 && WEBP_DEMUX_ABI_VERSION >= 0x0105
|
|
|
|
#define HAVE_WEBPANIM
|
|
|
|
#endif
|
|
|
|
|
2017-09-28 08:13:13 +03:00
|
|
|
#endif
|
2017-09-28 05:04:24 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
void
|
|
|
|
ImagingSectionEnter(ImagingSectionCookie *cookie) {
|
|
|
|
*cookie = (PyThreadState *)PyEval_SaveThread();
|
2020-02-17 23:56:30 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
void
|
|
|
|
ImagingSectionLeave(ImagingSectionCookie *cookie) {
|
|
|
|
PyEval_RestoreThread((PyThreadState *)*cookie);
|
2020-02-17 23:56:30 +03:00
|
|
|
}
|
|
|
|
|
2017-09-27 06:27:40 +03:00
|
|
|
/* -------------------------------------------------------------------- */
|
2017-09-28 06:12:10 +03:00
|
|
|
/* WebP Muxer Error Handling */
|
2017-09-27 06:27:40 +03:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
2017-09-28 08:13:13 +03:00
|
|
|
#ifdef HAVE_WEBPMUX
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
static const char *const kErrorMessages[-WEBP_MUX_NOT_ENOUGH_DATA + 1] = {
|
|
|
|
"WEBP_MUX_NOT_FOUND",
|
|
|
|
"WEBP_MUX_INVALID_ARGUMENT",
|
|
|
|
"WEBP_MUX_BAD_DATA",
|
|
|
|
"WEBP_MUX_MEMORY_ERROR",
|
|
|
|
"WEBP_MUX_NOT_ENOUGH_DATA"};
|
2017-09-27 06:27:40 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
HandleMuxError(WebPMuxError err, char *chunk) {
|
2017-09-29 23:21:26 +03:00
|
|
|
char message[100];
|
2017-09-29 23:59:05 +03:00
|
|
|
int message_len;
|
2017-09-27 06:27:40 +03:00
|
|
|
assert(err <= WEBP_MUX_NOT_FOUND && err >= WEBP_MUX_NOT_ENOUGH_DATA);
|
2017-09-28 06:12:10 +03:00
|
|
|
|
|
|
|
// Check for a memory error first
|
|
|
|
if (err == WEBP_MUX_MEMORY_ERROR) {
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the error message
|
|
|
|
if (chunk == NULL) {
|
2021-01-03 06:17:51 +03:00
|
|
|
message_len =
|
|
|
|
sprintf(message, "could not assemble chunks: %s", kErrorMessages[-err]);
|
2017-09-28 06:12:10 +03:00
|
|
|
} else {
|
2021-01-03 06:17:51 +03:00
|
|
|
message_len = sprintf(
|
|
|
|
message, "could not set %.4s chunk: %s", chunk, kErrorMessages[-err]);
|
2017-09-29 23:59:05 +03:00
|
|
|
}
|
|
|
|
if (message_len < 0) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "failed to construct error message");
|
|
|
|
return NULL;
|
2017-09-28 06:12:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the proper error type
|
|
|
|
switch (err) {
|
|
|
|
case WEBP_MUX_NOT_FOUND:
|
|
|
|
case WEBP_MUX_INVALID_ARGUMENT:
|
|
|
|
PyErr_SetString(PyExc_ValueError, message);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WEBP_MUX_BAD_DATA:
|
|
|
|
case WEBP_MUX_NOT_ENOUGH_DATA:
|
2020-04-07 09:58:21 +03:00
|
|
|
PyErr_SetString(PyExc_OSError, message);
|
2017-09-28 06:12:10 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-27 06:27:40 +03:00
|
|
|
|
2017-09-28 08:13:13 +03:00
|
|
|
#endif
|
|
|
|
|
2017-09-26 04:53:31 +03:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* WebP Animation Support */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
2017-09-28 08:13:13 +03:00
|
|
|
#ifdef HAVE_WEBPANIM
|
|
|
|
|
2017-09-26 12:58:54 +03:00
|
|
|
// Encoder type
|
2017-09-26 04:53:31 +03:00
|
|
|
typedef struct {
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject_HEAD WebPAnimEncoder *enc;
|
2017-09-26 04:53:31 +03:00
|
|
|
WebPPicture frame;
|
|
|
|
} WebPAnimEncoderObject;
|
|
|
|
|
|
|
|
static PyTypeObject WebPAnimEncoder_Type;
|
|
|
|
|
2017-09-26 12:58:54 +03:00
|
|
|
// Decoder type
|
|
|
|
typedef struct {
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject_HEAD WebPAnimDecoder *dec;
|
2017-09-26 12:58:54 +03:00
|
|
|
WebPAnimInfo info;
|
|
|
|
WebPData data;
|
2021-01-03 06:17:51 +03:00
|
|
|
char *mode;
|
2017-09-26 12:58:54 +03:00
|
|
|
} WebPAnimDecoderObject;
|
|
|
|
|
|
|
|
static PyTypeObject WebPAnimDecoder_Type;
|
|
|
|
|
|
|
|
// Encoder functions
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
_anim_encoder_new(PyObject *self, PyObject *args) {
|
2017-09-26 04:53:31 +03:00
|
|
|
int width, height;
|
|
|
|
uint32_t bgcolor;
|
|
|
|
int loop_count;
|
|
|
|
int minimize_size;
|
|
|
|
int kmin, kmax;
|
|
|
|
int allow_mixed;
|
|
|
|
int verbose;
|
2017-09-28 03:10:25 +03:00
|
|
|
WebPAnimEncoderOptions enc_options;
|
2021-01-03 06:17:51 +03:00
|
|
|
WebPAnimEncoderObject *encp = NULL;
|
|
|
|
WebPAnimEncoder *enc = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(
|
|
|
|
args,
|
|
|
|
"iiIiiiiii",
|
|
|
|
&width,
|
|
|
|
&height,
|
|
|
|
&bgcolor,
|
|
|
|
&loop_count,
|
|
|
|
&minimize_size,
|
|
|
|
&kmin,
|
|
|
|
&kmax,
|
|
|
|
&allow_mixed,
|
|
|
|
&verbose)) {
|
2017-09-26 04:53:31 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup and configure the encoder's options (these are animation-specific)
|
|
|
|
if (!WebPAnimEncoderOptionsInit(&enc_options)) {
|
2017-09-28 06:12:10 +03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "failed to initialize encoder options");
|
|
|
|
return NULL;
|
2017-09-26 04:53:31 +03:00
|
|
|
}
|
|
|
|
enc_options.anim_params.bgcolor = bgcolor;
|
|
|
|
enc_options.anim_params.loop_count = loop_count;
|
|
|
|
enc_options.minimize_size = minimize_size;
|
|
|
|
enc_options.kmin = kmin;
|
|
|
|
enc_options.kmax = kmax;
|
|
|
|
enc_options.allow_mixed = allow_mixed;
|
|
|
|
enc_options.verbose = verbose;
|
|
|
|
|
|
|
|
// Validate canvas dimensions
|
|
|
|
if (width <= 0 || height <= 0) {
|
2017-09-28 06:12:10 +03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "invalid canvas dimensions");
|
|
|
|
return NULL;
|
2017-09-26 04:53:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new animation encoder and picture frame
|
|
|
|
encp = PyObject_New(WebPAnimEncoderObject, &WebPAnimEncoder_Type);
|
|
|
|
if (encp) {
|
|
|
|
if (WebPPictureInit(&(encp->frame))) {
|
2017-09-28 03:10:25 +03:00
|
|
|
enc = WebPAnimEncoderNew(width, height, &enc_options);
|
2017-09-26 04:53:31 +03:00
|
|
|
if (enc) {
|
|
|
|
encp->enc = enc;
|
2021-01-03 06:17:51 +03:00
|
|
|
return (PyObject *)encp;
|
2017-09-26 04:53:31 +03:00
|
|
|
}
|
2017-09-27 06:27:40 +03:00
|
|
|
WebPPictureFree(&(encp->frame));
|
2017-09-26 04:53:31 +03:00
|
|
|
}
|
|
|
|
PyObject_Del(encp);
|
|
|
|
}
|
2017-09-28 06:12:10 +03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "could not create encoder object");
|
|
|
|
return NULL;
|
2017-09-26 04:53:31 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
_anim_encoder_dealloc(PyObject *self) {
|
|
|
|
WebPAnimEncoderObject *encp = (WebPAnimEncoderObject *)self;
|
2017-09-26 04:53:31 +03:00
|
|
|
WebPPictureFree(&(encp->frame));
|
|
|
|
WebPAnimEncoderDelete(encp->enc);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
_anim_encoder_add(PyObject *self, PyObject *args) {
|
|
|
|
uint8_t *rgb;
|
2017-09-26 04:53:31 +03:00
|
|
|
Py_ssize_t size;
|
|
|
|
int timestamp;
|
|
|
|
int width;
|
|
|
|
int height;
|
2021-01-03 06:17:51 +03:00
|
|
|
char *mode;
|
2017-09-26 04:53:31 +03:00
|
|
|
int lossless;
|
|
|
|
float quality_factor;
|
|
|
|
int method;
|
2017-09-29 23:59:05 +03:00
|
|
|
WebPConfig config;
|
2021-01-03 06:17:51 +03:00
|
|
|
WebPAnimEncoderObject *encp = (WebPAnimEncoderObject *)self;
|
|
|
|
WebPAnimEncoder *enc = encp->enc;
|
|
|
|
WebPPicture *frame = &(encp->frame);
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(
|
|
|
|
args,
|
|
|
|
"z#iiisifi",
|
|
|
|
(char **)&rgb,
|
|
|
|
&size,
|
|
|
|
×tamp,
|
|
|
|
&width,
|
|
|
|
&height,
|
|
|
|
&mode,
|
|
|
|
&lossless,
|
|
|
|
&quality_factor,
|
|
|
|
&method)) {
|
2017-09-26 04:53:31 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for NULL frame, which sets duration of final frame
|
|
|
|
if (!rgb) {
|
|
|
|
WebPAnimEncoderAdd(enc, NULL, timestamp, NULL);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup config for this frame
|
|
|
|
if (!WebPConfigInit(&config)) {
|
2017-09-28 06:12:10 +03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "failed to initialize config!");
|
|
|
|
return NULL;
|
2017-09-26 04:53:31 +03:00
|
|
|
}
|
|
|
|
config.lossless = lossless;
|
|
|
|
config.quality = quality_factor;
|
|
|
|
config.method = method;
|
|
|
|
|
|
|
|
// Validate the config
|
|
|
|
if (!WebPValidateConfig(&config)) {
|
2017-09-28 06:12:10 +03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "invalid configuration");
|
|
|
|
return NULL;
|
2017-09-26 04:53:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the frame with raw bytes passed to us
|
|
|
|
frame->width = width;
|
|
|
|
frame->height = height;
|
2021-01-03 06:17:51 +03:00
|
|
|
frame->use_argb = 1; // Don't convert RGB pixels to YUV
|
|
|
|
if (strcmp(mode, "RGBA") == 0) {
|
2017-09-26 04:53:31 +03:00
|
|
|
WebPPictureImportRGBA(frame, rgb, 4 * width);
|
2021-01-03 06:17:51 +03:00
|
|
|
} else if (strcmp(mode, "RGBX") == 0) {
|
2017-09-28 07:22:05 +03:00
|
|
|
WebPPictureImportRGBX(frame, rgb, 4 * width);
|
2018-08-04 00:21:47 +03:00
|
|
|
} else {
|
|
|
|
WebPPictureImportRGB(frame, rgb, 3 * width);
|
2017-09-26 04:53:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the frame to the encoder
|
|
|
|
if (!WebPAnimEncoderAdd(enc, frame, timestamp, &config)) {
|
2017-09-28 06:12:10 +03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, WebPAnimEncoderGetError(enc));
|
|
|
|
return NULL;
|
2017-09-26 04:53:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
_anim_encoder_assemble(PyObject *self, PyObject *args) {
|
|
|
|
uint8_t *icc_bytes;
|
|
|
|
uint8_t *exif_bytes;
|
|
|
|
uint8_t *xmp_bytes;
|
2017-09-26 04:53:31 +03:00
|
|
|
Py_ssize_t icc_size;
|
2017-09-27 06:27:40 +03:00
|
|
|
Py_ssize_t exif_size;
|
|
|
|
Py_ssize_t xmp_size;
|
2017-09-29 23:59:05 +03:00
|
|
|
WebPData webp_data;
|
2021-01-03 06:17:51 +03:00
|
|
|
WebPAnimEncoderObject *encp = (WebPAnimEncoderObject *)self;
|
|
|
|
WebPAnimEncoder *enc = encp->enc;
|
|
|
|
WebPMux *mux = NULL;
|
|
|
|
PyObject *ret = NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(
|
|
|
|
args,
|
|
|
|
"s#s#s#",
|
|
|
|
&icc_bytes,
|
|
|
|
&icc_size,
|
|
|
|
&exif_bytes,
|
|
|
|
&exif_size,
|
|
|
|
&xmp_bytes,
|
|
|
|
&xmp_size)) {
|
2017-09-26 04:53:31 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init the output buffer
|
|
|
|
WebPDataInit(&webp_data);
|
|
|
|
|
|
|
|
// Assemble everything into the output buffer
|
|
|
|
if (!WebPAnimEncoderAssemble(enc, &webp_data)) {
|
2017-09-28 06:12:10 +03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, WebPAnimEncoderGetError(enc));
|
|
|
|
return NULL;
|
2017-09-26 04:53:31 +03:00
|
|
|
}
|
|
|
|
|
2017-09-27 06:27:40 +03:00
|
|
|
// Re-mux to add metadata as needed
|
|
|
|
if (icc_size > 0 || exif_size > 0 || xmp_size > 0) {
|
|
|
|
WebPMuxError err = WEBP_MUX_OK;
|
|
|
|
int i_icc_size = (int)icc_size;
|
|
|
|
int i_exif_size = (int)exif_size;
|
|
|
|
int i_xmp_size = (int)xmp_size;
|
2021-01-03 06:17:51 +03:00
|
|
|
WebPData icc_profile = {icc_bytes, i_icc_size};
|
|
|
|
WebPData exif = {exif_bytes, i_exif_size};
|
|
|
|
WebPData xmp = {xmp_bytes, i_xmp_size};
|
2017-09-27 06:27:40 +03:00
|
|
|
|
|
|
|
mux = WebPMuxCreate(&webp_data, 1);
|
|
|
|
if (mux == NULL) {
|
2017-09-28 06:12:10 +03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "could not re-mux to add metadata");
|
|
|
|
return NULL;
|
2017-09-27 06:27:40 +03:00
|
|
|
}
|
|
|
|
WebPDataClear(&webp_data);
|
|
|
|
|
|
|
|
// Add ICCP chunk
|
|
|
|
if (i_icc_size > 0) {
|
|
|
|
err = WebPMuxSetChunk(mux, "ICCP", &icc_profile, 1);
|
|
|
|
if (err != WEBP_MUX_OK) {
|
2017-09-28 06:12:10 +03:00
|
|
|
return HandleMuxError(err, "ICCP");
|
2017-09-27 06:27:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add EXIF chunk
|
|
|
|
if (i_exif_size > 0) {
|
|
|
|
err = WebPMuxSetChunk(mux, "EXIF", &exif, 1);
|
|
|
|
if (err != WEBP_MUX_OK) {
|
2017-09-28 06:12:10 +03:00
|
|
|
return HandleMuxError(err, "EXIF");
|
2017-09-27 06:27:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add XMP chunk
|
|
|
|
if (i_xmp_size > 0) {
|
|
|
|
err = WebPMuxSetChunk(mux, "XMP ", &xmp, 1);
|
|
|
|
if (err != WEBP_MUX_OK) {
|
2017-09-28 06:12:10 +03:00
|
|
|
return HandleMuxError(err, "XMP");
|
2017-09-27 06:27:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = WebPMuxAssemble(mux, &webp_data);
|
|
|
|
if (err != WEBP_MUX_OK) {
|
2017-09-28 06:12:10 +03:00
|
|
|
return HandleMuxError(err, NULL);
|
2017-09-27 06:27:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to Python bytes
|
2021-01-03 06:17:51 +03:00
|
|
|
ret = PyBytes_FromStringAndSize((char *)webp_data.bytes, webp_data.size);
|
2017-09-26 04:53:31 +03:00
|
|
|
WebPDataClear(&webp_data);
|
2017-09-27 06:27:40 +03:00
|
|
|
|
|
|
|
// If we had to re-mux, we should free it now that we're done with it
|
|
|
|
if (mux != NULL) {
|
|
|
|
WebPMuxDelete(mux);
|
|
|
|
}
|
|
|
|
|
2017-09-26 04:53:31 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-26 12:58:54 +03:00
|
|
|
// Decoder functions
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
_anim_decoder_new(PyObject *self, PyObject *args) {
|
2017-09-26 12:58:54 +03:00
|
|
|
PyBytesObject *webp_string;
|
|
|
|
const uint8_t *webp;
|
|
|
|
Py_ssize_t size;
|
2017-09-28 03:10:25 +03:00
|
|
|
WebPData webp_src;
|
2021-01-03 06:17:51 +03:00
|
|
|
char *mode;
|
2017-09-28 03:10:25 +03:00
|
|
|
WebPDecoderConfig config;
|
2021-01-03 06:17:51 +03:00
|
|
|
WebPAnimDecoderObject *decp = NULL;
|
|
|
|
WebPAnimDecoder *dec = NULL;
|
2017-09-26 12:58:54 +03:00
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "S", &webp_string)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-01-03 06:17:51 +03:00
|
|
|
PyBytes_AsStringAndSize((PyObject *)webp_string, (char **)&webp, &size);
|
2017-09-28 03:10:25 +03:00
|
|
|
webp_src.bytes = webp;
|
|
|
|
webp_src.size = size;
|
2017-09-26 12:58:54 +03:00
|
|
|
|
2017-09-27 06:27:40 +03:00
|
|
|
// Sniff the mode, since the decoder API doesn't tell us
|
2017-09-29 23:59:05 +03:00
|
|
|
mode = "RGBA";
|
2017-09-27 06:27:40 +03:00
|
|
|
if (WebPGetFeatures(webp, size, &config.input) == VP8_STATUS_OK) {
|
|
|
|
if (!config.input.has_alpha) {
|
2017-09-28 03:10:25 +03:00
|
|
|
mode = "RGBX";
|
2017-09-27 06:27:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 12:58:54 +03:00
|
|
|
// Create the decoder (default mode is RGBA, if no options passed)
|
|
|
|
decp = PyObject_New(WebPAnimDecoderObject, &WebPAnimDecoder_Type);
|
|
|
|
if (decp) {
|
2017-09-27 06:27:40 +03:00
|
|
|
decp->mode = mode;
|
2017-09-26 12:58:54 +03:00
|
|
|
if (WebPDataCopy(&webp_src, &(decp->data))) {
|
2017-09-28 03:10:25 +03:00
|
|
|
dec = WebPAnimDecoderNew(&(decp->data), NULL);
|
2017-09-26 12:58:54 +03:00
|
|
|
if (dec) {
|
|
|
|
if (WebPAnimDecoderGetInfo(dec, &(decp->info))) {
|
|
|
|
decp->dec = dec;
|
2021-01-03 06:17:51 +03:00
|
|
|
return (PyObject *)decp;
|
2017-09-26 12:58:54 +03:00
|
|
|
}
|
|
|
|
}
|
2021-10-28 21:15:43 +03:00
|
|
|
WebPDataClear(&(decp->data));
|
2017-09-26 12:58:54 +03:00
|
|
|
}
|
|
|
|
PyObject_Del(decp);
|
|
|
|
}
|
2021-04-14 13:01:56 +03:00
|
|
|
PyErr_SetString(PyExc_OSError, "could not create decoder object");
|
2017-09-28 06:12:10 +03:00
|
|
|
return NULL;
|
2017-09-26 12:58:54 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
_anim_decoder_dealloc(PyObject *self) {
|
|
|
|
WebPAnimDecoderObject *decp = (WebPAnimDecoderObject *)self;
|
2017-09-26 12:58:54 +03:00
|
|
|
WebPDataClear(&(decp->data));
|
|
|
|
WebPAnimDecoderDelete(decp->dec);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
_anim_decoder_get_info(PyObject *self) {
|
|
|
|
WebPAnimDecoderObject *decp = (WebPAnimDecoderObject *)self;
|
|
|
|
WebPAnimInfo *info = &(decp->info);
|
2017-09-28 03:10:25 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
return Py_BuildValue(
|
|
|
|
"IIIIIs",
|
|
|
|
info->canvas_width,
|
|
|
|
info->canvas_height,
|
2017-09-26 12:58:54 +03:00
|
|
|
info->loop_count,
|
|
|
|
info->bgcolor,
|
|
|
|
info->frame_count,
|
2021-01-03 06:17:51 +03:00
|
|
|
decp->mode);
|
2017-09-26 12:58:54 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
_anim_decoder_get_chunk(PyObject *self, PyObject *args) {
|
|
|
|
char *mode;
|
|
|
|
WebPAnimDecoderObject *decp = (WebPAnimDecoderObject *)self;
|
|
|
|
const WebPDemuxer *demux;
|
2017-09-26 12:58:54 +03:00
|
|
|
WebPChunkIterator iter;
|
2017-09-28 03:10:25 +03:00
|
|
|
PyObject *ret;
|
2017-09-26 12:58:54 +03:00
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "s", &mode)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-09-28 03:10:25 +03:00
|
|
|
demux = WebPAnimDecoderGetDemuxer(decp->dec);
|
2017-09-26 12:58:54 +03:00
|
|
|
if (!WebPDemuxGetChunk(demux, mode, 1, &iter)) {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
ret = PyBytes_FromStringAndSize((const char *)iter.chunk.bytes, iter.chunk.size);
|
2017-09-26 12:58:54 +03:00
|
|
|
WebPDemuxReleaseChunkIterator(&iter);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
_anim_decoder_get_next(PyObject *self) {
|
|
|
|
uint8_t *buf;
|
2017-09-26 12:58:54 +03:00
|
|
|
int timestamp;
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *bytes;
|
|
|
|
PyObject *ret;
|
|
|
|
WebPAnimDecoderObject *decp = (WebPAnimDecoderObject *)self;
|
2017-09-26 12:58:54 +03:00
|
|
|
|
|
|
|
if (!WebPAnimDecoderGetNext(decp->dec, &buf, ×tamp)) {
|
2020-04-07 09:58:21 +03:00
|
|
|
PyErr_SetString(PyExc_OSError, "failed to read next frame");
|
2017-09-28 06:12:10 +03:00
|
|
|
return NULL;
|
2017-09-26 12:58:54 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
bytes = PyBytes_FromStringAndSize(
|
|
|
|
(char *)buf, decp->info.canvas_width * 4 * decp->info.canvas_height);
|
2018-01-24 16:22:51 +03:00
|
|
|
|
|
|
|
ret = Py_BuildValue("Si", bytes, timestamp);
|
|
|
|
|
|
|
|
Py_DECREF(bytes);
|
|
|
|
return ret;
|
2017-09-26 12:58:54 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
_anim_decoder_reset(PyObject *self) {
|
|
|
|
WebPAnimDecoderObject *decp = (WebPAnimDecoderObject *)self;
|
2017-09-26 12:58:54 +03:00
|
|
|
WebPAnimDecoderReset(decp->dec);
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Type Definitions */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
// WebPAnimEncoder methods
|
|
|
|
static struct PyMethodDef _anim_encoder_methods[] = {
|
|
|
|
{"add", (PyCFunction)_anim_encoder_add, METH_VARARGS, "add"},
|
|
|
|
{"assemble", (PyCFunction)_anim_encoder_assemble, METH_VARARGS, "assemble"},
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
2021-12-22 08:42:39 +03:00
|
|
|
// WebPAnimEncoder type definition
|
2017-09-26 12:58:54 +03:00
|
|
|
static PyTypeObject WebPAnimEncoder_Type = {
|
2021-01-03 06:17:51 +03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0) "WebPAnimEncoder", /*tp_name */
|
|
|
|
sizeof(WebPAnimEncoderObject), /*tp_size */
|
|
|
|
0, /*tp_itemsize */
|
2017-09-26 12:58:54 +03:00
|
|
|
/* methods */
|
|
|
|
(destructor)_anim_encoder_dealloc, /*tp_dealloc*/
|
2021-01-03 06:17:51 +03:00
|
|
|
0, /*tp_print*/
|
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number */
|
|
|
|
0, /*tp_as_sequence */
|
|
|
|
0, /*tp_as_mapping */
|
|
|
|
0, /*tp_hash*/
|
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
0, /*tp_getattro*/
|
|
|
|
0, /*tp_setattro*/
|
|
|
|
0, /*tp_as_buffer*/
|
|
|
|
Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
|
|
|
0, /*tp_doc*/
|
|
|
|
0, /*tp_traverse*/
|
|
|
|
0, /*tp_clear*/
|
|
|
|
0, /*tp_richcompare*/
|
|
|
|
0, /*tp_weaklistoffset*/
|
|
|
|
0, /*tp_iter*/
|
|
|
|
0, /*tp_iternext*/
|
|
|
|
_anim_encoder_methods, /*tp_methods*/
|
|
|
|
0, /*tp_members*/
|
|
|
|
0, /*tp_getset*/
|
2017-09-26 12:58:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// WebPAnimDecoder methods
|
|
|
|
static struct PyMethodDef _anim_decoder_methods[] = {
|
2020-02-21 14:24:20 +03:00
|
|
|
{"get_info", (PyCFunction)_anim_decoder_get_info, METH_NOARGS, "get_info"},
|
2017-09-26 12:58:54 +03:00
|
|
|
{"get_chunk", (PyCFunction)_anim_decoder_get_chunk, METH_VARARGS, "get_chunk"},
|
2020-02-21 14:24:20 +03:00
|
|
|
{"get_next", (PyCFunction)_anim_decoder_get_next, METH_NOARGS, "get_next"},
|
|
|
|
{"reset", (PyCFunction)_anim_decoder_reset, METH_NOARGS, "reset"},
|
2017-09-26 12:58:54 +03:00
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
// WebPAnimDecoder type definition
|
|
|
|
static PyTypeObject WebPAnimDecoder_Type = {
|
2021-01-03 06:17:51 +03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0) "WebPAnimDecoder", /*tp_name */
|
|
|
|
sizeof(WebPAnimDecoderObject), /*tp_size */
|
|
|
|
0, /*tp_itemsize */
|
2017-09-26 12:58:54 +03:00
|
|
|
/* methods */
|
|
|
|
(destructor)_anim_decoder_dealloc, /*tp_dealloc*/
|
2021-01-03 06:17:51 +03:00
|
|
|
0, /*tp_print*/
|
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
0, /*tp_compare*/
|
|
|
|
0, /*tp_repr*/
|
|
|
|
0, /*tp_as_number */
|
|
|
|
0, /*tp_as_sequence */
|
|
|
|
0, /*tp_as_mapping */
|
|
|
|
0, /*tp_hash*/
|
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
0, /*tp_getattro*/
|
|
|
|
0, /*tp_setattro*/
|
|
|
|
0, /*tp_as_buffer*/
|
|
|
|
Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
|
|
|
0, /*tp_doc*/
|
|
|
|
0, /*tp_traverse*/
|
|
|
|
0, /*tp_clear*/
|
|
|
|
0, /*tp_richcompare*/
|
|
|
|
0, /*tp_weaklistoffset*/
|
|
|
|
0, /*tp_iter*/
|
|
|
|
0, /*tp_iternext*/
|
|
|
|
_anim_decoder_methods, /*tp_methods*/
|
|
|
|
0, /*tp_members*/
|
|
|
|
0, /*tp_getset*/
|
2017-09-26 12:58:54 +03:00
|
|
|
};
|
|
|
|
|
2017-09-26 04:53:31 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2017-09-26 12:58:54 +03:00
|
|
|
/* Legacy WebP Support */
|
2017-09-26 04:53:31 +03:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
WebPEncode_wrapper(PyObject *self, PyObject *args) {
|
2013-03-12 18:30:59 +04:00
|
|
|
int width;
|
|
|
|
int height;
|
2013-10-20 23:44:22 +04:00
|
|
|
int lossless;
|
2013-03-12 18:30:59 +04:00
|
|
|
float quality_factor;
|
2020-04-11 13:43:49 +03:00
|
|
|
int method;
|
2021-01-03 06:17:51 +03:00
|
|
|
uint8_t *rgb;
|
|
|
|
uint8_t *icc_bytes;
|
|
|
|
uint8_t *exif_bytes;
|
|
|
|
uint8_t *xmp_bytes;
|
|
|
|
uint8_t *output;
|
|
|
|
char *mode;
|
2013-03-14 04:42:26 +04:00
|
|
|
Py_ssize_t size;
|
2013-10-20 23:44:22 +04:00
|
|
|
Py_ssize_t icc_size;
|
2017-09-27 06:27:40 +03:00
|
|
|
Py_ssize_t exif_size;
|
|
|
|
Py_ssize_t xmp_size;
|
2013-03-14 04:42:26 +04:00
|
|
|
size_t ret_size;
|
2020-04-11 13:43:49 +03:00
|
|
|
int rgba_mode;
|
|
|
|
int channels;
|
|
|
|
int ok;
|
2020-02-17 23:56:30 +03:00
|
|
|
ImagingSectionCookie cookie;
|
2020-04-11 13:43:49 +03:00
|
|
|
WebPConfig config;
|
|
|
|
WebPMemoryWriter writer;
|
|
|
|
WebPPicture pic;
|
2013-03-12 18:30:59 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
if (!PyArg_ParseTuple(
|
|
|
|
args,
|
|
|
|
"y#iiifss#is#s#",
|
|
|
|
(char **)&rgb,
|
|
|
|
&size,
|
|
|
|
&width,
|
|
|
|
&height,
|
|
|
|
&lossless,
|
|
|
|
&quality_factor,
|
|
|
|
&mode,
|
|
|
|
&icc_bytes,
|
|
|
|
&icc_size,
|
|
|
|
&method,
|
|
|
|
&exif_bytes,
|
|
|
|
&exif_size,
|
|
|
|
&xmp_bytes,
|
|
|
|
&xmp_size)) {
|
2017-05-12 01:20:22 +03:00
|
|
|
return NULL;
|
2013-03-12 18:30:59 +04:00
|
|
|
}
|
2020-04-11 13:43:49 +03:00
|
|
|
|
|
|
|
rgba_mode = strcmp(mode, "RGBA") == 0;
|
|
|
|
if (!rgba_mode && strcmp(mode, "RGB") != 0) {
|
2013-10-20 23:44:22 +04:00
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2013-05-13 20:01:42 +04:00
|
|
|
|
2020-04-11 13:43:49 +03:00
|
|
|
channels = rgba_mode ? 4 : 3;
|
|
|
|
if (size < width * height * channels) {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup config for this frame
|
|
|
|
if (!WebPConfigInit(&config)) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "failed to initialize config!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
config.lossless = lossless;
|
|
|
|
config.quality = quality_factor;
|
|
|
|
config.method = method;
|
|
|
|
|
|
|
|
// Validate the config
|
|
|
|
if (!WebPValidateConfig(&config)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "invalid configuration");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WebPPictureInit(&pic)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "could not initialise picture");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
pic.width = width;
|
|
|
|
pic.height = height;
|
2021-01-03 06:17:51 +03:00
|
|
|
pic.use_argb = 1; // Don't convert RGB pixels to YUV
|
2020-04-11 13:43:49 +03:00
|
|
|
|
|
|
|
if (rgba_mode) {
|
|
|
|
WebPPictureImportRGBA(&pic, rgb, channels * width);
|
|
|
|
} else {
|
|
|
|
WebPPictureImportRGB(&pic, rgb, channels * width);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebPMemoryWriterInit(&writer);
|
|
|
|
pic.writer = WebPMemoryWrite;
|
|
|
|
pic.custom_ptr = &writer;
|
|
|
|
|
|
|
|
ImagingSectionEnter(&cookie);
|
|
|
|
ok = WebPEncode(&config, &pic);
|
|
|
|
ImagingSectionLeave(&cookie);
|
|
|
|
|
|
|
|
WebPPictureFree(&pic);
|
|
|
|
if (!ok) {
|
2021-05-06 17:12:03 +03:00
|
|
|
PyErr_Format(PyExc_ValueError, "encoding error %d", (&pic)->error_code);
|
2020-04-11 13:43:49 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
output = writer.mem;
|
|
|
|
ret_size = writer.size;
|
|
|
|
|
2013-07-14 07:26:12 +04:00
|
|
|
#ifndef HAVE_WEBPMUX
|
|
|
|
if (ret_size > 0) {
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *ret = PyBytes_FromStringAndSize((char *)output, ret_size);
|
2013-07-14 07:26:12 +04:00
|
|
|
free(output);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
2017-09-28 03:10:25 +03:00
|
|
|
{
|
2021-01-03 06:17:51 +03:00
|
|
|
/* I want to truncate the *_size items that get passed into WebP
|
|
|
|
data. Pypy2.1.0 had some issues where the Py_ssize_t items had
|
|
|
|
data in the upper byte. (Not sure why, it shouldn't have been there)
|
|
|
|
*/
|
|
|
|
int i_icc_size = (int)icc_size;
|
|
|
|
int i_exif_size = (int)exif_size;
|
|
|
|
int i_xmp_size = (int)xmp_size;
|
|
|
|
WebPData output_data = {0};
|
|
|
|
WebPData image = {output, ret_size};
|
|
|
|
WebPData icc_profile = {icc_bytes, i_icc_size};
|
|
|
|
WebPData exif = {exif_bytes, i_exif_size};
|
|
|
|
WebPData xmp = {xmp_bytes, i_xmp_size};
|
|
|
|
WebPMuxError err;
|
|
|
|
int dbg = 0;
|
|
|
|
|
|
|
|
int copy_data = 0; // value 1 indicates given data WILL be copied to the mux
|
|
|
|
// and value 0 indicates data will NOT be copied.
|
|
|
|
|
|
|
|
WebPMux *mux = WebPMuxNew();
|
|
|
|
WebPMuxSetImage(mux, &image, copy_data);
|
|
|
|
|
2013-10-02 23:03:06 +04:00
|
|
|
if (dbg) {
|
2021-01-03 06:17:51 +03:00
|
|
|
/* was getting %ld icc_size == 0, icc_size>0 was true */
|
|
|
|
fprintf(stderr, "icc size %d, %d \n", i_icc_size, i_icc_size > 0);
|
2013-10-02 23:03:06 +04:00
|
|
|
}
|
2021-01-03 06:17:51 +03:00
|
|
|
|
|
|
|
if (i_icc_size > 0) {
|
|
|
|
if (dbg) {
|
|
|
|
fprintf(stderr, "Adding ICC Profile\n");
|
|
|
|
}
|
|
|
|
err = WebPMuxSetChunk(mux, "ICCP", &icc_profile, copy_data);
|
|
|
|
if (err != WEBP_MUX_OK) {
|
|
|
|
return HandleMuxError(err, "ICCP");
|
|
|
|
}
|
2013-10-02 23:03:06 +04:00
|
|
|
}
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2017-09-28 06:12:10 +03:00
|
|
|
if (dbg) {
|
2021-01-03 06:17:51 +03:00
|
|
|
fprintf(stderr, "exif size %d \n", i_exif_size);
|
2013-10-02 23:03:06 +04:00
|
|
|
}
|
2021-01-03 06:17:51 +03:00
|
|
|
if (i_exif_size > 0) {
|
|
|
|
if (dbg) {
|
|
|
|
fprintf(stderr, "Adding Exif Data\n");
|
|
|
|
}
|
|
|
|
err = WebPMuxSetChunk(mux, "EXIF", &exif, copy_data);
|
|
|
|
if (err != WEBP_MUX_OK) {
|
|
|
|
return HandleMuxError(err, "EXIF");
|
|
|
|
}
|
2013-10-02 23:03:06 +04:00
|
|
|
}
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
if (dbg) {
|
|
|
|
fprintf(stderr, "xmp size %d \n", i_xmp_size);
|
2017-09-27 06:27:40 +03:00
|
|
|
}
|
2021-01-03 06:17:51 +03:00
|
|
|
if (i_xmp_size > 0) {
|
|
|
|
if (dbg) {
|
|
|
|
fprintf(stderr, "Adding XMP Data\n");
|
|
|
|
}
|
|
|
|
err = WebPMuxSetChunk(mux, "XMP ", &xmp, copy_data);
|
|
|
|
if (err != WEBP_MUX_OK) {
|
|
|
|
return HandleMuxError(err, "XMP ");
|
|
|
|
}
|
2017-09-27 06:27:40 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
WebPMuxAssemble(mux, &output_data);
|
|
|
|
WebPMuxDelete(mux);
|
|
|
|
free(output);
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
ret_size = output_data.size;
|
|
|
|
if (ret_size > 0) {
|
|
|
|
PyObject *ret =
|
|
|
|
PyBytes_FromStringAndSize((char *)output_data.bytes, ret_size);
|
|
|
|
WebPDataClear(&output_data);
|
|
|
|
return ret;
|
|
|
|
}
|
2013-07-14 07:26:12 +04:00
|
|
|
}
|
|
|
|
#endif
|
2013-05-14 08:47:35 +04:00
|
|
|
Py_RETURN_NONE;
|
2013-05-13 20:01:42 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
WebPDecode_wrapper(PyObject *self, PyObject *args) {
|
|
|
|
PyBytesObject *webp_string;
|
|
|
|
const uint8_t *webp;
|
2013-03-14 04:42:26 +04:00
|
|
|
Py_ssize_t size;
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *ret = Py_None, *bytes = NULL, *pymode = NULL, *icc_profile = NULL,
|
|
|
|
*exif = NULL;
|
2013-05-14 08:47:35 +04:00
|
|
|
WebPDecoderConfig config;
|
2013-05-14 07:50:10 +04:00
|
|
|
VP8StatusCode vp8_status_code = VP8_STATUS_OK;
|
2021-01-03 06:17:51 +03:00
|
|
|
char *mode = "RGB";
|
2013-03-12 18:30:59 +04:00
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "S", &webp_string)) {
|
2017-05-12 01:20:22 +03:00
|
|
|
return NULL;
|
2013-03-14 04:42:26 +04:00
|
|
|
}
|
|
|
|
|
2013-05-14 08:47:35 +04:00
|
|
|
if (!WebPInitDecoderConfig(&config)) {
|
|
|
|
Py_RETURN_NONE;
|
2013-07-01 02:42:19 +04:00
|
|
|
}
|
2013-05-13 20:01:42 +04:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyBytes_AsStringAndSize((PyObject *)webp_string, (char **)&webp, &size);
|
2013-05-13 20:01:42 +04:00
|
|
|
|
2013-05-14 07:50:10 +04:00
|
|
|
vp8_status_code = WebPGetFeatures(webp, size, &config.input);
|
2013-05-14 08:47:35 +04:00
|
|
|
if (vp8_status_code == VP8_STATUS_OK) {
|
2013-07-01 02:42:19 +04:00
|
|
|
// If we don't set it, we don't get alpha.
|
2013-05-14 08:47:35 +04:00
|
|
|
// Initialized to MODE_RGB
|
|
|
|
if (config.input.has_alpha) {
|
|
|
|
config.output.colorspace = MODE_RGBA;
|
|
|
|
mode = "RGBA";
|
|
|
|
}
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2017-09-28 05:04:24 +03:00
|
|
|
#ifndef HAVE_WEBPMUX
|
|
|
|
vp8_status_code = WebPDecode(webp, size, &config);
|
|
|
|
#else
|
|
|
|
{
|
2021-01-03 06:17:51 +03:00
|
|
|
int copy_data = 0;
|
|
|
|
WebPData data = {webp, size};
|
|
|
|
WebPMuxFrameInfo image;
|
|
|
|
WebPData icc_profile_data = {0};
|
|
|
|
WebPData exif_data = {0};
|
|
|
|
|
|
|
|
WebPMux *mux = WebPMuxCreate(&data, copy_data);
|
|
|
|
if (NULL == mux) {
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-28 05:04:24 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
if (WEBP_MUX_OK != WebPMuxGetFrame(mux, 1, &image)) {
|
|
|
|
WebPMuxDelete(mux);
|
|
|
|
goto end;
|
|
|
|
}
|
2017-09-28 05:04:24 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
webp = image.bitstream.bytes;
|
|
|
|
size = image.bitstream.size;
|
2017-09-28 05:04:24 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
vp8_status_code = WebPDecode(webp, size, &config);
|
2017-09-28 05:04:24 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
if (WEBP_MUX_OK == WebPMuxGetChunk(mux, "ICCP", &icc_profile_data)) {
|
|
|
|
icc_profile = PyBytes_FromStringAndSize(
|
|
|
|
(const char *)icc_profile_data.bytes, icc_profile_data.size);
|
|
|
|
}
|
2017-09-28 05:04:24 +03:00
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
if (WEBP_MUX_OK == WebPMuxGetChunk(mux, "EXIF", &exif_data)) {
|
|
|
|
exif = PyBytes_FromStringAndSize(
|
|
|
|
(const char *)exif_data.bytes, exif_data.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebPDataClear(&image.bitstream);
|
|
|
|
WebPMuxDelete(mux);
|
2017-09-28 05:04:24 +03:00
|
|
|
}
|
|
|
|
#endif
|
2013-07-01 02:42:19 +04:00
|
|
|
}
|
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (vp8_status_code != VP8_STATUS_OK) {
|
2015-02-23 08:00:41 +03:00
|
|
|
goto end;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-14 08:47:35 +04:00
|
|
|
if (config.output.colorspace < MODE_YUV) {
|
2021-01-03 06:17:51 +03:00
|
|
|
bytes = PyBytes_FromStringAndSize(
|
|
|
|
(char *)config.output.u.RGBA.rgba, config.output.u.RGBA.size);
|
2013-05-14 08:47:35 +04:00
|
|
|
} else {
|
|
|
|
// Skipping YUV for now. Need Test Images.
|
|
|
|
// UNDONE -- unclear if we'll ever get here if we set mode_rgb*
|
2021-01-03 06:17:51 +03:00
|
|
|
bytes = PyBytes_FromStringAndSize(
|
|
|
|
(char *)config.output.u.YUVA.y, config.output.u.YUVA.y_size);
|
2013-05-14 08:47:35 +04:00
|
|
|
}
|
2013-05-14 07:50:10 +04:00
|
|
|
|
2013-05-14 08:43:13 +04:00
|
|
|
pymode = PyUnicode_FromString(mode);
|
2021-01-03 06:17:51 +03:00
|
|
|
ret = Py_BuildValue(
|
|
|
|
"SiiSSS",
|
|
|
|
bytes,
|
|
|
|
config.output.width,
|
|
|
|
config.output.height,
|
|
|
|
pymode,
|
|
|
|
NULL == icc_profile ? Py_None : icc_profile,
|
|
|
|
NULL == exif ? Py_None : exif);
|
2015-02-23 08:00:41 +03:00
|
|
|
|
|
|
|
end:
|
2013-05-14 08:47:35 +04:00
|
|
|
WebPFreeDecBuffer(&config.output);
|
2015-02-23 08:00:41 +03:00
|
|
|
|
|
|
|
Py_XDECREF(bytes);
|
|
|
|
Py_XDECREF(pymode);
|
2017-09-28 05:04:24 +03:00
|
|
|
Py_XDECREF(icc_profile);
|
|
|
|
Py_XDECREF(exif);
|
2015-02-23 08:00:41 +03:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (Py_None == ret) {
|
2015-02-23 08:00:41 +03:00
|
|
|
Py_RETURN_NONE;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2015-02-23 08:00:41 +03:00
|
|
|
|
2013-05-14 08:47:35 +04:00
|
|
|
return ret;
|
2013-05-13 20:01:42 +04:00
|
|
|
}
|
|
|
|
|
2013-05-14 08:28:18 +04:00
|
|
|
// Return the decoder's version number, packed in hexadecimal using 8bits for
|
|
|
|
// each of major/minor/revision. E.g: v2.5.7 is 0x020507.
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
WebPDecoderVersion_wrapper() {
|
2013-05-14 08:47:35 +04:00
|
|
|
return Py_BuildValue("i", WebPGetDecoderVersion());
|
2013-05-14 08:28:18 +04:00
|
|
|
}
|
2013-05-13 20:01:42 +04:00
|
|
|
|
2020-06-14 06:35:43 +03:00
|
|
|
// Version as string
|
2021-01-03 06:17:51 +03:00
|
|
|
const char *
|
|
|
|
WebPDecoderVersion_str(void) {
|
2020-06-14 06:35:43 +03:00
|
|
|
static char version[20];
|
|
|
|
int version_number = WebPGetDecoderVersion();
|
2021-01-03 06:17:51 +03:00
|
|
|
sprintf(
|
|
|
|
version,
|
|
|
|
"%d.%d.%d",
|
|
|
|
version_number >> 16,
|
|
|
|
(version_number >> 8) % 0x100,
|
|
|
|
version_number % 0x100);
|
2020-06-14 06:35:43 +03:00
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2013-05-14 08:28:18 +04:00
|
|
|
/*
|
2013-05-14 09:21:52 +04:00
|
|
|
* The version of webp that ships with (0.1.3) Ubuntu 12.04 doesn't handle alpha well.
|
2013-07-01 02:42:19 +04:00
|
|
|
* Files that are valid with 0.3 are reported as being invalid.
|
2013-05-14 08:28:18 +04:00
|
|
|
*/
|
2021-01-03 06:17:51 +03:00
|
|
|
int
|
|
|
|
WebPDecoderBuggyAlpha(void) {
|
|
|
|
return WebPGetDecoderVersion() == 0x0103;
|
2017-05-12 00:01:58 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *
|
|
|
|
WebPDecoderBuggyAlpha_wrapper() {
|
2017-05-12 00:01:58 +03:00
|
|
|
return Py_BuildValue("i", WebPDecoderBuggyAlpha());
|
2013-05-14 08:28:18 +04:00
|
|
|
}
|
2013-05-14 07:50:10 +04:00
|
|
|
|
2017-09-26 04:53:31 +03:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Module Setup */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
static PyMethodDef webpMethods[] = {
|
2017-09-28 05:04:24 +03:00
|
|
|
#ifdef HAVE_WEBPANIM
|
2017-09-26 12:58:54 +03:00
|
|
|
{"WebPAnimDecoder", _anim_decoder_new, METH_VARARGS, "WebPAnimDecoder"},
|
2017-09-26 04:53:31 +03:00
|
|
|
{"WebPAnimEncoder", _anim_encoder_new, METH_VARARGS, "WebPAnimEncoder"},
|
2017-09-27 06:27:40 +03:00
|
|
|
#endif
|
2013-05-16 03:56:59 +04:00
|
|
|
{"WebPEncode", WebPEncode_wrapper, METH_VARARGS, "WebPEncode"},
|
2013-05-14 07:50:10 +04:00
|
|
|
{"WebPDecode", WebPDecode_wrapper, METH_VARARGS, "WebPDecode"},
|
2020-02-21 14:24:20 +03:00
|
|
|
{"WebPDecoderVersion", WebPDecoderVersion_wrapper, METH_NOARGS, "WebPVersion"},
|
2021-01-03 06:17:51 +03:00
|
|
|
{"WebPDecoderBuggyAlpha",
|
|
|
|
WebPDecoderBuggyAlpha_wrapper,
|
|
|
|
METH_NOARGS,
|
|
|
|
"WebPDecoderBuggyAlpha"},
|
|
|
|
{NULL, NULL}};
|
|
|
|
|
|
|
|
void
|
|
|
|
addMuxFlagToModule(PyObject *m) {
|
2021-01-09 03:33:26 +03:00
|
|
|
PyObject *have_webpmux;
|
2013-07-05 00:57:05 +04:00
|
|
|
#ifdef HAVE_WEBPMUX
|
2021-01-09 03:33:26 +03:00
|
|
|
have_webpmux = Py_True;
|
2013-07-05 00:57:05 +04:00
|
|
|
#else
|
2021-01-09 03:33:26 +03:00
|
|
|
have_webpmux = Py_False;
|
2013-07-05 00:57:05 +04:00
|
|
|
#endif
|
2021-01-09 03:33:26 +03:00
|
|
|
Py_INCREF(have_webpmux);
|
|
|
|
PyModule_AddObject(m, "HAVE_WEBPMUX", have_webpmux);
|
2013-07-05 00:57:05 +04:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
void
|
|
|
|
addAnimFlagToModule(PyObject *m) {
|
2021-01-09 03:33:26 +03:00
|
|
|
PyObject *have_webpanim;
|
2017-09-28 05:04:24 +03:00
|
|
|
#ifdef HAVE_WEBPANIM
|
2021-01-09 03:33:26 +03:00
|
|
|
have_webpanim = Py_True;
|
2017-09-28 05:04:24 +03:00
|
|
|
#else
|
2021-01-09 03:33:26 +03:00
|
|
|
have_webpanim = Py_False;
|
2017-09-28 05:04:24 +03:00
|
|
|
#endif
|
2021-01-09 03:33:26 +03:00
|
|
|
Py_INCREF(have_webpanim);
|
|
|
|
PyModule_AddObject(m, "HAVE_WEBPANIM", have_webpanim);
|
2017-09-28 05:04:24 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
void
|
|
|
|
addTransparencyFlagToModule(PyObject *m) {
|
|
|
|
PyModule_AddObject(
|
|
|
|
m, "HAVE_TRANSPARENCY", PyBool_FromLong(!WebPDecoderBuggyAlpha()));
|
2017-05-12 00:01:58 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
static int
|
|
|
|
setup_module(PyObject *m) {
|
|
|
|
PyObject *d = PyModule_GetDict(m);
|
2017-09-26 04:53:31 +03:00
|
|
|
addMuxFlagToModule(m);
|
2017-09-28 05:04:24 +03:00
|
|
|
addAnimFlagToModule(m);
|
2017-09-26 04:53:31 +03:00
|
|
|
addTransparencyFlagToModule(m);
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
PyDict_SetItemString(
|
|
|
|
d, "webpdecoder_version", PyUnicode_FromString(WebPDecoderVersion_str()));
|
2020-06-14 06:35:43 +03:00
|
|
|
|
2017-09-28 05:04:24 +03:00
|
|
|
#ifdef HAVE_WEBPANIM
|
2017-09-26 04:53:31 +03:00
|
|
|
/* Ready object types */
|
2017-09-26 12:58:54 +03:00
|
|
|
if (PyType_Ready(&WebPAnimDecoder_Type) < 0 ||
|
2020-05-10 12:56:36 +03:00
|
|
|
PyType_Ready(&WebPAnimEncoder_Type) < 0) {
|
2017-09-26 04:53:31 +03:00
|
|
|
return -1;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2017-09-26 04:53:31 +03:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2013-05-13 20:01:42 +04:00
|
|
|
|
2013-03-14 06:37:00 +04:00
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__webp(void) {
|
2021-01-03 06:17:51 +03:00
|
|
|
PyObject *m;
|
2013-03-14 06:37:00 +04:00
|
|
|
|
|
|
|
static PyModuleDef module_def = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
2021-01-03 06:17:51 +03:00
|
|
|
"_webp", /* m_name */
|
|
|
|
NULL, /* m_doc */
|
|
|
|
-1, /* m_size */
|
|
|
|
webpMethods, /* m_methods */
|
2013-03-14 06:37:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
m = PyModule_Create(&module_def);
|
2020-05-10 12:56:36 +03:00
|
|
|
if (setup_module(m) < 0) {
|
2017-09-26 04:53:31 +03:00
|
|
|
return NULL;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2017-09-26 04:53:31 +03:00
|
|
|
|
2013-03-14 06:37:00 +04:00
|
|
|
return m;
|
|
|
|
}
|