Pillow/_webp.c

95 lines
2.2 KiB
C
Raw Normal View History

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>
PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args)
{
2013-03-14 06:37:00 +04:00
PyBytesObject *rgb_string;
2013-03-12 18:30:59 +04:00
int width;
int height;
int stride;
float quality_factor;
2013-03-14 04:42:26 +04:00
uint8_t *rgb;
uint8_t *output;
Py_ssize_t size;
size_t ret_size;
2013-03-12 18:30:59 +04:00
if (!PyArg_ParseTuple(args, "Siiif", &rgb_string, &width, &height, &stride, &quality_factor)) {
Py_INCREF(Py_None);
return Py_None;
}
2013-03-14 04:42:26 +04:00
PyBytes_AsStringAndSize((PyObject *) rgb_string, (char**)&rgb, &size);
2013-03-12 18:30:59 +04:00
if (stride * height > size) {
Py_INCREF(Py_None);
return Py_None;
}
2013-03-14 04:42:26 +04:00
ret_size = WebPEncodeRGB(rgb, width, height, stride, quality_factor, &output);
2013-03-12 18:30:59 +04:00
if (ret_size > 0) {
PyObject *ret = PyBytes_FromStringAndSize((char*)output, ret_size);
2013-03-12 18:30:59 +04:00
free(output);
return ret;
}
Py_INCREF(Py_None);
return Py_None;
}
PyObject* WebPDecodeRGB_wrapper(PyObject* self, PyObject* args)
{
2013-03-14 06:37:00 +04:00
PyBytesObject *webp_string;
2013-03-14 04:42:26 +04:00
int width;
int height;
uint8_t *webp;
uint8_t *output;
Py_ssize_t size;
PyObject *ret;
2013-03-12 18:30:59 +04:00
if (!PyArg_ParseTuple(args, "S", &webp_string)) {
Py_INCREF(Py_None);
return Py_None;
2013-03-14 04:42:26 +04:00
}
PyBytes_AsStringAndSize((PyObject *) webp_string, (char**)&webp, &size);
2013-03-12 18:30:59 +04:00
2013-03-14 04:42:26 +04:00
output = WebPDecodeRGB(webp, size, &width, &height);
2013-03-12 18:30:59 +04:00
ret = PyBytes_FromStringAndSize((char*)output, width * height * 3);
2013-03-12 18:30:59 +04:00
free(output);
return Py_BuildValue("Sii", ret, width, height);
}
static PyMethodDef webpMethods[] =
{
{"WebPEncodeRGB", WebPEncodeRGB_wrapper, METH_VARARGS, "WebPEncodeRGB"},
{"WebPDecodeRGB", WebPDecodeRGB_wrapper, METH_VARARGS, "WebPEncodeRGB"},
{NULL, NULL}
};
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);
return m;
}
#else
PyMODINIT_FUNC
init_webp(void)
2013-03-12 18:30:59 +04:00
{
Py_InitModule("_webp", webpMethods);
2013-03-12 18:30:59 +04:00
}
2013-03-14 06:37:00 +04:00
#endif