diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index a837cf77a51..ad7f841db25 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -183,7 +183,7 @@ def paste(self, im: Image.Image) -> None: image = im.im if not image.isblock() or im.mode != self.__mode: block = Image.core.new_block(self.__mode, im.size) - image.convert2(block) # convert directly between buffers + image.convert_into(block) # convert directly between buffers ptr = block.ptr _pyimagingtkcall("PyImagingPhoto", self.__photo, ptr) diff --git a/src/_imaging.c b/src/_imaging.c index 04b5da3f68c..4f559a7f724 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1023,18 +1023,22 @@ _convert(ImagingObject *self, PyObject *args) { const ModeID mode = findModeID(mode_name); return PyImagingNew(ImagingConvert( - self->image, mode, paletteimage ? paletteimage->image->palette : NULL, dither + NULL, + self->image, + mode, + paletteimage ? paletteimage->image->palette : NULL, + dither )); } static PyObject * -_convert2(ImagingObject *self, PyObject *args) { +_convert_into(ImagingObject *self, PyObject *args) { ImagingObject *imagep; if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { return NULL; } - if (!ImagingConvert2(imagep->image, self->image)) { + if (!ImagingConvert(imagep->image, self->image, imagep->image->mode, NULL, 0)) { return NULL; } @@ -3702,7 +3706,7 @@ static struct PyMethodDef methods[] = { /* Standard processing methods (Image) */ {"color_lut_3d", (PyCFunction)_color_lut_3d, METH_VARARGS}, {"convert", (PyCFunction)_convert, METH_VARARGS}, - {"convert2", (PyCFunction)_convert2, METH_VARARGS}, + {"convert_into", (PyCFunction)_convert_into, METH_VARARGS}, {"convert_matrix", (PyCFunction)_convert_matrix, METH_VARARGS}, {"convert_transparent", (PyCFunction)_convert_transparent, METH_VARARGS}, {"copy", (PyCFunction)_copy, METH_VARARGS}, diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 0f962a9fcae..cd5017ff358 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -1575,8 +1575,26 @@ static struct { {IMAGING_MODE_I_16B, IMAGING_MODE_F, I16B_F} }; -static Imaging -convert(Imaging imOut, Imaging imIn, ModeID mode, ImagingPalette palette, int dither) { +/** + * Convert imIn to `mode`. + * If imIn is already in `mode`, this performs a copy into imOut + * (or a newly allocated image if imOut is NULL). + * + * @param imOut Existing image to write into + * (must already be in `mode` and the same size as imIn), + * or NULL to allocate a new image for the result. + * @param imIn Source image to convert. + * @param mode Target mode. + * @param palette Target palette for conversions to "P" or "PA"; + * NULL to use a default palette. + * @param dither Nonzero to dither when converting to "P", "PA" or "1". + * @return The resulting Imaging object, + * or NULL with a Python exception set on failure. + */ +Imaging +ImagingConvert( + Imaging imOut, Imaging imIn, ModeID mode, ImagingPalette palette, int dither +) { ImagingSectionCookie cookie; ImagingShuffler convert; @@ -1644,16 +1662,6 @@ convert(Imaging imOut, Imaging imIn, ModeID mode, ImagingPalette palette, int di return imOut; } -Imaging -ImagingConvert(Imaging imIn, const ModeID mode, ImagingPalette palette, int dither) { - return convert(NULL, imIn, mode, palette, dither); -} - -Imaging -ImagingConvert2(Imaging imOut, Imaging imIn) { - return convert(imOut, imIn, imOut->mode, NULL, 0); -} - Imaging ImagingConvertTransparent(Imaging imIn, const ModeID mode, int r, int g, int b) { ImagingSectionCookie cookie; diff --git a/src/libImaging/Imaging.h b/src/libImaging/Imaging.h index 472bda5d0fd..72de8a894f7 100644 --- a/src/libImaging/Imaging.h +++ b/src/libImaging/Imaging.h @@ -303,7 +303,9 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha); extern Imaging ImagingCopy(Imaging im); extern Imaging -ImagingConvert(Imaging im, ModeID mode, ImagingPalette palette, int dither); +ImagingConvert( + Imaging imOut, Imaging imIn, ModeID mode, ImagingPalette palette, int dither +); extern Imaging ImagingConvertInPlace(Imaging im, ModeID mode); extern Imaging @@ -417,8 +419,6 @@ ImagingColorLUT3D_linear( extern Imaging ImagingCopy2(Imaging imOut, Imaging imIn); -extern Imaging -ImagingConvert2(Imaging imOut, Imaging imIn); /* Channel operations */ /* any mode, except "F" */