Coverage Report

Created: 2026-12-01 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libgd/src/gd_gd.c
Line
Count
Source
1
/**
2
 * File: GD IO
3
 *
4
 * Read and write GD images.
5
 *
6
 * The GD image format is a proprietary image format of libgd. *It has to be*
7
 * *regarded as being obsolete, and should only be used for development and*
8
 * *testing purposes.*
9
 *
10
 * Structure of a GD image file:
11
 *  - file header
12
 *  - color header (either truecolor or palette)
13
 *  - image data
14
 *
15
 * All numbers are stored in big-endian format. Note that all GD output is done
16
 * in the GD 2.x format (not to be confused with the GD2 format), but input may
17
 * also be in the GD 1.x format.
18
 *
19
 * GD 1.x file header structure:
20
 *  width  - 1 word
21
 *  height - 1 word
22
 *
23
 * GD 1.x color header (palette only):
24
 *  count       - 1 byte (the number of used palette colors)
25
 *  transparent - 1 word (257 signals no transparency)
26
 *  palette     - 256×3 bytes (RGB triplets)
27
 *
28
 * GD 2.x file header structure:
29
 *  signature     - 1 word ("\xFF\xFE" for truecolor, "\xFF\xFF" for palette)
30
 *  width         - 1 word
31
 *  height        - 1 word
32
 *
33
 * GD 2.x truecolor image color header:
34
 *  truecolor   - 1 byte (always "\001")
35
 *  transparent - 1 dword (ARGB color); "\377\377\377\377" means that no
36
 *          transparent color is set
37
 *
38
 * GD 2.x palette image color header:
39
 *  truecolor   - 1 byte (always "\0")
40
 *  count       - 1 word (the number of used palette colors)
41
 *  transparent - 1 dword (palette index); "\377\377\377\377" means that no
42
 *          transparent color is set
43
 *  palette     - 256 dwords (RGBA colors)
44
 *
45
 * Image data:
46
 *  Sequential pixel data; row-major from top to bottom, left to right:
47
 *   - 1 byte per pixel for palette images
48
 *   - 1 dword (ARGB) per pixel for truecolor images
49
 */
50
51
#ifdef HAVE_CONFIG_H
52
#include "config.h"
53
#endif
54
55
#include "gd.h"
56
#include "gd_errors.h"
57
#include <math.h>
58
#include <stdio.h>
59
#include <stdlib.h>
60
#include <string.h>
61
62
/* 2.3: gd is no longer mandatory */
63
#if ENABLE_GD_FORMATS
64
65
#define TRUE 1
66
#define FALSE 0
67
68
/* Use this for commenting out debug-print statements. */
69
/* Just use the first '#define' to allow all the prints... */
70
/*#define GD2_DBG(s) (s) */
71
#define GD2_DBG(s)
72
73
/*
74
 * Shared code to read color tables from gd file.
75
 */
76
int _gdGetColors(gdIOCtx *in, gdImagePtr im, int gd2xFlag)
77
{
78
    int i;
79
    if (gd2xFlag) {
80
        int trueColorFlag;
81
        if (!gdGetByte(&trueColorFlag, in)) {
82
            goto fail1;
83
        }
84
        /* 2.0.12: detect bad truecolor .gd files created by pre-2.0.12.
85
           Beginning in 2.0.12 truecolor is indicated by the initial 2-byte
86
           signature. */
87
        if (trueColorFlag != im->trueColor) {
88
            goto fail1;
89
        }
90
        /* This should have been a word all along */
91
        if (!im->trueColor) {
92
            if (!gdGetWord(&im->colorsTotal, in)) {
93
                goto fail1;
94
            }
95
            if (im->colorsTotal > gdMaxColors) {
96
                goto fail1;
97
            }
98
        }
99
        /* Int to accommodate truecolor single-color transparency */
100
        if (!gdGetInt(&im->transparent, in)) {
101
            goto fail1;
102
        }
103
    } else {
104
        if (!gdGetByte(&im->colorsTotal, in)) {
105
            goto fail1;
106
        }
107
        if (!gdGetWord(&im->transparent, in)) {
108
            goto fail1;
109
        }
110
    }
111
    /* Make sure transparent index is within bounds of the palette. */
112
    if (!(im->trueColor) && (im->transparent >= im->colorsTotal || im->transparent < 0)) {
113
        im->transparent = (-1);
114
    }
115
    GD2_DBG(printf("Palette had %d colours (T=%d)\n", im->colorsTotal, im->transparent));
116
    if (im->trueColor) {
117
        return TRUE;
118
    }
119
    for (i = 0; (i < gdMaxColors); i++) {
120
        if (!gdGetByte(&im->red[i], in)) {
121
            goto fail1;
122
        }
123
        if (!gdGetByte(&im->green[i], in)) {
124
            goto fail1;
125
        }
126
        if (!gdGetByte(&im->blue[i], in)) {
127
            goto fail1;
128
        }
129
        if (gd2xFlag) {
130
            if (!gdGetByte(&im->alpha[i], in)) {
131
                goto fail1;
132
            }
133
        }
134
    }
135
136
    for (i = 0; (i < im->colorsTotal); i++) {
137
        im->open[i] = 0;
138
    }
139
140
    return TRUE;
141
fail1:
142
    return FALSE;
143
}
144
145
/* */
146
/* Use the common basic header info to make the image object. */
147
/* */
148
static gdImagePtr _gdCreateFromFile(gdIOCtx *in, int *sx, int *sy)
149
{
150
    gdImagePtr im;
151
    int gd2xFlag = 0;
152
    int trueColorFlag = 0;
153
154
    if (!gdGetWord(sx, in)) {
155
        goto fail1;
156
    }
157
    if ((*sx == 65535) || (*sx == 65534)) {
158
        /* This is a gd 2.0 .gd file */
159
        gd2xFlag = 1;
160
        /* 2.0.12: 65534 signals a truecolor .gd file. There is a slight redundancy here but we can
161
         * live with it. */
162
        if (*sx == 65534) {
163
            trueColorFlag = 1;
164
        }
165
        if (!gdGetWord(sx, in)) {
166
            goto fail1;
167
        }
168
    }
169
    if (!gdGetWord(sy, in)) {
170
        goto fail1;
171
    }
172
173
    GD2_DBG(printf("Image is %dx%d\n", *sx, *sy));
174
175
    if (trueColorFlag) {
176
        im = gdImageCreateTrueColor(*sx, *sy);
177
    } else {
178
        im = gdImageCreate(*sx, *sy);
179
    }
180
    if (!im) {
181
        goto fail1;
182
    }
183
    if (!_gdGetColors(in, im, gd2xFlag)) {
184
        goto fail2;
185
    }
186
187
    return im;
188
fail2:
189
    gdImageDestroy(im);
190
fail1:
191
    return 0;
192
}
193
194
BGD_DECLARE(gdImagePtr) gdImageCreateFromGd(FILE *inFile)
195
{
196
    gdImagePtr im;
197
    gdIOCtx *in;
198
199
    in = gdNewFileCtx(inFile);
200
    if (in == NULL)
201
        return NULL;
202
    im = gdImageCreateFromGdCtx(in);
203
204
    in->gd_free(in);
205
206
    return im;
207
}
208
209
BGD_DECLARE(gdImagePtr) gdImageCreateFromGdPtr(int size, void *data)
210
{
211
    gdImagePtr im;
212
    gdIOCtx *in = gdNewDynamicCtxEx(size, data, 0);
213
    if (!in)
214
        return 0;
215
    im = gdImageCreateFromGdCtx(in);
216
    in->gd_free(in);
217
218
    return im;
219
}
220
221
BGD_DECLARE(gdImagePtr) gdImageCreateFromGdCtx(gdIOCtxPtr in)
222
{
223
    int sx, sy;
224
    int x, y;
225
    gdImagePtr im;
226
227
    /* Read the header */
228
    im = _gdCreateFromFile(in, &sx, &sy);
229
230
    if (im == NULL) {
231
        goto fail1;
232
    }
233
234
    /* Then the data... */
235
    /* 2.0.12: support truecolor properly in .gd as well as in .gd2. Problem reported by Andreas
236
     * Pfaller. */
237
    if (im->trueColor) {
238
        for (y = 0; (y < sy); y++) {
239
            for (x = 0; (x < sx); x++) {
240
                int pix;
241
                if (!gdGetInt(&pix, in)) {
242
                    goto fail2;
243
                }
244
                im->tpixels[y][x] = pix;
245
            }
246
        }
247
    } else {
248
        for (y = 0; (y < sy); y++) {
249
            for (x = 0; (x < sx); x++) {
250
                int ch;
251
                ch = gdGetC(in);
252
                if (ch == EOF) {
253
                    goto fail2;
254
                }
255
                /* ROW-MAJOR IN GD 1.3 */
256
                im->pixels[y][x] = ch;
257
            }
258
        }
259
    }
260
261
    return im;
262
263
fail2:
264
    gdImageDestroy(im);
265
fail1:
266
    return 0;
267
}
268
269
void _gdPutColors(gdImagePtr im, gdIOCtx *out)
270
{
271
    int i;
272
273
    gdPutC(im->trueColor, out);
274
    if (!im->trueColor) {
275
        gdPutWord(im->colorsTotal, out);
276
    }
277
    gdPutInt(im->transparent, out);
278
    if (!im->trueColor) {
279
        for (i = 0; (i < gdMaxColors); i++) {
280
            gdPutC((unsigned char)im->red[i], out);
281
            gdPutC((unsigned char)im->green[i], out);
282
            gdPutC((unsigned char)im->blue[i], out);
283
            gdPutC((unsigned char)im->alpha[i], out);
284
        }
285
    }
286
}
287
288
static void _gdPutHeader(gdImagePtr im, gdIOCtx *out)
289
{
290
    /* 65535 indicates this is a gd 2.x .gd file.
291
     * 2.0.12: 65534 indicates truecolor.
292
     */
293
    if (im->trueColor) {
294
        gdPutWord(65534, out);
295
    } else {
296
        gdPutWord(65535, out);
297
    }
298
    gdPutWord(im->sx, out);
299
    gdPutWord(im->sy, out);
300
301
    _gdPutColors(im, out);
302
}
303
304
static void _gdImageGd(gdImagePtr im, gdIOCtx *out)
305
{
306
    int x, y;
307
308
    _gdPutHeader(im, out);
309
310
    for (y = 0; (y < im->sy); y++) {
311
        for (x = 0; (x < im->sx); x++) {
312
            /* ROW-MAJOR IN GD 1.3 */
313
            if (im->trueColor) {
314
                gdPutInt(im->tpixels[y][x], out);
315
            } else {
316
                gdPutC((unsigned char)im->pixels[y][x], out);
317
            }
318
        }
319
    }
320
}
321
322
BGD_DECLARE(void) gdImageGd(gdImagePtr im, FILE *outFile)
323
{
324
    gdIOCtx *out = gdNewFileCtx(outFile);
325
    if (out == NULL)
326
        return;
327
    _gdImageGd(im, out);
328
    out->gd_free(out);
329
}
330
331
BGD_DECLARE(void *) gdImageGdPtr(gdImagePtr im, int *size)
332
{
333
    void *rv;
334
    gdIOCtx *out = gdNewDynamicCtx(2048, NULL);
335
    if (out == NULL)
336
        return NULL;
337
    _gdImageGd(im, out);
338
    rv = gdDPExtractData(out, size);
339
    out->gd_free(out);
340
    return rv;
341
}
342
343
#else /* no HAVE_LIBZ or !ENABLE_GD_FORMATS */
344
345
1
static void _noGdError(void) { gd_error("GD image support has been disabled\n"); }
346
347
BGD_DECLARE(gdImagePtr) gdImageCreateFromGd(FILE *inFile)
348
0
{
349
0
    ARG_NOT_USED(inFile);
350
0
    _noGdError();
351
0
    return NULL;
352
0
}
353
354
BGD_DECLARE(gdImagePtr) gdImageCreateFromGdPtr(int size, void *data)
355
1
{
356
1
    ARG_NOT_USED(size);
357
1
    ARG_NOT_USED(data);
358
1
    _noGdError();
359
1
    return NULL;
360
1
}
361
362
BGD_DECLARE(gdImagePtr) gdImageCreateFromGdCtx(gdIOCtxPtr in)
363
0
{
364
0
    ARG_NOT_USED(in);
365
0
    _noGdError();
366
0
    return NULL;
367
0
}
368
369
BGD_DECLARE(void) gdImageGd(gdImagePtr im, FILE *outFile)
370
0
{
371
0
    ARG_NOT_USED(im);
372
0
    ARG_NOT_USED(outFile);
373
0
    _noGdError();
374
0
}
375
376
BGD_DECLARE(void *) gdImageGdPtr(gdImagePtr im, int *size)
377
0
{
378
0
    ARG_NOT_USED(im);
379
0
    ARG_NOT_USED(size);
380
0
    _noGdError();
381
    return NULL;
382
0
}
383
384
#endif /* ENABLE_GD_FORMATS */