2010-07-31 06:52:47 +04:00
|
|
|
/*
|
|
|
|
* The Python Imaging Library.
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* decoder for PCX image data.
|
|
|
|
*
|
|
|
|
* history:
|
2020-05-01 15:08:57 +03:00
|
|
|
* 95-09-14 fl Created
|
2010-07-31 06:52:47 +04:00
|
|
|
*
|
|
|
|
* Copyright (c) Fredrik Lundh 1995.
|
|
|
|
* Copyright (c) Secret Labs AB 1997.
|
|
|
|
*
|
|
|
|
* See the README file for information on usage and redistribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Imaging.h"
|
|
|
|
|
|
|
|
int
|
2021-01-03 06:17:51 +03:00
|
|
|
ImagingPcxDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
|
2010-07-31 06:52:47 +04:00
|
|
|
UINT8 n;
|
2021-01-03 06:17:51 +03:00
|
|
|
UINT8 *ptr;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-03-09 23:22:06 +03:00
|
|
|
if ((state->xsize * state->bits + 7) / 8 > state->bytes) {
|
2019-12-21 10:38:22 +03:00
|
|
|
state->errcode = IMAGING_CODEC_OVERRUN;
|
|
|
|
return -1;
|
2019-09-30 11:45:43 +03:00
|
|
|
}
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
ptr = buf;
|
|
|
|
|
|
|
|
for (;;) {
|
2020-05-10 12:56:36 +03:00
|
|
|
if (bytes < 1) {
|
2020-05-01 15:08:57 +03:00
|
|
|
return ptr - buf;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
if ((*ptr & 0xC0) == 0xC0) {
|
|
|
|
/* Run */
|
2020-05-10 12:56:36 +03:00
|
|
|
if (bytes < 2) {
|
2020-05-01 15:08:57 +03:00
|
|
|
return ptr - buf;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
n = ptr[0] & 0x3F;
|
2013-07-01 02:42:19 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
while (n > 0) {
|
|
|
|
if (state->x >= state->bytes) {
|
|
|
|
state->errcode = IMAGING_CODEC_OVERRUN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
state->buffer[state->x++] = ptr[1];
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
2021-01-03 06:17:51 +03:00
|
|
|
ptr += 2;
|
|
|
|
bytes -= 2;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
} else {
|
|
|
|
/* Literal */
|
|
|
|
state->buffer[state->x++] = ptr[0];
|
2021-01-03 06:17:51 +03:00
|
|
|
ptr++;
|
|
|
|
bytes--;
|
2020-05-01 15:08:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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 i;
|
2021-01-03 06:17:51 +03:00
|
|
|
for (i = 1; i < bands; i++) { // note -- skipping first band
|
|
|
|
memmove(
|
|
|
|
&state->buffer[i * state->xsize],
|
|
|
|
&state->buffer[i * stride],
|
|
|
|
state->xsize);
|
2020-05-01 15:08:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Got a full line, unpack it */
|
2021-01-03 06:17:51 +03:00
|
|
|
state->shuffle(
|
|
|
|
(UINT8 *)im->image[state->y + state->yoff] +
|
|
|
|
state->xoff * im->pixelsize,
|
|
|
|
state->buffer,
|
|
|
|
state->xsize);
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
state->x = 0;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
if (++state->y >= state->ysize) {
|
|
|
|
/* End of file (errcode = 0) */
|
|
|
|
return -1;
|
2014-02-16 10:41:02 +04:00
|
|
|
}
|
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
}
|