4
** WBMP Level 0: B/W, Uncompressed
5
** This implements the WBMP format as specified in WAPSpec 1.1 and 1.2.
6
** It does not support ExtHeaders as defined in the spec. The spec states
7
** that a WAP client does not need to implement ExtHeaders.
9
** (c) 2000 Johan Van den Brande <johan@vandenbrande.com>
20
#include "gdhelpers.h"
23
#define __TEST /* Compile with main function */
24
#define __DEBUG /* Extra verbose when with __TEST */
25
#define __WRITE /* readwbmp and writewbmp(stdout) */
26
#define __VIEW /* view the wbmp on stdout */
31
** Get a multibyte integer from a generic getin function
32
** 'getin' can be getc, with in = NULL
33
** you can find getin as a function just above the main function
34
** This way you gain a lot of flexibilty about how this package
38
getmbi (int (*getin) (void *in), void *in)
47
mbi = mbi << 7 | i & 0x7f;
57
** Put a multibyte intgerer in some kind of output stream
58
** I work here with a function pointer, to make it as generic
59
** as possible. Look at this function as an iterator on the
60
** mbi integers it spits out.
64
putmbi (int i, void (*putout) (int c, void *out), void *out)
68
/* Get number of septets */
72
accu += i & 0x7f << 7 * cnt++;
74
/* Produce the multibyte output */
75
for (l = cnt - 1; l > 0; l--)
76
putout (0x80 | (i & 0x7f << 7 * l) >> 7 * l, out);
78
putout (i & 0x7f, out);
86
** Skips the ExtHeader. Not needed for the moment
90
skipheader (int (*getin) (void *in), void *in)
107
** create an empty wbmp
111
createwbmp (int width, int height, int color)
116
if ((wbmp = (Wbmp *) gdMalloc (sizeof (Wbmp))) == NULL)
119
if ((wbmp->bitmap = (int *) gdMalloc (sizeof (int) * width * height)) == NULL)
126
wbmp->height = height;
128
for (i = 0; i < width * height; wbmp->bitmap[i++] = color);
137
** Actually reads the WBMP format from an open file descriptor
138
** It goes along by returning a pointer to a WBMP struct.
142
readwbmp (int (*getin) (void *in), void *in, Wbmp ** return_wbmp)
144
int row, col, byte, pel, pos;
147
if ((wbmp = (Wbmp *) gdMalloc (sizeof (Wbmp))) == NULL)
150
wbmp->type = getin (in);
157
if (skipheader (getin, in))
161
wbmp->width = getmbi (getin, in);
162
if (wbmp->width == -1)
168
wbmp->height = getmbi (getin, in);
169
if (wbmp->height == -1)
176
printf ("W: %d, H: %d\n", wbmp->width, wbmp->height);
179
if ((wbmp->bitmap = (int *) gdMalloc (sizeof (int) * wbmp->width * wbmp->height)) == NULL)
186
printf ("DATA CONSTRUCTED\n");
190
for (row = 0; row < wbmp->height; row++)
192
for (col = 0; col < wbmp->width;)
196
for (pel = 7; pel >= 0; pel--)
198
if (col++ < wbmp->width)
202
wbmp->bitmap[pos] = WBMP_WHITE;
206
wbmp->bitmap[pos] = WBMP_BLACK;
222
** Write a wbmp to a file descriptor
224
** Why not just giving a filedescriptor to this function?
225
** Well, the incentive to write this function was the complete
226
** integration in gd library from www.boutell.com. They use
227
** their own io functions, so the passing of a function seemed to be
228
** a logic(?) decision ...
232
writewbmp (Wbmp * wbmp, void (*putout) (int c, void *out), void *out)
237
/* Generate the header */
238
putout (0, out); /* WBMP Type 0: B/W, Uncompressed bitmap */
239
putout (0, out); /* FixHeaderField */
243
/* Size of the image */
244
putmbi (wbmp->width, putout, out); /* width */
245
putmbi (wbmp->height, putout, out); /* height */
249
for (row = 0; row < wbmp->height; row++)
253
for (col = 0; col < wbmp->width; col++)
255
octet |= ((wbmp->bitmap[row * wbmp->width + col] == 1) ? WBMP_WHITE : WBMP_BLACK) << --bitpos;
274
** gdFrees up memory occupied by a WBMP structure
278
freewbmp (Wbmp * wbmp)
280
gdFree (wbmp->bitmap);
287
** print a WBMP to stdout for visualisation
291
printwbmp (Wbmp * wbmp)
294
for (row = 0; row < wbmp->height; row++)
296
for (col = 0; col < wbmp->width; col++)
298
if (wbmp->bitmap[wbmp->width * row + col] == WBMP_BLACK)
313
/* putout to file descriptor
314
** -------------------------
317
putout (int c, void *out)
319
return (putc (c, (FILE *) out));
322
/* getin from file descriptor
323
** --------------------------
328
return (getc ((FILE *) in));
337
main (int argc, char *argv[])
342
wbmp_file = fopen (argv[1], "rb");
345
readwbmp (&getin, wbmp_file, &wbmp);
350
printf ("\nVIEWING IMAGE\n");
359
printf ("\nDUMPING WBMP to STDOUT\n");
362
writewbmp (wbmp, &putout, stdout);