mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-14 21:56:56 +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)
|
||||
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")
|
||||
|
||||
data = _webp.WebPEncode(
|
||||
im.tobytes(),
|
||||
im.size[0],
|
||||
im.size[1],
|
||||
im.im.ptr,
|
||||
lossless,
|
||||
float(quality),
|
||||
float(alpha_quality),
|
||||
im.mode,
|
||||
icc_profile,
|
||||
method,
|
||||
exact,
|
||||
|
|
75
src/_webp.c
75
src/_webp.c
|
@ -566,32 +566,65 @@ static PyTypeObject WebPAnimDecoder_Type = {
|
|||
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 */
|
||||
/* -------------------------------------------------------------------- */
|
||||
|
||||
PyObject *
|
||||
WebPEncode_wrapper(PyObject *self, PyObject *args) {
|
||||
int width;
|
||||
int height;
|
||||
int lossless;
|
||||
float quality_factor;
|
||||
float alpha_quality_factor;
|
||||
int method;
|
||||
int exact;
|
||||
uint8_t *rgb;
|
||||
Imaging im;
|
||||
PyObject *i0;
|
||||
uint8_t *icc_bytes;
|
||||
uint8_t *exif_bytes;
|
||||
uint8_t *xmp_bytes;
|
||||
uint8_t *output;
|
||||
char *mode;
|
||||
Py_ssize_t size;
|
||||
Py_ssize_t icc_size;
|
||||
Py_ssize_t exif_size;
|
||||
Py_ssize_t xmp_size;
|
||||
size_t ret_size;
|
||||
int rgba_mode;
|
||||
int channels;
|
||||
int ok;
|
||||
ImagingSectionCookie cookie;
|
||||
WebPConfig config;
|
||||
|
@ -600,15 +633,11 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
|
|||
|
||||
if (!PyArg_ParseTuple(
|
||||
args,
|
||||
"y#iiiffss#iis#s#",
|
||||
(char **)&rgb,
|
||||
&size,
|
||||
&width,
|
||||
&height,
|
||||
"Oiffs#iis#s#",
|
||||
&i0,
|
||||
&lossless,
|
||||
&quality_factor,
|
||||
&alpha_quality_factor,
|
||||
&mode,
|
||||
&icc_bytes,
|
||||
&icc_size,
|
||||
&method,
|
||||
|
@ -621,15 +650,12 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
rgba_mode = strcmp(mode, "RGBA") == 0;
|
||||
if (!rgba_mode && strcmp(mode, "RGB") != 0) {
|
||||
Py_RETURN_NONE;
|
||||
if (!PyCapsule_IsValid(i0, IMAGING_MAGIC)) {
|
||||
PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
channels = rgba_mode ? 4 : 3;
|
||||
if (size < width * height * channels) {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
im = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
|
||||
|
||||
// Setup config for this frame
|
||||
if (!WebPConfigInit(&config)) {
|
||||
|
@ -652,14 +678,9 @@ WebPEncode_wrapper(PyObject *self, PyObject *args) {
|
|||
PyErr_SetString(PyExc_ValueError, "could not initialise picture");
|
||||
return NULL;
|
||||
}
|
||||
pic.width = width;
|
||||
pic.height = height;
|
||||
pic.use_argb = 1; // Don't convert RGB pixels to YUV
|
||||
|
||||
if (rgba_mode) {
|
||||
WebPPictureImportRGBA(&pic, rgb, channels * width);
|
||||
} else {
|
||||
WebPPictureImportRGB(&pic, rgb, channels * width);
|
||||
if (import_frame_libwebp(&pic, im)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
WebPMemoryWriterInit(&writer);
|
||||
|
|
Loading…
Reference in New Issue
Block a user