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