w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

gd_wbmp.c
Go to the documentation of this file.
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  ** Copyright (C) Maurice Szmurlo --- T-SIT --- January 2000
24  ** (Maurice.Szmurlo@info.unicaen.fr)
25  **
26  ** Permission to use, copy, modify, and distribute this software and its
27  ** documentation for any purpose and without fee is hereby granted, provided
28  ** that the above copyright notice appear in all copies and that both that
29  ** copyright notice and this permission notice appear in supporting
30  ** documentation. This software is provided "as is" without express or
31  ** implied warranty.
32  *
33  *--------------------------------------------------------------------------
34  *
35  * Parts of 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 /**
55  * File: WBMP IO
56  *
57  * Read and write WBMP images.
58  */
59 
60 #ifdef HAVE_CONFIG_H
61 # include "config.h"
62 #endif
63 
64 #include "gd.h"
65 #include "gd_errors.h"
66 #include "gdfonts.h"
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <limits.h>
70 
71 #include "wbmp.h"
72 
73 /* gd_putout
74  * ---------
75  * Wrapper around gdPutC for use with writewbmp
76  */
77 void gd_putout(int i, void *out)
78 {
79  gdPutC(i, (gdIOCtx *)out);
80 }
81 
82 /* gd_getin
83  * --------
84  * Wrapper around gdGetC for use with readwbmp
85  */
86 int gd_getin(void *in)
87 {
88  return (gdGetC((gdIOCtx *)in));
89 }
90 
91 static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
92 
93 /*
94  Function: gdImageWBMPCtx
95 
96  Write the image as a wbmp file
97 
98  Parameters:
99  image - gd image structure
100  fg - the index of the foreground color. any other value will be
101  considered as background and will not be written
102  out - the stream where to write
103 */
105 {
106  _gdImageWBMPCtx(image, fg, out);
107 }
108 
109 /* returns 0 on success, 1 on failure */
111 {
112  int x, y, pos;
113  Wbmp *wbmp;
114 
115  /* create the WBMP */
116  if((wbmp = createwbmp(gdImageSX(image), gdImageSY(image), WBMP_WHITE)) == NULL) {
117  gd_error("Could not create WBMP\n");
118  return 1;
119  }
120 
121  /* fill up the WBMP structure */
122  pos = 0;
123  for(y = 0; y < gdImageSY(image); y++) {
124  for(x = 0; x < gdImageSX(image); x++) {
125  if(gdImageGetPixel(image, x, y) == fg) {
126  wbmp->bitmap[pos] = WBMP_BLACK;
127  }
128  pos++;
129  }
130  }
131 
132  /* write the WBMP to a gd file descriptor */
133  if(writewbmp(wbmp, &gd_putout, out)) {
134  freewbmp(wbmp);
135  gd_error("Could not save WBMP\n");
136  return 1;
137  }
138 
139  /* des submitted this bugfix: gdFree the memory. */
140  freewbmp(wbmp);
141 
142  return 0;
143 }
144 
145 /*
146  Function: gdImageCreateFromWBMPCtx
147 
148  Reads in a WBMP image via a <gdIOCtx> struct. See
149  <gdImageCreateFromWBMP>.
150 */
152 {
153  Wbmp *wbmp;
154  gdImagePtr im = NULL;
155  int black, white;
156  int col, row, pos;
157 
158  if(readwbmp(&gd_getin, infile, &wbmp)) {
159  return (NULL);
160  }
161 
162  if(!(im = gdImageCreate(wbmp->width, wbmp->height))) {
163  freewbmp(wbmp);
164  return NULL;
165  }
166 
167  /* create the background color */
168  white = gdImageColorAllocate(im, 255, 255, 255);
169  /* create foreground color */
170  black = gdImageColorAllocate(im, 0, 0, 0);
171 
172  /* fill in image (in a wbmp 1 = white/ 0 = black) */
173  pos = 0;
174  for(row = 0; row < wbmp->height; row++) {
175  for(col = 0; col < wbmp->width; col++) {
176  if(wbmp->bitmap[pos++] == WBMP_WHITE) {
177  gdImageSetPixel(im, col, row, white);
178  } else {
179  gdImageSetPixel(im, col, row, black);
180  }
181  }
182  }
183 
184  freewbmp(wbmp);
185 
186  return im;
187 }
188 
189 
190 /*
191  Function: gdImageCreateFromWBMP
192 
193  <gdImageCreateFromWBMP> is called to load images from WBMP format
194  files. Invoke <gdImageCreateFromWBMP> with an already opened
195  pointer to a file containing the desired
196  image. <gdImageCreateFromWBMP> returns a gdImagePtr to the new
197  image, or NULL if unable to load the image (most often because the
198  file is corrupt or does not contain a WBMP
199  image). <gdImageCreateFromWBMP> does not close the file. You can
200  inspect the sx and sy members of the image to determine its
201  size. The image must eventually be destroyed using
202  <gdImageDestroy>.
203 
204  Variants:
205 
206  <gdImageCreateFromWBMPPtr> creates an image from WBMP data (i.e. the
207  contents of a WBMP file) already in memory.
208 
209  <gdImageCreateFromWBMPCtx> reads in an image using the functions in
210  a <gdIOCtx> struct.
211 
212  Parameters:
213 
214  infile - The input FILE pointer
215 
216  Returns:
217 
218  A pointer to the new image or NULL if an error occurred.
219 
220  Example:
221  (start code)
222 
223  gdImagePtr im;
224  FILE *in;
225  in = fopen("mywbmp.wbmp", "rb");
226  im = gdImageCreateFromWBMP(in);
227  fclose(in);
228  // ... Use the image ...
229  gdImageDestroy(im);
230 
231  (end code)
232 */
233 
235 {
236  gdImagePtr im;
238  if (in == NULL) return NULL;
240  in->gd_free(in);
241  return im;
242 }
243 
244 
245 /*
246  Function: gdImageCreateFromWBMPPtr
247 
248  Parameters:
249 
250  size - size of WBMP data in bytes.
251  data - WBMP data (i.e. contents of a WBMP file).
252 
253  See <gdImageCreateFromWBMP>.
254 */
256 {
257  gdImagePtr im;
259  if(!in) {
260  return 0;
261  }
263  in->gd_free(in);
264  return im;
265 }
266 
267 /*
268  Function: gdImageWBMP
269 */
270 BGD_DECLARE(void) gdImageWBMP(gdImagePtr im, int fg, FILE *outFile)
271 {
272  gdIOCtx *out = gdNewFileCtx(outFile);
273  if (out == NULL) return;
274  gdImageWBMPCtx(im, fg, out);
275  out->gd_free(out);
276 }
277 
278 /*
279  Function: gdImageWBMPPtr
280 */
281 BGD_DECLARE(void *) gdImageWBMPPtr(gdImagePtr im, int *size, int fg)
282 {
283  void *rv;
284  gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
285  if (out == NULL) return NULL;
286  if (!_gdImageWBMPCtx(im, fg, out)) {
287  rv = gdDPExtractData(out, size);
288  } else {
289  rv = NULL;
290  }
291  out->gd_free(out);
292  return rv;
293 }
#define black
Definition: backend_eps.c:34
#define white(x)
struct rect data
Definition: dvipdfm.c:64
int col
Definition: gsftopk.c:443
FILE * out
Definition: hbf2gf.c:286
static FILE * infile
Definition: rdjpgcom.c:61
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific i
Definition: afcover.h:80
kerning y
Definition: ttdriver.c:212
gdImagePtr gdImageCreate(int sx, int sy)
Definition: gd.c:181
int gdImageGetPixel(gdImagePtr im, int x, int y)
Definition: gd.c:1443
void gd_error(const char *format,...)
Definition: gd.c:108
int gdImageColorAllocate(gdImagePtr im, int r, int g, int b)
Definition: gd.c:741
void gdImageSetPixel(gdImagePtr im, int x, int y, int color)
Definition: gd.c:1237
gdIOCtx * gdNewDynamicCtxEx(int size, void *data, int freeFlag)
Definition: gd_io_dp.c:81
gdIOCtx * gdNewDynamicCtx(int size, void *data)
Definition: gd_io_dp.c:72
gdIOCtx * gdNewFileCtx(FILE *)
Definition: gd_io_file.c:56
#define gdImageSY(im)
Definition: gd.h:1330
#define BGD_DECLARE(rt)
Definition: gd.h:79
#define gdImageSX(im)
Definition: gd.h:1320
void * gdDPExtractData(struct gdIOCtx *ctx, int *size)
Definition: gd_io_dp.c:116
static void writewbmp(gdImagePtr im, FILE *out)
Definition: gd_filename.c:29
int gdGetC(gdIOCtx *ctx)
Definition: gd_io.c:54
void gdPutC(const unsigned char c, gdIOCtx *ctx)
Definition: gd_io.c:31
void gd_putout(int i, void *out)
Definition: gd_wbmp.c:77
gdImagePtr gdImageCreateFromWBMPCtx(gdIOCtx *infile)
Definition: gd_wbmp.c:151
int gd_getin(void *in)
Definition: gd_wbmp.c:86
void gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
Definition: gd_wbmp.c:104
void gdImageWBMP(gdImagePtr im, int fg, FILE *outFile)
Definition: gd_wbmp.c:270
void * gdImageWBMPPtr(gdImagePtr im, int *size, int fg)
Definition: gd_wbmp.c:281
gdImagePtr gdImageCreateFromWBMP(FILE *inFile)
Definition: gd_wbmp.c:234
gdImagePtr gdImageCreateFromWBMPPtr(int size, void *data)
Definition: gd_wbmp.c:255
Wbmp * createwbmp(int width, int height, int color)
Definition: wbmp.c:103
int readwbmp(int(*getin)(void *in), void *in, Wbmp **return_wbmp)
Definition: wbmp.c:140
void freewbmp(Wbmp *wbmp)
Definition: wbmp.c:264
#define WBMP_WHITE
Definition: wbmp.h:32
#define WBMP_BLACK
Definition: wbmp.h:33
const int * pos
Definition: combiners.h:905
float x
Definition: cordic.py:15
FILE * inFile
Definition: plain2.c:111
static int size
Definition: ppmlabel.c:24
static int row
Definition: ps2pk.c:587
static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
Definition: gd_wbmp.c:110
static FILE * in
Definition: squeeze.c:36
Definition: wbmp.h:24
int * bitmap
Definition: wbmp.h:28
int width
Definition: wbmp.h:26
int height
Definition: wbmp.h:27
Definition: gd_io.h:59
Definition: sd.h:76
#define FILE
Definition: t1stdio.h:34