Added dedicated unpacker for inverted alpha

This commit is contained in:
Andrew Murray 2024-06-26 07:19:44 +10:00 committed by Yay295
parent 4cadf5c99f
commit 6ee41897e2
2 changed files with 22 additions and 10 deletions

View File

@ -36,7 +36,7 @@ MODES = {
(3, 1): "1", (3, 1): "1",
(3, 8): "L", (3, 8): "L",
(3, 16): "LA", (3, 16): "LA",
(2, 16): "BGRA;15", (2, 16): "BGRA;15Z",
(2, 24): "BGR", (2, 24): "BGR",
(2, 32): "BGRA", (2, 32): "BGRA",
} }
@ -87,9 +87,7 @@ class TgaImageFile(ImageFile.ImageFile):
elif imagetype in (1, 9): elif imagetype in (1, 9):
self._mode = "P" if colormaptype else "L" self._mode = "P" if colormaptype else "L"
elif imagetype in (2, 10): elif imagetype in (2, 10):
self._mode = "RGB" self._mode = "RGB" if depth == 24 else "RGBA"
if depth == 32:
self._mode = "RGBA"
else: else:
msg = "unknown TGA mode" msg = "unknown TGA mode"
raise SyntaxError(msg) raise SyntaxError(msg)
@ -117,11 +115,9 @@ class TgaImageFile(ImageFile.ImageFile):
# read palette # read palette
start, size, mapdepth = i16(s, 3), i16(s, 5), s[7] start, size, mapdepth = i16(s, 3), i16(s, 5), s[7]
if mapdepth == 16: if mapdepth == 16:
colormap = self.fp.read(2 * size) self.palette = ImagePalette.raw(
palette_data = bytearray(2 * start) "BGRA;15Z", bytes(2 * start) + self.fp.read(2 * size)
for a, b in zip(colormap[::2], colormap[1::2]): )
palette_data += bytearray((a, b ^ 128))
self.palette = ImagePalette.raw("BGRA;15", bytes(palette_data))
self.palette.mode = "RGBA" self.palette.mode = "RGBA"
elif mapdepth == 24: elif mapdepth == 24:
self.palette = ImagePalette.raw( self.palette = ImagePalette.raw(

View File

@ -718,6 +718,21 @@ ImagingUnpackBGRA15(UINT8 *out, const UINT8 *in, int pixels) {
} }
} }
void
ImagingUnpackBGRA15Z(UINT8 *out, const UINT8 *in, int pixels) {
int i, pixel;
/* RGB, rearranged channels, 5/5/5/1 bits per pixel, inverted alpha */
for (i = 0; i < pixels; i++) {
pixel = in[0] + (in[1] << 8);
out[B] = (pixel & 31) * 255 / 31;
out[G] = ((pixel >> 5) & 31) * 255 / 31;
out[R] = ((pixel >> 10) & 31) * 255 / 31;
out[A] = ~((pixel >> 15) * 255);
out += 4;
in += 2;
}
}
void void
ImagingUnpackRGB16(UINT8 *out, const UINT8 *in, int pixels) { ImagingUnpackRGB16(UINT8 *out, const UINT8 *in, int pixels) {
int i, pixel; int i, pixel;
@ -1538,7 +1553,7 @@ static struct {
/* flags: "I" inverted data; "R" reversed bit order; "B" big /* flags: "I" inverted data; "R" reversed bit order; "B" big
endian byte order (default is little endian); "L" line endian byte order (default is little endian); "L" line
interleave, "S" signed, "F" floating point */ interleave, "S" signed, "F" floating point, "Z" inverted alpha */
/* exception: rawmodes "I" and "F" are always native endian byte order */ /* exception: rawmodes "I" and "F" are always native endian byte order */
@ -1646,6 +1661,7 @@ static struct {
{"RGBA", "RGBA;L", 32, unpackRGBAL}, {"RGBA", "RGBA;L", 32, unpackRGBAL},
{"RGBA", "RGBA;15", 16, ImagingUnpackRGBA15}, {"RGBA", "RGBA;15", 16, ImagingUnpackRGBA15},
{"RGBA", "BGRA;15", 16, ImagingUnpackBGRA15}, {"RGBA", "BGRA;15", 16, ImagingUnpackBGRA15},
{"RGBA", "BGRA;15Z", 16, ImagingUnpackBGRA15Z},
{"RGBA", "RGBA;4B", 16, ImagingUnpackRGBA4B}, {"RGBA", "RGBA;4B", 16, ImagingUnpackRGBA4B},
{"RGBA", "RGBA;16L", 64, unpackRGBA16L}, {"RGBA", "RGBA;16L", 64, unpackRGBA16L},
{"RGBA", "RGBA;16B", 64, unpackRGBA16B}, {"RGBA", "RGBA;16B", 64, unpackRGBA16B},