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