Pillow/src/libImaging/XbmDecode.c

79 lines
1.6 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library.
* $Id$
*
* decoder for XBM hex image data
*
* history:
2020-05-01 15:08:57 +03:00
* 96-04-13 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"
2021-01-03 06:17:51 +03:00
#define HEX(v) \
((v >= '0' && v <= '9') ? v - '0' \
: (v >= 'a' && v <= 'f') ? v - 'a' + 10 \
: (v >= 'A' && v <= 'F') ? v - 'A' + 10 \
: 0)
2010-07-31 06:52:47 +04:00
int
2021-01-03 06:17:51 +03:00
ImagingXbmDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
2010-07-31 06:52:47 +04:00
enum { BYTE = 1, SKIP };
2021-01-03 06:17:51 +03:00
UINT8 *ptr;
2010-07-31 06:52:47 +04:00
2020-05-10 12:56:36 +03:00
if (!state->state) {
2020-05-01 15:08:57 +03:00
state->state = SKIP;
2020-05-10 12:56:36 +03:00
}
2010-07-31 06:52:47 +04:00
ptr = buf;
for (;;) {
2020-05-01 15:08:57 +03:00
if (state->state == SKIP) {
/* Skip forward until next 'x' */
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
while (bytes > 0) {
2020-05-10 12:56:36 +03:00
if (*ptr == 'x') {
2020-05-01 15:08:57 +03:00
break;
2020-05-10 12:56:36 +03:00
}
2020-05-01 15:08:57 +03:00
ptr++;
bytes--;
}
2010-07-31 06:52:47 +04:00
2020-05-10 12:56:36 +03:00
if (bytes == 0) {
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
state->state = BYTE;
}
2010-07-31 06:52:47 +04:00
2020-05-10 12:56:36 +03:00
if (bytes < 3) {
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
2021-01-03 06:17:51 +03:00
state->buffer[state->x] = (HEX(ptr[1]) << 4) + HEX(ptr[2]);
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->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
2020-05-01 15:08:57 +03:00
ptr += 3;
bytes -= 3;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
state->state = SKIP;
2010-07-31 06:52:47 +04:00
}
}