Merge pull request #111 from cgohlke/patch-8

Port WebPImagePlugin to Python 3
This commit is contained in:
Alex Clark ☺ 2013-03-14 03:54:28 -07:00
commit 3ecf2afa2d
3 changed files with 32 additions and 11 deletions

View File

@ -1,10 +1,10 @@
from PIL import Image from PIL import Image
from PIL import ImageFile from PIL import ImageFile
import StringIO from io import BytesIO
import _webp import _webp
def _accept(prefix): def _accept(prefix):
return prefix[:4] == "RIFF" and prefix[8:16] == "WEBPVP8 " return prefix[:4] == b"RIFF" and prefix[8:16] == b"WEBPVP8 "
class WebPImageFile(ImageFile.ImageFile): class WebPImageFile(ImageFile.ImageFile):
@ -15,7 +15,7 @@ class WebPImageFile(ImageFile.ImageFile):
self.mode = "RGB" self.mode = "RGB"
data, width, height = _webp.WebPDecodeRGB(self.fp.read()) data, width, height = _webp.WebPDecodeRGB(self.fp.read())
self.size = width, height self.size = width, height
self.fp = StringIO.StringIO(data) self.fp = BytesIO(data)
self.tile = [("raw", (0, 0) + self.size, 0, 'RGB')] self.tile = [("raw", (0, 0) + self.size, 0, 'RGB')]
def _save(im, fp, filename): def _save(im, fp, filename):

34
_webp.c
View File

@ -1,10 +1,11 @@
#include <Python.h> #include <Python.h>
#include "py3.h"
#include <webp/encode.h> #include <webp/encode.h>
#include <webp/decode.h> #include <webp/decode.h>
PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args) PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args)
{ {
PyStringObject *rgb_string; PyBytesObject *rgb_string;
int width; int width;
int height; int height;
int stride; int stride;
@ -19,7 +20,7 @@ PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args)
return Py_None; return Py_None;
} }
PyString_AsStringAndSize((struct PyObject *) rgb_string, &rgb, &size); PyBytes_AsStringAndSize((PyObject *) rgb_string, &rgb, &size);
if (stride * height > size) { if (stride * height > size) {
Py_INCREF(Py_None); Py_INCREF(Py_None);
@ -28,7 +29,7 @@ PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args)
ret_size = WebPEncodeRGB(rgb, width, height, stride, quality_factor, &output); ret_size = WebPEncodeRGB(rgb, width, height, stride, quality_factor, &output);
if (ret_size > 0) { if (ret_size > 0) {
PyObject *ret = PyString_FromStringAndSize(output, ret_size); PyObject *ret = PyBytes_FromStringAndSize(output, ret_size);
free(output); free(output);
return ret; return ret;
} }
@ -39,7 +40,7 @@ PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args)
PyObject* WebPDecodeRGB_wrapper(PyObject* self, PyObject* args) PyObject* WebPDecodeRGB_wrapper(PyObject* self, PyObject* args)
{ {
PyStringObject *webp_string; PyBytesObject *webp_string;
float quality_factor; float quality_factor;
int width; int width;
int height; int height;
@ -53,11 +54,11 @@ PyObject* WebPDecodeRGB_wrapper(PyObject* self, PyObject* args)
return Py_None; return Py_None;
} }
PyString_AsStringAndSize((struct PyObject *) webp_string, &webp, &size); PyBytes_AsStringAndSize((PyObject *) webp_string, &webp, &size);
output = WebPDecodeRGB(webp, size, &width, &height); output = WebPDecodeRGB(webp, size, &width, &height);
ret = PyString_FromStringAndSize(output, width * height * 3); ret = PyBytes_FromStringAndSize(output, width * height * 3);
free(output); free(output);
return Py_BuildValue("Sii", ret, width, height); return Py_BuildValue("Sii", ret, width, height);
} }
@ -69,8 +70,27 @@ static PyMethodDef webpMethods[] =
{NULL, NULL} {NULL, NULL}
}; };
void init_webp() #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()
{ {
PyObject* m; PyObject* m;
m = Py_InitModule("_webp", webpMethods); m = Py_InitModule("_webp", webpMethods);
} }
#endif

3
py3.h
View File

@ -43,12 +43,13 @@
#endif #endif
/* Map PyBytes -> PyString */ /* Map PyBytes -> PyString */
#define PyBytesObject PyStringObject
#define PyBytes_AsString PyString_AsString #define PyBytes_AsString PyString_AsString
#define PyBytes_AS_STRING PyString_AS_STRING #define PyBytes_AS_STRING PyString_AS_STRING
#define PyBytes_Check PyString_Check #define PyBytes_Check PyString_Check
#define PyBytes_AsStringAndSize PyString_AsStringAndSize
#define PyBytes_FromStringAndSize PyString_FromStringAndSize #define PyBytes_FromStringAndSize PyString_FromStringAndSize
#define PyBytes_FromString PyString_FromString #define PyBytes_FromString PyString_FromString
#define _PyBytes_Resize _PyString_Resize #define _PyBytes_Resize _PyString_Resize
#endif /* PY_VERSION_HEX < 0x03000000 */ #endif /* PY_VERSION_HEX < 0x03000000 */