gd_wbmp.c 5.2 KB

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