Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved conversion from RGB to RGBa, LA and La #7888

Merged
merged 3 commits into from
Mar 28, 2024
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
12 changes: 12 additions & 0 deletions Tests/test_image_convert.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,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
Original file line number Diff line number Diff line change
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
Loading