Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider palette size when converting and in getpalette() #6060

Merged
merged 2 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Tests/test_image_putpalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ def test_putpalette_with_alpha_values():
def test_rgba_palette(mode, palette):
im = Image.new("P", (1, 1))
im.putpalette(palette, mode)
assert im.getpalette() == [1, 2, 3]
assert im.palette.colors == {(1, 2, 3, 4): 0}
15 changes: 15 additions & 0 deletions Tests/test_image_quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,18 @@ def test_palette(method, color):
converted = im.quantize(method=method)
converted_px = converted.load()
assert converted_px[0, 0] == converted.palette.colors[color]


def test_small_palette():
# Arrange
im = hopper()

colors = (255, 0, 0, 0, 0, 255)
p = Image.new("P", (1, 1))
p.putpalette(colors)

# Act
im = im.quantize(palette=p)

# Assert
assert len(im.getcolors()) == 2
6 changes: 2 additions & 4 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ def load(self):
if self.im and self.palette and self.palette.dirty:
# realize palette
mode, arr = self.palette.getdata()
palette_length = self.im.putpalette(mode, arr)
self.im.putpalette(mode, arr)
self.palette.dirty = 0
self.palette.rawmode = None
if "transparency" in self.info and mode in ("LA", "PA"):
Expand All @@ -833,9 +833,7 @@ def load(self):
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)[
: palette_length * len(palette_mode)
]
self.palette.palette = self.im.getpalette(palette_mode, palette_mode)

if self.im:
if cffi and USE_CFFI_ACCESS:
Expand Down
9 changes: 6 additions & 3 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ _gaussian_blur(ImagingObject *self, PyObject *args) {
static PyObject *
_getpalette(ImagingObject *self, PyObject *args) {
PyObject *palette;
int palettesize = 256;
int palettesize;
int bits;
ImagingShuffler pack;

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

palettesize = self->image->palette->size;
palette = PyBytes_FromStringAndSize(NULL, palettesize * bits / 8);
if (!palette) {
return NULL;
Expand Down Expand Up @@ -1672,9 +1673,11 @@ _putpalette(ImagingObject *self, PyObject *args) {

self->image->palette = ImagingPaletteNew(palette_mode);

unpack(self->image->palette->palette, palette, palettesize * 8 / bits);
self->image->palette->size = palettesize * 8 / bits;
unpack(self->image->palette->palette, palette, self->image->palette->size);

return PyLong_FromLong(palettesize * 8 / bits);
Py_INCREF(Py_None);
return Py_None;
}

static PyObject *
Expand Down
1 change: 1 addition & 0 deletions src/libImaging/Imaging.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ struct ImagingPaletteInstance {
char mode[IMAGING_MODE_LENGTH]; /* Band names */

/* Data */
int size;
UINT8 palette[1024]; /* Palette data (same format as image data) */

INT16 *cache; /* Palette cache (used for predefined palettes) */
Expand Down
5 changes: 3 additions & 2 deletions src/libImaging/Palette.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ImagingPaletteNew(const char *mode) {
palette->mode[IMAGING_MODE_LENGTH - 1] = 0;

/* Initialize to ramp */
palette->size = 256;
for (i = 0; i < 256; i++) {
palette->palette[i * 4 + 0] = palette->palette[i * 4 + 1] =
palette->palette[i * 4 + 2] = (UINT8)i;
Expand Down Expand Up @@ -193,7 +194,7 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) {

dmax = (unsigned int)~0;

for (i = 0; i < 256; i++) {
for (i = 0; i < palette->size; i++) {
int r, g, b;
unsigned int tmin, tmax;

Expand Down Expand Up @@ -226,7 +227,7 @@ ImagingPaletteCacheUpdate(ImagingPalette palette, int r, int g, int b) {
d[i] = (unsigned int)~0;
}

for (i = 0; i < 256; i++) {
for (i = 0; i < palette->size; i++) {
if (dmin[i] <= dmax) {
int rd, gd, bd;
int ri, gi, bi;
Expand Down