Pillow/src/libImaging/TgaRleDecode.c

119 lines
2.4 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
ImagingTgaRleDecode(Imaging im, ImagingCodecState state,
2020-05-01 15:08:57 +03:00
UINT8* buf, Py_ssize_t bytes)
2010-07-31 06:52:47 +04:00
{
int n, depth;
UINT8* ptr;
ptr = buf;
if (state->state == 0) {
2020-05-01 15:08:57 +03:00
/* check image orientation */
if (state->ystep < 0) {
state->y = state->ysize-1;
state->ystep = -1;
} else
state->ystep = 1;
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-01 15:08:57 +03:00
if (bytes < 1)
return ptr - buf;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (ptr[0] & 0x80) {
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
/* Run (1 + pixelsize bytes) */
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (bytes < 1 + depth)
break;
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
if (depth == 1)
memset(state->buffer + state->x, ptr[1], n);
else {
int i;
for (i = 0; i < n; i += depth)
memcpy(state->buffer + state->x + i, ptr+1, depth);
}
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 {
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
/* Literal (1+n+1 bytes block) */
n = depth * (ptr[0] + 1);
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (bytes < 1 + n)
break;
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
}
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) {
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
/* 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
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;
}