Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Imaging/libImaging/XbmEncode.c
diff options
context:
space:
mode:
Diffstat (limited to 'Imaging/libImaging/XbmEncode.c')
-rw-r--r--Imaging/libImaging/XbmEncode.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/Imaging/libImaging/XbmEncode.c b/Imaging/libImaging/XbmEncode.c
new file mode 100644
index 0000000..8ca8ab1
--- /dev/null
+++ b/Imaging/libImaging/XbmEncode.c
@@ -0,0 +1,106 @@
+/*
+ * The Python Imaging Library.
+ * $Id: XbmEncode.c 2134 2004-10-06 08:55:20Z fredrik $
+ *
+ * encoder for Xbm data
+ *
+ * history:
+ * 96-11-01 fl created
+ *
+ * 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) {
+
+ /* 8 pixels are stored in no more than 6 bytes */
+ state->bytes = 6*(state->xsize+7)/8;
+
+ state->state = 1;
+
+ }
+
+ if (bytes < state->bytes) {
+ state->errcode = IMAGING_CODEC_MEMORY;
+ return 0;
+ }
+
+ ptr = buf;
+
+ while (bytes >= state->bytes) {
+
+ state->shuffle(state->buffer,
+ (UINT8*) im->image[state->y + state->yoff] +
+ state->xoff * im->pixelsize, state->xsize);
+
+ if (state->y < state->ysize-1) {
+
+ /* any line but the last */
+ for (n = 0; n < state->xsize; n += 8) {
+
+ i = state->buffer[n/8];
+
+ *ptr++ = '0';
+ *ptr++ = 'x';
+ *ptr++ = hex[(i>>4)&15];
+ *ptr++ = hex[i&15];
+ *ptr++ = ',';
+ bytes -= 5;
+
+ if (++state->count >= 79/5) {
+ *ptr++ = '\n';
+ bytes--;
+ state->count = 0;
+ }
+
+ }
+
+ state->y++;
+
+ } else {
+
+ /* last line */
+ for (n = 0; n < state->xsize; n += 8) {
+
+ i = state->buffer[n/8];
+
+ *ptr++ = '0';
+ *ptr++ = 'x';
+ *ptr++ = hex[(i>>4)&15];
+ *ptr++ = hex[i&15];
+
+ if (n < state->xsize-8) {
+ *ptr++ = ',';
+ if (++state->count >= 79/5) {
+ *ptr++ = '\n';
+ bytes--;
+ state->count = 0;
+ }
+ } else
+ *ptr++ = '\n';
+
+ bytes -= 5;
+
+ }
+
+ state->errcode = IMAGING_CODEC_END;
+ break;
+ }
+ }
+
+ return ptr - buf;
+}