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

Added conversion from RGBa to RGB #6708

Merged
merged 1 commit into from Dec 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions Tests/test_image_convert.py
Expand Up @@ -104,6 +104,13 @@ def test_rgba_p():
assert_image_similar(im, comparable, 20)


def test_rgba():
with Image.open("Tests/images/transparent.png") as im:
assert im.mode == "RGBA"

assert_image_similar(im.convert("RGBa").convert("RGB"), im.convert("RGB"), 1.5)


def test_trns_p(tmp_path):
im = hopper("P")
im.info["transparency"] = 0
Expand Down
20 changes: 20 additions & 0 deletions src/libImaging/Convert.c
Expand Up @@ -479,6 +479,25 @@ rgba2rgbA(UINT8 *out, const UINT8 *in, int xsize) {
}
}

static void
rgba2rgb_(UINT8 *out, const UINT8 *in, int xsize) {
int x;
unsigned int alpha;
for (x = 0; x < xsize; x++, in += 4) {
alpha = in[3];
if (alpha == 255 || alpha == 0) {
*out++ = in[0];
*out++ = in[1];
*out++ = in[2];
} else {
*out++ = CLIP8((255 * in[0]) / alpha);
*out++ = CLIP8((255 * in[1]) / alpha);
*out++ = CLIP8((255 * in[2]) / alpha);
}
*out++ = 255;
}
}

/*
* Conversion of RGB + single transparent color to RGBA,
* where any pixel that matches the color will have the
Expand Down Expand Up @@ -934,6 +953,7 @@ static struct {
{"RGBA", "HSV", rgb2hsv},

{"RGBa", "RGBA", rgba2rgbA},
{"RGBa", "RGB", rgba2rgb_},

{"RGBX", "1", rgb2bit},
{"RGBX", "L", rgb2l},
Expand Down