~ubuntu-branches/ubuntu/saucy/python-imaging/saucy-proposed

« back to all changes in this revision

Viewing changes to _webp.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-03-20 16:44:01 UTC
  • mfrom: (2.1.13 experimental)
  • Revision ID: package-import@ubuntu.com-20130320164401-ptf6m0ttg4zw72az
Tags: 1.1.7+2.0.0-1
Pillow 2.0.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <Python.h>
 
2
#include "py3.h"
 
3
#include <webp/encode.h>
 
4
#include <webp/decode.h>
 
5
 
 
6
PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args)
 
7
{
 
8
    PyBytesObject *rgb_string;
 
9
    int width;
 
10
    int height;
 
11
    int stride;
 
12
    float quality_factor;
 
13
    uint8_t *rgb;
 
14
    uint8_t *output;
 
15
    Py_ssize_t size;
 
16
    size_t ret_size;
 
17
 
 
18
    if (!PyArg_ParseTuple(args, "Siiif", &rgb_string, &width, &height, &stride, &quality_factor)) {
 
19
        Py_INCREF(Py_None);
 
20
        return Py_None;
 
21
    }
 
22
 
 
23
    PyBytes_AsStringAndSize((PyObject *) rgb_string, &rgb, &size);
 
24
 
 
25
    if (stride * height > size) {
 
26
        Py_INCREF(Py_None);
 
27
        return Py_None;
 
28
    }
 
29
 
 
30
    ret_size = WebPEncodeRGB(rgb, width, height, stride, quality_factor, &output);
 
31
    if (ret_size > 0) {
 
32
        PyObject *ret = PyBytes_FromStringAndSize(output, ret_size);
 
33
        free(output);
 
34
        return ret;
 
35
    }
 
36
    Py_INCREF(Py_None);
 
37
    return Py_None;
 
38
 
 
39
}
 
40
 
 
41
PyObject* WebPDecodeRGB_wrapper(PyObject* self, PyObject* args)
 
42
{
 
43
    PyBytesObject *webp_string;
 
44
    float quality_factor;
 
45
    int width;
 
46
    int height;
 
47
    uint8_t *webp;
 
48
    uint8_t *output;
 
49
    Py_ssize_t size;
 
50
    PyObject *ret;
 
51
 
 
52
    if (!PyArg_ParseTuple(args, "S", &webp_string)) {
 
53
        Py_INCREF(Py_None);
 
54
        return Py_None;
 
55
    }
 
56
 
 
57
    PyBytes_AsStringAndSize((PyObject *) webp_string, &webp, &size);
 
58
 
 
59
    output = WebPDecodeRGB(webp, size, &width, &height);
 
60
 
 
61
    ret = PyBytes_FromStringAndSize(output, width * height * 3);
 
62
    free(output);
 
63
    return Py_BuildValue("Sii", ret, width, height);
 
64
}
 
65
 
 
66
static PyMethodDef webpMethods[] =
 
67
{
 
68
    {"WebPEncodeRGB", WebPEncodeRGB_wrapper, METH_VARARGS, "WebPEncodeRGB"},
 
69
    {"WebPDecodeRGB", WebPDecodeRGB_wrapper, METH_VARARGS, "WebPEncodeRGB"},
 
70
    {NULL, NULL}
 
71
};
 
72
 
 
73
#if PY_VERSION_HEX >= 0x03000000
 
74
PyMODINIT_FUNC
 
75
PyInit__webp(void) {
 
76
    PyObject* m;
 
77
 
 
78
    static PyModuleDef module_def = {
 
79
        PyModuleDef_HEAD_INIT,
 
80
        "_webp",            /* m_name */
 
81
        NULL,               /* m_doc */
 
82
        -1,                 /* m_size */
 
83
        webpMethods,        /* m_methods */
 
84
    };
 
85
 
 
86
    m = PyModule_Create(&module_def);
 
87
    return m;
 
88
}
 
89
#else
 
90
PyMODINIT_FUNC
 
91
init_webp()
 
92
{
 
93
    PyObject* m;
 
94
    m = Py_InitModule("_webp", webpMethods);
 
95
}
 
96
#endif