2013-06-30 01:05:48 +04:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
2013-03-12 18:30:59 +04:00
|
|
|
#include <Python.h>
|
2013-03-14 06:37:00 +04:00
|
|
|
#include "py3.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>
|
|
|
|
#endif
|
2013-05-13 20:02:36 +04:00
|
|
|
|
2013-05-16 03:56:59 +04: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;
|
2013-03-14 04:42:26 +04:00
|
|
|
uint8_t *rgb;
|
2013-07-05 00:57:05 +04:00
|
|
|
uint8_t *icc_bytes;
|
|
|
|
uint8_t *exif_bytes;
|
2013-03-14 04:42:26 +04:00
|
|
|
uint8_t *output;
|
2013-07-05 00:57:05 +04:00
|
|
|
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;
|
2013-10-04 22:27:28 +04:00
|
|
|
Py_ssize_t exif_size;
|
2013-03-14 04:42:26 +04:00
|
|
|
size_t ret_size;
|
2013-03-12 18:30:59 +04:00
|
|
|
|
2013-10-21 07:54:12 +04:00
|
|
|
if (!PyArg_ParseTuple(args, "s#iiifss#s#",
|
2013-10-20 23:44:22 +04:00
|
|
|
(char**)&rgb, &size, &width, &height, &lossless, &quality_factor, &mode,
|
2013-07-05 00:57:05 +04:00
|
|
|
&icc_bytes, &icc_size, &exif_bytes, &exif_size)) {
|
2013-05-14 08:47:35 +04:00
|
|
|
Py_RETURN_NONE;
|
2013-03-12 18:30:59 +04:00
|
|
|
}
|
2013-10-20 23:44:22 +04:00
|
|
|
if (strcmp(mode, "RGBA")==0){
|
|
|
|
if (size < width * height * 4){
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2014-01-17 17:20:11 +04:00
|
|
|
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
|
2013-10-21 07:54:12 +04:00
|
|
|
if (lossless) {
|
2013-10-20 23:44:22 +04:00
|
|
|
ret_size = WebPEncodeLosslessRGBA(rgb, width, height, 4* width, &output);
|
2014-01-17 17:20:11 +04:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2013-10-20 23:44:22 +04:00
|
|
|
ret_size = WebPEncodeRGBA(rgb, width, height, 4* width, quality_factor, &output);
|
|
|
|
}
|
|
|
|
} else if (strcmp(mode, "RGB")==0){
|
|
|
|
if (size < width * height * 3){
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2014-01-17 17:20:11 +04:00
|
|
|
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
|
2013-10-21 07:54:12 +04:00
|
|
|
if (lossless) {
|
2013-10-20 23:44:22 +04:00
|
|
|
ret_size = WebPEncodeLosslessRGB(rgb, width, height, 3* width, &output);
|
2014-01-17 17:20:11 +04:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2013-10-20 23:44:22 +04:00
|
|
|
ret_size = WebPEncodeRGB(rgb, width, height, 3* width, quality_factor, &output);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2013-05-13 20:01:42 +04:00
|
|
|
|
2013-07-14 07:26:12 +04:00
|
|
|
#ifndef HAVE_WEBPMUX
|
|
|
|
if (ret_size > 0) {
|
|
|
|
PyObject *ret = PyBytes_FromStringAndSize((char*)output, ret_size);
|
|
|
|
free(output);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
2013-10-04 22:27:28 +04:00
|
|
|
/* I want to truncate the *_size items that get passed into webp
|
2013-10-20 23:44:22 +04:00
|
|
|
data. Pypy2.1.0 had some issues where the Py_ssize_t items had
|
2013-10-04 22:27:28 +04:00
|
|
|
data in the upper byte. (Not sure why, it shouldn't have been there)
|
|
|
|
*/
|
2013-10-20 23:44:22 +04:00
|
|
|
int i_icc_size = (int)icc_size;
|
2013-10-04 22:27:28 +04:00
|
|
|
int i_exif_size = (int)exif_size;
|
2013-07-05 00:57:05 +04:00
|
|
|
WebPData output_data = {0};
|
|
|
|
WebPData image = { output, ret_size };
|
2013-10-04 22:27:28 +04:00
|
|
|
WebPData icc_profile = { icc_bytes, i_icc_size };
|
|
|
|
WebPData exif = { exif_bytes, i_exif_size };
|
2013-10-02 23:03:06 +04:00
|
|
|
WebPMuxError err;
|
|
|
|
int dbg = 0;
|
2013-07-05 00:57:05 +04:00
|
|
|
|
|
|
|
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) {
|
2013-10-04 22:27:28 +04: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
|
|
|
}
|
2013-10-04 22:27:28 +04:00
|
|
|
|
|
|
|
if (i_icc_size > 0) {
|
2013-10-02 23:03:06 +04:00
|
|
|
if (dbg) {
|
|
|
|
fprintf (stderr, "Adding ICC Profile\n");
|
|
|
|
}
|
|
|
|
err = WebPMuxSetChunk(mux, "ICCP", &icc_profile, copy_data);
|
|
|
|
if (dbg && err == WEBP_MUX_INVALID_ARGUMENT) {
|
|
|
|
fprintf(stderr, "Invalid ICC Argument\n");
|
|
|
|
} else if (dbg && err == WEBP_MUX_MEMORY_ERROR) {
|
|
|
|
fprintf(stderr, "ICC Memory Error\n");
|
|
|
|
}
|
2013-07-05 00:57:05 +04:00
|
|
|
}
|
|
|
|
|
2013-10-02 23:03:06 +04:00
|
|
|
if (dbg) {
|
2013-10-04 22:27:28 +04:00
|
|
|
fprintf(stderr, "exif size %d \n", i_exif_size);
|
2013-10-02 23:03:06 +04:00
|
|
|
}
|
2013-10-04 22:27:28 +04:00
|
|
|
if (i_exif_size > 0) {
|
2013-10-02 23:03:06 +04:00
|
|
|
if (dbg){
|
|
|
|
fprintf (stderr, "Adding Exif Data\n");
|
|
|
|
}
|
|
|
|
err = WebPMuxSetChunk(mux, "EXIF", &exif, copy_data);
|
|
|
|
if (dbg && err == WEBP_MUX_INVALID_ARGUMENT) {
|
|
|
|
fprintf(stderr, "Invalid Exif Argument\n");
|
|
|
|
} else if (dbg && err == WEBP_MUX_MEMORY_ERROR) {
|
|
|
|
fprintf(stderr, "Exif Memory Error\n");
|
|
|
|
}
|
2013-07-05 00:57:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
WebPMuxAssemble(mux, &output_data);
|
|
|
|
WebPMuxDelete(mux);
|
2013-11-29 20:33:57 +04:00
|
|
|
free(output);
|
2013-07-05 00:57:05 +04:00
|
|
|
|
|
|
|
ret_size = output_data.size;
|
2013-05-13 20:01:42 +04:00
|
|
|
if (ret_size > 0) {
|
2013-11-29 20:33:57 +04:00
|
|
|
PyObject *ret = PyBytes_FromStringAndSize((char*)output_data.bytes, ret_size);
|
2013-07-05 00:57:05 +04:00
|
|
|
WebPDataClear(&output_data);
|
2013-05-13 20:01:42 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-14 07:50:10 +04:00
|
|
|
PyObject* WebPDecode_wrapper(PyObject* self, PyObject* args)
|
2013-03-12 18:30:59 +04:00
|
|
|
{
|
2013-03-14 06:37:00 +04:00
|
|
|
PyBytesObject *webp_string;
|
2015-02-23 07:59:40 +03:00
|
|
|
const uint8_t *webp;
|
2013-03-14 04:42:26 +04:00
|
|
|
Py_ssize_t size;
|
2015-02-23 08:00:41 +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;
|
2013-05-14 08:47:35 +04:00
|
|
|
char* mode = "RGB";
|
2013-03-12 18:30:59 +04:00
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "S", &webp_string)) {
|
2013-05-14 08:47:35 +04:00
|
|
|
Py_RETURN_NONE;
|
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
|
|
|
|
|
|
|
PyBytes_AsStringAndSize((PyObject *) webp_string, (char**)&webp, &size);
|
|
|
|
|
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
|
|
|
|
2013-07-14 07:26:12 +04:00
|
|
|
#ifndef HAVE_WEBPMUX
|
|
|
|
vp8_status_code = WebPDecode(webp, size, &config);
|
|
|
|
#else
|
|
|
|
{
|
2013-07-05 00:57:05 +04:00
|
|
|
int copy_data = 0;
|
|
|
|
WebPData data = { webp, size };
|
|
|
|
WebPMuxFrameInfo image;
|
2013-07-14 07:26:12 +04:00
|
|
|
WebPData icc_profile_data = {0};
|
|
|
|
WebPData exif_data = {0};
|
2013-07-05 00:57:05 +04:00
|
|
|
|
|
|
|
WebPMux* mux = WebPMuxCreate(&data, copy_data);
|
2015-02-23 08:04:32 +03:00
|
|
|
if (NULL == mux)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (WEBP_MUX_OK != WebPMuxGetFrame(mux, 1, &image))
|
|
|
|
{
|
|
|
|
WebPMuxDelete(mux);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2015-02-23 07:59:40 +03:00
|
|
|
webp = image.bitstream.bytes;
|
2013-07-05 00:57:05 +04:00
|
|
|
size = image.bitstream.size;
|
|
|
|
|
2013-05-14 08:47:35 +04:00
|
|
|
vp8_status_code = WebPDecode(webp, size, &config);
|
2013-07-05 00:57:05 +04:00
|
|
|
|
2015-02-23 08:04:32 +03:00
|
|
|
if (WEBP_MUX_OK == WebPMuxGetChunk(mux, "ICCP", &icc_profile_data))
|
2013-07-05 00:57:05 +04:00
|
|
|
icc_profile = PyBytes_FromStringAndSize((const char*)icc_profile_data.bytes, icc_profile_data.size);
|
|
|
|
|
2015-02-23 08:04:32 +03:00
|
|
|
if (WEBP_MUX_OK == WebPMuxGetChunk(mux, "EXIF", &exif_data))
|
2013-07-05 00:57:05 +04:00
|
|
|
exif = PyBytes_FromStringAndSize((const char*)exif_data.bytes, exif_data.size);
|
|
|
|
|
2015-02-23 08:04:09 +03:00
|
|
|
WebPDataClear(&image.bitstream);
|
2013-07-05 00:57:05 +04:00
|
|
|
WebPMuxDelete(mux);
|
2013-07-14 07:26:12 +04:00
|
|
|
}
|
2013-07-05 00:57:05 +04:00
|
|
|
#endif
|
2013-07-01 02:42:19 +04:00
|
|
|
}
|
|
|
|
|
2015-02-23 08:00:41 +03:00
|
|
|
if (vp8_status_code != VP8_STATUS_OK)
|
|
|
|
goto end;
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2013-05-14 08:47:35 +04:00
|
|
|
if (config.output.colorspace < MODE_YUV) {
|
2013-07-01 02:42:19 +04:00
|
|
|
bytes = PyBytes_FromStringAndSize((char *)config.output.u.RGBA.rgba,
|
2013-05-14 08:47:35 +04:00
|
|
|
config.output.u.RGBA.size);
|
|
|
|
} else {
|
|
|
|
// Skipping YUV for now. Need Test Images.
|
|
|
|
// UNDONE -- unclear if we'll ever get here if we set mode_rgb*
|
2013-07-01 02:42:19 +04:00
|
|
|
bytes = PyBytes_FromStringAndSize((char *)config.output.u.YUVA.y,
|
2013-05-14 08:47:35 +04:00
|
|
|
config.output.u.YUVA.y_size);
|
|
|
|
}
|
2013-05-14 07:50:10 +04:00
|
|
|
|
2013-05-14 08:43:13 +04:00
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
|
|
|
pymode = PyUnicode_FromString(mode);
|
|
|
|
#else
|
|
|
|
pymode = PyString_FromString(mode);
|
|
|
|
#endif
|
2013-07-05 00:57:05 +04:00
|
|
|
ret = Py_BuildValue("SiiSSS", bytes, config.output.width,
|
2015-02-23 08:00:41 +03:00
|
|
|
config.output.height, pymode,
|
|
|
|
NULL == icc_profile ? Py_None : icc_profile,
|
|
|
|
NULL == exif ? Py_None : exif);
|
|
|
|
|
|
|
|
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);
|
|
|
|
Py_XDECREF(icc_profile);
|
|
|
|
Py_XDECREF(exif);
|
|
|
|
|
|
|
|
if (Py_None == ret)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
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.
|
|
|
|
PyObject* WebPDecoderVersion_wrapper(PyObject* self, PyObject* args){
|
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
|
|
|
|
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
|
|
|
*/
|
|
|
|
PyObject* WebPDecoderBuggyAlpha_wrapper(PyObject* self, PyObject* args){
|
2013-05-14 09:21:52 +04:00
|
|
|
return Py_BuildValue("i", WebPGetDecoderVersion()==0x0103);
|
2013-05-14 08:28:18 +04:00
|
|
|
}
|
2013-05-14 07:50:10 +04:00
|
|
|
|
2013-03-12 18:30:59 +04:00
|
|
|
static PyMethodDef webpMethods[] =
|
|
|
|
{
|
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"},
|
2013-05-14 08:28:18 +04:00
|
|
|
{"WebPDecoderVersion", WebPDecoderVersion_wrapper, METH_VARARGS, "WebPVersion"},
|
|
|
|
{"WebPDecoderBuggyAlpha", WebPDecoderBuggyAlpha_wrapper, METH_VARARGS, "WebPDecoderBuggyAlpha"},
|
2013-03-12 18:30:59 +04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2013-07-05 00:57:05 +04:00
|
|
|
void addMuxFlagToModule(PyObject* m) {
|
|
|
|
#ifdef HAVE_WEBPMUX
|
|
|
|
PyModule_AddObject(m, "HAVE_WEBPMUX", Py_True);
|
|
|
|
#else
|
|
|
|
PyModule_AddObject(m, "HAVE_WEBPMUX", Py_False);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-13 20:01:42 +04:00
|
|
|
|
2013-03-14 06:37:00 +04:00
|
|
|
#if PY_VERSION_HEX >= 0x03000000
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__webp(void) {
|
|
|
|
PyObject* m;
|
|
|
|
|
|
|
|
static PyModuleDef module_def = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
"_webp", /* m_name */
|
|
|
|
NULL, /* m_doc */
|
|
|
|
-1, /* m_size */
|
|
|
|
webpMethods, /* m_methods */
|
|
|
|
};
|
|
|
|
|
|
|
|
m = PyModule_Create(&module_def);
|
2013-07-05 00:57:05 +04:00
|
|
|
addMuxFlagToModule(m);
|
2013-03-14 06:37:00 +04:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
PyMODINIT_FUNC
|
2013-04-13 02:28:26 +04:00
|
|
|
init_webp(void)
|
2013-03-12 18:30:59 +04:00
|
|
|
{
|
2013-07-05 00:57:05 +04:00
|
|
|
PyObject* m = Py_InitModule("_webp", webpMethods);
|
|
|
|
addMuxFlagToModule(m);
|
2013-03-12 18:30:59 +04:00
|
|
|
}
|
2013-03-14 06:37:00 +04:00
|
|
|
#endif
|