mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-10 19:56:47 +03:00
Allow getpalette() to return less than 256 colors
This commit is contained in:
parent
10e1731149
commit
948c064b28
|
@ -74,4 +74,5 @@ def test_putpalette_with_alpha_values():
|
||||||
def test_rgba_palette(mode, palette):
|
def test_rgba_palette(mode, palette):
|
||||||
im = Image.new("P", (1, 1))
|
im = Image.new("P", (1, 1))
|
||||||
im.putpalette(palette, mode)
|
im.putpalette(palette, mode)
|
||||||
|
assert im.getpalette() == [1, 2, 3]
|
||||||
assert im.palette.colors == {(1, 2, 3, 4): 0}
|
assert im.palette.colors == {(1, 2, 3, 4): 0}
|
||||||
|
|
|
@ -821,7 +821,7 @@ class Image:
|
||||||
if self.im and self.palette and self.palette.dirty:
|
if self.im and self.palette and self.palette.dirty:
|
||||||
# realize palette
|
# realize palette
|
||||||
mode, arr = self.palette.getdata()
|
mode, arr = self.palette.getdata()
|
||||||
palette_length = self.im.putpalette(mode, arr)
|
self.im.putpalette(mode, arr)
|
||||||
self.palette.dirty = 0
|
self.palette.dirty = 0
|
||||||
self.palette.rawmode = None
|
self.palette.rawmode = None
|
||||||
if "transparency" in self.info and mode in ("LA", "PA"):
|
if "transparency" in self.info and mode in ("LA", "PA"):
|
||||||
|
@ -833,9 +833,7 @@ class Image:
|
||||||
else:
|
else:
|
||||||
palette_mode = "RGBA" if mode.startswith("RGBA") else "RGB"
|
palette_mode = "RGBA" if mode.startswith("RGBA") else "RGB"
|
||||||
self.palette.mode = palette_mode
|
self.palette.mode = palette_mode
|
||||||
self.palette.palette = self.im.getpalette(palette_mode, palette_mode)[
|
self.palette.palette = self.im.getpalette(palette_mode, palette_mode)
|
||||||
: palette_length * len(palette_mode)
|
|
||||||
]
|
|
||||||
|
|
||||||
if self.im:
|
if self.im:
|
||||||
if cffi and USE_CFFI_ACCESS:
|
if cffi and USE_CFFI_ACCESS:
|
||||||
|
|
|
@ -1063,7 +1063,7 @@ _gaussian_blur(ImagingObject *self, PyObject *args) {
|
||||||
static PyObject *
|
static PyObject *
|
||||||
_getpalette(ImagingObject *self, PyObject *args) {
|
_getpalette(ImagingObject *self, PyObject *args) {
|
||||||
PyObject *palette;
|
PyObject *palette;
|
||||||
int palettesize = 256;
|
int palettesize;
|
||||||
int bits;
|
int bits;
|
||||||
ImagingShuffler pack;
|
ImagingShuffler pack;
|
||||||
|
|
||||||
|
@ -1084,6 +1084,7 @@ _getpalette(ImagingObject *self, PyObject *args) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
palettesize = self->image->palette->size;
|
||||||
palette = PyBytes_FromStringAndSize(NULL, palettesize * bits / 8);
|
palette = PyBytes_FromStringAndSize(NULL, palettesize * bits / 8);
|
||||||
if (!palette) {
|
if (!palette) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -1672,9 +1673,11 @@ _putpalette(ImagingObject *self, PyObject *args) {
|
||||||
|
|
||||||
self->image->palette = ImagingPaletteNew(palette_mode);
|
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 *
|
static PyObject *
|
||||||
|
|
|
@ -143,6 +143,7 @@ struct ImagingPaletteInstance {
|
||||||
char mode[IMAGING_MODE_LENGTH]; /* Band names */
|
char mode[IMAGING_MODE_LENGTH]; /* Band names */
|
||||||
|
|
||||||
/* Data */
|
/* Data */
|
||||||
|
int size;
|
||||||
UINT8 palette[1024]; /* Palette data (same format as image data) */
|
UINT8 palette[1024]; /* Palette data (same format as image data) */
|
||||||
|
|
||||||
INT16 *cache; /* Palette cache (used for predefined palettes) */
|
INT16 *cache; /* Palette cache (used for predefined palettes) */
|
||||||
|
|
|
@ -40,6 +40,7 @@ ImagingPaletteNew(const char *mode) {
|
||||||
palette->mode[IMAGING_MODE_LENGTH - 1] = 0;
|
palette->mode[IMAGING_MODE_LENGTH - 1] = 0;
|
||||||
|
|
||||||
/* Initialize to ramp */
|
/* Initialize to ramp */
|
||||||
|
palette->size = 256;
|
||||||
for (i = 0; i < 256; i++) {
|
for (i = 0; i < 256; i++) {
|
||||||
palette->palette[i * 4 + 0] = palette->palette[i * 4 + 1] =
|
palette->palette[i * 4 + 0] = palette->palette[i * 4 + 1] =
|
||||||
palette->palette[i * 4 + 2] = (UINT8)i;
|
palette->palette[i * 4 + 2] = (UINT8)i;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user