php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
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
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 <stdio.h>
55#include <stdlib.h>
56#include <limits.h>
57
58#include "gd.h"
59#include "gdfonts.h"
60#include "gd_errors.h"
61#include "wbmp.h"
62
63
64/* gd_putout
65 ** ---------
66 ** Wrapper around gdPutC for use with writewbmp
67 **
68 */
69void 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 */
80int gd_getin (void *in)
81{
82 return (gdGetC((gdIOCtx *) in));
83}
84
85static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
86
87/* gdImageWBMPCtx
88 ** --------------
89 ** Write the image as a wbmp file
90 ** Parameters are:
91 ** image: gd image structure;
92 ** fg: the index of the foreground color. any other value will be
93 ** considered as background and will not be written
94 ** out: the stream where to write
95 */
96void gdImageWBMPCtx (gdImagePtr image, int fg, gdIOCtx * out)
97{
98 _gdImageWBMPCtx(image, fg, out);
99}
100
101/* returns 0 on success, 1 on failure */
102static int _gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
103{
104 int x, y, pos;
105 Wbmp *wbmp;
106
107 /* create the WBMP */
108 if ((wbmp = createwbmp (gdImageSX (image), gdImageSY (image), WBMP_WHITE)) == NULL) {
109 gd_error("Could not create WBMP");
110 return 1;
111 }
112
113 /* fill up the WBMP structure */
114 pos = 0;
115 for (y = 0; y < gdImageSY(image); y++) {
116 for (x = 0; x < gdImageSX(image); x++) {
117 if (gdImageGetPixel (image, x, y) == fg) {
118 wbmp->bitmap[pos] = WBMP_BLACK;
119 }
120 pos++;
121 }
122 }
123
124 /* write the WBMP to a gd file descriptor */
125 if (writewbmp (wbmp, &gd_putout, out)) {
126 freewbmp(wbmp);
127 gd_error("Could not save WBMP");
128 return 1;
129 }
130
131 /* des submitted this bugfix: gdFree the memory. */
132 freewbmp(wbmp);
133
134 return 0;
135}
136
137/* gdImageCreateFromWBMPCtx
138 ** ------------------------
139 ** Create a gdImage from a WBMP file input from an gdIOCtx
140 */
142{
143 /* FILE *wbmp_file; */
144 Wbmp *wbmp;
145 gdImagePtr im = NULL;
146 int black, white;
147 int col, row, pos;
148
149 if (readwbmp (&gd_getin, infile, &wbmp)) {
150 return NULL;
151 }
152
153 if (!(im = gdImageCreate (wbmp->width, wbmp->height))) {
154 freewbmp (wbmp);
155 return NULL;
156 }
157
158 /* create the background color */
159 white = gdImageColorAllocate(im, 255, 255, 255);
160 /* create foreground color */
161 black = gdImageColorAllocate(im, 0, 0, 0);
162
163 /* fill in image (in a wbmp 1 = white/ 0 = black) */
164 pos = 0;
165 for (row = 0; row < wbmp->height; row++) {
166 for (col = 0; col < wbmp->width; col++) {
167 if (wbmp->bitmap[pos++] == WBMP_WHITE) {
168 gdImageSetPixel(im, col, row, white);
169 } else {
170 gdImageSetPixel(im, col, row, black);
171 }
172 }
173 }
174
175 freewbmp(wbmp);
176
177 return im;
178}
179
180/* gdImageCreateFromWBMP
181 ** ---------------------
182 */
184{
185 gdImagePtr im;
186 gdIOCtx *in = gdNewFileCtx(inFile);
188 in->gd_free(in);
189
190 return im;
191}
192
194{
195 gdImagePtr im;
198 in->gd_free(in);
199 return im;
200}
201
202/* gdImageWBMP
203 ** -----------
204 */
205void gdImageWBMP (gdImagePtr im, int fg, FILE * outFile)
206{
207 gdIOCtx *out = gdNewFileCtx(outFile);
208 gdImageWBMPCtx(im, fg, out);
209 out->gd_free(out);
210}
211
212/* gdImageWBMPPtr
213 ** --------------
214 */
215void * gdImageWBMPPtr (gdImagePtr im, int *size, int fg)
216{
217 void *rv;
219 if (!_gdImageWBMPCtx(im, fg, out)) {
221 } else {
222 rv = NULL;
223 }
224 out->gd_free(out);
225
226 return rv;
227}
new_type size
Definition ffi.c:4365
void * gdDPExtractData(struct gdIOCtx *ctx, int *size)
Definition gd_io_dp.c:95
#define gdImageSY(im)
Definition gd.h:769
#define gdImageSX(im)
Definition gd.h:768
gdIOCtx * gdNewFileCtx(FILE *)
Definition gd_io_file.c:49
gdIOCtx * gdNewDynamicCtx(int, void *)
Definition gd_io_dp.c:65
gdIOCtx * gdNewDynamicCtxEx(int size, void *data, int freeFlag)
Definition gd_io_dp.c:70
gdImage * gdImagePtr
Definition gd.h:248
int gdGetC(gdIOCtx *ctx)
Definition gd_io.c:71
void gdPutC(const unsigned char c, gdIOCtx *ctx)
Definition gd_io.c:48
void * gdImageWBMPPtr(gdImagePtr im, int *size, int fg)
Definition gd_wbmp.c:215
void gd_putout(int i, void *out)
Definition gd_wbmp.c:69
gdImagePtr gdImageCreateFromWBMPCtx(gdIOCtx *infile)
Definition gd_wbmp.c:141
int gd_getin(void *in)
Definition gd_wbmp.c:80
void gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out)
Definition gd_wbmp.c:96
void gdImageWBMP(gdImagePtr im, int fg, FILE *outFile)
Definition gd_wbmp.c:205
gdImagePtr gdImageCreateFromWBMP(FILE *inFile)
Definition gd_wbmp.c:183
gdImagePtr gdImageCreateFromWBMPPtr(int size, void *data)
Definition gd_wbmp.c:193
#define NULL
Definition gdcache.h:45
gdImagePtr gdImageCreate(int sx, int sy)
Definition gd.c:141
int gdImageGetPixel(gdImagePtr im, int x, int y)
Definition gd.c:953
void gd_error(const char *format,...)
Definition gd.c:103
int gdImageColorAllocate(gdImagePtr im, int r, int g, int b)
Definition gd.c:489
void gdImageSetPixel(gdImagePtr im, int x, int y, int color)
Definition gd.c:733
unsigned const char * pos
Definition php_ffi.h:52
zend_constant * data
zval rv
Definition session.c:1024
int width
Definition wbmp.h:30
int * bitmap
Definition wbmp.h:32
int height
Definition wbmp.h:31
void(* gd_free)(struct gdIOCtx *)
Definition gd_io.h:20
Wbmp * createwbmp(int width, int height, int color)
Definition wbmp.c:112
int writewbmp(Wbmp *wbmp, void(*putout)(int c, void *out), void *out)
Definition wbmp.c:252
int readwbmp(int(*getin)(void *in), void *in, Wbmp **return_wbmp)
Definition wbmp.c:152
void freewbmp(Wbmp *wbmp)
Definition wbmp.c:298
#define WBMP_WHITE
Definition wbmp.h:35
struct Wbmp_ Wbmp
#define WBMP_BLACK
Definition wbmp.h:36
out($f, $s)