Skip to content

Commit 8ccdc39

Browse files
authored
Remove padding between interleaved PCX palette data (#9005)
1 parent 7f7c27f commit 8ccdc39

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

Tests/images/p_4_planes.pcx

136 Bytes
Binary file not shown.

Tests/test_file_pcx.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ def test_sanity(tmp_path: Path) -> None:
3737
im.save(f)
3838

3939

40+
def test_p_4_planes() -> None:
41+
with Image.open("Tests/images/p_4_planes.pcx") as im:
42+
assert im.getpixel((0, 0)) == 3
43+
44+
4045
def test_bad_image_size() -> None:
4146
with open("Tests/images/pil184.pcx", "rb") as fp:
4247
data = fp.read()

src/libImaging/PcxDecode.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,25 @@ ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t byt
6060
}
6161

6262
if (state->x >= state->bytes) {
63-
if (state->bytes % state->xsize && state->bytes > state->xsize) {
64-
int bands = state->bytes / state->xsize;
65-
int stride = state->bytes / bands;
63+
int bands;
64+
int xsize = 0;
65+
int stride = 0;
66+
if (state->bits == 2 || state->bits == 4) {
67+
xsize = (state->xsize + 7) / 8;
68+
bands = state->bits;
69+
stride = state->bytes / state->bits;
70+
} else {
71+
xsize = state->xsize;
72+
bands = state->bytes / state->xsize;
73+
if (bands != 0) {
74+
stride = state->bytes / bands;
75+
}
76+
}
77+
if (stride > xsize) {
6678
int i;
6779
for (i = 1; i < bands; i++) { // note -- skipping first band
6880
memmove(
69-
&state->buffer[i * state->xsize],
70-
&state->buffer[i * stride],
71-
state->xsize
81+
&state->buffer[i * xsize], &state->buffer[i * stride], xsize
7282
);
7383
}
7484
}

0 commit comments

Comments
 (0)