Skip to content

Commit 6ee4189

Browse files
radarhereYay295
authored andcommitted
Added dedicated unpacker for inverted alpha
1 parent 4cadf5c commit 6ee4189

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/PIL/TgaImagePlugin.py

Lines changed: 5 additions & 9 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): "BGRA;15",
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)
@@ -117,11 +115,9 @@ def _open(self) -> None:
117115
# read palette
118116
start, size, mapdepth = i16(s, 3), i16(s, 5), s[7]
119117
if mapdepth == 16:
120-
colormap = self.fp.read(2 * size)
121-
palette_data = bytearray(2 * start)
122-
for a, b in zip(colormap[::2], colormap[1::2]):
123-
palette_data += bytearray((a, b ^ 128))
124-
self.palette = ImagePalette.raw("BGRA;15", bytes(palette_data))
118+
self.palette = ImagePalette.raw(
119+
"BGRA;15Z", bytes(2 * start) + self.fp.read(2 * size)
120+
)
125121
self.palette.mode = "RGBA"
126122
elif mapdepth == 24:
127123
self.palette = ImagePalette.raw(

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)