gd_wbmp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. WBMP: Wireless Bitmap Type 0: B/W, Uncompressed Bitmap
  3. Specification of the WBMP format can be found in the file:
  4. SPEC-WAESpec-19990524.pdf
  5. You can download the WAP specification on: http://www.wapforum.com/
  6. gd_wbmp.c
  7. Copyright (C) Johan Van den Brande (johan@vandenbrande.com)
  8. Fixed: gdImageWBMPPtr, gdImageWBMP
  9. Recoded: gdImageWBMPCtx for use with my wbmp library
  10. (wbmp library included, but you can find the latest distribution
  11. at http://www.vandenbrande.com/wbmp)
  12. Implemented: gdImageCreateFromWBMPCtx, gdImageCreateFromWBMP
  13. ---------------------------------------------------------------------------
  14. Parts of this code are from Maurice Smurlo.
  15. ** Copyright (C) Maurice Szmurlo --- T-SIT --- January 2000
  16. ** (Maurice.Szmurlo@info.unicaen.fr)
  17. ** Permission to use, copy, modify, and distribute this software and its
  18. ** documentation for any purpose and without fee is hereby granted, provided
  19. ** that the above copyright notice appear in all copies and that both that
  20. ** copyright notice and this permission notice appear in supporting
  21. ** documentation. This software is provided "as is" without express or
  22. ** implied warranty.
  23. ---------------------------------------------------------------------------
  24. Parts od this code are inspired by 'pbmtowbmp.c' and 'wbmptopbm.c' by
  25. Terje Sannum <terje@looplab.com>.
  26. **
  27. ** Permission to use, copy, modify, and distribute this software and its
  28. ** documentation for any purpose and without fee is hereby granted, provided
  29. ** that the above copyright notice appear in all copies and that both that
  30. ** copyright notice and this permission notice appear in supporting
  31. ** documentation. This software is provided "as is" without express or
  32. ** implied warranty.
  33. **
  34. ---------------------------------------------------------------------------
  35. Todo:
  36. gdCreateFromWBMP function for reading WBMP files
  37. ----------------------------------------------------------------------------
  38. */
  39. #include <gd.h>
  40. #include <gdfonts.h>
  41. #include <gd_errors.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <limits.h>
  45. #include "wbmp.h"
  46. /* gd_putout
  47. ** ---------
  48. ** Wrapper around gdPutC for use with writewbmp
  49. **
  50. */
  51. void gd_putout (int i, void *out)
  52. {
  53. gdPutC(i, (gdIOCtx *) out);
  54. }
  55. /* gd_getin
  56. ** --------
  57. ** Wrapper around gdGetC for use with readwbmp
  58. **
  59. */
  60. int gd_getin (void *in)
  61. {
  62. return (gdGetC((gdIOCtx *) in));
  63. }
  64. /* gdImageWBMPCtx
  65. ** --------------
  66. ** Write the image as a wbmp file
  67. ** Parameters are:
  68. ** image: gd image structure;
  69. ** fg: the index of the foreground color. any other value will be
  70. ** considered as background and will not be written
  71. ** out: the stream where to write
  72. */
  73. void gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
  74. {
  75. int x, y, pos;
  76. Wbmp *wbmp;
  77. /* create the WBMP */
  78. if ((wbmp = createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL) {
  79. gd_error("Could not create WBMP");
  80. return;
  81. }
  82. /* fill up the WBMP structure */
  83. pos = 0;
  84. for (y = 0; y < gdImageSY(image); y++) {
  85. for (x = 0; x < gdImageSX(image); x++) {
  86. if (gdImageGetPixel (image, x, y) == fg) {
  87. wbmp->bitmap[pos] = WBMP_BLACK;
  88. }
  89. pos++;
  90. }
  91. }
  92. /* write the WBMP to a gd file descriptor */
  93. if (writewbmp (wbmp, &gd_putout, out)) {
  94. gd_error("Could not save WBMP");
  95. }
  96. /* des submitted this bugfix: gdFree the memory. */
  97. freewbmp(wbmp);
  98. }
  99. /* gdImageCreateFromWBMPCtx
  100. ** ------------------------
  101. ** Create a gdImage from a WBMP file input from an gdIOCtx
  102. */
  103. gdImagePtr gdImageCreateFromWBMPCtx (gdIOCtx * infile)
  104. {
  105. /* FILE *wbmp_file; */
  106. Wbmp *wbmp;
  107. gdImagePtr im = NULL;
  108. int black, white;
  109. int col, row, pos;
  110. if (readwbmp (&gd_getin, infile, &wbmp)) {
  111. return NULL;
  112. }
  113. if (!(im = gdImageCreate (wbmp->width, wbmp->height))) {
  114. freewbmp (wbmp);
  115. return NULL;
  116. }
  117. /* create the background color */
  118. white = gdImageColorAllocate(im, 255, 255, 255);
  119. /* create foreground color */
  120. black = gdImageColorAllocate(im, 0, 0, 0);
  121. /* fill in image (in a wbmp 1 = white/ 0 = black) */
  122. pos = 0;
  123. for (row = 0; row < wbmp->height; row++) {
  124. for (col = 0; col < wbmp->width; col++) {
  125. if (wbmp->bitmap[pos++] == WBMP_WHITE) {
  126. gdImageSetPixel(im, col, row, white);
  127. } else {
  128. gdImageSetPixel(im, col, row, black);
  129. }
  130. }
  131. }
  132. freewbmp(wbmp);
  133. return im;
  134. }
  135. /* gdImageCreateFromWBMP
  136. ** ---------------------
  137. */
  138. gdImagePtr gdImageCreateFromWBMP (FILE * inFile)
  139. {
  140. gdImagePtr im;
  141. gdIOCtx *in = gdNewFileCtx(inFile);
  142. im = gdImageCreateFromWBMPCtx(in);
  143. in->gd_free(in);
  144. return im;
  145. }
  146. gdImagePtr gdImageCreateFromWBMPPtr (int size, void *data)
  147. {
  148. gdImagePtr im;
  149. gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
  150. im = gdImageCreateFromWBMPCtx(in);
  151. in->gd_free(in);
  152. return im;
  153. }
  154. /* gdImageWBMP
  155. ** -----------
  156. */
  157. void gdImageWBMP (gdImagePtr im, int fg, FILE * outFile)
  158. {
  159. gdIOCtx *out = gdNewFileCtx(outFile);
  160. gdImageWBMPCtx(im, fg, out);
  161. out->gd_free(out);
  162. }
  163. /* gdImageWBMPPtr
  164. ** --------------
  165. */
  166. void * gdImageWBMPPtr (gdImagePtr im, int *size, int fg)
  167. {
  168. void *rv;
  169. gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
  170. gdImageWBMPCtx(im, fg, out);
  171. rv = gdDPExtractData(out, size);
  172. out->gd_free(out);
  173. return rv;
  174. }