wbmp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* WBMP
  2. ** ----
  3. ** WBMP Level 0: B/W, Uncompressed
  4. ** This implements the WBMP format as specified in WAPSpec 1.1 and 1.2.
  5. ** It does not support ExtHeaders as defined in the spec. The spec states
  6. ** that a WAP client does not need to implement ExtHeaders.
  7. **
  8. ** (c) 2000 Johan Van den Brande <johan@vandenbrande.com>
  9. */
  10. #include <stdio.h>
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "wbmp.h"
  15. #include "gd.h"
  16. #include "gdhelpers.h"
  17. #ifdef NOTDEF
  18. #define __TEST /* Compile with main function */
  19. #define __DEBUG /* Extra verbose when with __TEST */
  20. #define __WRITE /* readwbmp and writewbmp(stdout) */
  21. #define __VIEW /* view the wbmp on stdout */
  22. #endif
  23. /* getmbi
  24. ** ------
  25. ** Get a multibyte integer from a generic getin function
  26. ** 'getin' can be getc, with in = NULL
  27. ** you can find getin as a function just above the main function
  28. ** This way you gain a lot of flexibilty about how this package
  29. ** reads a wbmp file.
  30. */
  31. int
  32. getmbi (int (*getin) (void *in), void *in)
  33. {
  34. int i, mbi = 0;
  35. do
  36. {
  37. i = getin (in);
  38. if (i < 0)
  39. return (-1);
  40. mbi = (mbi << 7) | (i & 0x7f);
  41. }
  42. while (i & 0x80);
  43. return (mbi);
  44. }
  45. /* putmbi
  46. ** ------
  47. ** Put a multibyte intgerer in some kind of output stream
  48. ** I work here with a function pointer, to make it as generic
  49. ** as possible. Look at this function as an iterator on the
  50. ** mbi integers it spits out.
  51. **
  52. */
  53. void
  54. putmbi (int i, void (*putout) (int c, void *out), void *out)
  55. {
  56. int cnt, l, accu;
  57. /* Get number of septets */
  58. cnt = 0;
  59. accu = 0;
  60. while (accu != i)
  61. accu += i & 0x7f << 7 * cnt++;
  62. /* Produce the multibyte output */
  63. for (l = cnt - 1; l > 0; l--)
  64. putout (0x80 | (i & 0x7f << 7 * l) >> 7 * l, out);
  65. putout (i & 0x7f, out);
  66. }
  67. /* skipheader
  68. ** ----------
  69. ** Skips the ExtHeader. Not needed for the moment
  70. **
  71. */
  72. int
  73. skipheader (int (*getin) (void *in), void *in)
  74. {
  75. int i;
  76. do
  77. {
  78. i = getin (in);
  79. if (i < 0)
  80. return (-1);
  81. }
  82. while (i & 0x80);
  83. return (0);
  84. }
  85. /* create wbmp
  86. ** -----------
  87. ** create an empty wbmp
  88. **
  89. */
  90. Wbmp *
  91. createwbmp (int width, int height, int color)
  92. {
  93. int i;
  94. Wbmp *wbmp;
  95. if ((wbmp = (Wbmp *) gdMalloc (sizeof (Wbmp))) == NULL)
  96. return (NULL);
  97. if (overflow2(sizeof (int), width)) {
  98. gdFree(wbmp);
  99. return NULL;
  100. }
  101. if (overflow2(sizeof (int) * width, height)) {
  102. gdFree(wbmp);
  103. return NULL;
  104. }
  105. if ((wbmp->bitmap = (int *) safe_emalloc(sizeof(int), width * height, 0)) == NULL)
  106. {
  107. gdFree (wbmp);
  108. return (NULL);
  109. }
  110. wbmp->width = width;
  111. wbmp->height = height;
  112. for (i = 0; i < width * height; wbmp->bitmap[i++] = color);
  113. return (wbmp);
  114. }
  115. /* readwbmp
  116. ** -------
  117. ** Actually reads the WBMP format from an open file descriptor
  118. ** It goes along by returning a pointer to a WBMP struct.
  119. **
  120. */
  121. int
  122. readwbmp (int (*getin) (void *in), void *in, Wbmp ** return_wbmp)
  123. {
  124. int row, col, byte, pel, pos;
  125. Wbmp *wbmp;
  126. if ((wbmp = (Wbmp *) gdMalloc (sizeof (Wbmp))) == NULL)
  127. return (-1);
  128. wbmp->type = getin (in);
  129. if (wbmp->type != 0)
  130. {
  131. gdFree (wbmp);
  132. return (-1);
  133. }
  134. if (skipheader (getin, in))
  135. return (-1);
  136. wbmp->width = getmbi (getin, in);
  137. if (wbmp->width == -1)
  138. {
  139. gdFree (wbmp);
  140. return (-1);
  141. }
  142. wbmp->height = getmbi (getin, in);
  143. if (wbmp->height == -1)
  144. {
  145. gdFree (wbmp);
  146. return (-1);
  147. }
  148. #ifdef __DEBUG
  149. printf ("W: %d, H: %d\n", wbmp->width, wbmp->height);
  150. #endif
  151. if (overflow2(sizeof (int), wbmp->width) ||
  152. overflow2(sizeof (int) * wbmp->width, wbmp->height))
  153. {
  154. gdFree(wbmp);
  155. return (-1);
  156. }
  157. if ((wbmp->bitmap = (int *) safe_emalloc((size_t)wbmp->width * wbmp->height, sizeof(int), 0)) == NULL)
  158. {
  159. gdFree (wbmp);
  160. return (-1);
  161. }
  162. #ifdef __DEBUG
  163. printf ("DATA CONSTRUCTED\n");
  164. #endif
  165. pos = 0;
  166. for (row = 0; row < wbmp->height; row++)
  167. {
  168. for (col = 0; col < wbmp->width;)
  169. {
  170. byte = getin (in);
  171. for (pel = 7; pel >= 0; pel--)
  172. {
  173. if (col++ < wbmp->width)
  174. {
  175. if (byte & 1 << pel)
  176. {
  177. wbmp->bitmap[pos] = WBMP_WHITE;
  178. }
  179. else
  180. {
  181. wbmp->bitmap[pos] = WBMP_BLACK;
  182. }
  183. pos++;
  184. }
  185. }
  186. }
  187. }
  188. *return_wbmp = wbmp;
  189. return (0);
  190. }
  191. /* writewbmp
  192. ** ---------
  193. ** Write a wbmp to a file descriptor
  194. **
  195. ** Why not just giving a filedescriptor to this function?
  196. ** Well, the incentive to write this function was the complete
  197. ** integration in gd library from www.boutell.com. They use
  198. ** their own io functions, so the passing of a function seemed to be
  199. ** a logic(?) decision ...
  200. **
  201. */
  202. int
  203. writewbmp (Wbmp * wbmp, void (*putout) (int c, void *out), void *out)
  204. {
  205. int row, col;
  206. int bitpos, octet;
  207. /* Generate the header */
  208. putout (0, out); /* WBMP Type 0: B/W, Uncompressed bitmap */
  209. putout (0, out); /* FixHeaderField */
  210. /* Size of the image */
  211. putmbi (wbmp->width, putout, out); /* width */
  212. putmbi (wbmp->height, putout, out); /* height */
  213. /* Image data */
  214. for (row = 0; row < wbmp->height; row++)
  215. {
  216. bitpos = 8;
  217. octet = 0;
  218. for (col = 0; col < wbmp->width; col++)
  219. {
  220. octet |= ((wbmp->bitmap[row * wbmp->width + col] == 1) ? WBMP_WHITE : WBMP_BLACK) << --bitpos;
  221. if (bitpos == 0)
  222. {
  223. bitpos = 8;
  224. putout (octet, out);
  225. octet = 0;
  226. }
  227. }
  228. if (bitpos != 8)
  229. putout (octet, out);
  230. }
  231. return (0);
  232. }
  233. /* freewbmp
  234. ** --------
  235. ** gdFrees up memory occupied by a WBMP structure
  236. **
  237. */
  238. void
  239. freewbmp (Wbmp * wbmp)
  240. {
  241. gdFree (wbmp->bitmap);
  242. gdFree (wbmp);
  243. }
  244. /* printwbmp
  245. ** ---------
  246. ** print a WBMP to stdout for visualisation
  247. **
  248. */
  249. void
  250. printwbmp (Wbmp * wbmp)
  251. {
  252. int row, col;
  253. for (row = 0; row < wbmp->height; row++)
  254. {
  255. for (col = 0; col < wbmp->width; col++)
  256. {
  257. if (wbmp->bitmap[wbmp->width * row + col] == WBMP_BLACK)
  258. {
  259. putchar ('#');
  260. }
  261. else
  262. {
  263. putchar (' ');
  264. }
  265. }
  266. putchar ('\n');
  267. }
  268. }
  269. #ifdef __TEST
  270. /* putout to file descriptor
  271. ** -------------------------
  272. */
  273. int
  274. putout (int c, void *out)
  275. {
  276. return (putc (c, (FILE *) out));
  277. }
  278. /* getin from file descriptor
  279. ** --------------------------
  280. */
  281. int
  282. getin (void *in)
  283. {
  284. return (getc ((FILE *) in));
  285. }
  286. /* Main function
  287. ** -------------
  288. **
  289. */
  290. int
  291. main (int argc, char *argv[])
  292. {
  293. FILE *wbmp_file;
  294. Wbmp *wbmp;
  295. wbmp_file = fopen (argv[1], "rb");
  296. if (wbmp_file)
  297. {
  298. readwbmp (&getin, wbmp_file, &wbmp);
  299. #ifdef __VIEW
  300. #ifdef __DEBUG
  301. printf ("\nVIEWING IMAGE\n");
  302. #endif
  303. printwbmp (wbmp);
  304. #endif
  305. #ifdef __WRITE
  306. #ifdef __DEBUG
  307. printf ("\nDUMPING WBMP to STDOUT\n");
  308. #endif
  309. writewbmp (wbmp, &putout, stdout);
  310. #endif
  311. freewbmp (wbmp);
  312. fclose (wbmp_file);
  313. }
  314. }
  315. #endif