xref: /PHP-5.5/ext/gd/libgd/gd_wbmp.c (revision 75d36222)
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  */
gd_putout(int i,void * out)68 void gd_putout (int i, void *out)
69 {
70 	gdPutC(i, (gdIOCtx *) out);
71 }
72 
73 
74 /* gd_getin
75    ** --------
76    ** Wrapper around gdGetC for use with readwbmp
77    **
78  */
gd_getin(void * in)79 int gd_getin (void *in)
80 {
81 	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  */
gdImageWBMPCtx(gdImagePtr image,int fg,gdIOCtx * out)94 void gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
95 {
96 	int x, y, pos;
97 	Wbmp *wbmp;
98 
99 	/* create the WBMP */
100 	if ((wbmp = createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL) {
101 		php_gd_error("Could not create WBMP");
102 	}
103 
104 	/* fill up the WBMP structure */
105 	pos = 0;
106 	for (y = 0; y < gdImageSY(image); y++) {
107 		for (x = 0; x < gdImageSX(image); x++) {
108 			if (gdImageGetPixel (image, x, y) == fg) {
109 				wbmp->bitmap[pos] = WBMP_BLACK;
110 			}
111 			pos++;
112 		}
113 	}
114 
115 	/* write the WBMP to a gd file descriptor */
116 	if (writewbmp (wbmp, &gd_putout, out)) {
117 		php_gd_error("Could not save WBMP");
118 	}
119 	/* des submitted this bugfix: gdFree the memory. */
120 	freewbmp(wbmp);
121 }
122 
123 /* gdImageCreateFromWBMPCtx
124    ** ------------------------
125    ** Create a gdImage from a WBMP file input from an gdIOCtx
126  */
gdImageCreateFromWBMPCtx(gdIOCtx * infile)127 gdImagePtr gdImageCreateFromWBMPCtx (gdIOCtx * infile)
128 {
129 	/* FILE *wbmp_file; */
130 	Wbmp *wbmp;
131 	gdImagePtr im = NULL;
132 	int black, white;
133 	int col, row, pos;
134 
135 	if (readwbmp (&gd_getin, infile, &wbmp)) {
136 		return NULL;
137 	}
138 
139 	if (!(im = gdImageCreate (wbmp->width, wbmp->height))) {
140 		freewbmp (wbmp);
141 		return NULL;
142 	}
143 
144 	/* create the background color */
145 	white = gdImageColorAllocate(im, 255, 255, 255);
146 	/* create foreground color */
147 	black = gdImageColorAllocate(im, 0, 0, 0);
148 
149 	/* fill in image (in a wbmp 1 = white/ 0 = black) */
150 	pos = 0;
151 	for (row = 0; row < wbmp->height; row++) {
152 		for (col = 0; col < wbmp->width; col++) {
153 			if (wbmp->bitmap[pos++] == WBMP_WHITE) {
154 				gdImageSetPixel(im, col, row, white);
155 			} else {
156 				gdImageSetPixel(im, col, row, black);
157 			}
158 		}
159 	}
160 
161 	freewbmp(wbmp);
162 
163 	return im;
164 }
165 
166 /* gdImageCreateFromWBMP
167    ** ---------------------
168  */
gdImageCreateFromWBMP(FILE * inFile)169 gdImagePtr gdImageCreateFromWBMP (FILE * inFile)
170 {
171 	gdImagePtr im;
172 	gdIOCtx *in = gdNewFileCtx(inFile);
173 	im = gdImageCreateFromWBMPCtx(in);
174 	in->gd_free(in);
175 
176 	return im;
177 }
178 
gdImageCreateFromWBMPPtr(int size,void * data)179 gdImagePtr gdImageCreateFromWBMPPtr (int size, void *data)
180 {
181 	gdImagePtr im;
182 	gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
183 	im = gdImageCreateFromWBMPCtx(in);
184 	in->gd_free(in);
185 	return im;
186 }
187 
188 /* gdImageWBMP
189    ** -----------
190  */
gdImageWBMP(gdImagePtr im,int fg,FILE * outFile)191 void gdImageWBMP (gdImagePtr im, int fg, FILE * outFile)
192 {
193 	gdIOCtx *out = gdNewFileCtx(outFile);
194 	gdImageWBMPCtx(im, fg, out);
195 	out->gd_free(out);
196 }
197 
198 /* gdImageWBMPPtr
199    ** --------------
200  */
gdImageWBMPPtr(gdImagePtr im,int * size,int fg)201 void * gdImageWBMPPtr (gdImagePtr im, int *size, int fg)
202 {
203 	void *rv;
204 	gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
205 	gdImageWBMPCtx(im, fg, out);
206 	rv = gdDPExtractData(out, size);
207 	out->gd_free(out);
208 
209 	return rv;
210 }
211