gd_wbmp.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <stdio.h>
  40. #include <stdlib.h>
  41. #include <limits.h>
  42. #include "gd.h"
  43. #include "gdfonts.h"
  44. #include "gd_errors.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. static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
  65. /* gdImageWBMPCtx
  66. ** --------------
  67. ** Write the image as a wbmp file
  68. ** Parameters are:
  69. ** image: gd image structure;
  70. ** fg: the index of the foreground color. any other value will be
  71. ** considered as background and will not be written
  72. ** out: the stream where to write
  73. */
  74. void gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
  75. {
  76. _gdImageWBMPCtx(image, fg, out);
  77. }
  78. /* returns 0 on success, 1 on failure */
  79. static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
  80. {
  81. int x, y, pos;
  82. Wbmp *wbmp;
  83. /* create the WBMP */
  84. if ((wbmp = createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL) {
  85. gd_error("Could not create WBMP");
  86. return 1;
  87. }
  88. /* fill up the WBMP structure */
  89. pos = 0;
  90. for (y = 0; y < gdImageSY(image); y++) {
  91. for (x = 0; x < gdImageSX(image); x++) {
  92. if (gdImageGetPixel (image, x, y) == fg) {
  93. wbmp->bitmap[pos] = WBMP_BLACK;
  94. }
  95. pos++;
  96. }
  97. }
  98. /* write the WBMP to a gd file descriptor */
  99. if (writewbmp (wbmp, &gd_putout, out)) {
  100. freewbmp(wbmp);
  101. gd_error("Could not save WBMP");
  102. return 1;
  103. }
  104. /* des submitted this bugfix: gdFree the memory. */
  105. freewbmp(wbmp);
  106. return 0;
  107. }
  108. /* gdImageCreateFromWBMPCtx
  109. ** ------------------------
  110. ** Create a gdImage from a WBMP file input from an gdIOCtx
  111. */
  112. gdImagePtr gdImageCreateFromWBMPCtx (gdIOCtx * infile)
  113. {
  114. /* FILE *wbmp_file; */
  115. Wbmp *wbmp;
  116. gdImagePtr im = NULL;
  117. int black, white;
  118. int col, row, pos;
  119. if (readwbmp (&gd_getin, infile, &wbmp)) {
  120. return NULL;
  121. }
  122. if (!(im = gdImageCreate (wbmp->width, wbmp->height))) {
  123. freewbmp (wbmp);
  124. return NULL;
  125. }
  126. /* create the background color */
  127. white = gdImageColorAllocate(im, 255, 255, 255);
  128. /* create foreground color */
  129. black = gdImageColorAllocate(im, 0, 0, 0);
  130. /* fill in image (in a wbmp 1 = white/ 0 = black) */
  131. pos = 0;
  132. for (row = 0; row < wbmp->height; row++) {
  133. for (col = 0; col < wbmp->width; col++) {
  134. if (wbmp->bitmap[pos++] == WBMP_WHITE) {
  135. gdImageSetPixel(im, col, row, white);
  136. } else {
  137. gdImageSetPixel(im, col, row, black);
  138. }
  139. }
  140. }
  141. freewbmp(wbmp);
  142. return im;
  143. }
  144. /* gdImageCreateFromWBMP
  145. ** ---------------------
  146. */
  147. gdImagePtr gdImageCreateFromWBMP (FILE * inFile)
  148. {
  149. gdImagePtr im;
  150. gdIOCtx *in = gdNewFileCtx(inFile);
  151. im = gdImageCreateFromWBMPCtx(in);
  152. in->gd_free(in);
  153. return im;
  154. }
  155. gdImagePtr gdImageCreateFromWBMPPtr (int size, void *data)
  156. {
  157. gdImagePtr im;
  158. gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
  159. im = gdImageCreateFromWBMPCtx(in);
  160. in->gd_free(in);
  161. return im;
  162. }
  163. /* gdImageWBMP
  164. ** -----------
  165. */
  166. void gdImageWBMP (gdImagePtr im, int fg, FILE * outFile)
  167. {
  168. gdIOCtx *out = gdNewFileCtx(outFile);
  169. gdImageWBMPCtx(im, fg, out);
  170. out->gd_free(out);
  171. }
  172. /* gdImageWBMPPtr
  173. ** --------------
  174. */
  175. void * gdImageWBMPPtr (gdImagePtr im, int *size, int fg)
  176. {
  177. void *rv;
  178. gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
  179. if (!_gdImageWBMPCtx(im, fg, out)) {
  180. rv = gdDPExtractData(out, size);
  181. } else {
  182. rv = NULL;
  183. }
  184. out->gd_free(out);
  185. return rv;
  186. }