Skip to content

Commit

Permalink
Merge pull request #7888 from radarhere/convert_rgb
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 28, 2024
2 parents 6464d5c + e79d174 commit d734c8b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
12 changes: 12 additions & 0 deletions Tests/test_image_convert.py
Expand Up @@ -183,6 +183,14 @@ def test_trns_RGB(tmp_path: Path) -> None:
assert im_l.info["transparency"] == im_l.getpixel((0, 0)) # undone
im_l.save(f)

im_la = im.convert("LA")
assert "transparency" not in im_la.info
im_la.save(f)

im_la = im.convert("La")
assert "transparency" not in im_la.info
assert im_la.getpixel((0, 0)) == (0, 0)

im_p = im.convert("P")
assert "transparency" in im_p.info
im_p.save(f)
Expand All @@ -191,6 +199,10 @@ def test_trns_RGB(tmp_path: Path) -> None:
assert "transparency" not in im_rgba.info
im_rgba.save(f)

im_rgba = im.convert("RGBa")
assert "transparency" not in im_rgba.info
assert im_rgba.getpixel((0, 0)) == (0, 0, 0, 0)

im_p = pytest.warns(UserWarning, im.convert, "P", palette=Image.Palette.ADAPTIVE)
assert "transparency" not in im_p.info
im_p.save(f)
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Expand Up @@ -978,7 +978,7 @@ def convert_transparency(m, v):
# transparency handling
if has_transparency:
if (self.mode in ("1", "L", "I", "I;16") and mode in ("LA", "RGBA")) or (
self.mode == "RGB" and mode == "RGBA"
self.mode == "RGB" and mode in ("La", "LA", "RGBa", "RGBA")
):
# Use transparent conversion to promote from transparent
# color to an alpha channel.
Expand Down
38 changes: 28 additions & 10 deletions src/libImaging/Convert.c
Expand Up @@ -499,26 +499,27 @@ rgba2rgb_(UINT8 *out, const UINT8 *in, int xsize) {
}

/*
* Conversion of RGB + single transparent color to RGBA,
* where any pixel that matches the color will have the
* alpha channel set to 0
* Conversion of RGB + single transparent color either to
* RGBA or LA, where any pixel matching the color will have the alpha channel set to 0, or
* RGBa or La, where any pixel matching the color will have all channels set to 0
*/

static void
rgbT2rgba(UINT8 *out, int xsize, int r, int g, int b) {
rgbT2a(UINT8 *out, UINT8 *in, int xsize, int r, int g, int b, int premultiplied) {
#ifdef WORDS_BIGENDIAN
UINT32 trns = ((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | 0xff;
UINT32 repl = trns & 0xffffff00;
UINT32 repl = premultiplied ? 0 : (trns & 0xffffff00);
#else
UINT32 trns = (0xffU << 24) | ((b & 0xff) << 16) | ((g & 0xff) << 8) | (r & 0xff);
UINT32 repl = trns & 0x00ffffff;
UINT32 repl = premultiplied ? 0 : (trns & 0x00ffffff);
#endif

int i;

for (i = 0; i < xsize; i++, out += sizeof(trns)) {
UINT8 *ref = in != NULL ? in : out;
for (i = 0; i < xsize; i++, ref += sizeof(trns), out += sizeof(trns)) {
UINT32 v;
memcpy(&v, out, sizeof(v));
memcpy(&v, ref, sizeof(v));
if (v == trns) {
memcpy(out, &repl, sizeof(repl));
}
Expand Down Expand Up @@ -941,12 +942,14 @@ static struct {
{"RGB", "1", rgb2bit},
{"RGB", "L", rgb2l},
{"RGB", "LA", rgb2la},
{"RGB", "La", rgb2la},
{"RGB", "I", rgb2i},
{"RGB", "F", rgb2f},
{"RGB", "BGR;15", rgb2bgr15},
{"RGB", "BGR;16", rgb2bgr16},
{"RGB", "BGR;24", rgb2bgr24},
{"RGB", "RGBA", rgb2rgba},
{"RGB", "RGBa", rgb2rgba},
{"RGB", "RGBX", rgb2rgba},
{"RGB", "CMYK", rgb2cmyk},
{"RGB", "YCbCr", ImagingConvertRGB2YCbCr},
Expand Down Expand Up @@ -1681,14 +1684,27 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) {
ImagingSectionCookie cookie;
ImagingShuffler convert;
Imaging imOut = NULL;
int premultiplied = 0;
// If the transparency matches pixels in the source image, not the converted image
UINT8 *source;
int source_transparency = 0;
int y;

if (!imIn) {
return (Imaging)ImagingError_ModeError();
}

if (strcmp(imIn->mode, "RGB") == 0 && strcmp(mode, "RGBA") == 0) {
if (strcmp(imIn->mode, "RGB") == 0 && (strcmp(mode, "RGBA") == 0 || strcmp(mode, "RGBa") == 0)) {
convert = rgb2rgba;
if (strcmp(mode, "RGBa") == 0) {
premultiplied = 1;
}
} else if (strcmp(imIn->mode, "RGB") == 0 && (strcmp(mode, "LA") == 0 || strcmp(mode, "La") == 0)) {
convert = rgb2la;
source_transparency = 1;
if (strcmp(mode, "La") == 0) {
premultiplied = 1;
}
} else if ((strcmp(imIn->mode, "1") == 0 ||
strcmp(imIn->mode, "I") == 0 ||
strcmp(imIn->mode, "I;16") == 0 ||
Expand Down Expand Up @@ -1726,7 +1742,9 @@ ImagingConvertTransparent(Imaging imIn, const char *mode, int r, int g, int b) {
ImagingSectionEnter(&cookie);
for (y = 0; y < imIn->ysize; y++) {
(*convert)((UINT8 *)imOut->image[y], (UINT8 *)imIn->image[y], imIn->xsize);
rgbT2rgba((UINT8 *)imOut->image[y], imIn->xsize, r, g, b);

source = source_transparency ? (UINT8 *)imIn->image[y] : NULL;
rgbT2a((UINT8 *)imOut->image[y], source, imIn->xsize, r, g, b, premultiplied);
}
ImagingSectionLeave(&cookie);

Expand Down

0 comments on commit d734c8b

Please sign in to comment.