php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
gd_gd.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <math.h>
3#include <string.h>
4#include <stdlib.h>
5#include "gd.h"
6
7#define TRUE 1
8#define FALSE 0
9
10/* Exported functions: */
11extern void gdImageGd (gdImagePtr im, FILE * out);
12
13
14/* Use this for commenting out debug-print statements. */
15/* Just use the first '#define' to allow all the prints... */
16/*#define GD2_DBG(s) (s) */
17#define GD2_DBG(s)
18
19/* */
20/* Shared code to read color tables from gd file. */
21/* */
22int _gdGetColors (gdIOCtx * in, gdImagePtr im, int gd2xFlag)
23{
24 int i;
25 if (gd2xFlag) {
26 int trueColorFlag;
27 if (!gdGetByte(&trueColorFlag, in)) {
28 goto fail1;
29 }
30 /* 2.0.12: detect bad truecolor .gd files created by pre-2.0.12.
31 * Beginning in 2.0.12 truecolor is indicated by the initial 2-byte
32 * signature.
33 */
34 if (trueColorFlag != im->trueColor) {
35 goto fail1;
36 }
37 /* This should have been a word all along */
38 if (!im->trueColor) {
39 if (!gdGetWord(&im->colorsTotal, in)) {
40 goto fail1;
41 }
42 if (im->colorsTotal > gdMaxColors) {
43 goto fail1;
44 }
45 }
46 /* Int to accommodate truecolor single-color transparency */
47 if (!gdGetInt(&im->transparent, in)) {
48 goto fail1;
49 }
50 } else {
51 if (!gdGetByte(&im->colorsTotal, in)) {
52 goto fail1;
53 }
54 if (!gdGetWord(&im->transparent, in)) {
55 goto fail1;
56 }
57 if (im->transparent == 257) {
58 im->transparent = (-1);
59 }
60 }
61
62 GD2_DBG(printf("Palette had %d colours (T=%d)\n", im->colorsTotal, im->transparent));
63
64 if (im->trueColor) {
65 return TRUE;
66 }
67
68 for (i = 0; i < gdMaxColors; i++) {
69 if (!gdGetByte(&im->red[i], in)) {
70 goto fail1;
71 }
72 if (!gdGetByte(&im->green[i], in)) {
73 goto fail1;
74 }
75 if (!gdGetByte(&im->blue[i], in)) {
76 goto fail1;
77 }
78 if (gd2xFlag) {
79 if (!gdGetByte(&im->alpha[i], in)) {
80 goto fail1;
81 }
82 }
83 }
84
85 for (i = 0; i < im->colorsTotal; i++) {
86 im->open[i] = 0;
87 }
88
89 return TRUE;
90fail1:
91 return FALSE;
92}
93
94/* */
95/* Use the common basic header info to make the image object. */
96/* */
97static gdImagePtr _gdCreateFromFile (gdIOCtx * in, int *sx, int *sy)
98{
99 gdImagePtr im;
100 int gd2xFlag = 0;
101 int trueColorFlag = 0;
102
103 if (!gdGetWord(sx, in)) {
104 goto fail1;
105 }
106 if (*sx == 65535 || *sx == 65534) {
107 /* This is a gd 2.0 .gd file */
108 gd2xFlag = 1;
109 /* 2.0.12: 65534 signals a truecolor .gd file. There is a slight redundancy here but we can live with it. */
110 if (*sx == 65534) {
111 trueColorFlag = 1;
112 }
113 if (!gdGetWord(sx, in)) {
114 goto fail1;
115 }
116 }
117 if (!gdGetWord(sy, in)) {
118 goto fail1;
119 }
120
121 GD2_DBG(printf("Image is %dx%d\n", *sx, *sy));
122
123 if (trueColorFlag) {
124 im = gdImageCreateTrueColor(*sx, *sy);
125 } else {
126 im = gdImageCreate(*sx, *sy);
127 }
128 if(!im) {
129 goto fail1;
130 }
131 if (!_gdGetColors(in, im, gd2xFlag)) {
132 goto fail2;
133 }
134
135 return im;
136fail2:
137 gdImageDestroy(im);
138fail1:
139 return 0;
140}
141
143{
144 gdImagePtr im;
145 gdIOCtx *in;
146
147 in = gdNewFileCtx(inFile);
148 im = gdImageCreateFromGdCtx(in);
149
150 in->gd_free(in);
151
152 return im;
153}
154
156{
157 gdImagePtr im;
159 im = gdImageCreateFromGdCtx(in);
160 in->gd_free(in);
161
162 return im;
163}
164
166{
167 int sx, sy;
168 int x, y;
169 gdImagePtr im;
170
171 /* Read the header */
172 im = _gdCreateFromFile(in, &sx, &sy);
173
174 if (im == NULL) {
175 goto fail1;
176 }
177
178 /* Then the data... */
179 /* 2.0.12: support truecolor properly in .gd as well as in .gd2. Problem reported by Andreas Pfaller. */
180 if (im->trueColor) {
181 for (y = 0; y < sy; y++) {
182 for (x = 0; x < sx; x++) {
183 int pix;
184 if (!gdGetInt(&pix, in)) {
185 goto fail2;
186 }
187 im->tpixels[y][x] = pix;
188 }
189 }
190 } else {
191 for (y = 0; y < sy; y++) {
192 for (x = 0; x < sx; x++) {
193 int ch;
194 ch = gdGetC(in);
195 if (ch == EOF) {
196 goto fail2;
197 }
198 /* ROW-MAJOR IN GD 1.3 */
199 im->pixels[y][x] = ch;
200 }
201 }
202 }
203
204 return im;
205
206fail2:
207 gdImageDestroy (im);
208fail1:
209 return 0;
210}
211
213{
214 int i;
215
216 gdPutC(im->trueColor, out);
217 if (!im->trueColor) {
219 }
221 if (!im->trueColor) {
222 for (i = 0; i < gdMaxColors; i++) {
223 gdPutC((unsigned char) im->red[i], out);
224 gdPutC((unsigned char) im->green[i], out);
225 gdPutC((unsigned char) im->blue[i], out);
226 gdPutC((unsigned char) im->alpha[i], out);
227 }
228 }
229}
230
231static void _gdPutHeader (gdImagePtr im, gdIOCtx * out)
232{
233 /* 65535 indicates this is a gd 2.x .gd file.
234 * 2.0.12: 65534 indicates truecolor.
235 */
236 if (im->trueColor) {
237 gdPutWord(65534, out);
238 } else {
239 gdPutWord(65535, out);
240 }
241 gdPutWord(im->sx, out);
242 gdPutWord(im->sy, out);
243
244 _gdPutColors(im, out);
245}
246
247static void _gdImageGd (gdImagePtr im, gdIOCtx * out)
248{
249 int x, y;
250
251 _gdPutHeader(im, out);
252
253 for (y = 0; y < im->sy; y++) {
254 for (x = 0; x < im->sx; x++) {
255 /* ROW-MAJOR IN GD 1.3 */
256 if (im->trueColor) {
257 gdPutInt(im->tpixels[y][x], out);
258 } else {
259 gdPutC((unsigned char) im->pixels[y][x], out);
260 }
261 }
262 }
263}
264
265void gdImageGd (gdImagePtr im, FILE * outFile)
266{
267 gdIOCtx *out = gdNewFileCtx(outFile);
268 _gdImageGd(im, out);
269 out->gd_free(out);
270}
271
273{
274 void *rv;
276 _gdImageGd(im, out);
278 out->gd_free(out);
279 return rv;
280}
printf(string $format, mixed ... $values)
zend_long ch
Definition ffi.c:4580
new_type size
Definition ffi.c:4365
void * gdDPExtractData(struct gdIOCtx *ctx, int *size)
Definition gd_io_dp.c:95
#define gdMaxColors
Definition gd.h:56
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
#define GD2_DBG(s)
Definition gd_gd.c:17
int _gdGetColors(gdIOCtx *in, gdImagePtr im, int gd2xFlag)
Definition gd_gd.c:22
void * gdImageGdPtr(gdImagePtr im, int *size)
Definition gd_gd.c:272
gdImagePtr gdImageCreateFromGd(FILE *inFile)
Definition gd_gd.c:142
gdImagePtr gdImageCreateFromGdPtr(int size, void *data)
Definition gd_gd.c:155
void gdImageGd(gdImagePtr im, FILE *out)
Definition gd_gd.c:265
#define TRUE
Definition gd_gd.c:7
#define FALSE
Definition gd_gd.c:8
void _gdPutColors(gdImagePtr im, gdIOCtx *out)
Definition gd_gd.c:212
gdImagePtr gdImageCreateFromGdCtx(gdIOCtxPtr in)
Definition gd_gd.c:165
void gdPutInt(int w, gdIOCtx *ctx)
Definition gd_io.c:61
int gdGetC(gdIOCtx *ctx)
Definition gd_io.c:71
int gdGetByte(int *result, gdIOCtx *ctx)
Definition gd_io.c:76
int gdGetInt(int *result, gdIOCtx *ctx)
Definition gd_io.c:118
int gdGetWord(int *result, gdIOCtx *ctx)
Definition gd_io.c:85
void gdPutWord(int w, gdIOCtx *ctx)
Definition gd_io.c:53
void gdPutC(const unsigned char c, gdIOCtx *ctx)
Definition gd_io.c:48
struct gdIOCtx * gdIOCtxPtr
Definition gd_io.h:25
#define NULL
Definition gdcache.h:45
gdImagePtr gdImageCreate(int sx, int sy)
Definition gd.c:141
void gdImageDestroy(gdImagePtr im)
Definition gd.c:247
gdImagePtr gdImageCreateTrueColor(int sx, int sy)
Definition gd.c:195
zend_constant * data
zval rv
Definition session.c:1024
void(* gd_free)(struct gdIOCtx *)
Definition gd_io.h:20
int trueColor
Definition gd.h:217
int red[gdMaxColors]
Definition gd.h:179
unsigned char ** pixels
Definition gd.h:172
int blue[gdMaxColors]
Definition gd.h:181
int open[gdMaxColors]
Definition gd.h:182
int alpha[gdMaxColors]
Definition gd.h:214
int ** tpixels
Definition gd.h:218
int sy
Definition gd.h:174
int sx
Definition gd.h:173
int green[gdMaxColors]
Definition gd.h:180
int transparent
Definition gd.h:193
int colorsTotal
Definition gd.h:178
out($f, $s)