Pillow/src/libImaging/TgaRleDecode.c

130 lines
3.1 KiB
C
Raw Normal View History

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;
2022-02-23 07:08:33 +03:00
int extra_bytes = 0;
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
n = depth * ((ptr[0] & 0x7f) + 1);
2020-05-01 15:08:57 +03:00
if (ptr[0] & 0x80) {
/* Run (1 + pixelsize bytes) */
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
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;
} else {
/* Literal (1+n+1 bytes block) */
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) {
2022-02-23 07:08:33 +03:00
extra_bytes = n; /* full value */
n = state->bytes - state->x;
extra_bytes -= n;
2020-05-01 15:08:57 +03:00
}
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
2022-02-23 07:08:33 +03:00
for (;;) {
state->x += n;
2010-07-31 06:52:47 +04:00
2022-02-23 07:08:33 +03:00
if (state->x >= state->bytes) {
/* Got a full line, unpack it */
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
2022-02-23 07:08:33 +03:00
state->x = 0;
2010-07-31 06:52:47 +04:00
2022-02-23 07:08:33 +03:00
state->y += state->ystep;
2010-07-31 06:52:47 +04:00
2022-02-23 07:08:33 +03:00
if (state->y < 0 || state->y >= state->ysize) {
/* End of file (errcode = 0) */
return -1;
}
}
if (extra_bytes == 0) {
break;
}
if (state->x > 0) {
break; // assert
}
if (extra_bytes >= state->bytes) {
n = state->bytes;
} else {
n = extra_bytes;
2010-07-31 06:52:47 +04:00
}
2022-02-23 07:08:33 +03:00
memcpy(state->buffer + state->x, ptr, n);
ptr += n;
bytes -= n;
2022-02-23 07:08:33 +03:00
extra_bytes -= n;
2020-05-01 15:08:57 +03:00
}
2010-07-31 06:52:47 +04:00
}
return ptr - buf;
}