diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index f2dd618cd01..2be54fb1297 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -184,8 +184,7 @@ def paste(self, im: Image.Image) -> None: ptr = im.getim() image = im.im if not image.isblock() or im.mode != self.__mode: - block = Image.core.new_block(self.__mode, im.size) - image.convert_into(block) # convert directly between buffers + block = image.convert_block(self.__mode) # contiguous, for Tk ptr = block.ptr _pyimagingtkcall("PyImagingPhoto", self.__photo, ptr) diff --git a/src/_imaging.c b/src/_imaging.c index 449f94ae005..d2744786147 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -784,20 +784,6 @@ _new(PyObject *self, PyObject *args) { return PyImagingNew(ImagingNew(mode, xsize, ysize)); } -static PyObject * -_new_block(PyObject *self, PyObject *args) { - char *mode_name; - int xsize, ysize; - - if (!PyArg_ParseTuple(args, "s(ii)", &mode_name, &xsize, &ysize)) { - return NULL; - } - - const ModeID mode = findModeID(mode_name); - - return PyImagingNew(ImagingNewBlock(mode, xsize, ysize)); -} - static PyObject * _linear_gradient(PyObject *self, PyObject *args) { char *mode_name; @@ -1077,12 +1063,12 @@ _convert_impl(ImagingObject *self, PyObject *args) { const ModeID mode = findModeID(mode_name); - return PyImagingNew(ImagingConvert( - NULL, + return PyImagingNew(ImagingConvertUsingAllocator( self->image, mode, paletteimage ? paletteimage->image->palette : NULL, - dither + dither, + ImagingNewDirty )); } @@ -1096,24 +1082,25 @@ _convert(ImagingObject *self, PyObject *args) { } static PyObject * -_convert_into(ImagingObject *self, PyObject *args) { - ImagingObject *imagep; +_convert_block(ImagingObject *self, PyObject *args) { + char *mode_name; Imaging result; - if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep)) { + if (!PyArg_ParseTuple(args, "s", &mode_name)) { return NULL; } - Py_BEGIN_CRITICAL_SECTION2(self, imagep); - result = ImagingConvert( - imagep->image, self->image, imagep->image->mode, NULL, IMAGING_DITHER_NONE - ); - Py_END_CRITICAL_SECTION2(); - - if (!result) { - return NULL; + const ModeID mode = findModeID(mode_name); + if (mode == IMAGING_MODE_UNKNOWN) { + return ImagingError_ValueError("unrecognized image mode"); } - Py_RETURN_NONE; + Py_BEGIN_CRITICAL_SECTION(self); + result = ImagingConvertUsingAllocator( + self->image, mode, NULL, IMAGING_DITHER_NONE, ImagingNewBlock + ); + Py_END_CRITICAL_SECTION(); + + return PyImagingNew(result); } static PyObject * @@ -4319,7 +4306,7 @@ static struct PyMethodDef methods[] = { /* Standard processing methods (Image) */ {"color_lut_3d", (PyCFunction)_color_lut_3d, METH_VARARGS}, {"convert", (PyCFunction)_convert, METH_VARARGS}, - {"convert_into", (PyCFunction)_convert_into, METH_VARARGS}, + {"convert_block", (PyCFunction)_convert_block, METH_VARARGS}, {"convert_matrix", (PyCFunction)_convert_matrix, METH_VARARGS}, {"convert_transparent", (PyCFunction)_convert_transparent, METH_VARARGS}, {"copy", (PyCFunction)_copy, METH_VARARGS}, @@ -5040,7 +5027,6 @@ static PyMethodDef functions[] = { {"blend", (PyCFunction)_blend, METH_VARARGS}, {"fill", (PyCFunction)_fill, METH_VARARGS}, {"new", (PyCFunction)_new, METH_VARARGS}, - {"new_block", (PyCFunction)_new_block, METH_VARARGS}, {"new_arrow", (PyCFunction)_new_arrow, METH_VARARGS}, {"merge", (PyCFunction)_merge, METH_VARARGS}, diff --git a/src/libImaging/Bands.c b/src/libImaging/Bands.c index ff23475c953..101118fc7a5 100644 --- a/src/libImaging/Bands.c +++ b/src/libImaging/Bands.c @@ -218,7 +218,7 @@ ImagingPutBand(Imaging imOut, Imaging imIn, int band) { /* Shortcuts */ if (imOut->bands == 1) { - return ImagingCopy2(imOut, imIn); + return ImagingCopyInto(imOut, imIn); } /* Special case for LXXA etc */ @@ -324,7 +324,7 @@ ImagingMerge(const ModeID mode, Imaging bands[4]) { } if (bandsCount == 1) { - if (!ImagingCopy2(imOut, firstBand)) { + if (!ImagingCopyInto(imOut, firstBand)) { ImagingDelete(imOut); return NULL; } diff --git a/src/libImaging/BoxBlur.c b/src/libImaging/BoxBlur.c index be011ca41af..92c612f6a3c 100644 --- a/src/libImaging/BoxBlur.c +++ b/src/libImaging/BoxBlur.c @@ -387,7 +387,7 @@ ImagingBoxBlur(Imaging imOut, Imaging imIn, float xradius, float yradius, int n) ImagingDelete(imTransposed); } if (xradius == 0 && yradius == 0) { - if (!ImagingCopy2(imOut, imIn)) { + if (!ImagingCopyInto(imOut, imIn)) { return NULL; } } diff --git a/src/libImaging/Convert.c b/src/libImaging/Convert.c index 6ff657cf55e..44de534a144 100644 --- a/src/libImaging/Convert.c +++ b/src/libImaging/Convert.c @@ -1109,14 +1109,13 @@ pa2ycbcr(UINT8 *out, const UINT8 *in, int xsize, ImagingPalette palette) { /** * Map P or PA image to another mode. * - * @param imOut The output image. - * Must be NULL or have the same size as imIn and the same mode as `mode`. * @param imIn The input palette image. * @param mode The desired output mode. - * @return imOut, or NULL on error. + * @param new_image Allocator for the output image. + * @return A new image owned by the caller, or NULL on error. */ static Imaging -frompalette(Imaging imOut, Imaging imIn, const ModeID mode) { +frompalette(Imaging imIn, const ModeID mode, ImagingAllocator new_image) { ImagingSectionCookie cookie; int alpha; int y; @@ -1156,7 +1155,7 @@ frompalette(Imaging imOut, Imaging imIn, const ModeID mode) { return (Imaging)ImagingError_ValueError("conversion not supported"); } - imOut = ImagingNew2Dirty(mode, imOut, imIn); + Imaging imOut = new_image(mode, imIn->xsize, imIn->ysize); if (!imOut) { return NULL; } @@ -1184,11 +1183,11 @@ frompalette(Imaging imOut, Imaging imIn, const ModeID mode) { #endif static Imaging topalette( - Imaging imOut, Imaging imIn, const ModeID mode, ImagingPalette inpalette, - ImagingDitherType dither + ImagingDitherType dither, + ImagingAllocator new_image ) { ImagingSectionCookie cookie; int alpha; @@ -1228,7 +1227,7 @@ topalette( return (Imaging)ImagingError_ValueError("no palette"); } - imOut = ImagingNew2Dirty(mode, imOut, imIn); + Imaging imOut = new_image(mode, imIn->xsize, imIn->ysize); if (!imOut) { if (palette != inpalette) { ImagingPaletteDelete(palette); @@ -1409,7 +1408,7 @@ topalette( * Map L or RGB to dithered 1 image */ static Imaging -tobilevel(Imaging imOut, Imaging imIn) { +tobilevel(Imaging imIn, ImagingAllocator new_image) { ImagingSectionCookie cookie; int x, y; int *errors; @@ -1418,7 +1417,7 @@ tobilevel(Imaging imOut, Imaging imIn) { return (Imaging)ImagingError_ValueError("conversion not supported"); } - imOut = ImagingNew2Dirty(IMAGING_MODE_1, imOut, imIn); + Imaging imOut = new_image(IMAGING_MODE_1, imIn->xsize, imIn->ysize); if (!imOut) { return NULL; } @@ -1681,34 +1680,33 @@ static struct { }; /** - * 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). + * Convert imIn to `mode`, returning a new image. + * If imIn is already in `mode`, this returns a copy. * - * @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 Dither method when converting to "P", "PA" or "1". - * Currently, for historical PIL reasons, - * any value other than IMAGING_DITHER_NONE uses Floyd-Steinberg - * dithering. - * @return The resulting Imaging object, - * or NULL with a Python exception set on failure. + * @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 Dither method when converting to "P", "PA" or "1". + * Currently, for historical PIL reasons, + * any value other than IMAGING_DITHER_NONE uses Floyd-Steinberg + * dithering. + * @param new_image Allocator used to create the output image, + * e.g. ImagingNewDirty or ImagingNewBlock. + * @return A new image owned by the caller, + * or NULL with a Python exception set on failure. */ Imaging -ImagingConvert( - Imaging imOut, +ImagingConvertUsingAllocator( Imaging imIn, ModeID mode, ImagingPalette palette, - ImagingDitherType dither + ImagingDitherType dither, + ImagingAllocator new_image ) { ImagingSectionCookie cookie; ImagingShuffler convert; + Imaging imOut; if (!imIn) { return (Imaging)ImagingError_ModeError(); @@ -1723,22 +1721,27 @@ ImagingConvert( } else { /* Same mode? */ if (imIn->mode == mode) { - return ImagingCopy2(imOut, imIn); + imOut = new_image(mode, imIn->xsize, imIn->ysize); + if (imOut && !ImagingCopyInto(imOut, imIn)) { + ImagingDelete(imOut); + return NULL; + } + return imOut; } } /* test for special conversions */ if (imIn->mode == IMAGING_MODE_P || imIn->mode == IMAGING_MODE_PA) { - return frompalette(imOut, imIn, mode); + return frompalette(imIn, mode, new_image); } if (mode == IMAGING_MODE_P || mode == IMAGING_MODE_PA) { - return topalette(imOut, imIn, mode, palette, dither); + return topalette(imIn, mode, palette, dither, new_image); } if (dither != IMAGING_DITHER_NONE && mode == IMAGING_MODE_1) { - return tobilevel(imOut, imIn); + return tobilevel(imIn, new_image); } /* standard conversion machinery */ @@ -1760,7 +1763,7 @@ ImagingConvert( ); } - imOut = ImagingNew2Dirty(mode, imOut, imIn); + imOut = new_image(mode, imIn->xsize, imIn->ysize); if (!imOut) { return NULL; } @@ -1830,7 +1833,7 @@ ImagingConvertTransparent(Imaging imIn, const ModeID mode, int r, int g, int b) ); } - imOut = ImagingNew2Dirty(mode, imOut, imIn); + imOut = ImagingNewDirty(mode, imIn->xsize, imIn->ysize); if (!imOut) { return NULL; } diff --git a/src/libImaging/Copy.c b/src/libImaging/Copy.c index cd4523ea20a..f37ca588de9 100644 --- a/src/libImaging/Copy.c +++ b/src/libImaging/Copy.c @@ -17,24 +17,21 @@ #include "Imaging.h" -static Imaging +/** + * Copy imIn into imOut. + * This internal function requires that imOut matches imIn in mode and size. + * + * @param imOut Caller-owned image to copy into. + * @param imIn Image to copy from. + */ +static void _copy(Imaging imOut, Imaging imIn) { ImagingSectionCookie cookie; - int y; - - if (!imIn) { - return (Imaging)ImagingError_ValueError(NULL); - } - - imOut = ImagingNew2Dirty(imIn->mode, imOut, imIn); - if (!imOut) { - return NULL; - } ImagingCopyPalette(imOut, imIn); if (!imIn->linesize || !imIn->ysize) { - return imOut; + return; } ImagingSectionEnter(&cookie); @@ -42,22 +39,47 @@ _copy(Imaging imOut, Imaging imIn) { if (imIn->block != NULL && imOut->block != NULL) { memcpy(imOut->block, imIn->block, (size_t)imIn->ysize * imIn->linesize); } else { - for (y = 0; y < imIn->ysize; y++) { + for (int y = 0; y < imIn->ysize; y++) { memcpy(imOut->image[y], imIn->image[y], imIn->linesize); } } MUTEX_UNLOCK(&imIn->mutex); ImagingSectionLeave(&cookie); - - return imOut; } +/** + * Create a new image by copying imIn. The caller owns the returned image. + * + * The new image is allocated with the current default allocation strategy. + * + * @param imIn Image to copy from. + * @return A new image on success, or NULL on error with a Python exception set. + */ Imaging ImagingCopy(Imaging imIn) { - return _copy(NULL, imIn); + Imaging imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize); + if (!imOut) { + return NULL; + } + + _copy(imOut, imIn); + return imOut; } +/** + * Copy imIn into the caller-owned imOut, which must match it in mode and size. + * + * @param imOut Caller-owned image to copy into. + * @param imIn Image to copy from. + * @return imOut on success, or NULL on error with a Python exception set. + */ Imaging -ImagingCopy2(Imaging imOut, Imaging imIn) { - return _copy(imOut, imIn); +ImagingCopyInto(Imaging imOut, Imaging imIn) { + if (imOut->mode != imIn->mode || imOut->xsize != imIn->xsize || + imOut->ysize != imIn->ysize) { + return ImagingError_Mismatch(); + } + + _copy(imOut, imIn); + return imOut; } diff --git a/src/libImaging/Imaging.h b/src/libImaging/Imaging.h index 1d053828b5d..2a2dfa86437 100644 --- a/src/libImaging/Imaging.h +++ b/src/libImaging/Imaging.h @@ -191,14 +191,14 @@ ImagingMemorySetBlockAllocator(ImagingMemoryArena arena, int use_block_allocator extern Imaging ImagingNew(ModeID mode, int xsize, int ysize); extern Imaging -ImagingNewDirty(ModeID mode, int xsize, int ysize); -extern Imaging -ImagingNew2Dirty(ModeID mode, Imaging imOut, Imaging imIn); +ImagingNewDirty(const ModeID mode, int xsize, int ysize); extern void ImagingDelete(Imaging im); extern Imaging -ImagingNewBlock(ModeID mode, int xsize, int ysize); +ImagingNewBlock(const ModeID mode, int xsize, int ysize); + +typedef Imaging (*ImagingAllocator)(const ModeID mode, int xsize, int ysize); extern Imaging ImagingNewArrow( @@ -322,12 +322,12 @@ ImagingBlend(Imaging imIn1, Imaging imIn2, float alpha); extern Imaging ImagingCopy(Imaging im); extern Imaging -ImagingConvert( - Imaging imOut, +ImagingConvertUsingAllocator( Imaging imIn, ModeID mode, ImagingPalette palette, - ImagingDitherType dither + ImagingDitherType dither, + ImagingAllocator new_image ); extern Imaging ImagingConvertMatrix(Imaging im, ModeID mode, const float m[12]); @@ -443,7 +443,7 @@ ImagingColorLUT3D_linear( ); extern Imaging -ImagingCopy2(Imaging imOut, Imaging imIn); +ImagingCopyInto(Imaging imOut, Imaging imIn); /* Channel operations */ /* any mode, except "F" */ diff --git a/src/libImaging/Storage.c b/src/libImaging/Storage.c index 52237314da4..ee3237c1269 100644 --- a/src/libImaging/Storage.c +++ b/src/libImaging/Storage.c @@ -773,27 +773,6 @@ ImagingNewArrow( ); } -Imaging -ImagingNew2Dirty(const ModeID mode, Imaging imOut, Imaging imIn) { - /* allocate or validate output image */ - - if (imOut) { - /* make sure images match */ - if (imOut->mode != mode || imOut->xsize != imIn->xsize || - imOut->ysize != imIn->ysize) { - return ImagingError_Mismatch(); - } - } else { - /* create new image */ - imOut = ImagingNewDirty(mode, imIn->xsize, imIn->ysize); - if (!imOut) { - return NULL; - } - } - - return imOut; -} - void ImagingCopyPalette(Imaging destination, Imaging source) { if (source->palette) {