Skip to content

Commit

Permalink
Support unpacking more rawmodes to RGBA palette
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Apr 11, 2024
1 parent 4897954 commit 952d41e
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions Tests/test_image_putpalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_putpalette_with_alpha_values() -> None:
(
("RGBA", (1, 2, 3, 4)),
("RGBAX", (1, 2, 3, 4, 0)),
("ARGB", (4, 1, 2, 3)),
),
)
def test_rgba_palette(mode: str, palette: tuple[int, ...]) -> None:
Expand Down
1 change: 1 addition & 0 deletions src/PIL/DdsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ def _open(self):
elif pfflags & DDPF.PALETTEINDEXED8:
self._mode = "P"
self.palette = ImagePalette.raw("RGBA", self.fp.read(1024))
self.palette.mode = "RGBA"

Check warning on line 382 in src/PIL/DdsImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/DdsImagePlugin.py#L382

Added line #L382 was not covered by tests
elif pfflags & DDPF.FOURCC:
offset = header_size + 4
if fourcc == D3DFMT.DXT1:
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def load_prepare(self):
self._prev_im = self.im
if self._frame_palette:
self.im = Image.core.fill("P", self.size, self._frame_transparency or 0)
self.im.putpalette(*self._frame_palette.getdata())
self.im.putpalette("RGB", *self._frame_palette.getdata())

Check warning on line 432 in src/PIL/GifImagePlugin.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/GifImagePlugin.py#L432

Added line #L432 was not covered by tests
else:
self.im = None
self._mode = temp_mode
Expand Down
12 changes: 6 additions & 6 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ def load(self):
if self.im is not None and self.palette and self.palette.dirty:
# realize palette
mode, arr = self.palette.getdata()
self.im.putpalette(mode, arr)
self.im.putpalette(self.palette.mode, mode, arr)

Check warning on line 854 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L854

Added line #L854 was not covered by tests
self.palette.dirty = 0
self.palette.rawmode = None
if "transparency" in self.info and mode in ("LA", "PA"):
Expand All @@ -861,9 +861,9 @@ def load(self):
self.im.putpalettealphas(self.info["transparency"])
self.palette.mode = "RGBA"
else:
palette_mode = "RGBA" if mode.startswith("RGBA") else "RGB"
self.palette.mode = palette_mode
self.palette.palette = self.im.getpalette(palette_mode, palette_mode)
self.palette.palette = self.im.getpalette(

Check warning on line 864 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L864

Added line #L864 was not covered by tests
self.palette.mode, self.palette.mode
)

if self.im is not None:
if cffi and USE_CFFI_ACCESS:
Expand Down Expand Up @@ -1968,7 +1968,7 @@ def putpalette(self, data, rawmode="RGB") -> None:
palette = ImagePalette.raw(rawmode, data)
self._mode = "PA" if "A" in self.mode else "P"
self.palette = palette
self.palette.mode = "RGB"
self.palette.mode = "RGBA" if "A" in rawmode else "RGB"

Check warning on line 1971 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L1971

Added line #L1971 was not covered by tests
self.load() # install new palette

def putpixel(self, xy, value):
Expand Down Expand Up @@ -2083,7 +2083,7 @@ def remap_palette(self, dest_map, source_palette=None):
# m_im.putpalette(mapping_palette, 'L') # converts to 'P'
# or just force it.
# UNDONE -- this is part of the general issue with palettes
m_im.im.putpalette(palette_mode + ";L", m_im.palette.tobytes())
m_im.im.putpalette(palette_mode, palette_mode + ";L", m_im.palette.tobytes())

Check warning on line 2086 in src/PIL/Image.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/Image.py#L2086

Added line #L2086 was not covered by tests

m_im = m_im.convert("L")

Expand Down
5 changes: 2 additions & 3 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -1715,10 +1715,10 @@ _putpalette(ImagingObject *self, PyObject *args) {
ImagingShuffler unpack;
int bits;

char *rawmode, *palette_mode;
char *palette_mode, *rawmode;
UINT8 *palette;
Py_ssize_t palettesize;
if (!PyArg_ParseTuple(args, "sy#", &rawmode, &palette, &palettesize)) {
if (!PyArg_ParseTuple(args, "ssy#", &palette_mode, &rawmode, &palette, &palettesize)) {
return NULL;
}

Expand All @@ -1728,7 +1728,6 @@ _putpalette(ImagingObject *self, PyObject *args) {
return NULL;
}

palette_mode = strncmp("RGBA", rawmode, 4) == 0 ? "RGBA" : "RGB";
unpack = ImagingFindUnpacker(palette_mode, rawmode, &bits);
if (!unpack) {
PyErr_SetString(PyExc_ValueError, wrong_raw_mode);
Expand Down

0 comments on commit 952d41e

Please sign in to comment.