Pillow/src/libImaging/XbmEncode.c

108 lines
2.2 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library.
* $Id$
*
* encoder for Xbm data
*
* history:
2020-05-01 15:08:57 +03:00
* 96-11-01 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
ImagingXbmEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
{
const char *hex = "0123456789abcdef";
UINT8* ptr = buf;
int i, n;
if (!state->state) {
2020-05-01 15:08:57 +03:00
/* 8 pixels are stored in no more than 6 bytes */
state->bytes = 6*(state->xsize+7)/8;
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
}
if (bytes < state->bytes) {
2020-05-01 15:08:57 +03:00
state->errcode = IMAGING_CODEC_MEMORY;
return 0;
2010-07-31 06:52:47 +04:00
}
ptr = buf;
while (bytes >= state->bytes) {
2020-05-01 15:08:57 +03:00
state->shuffle(state->buffer,
(UINT8*) im->image[state->y + state->yoff] +
state->xoff * im->pixelsize, state->xsize);
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (state->y < state->ysize-1) {
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
/* any line but the last */
for (n = 0; n < state->xsize; n += 8) {
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
i = state->buffer[n/8];
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
*ptr++ = '0';
*ptr++ = 'x';
*ptr++ = hex[(i>>4)&15];
*ptr++ = hex[i&15];
*ptr++ = ',';
bytes -= 5;
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (++state->count >= 79/5) {
*ptr++ = '\n';
bytes--;
state->count = 0;
}
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->y++;
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
/* last line */
for (n = 0; n < state->xsize; n += 8) {
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
i = state->buffer[n/8];
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
*ptr++ = '0';
*ptr++ = 'x';
*ptr++ = hex[(i>>4)&15];
*ptr++ = hex[i&15];
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
if (n < state->xsize-8) {
*ptr++ = ',';
if (++state->count >= 79/5) {
*ptr++ = '\n';
bytes--;
state->count = 0;
}
2020-05-11 00:46:12 +03:00
} else {
2020-05-01 15:08:57 +03:00
*ptr++ = '\n';
2020-05-11 00:46:12 +03:00
}
2010-07-31 06:52:47 +04:00
2020-05-01 15:08:57 +03:00
bytes -= 5;
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->errcode = IMAGING_CODEC_END;
break;
}
2010-07-31 06:52:47 +04:00
}
return ptr - buf;
}