mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-15 06:07:33 +03:00
call _webp.WebPEncode with ptr
This commit is contained in:
parent
0a8e6dbedb
commit
8bb3134b1d
|
@ -295,17 +295,14 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
method = im.encoderinfo.get("method", 4)
|
method = im.encoderinfo.get("method", 4)
|
||||||
exact = 1 if im.encoderinfo.get("exact") else 0
|
exact = 1 if im.encoderinfo.get("exact") else 0
|
||||||
|
|
||||||
if im.mode not in ("RGB", "RGBA"):
|
if im.mode not in ("RGBX", "RGBA", "RGB"):
|
||||||
im = im.convert("RGBA" if im.has_transparency_data else "RGB")
|
im = im.convert("RGBA" if im.has_transparency_data else "RGB")
|
||||||
|
|
||||||
data = _webp.WebPEncode(
|
data = _webp.WebPEncode(
|
||||||
im.tobytes(),
|
im.im.ptr,
|
||||||
im.size[0],
|
|
||||||
im.size[1],
|
|
||||||
lossless,
|
lossless,
|
||||||
float(quality),
|
float(quality),
|
||||||
float(alpha_quality),
|
float(alpha_quality),
|
||||||
im.mode,
|
|
||||||
icc_profile,
|
icc_profile,
|
||||||
method,
|
method,
|
||||||
exact,
|
exact,
|
||||||
|
|
75
src/_webp.c
75
src/_webp.c
|
@ -566,32 +566,65 @@ static PyTypeObject WebPAnimDecoder_Type = {
|
||||||
0, /*tp_getset*/
|
0, /*tp_getset*/
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* -------------------------------------------------------------------- */
|
||||||
|
/* Frame import */
|
||||||
|
/* -------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
static int
|
||||||
|
import_frame_libwebp(WebPPicture *frame, Imaging im) {
|
||||||
|
UINT32 mask = 0;
|
||||||
|
|
||||||
|
if (strcmp(im->mode, "RGBA") && strcmp(im->mode, "RGB") && strcmp(im->mode, "RGBX")) {
|
||||||
|
PyErr_SetString(PyExc_ValueError, "unsupported image mode");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strcmp(im->mode, "RGBA")) {
|
||||||
|
mask = MASK_UINT32_CHANNEL_3;
|
||||||
|
}
|
||||||
|
|
||||||
|
frame->width = im->xsize;
|
||||||
|
frame->height = im->ysize;
|
||||||
|
frame->use_argb = 1; // Don't convert RGB pixels to YUV
|
||||||
|
|
||||||
|
if (!WebPPictureAlloc(frame)) {
|
||||||
|
PyErr_SetString(PyExc_MemoryError, "can't allocate picture frame");
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int y = 0; y < im->ysize; ++y) {
|
||||||
|
UINT8 *src = (UINT8 *)im->image32[y];
|
||||||
|
UINT32 *dst = frame->argb + frame->argb_stride * y;
|
||||||
|
for (int x = 0; x < im->xsize; ++x) {
|
||||||
|
UINT32 pix = MAKE_UINT32(src[x * 4 + 2], src[x * 4 + 1], src[x * 4], src[x * 4 + 3]);
|
||||||
|
dst[x] = pix | mask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* -------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------- */
|
||||||
/* Legacy WebP Support */
|
/* Legacy WebP Support */
|
||||||
/* -------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------- */
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
WebPEncode_wrapper(PyObject *self, PyObject *args) {
|
WebPEncode_wrapper(PyObject *self, PyObject *args) {
|
||||||
int width;
|
|
||||||
int height;
|
|
||||||
int lossless;
|
int lossless;
|
||||||
float quality_factor;
|
float quality_factor;
|
||||||
float alpha_quality_factor;
|
float alpha_quality_factor;
|
||||||
int method;
|
int method;
|
||||||
int exact;
|
int exact;
|
||||||
uint8_t *rgb;
|
Imaging im;
|
||||||
|
PyObject *i0;
|
||||||
uint8_t *icc_bytes;
|
uint8_t *icc_bytes;
|
||||||
uint8_t *exif_bytes;
|
uint8_t *exif_bytes;
|
||||||
uint8_t *xmp_bytes;
|
uint8_t *xmp_bytes;
|
||||||
uint8_t *output;
|
uint8_t *output;
|
||||||
char *mode;
|
|
||||||
Py_ssize_t size;
|
|
||||||
Py_ssize_t icc_size;
|
Py_ssize_t icc_size;
|
||||||
Py_ssize_t exif_size;
|
Py_ssize_t exif_size;
|
||||||
Py_ssize_t xmp_size;
|
Py_ssize_t xmp_size;
|
||||||
size_t ret_size;
|
size_t ret_size;
|
||||||
int rgba_mode;
|
|
||||||
int channels;
|
|
||||||
int ok;
|
int ok;
|
||||||
ImagingSectionCookie cookie;
|
ImagingSectionCookie cookie;
|
||||||
WebPConfig config;
|
WebPConfig config;
|
||||||
|
@ -600,15 +633,11 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(
|
if (!PyArg_ParseTuple(
|
||||||
args,
|
args,
|
||||||
"y#iiiffss#iis#s#",
|
"Oiffs#iis#s#",
|
||||||
(char **)&rgb,
|
&i0,
|
||||||
&size,
|
|
||||||
&width,
|
|
||||||
&height,
|
|
||||||
&lossless,
|
&lossless,
|
||||||
&quality_factor,
|
&quality_factor,
|
||||||
&alpha_quality_factor,
|
&alpha_quality_factor,
|
||||||
&mode,
|
|
||||||
&icc_bytes,
|
&icc_bytes,
|
||||||
&icc_size,
|
&icc_size,
|
||||||
&method,
|
&method,
|
||||||
|
@ -621,15 +650,12 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
rgba_mode = strcmp(mode, "RGBA") == 0;
|
if (!PyCapsule_IsValid(i0, IMAGING_MAGIC)) {
|
||||||
if (!rgba_mode && strcmp(mode, "RGB") != 0) {
|
PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);
|
||||||
Py_RETURN_NONE;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
channels = rgba_mode ? 4 : 3;
|
im = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
|
||||||
if (size < width * height * channels) {
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup config for this frame
|
// Setup config for this frame
|
||||||
if (!WebPConfigInit(&config)) {
|
if (!WebPConfigInit(&config)) {
|
||||||
|
@ -652,14 +678,9 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
|
||||||
PyErr_SetString(PyExc_ValueError, "could not initialise picture");
|
PyErr_SetString(PyExc_ValueError, "could not initialise picture");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
pic.width = width;
|
|
||||||
pic.height = height;
|
|
||||||
pic.use_argb = 1; // Don't convert RGB pixels to YUV
|
|
||||||
|
|
||||||
if (rgba_mode) {
|
if (import_frame_libwebp(&pic, im)) {
|
||||||
WebPPictureImportRGBA(&pic, rgb, channels * width);
|
return NULL;
|
||||||
} else {
|
|
||||||
WebPPictureImportRGB(&pic, rgb, channels * width);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WebPMemoryWriterInit(&writer);
|
WebPMemoryWriterInit(&writer);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user