Pillow/src/libImaging/PackDecode.c

93 lines
1.9 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library.
* $Id$
*
* decoder for PackBits image data.
*
* history:
2020-05-01 15:08:57 +03:00
* 96-04-19 fl Created
2010-07-31 06:52:47 +04:00
*
* Copyright (c) Fredrik Lundh 1996.
* Copyright (c) Secret Labs AB 1997.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
int
ImagingPackbitsDecode(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
{
UINT8 n;
UINT8* ptr;
int i;
ptr = buf;
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
if (ptr[0] == 0x80) {
/* Nop */
ptr++; bytes--;
continue;
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
/* Run */
if (bytes < 2)
return ptr - buf;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
for (n = 257 - ptr[0]; n > 0; n--) {
if (state->x >= state->bytes) {
/* state->errcode = IMAGING_CODEC_OVERRUN; */
break;
}
state->buffer[state->x++] = ptr[1];
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
ptr += 2; bytes -= 2;
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 */
n = ptr[0]+2;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (bytes < n)
return ptr - buf;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
for (i = 1; i < n; i++) {
if (state->x >= state->bytes) {
/* state->errcode = IMAGING_CODEC_OVERRUN; */
break;
}
state->buffer[state->x++] = ptr[i];
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
ptr += n; bytes -= 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
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
2020-05-01 15:08:57 +03:00
if (++state->y >= state->ysize) {
/* End of file (errcode = 0) */
return -1;
}
}
2010-07-31 06:52:47 +04:00
}
}