Set undefined palette values to black

This commit is contained in:
Andrew Murray 2023-07-17 09:26:24 +10:00
parent b945154097
commit 7bfc6f16dc
2 changed files with 17 additions and 4 deletions

View File

@ -84,3 +84,9 @@ def test_rgba_palette(mode, palette):
im.putpalette(palette, mode)
assert im.getpalette() == [1, 2, 3]
assert im.palette.colors == {(1, 2, 3, 4): 0}
def test_undefined_palette_index():
im = Image.new("P", (1, 1), 3)
im.putpalette((1, 2, 3))
assert im.convert("RGB").getpixel((0, 0)) == (0, 0, 0)

View File

@ -1676,11 +1676,12 @@ _quantize(ImagingObject *self, PyObject *args) {
static PyObject *
_putpalette(ImagingObject *self, PyObject *args) {
ImagingShuffler unpack;
int bits;
int i, bits;
char *rawmode, *palette_mode;
UINT8 *palette;
Py_ssize_t palettesize;
ImagingPalette im_palette;
if (!PyArg_ParseTuple(args, "sy#", &rawmode, &palette, &palettesize)) {
return NULL;
}
@ -1707,10 +1708,16 @@ _putpalette(ImagingObject *self, PyObject *args) {
strcpy(self->image->mode, strlen(self->image->mode) == 2 ? "PA" : "P");
self->image->palette = ImagingPaletteNew(palette_mode);
im_palette = ImagingPaletteNew(palette_mode);
self->image->palette = im_palette;
self->image->palette->size = palettesize * 8 / bits;
unpack(self->image->palette->palette, palette, self->image->palette->size);
im_palette->size = palettesize * 8 / bits;
unpack(im_palette->palette, palette, im_palette->size);
for (i=im_palette->size; i<256; i++) {
im_palette->palette[i * 4] = 0;
im_palette->palette[i * 4 + 1] = 0;
im_palette->palette[i * 4 + 2] = 0;
}
Py_INCREF(Py_None);
return Py_None;