Skip to content

Commit c6d8c58

Browse files
authored
Merge pull request #7965 from Yay295/patch-3
2 parents b48d175 + 6863c87 commit c6d8c58

File tree

5 files changed

+34
-10
lines changed

5 files changed

+34
-10
lines changed

Tests/images/p_16.png

36 Bytes
Loading

Tests/images/rgba16.tga

48 Bytes
Binary file not shown.

Tests/test_file_tga.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,21 @@ def test_palette_depth_8(tmp_path: Path) -> None:
7272

7373
def test_palette_depth_16(tmp_path: Path) -> None:
7474
with Image.open("Tests/images/p_16.tga") as im:
75-
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/p_16.png")
75+
assert im.palette.mode == "RGBA"
76+
assert_image_equal_tofile(im.convert("RGBA"), "Tests/images/p_16.png")
7677

7778
out = str(tmp_path / "temp.png")
7879
im.save(out)
7980
with Image.open(out) as reloaded:
80-
assert_image_equal_tofile(reloaded.convert("RGB"), "Tests/images/p_16.png")
81+
assert_image_equal_tofile(reloaded.convert("RGBA"), "Tests/images/p_16.png")
82+
83+
84+
def test_rgba_16() -> None:
85+
with Image.open("Tests/images/rgba16.tga") as im:
86+
assert im.mode == "RGBA"
87+
88+
assert im.getpixel((0, 0)) == (172, 0, 255, 255)
89+
assert im.getpixel((1, 0)) == (0, 255, 82, 0)
8190

8291

8392
def test_id_field() -> None:

src/PIL/TgaImagePlugin.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
(3, 1): "1",
3737
(3, 8): "L",
3838
(3, 16): "LA",
39-
(2, 16): "BGR;5",
39+
(2, 16): "BGRA;15Z",
4040
(2, 24): "BGR",
4141
(2, 32): "BGRA",
4242
}
@@ -87,9 +87,7 @@ def _open(self) -> None:
8787
elif imagetype in (1, 9):
8888
self._mode = "P" if colormaptype else "L"
8989
elif imagetype in (2, 10):
90-
self._mode = "RGB"
91-
if depth == 32:
92-
self._mode = "RGBA"
90+
self._mode = "RGB" if depth == 24 else "RGBA"
9391
else:
9492
msg = "unknown TGA mode"
9593
raise SyntaxError(msg)
@@ -118,15 +116,16 @@ def _open(self) -> None:
118116
start, size, mapdepth = i16(s, 3), i16(s, 5), s[7]
119117
if mapdepth == 16:
120118
self.palette = ImagePalette.raw(
121-
"BGR;15", b"\0" * 2 * start + self.fp.read(2 * size)
119+
"BGRA;15Z", bytes(2 * start) + self.fp.read(2 * size)
122120
)
121+
self.palette.mode = "RGBA"
123122
elif mapdepth == 24:
124123
self.palette = ImagePalette.raw(
125-
"BGR", b"\0" * 3 * start + self.fp.read(3 * size)
124+
"BGR", bytes(3 * start) + self.fp.read(3 * size)
126125
)
127126
elif mapdepth == 32:
128127
self.palette = ImagePalette.raw(
129-
"BGRA", b"\0" * 4 * start + self.fp.read(4 * size)
128+
"BGRA", bytes(4 * start) + self.fp.read(4 * size)
130129
)
131130
else:
132131
msg = "unknown TGA map depth"

src/libImaging/Unpack.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,21 @@ ImagingUnpackBGRA15(UINT8 *out, const UINT8 *in, int pixels) {
718718
}
719719
}
720720

721+
void
722+
ImagingUnpackBGRA15Z(UINT8 *out, const UINT8 *in, int pixels) {
723+
int i, pixel;
724+
/* RGB, rearranged channels, 5/5/5/1 bits per pixel, inverted alpha */
725+
for (i = 0; i < pixels; i++) {
726+
pixel = in[0] + (in[1] << 8);
727+
out[B] = (pixel & 31) * 255 / 31;
728+
out[G] = ((pixel >> 5) & 31) * 255 / 31;
729+
out[R] = ((pixel >> 10) & 31) * 255 / 31;
730+
out[A] = ~((pixel >> 15) * 255);
731+
out += 4;
732+
in += 2;
733+
}
734+
}
735+
721736
void
722737
ImagingUnpackRGB16(UINT8 *out, const UINT8 *in, int pixels) {
723738
int i, pixel;
@@ -1538,7 +1553,7 @@ static struct {
15381553

15391554
/* flags: "I" inverted data; "R" reversed bit order; "B" big
15401555
endian byte order (default is little endian); "L" line
1541-
interleave, "S" signed, "F" floating point */
1556+
interleave, "S" signed, "F" floating point, "Z" inverted alpha */
15421557

15431558
/* exception: rawmodes "I" and "F" are always native endian byte order */
15441559

@@ -1646,6 +1661,7 @@ static struct {
16461661
{"RGBA", "RGBA;L", 32, unpackRGBAL},
16471662
{"RGBA", "RGBA;15", 16, ImagingUnpackRGBA15},
16481663
{"RGBA", "BGRA;15", 16, ImagingUnpackBGRA15},
1664+
{"RGBA", "BGRA;15Z", 16, ImagingUnpackBGRA15Z},
16491665
{"RGBA", "RGBA;4B", 16, ImagingUnpackRGBA4B},
16501666
{"RGBA", "RGBA;16L", 64, unpackRGBA16L},
16511667
{"RGBA", "RGBA;16B", 64, unpackRGBA16B},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy