Skip to content

Remove unused ImagingConvertInPlace - #9878

Open
radarhere wants to merge 3 commits into
python-pillow:mainfrom
radarhere:convert
Open

Remove unused ImagingConvertInPlace#9878
radarhere wants to merge 3 commits into
python-pillow:mainfrom
radarhere:convert

Conversation

@radarhere

@radarhere radarhere commented Aug 19, 2026

Copy link
Copy Markdown
Member

im.putalpha() is the only place where C's setmode() is called.

Pillow/src/PIL/Image.py

Lines 2050 to 2052 in 3220aca

mode = getmodebase(self.mode) + "A"
try:
self.im.setmode(mode)

This is only trying to convert the image to LA, PA or RGBA.

However, the only operation setmode() can actually succeed at is converting RGB or RGBX to RGBA.

Pillow/src/_imaging.c

Lines 2079 to 2090 in 3220aca

if (im->mode == mode) {
; /* same mode; always succeeds */
} else if (isRGB(im->mode) && isRGB(mode)) {
/* color to color */
im->mode = mode;
im->bands = modelen;
if (mode == IMAGING_MODE_RGBA) {
(void)ImagingFillBand(im, 3, 255);
}
} else {
/* trying doing an in-place conversion */
if (!ImagingConvertInPlace(im, mode)) {

ImagingConvertInPlace(Imaging imIn, const ModeID mode) {
ImagingSectionCookie cookie;
ImagingShuffler convert;
int y;
/* limited support for inplace conversion */
if (imIn->mode == IMAGING_MODE_L && mode == IMAGING_MODE_1) {
convert = l2bit;
} else if (imIn->mode == IMAGING_MODE_1 && mode == IMAGING_MODE_L) {
convert = bit2l;

ImagingConvertInPlace() isn't able to convert anything to LA, PA or RGBA.

So this PR

  • removes ImagingConvertInPlace(). setmode() is the only place where ImagingConvertInPlace() is called.
  • simplifies setmode() to more clearly only convert RGB or RGBX images to RGBA. I've also renamed it to setalpha().

@akx akx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had been looking at removing ImagingConvertInPlace (in a bid to see if the shuffler functions could be implemented in a faster way) but didn't dare go through with it.

Some thoughts within though :)

Comment thread src/_imaging.c
static PyObject *
im_setmode(ImagingObject *self, PyObject *args) {
im_setalpha(ImagingObject *self, PyObject *args) {
/* attempt to modify the mode of an image in place */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is a little stale now?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it? The method is still modifying the mode of an image in place. It's more specific now, is all.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose...

Should this function know how to do P-to-PA and L-to-LA too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thought occurred to me. It is a bit different to RGB, since RGB already has the same pixelsize as RGBA.

Do you mind if that is a follow-up PR? I would rather tidy things up first.

Comment thread src/_imaging.c
Comment on lines -3742 to +3712
{"setmode", (PyCFunction)im_setmode, METH_VARARGS},
{"setalpha", (PyCFunction)im_setalpha, METH_NOARGS},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the deprecation policy for methods on the core image objects in general (is it documented)?

GitHub's Code Search isn't making it easy to find whether there are external users of im.im.setmode.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no deprecation period.

#4532 (comment)

Pillow makes a commitment to stable public interfaces, which are defined at the Python layer. The C interfaces are explicitly internal, and no effort is made to keep them stable even between minor releases, and no support or warning is given when they change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an useful comment, thanks! Should maybe codify it into development documentation if it isn't already (for instance, I feel much better about touching the palette code even harder in #9829).

And I'll understand this as "the Python-facing interfaces defined in C extension module(s) in Pillow are also malleable" (since this is exactly that). 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created #9879

Comment thread src/_imaging.c Outdated
im_setalpha(ImagingObject *self, PyObject *args) {
/* attempt to modify the mode of an image in place */
Imaging im = self->image;
if (im->mode == IMAGING_MODE_RGB || im->mode == IMAGING_MODE_RGBX) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this function raise a mode error instead of quietly doing nothing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a commit for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants