mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-14 05:36:48 +03:00
consolidated to WebPEncode_wrapper
This commit is contained in:
parent
a235a69d0c
commit
11a0fb5f76
|
@ -5,8 +5,8 @@ import _webp
|
||||||
|
|
||||||
|
|
||||||
_VALID_WEBP_ENCODERS_BY_MODE = {
|
_VALID_WEBP_ENCODERS_BY_MODE = {
|
||||||
"RGB": _webp.WebPEncodeRGB,
|
"RGB": _webp.WebPEncode,
|
||||||
"RGBA": _webp.WebPEncodeRGBA,
|
"RGBA": _webp.WebPEncode,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,20 +50,17 @@ class WebPImageFile(ImageFile.ImageFile):
|
||||||
|
|
||||||
def _save(im, fp, filename):
|
def _save(im, fp, filename):
|
||||||
image_mode = im.mode
|
image_mode = im.mode
|
||||||
if image_mode not in _VALID_WEBP_ENCODERS_BY_MODE:
|
if im.mode not in _VALID_WEBP_ENCODERS_BY_MODE:
|
||||||
raise IOError("cannot write mode %s as WEBP" % image_mode)
|
raise IOError("cannot write mode %s as WEBP" % image_mode)
|
||||||
|
|
||||||
webp_encoder = _VALID_WEBP_ENCODERS_BY_MODE[image_mode]
|
|
||||||
|
|
||||||
stride = im.size[0] * _STRIDE_MULTIPLIERS_BY_MODE[image_mode]
|
|
||||||
quality = im.encoderinfo.get("quality", 80)
|
quality = im.encoderinfo.get("quality", 80)
|
||||||
|
|
||||||
data = webp_encoder(
|
data = _webp.WebPEncode(
|
||||||
im.tobytes(),
|
im.tobytes(),
|
||||||
im.size[0],
|
im.size[0],
|
||||||
im.size[1],
|
im.size[1],
|
||||||
stride,
|
|
||||||
float(quality),
|
float(quality),
|
||||||
|
im.mode
|
||||||
)
|
)
|
||||||
fp.write(data)
|
fp.write(data)
|
||||||
|
|
||||||
|
|
48
_webp.c
48
_webp.c
|
@ -5,61 +5,32 @@
|
||||||
#include <webp/types.h>
|
#include <webp/types.h>
|
||||||
|
|
||||||
|
|
||||||
PyObject* WebPEncodeRGB_wrapper(PyObject* self, PyObject* args)
|
PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args)
|
||||||
{
|
{
|
||||||
PyBytesObject *rgb_string;
|
PyBytesObject *rgb_string;
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
int stride;
|
|
||||||
float quality_factor;
|
float quality_factor;
|
||||||
uint8_t *rgb;
|
uint8_t *rgb;
|
||||||
uint8_t *output;
|
uint8_t *output;
|
||||||
|
char *mode;
|
||||||
Py_ssize_t size;
|
Py_ssize_t size;
|
||||||
size_t ret_size;
|
size_t ret_size;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "Siiif", &rgb_string, &width, &height, &stride, &quality_factor)) {
|
if (!PyArg_ParseTuple(args, "Siifs", &rgb_string, &width, &height, &quality_factor, &mode)) {
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyBytes_AsStringAndSize((PyObject *) rgb_string, (char**)&rgb, &size);
|
PyBytes_AsStringAndSize((PyObject *) rgb_string, (char**)&rgb, &size);
|
||||||
|
|
||||||
if (stride * height > size) {
|
if (strcmp(mode, "RGBA")==0){
|
||||||
|
ret_size = WebPEncodeRGBA(rgb, width, height, 4* width, quality_factor, &output);
|
||||||
|
} else if (strcmp(mode, "RGB")==0){
|
||||||
|
ret_size = WebPEncodeRGB(rgb, width, height, 3* width, quality_factor, &output);
|
||||||
|
} else {
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret_size = WebPEncodeRGB(rgb, width, height, stride, quality_factor, &output);
|
|
||||||
if (ret_size > 0) {
|
|
||||||
PyObject *ret = PyBytes_FromStringAndSize((char*)output, ret_size);
|
|
||||||
free(output);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
PyObject* WebPEncodeRGBA_wrapper(PyObject* self, PyObject* args)
|
|
||||||
{
|
|
||||||
PyBytesObject *rgba_string;
|
|
||||||
int width;
|
|
||||||
int height;
|
|
||||||
int stride;
|
|
||||||
float quality_factor;
|
|
||||||
uint8_t *rgba;
|
|
||||||
uint8_t *output;
|
|
||||||
Py_ssize_t size;
|
|
||||||
size_t ret_size;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "Siiif", &rgba_string, &width, &height, &stride, &quality_factor)) {
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
PyBytes_AsStringAndSize((PyObject *) rgba_string, (char**)&rgba, &size);
|
|
||||||
|
|
||||||
if (stride * height > size) {
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret_size = WebPEncodeRGBA(rgba, width, height, stride, quality_factor, &output);
|
|
||||||
if (ret_size > 0) {
|
if (ret_size > 0) {
|
||||||
PyObject *ret = PyBytes_FromStringAndSize((char*)output, ret_size);
|
PyObject *ret = PyBytes_FromStringAndSize((char*)output, ret_size);
|
||||||
free(output);
|
free(output);
|
||||||
|
@ -141,8 +112,7 @@ PyObject* WebPDecoderBuggyAlpha_wrapper(PyObject* self, PyObject* args){
|
||||||
|
|
||||||
static PyMethodDef webpMethods[] =
|
static PyMethodDef webpMethods[] =
|
||||||
{
|
{
|
||||||
{"WebPEncodeRGB", WebPEncodeRGB_wrapper, METH_VARARGS, "WebPEncodeRGB"},
|
{"WebPEncode", WebPEncode_wrapper, METH_VARARGS, "WebPEncode"},
|
||||||
{"WebPEncodeRGBA", WebPEncodeRGBA_wrapper, METH_VARARGS, "WebPEncodeRGBA"},
|
|
||||||
{"WebPDecode", WebPDecode_wrapper, METH_VARARGS, "WebPDecode"},
|
{"WebPDecode", WebPDecode_wrapper, METH_VARARGS, "WebPDecode"},
|
||||||
{"WebPDecoderVersion", WebPDecoderVersion_wrapper, METH_VARARGS, "WebPVersion"},
|
{"WebPDecoderVersion", WebPDecoderVersion_wrapper, METH_VARARGS, "WebPVersion"},
|
||||||
{"WebPDecoderBuggyAlpha", WebPDecoderBuggyAlpha_wrapper, METH_VARARGS, "WebPDecoderBuggyAlpha"},
|
{"WebPDecoderBuggyAlpha", WebPDecoderBuggyAlpha_wrapper, METH_VARARGS, "WebPDecoderBuggyAlpha"},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user