2010-07-31 06:52:47 +04:00
|
|
|
/*
|
|
|
|
* The Python Imaging Library.
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* decoder for Targa RLE data.
|
|
|
|
*
|
|
|
|
* history:
|
2020-05-01 15:08:57 +03:00
|
|
|
* 97-01-04 fl created
|
|
|
|
* 98-09-11 fl don't one byte per pixel; take orientation into account
|
2010-07-31 06:52:47 +04:00
|
|
|
*
|
|
|
|
* Copyright (c) Fredrik Lundh 1997.
|
|
|
|
* Copyright (c) Secret Labs AB 1997-98.
|
|
|
|
*
|
|
|
|
* See the README file for information on usage and redistribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Imaging.h"
|
|
|
|
|
|
|
|
int
|
2021-01-03 06:17:51 +03:00
|
|
|
ImagingTgaRleDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
|
2010-07-31 06:52:47 +04:00
|
|
|
int n, depth;
|
2021-01-03 06:17:51 +03:00
|
|
|
UINT8 *ptr;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
ptr = buf;
|
|
|
|
|
|
|
|
if (state->state == 0) {
|
2020-05-01 15:08:57 +03:00
|
|
|
/* check image orientation */
|
|
|
|
if (state->ystep < 0) {
|
2021-01-03 06:17:51 +03:00
|
|
|
state->y = state->ysize - 1;
|
2020-05-01 15:08:57 +03:00
|
|
|
state->ystep = -1;
|
2020-05-10 12:56:36 +03:00
|
|
|
} else {
|
2020-05-01 15:08:57 +03:00
|
|
|
state->ystep = 1;
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
state->state = 1;
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
depth = state->count;
|
|
|
|
|
|
|
|
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[0] & 0x80) {
|
|
|
|
/* Run (1 + pixelsize bytes) */
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (bytes < 1 + depth) {
|
2020-05-01 15:08:57 +03:00
|
|
|
break;
|
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 = depth * ((ptr[0] & 0x7f) + 1);
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
if (state->x + n > state->bytes) {
|
|
|
|
state->errcode = IMAGING_CODEC_OVERRUN;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (depth == 1) {
|
2010-07-31 06:52:47 +04:00
|
|
|
memset(state->buffer + state->x, ptr[1], n);
|
2020-05-10 12:56:36 +03:00
|
|
|
} else {
|
2010-07-31 06:52:47 +04:00
|
|
|
int i;
|
2020-05-10 12:56:36 +03:00
|
|
|
for (i = 0; i < n; i += depth) {
|
2021-01-03 06:17:51 +03:00
|
|
|
memcpy(state->buffer + state->x + i, ptr + 1, depth);
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ptr += 1 + depth;
|
2020-05-01 15:08:57 +03:00
|
|
|
bytes -= 1 + depth;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
} else {
|
|
|
|
/* Literal (1+n+1 bytes block) */
|
|
|
|
n = depth * (ptr[0] + 1);
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-10 12:56:36 +03:00
|
|
|
if (bytes < 1 + n) {
|
2020-05-01 15:08:57 +03:00
|
|
|
break;
|
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 (state->x + n > state->bytes) {
|
|
|
|
state->errcode = IMAGING_CODEC_OVERRUN;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
memcpy(state->buffer + state->x, ptr + 1, n);
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
ptr += 1 + n;
|
|
|
|
bytes -= 1 + n;
|
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
state->x += n;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2020-05-01 15:08:57 +03:00
|
|
|
if (state->x >= state->bytes) {
|
|
|
|
/* 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
|
|
|
|
|
|
|
state->y += state->ystep;
|
|
|
|
|
|
|
|
if (state->y < 0 || state->y >= state->ysize) {
|
|
|
|
/* End of file (errcode = 0) */
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-01 15:08:57 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ptr - buf;
|
|
|
|
}
|