Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/PIL/ImageTk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 17 additions & 31 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
));
}

Expand All @@ -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 *
Expand Down Expand Up @@ -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},
Expand Down Expand Up @@ -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},

Expand Down
4 changes: 2 additions & 2 deletions src/libImaging/Bands.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/BoxBlur.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
73 changes: 38 additions & 35 deletions src/libImaging/Convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
Expand All @@ -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 */
Expand All @@ -1760,7 +1763,7 @@ ImagingConvert(
);
}

imOut = ImagingNew2Dirty(mode, imOut, imIn);
imOut = new_image(mode, imIn->xsize, imIn->ysize);
if (!imOut) {
return NULL;
}
Expand Down Expand Up @@ -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;
}
Expand Down
58 changes: 40 additions & 18 deletions src/libImaging/Copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,69 @@

#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);
MUTEX_LOCK(&imIn->mutex);
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;
}
Loading
Loading