Pillow/src/libImaging/XbmDecode.c

82 lines
1.5 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"
2020-05-01 15:08:57 +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
ImagingXbmDecode(Imaging im, ImagingCodecState state, UINT8* buf, Py_ssize_t bytes)
2010-07-31 06:52:47 +04:00
{
enum { BYTE = 1, SKIP };
UINT8* ptr;
if (!state->state)
2020-05-01 15:08:57 +03:00
state->state = SKIP;
2010-07-31 06:52:47 +04:00
ptr = buf;
for (;;) {
2020-05-01 15:08:57 +03:00
if (state->state == SKIP) {
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
/* Skip forward until next 'x' */
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
while (bytes > 0) {
if (*ptr == 'x')
break;
ptr++;
bytes--;
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (bytes == 0)
return ptr - buf;
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-01 15:08:57 +03:00
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (bytes < 3)
return ptr - buf;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +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) {
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->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
}
}