If an alpha channel is present, only use that to get bounding box

This commit is contained in:
Andrew Murray 2020-02-28 22:33:02 +11:00
parent 1c1ad65a96
commit b934b50867
2 changed files with 33 additions and 19 deletions

View File

@ -10,29 +10,32 @@ def test_sanity():
def test_bbox():
def check(im, fill_color):
assert im.getbbox() is None
im.paste(fill_color, (10, 25, 90, 75))
assert im.getbbox() == (10, 25, 90, 75)
im.paste(fill_color, (25, 10, 75, 90))
assert im.getbbox() == (10, 10, 90, 90)
im.paste(fill_color, (-10, -10, 110, 110))
assert im.getbbox() == (0, 0, 100, 100)
# 8-bit mode
im = Image.new("L", (100, 100), 0)
assert im.getbbox() is None
im.paste(255, (10, 25, 90, 75))
assert im.getbbox() == (10, 25, 90, 75)
im.paste(255, (25, 10, 75, 90))
assert im.getbbox() == (10, 10, 90, 90)
im.paste(255, (-10, -10, 110, 110))
assert im.getbbox() == (0, 0, 100, 100)
check(im, 255)
# 32-bit mode
im = Image.new("RGB", (100, 100), 0)
assert im.getbbox() is None
check(im, 255)
im.paste(255, (10, 25, 90, 75))
assert im.getbbox() == (10, 25, 90, 75)
for mode in ("RGBA", "RGBa"):
for color in ((0, 0, 0, 0), (127, 127, 127, 0), (255, 255, 255, 0)):
im = Image.new(mode, (100, 100), color)
check(im, (255, 255, 255, 255))
im.paste(255, (25, 10, 75, 90))
assert im.getbbox() == (10, 10, 90, 90)
im.paste(255, (-10, -10, 110, 110))
assert im.getbbox() == (0, 0, 100, 100)
for mode in ("La", "LA", "PA"):
for color in ((0, 0), (127, 0), (255, 0)):
im = Image.new(mode, (100, 100), color)
check(im, (255, 255))

View File

@ -55,8 +55,19 @@ ImagingGetBBox(Imaging im, int bbox[4])
GETBBOX(image8, 0xff);
} else {
INT32 mask = 0xffffffff;
if (im->bands == 3)
if (im->bands == 3) {
((UINT8*) &mask)[3] = 0;
} else if (strcmp(im->mode, "RGBa") == 0 ||
strcmp(im->mode, "RGBA") == 0 ||
strcmp(im->mode, "La") == 0 ||
strcmp(im->mode, "LA") == 0 ||
strcmp(im->mode, "PA") == 0) {
#ifdef WORDS_BIGENDIAN
mask = 0x000000ff;
#else
mask = 0xff000000;
#endif
}
GETBBOX(image32, mask);
}