Remove padding between interleaved PCX palette data (#9005)

This commit is contained in:
Andrew Murray 2025-06-11 23:19:09 +10:00 committed by GitHub
parent 7f7c27f66a
commit 8ccdc399df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 6 deletions

BIN
Tests/images/p_4_planes.pcx Normal file

Binary file not shown.

View File

@ -37,6 +37,11 @@ def test_sanity(tmp_path: Path) -> None:
im.save(f)
def test_p_4_planes() -> None:
with Image.open("Tests/images/p_4_planes.pcx") as im:
assert im.getpixel((0, 0)) == 3
def test_bad_image_size() -> None:
with open("Tests/images/pil184.pcx", "rb") as fp:
data = fp.read()

View File

@ -60,15 +60,25 @@ ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
}
if (state->x >= state->bytes) {
if (state->bytes % state->xsize && state->bytes > state->xsize) {
int bands = state->bytes / state->xsize;
int stride = state->bytes / bands;
int bands;
int xsize = 0;
int stride = 0;
if (state->bits == 2 || state->bits == 4) {
xsize = (state->xsize + 7) / 8;
bands = state->bits;
stride = state->bytes / state->bits;
} else {
xsize = state->xsize;
bands = state->bytes / state->xsize;
if (bands != 0) {
stride = state->bytes / bands;
}
}
if (stride > xsize) {
int i;
for (i = 1; i < bands; i++) { // note -- skipping first band
memmove(
&state->buffer[i * state->xsize],
&state->buffer[i * stride],
state->xsize
&state->buffer[i * xsize], &state->buffer[i * stride], xsize
);
}
}