Coverage Report

Created: 2026-09-28 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libgd/src/gd.h
Line
Count
Source
1
#ifndef GD_H
2
#define GD_H 1
3
4
#include <stdlib.h>
5
6
#ifdef __cplusplus
7
extern "C" {
8
#endif
9
10
/* Version information.  This gets parsed by build scripts as well as
11
 * gcc so each #define line in this group must also be splittable on
12
 * whitespace, take the form GD_*_VERSION and contain the magical
13
 * trailing comment. */
14
#define GD_MAJOR_VERSION 2      /*version605b5d1778*/
15
#define GD_MINOR_VERSION 4      /*version605b5d1778*/
16
#define GD_RELEASE_VERSION 0    /*version605b5d1778*/
17
#define GD_EXTRA_VERSION "-dev" /*version605b5d1778*/
18
19
/* End parsable section. */
20
21
/* The version string.  This is constructed from the version number
22
 * parts above via macro abuse^Wtrickery. */
23
#define GDXXX_VERSION_STR(mjr, mnr, rev, ext) mjr "." mnr "." rev ext
24
#define GDXXX_STR(s) GDXXX_SSTR(s) /* Two levels needed to expand args. */
25
#define GDXXX_SSTR(s) #s
26
27
#define GD_VERSION_STRING                                                                          \
28
    GDXXX_VERSION_STR(GDXXX_STR(GD_MAJOR_VERSION), GDXXX_STR(GD_MINOR_VERSION),                    \
29
                      GDXXX_STR(GD_RELEASE_VERSION), GD_EXTRA_VERSION)
30
31
/* Do the DLL dance: dllexport when building the DLL,
32
   dllimport when importing from it, nothing when
33
   not on Silly Silly Windows (tm Aardman Productions). */
34
35
/* 2.0.20: for headers */
36
37
/* 2.0.24: __stdcall also needed for Visual BASIC
38
   and other languages. This breaks ABI compatibility
39
   with previous DLL revs, but it's necessary. */
40
41
/* 2.0.29: WIN32 programmers can declare the NONDLL macro if they
42
   wish to build gd as a static library or by directly including
43
   the gd sources in a project. */
44
45
/* http://gcc.gnu.org/wiki/Visibility */
46
#if defined(_WIN32) || defined(CYGWIN) || defined(_WIN32_WCE)
47
#ifdef BGDWIN32
48
#ifdef NONDLL
49
#define BGD_EXPORT_DATA_PROT
50
#else
51
#ifdef __GNUC__
52
#define BGD_EXPORT_DATA_PROT __attribute__((__dllexport__))
53
#else
54
#define BGD_EXPORT_DATA_PROT __declspec(dllexport)
55
#endif
56
#endif
57
#else
58
#ifdef __GNUC__
59
#define BGD_EXPORT_DATA_PROT __attribute__((__dllimport__))
60
#else
61
#define BGD_EXPORT_DATA_PROT __declspec(dllimport)
62
#endif
63
#endif
64
#define BGD_STDCALL __stdcall
65
#define BGD_EXPORT_DATA_IMPL
66
#define BGD_MALLOC
67
#else
68
#if defined(__GNUC__) || defined(__clang__)
69
#define BGD_EXPORT_DATA_PROT __attribute__((__visibility__("default")))
70
#define BGD_EXPORT_DATA_IMPL __attribute__((__visibility__("hidden")))
71
#else
72
#define BGD_EXPORT_DATA_PROT
73
#define BGD_EXPORT_DATA_IMPL
74
#endif
75
#define BGD_STDCALL
76
#define BGD_MALLOC __attribute__((__malloc__))
77
#endif
78
79
#define BGD_DECLARE(rt) BGD_EXPORT_DATA_PROT rt BGD_STDCALL
80
81
/* VS2012+ disable keyword macroizing unless _ALLOW_KEYWORD_MACROS is set
82
   We define inline, and strcasecmp if they're missing
83
*/
84
#ifdef _MSC_VER
85
#define _ALLOW_KEYWORD_MACROS
86
#ifndef inline
87
#define inline __inline
88
#endif
89
#ifndef strcasecmp
90
#define strcasecmp _stricmp
91
#endif
92
#endif
93
94
#undef ARG_NOT_USED
95
#define ARG_NOT_USED(arg) (void)arg
96
97
/* gd.h: declarations file for the graphic-draw module.
98
 * Permission to use, copy, modify, and distribute this software and its
99
 * documentation for any purpose and without fee is hereby granted, provided
100
 * that the above copyright notice appear in all copies and that both that
101
 * copyright notice and this permission notice appear in supporting
102
 * documentation.  This software is provided "AS IS." Thomas Boutell and
103
 * Boutell.Com, Inc. disclaim all warranties, either express or implied,
104
 * including but not limited to implied warranties of merchantability and
105
 * fitness for a particular purpose, with respect to this code and accompanying
106
 * documentation. */
107
108
/* stdio is needed for file I/O. */
109
#include "gd_io.h"
110
#include <stdarg.h>
111
#include <stdio.h>
112
113
/* The maximum number of palette entries in palette-based images.
114
   In the wonderful new world of gd 2.0, you can of course have
115
   many more colors when using truecolor mode. */
116
117
0
#define gdMaxColors 256
118
119
/* Image type. See functions below; you will not need to change
120
   the elements directly. Use the provided macros to
121
   access sx, sy, the color table, and colorsTotal for
122
   read-only purposes. */
123
124
/* If 'truecolor' is set true, the image is truecolor;
125
   pixels are represented by integers, which
126
   must be 32 bits wide or more.
127
128
   True colors are represented as follows:
129
130
   ARGB
131
132
   Where 'A' (alpha channel) occupies only the
133
   LOWER 7 BITS of the MSB. This very small
134
   loss of alpha channel resolution allows gd 2.x
135
   to keep backwards compatibility by allowing
136
   signed integers to be used to represent colors,
137
   and negative numbers to represent special cases,
138
   just as in gd 1.x. */
139
140
638
#define gdAlphaMax 127
141
8.66k
#define gdAlphaOpaque 0
142
3.90k
#define gdAlphaTransparent 127
143
0
#define gdRedMax 255
144
0
#define gdGreenMax 255
145
0
#define gdBlueMax 255
146
147
/**
148
 * Group: Color Decomposition
149
 */
150
151
/**
152
 * Macro: gdTrueColorGetAlpha
153
 *
154
 * Gets the alpha channel value
155
 *
156
 * Parameters:
157
 *   c - The color
158
 *
159
 * See also:
160
 *   - <gdTrueColorAlpha>
161
 */
162
11.6k
#define gdTrueColorGetAlpha(c) (((c) & 0x7F000000) >> 24)
163
164
/**
165
 * Macro: gdTrueColorGetRed
166
 *
167
 * Gets the red channel value
168
 *
169
 * Parameters:
170
 *   c - The color
171
 *
172
 * See also:
173
 *   - <gdTrueColorAlpha>
174
 */
175
638
#define gdTrueColorGetRed(c) (((c) & 0xFF0000) >> 16)
176
177
/**
178
 * Macro: gdTrueColorGetGreen
179
 *
180
 * Gets the green channel value
181
 *
182
 * Parameters:
183
 *   c - The color
184
 *
185
 * See also:
186
 *   - <gdTrueColorAlpha>
187
 */
188
638
#define gdTrueColorGetGreen(c) (((c) & 0x00FF00) >> 8)
189
190
/**
191
 * Macro: gdTrueColorGetBlue
192
 *
193
 * Gets the blue channel value
194
 *
195
 * Parameters:
196
 *   c - The color
197
 *
198
 * See also:
199
 *   - <gdTrueColorAlpha>
200
 */
201
638
#define gdTrueColorGetBlue(c) ((c) & 0x0000FF)
202
203
/**
204
 * Group: Effects
205
 *
206
 * The layering effect
207
 *
208
 * When pixels are drawn the new colors are "mixed" with the background
209
 * depending on the effect.
210
 *
211
 * Note that the effect does not apply to palette images, where pixels
212
 * are always replaced.
213
 *
214
 * Modes:
215
 *   gdEffectReplace    - replace pixels
216
 *   gdEffectAlphaBlend - blend pixels, see <gdAlphaBlend>
217
 *   gdEffectNormal     - default mode; same as gdEffectAlphaBlend
218
 *   gdEffectOverlay    - overlay pixels, see <gdLayerOverlay>
219
 *   gdEffectMultiply   - overlay pixels with multiply effect, see
220
 *                        <gdLayerMultiply>
221
 *
222
 * See also:
223
 *   - <gdImageAlphaBlending>
224
 */
225
0
#define gdEffectReplace 0
226
8.66k
#define gdEffectAlphaBlend 1
227
8.66k
#define gdEffectNormal 2
228
0
#define gdEffectOverlay 3
229
0
#define gdEffectMultiply 4
230
231
#define GD_TRUE 1
232
#define GD_FALSE 0
233
234
#define GD_EPSILON 1e-6
235
#ifndef M_PI
236
#define M_PI 3.14159265358979323846
237
#endif
238
239
/* This function accepts truecolor pixel values only. The
240
   source color is composited with the destination color
241
   based on the alpha channel value of the source color.
242
   The resulting color is opaque. */
243
244
/**
245
 * @brief Blend two colors
246
 *
247
 * This function accepts truecolor pixel values only. The
248
 * source color is composited with the destination color
249
 * based on the alpha channel value of the source color.
250
 * The resulting color is opaque.
251
 * @param dst The color to blend onto.
252
 * @param src The color to blend.
253
 *
254
 * @see gdImageAlphaBlending gdLayerOverlay gdLayerMultiply
255
 */
256
BGD_DECLARE(int) gdAlphaBlend(int dest, int src);
257
258
/**
259
 * @brief Overlay two colors
260
 *
261
 * @param dst The color to overlay onto.
262
 * @param src The color to overlay.
263
 *
264
 * @return The resulting color.
265
 *
266
 * @see gdImageAlphaBlending gdAlphaBlend gdLayerMultiply
267
 */
268
BGD_DECLARE(int) gdLayerOverlay(int dest, int src);
269
270
/**
271
 * @brief Overlay two colors with multiply effect
272
 *
273
 * @param dst The color to overlay onto.
274
 * @param src The color to overlay.
275
 *
276
 * @return The resulting color.
277
 *
278
 * @see gdImageAlphaBlending  gdAlphaBlend gdLayerOverlay
279
 */
280
BGD_DECLARE(int) gdLayerMultiply(int dest, int src);
281
282
/**
283
 * @addtogroup TransformScaleRotate Transform, scale and rotate
284
 * @{
285
 */
286
/**
287
* @brief Let @ref gdImageScaleWithOptions choose the interpolation method from the scale
288
 * direction.
289
 *
290
 * Automatic selection uses @ref GD_LANCZOS3 for downscales or mixed-axis scales,
291
 * and @ref GD_CATMULLROM for pure upscales.
292
 */
293
#define GD_SCALE_INTERPOLATION_AUTO -1
294
295
/**
296
 * @brief gdInterpolationMethod
297
 *
298
 * Interpolation kernels used by image scaling, rotation and affine
299
 * transformation functions. Newly-created images use @ref GD_BILINEAR_FIXED
300
 * by default. Call @ref gdImageSetInterpolationMethod on the source image before
301
 * using APIs that read the image's current interpolation method.
302
 *
303
 * @ref gdImageScaleWithOptions can either use one of these values explicitly or
304
 * use @ref GD_SCALE_INTERPOLATION_AUTO to choose a method from the requested
305
 * scale direction.
306
 * @note
307
 *   @ref GD_WEIGHTED4 is not supported by @ref gdImageScale. For downscales or
308
 *   mixed-axis scales, @ref gdImageScale maps the fixed compatibility methods
309
 *   (@ref GD_DEFAULT, @ref GD_BILINEAR_FIXED, @ref GD_LINEAR, @ref GD_BICUBIC_FIXED and
310
 *   @ref GD_BICUBIC) to @ref GD_TRIANGLE to avoid the blur and aliasing of the old
311
 *   fixed scalers.
312
 *
313
 * @see gdImageSetInterpolationMethod gdImageScale gdImageScaleWithOptions gdImageRotateInterpolated gdTransformAffineCopy
314
 */
315
typedef enum {
316
    GD_DEFAULT = 0, /**< Compatibility default. Setting this resolves to @ref GD_LINEAR */
317
    GD_BELL,        /**< Bell filter. */
318
    GD_BESSEL,      /**< Bessel filter. */
319
    GD_BILINEAR_FIXED, /**< Compatibility bilinear scaler. */
320
    GD_BICUBIC,     /**< Bicubic interpolation. */
321
    GD_BICUBIC_FIXED, /**< Compatibility bicubic scaler. */
322
    GD_BLACKMAN,    /**< Blackman filter. */
323
    GD_BOX,         /**< Box filter. */
324
    GD_BSPLINE,     /**< B-spline filter. */
325
    GD_CATMULLROM,  /**< Catmull-Rom filter. */
326
    GD_GAUSSIAN,    /**< Gaussian filter. */
327
    GD_GENERALIZED_CUBIC, /**< Generalized cubic filter. */
328
    GD_HERMITE,     /**< Hermite filter. */
329
    GD_HAMMING,     /**< Hamming filter. */
330
    GD_HANNING,     /**< Hanning filter. */
331
    GD_MITCHELL,    /**< Mitchell filter. */
332
    GD_NEAREST_NEIGHBOUR, /**< Nearest-neighbour interpolation. */
333
    GD_POWER,     /**< Power filter. */
334
    GD_QUADRATIC, /**< Quadratic filter. */
335
    GD_SINC,      /**< Sinc filter. */
336
    GD_TRIANGLE,  /**< Triangle filter. */
337
    GD_WEIGHTED4, /**< Four-pixel weighted interpolation for rotation and affine sampling. */
338
    GD_LINEAR,    /**< Bilinear interpolation. */
339
    GD_LANCZOS3,  /**< Lanczos filter with radius 3. */
340
    GD_LANCZOS8,  /**< Lanczos filter with radius 8. */
341
    GD_BLACKMAN_BESSEL, /**< Blackman-windowed Bessel filter. */
342
    GD_BLACKMAN_SINC,   /**< Blackman-windowed sinc filter. */
343
    GD_QUADRATIC_BSPLINE, /**< Quadratic B-spline filter. */
344
    GD_CUBIC_SPLINE,      /**< Cubic spline filter. */
345
    GD_COSINE,         /**< Cosine filter. */
346
    GD_WELSH,       /**< Welsh filter. */
347
    GD_METHOD_COUNT = 30
348
} gdInterpolationMethod;
349
350
/* Interpolation function ptr */
351
typedef double (*interpolation_method)(double, double);
352
/** @} */
353
354
/*
355
   Group: Types
356
357
   typedef: gdImage
358
359
   typedef: gdImagePtr
360
361
   The data structure in which gd stores images. <gdImageCreate>,
362
   <gdImageCreateTrueColor> and the various image file-loading functions
363
   return a pointer to this type, and the other functions expect to
364
   receive a pointer to this type as their first argument.
365
366
   *gdImagePtr* is a pointer to *gdImage*.
367
368
   See also:
369
         <Accessor Macros>
370
371
   (Previous versions of this library encouraged directly manipulating
372
   the contents of the struct.
373
   We are attempting to move away from this practice so the fields
374
   will be considered private in 2.5 and later. )
375
*/
376
typedef struct gdImageStruct {
377
    /* Palette-based image pixels */
378
    unsigned char **pixels;
379
    int sx;
380
    int sy;
381
    /* These are valid in palette images only. See also
382
       'alpha', which appears later in the structure to
383
       preserve binary backwards compatibility */
384
    int colorsTotal;
385
    int red[gdMaxColors];
386
    int green[gdMaxColors];
387
    int blue[gdMaxColors];
388
    int open[gdMaxColors];
389
    /* For backwards compatibility, this is set to the
390
       first palette entry with 100% transparency,
391
       and is also set and reset by the
392
       gdImageColorTransparent function. Newer
393
       applications can allocate palette entries
394
       with any desired level of transparency; however,
395
       bear in mind that many viewers, notably
396
       many web browsers, fail to implement
397
       full alpha channel for PNG and provide
398
       support for full opacity or transparency only. */
399
    int transparent;
400
    int *polyInts;
401
    int polyAllocated;
402
    struct gdImageStruct *brush;
403
    struct gdImageStruct *tile;
404
    int brushColorMap[gdMaxColors];
405
    int tileColorMap[gdMaxColors];
406
    int styleLength;
407
    int stylePos;
408
    int *style;
409
    int interlace;
410
    /* New in 2.0: thickness of line. Initialized to 1. */
411
    int thick;
412
    /* New in 2.0: alpha channel for palettes. Note that only
413
       Macintosh Internet Explorer and (possibly) Netscape 6
414
       really support multiple levels of transparency in
415
       palettes, to my knowledge, as of 2/15/01. Most
416
       common browsers will display 100% opaque and
417
       100% transparent correctly, and do something
418
       unpredictable and/or undesirable for levels
419
       in between. TBB */
420
    int alpha[gdMaxColors];
421
    /* Truecolor flag and pixels. New 2.0 fields appear here at the
422
       end to minimize breakage of existing object code. */
423
    int trueColor;
424
    int **tpixels;
425
    /* Should alpha channel be copied, or applied, each time a
426
       pixel is drawn? This applies to truecolor images only.
427
       No attempt is made to alpha-blend in palette images,
428
       even if semitransparent palette entries exist.
429
       To do that, build your image as a truecolor image,
430
       then quantize down to 8 bits. */
431
    int alphaBlendingFlag;
432
    /* Should the alpha channel of the image be saved? This affects
433
       PNG at the moment; other future formats may also
434
       have that capability. JPEG doesn't. */
435
    int saveAlphaFlag;
436
437
    /* There should NEVER BE ACCESSOR MACROS FOR ITEMS BELOW HERE, so this
438
       part of the structure can be safely changed in new releases. */
439
440
    /* 2.0.12: anti-aliased globals. 2.0.26: just a few vestiges after
441
      switching to the fast, memory-cheap implementation from PHP-gd. */
442
    int AA;
443
    int AA_color;
444
    int AA_dont_blend;
445
446
    /* 2.0.12: simple clipping rectangle. These values
447
      must be checked for safety when set; please use
448
      gdImageSetClip */
449
    int cx1;
450
    int cy1;
451
    int cx2;
452
    int cy2;
453
454
    /* 2.1.0: allows to specify resolution in dpi */
455
    unsigned int res_x;
456
    unsigned int res_y;
457
458
    /* Selects quantization method, see gdImageTrueColorToPaletteSetMethod() and
459
     * gdPaletteQuantizationMethod enum. */
460
    int paletteQuantizationMethod;
461
    /* speed/quality trade-off. 1 = best quality, 10 = best speed. 0 =
462
       method-specific default. Applicable to GD_QUANT_LIQ and
463
       GD_QUANT_NEUQUANT. */
464
    int paletteQuantizationSpeed;
465
    /* Image will remain true-color if conversion to palette cannot achieve
466
       given quality. Value from 1 to 100, 1 = ugly, 100 = perfect. Applicable
467
       to GD_QUANT_LIQ.*/
468
    int paletteQuantizationMinQuality;
469
    /* Image will use minimum number of palette colors needed to achieve given
470
       quality. Must be higher than paletteQuantizationMinQuality Value from 1
471
       to 100, 1 = ugly, 100 = perfect. Applicable to GD_QUANT_LIQ.*/
472
    int paletteQuantizationMaxQuality;
473
    gdInterpolationMethod interpolation_id;
474
    interpolation_method interpolation;
475
} gdImage;
476
477
typedef gdImage *gdImagePtr;
478
479
typedef struct gdImageMetadata gdImageMetadata;
480
481
#define GD_META_OK 0
482
#define GD_META_ERR_FORMAT -1
483
#define GD_META_ERR_PARSE -2
484
#define GD_META_ERR_NOMEM -3
485
#define GD_META_ERR_LIMIT -4
486
#define GD_META_ERR_UNSUPPORTED -5
487
#define GD_META_ERR_INVALID -6
488
489
#define GD_METADATA_DEFAULT_MAX_PROFILE_SIZE ((size_t)64 * 1024 * 1024)
490
#define GD_METADATA_DEFAULT_MAX_TOTAL_SIZE ((size_t)256 * 1024 * 1024)
491
492
BGD_DECLARE(gdImageMetadata *) gdImageMetadataCreate(void);
493
BGD_DECLARE(void) gdImageMetadataFree(gdImageMetadata *metadata);
494
BGD_DECLARE(void) gdImageMetadataReset(gdImageMetadata *metadata);
495
BGD_DECLARE(int)
496
gdImageMetadataSetLimits(gdImageMetadata *metadata, size_t max_profile_size, size_t max_total_size);
497
BGD_DECLARE(void)
498
gdImageMetadataGetLimits(const gdImageMetadata *metadata, size_t *max_profile_size,
499
                         size_t *max_total_size);
500
BGD_DECLARE(int)
501
gdImageMetadataSetProfile(gdImageMetadata *metadata, const char *key, const unsigned char *data,
502
                          size_t size);
503
BGD_DECLARE(const unsigned char *)
504
gdImageMetadataGetProfile(const gdImageMetadata *metadata, const char *key, size_t *size);
505
BGD_DECLARE(int)
506
gdImageMetadataRemoveProfile(gdImageMetadata *metadata, const char *key);
507
BGD_DECLARE(size_t)
508
gdImageMetadataGetProfileCount(const gdImageMetadata *metadata);
509
BGD_DECLARE(int)
510
gdImageMetadataGetProfileAt(const gdImageMetadata *metadata, size_t index, const char **key,
511
                            const unsigned char **data, size_t *size);
512
513
/* Point type for use in polygon drawing. */
514
515
/**
516
 * @brief Defines a point in a 2D coordinate system using floating point values.
517
  */
518
typedef struct {
519
    double x, y; /**< Floating point coordinates. x increases from left to right, y increases from top to bottom */
520
} gdPointF, *gdPointFPtr;
521
522
/*
523
  Group: Types
524
525
  typedef: gdFont
526
527
  typedef: gdFontPtr
528
529
  A font structure, containing the bitmaps of all characters in a
530
  font.  Used to declare the characteristics of a font. Text-output
531
  functions expect these as their second argument, following the
532
  <gdImagePtr> argument.  <gdFontGetSmall> and <gdFontGetLarge> both
533
  return one.
534
535
  You can provide your own font data by providing such a structure and
536
  the associated pixel array. You can determine the width and height
537
  of a single character in a font by examining the w and h members of
538
  the structure. If you will not be creating your own fonts, you will
539
  not need to concern yourself with the rest of the components of this
540
  structure.
541
542
  Please see the files gdfontl.c and gdfontl.h for an example of
543
  the proper declaration of this structure.
544
545
  > typedef struct {
546
  >   // # of characters in font
547
  >   int nchars;
548
  >   // First character is numbered... (usually 32 = space)
549
  >   int offset;
550
  >   // Character width and height
551
  >   int w;
552
  >   int h;
553
  >   // Font data; array of characters, one row after another.
554
  >   // Easily included in code, also easily loaded from
555
  >   // data files.
556
  >   char *data;
557
  > } gdFont;
558
559
  gdFontPtr is a pointer to gdFont.
560
561
*/
562
typedef struct {
563
    /* # of characters in font */
564
    int nchars;
565
    /* First character is numbered... (usually 32 = space) */
566
    int offset;
567
    /* Character width and height */
568
    int w;
569
    int h;
570
    /* Font data; array of characters, one row after another.
571
       Easily included in code, also easily loaded from
572
       data files. */
573
    char *data;
574
} gdFont;
575
576
/* Text functions take these. */
577
typedef gdFont *gdFontPtr;
578
579
typedef void (*gdErrorMethod)(int, const char *, va_list);
580
581
BGD_DECLARE(void) gdSetErrorMethod(gdErrorMethod);
582
BGD_DECLARE(void) gdClearErrorMethod(void);
583
584
/**
585
 * Group: Colors
586
 *
587
 * Colors are always of type int which is supposed to be at least 32 bit large.
588
 *
589
 * Kinds of colors:
590
 *   true colors     - ARGB values where the alpha channel is stored as most
591
 *                     significant, and the blue channel as least significant
592
 *                     byte. Note that the alpha channel only uses the 7 least
593
 *                     significant bits.
594
 *                     Don't rely on the internal representation, though, and
595
 *                     use <gdTrueColorAlpha> to compose a truecolor value, and
596
 *                     <gdTrueColorGetAlpha>, <gdTrueColorGetRed>,
597
 *                     <gdTrueColorGetGreen> and <gdTrueColorGetBlue> to access
598
 *                     the respective channels.
599
 *   palette indexes - The index of a color palette entry (0-255).
600
 *   special colors  - As listed in the following section.
601
 *
602
 * Constants: Special Colors
603
 *   gdStyled        - use the current style, see <gdImageSetStyle>
604
 *   gdBrushed       - use the current brush, see <gdImageSetBrush>
605
 *   gdStyledBrushed - use the current style and brush
606
 *   gdTiled         - use the current tile, see <gdImageSetTile>
607
 *   gdTransparent   - indicate transparency, what is not the same as the
608
 *                     transparent color index; used for lines only
609
 *   gdAntiAliased   - draw anti aliased
610
 */
611
612
/* For backwards compatibility only. Use gdImageSetStyle()
613
   for MUCH more flexible line drawing. Also see
614
   gdImageSetBrush(). */
615
0
#define gdDashSize 4
616
1.38k
#define gdStyled (-2)
617
6.12k
#define gdBrushed (-3)
618
224
#define gdStyledBrushed (-4)
619
792
#define gdTiled (-5)
620
621
/* NOT the same as the transparent color index.
622
        This is used in line styles only. */
623
0
#define gdTransparent (-6)
624
625
266k
#define gdAntiAliased (-7)
626
627
/* Functions to manipulate images. */
628
629
/* Creates a palette-based image (up to 256 colors). */
630
BGD_DECLARE(gdImagePtr) gdImageCreate(int sx, int sy);
631
632
/* An alternate name for the above (2.0). */
633
#define gdImageCreatePalette gdImageCreate
634
635
/* Creates a truecolor image (millions of colors). */
636
BGD_DECLARE(gdImagePtr) gdImageCreateTrueColor(int sx, int sy);
637
638
/* Creates an image from various file types. These functions
639
   return a palette or truecolor image based on the
640
   nature of the file being loaded. Truecolor PNG
641
   stays truecolor; palette PNG stays palette-based;
642
   JPEG is always truecolor. */
643
/**
644
 * @defgroup gdCodecs Codecs
645
 * @brief Image codec support for reading and writing various file formats.
646
 *
647
 * GD supports a range of raster codecs for loading and saving images,
648
 * including JPEG, PNG, GIF, WebP, and others. Each codec is exposed as its
649
 * own subgroup with format-specific options and functions.
650
 */
651
652
/**
653
 * @defgroup gdCodecPng PNG
654
 * @brief PNG image reading and writing support.
655
 * @ingroup gdCodecs
656
 *
657
 * PNG support preserves palette images as palette-based gd images and reads
658
 * truecolor PNG data as truecolor gd images. PNG output is palette-aware,
659
 * supports alpha, metadata, compression settings, and libpng filter options.
660
 *
661
 * @code{.c}
662
 *        gdImagePtr im;
663
 *        int black, white;
664
 *        FILE *out;
665
 *
666
 *        im = gdImageCreate(100, 100);
667
 *        if (im == NULL) {
668
 *          fprintf(stderr, "Unable to create image\n");
669
 *          exit(1);
670
 *        }
671
 *
672
 *        white = gdImageColorAllocate(im, 255, 255, 255);
673
 *        black = gdImageColorAllocate(im, 0, 0, 0);
674
 *        gdImageRectangle(im, 0, 0, 99, 99, black);
675
 *
676
 *        out = fopen("rect.png", "wb");
677
 *        if (out == NULL) {
678
 *           fprintf(stderr, "Unable to open output file\n");
679
 *           gdImageDestroy(im);
680
 *           exit(1);
681
 *        }
682
 *
683
 *        gdImagePngEx(im, out, 9);
684
 *        fclose(out);
685
 *        gdImageDestroy(im);
686
 * @endcode
687
 *
688
 * @{
689
 */
690
691
/**
692
 * @brief Create an image from a PNG stdio file.
693
 *
694
 * @param fd Pointer to the input FILE stream.
695
 *
696
 * @return Returns a gdImagePtr on success, or NULL on failure.
697
 */
698
BGD_DECLARE(gdImagePtr) gdImageCreateFromPng(FILE *fd);
699
700
/**
701
 * @brief Create an image from PNG data read through a gdIOCtx.
702
 *
703
 * @param in Pointer to the gdIOCtx input context.
704
 *
705
 * @return Returns a gdImagePtr on success, or NULL on failure.
706
 */
707
BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx(gdIOCtxPtr in);
708
709
/**
710
 * @brief Create an image from a PNG memory buffer.
711
 *
712
 * @param size Size of the PNG memory buffer in bytes.
713
 * @param data Pointer to the PNG memory buffer.
714
 *
715
 * @return Returns a gdImagePtr on success, or NULL on failure.
716
 */
717
BGD_DECLARE(gdImagePtr) gdImageCreateFromPngPtr(int size, void *data);
718
719
/**
720
 * @brief Write an image as PNG data to a stdio file.
721
 *
722
 * @param im The image to write.
723
 * @param out The stdio file to write the PNG data to.
724
 */
725
BGD_DECLARE(void) gdImagePng(gdImagePtr im, FILE *out);
726
727
/**
728
 * @brief Write an image as PNG data to a gdIOCtx.
729
 *
730
 * @param im The image to write.
731
 * @param out The gdIOCtx to write the PNG data to.
732
 */
733
BGD_DECLARE(void) gdImagePngCtx(gdImagePtr im, gdIOCtxPtr out);
734
735
/* 2.0.12: Compression level: 0-9 or -1, where 0 is NO COMPRESSION at all,
736
   1 is FASTEST but produces larger files, 9 provides the best
737
   compression (smallest files) but takes a long time to compress, and
738
   -1 selects the default compiled into the zlib library. */
739
/**
740
 * @brief Write an image as PNG data to a stdio file with a compression level.
741
 *
742
 * @param im The image to write.
743
 * @param out The stdio file to write the PNG data to.
744
 * @param level Compression level: 0 for no compression, 1-9 for zlib levels, or -1 for the default.
745
 */
746
BGD_DECLARE(void) gdImagePngEx(gdImagePtr im, FILE *out, int level);
747
748
/**
749
 * @brief Write an image as PNG data to a gdIOCtx with a compression level.
750
 *
751
 * @param im The image to write.
752
 * @param out The gdIOCtx to write the PNG data to.
753
 * @param level Compression level: 0 for no compression, 1-9 for zlib levels, or -1 for the default.
754
 */
755
BGD_DECLARE(void) gdImagePngCtxEx(gdImagePtr im, gdIOCtxPtr out, int level);
756
757
/* Best to free this memory with gdFree(), not free() */
758
/**
759
 * @brief Write an image as PNG data to a newly allocated memory buffer.
760
 *
761
 * @param im The image to write.
762
 * @param size Pointer to an integer that receives the returned buffer size.
763
 *
764
 * @return A pointer to the newly allocated PNG data, or NULL on failure.
765
 */
766
BGD_DECLARE(void *) gdImagePngPtr(gdImagePtr im, int *size);
767
768
/**
769
 * @brief Write an image as PNG data to a memory buffer with a compression level.
770
 *
771
 * @param im The image to write.
772
 * @param size Pointer to an integer that receives the returned buffer size.
773
 * @param level Compression level: 0 for no compression, 1-9 for zlib levels, or -1 for the default.
774
 *
775
 * @return A pointer to the newly allocated PNG data, or NULL on failure.
776
 */
777
BGD_DECLARE(void *) gdImagePngPtrEx(gdImagePtr im, int *size, int level);
778
779
/** Let libpng choose PNG row filters automatically. */
780
#define GD_PNG_FILTER_AUTO 0U
781
/** Enable the PNG "None" row filter. */
782
#define GD_PNG_FILTER_NONE (1U << 0)
783
/** Enable the PNG "Sub" row filter. */
784
#define GD_PNG_FILTER_SUB (1U << 1)
785
/** Enable the PNG "Up" row filter. */
786
#define GD_PNG_FILTER_UP (1U << 2)
787
/** Enable the PNG "Average" row filter. */
788
#define GD_PNG_FILTER_AVERAGE (1U << 3)
789
/** Enable the PNG "Paeth" row filter. */
790
#define GD_PNG_FILTER_PAETH (1U << 4)
791
/** Enable all PNG row filters. */
792
#define GD_PNG_FILTER_ALL                                                                          \
793
    (GD_PNG_FILTER_NONE | GD_PNG_FILTER_SUB | GD_PNG_FILTER_UP | GD_PNG_FILTER_AVERAGE |           \
794
     GD_PNG_FILTER_PAETH)
795
796
/**
797
 * @brief PNG compression strategy values for gdPngWriteOptions.
798
 */
799
enum {
800
    GD_PNG_COMPRESSION_STRATEGY_DEFAULT = 0, /**< Use zlib's default strategy. */
801
    GD_PNG_COMPRESSION_STRATEGY_FILTERED,    /**< Prefer zlib's filtered-data strategy. */
802
    GD_PNG_COMPRESSION_STRATEGY_HUFFMAN_ONLY, /**< Use Huffman coding only. */
803
    GD_PNG_COMPRESSION_STRATEGY_RLE,         /**< Use zlib's run-length encoding strategy. */
804
    GD_PNG_COMPRESSION_STRATEGY_FIXED        /**< Use zlib's fixed Huffman codes strategy. */
805
};
806
807
/**
808
 * @brief Options for writing PNG data.
809
 */
810
typedef struct {
811
    int compression_level;           /**< PNG compression level: 0-9, or -1 for the zlib default. */
812
    unsigned int filters;            /**< Bitmask of GD_PNG_FILTER_* constants. */
813
    int compression_strategy;        /**< One of the GD_PNG_COMPRESSION_STRATEGY_* constants. */
814
    const gdImageMetadata *metadata; /**< Optional metadata to embed in the PNG. */
815
    unsigned int resolution_x;       /**< Horizontal resolution in DPI, or 0 to use the gdImage value. */
816
    unsigned int resolution_y;       /**< Vertical resolution in DPI, or 0 to use the gdImage value. */
817
} gdPngWriteOptions;
818
819
/**
820
 * @brief Basic information read from a PNG stream.
821
 *
822
 * PNG stores physical pixel density in the pHYs chunk as two raw
823
 * pixels-per-unit values plus a unit flag. When physical_unit is
824
 * PNG_RESOLUTION_METER, x_pixels_per_unit and y_pixels_per_unit are pixels
825
 * per meter and resolution_x/resolution_y contain the converted DPI values.
826
 * When physical_unit is PNG_RESOLUTION_UNKNOWN, the raw values describe pixel
827
 * aspect ratio only and resolution_x/resolution_y remain -1.
828
 */
829
typedef struct {
830
    int width;                   /**< Image width in pixels. */
831
    int height;                  /**< Image height in pixels. */
832
    int bit_depth;               /**< PNG bit depth from the IHDR chunk. */
833
    int color_type;              /**< PNG color type from the IHDR chunk. */
834
    int has_alpha;               /**< Non-zero if the PNG color type includes alpha. */
835
    int has_transparency;        /**< Non-zero if a tRNS transparency chunk is present. */
836
    int palette_entries;         /**< Number of palette entries, or -1 if no PLTE chunk was read. */
837
    int interlace_method;        /**< PNG interlace method from the IHDR chunk. */
838
    int x_pixels_per_unit;       /**< Raw pHYs horizontal pixels per unit, or -1 if not available. */
839
    int y_pixels_per_unit;       /**< Raw pHYs vertical pixels per unit, or -1 if not available. */
840
    int physical_unit;           /**< pHYs unit flag: PNG_RESOLUTION_UNKNOWN, PNG_RESOLUTION_METER, or -1. */
841
    gdImageMetadata *metadata;   /**< Optional metadata object populated while probing. */
842
    int decoded_truecolor;       /**< Non-zero if gd decodes this PNG as truecolor. */
843
    int resolution_x;            /**< Horizontal DPI converted from meter pHYs, or -1 if not available. */
844
    int resolution_y;            /**< Vertical DPI converted from meter pHYs, or -1 if not available. */
845
} gdPngInfo;
846
847
/**
848
 * @brief Initialize PNG write options with default values.
849
 *
850
 * @param options Pointer to the gdPngWriteOptions structure to initialize.
851
 */
852
BGD_DECLARE(void) gdPngWriteOptionsInit(gdPngWriteOptions *options);
853
854
/**
855
 * @brief Initialize a gdPngInfo structure with default values.
856
 *
857
 * @param info Pointer to the gdPngInfo structure to initialize.
858
 */
859
BGD_DECLARE(void) gdPngInfoInit(gdPngInfo *info);
860
861
/**
862
 * @brief Write an image as PNG data to a stdio file using write options.
863
 *
864
 * @param im The image to write.
865
 * @param out The stdio file to write the PNG data to.
866
 * @param options Pointer to a gdPngWriteOptions structure, or NULL for defaults.
867
 *
868
 * @return Returns 0 on success, or 1 on failure.
869
 */
870
BGD_DECLARE(int) gdImagePngWithOptions(gdImagePtr im, FILE *out, const gdPngWriteOptions *options);
871
872
/**
873
 * @brief Write an image as PNG data to a gdIOCtx using write options.
874
 *
875
 * @param im The image to write.
876
 * @param out The gdIOCtx to write the PNG data to.
877
 * @param options Pointer to a gdPngWriteOptions structure, or NULL for defaults.
878
 *
879
 * @return Returns 0 on success, or 1 on failure.
880
 */
881
BGD_DECLARE(int)
882
gdImagePngCtxWithOptions(gdImagePtr im, gdIOCtxPtr out, const gdPngWriteOptions *options);
883
884
/**
885
 * @brief Write an image as PNG data to a memory buffer using write options.
886
 *
887
 * @param im The image to write.
888
 * @param size Pointer to an integer that receives the returned buffer size.
889
 * @param options Pointer to a gdPngWriteOptions structure, or NULL for defaults.
890
 *
891
 * @return A pointer to the newly allocated PNG data, or NULL on failure.
892
 */
893
BGD_DECLARE(void *)
894
gdImagePngPtrWithOptions(gdImagePtr im, int *size, const gdPngWriteOptions *options);
895
896
/**
897
 * @brief Read PNG header information from a stdio file.
898
 *
899
 * @param in Pointer to the input FILE stream.
900
 * @param info Pointer to the gdPngInfo structure to populate.
901
 *
902
 * @return Returns 0 on success, or 1 on failure.
903
 */
904
BGD_DECLARE(int) gdPngGetInfo(FILE *in, gdPngInfo *info);
905
906
/**
907
 * @brief Read PNG header information from a gdIOCtx.
908
 *
909
 * @param in Pointer to the gdIOCtx input context.
910
 * @param info Pointer to the gdPngInfo structure to populate.
911
 *
912
 * @return Returns 0 on success, or 1 on failure.
913
 */
914
BGD_DECLARE(int) gdPngGetInfoCtx(gdIOCtxPtr in, gdPngInfo *info);
915
916
/**
917
 * @brief Read PNG header information from a memory buffer.
918
 *
919
 * @param size Size of the PNG memory buffer in bytes.
920
 * @param data Pointer to the PNG memory buffer.
921
 * @param info Pointer to the gdPngInfo structure to populate.
922
 *
923
 * @return Returns 0 on success, or 1 on failure.
924
 */
925
BGD_DECLARE(int) gdPngGetInfoPtr(int size, const void *data, gdPngInfo *info);
926
927
/**
928
 * @brief Return a string describing the linked libpng version.
929
 *
930
 * @return Returns the linked libpng version string.
931
 */
932
BGD_DECLARE(const char *) gdPngGetVersionString(void);
933
/** @} */
934
935
/**
936
 * @defgroup gdCodecQoi QOI
937
 * @brief QOI image reading and writing support.
938
 * @ingroup gdCodecs
939
 *
940
 * QOI support reads images as truecolor RGBA gd images with alpha saving
941
 * enabled. QOI output writes RGBA data for both truecolor and palette images;
942
 * palette images are expanded through their color table. The colorspace value
943
 * controls the QOI header colorspace flag and does not transform pixel data.
944
 *
945
 * @code{.c}
946
 *        gdImagePtr im;
947
 *        int black, white;
948
 *        FILE *out;
949
 *
950
 *        im = gdImageCreateTrueColor(100, 100);
951
 *        if (im == NULL) {
952
 *          fprintf(stderr, "Unable to create image\n");
953
 *          exit(1);
954
 *        }
955
 *
956
 *        white = gdTrueColor(255, 255, 255);
957
 *        black = gdTrueColor(0, 0, 0);
958
 *        gdImageFilledRectangle(im, 0, 0, 99, 99, white);
959
 *        gdImageRectangle(im, 0, 0, 99, 99, black);
960
 *
961
 *        out = fopen("rect.qoi", "wb");
962
 *        if (out == NULL) {
963
 *           fprintf(stderr, "Unable to open output file\n");
964
 *           gdImageDestroy(im);
965
 *           exit(1);
966
 *        }
967
 *
968
 *        gdImageQoi(im, out);
969
 *        fclose(out);
970
 *        gdImageDestroy(im);
971
 * @endcode
972
 *
973
 * @{
974
 */
975
976
/**
977
 * @brief Create an image from a QOI stdio file.
978
 *
979
 * @param fd Pointer to the input FILE stream.
980
 *
981
 * @return Returns a gdImagePtr on success, or NULL on failure.
982
 */
983
BGD_DECLARE(gdImagePtr) gdImageCreateFromQoi(FILE *fd);
984
985
/**
986
 * @brief Create an image from QOI data read through a gdIOCtx.
987
 *
988
 * @param in Pointer to the gdIOCtx input context.
989
 *
990
 * @return Returns a gdImagePtr on success, or NULL on failure.
991
 */
992
BGD_DECLARE(gdImagePtr) gdImageCreateFromQoiCtx(gdIOCtxPtr in);
993
994
/**
995
 * @brief Create an image from a QOI memory buffer.
996
 *
997
 * @param size Size of the QOI memory buffer in bytes.
998
 * @param data Pointer to the QOI memory buffer.
999
 *
1000
 * @return Returns a gdImagePtr on success, or NULL on failure.
1001
 */
1002
BGD_DECLARE(gdImagePtr) gdImageCreateFromQoiPtr(int size, void *data);
1003
1004
/**
1005
 * @brief Information read from a QOI data
1006
 */
1007
typedef struct {
1008
    unsigned int width; /**< Image width in pixels. */
1009
    unsigned int height; /**< Image height in pixels. */
1010
    int channels; /**< Number of color channels (3 for RGB, 4 for RGBA). */
1011
    int colorspace; /**< QOI colorspace flag (GD_QOI_SRGB or GD_QOI_LINEAR). */
1012
} gdQoiInfo;
1013
1014
/**
1015
 * @brief Initialize a gdQoiInfo structure to default values.
1016
 *
1017
 * The default may change in future versions, so it is recommended to call this function before using the structure.
1018
 * Default values update is not considered a breaking change, but it is still recommended to call this function to ensure proper initialization.
1019
 *
1020
 * @param info Pointer to the gdQoiInfo structure to initialize.
1021
 */
1022
BGD_DECLARE(void) gdQoiInfoInit(gdQoiInfo *info);
1023
1024
/**
1025
 * @brief Read QOI header information from a stdio file.
1026
 *
1027
 * @param infile Pointer to the input FILE stream.
1028
 * @param info Pointer to the gdQoiInfo structure to populate.
1029
 *
1030
 * @return Returns 0 on success, or 1 on failure.
1031
 */
1032
BGD_DECLARE(int) gdQoiGetInfo(FILE *infile, gdQoiInfo *info);
1033
1034
/**
1035
 * @brief Read QOI header information from a gdIOCtx.
1036
 *
1037
 * @param infile Pointer to the gdIOCtx input context.
1038
 * @param info Pointer to the gdQoiInfo structure to populate.
1039
 *
1040
 * @return Returns 0 on success, or 1 on failure.
1041
 */
1042
BGD_DECLARE(int) gdQoiGetInfoCtx(gdIOCtxPtr infile, gdQoiInfo *info);
1043
1044
/**
1045
 * @brief Read QOI header information from a memory buffer.
1046
 *
1047
 * @param size Size of the QOI memory buffer in bytes.
1048
 * @param data Pointer to the QOI memory buffer.
1049
 * @param info Pointer to the gdQoiInfo structure to populate.
1050
 *
1051
 * @return Returns 0 on success, or 1 on failure.
1052
 */
1053
BGD_DECLARE(int) gdQoiGetInfoPtr(int size, const void *data, gdQoiInfo *info);
1054
1055
/**
1056
 * @brief Options for writing QOI data.
1057
 */
1058
typedef struct {
1059
    int colorspace; /**< QOI colorspace flag, either GD_QOI_SRGB or GD_QOI_LINEAR. */
1060
    const gdImageMetadata *metadata; /**< Optional metadata, ignored by QOI. */
1061
} gdQoiWriteOptions;
1062
1063
/**
1064
 * @brief Initialize a gdQoiWriteOptions structure to default values.
1065
 *
1066
 * The default may change in future versions, so it is recommended to call this function before using the structure.
1067
 * Default values update is not considered a breaking change, but it is still recommended to call this function to ensure proper initialization.
1068
 *
1069
 * @param options Pointer to the gdQoiWriteOptions structure to initialize.
1070
 */
1071
BGD_DECLARE(void) gdQoiWriteOptionsInit(gdQoiWriteOptions *options);
1072
1073
/**
1074
 * @brief Write an image as QOI data to a stdio file with options.
1075
 *
1076
 * @param im The image to write.
1077
 * @param out The stdio file to write the QOI data to.
1078
 * @param options Pointer to the gdQoiWriteOptions structure specifying write options.
1079
 *
1080
 * @return Returns 0 on success, or 1 on failure.
1081
 */
1082
BGD_DECLARE(int)
1083
gdImageQoiWithOptions(gdImagePtr im, FILE *out, const gdQoiWriteOptions *options);
1084
1085
/**
1086
 * @brief Write an image as QOI data to a gdIOCtx with options.
1087
 *
1088
 * @param im The image to write.
1089
 * @param out The gdIOCtx to write the QOI data to.
1090
 * @param options Pointer to the gdQoiWriteOptions structure specifying write options.
1091
 *
1092
 * @return Returns 0 on success, or 1 on failure.
1093
 */
1094
BGD_DECLARE(int)
1095
gdImageQoiCtxWithOptions(gdImagePtr im, gdIOCtxPtr out, const gdQoiWriteOptions *options);
1096
1097
/**
1098
 * @brief Write an image as QOI data to a newly allocated memory buffer with options.
1099
 *
1100
 * @param im The image to write.
1101
 * @param size Pointer to an integer that receives the returned buffer size.
1102
 * @param options Pointer to the gdQoiWriteOptions structure specifying write options.
1103
 *
1104
 * @return A pointer to the newly allocated QOI data, or NULL on failure.
1105
 */
1106
BGD_DECLARE(void *)
1107
gdImageQoiPtrWithOptions(gdImagePtr im, int *size, const gdQoiWriteOptions *options);
1108
1109
/**
1110
 * @brief Write an image as QOI data to a newly allocated memory buffer.
1111
 *
1112
 * @param im The image to write.
1113
 * @param size Pointer to an integer that receives the returned buffer size.
1114
 *
1115
 * @return A pointer to the newly allocated QOI data, or NULL on failure.
1116
 */
1117
BGD_DECLARE(void *) gdImageQoiPtr(gdImagePtr im, int *size);
1118
1119
/**
1120
 * @brief Write an image as QOI data to a memory buffer with an explicit colorspace flag.
1121
 *
1122
 * @param im The image to write.
1123
 * @param size Pointer to an integer that receives the returned buffer size.
1124
 * @param colorspace The QOI colorspace flag, either GD_QOI_SRGB or GD_QOI_LINEAR.
1125
 *
1126
 * @return A pointer to the newly allocated QOI data, or NULL on failure.
1127
 */
1128
BGD_DECLARE(void *) gdImageQoiPtrEx(gdImagePtr im, int *size, int colorspace);
1129
1130
/**
1131
 * @brief Write an image as QOI data to a memory buffer.
1132
 *
1133
 * @param im The image to write.
1134
 * @param size Pointer to an integer that receives the returned buffer size.
1135
 * @param metadata Reserved metadata input parameter; QOI metadata is currently ignored.
1136
 *
1137
 * @return A pointer to the newly allocated QOI data, or NULL on failure.
1138
 */
1139
/**
1140
 * @brief Write an image as QOI data to a stdio file.
1141
 *
1142
 * @param im The image to write.
1143
 * @param out The stdio file to write the QOI data to.
1144
 */
1145
BGD_DECLARE(void) gdImageQoi(gdImagePtr im, FILE *out);
1146
1147
/**
1148
 * @brief Write an image as QOI data to a gdIOCtx.
1149
 *
1150
 * @param im The image to write.
1151
 * @param out The gdIOCtx to write the QOI data to.
1152
 */
1153
BGD_DECLARE(void) gdImageQoiCtx(gdImagePtr im, gdIOCtxPtr out);
1154
1155
/**
1156
 * @brief QOI colorspace flags written to the QOI header.
1157
 */
1158
enum {
1159
    GD_QOI_SRGB = 0,  /**< Pixel data is encoded with sRGB transfer characteristics. */
1160
    GD_QOI_LINEAR = 1 /**< Pixel data is encoded with linear transfer characteristics. */
1161
};
1162
1163
/**
1164
 * @brief Write an image as QOI data to a stdio file with an explicit colorspace flag.
1165
 *
1166
 * @param im The image to write.
1167
 * @param out The stdio file to write the QOI data to.
1168
 */
1169
BGD_DECLARE(void) gdImageQoi(gdImagePtr im, FILE *out);
1170
1171
/**
1172
 * @brief Write an image as QOI data to a gdIOCtx with an explicit colorspace flag.
1173
 *
1174
 * @param im The image to write.
1175
 * @param out The gdIOCtx to write the QOI data to.
1176
  */
1177
BGD_DECLARE(void) gdImageQoiCtx(gdImagePtr im, gdIOCtxPtr out);
1178
1179
/**
1180
 * @brief Write an image as QOI data to a stdio file with an explicit colorspace flag.
1181
 *
1182
 * @param im The image to write.
1183
 * @param out The stdio file to write the QOI data to.
1184
 * @param colorspace The QOI colorspace flag, either GD_QOI_SRGB or GD_QOI_LINEAR.
1185
 */
1186
BGD_DECLARE(void) gdImageQoiEx(gdImagePtr im, FILE *out, int colorspace);
1187
1188
/**
1189
 * @brief Write an image as QOI data to a gdIOCtx with an explicit colorspace flag.
1190
 *
1191
 * @param im The image to write.
1192
 * @param out The gdIOCtx to write the QOI data to.
1193
 * @param colorspace The QOI colorspace flag, either GD_QOI_SRGB or GD_QOI_LINEAR.
1194
 */
1195
BGD_DECLARE(void)
1196
gdImageQoiCtxEx(gdImagePtr im, gdIOCtxPtr out, int colorspace);
1197
1198
/** @} */
1199
1200
/**
1201
 * @defgroup gdCodecGif GIF
1202
 * @brief GIF image and animation reading and writing support.
1203
 * @ingroup gdCodecs
1204
 *
1205
 * GIF support reads single images as palette-based gd images and writes
1206
 * palette-based GIF data, quantizing truecolor input when needed. Animated GIF
1207
 * support includes a reader for raw frames or composited images and a legacy
1208
 * begin/add/end writer API.
1209
 *
1210
 * @code{.c}
1211
 *        gdImagePtr im;
1212
 *        gdImagePtr prev = NULL;
1213
 *        FILE *out;
1214
 *        int i;
1215
 *
1216
 *        im = gdImageCreate(100, 100);
1217
 *        if (im == NULL) {
1218
 *          fprintf(stderr, "Unable to create image\n");
1219
 *          exit(1);
1220
 *        }
1221
 *
1222
 *        gdImageColorAllocate(im, 255, 255, 255);
1223
 *
1224
 *        out = fopen("anim.gif", "wb");
1225
 *        if (out == NULL) {
1226
 *           fprintf(stderr, "Unable to open output file\n");
1227
 *           gdImageDestroy(im);
1228
 *           exit(1);
1229
 *        }
1230
 *
1231
 *        gdImageGifAnimBegin(im, out, 1, -1);
1232
 *        for (i = 0; i < 20; i++) {
1233
 *          gdImagePtr frame;
1234
 *          int color;
1235
 *
1236
 *          frame = gdImageCreate(100, 100);
1237
 *          if (frame == NULL) {
1238
 *            break;
1239
 *          }
1240
 *          gdImageColorAllocate(frame, 255, 255, 255);
1241
 *          color = gdImageColorAllocate(frame, i * 10, 0, 255 - i * 10);
1242
 *          gdImageFilledRectangle(frame, 10 + i, 10 + i, 40 + i, 40 + i, color);
1243
 *          gdImageGifAnimAdd(frame, out, 1, 0, 0, 10, GD_GIF_DISPOSAL_NONE, prev);
1244
 *          if (prev != NULL) {
1245
 *            gdImageDestroy(prev);
1246
 *          }
1247
 *          prev = frame;
1248
 *        }
1249
 *        if (prev != NULL) {
1250
 *          gdImageDestroy(prev);
1251
 *        }
1252
 *        gdImageGifAnimEnd(out);
1253
 *        fclose(out);
1254
 *        gdImageDestroy(im);
1255
 * @endcode
1256
 *
1257
 * @{
1258
 */
1259
1260
/**
1261
 * @name Single-frame GIF reading and writing
1262
 * @{
1263
 */
1264
1265
/**
1266
 * @brief Create an image from the first frame of a GIF stdio file.
1267
 *
1268
 * @param fd Pointer to the input FILE stream.
1269
 *
1270
 * @return Returns a caller-owned gdImagePtr on success, or NULL on failure.
1271
 */
1272
BGD_DECLARE(gdImagePtr) gdImageCreateFromGif(FILE *fd);
1273
1274
/**
1275
 * @brief Create an image from the first frame of GIF data read through a gdIOCtx.
1276
 *
1277
 * @param in Pointer to the gdIOCtx input context.
1278
 *
1279
 * @return Returns a caller-owned gdImagePtr on success, or NULL on failure.
1280
 */
1281
BGD_DECLARE(gdImagePtr) gdImageCreateFromGifCtx(gdIOCtxPtr in);
1282
1283
/**
1284
 * @brief Create an image from the first frame of a GIF memory buffer.
1285
 *
1286
 * @param size Size of the GIF memory buffer in bytes.
1287
 * @param data Pointer to the GIF memory buffer.
1288
 *
1289
 * @return Returns a caller-owned gdImagePtr on success, or NULL on failure.
1290
 */
1291
BGD_DECLARE(gdImagePtr) gdImageCreateFromGifPtr(int size, void *data);
1292
1293
/**
1294
 * @brief Write an image as GIF data to a gdIOCtx.
1295
 *
1296
 * @param im The image to write.
1297
 * @param out The gdIOCtx to write the GIF data to.
1298
 */
1299
BGD_DECLARE(void) gdImageGifCtx(gdImagePtr im, gdIOCtxPtr out);
1300
1301
/**
1302
 * @brief Write an image as GIF data to a stdio file.
1303
 *
1304
 * @param im The image to write.
1305
 * @param out The stdio file to write the GIF data to.
1306
 */
1307
BGD_DECLARE(void) gdImageGif(gdImagePtr im, FILE *out);
1308
1309
/**
1310
 * @brief Write an image as GIF data to a newly allocated memory buffer.
1311
 *
1312
 * @param im The image to write.
1313
 * @param size Pointer to an integer that receives the returned buffer size.
1314
 *
1315
 * @return A pointer to the newly allocated GIF data, or NULL on failure. Free
1316
 *         the returned buffer with gdFree().
1317
 */
1318
BGD_DECLARE(void *) gdImageGifPtr(gdImagePtr im, int *size);
1319
1320
/** @} */
1321
1322
/**
1323
 * @name Animated GIF reading
1324
 * @{
1325
 */
1326
1327
/**
1328
 * @brief Opaque animated GIF reader handle.
1329
 */
1330
typedef struct gdGifReadStruct *gdGifReadPtr;
1331
1332
/**
1333
 * @brief Basic information read from a GIF stream.
1334
 */
1335
typedef struct {
1336
    char version[4];       /**< GIF version, excluding the terminating NUL. */
1337
    int width;            /**< Logical screen width in pixels. */
1338
    int height;           /**< Logical screen height in pixels. */
1339
    int background_index;      /**< GIF logical screen background color index. */
1340
    int global_color_table;    /**< Non-zero if the GIF has a global color table. */
1341
    int color_resolution;      /**< GIF color resolution in bits per primary color. */
1342
    double pixel_aspect_ratio; /**< GIF pixel aspect ratio, or 1.0 when unspecified. */
1343
    int loop_count;            /**< Netscape loop count, 0 for infinite, or 1 when absent. */
1344
    int loop_count_present;    /**< Non-zero if a Netscape loop count was present. */
1345
} gdGifInfo;
1346
1347
/**
1348
 * @brief Per-frame information read from a GIF animation.
1349
 */
1350
typedef struct {
1351
    int frame_index;       /**< Zero-based frame index. */
1352
    int x;                /**< Frame left offset on the logical screen. */
1353
    int y;                /**< Frame top offset on the logical screen. */
1354
    int width;            /**< Frame width in pixels. */
1355
    int height;           /**< Frame height in pixels. */
1356
    int delay;            /**< Frame delay in hundredths of a second. */
1357
    int disposal;         /**< One of the GD_GIF_DISPOSAL_* constants. */
1358
    int transparent_index; /**< Transparent color index, or -1 if not present. */
1359
    int local_color_table; /**< Non-zero if this frame has a local color table. */
1360
    int interlace;        /**< Non-zero if this frame is interlaced. */
1361
} gdGifFrameInfo;
1362
1363
/**
1364
 * @brief Test whether a seekable GIF stdio file contains more than one frame.
1365
 *
1366
 * @param fd Pointer to the input FILE stream.
1367
 *
1368
 * @return Returns 1 if animated, 0 if readable but not animated, or -1 on error.
1369
 */
1370
BGD_DECLARE(int) gdGifIsAnimated(FILE *fd);
1371
1372
/**
1373
 * @brief Test whether a seekable GIF gdIOCtx contains more than one frame.
1374
 *
1375
 * @param in Pointer to the gdIOCtx input context.
1376
 *
1377
 * @return Returns 1 if animated, 0 if readable but not animated, or -1 on error.
1378
 */
1379
BGD_DECLARE(int) gdGifIsAnimatedCtx(gdIOCtxPtr in);
1380
1381
/**
1382
 * @brief Test whether a GIF memory buffer contains more than one frame.
1383
 *
1384
 * @param size Size of the GIF memory buffer in bytes.
1385
 * @param data Pointer to the GIF memory buffer.
1386
 *
1387
 * @return Returns 1 if animated, 0 if readable but not animated, or -1 on error.
1388
 */
1389
BGD_DECLARE(int) gdGifIsAnimatedPtr(int size, void *data);
1390
1391
/**
1392
 * @brief Open an animated GIF reader from a stdio file.
1393
 *
1394
 * @param fd Pointer to the input FILE stream.
1395
 *
1396
 * @return Returns a gdGifReadPtr on success, or NULL on failure. Close it with
1397
 *         gdGifReadClose().
1398
 */
1399
BGD_DECLARE(gdGifReadPtr) gdGifReadOpen(FILE *fd);
1400
1401
/**
1402
 * @brief Open an animated GIF reader from a gdIOCtx.
1403
 *
1404
 * @param in Pointer to the gdIOCtx input context. The reader does not take
1405
 *        ownership of this context.
1406
 *
1407
 * @return Returns a gdGifReadPtr on success, or NULL on failure. Close it with
1408
 *         gdGifReadClose().
1409
 */
1410
BGD_DECLARE(gdGifReadPtr) gdGifReadOpenCtx(gdIOCtxPtr in);
1411
1412
/**
1413
 * @brief Open an animated GIF reader from a memory buffer.
1414
 *
1415
 * @param size Size of the GIF memory buffer in bytes.
1416
 * @param data Pointer to the GIF memory buffer.
1417
 *
1418
 * @return Returns a gdGifReadPtr on success, or NULL on failure. Close it with
1419
 *         gdGifReadClose().
1420
 */
1421
BGD_DECLARE(gdGifReadPtr) gdGifReadOpenPtr(int size, void *data);
1422
1423
/**
1424
 * @brief Close an animated GIF reader.
1425
 *
1426
 * @param gif The GIF reader to close.
1427
 */
1428
BGD_DECLARE(void) gdGifReadClose(gdGifReadPtr gif);
1429
1430
/**
1431
 * @brief Read logical screen and loop information from a GIF reader.
1432
 *
1433
 * @param gif The GIF reader.
1434
 * @param info Pointer to the gdGifInfo structure to populate.
1435
 *
1436
 * @return Returns 1 on success, or 0 on failure.
1437
 */
1438
BGD_DECLARE(int) gdGifReadGetInfo(gdGifReadPtr gif, gdGifInfo *info);
1439
1440
/**
1441
 * @brief Read logical screen and loop information from a GIF stdio file.
1442
 *
1443
 * The input stream position is restored before returning.
1444
 */
1445
BGD_DECLARE(int) gdGifGetInfo(FILE *file, gdGifInfo *info);
1446
1447
/**
1448
 * @brief Read logical screen and loop information from a seekable gdIOCtx.
1449
 *
1450
 * The input context position is restored before returning.
1451
 */
1452
BGD_DECLARE(int) gdGifGetInfoCtx(gdIOCtxPtr input, gdGifInfo *info);
1453
1454
/**
1455
 * @brief Read logical screen and loop information from a GIF memory buffer.
1456
 */
1457
BGD_DECLARE(int) gdGifGetInfoPtr(int size, const void *data, gdGifInfo *info);
1458
1459
/**
1460
 * @brief Read the next raw GIF frame.
1461
 *
1462
 * @param gif The GIF reader.
1463
 * @param info Pointer to a gdGifFrameInfo structure to populate, or NULL.
1464
 * @param frame Pointer to receive a caller-owned raw frame image, or NULL to
1465
 *        skip receiving the image.
1466
 *
1467
 * @return Returns 1 when a frame is read, 0 at end of stream, or -1 on error.
1468
 */
1469
BGD_DECLARE(int)
1470
gdGifReadNextFrame(gdGifReadPtr gif, gdGifFrameInfo *info, gdImagePtr *frame);
1471
1472
/**
1473
 * @brief Read the next GIF frame composited onto the logical screen.
1474
 *
1475
 * @param gif The GIF reader.
1476
 * @param info Pointer to a gdGifFrameInfo structure to populate, or NULL.
1477
 * @param image Pointer to receive a caller-owned composited image, or NULL to
1478
 *        skip receiving the image.
1479
 *
1480
 * @return Returns 1 when an image is read, 0 at end of stream, or -1 on error.
1481
 */
1482
BGD_DECLARE(int)
1483
gdGifReadNextImage(gdGifReadPtr gif, gdGifFrameInfo *info, gdImagePtr *image);
1484
1485
/** @} */
1486
1487
/**
1488
 * @name GIF animation disposal constants
1489
 * @{
1490
 */
1491
1492
/**
1493
 * @brief GIF frame disposal methods.
1494
 */
1495
enum {
1496
    gdDisposalUnknown,           /**< Unknown disposal method; not recommended for writing. */
1497
    gdDisposalNone,              /**< Preserve previous frame contents. */
1498
    gdDisposalRestoreBackground, /**< Restore the frame area to the background color. */
1499
    gdDisposalRestorePrevious    /**< Restore the frame area to its previous contents. */
1500
};
1501
1502
/** Alias for gdDisposalUnknown. */
1503
#define GD_GIF_DISPOSAL_UNKNOWN gdDisposalUnknown
1504
/** Alias for gdDisposalNone. */
1505
#define GD_GIF_DISPOSAL_NONE gdDisposalNone
1506
/** Alias for gdDisposalRestoreBackground. */
1507
#define GD_GIF_DISPOSAL_RESTORE_BACKGROUND gdDisposalRestoreBackground
1508
/** Alias for gdDisposalRestorePrevious. */
1509
#define GD_GIF_DISPOSAL_RESTORE_PREVIOUS gdDisposalRestorePrevious
1510
1511
/** @} */
1512
1513
/**
1514
 * @name Animated GIF writing
1515
 * @{
1516
 */
1517
1518
/**
1519
 * @brief Begin writing a GIF animation to a stdio file.
1520
 *
1521
 * @param im Reference image used for logical screen size, interlace flag, and
1522
 *        optional global color table.
1523
 * @param outFile The stdio file to write to.
1524
 * @param GlobalCM Global color table flag: 1 to write one, 0 to omit it, or -1
1525
 *        for the default.
1526
 * @param Loops Loop count: 0 for infinite looping, -1 to omit the loop
1527
 *        extension, or a positive finite loop count.
1528
 */
1529
BGD_DECLARE(void)
1530
gdImageGifAnimBegin(gdImagePtr im, FILE *outFile, int GlobalCM, int Loops);
1531
1532
/**
1533
 * @brief Add a frame to a GIF animation written to a stdio file.
1534
 *
1535
 * @param im The frame image to add.
1536
 * @param outFile The stdio file to write to.
1537
 * @param LocalCM Local color table flag: 1 to write one, 0 to use the global
1538
 *        color table, or -1 for the default.
1539
 * @param LeftOfs Frame left offset on the logical screen.
1540
 * @param TopOfs Frame top offset on the logical screen.
1541
 * @param Delay Frame delay in hundredths of a second.
1542
 * @param Disposal One of the GD_GIF_DISPOSAL_* constants.
1543
 * @param previm Previous frame image for built-in optimization, or NULL.
1544
 */
1545
BGD_DECLARE(void)
1546
gdImageGifAnimAdd(gdImagePtr im, FILE *outFile, int LocalCM, int LeftOfs, int TopOfs, int Delay,
1547
                  int Disposal, gdImagePtr previm);
1548
1549
/**
1550
 * @brief Finish writing a GIF animation to a stdio file.
1551
 *
1552
 * @param outFile The stdio file to write to.
1553
 */
1554
BGD_DECLARE(void) gdImageGifAnimEnd(FILE *outFile);
1555
1556
/**
1557
 * @brief Begin writing a GIF animation to a gdIOCtx.
1558
 *
1559
 * @param im Reference image used for logical screen size, interlace flag, and
1560
 *        optional global color table.
1561
 * @param out The gdIOCtx to write to.
1562
 * @param GlobalCM Global color table flag: 1 to write one, 0 to omit it, or -1
1563
 *        for the default.
1564
 * @param Loops Loop count: 0 for infinite looping, -1 to omit the loop
1565
 *        extension, or a positive finite loop count.
1566
 */
1567
BGD_DECLARE(void)
1568
gdImageGifAnimBeginCtx(gdImagePtr im, gdIOCtxPtr out, int GlobalCM, int Loops);
1569
1570
/**
1571
 * @brief Add a frame to a GIF animation written to a gdIOCtx.
1572
 *
1573
 * @param im The frame image to add.
1574
 * @param out The gdIOCtx to write to.
1575
 * @param LocalCM Local color table flag: 1 to write one, 0 to use the global
1576
 *        color table, or -1 for the default.
1577
 * @param LeftOfs Frame left offset on the logical screen.
1578
 * @param TopOfs Frame top offset on the logical screen.
1579
 * @param Delay Frame delay in hundredths of a second.
1580
 * @param Disposal One of the GD_GIF_DISPOSAL_* constants.
1581
 * @param previm Previous frame image for built-in optimization, or NULL.
1582
 */
1583
BGD_DECLARE(void)
1584
gdImageGifAnimAddCtx(gdImagePtr im, gdIOCtxPtr out, int LocalCM, int LeftOfs, int TopOfs, int Delay,
1585
                     int Disposal, gdImagePtr previm);
1586
1587
/**
1588
 * @brief Finish writing a GIF animation to a gdIOCtx.
1589
 *
1590
 * @param out The gdIOCtx to write to.
1591
 */
1592
BGD_DECLARE(void) gdImageGifAnimEndCtx(gdIOCtxPtr out);
1593
1594
/**
1595
 * @brief Begin writing a GIF animation to a newly allocated memory buffer.
1596
 *
1597
 * @param im Reference image used for logical screen size, interlace flag, and
1598
 *        optional global color table.
1599
 * @param size Pointer to an integer that receives the returned buffer size.
1600
 * @param GlobalCM Global color table flag: 1 to write one, 0 to omit it, or -1
1601
 *        for the default.
1602
 * @param Loops Loop count: 0 for infinite looping, -1 to omit the loop
1603
 *        extension, or a positive finite loop count.
1604
 *
1605
 * @return A pointer to the newly allocated GIF animation header data, or NULL
1606
 *         on failure. Free the returned buffer with gdFree().
1607
 */
1608
BGD_DECLARE(void *)
1609
gdImageGifAnimBeginPtr(gdImagePtr im, int *size, int GlobalCM, int Loops);
1610
1611
/**
1612
 * @brief Add a GIF animation frame to a newly allocated memory buffer.
1613
 *
1614
 * @param im The frame image to add.
1615
 * @param size Pointer to an integer that receives the returned buffer size.
1616
 * @param LocalCM Local color table flag: 1 to write one, 0 to use the global
1617
 *        color table, or -1 for the default.
1618
 * @param LeftOfs Frame left offset on the logical screen.
1619
 * @param TopOfs Frame top offset on the logical screen.
1620
 * @param Delay Frame delay in hundredths of a second.
1621
 * @param Disposal One of the GD_GIF_DISPOSAL_* constants.
1622
 * @param previm Previous frame image for built-in optimization, or NULL.
1623
 *
1624
 * @return A pointer to the newly allocated GIF animation frame data, or NULL
1625
 *         on failure. Free the returned buffer with gdFree().
1626
 */
1627
BGD_DECLARE(void *)
1628
gdImageGifAnimAddPtr(gdImagePtr im, int *size, int LocalCM, int LeftOfs, int TopOfs, int Delay,
1629
                     int Disposal, gdImagePtr previm);
1630
1631
/**
1632
 * @brief Finish a GIF animation into a newly allocated memory buffer.
1633
 *
1634
 * @param size Pointer to an integer that receives the returned buffer size.
1635
 *
1636
 * @return A pointer to the newly allocated GIF animation terminator data, or
1637
 *         NULL on failure. Free the returned buffer with gdFree().
1638
 */
1639
BGD_DECLARE(void *) gdImageGifAnimEndPtr(int *size);
1640
1641
/** @} */
1642
/** @} */
1643
1644
/**
1645
 * @defgroup gdCodecWbmp WBMP
1646
 * @brief Wireless Bitmap reading and writing support.
1647
 * @ingroup gdCodecs
1648
 *
1649
 * WBMP support reads Wireless Bitmap Type 0 images into palette-based gd
1650
 * images with white and black colors. WBMP output writes a 1-bit image: pixels
1651
 * whose color matches the foreground color parameter are written as black, and
1652
 * all other pixels are written as white.
1653
 *
1654
 * @code{.c}
1655
 *        gdImagePtr im, roundtrip;
1656
 *        int white, black;
1657
 *        void *data;
1658
 *        int size;
1659
 *
1660
 *        im = gdImageCreate(100, 100);
1661
 *        if (im == NULL) {
1662
 *          exit(1);
1663
 *        }
1664
 *
1665
 *        white = gdImageColorAllocate(im, 255, 255, 255);
1666
 *        black = gdImageColorAllocate(im, 0, 0, 0);
1667
 *        gdImageFilledRectangle(im, 0, 0, 99, 99, white);
1668
 *        gdImageRectangle(im, 20, 20, 79, 79, black);
1669
 *
1670
 *        data = gdImageWBMPPtr(im, &size, black);
1671
 *        if (data == NULL) {
1672
 *          gdImageDestroy(im);
1673
 *          exit(1);
1674
 *        }
1675
 *
1676
 *        roundtrip = gdImageCreateFromWBMPPtr(size, data);
1677
 *        gdFree(data);
1678
 *        gdImageDestroy(roundtrip);
1679
 *        gdImageDestroy(im);
1680
 * @endcode
1681
 *
1682
 * @{
1683
 */
1684
1685
/** @name WBMP Reading */
1686
/** @{ */
1687
1688
/**
1689
 * @brief Create an image from a WBMP stdio file.
1690
 *
1691
 * gdImageCreateFromWBMP() does not close inFile. The returned image is
1692
 * caller-owned and must be destroyed with @ref gdImageDestroy.
1693
 *
1694
 * @param inFile Pointer to the input FILE stream.
1695
 *
1696
 * @return Returns a gdImagePtr on success, or NULL on failure.
1697
 */
1698
BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMP(FILE *inFile);
1699
1700
/**
1701
 * @brief Create an image from WBMP data read through a gdIOCtx.
1702
 *
1703
 * gdImageCreateFromWBMPCtx() does not close infile. The returned image is
1704
 * caller-owned and must be destroyed with @ref gdImageDestroy.
1705
 *
1706
 * @param infile Pointer to the gdIOCtx input context.
1707
 *
1708
 * @return Returns a gdImagePtr on success, or NULL on failure.
1709
 */
1710
BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPCtx(gdIOCtxPtr infile);
1711
1712
/**
1713
 * @brief Create an image from a WBMP memory buffer.
1714
 *
1715
 * The data buffer is borrowed for the duration of the call. The returned image
1716
 * is caller-owned and must be destroyed with @ref gdImageDestroy.
1717
 *
1718
 * @param size Size of the WBMP memory buffer in bytes.
1719
 * @param data Pointer to the WBMP memory buffer.
1720
 *
1721
 * @return Returns a gdImagePtr on success, or NULL on failure.
1722
 */
1723
BGD_DECLARE(gdImagePtr) gdImageCreateFromWBMPPtr(int size, void *data);
1724
1725
/** @} */
1726
/** @} */
1727
1728
/**
1729
 * @defgroup gdCodecJpeg JPEG
1730
 * @brief JPEG image reading and writing support.
1731
 * @ingroup gdCodecs
1732
 *
1733
 * GD supports a range of raster codecs for loading and saving images,
1734
 * including JPEG, PNG, GIF, WebP, and others. Each codec is exposed as its
1735
 * own subgroup with format-specific options and functions.
1736
 *
1737
 * @code{.c}
1738
 *        gdImagePtr im;
1739
 *        int black, white;
1740
 *        FILE *out;
1741
1742
 *        im = gdImageCreate(100, 100);
1743
 *        if (im == NULL) {
1744
 *          fprintf(stderr, "Unable to create image\n");
1745
 *          exit(1);
1746
 *        }
1747
 *
1748
 *        // Allocate background
1749
 *        white = gdImageColorAllocate(im, 255, 255, 255);
1750
 *
1751
 *        // Allocate drawing color
1752
 *        black = gdImageColorAllocate(im, 0, 0, 0);
1753
 *
1754
 *        // Draw rectangle
1755
 *        gdImageRectangle(im, 0, 0, 99, 99, black);
1756
 *
1757
 *        // Open output file in binary mode
1758
 *        out = fopen("rect.jpg", "wb");
1759
 *        if (out == NULL) {
1760
 *           fprintf(stderr, "Unable to open output file\n");
1761
 *           exit(1);
1762
 *        }
1763
 *        // Write JPEG using default quality
1764
 *        gdImageJpeg(im, out, -1);
1765
 *        // Close file
1766
 *        fclose(out);
1767
 *        // Destroy image
1768
 *        gdImageDestroy(im);
1769
 * @endcode
1770
 *
1771
 * @ingroup gdCodecs
1772
 * @{
1773
 */
1774
1775
/**
1776
 * @brief JPEG color space identifiers reported by gdJpegInfo.
1777
 */
1778
enum {
1779
    GD_JPEG_COLOR_SPACE_UNKNOWN = 0,   /**< Unknown or unsupported JPEG color space. */
1780
    GD_JPEG_COLOR_SPACE_GRAYSCALE = 1, /**< Grayscale JPEG color space. */
1781
    GD_JPEG_COLOR_SPACE_RGB = 2,       /**< RGB JPEG color space. */
1782
    GD_JPEG_COLOR_SPACE_YCBCR = 3,     /**< YCbCr JPEG color space. */
1783
    GD_JPEG_COLOR_SPACE_CMYK = 4,      /**< CMYK JPEG color space. */
1784
    GD_JPEG_COLOR_SPACE_YCCK = 5       /**< YCCK JPEG color space. */
1785
};
1786
1787
/**
1788
 * @brief JPEG density units reported by gdJpegInfo.
1789
 */
1790
enum {
1791
    GD_JPEG_DENSITY_UNIT_NONE = 0, /**< No density unit is specified. */
1792
    GD_JPEG_DENSITY_UNIT_DPI = 1,  /**< Density is measured in dots per inch. */
1793
    GD_JPEG_DENSITY_UNIT_DPCM = 2  /**< Density is measured in dots per centimeter. */
1794
};
1795
1796
/**
1797
 * @brief JPEG DCT method options for gdJpegReadOptions.
1798
 */
1799
enum {
1800
    GD_JPEG_DCT_DEFAULT = 0, /**< Use the JPEG library default DCT method. */
1801
    GD_JPEG_DCT_SLOW = 1,    /**< Use the slow integer DCT method. */
1802
    GD_JPEG_DCT_FAST = 2,    /**< Use the fast integer DCT method. */
1803
    GD_JPEG_DCT_FLOAT = 3    /**< Use the floating-point DCT method. */
1804
};
1805
1806
/**
1807
 * @brief Basic information read from a JPEG header.
1808
 */
1809
typedef struct {
1810
    int width;            /**< Image width in pixels. */
1811
    int height;           /**< Image height in pixels. */
1812
    int bits_per_sample;  /**< Bits per sample reported by the JPEG library. */
1813
    int components;       /**< Number of image components. */
1814
    int color_space;      /**< One of the GD_JPEG_COLOR_SPACE_* constants. */
1815
    int progressive;      /**< Non-zero if the image is progressive. */
1816
    int density_unit;     /**< One of the GD_JPEG_DENSITY_UNIT_* constants. */
1817
    int x_density;        /**< Horizontal density, or -1 if not available. */
1818
    int y_density;        /**< Vertical density, or -1 if not available. */
1819
} gdJpegInfo;
1820
1821
/**
1822
 * @brief Options for reading JPEG data.
1823
 *
1824
 * scale_num / scale_denom: The ratio to scale by.
1825
 */
1826
typedef struct {
1827
    int ignore_warning;       /**< Non-zero to suppress recoverable JPEG warnings. */
1828
    unsigned int scale_num;   /**< Decode scale numerator. When build against libjpeg-turbo, the library handles the scaling internally. With libjpeg, the available closed scaling factors are handled by the library, and GD handles the requested scaling then. */
1829
    unsigned int scale_denom; /**< Decode scale denominator. */
1830
    int dct_method;           /**< One of the GD_JPEG_DCT_* constants. */
1831
} gdJpegReadOptions;
1832
1833
/**
1834
 * @brief Options for writing JPEG data.
1835
 */
1836
typedef struct {
1837
    int quality;                     /**< JPEG quality, or -1 for the JPEG library default. */
1838
    int progressive;                 /**< Controls progressive JPEG output. */
1839
    int force_no_subsampling;        /**< Non-zero to force 4:4:4 chroma sampling. */
1840
    const gdImageMetadata *metadata; /**< Optional metadata to embed in the JPEG. */
1841
} gdJpegWriteOptions;
1842
1843
/**
1844
 * @brief Initialize a gdJpegInfo structure with default values.
1845
 *
1846
 * @param info Pointer to the gdJpegInfo structure to initialize.
1847
 */
1848
BGD_DECLARE(void) gdJpegInfoInit(gdJpegInfo *info);
1849
1850
/**
1851
 * @brief Initialize JPEG read options with default values.
1852
 *
1853
 * @param options Pointer to the gdJpegReadOptions structure to initialize.
1854
 */
1855
BGD_DECLARE(void) gdJpegReadOptionsInit(gdJpegReadOptions *options);
1856
1857
/**
1858
 * @brief Initialize JPEG write options with default values.
1859
 *
1860
 * @param options Pointer to the gdJpegWriteOptions structure to initialize.
1861
 */
1862
BGD_DECLARE(void) gdJpegWriteOptionsInit(gdJpegWriteOptions *options);
1863
1864
/**
1865
 * @brief Read JPEG header information from a stdio file.
1866
 *
1867
 * @param infile Pointer to the input FILE stream.
1868
 * @param info Pointer to the gdJpegInfo structure to populate with header information.
1869
 *
1870
 * @return Returns 1 on success, 0 on failure.
1871
 */
1872
BGD_DECLARE(int) gdJpegGetInfo(FILE *infile, gdJpegInfo *info);
1873
1874
1875
/**
1876
 * @brief Read JPEG header information from a gdIOCtx.
1877
 *
1878
 * @param infile Pointer to the gdIOCtx input context.
1879
 * @param info Pointer to the gdJpegInfo structure to populate with header information.
1880
 *
1881
 * @return Returns 1 on success, 0 on failure.
1882
 */
1883
BGD_DECLARE(int) gdJpegGetInfoCtx(gdIOCtxPtr infile, gdJpegInfo *info);
1884
1885
/**
1886
 * @brief Read JPEG header information from a memory buffer.
1887
 *
1888
 * @param size Size of the memory buffer.
1889
 * @param data Pointer to the memory buffer containing JPEG data.
1890
 * @param info Pointer to the gdJpegInfo structure to populate with header information.
1891
 *
1892
 * @return Returns 1 on success, 0 on failure.
1893
 */
1894
BGD_DECLARE(int) gdJpegGetInfoPtr(int size, const void *data, gdJpegInfo *info);
1895
1896
BGD_DECLARE(int) gdJpegGetMetadata(FILE *infile, gdImageMetadata *metadata);
1897
BGD_DECLARE(int) gdJpegGetMetadataCtx(gdIOCtxPtr infile, gdImageMetadata *metadata);
1898
BGD_DECLARE(int) gdJpegGetMetadataPtr(int size, const void *data, gdImageMetadata *metadata);
1899
1900
/**
1901
 * @brief Create an image from a JPEG stdio file.
1902
 *
1903
 * @param infile Pointer to the input FILE stream.
1904
 *
1905
 * @return Returns a gdImagePtr on success, or NULL on failure.
1906
 */
1907
BGD_DECLARE(gdImagePtr) gdImageCreateFromJpeg(FILE *infile);
1908
1909
/**
1910
 * @brief Create an image from a JPEG stdio file, controlling warning handling.
1911
 *
1912
 * @param infile Pointer to the input FILE stream.
1913
 * @param ignore_warning Non-zero to suppress recoverable JPEG warnings.
1914
 *
1915
 * @return Returns a gdImagePtr on success, or NULL on failure.
1916
 */
1917
BGD_DECLARE(gdImagePtr)
1918
gdImageCreateFromJpegEx(FILE *infile, int ignore_warning);
1919
1920
/**
1921
 * @brief Create an image from JPEG data read through a gdIOCtx.
1922
 *
1923
 * @param infile Pointer to the gdIOCtx input context.
1924
 *
1925
 * @return Returns a gdImagePtr on success, or NULL on failure.
1926
 */
1927
BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtx(gdIOCtxPtr infile);
1928
1929
/**
1930
 * @brief Create an image from a JPEG gdIOCtx, controlling warning handling.
1931
 *
1932
 * @param infile Pointer to the gdIOCtx input context.
1933
 * @param ignore_warning Non-zero to suppress recoverable JPEG warnings.
1934
 *
1935
 * @return Returns a gdImagePtr on success, or NULL on failure.
1936
 */
1937
BGD_DECLARE(gdImagePtr)
1938
gdImageCreateFromJpegCtxEx(gdIOCtxPtr infile, int ignore_warning);
1939
1940
/**
1941
 * @brief Create an image from a JPEG gdIOCtx and collect metadata.
1942
 *
1943
 * @param infile Pointer to the gdIOCtx input context.
1944
 * @param metadata Pointer to a gdImageMetadata structure to collect metadata.
1945
 *
1946
 * @return Returns a gdImagePtr on success, or NULL on failure.
1947
 */
1948
1949
/**
1950
 * @brief Create an image from a JPEG gdIOCtx with warning control and metadata collection.
1951
 *
1952
 * @param infile Pointer to the gdIOCtx input context.
1953
 * @param ignore_warning Non-zero to suppress recoverable JPEG warnings.
1954
 * @param metadata Pointer to a gdImageMetadata structure to collect metadata.
1955
 *
1956
 * @return Returns a gdImagePtr on success, or NULL on failure.
1957
 */
1958
1959
/**
1960
 * @brief Create an image from a JPEG gdIOCtx using read options.
1961
 *
1962
 * @param infile Pointer to the gdIOCtx input context.
1963
 * @param options Pointer to a gdJpegReadOptions structure specifying read options.
1964
 *
1965
 * @return Returns a gdImagePtr on success, or NULL on failure.
1966
 */
1967
BGD_DECLARE(gdImagePtr)
1968
gdImageCreateFromJpegCtxWithOptions(gdIOCtxPtr infile, const gdJpegReadOptions *options);
1969
1970
/**
1971
 * @brief Create an image from a JPEG memory buffer.
1972
 *
1973
 * @param size The size of the JPEG memory buffer.
1974
 * @param data Pointer to the JPEG memory buffer.
1975
 *
1976
 * @return Returns a gdImagePtr on success, or NULL on failure.
1977
 */
1978
BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegPtr(int size, void *data);
1979
/**
1980
 * @brief Create an image from a JPEG memory buffer, controlling warning handling.
1981
 *
1982
 * @param size The size of the JPEG memory buffer.
1983
 * @param data Pointer to the JPEG memory buffer.
1984
 * @param ignore_warning Non-zero to suppress recoverable JPEG warnings.
1985
 *
1986
 * @return Returns a gdImagePtr on success, or NULL on failure.
1987
 */
1988
BGD_DECLARE(gdImagePtr)
1989
gdImageCreateFromJpegPtrEx(int size, void *data, int ignore_warning);
1990
1991
/**
1992
 * @brief Create an image from a JPEG memory buffer using read options.
1993
 *
1994
 * @param size The size of the JPEG memory buffer.
1995
 * @param data Pointer to the JPEG memory buffer.
1996
 * @param options Pointer to a gdJpegReadOptions structure specifying read options.
1997
 *
1998
 * @return Returns a gdImagePtr on success, or NULL on failure.
1999
 */
2000
BGD_DECLARE(gdImagePtr)
2001
gdImageCreateFromJpegPtrWithOptions(int size, void *data, const gdJpegReadOptions *options);
2002
2003
/** Create a JPEG image from memory using read options and collect metadata. */
2004
2005
/**
2006
 * @brief Create an image from a JPEG memory buffer and collect metadata.
2007
 *
2008
 * @param size The size of the JPEG memory buffer.
2009
 * @param data Pointer to the JPEG memory buffer.
2010
 * @param metadata Pointer to a gdImageMetadata structure to collect metadata.
2011
 *
2012
 * @return Returns a gdImagePtr on success, or NULL on failure.
2013
 */
2014
2015
/**
2016
 * @brief Create an image from a JPEG memory buffer with warning control and metadata collection.
2017
 *
2018
 * @param size The size of the JPEG memory buffer.
2019
 * @param data Pointer to the JPEG memory buffer.
2020
 * @param ignore_warning Non-zero to suppress recoverable JPEG warnings.
2021
 * @param metadata Pointer to a gdImageMetadata structure to collect metadata.
2022
 *
2023
 * @return Returns a gdImagePtr on success, or NULL on failure.
2024
 */
2025
2026
/**
2027
 * @brief Return a string describing the linked JPEG library version.
2028
 *
2029
 * @return Returns a string describing the linked JPEG library version.
2030
 */
2031
BGD_DECLARE(const char *) gdJpegGetVersionString();
2032
/** @} */
2033
2034
/**
2035
 * @defgroup gdCodecWebp WebP
2036
 * @brief WebP image reading, writing and animations support.
2037
 * @ingroup gdCodecs
2038
 *
2039
 * WebP support reads still images as truecolor gd images and provides
2040
 * animation readers for raw frame rectangles or coalesced full-canvas images.
2041
 * WebP writers accept truecolor gd images; single-image pointer writers return
2042
 * buffers that must be freed with gdFree(), and animation writers are closed
2043
 * with gdWebpWriteClose() or gdWebpWritePtrFinish().
2044
 *
2045
 * @code{.c}
2046
 *        FILE *in, *out;
2047
 *        gdWebpReadPtr reader;
2048
 *        gdWebpWritePtr writer;
2049
 *        gdWebpInfo info;
2050
 *        gdWebpFrameInfo frameInfo;
2051
 *        gdWebpAnimWriteOptions options;
2052
 *        gdImagePtr image;
2053
 *        int result;
2054
 *
2055
 *        in = fopen("input.webp", "rb");
2056
 *        if (in == NULL) {
2057
 *          fprintf(stderr, "cannot open input.webp\n");
2058
 *          exit(1);
2059
 *        }
2060
 *
2061
 *        reader = gdWebpReadOpen(in, NULL);
2062
 *        fclose(in);
2063
 *        if (reader == NULL || !gdWebpReadGetInfo(reader, &info)) {
2064
 *          fprintf(stderr, "cannot read WebP\n");
2065
 *          if (reader != NULL) {
2066
 *            gdWebpReadClose(reader);
2067
 *          }
2068
 *          exit(1);
2069
 *        }
2070
 *
2071
 *        gdWebpAnimWriteOptionsInit(&options);
2072
 *        options.canvasWidth = info.width;
2073
 *        options.canvasHeight = info.height;
2074
 *        options.loopCount = info.loopCount;
2075
 *        options.backgroundColor = info.backgroundColor;
2076
 *        options.quality = gdWebpLossless;
2077
 *
2078
 *        out = fopen("output.webp", "wb");
2079
 *        if (out == NULL) {
2080
 *          gdWebpReadClose(reader);
2081
 *          exit(1);
2082
 *        }
2083
 *        writer = gdWebpWriteOpen(out, &options);
2084
 *        if (writer == NULL) {
2085
 *          fclose(out);
2086
 *          gdWebpReadClose(reader);
2087
 *          exit(1);
2088
 *        }
2089
 *
2090
 *        while ((result = gdWebpReadNextImage(reader, &frameInfo, &image)) == 1) {
2091
 *          if (!gdWebpWriteAddImage(writer, image, frameInfo.duration)) {
2092
 *            gdImageDestroy(image);
2093
 *            gdWebpWriteClose(writer);
2094
 *            fclose(out);
2095
 *            gdWebpReadClose(reader);
2096
 *            exit(1);
2097
 *          }
2098
 *          gdImageDestroy(image);
2099
 *        }
2100
 *
2101
 *        gdWebpWriteClose(writer);
2102
 *        fclose(out);
2103
 *        gdWebpReadClose(reader);
2104
 * @endcode
2105
 *
2106
 * @{
2107
 */
2108
2109
/** @name Single-Image Reading */
2110
/** @{ */
2111
2112
/**
2113
 * @brief Create a truecolor image from a WebP stdio file.
2114
 *
2115
 * gdImageCreateFromWebp() does not close inFile. The returned image is
2116
 * caller-owned and must be destroyed with @ref gdImageDestroy.
2117
 *
2118
 * @param inFile Pointer to the input FILE stream.
2119
 *
2120
 * @return Returns a gdImagePtr on success, or NULL on failure.
2121
 */
2122
BGD_DECLARE(gdImagePtr) gdImageCreateFromWebp(FILE *inFile);
2123
2124
/**
2125
 * @brief Create a truecolor image from a WebP memory buffer.
2126
 *
2127
 * The data buffer is borrowed for the duration of the call. The returned image
2128
 * is caller-owned and must be destroyed with @ref gdImageDestroy.
2129
 *
2130
 * @param size Size of the WebP memory buffer in bytes.
2131
 * @param data Pointer to the WebP memory buffer.
2132
 *
2133
 * @return Returns a gdImagePtr on success, or NULL on failure.
2134
 */
2135
BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpPtr(int size, void *data);
2136
2137
/**
2138
 * @brief Create a truecolor image from WebP data read through a gdIOCtx.
2139
 *
2140
 * gdImageCreateFromWebpCtx() does not close infile. The returned image is
2141
 * caller-owned and must be destroyed with @ref gdImageDestroy.
2142
 *
2143
 * @param infile Pointer to the gdIOCtx input context.
2144
 *
2145
 * @return Returns a gdImagePtr on success, or NULL on failure.
2146
 */
2147
BGD_DECLARE(gdImagePtr) gdImageCreateFromWebpCtx(gdIOCtxPtr infile);
2148
2149
/** @} */
2150
2151
/** @name WebP Types And Constants */
2152
/** @{ */
2153
2154
/**
2155
 * @brief Opaque WebP animation reader handle.
2156
 *
2157
 * Handles returned by gdWebpReadOpen(), gdWebpReadOpenCtx(), or
2158
 * gdWebpReadOpenPtr() must be closed with gdWebpReadClose().
2159
 */
2160
typedef struct gdWebpRead *gdWebpReadPtr;
2161
2162
/**
2163
 * @brief Opaque WebP animation writer handle.
2164
 *
2165
 * Handles returned by gdWebpWriteOpen() or gdWebpWriteOpenCtx() must be closed
2166
 * with gdWebpWriteClose(). Handles returned by gdWebpWriteOpenPtr() must be
2167
 * finished with gdWebpWritePtrFinish().
2168
 */
2169
typedef struct gdWebpWrite *gdWebpWritePtr;
2170
2171
/**
2172
 * @brief WebP container information.
2173
 */
2174
typedef struct {
2175
    int width;           /**< Canvas width in pixels. */
2176
    int height;          /**< Canvas height in pixels. */
2177
    int frame_count;     /**< Number of frames in the WebP container. */
2178
    int loop_count;      /**< Animation loop count, or 0 for infinite looping. */
2179
    int background_color; /**< Canvas background color as stored in the WebP container. */
2180
    int format_flags;    /**< WebP container feature flags reported by libwebp. */
2181
    int is_animation;    /**< Non-zero if the WebP container is animated. */
2182
} gdWebpInfo;
2183
2184
/**
2185
 * @brief WebP animation frame information.
2186
 */
2187
typedef struct {
2188
    int frame_index; /**< Zero-based frame index. */
2189
    int x;          /**< Frame rectangle X offset in pixels. */
2190
    int y;          /**< Frame rectangle Y offset in pixels. */
2191
    int width;      /**< Frame rectangle width in pixels. */
2192
    int height;     /**< Frame rectangle height in pixels. */
2193
    int duration;   /**< Frame duration in milliseconds. */
2194
    int timestamp;  /**< Frame start timestamp in milliseconds. */
2195
    int dispose;    /**< Disposal method, gdWebpDisposeNone or gdWebpDisposeBackground. */
2196
    int blend;      /**< Blend method, gdWebpBlendAlpha or gdWebpBlendNone. */
2197
    int has_alpha;  /**< Non-zero if the frame has alpha. */
2198
    int complete;   /**< Non-zero if the frame data is complete. */
2199
} gdWebpFrameInfo;
2200
2201
/**
2202
 * @brief WebP multi-image/animation reader options.
2203
 */
2204
typedef struct {
2205
    int coalesced;      /**< Non-zero to read full-canvas images, zero to read raw frame rectangles. */
2206
} gdWebpReadOptions;
2207
2208
/**
2209
 * @brief WebP still-image writer options.
2210
 */
2211
typedef struct {
2212
    int quality;                     /**< Encoding quality, -1 for default, 0-100 for lossy, or gdWebpLossless. */
2213
    const gdImageMetadata *metadata; /**< Optional metadata to embed in the WebP container. */
2214
} gdWebpWriteOptions;
2215
2216
/**
2217
 * @brief WebP animation writer options.
2218
 */
2219
typedef struct {
2220
    int canvas_width;     /**< Canvas width in pixels, or 0 to use the first image width. */
2221
    int canvas_height;    /**< Canvas height in pixels, or 0 to use the first image height. */
2222
    int loop_count;       /**< Animation loop count, or 0 for infinite looping. */
2223
    int background_color; /**< Canvas background color to store in the WebP container. */
2224
    int quality;         /**< Encoding quality, -1 for default, 0-100 for lossy, or gdWebpLossless. */
2225
    int lossless;        /**< Non-zero to force lossless encoding. */
2226
    int method;          /**< Compression method, or a negative value to use libwebp default. */
2227
    int minimize_size;    /**< Non-zero to enable libwebp minimized-size animation encoding. */
2228
    int kmin;            /**< Minimum distance between key frames, or 0 for libwebp default. */
2229
    int kmax;            /**< Maximum distance between key frames, or 0 for libwebp default. */
2230
    int allow_mixed;      /**< Non-zero to allow mixed lossy and lossless frames. */
2231
} gdWebpAnimWriteOptions;
2232
2233
/**
2234
 * @brief Initialize WebP multi-image/animation read options with gd defaults.
2235
 *
2236
 * The default reader mode is coalesced, so gdWebpReadNextImage() returns
2237
 * full-canvas rendered images. Set gdWebpReadOptions::coalesced to zero before
2238
 * opening the reader to read raw frame rectangles with gdWebpReadNextFrame().
2239
 *
2240
 * @param options Pointer to the read options structure to initialize.
2241
 */
2242
BGD_DECLARE(void) gdWebpReadOptionsInit(gdWebpReadOptions *options);
2243
2244
/**
2245
 * @brief Initialize WebP still-image write options with gd defaults.
2246
 *
2247
 * The default writer uses libwebp's default quality and writes no metadata.
2248
 *
2249
 * @param options Pointer to the write options structure to initialize.
2250
 */
2251
BGD_DECLARE(void) gdWebpWriteOptionsInit(gdWebpWriteOptions *options);
2252
2253
/**
2254
 * @brief Initialize WebP multi-image/animation write options with gd defaults.
2255
 *
2256
 * The default writer infers the canvas size from the first frame, writes lossy
2257
 * WebP with libwebp's default animation settings, and uses loopCount 0 for
2258
 * infinite looping.
2259
 *
2260
 * @param options Pointer to the write options structure to initialize.
2261
 */
2262
BGD_DECLARE(void) gdWebpAnimWriteOptionsInit(gdWebpAnimWriteOptions *options);
2263
2264
/**
2265
 * @brief WebP frame disposal methods.
2266
 */
2267
enum {
2268
    gdWebpDisposeNone,      /**< Do not dispose the frame after display. */
2269
    gdWebpDisposeBackground /**< Clear the frame rectangle to the background after display. */
2270
};
2271
2272
/**
2273
 * @brief WebP frame blend methods.
2274
 */
2275
enum {
2276
    gdWebpBlendAlpha, /**< Blend the frame using alpha compositing. */
2277
    gdWebpBlendNone   /**< Replace the frame rectangle without alpha blending. */
2278
};
2279
2280
/** @} */
2281
2282
/** @name WebP Multi-Image/Animation Reading */
2283
/** @{ */
2284
2285
/**
2286
 * @brief Test whether a WebP stdio file contains animation.
2287
 *
2288
 * The stream position is restored before returning when possible. gdWebpIsAnimated()
2289
 * does not close fd.
2290
 *
2291
 * @param fd Pointer to the input FILE stream.
2292
 *
2293
 * @return Returns 1 for animated WebP, 0 for still WebP, or -1 on error.
2294
 */
2295
BGD_DECLARE(int) gdWebpIsAnimated(FILE *fd);
2296
2297
/**
2298
 * @brief Test whether a seekable gdIOCtx contains animated WebP data.
2299
 *
2300
 * The context position is restored before returning when possible.
2301
 * gdWebpIsAnimatedCtx() does not close in.
2302
 *
2303
 * @param in Pointer to the gdIOCtx input context.
2304
 *
2305
 * @return Returns 1 for animated WebP, 0 for still WebP, or -1 on error.
2306
 */
2307
BGD_DECLARE(int) gdWebpIsAnimatedCtx(gdIOCtxPtr in);
2308
2309
/**
2310
 * @brief Test whether a WebP memory buffer contains animation.
2311
 *
2312
 * The data buffer is borrowed for the duration of the call.
2313
 *
2314
 * @param size Size of the WebP memory buffer in bytes.
2315
 * @param data Pointer to the WebP memory buffer.
2316
 *
2317
 * @return Returns 1 for animated WebP, 0 for still WebP, or -1 on error.
2318
 */
2319
BGD_DECLARE(int) gdWebpIsAnimatedPtr(int size, void *data);
2320
2321
/**
2322
 * @brief Open a WebP animation reader from a stdio file.
2323
 *
2324
 * gdWebpReadOpen() reads the WebP data into the reader and does not close fd.
2325
 * Pass NULL for options to use gd defaults. The returned handle must be closed
2326
 * with gdWebpReadClose().
2327
 *
2328
 * @param fd Pointer to the input FILE stream.
2329
 * @param options Pointer to read options, or NULL for defaults.
2330
 *
2331
 * @return Returns a WebP reader handle on success, or NULL on failure.
2332
 */
2333
BGD_DECLARE(gdWebpReadPtr) gdWebpReadOpen(FILE *fd, const gdWebpReadOptions *options);
2334
2335
/**
2336
 * @brief Open a WebP animation reader from a gdIOCtx.
2337
 *
2338
 * gdWebpReadOpenCtx() reads the WebP data into the reader and does not close
2339
 * in. Pass NULL for options to use gd defaults. The returned handle must be
2340
 * closed with gdWebpReadClose().
2341
 *
2342
 * @param in Pointer to the gdIOCtx input context.
2343
 * @param options Pointer to read options, or NULL for defaults.
2344
 *
2345
 * @return Returns a WebP reader handle on success, or NULL on failure.
2346
 */
2347
BGD_DECLARE(gdWebpReadPtr) gdWebpReadOpenCtx(gdIOCtxPtr in, const gdWebpReadOptions *options);
2348
2349
/**
2350
 * @brief Open a WebP animation reader from a memory buffer.
2351
 *
2352
 * The data buffer is borrowed for the duration of the call. The returned
2353
 * handle owns its copy of the WebP data and must be closed with
2354
 * gdWebpReadClose(). Pass NULL for options to use gd defaults.
2355
 *
2356
 * @param size Size of the WebP memory buffer in bytes.
2357
 * @param data Pointer to the WebP memory buffer.
2358
 * @param options Pointer to read options, or NULL for defaults.
2359
 *
2360
 * @return Returns a WebP reader handle on success, or NULL on failure.
2361
 */
2362
BGD_DECLARE(gdWebpReadPtr)
2363
gdWebpReadOpenPtr(int size, void *data, const gdWebpReadOptions *options);
2364
2365
/**
2366
 * @brief Close a WebP animation reader.
2367
 *
2368
 * @param webp WebP reader handle to close, or NULL.
2369
 */
2370
BGD_DECLARE(void) gdWebpReadClose(gdWebpReadPtr webp);
2371
2372
/**
2373
 * @brief Get WebP container information from a WebP reader.
2374
 *
2375
 * @param webp WebP reader handle.
2376
 * @param info Pointer to a gdWebpInfo structure to receive container information.
2377
 *
2378
 * @return Returns 1 on success, or 0 on failure.
2379
 */
2380
BGD_DECLARE(int) gdWebpReadGetInfo(gdWebpReadPtr webp, gdWebpInfo *info);
2381
2382
/**
2383
 * @brief Extract opaque EXIF, XMP, and ICC metadata from a WebP reader.
2384
 *
2385
 * @param webp The WebP reader.
2386
 * @param metadata Metadata object to populate.
2387
 * @return GD_META_OK on success, or a GD_META_ERR_* value on failure.
2388
 */
2389
BGD_DECLARE(int) gdWebpReadGetMetadata(gdWebpReadPtr webp, gdImageMetadata *metadata);
2390
2391
/**
2392
 * @brief Read the next raw WebP animation frame rectangle.
2393
 *
2394
 * When frame is not NULL and the function returns 1, *frame receives a
2395
 * caller-owned truecolor image that must be destroyed with @ref gdImageDestroy.
2396
 * Passing NULL for frame advances the reader without returning the image.
2397
 *
2398
 * @param webp WebP reader handle opened with raw-frame mode.
2399
 * @param info Pointer to a gdWebpFrameInfo structure to receive frame information, or NULL.
2400
 * @param frame Pointer to receive the caller-owned frame image, or NULL.
2401
 *
2402
 * @return Returns 1 when a frame is read, 0 at end of animation, or -1 on error.
2403
 */
2404
BGD_DECLARE(int)
2405
gdWebpReadNextFrame(gdWebpReadPtr webp, gdWebpFrameInfo *info, gdImagePtr *frame);
2406
2407
/**
2408
 * @brief Read the next coalesced WebP animation image.
2409
 *
2410
 * When image is not NULL and the function returns 1, *image receives a
2411
 * caller-owned truecolor full-canvas image that must be destroyed with
2412
 * @ref gdImageDestroy. Passing NULL for image advances the reader without
2413
 * returning the image.
2414
 *
2415
 * @param webp WebP reader handle.
2416
 * @param info Pointer to a gdWebpFrameInfo structure to receive frame information, or NULL.
2417
 * @param image Pointer to receive the caller-owned full-canvas image, or NULL.
2418
 *
2419
 * @return Returns 1 when an image is read, 0 at end of animation, or -1 on error.
2420
 */
2421
BGD_DECLARE(int)
2422
gdWebpReadNextImage(gdWebpReadPtr webp, gdWebpFrameInfo *info, gdImagePtr *image);
2423
2424
/** @} */
2425
2426
/** @name WebP Multi-Image/Animation Writing */
2427
/** @{ */
2428
2429
/**
2430
 * @brief Open a WebP animation writer for a stdio file.
2431
 *
2432
 * gdWebpWriteOpen() does not close outFile. The returned handle must be closed
2433
 * with gdWebpWriteClose(), which assembles and writes the animation.
2434
 *
2435
 * @param outFile Pointer to the output FILE stream.
2436
 * @param options Pointer to write options, or NULL for defaults.
2437
 *
2438
 * @return Returns a WebP writer handle on success, or NULL on failure.
2439
 */
2440
BGD_DECLARE(gdWebpWritePtr)
2441
gdWebpWriteOpen(FILE *outFile, const gdWebpAnimWriteOptions *options);
2442
2443
/**
2444
 * @brief Open a WebP animation writer for a gdIOCtx.
2445
 *
2446
 * The output context is borrowed and is not closed by gdWebpWriteClose().
2447
 * The returned handle must be closed with gdWebpWriteClose(), which assembles
2448
 * and writes the animation.
2449
 *
2450
 * @param out Pointer to the gdIOCtx output context.
2451
 * @param options Pointer to write options, or NULL for defaults.
2452
 *
2453
 * @return Returns a WebP writer handle on success, or NULL on failure.
2454
 */
2455
BGD_DECLARE(gdWebpWritePtr)
2456
gdWebpWriteOpenCtx(gdIOCtxPtr out, const gdWebpAnimWriteOptions *options);
2457
2458
/**
2459
 * @brief Open a WebP animation writer that returns a memory buffer.
2460
 *
2461
 * The returned handle must be finished with gdWebpWritePtrFinish().
2462
 *
2463
 * @param options Pointer to write options, or NULL for defaults.
2464
 *
2465
 * @return Returns a WebP memory writer handle on success, or NULL on failure.
2466
 */
2467
BGD_DECLARE(gdWebpWritePtr)
2468
gdWebpWriteOpenPtr(const gdWebpAnimWriteOptions *options);
2469
2470
/**
2471
 * @brief Add an image to a WebP animation writer.
2472
 *
2473
 * The image is borrowed for the duration of the call and remains owned by the
2474
 * caller. All frames must match the writer canvas size.
2475
 *
2476
 * @param webp WebP writer handle.
2477
 * @param image Image to add as the next frame.
2478
 * @param durationMs Frame duration in milliseconds.
2479
 *
2480
 * @return Returns 1 on success, or 0 on failure.
2481
 */
2482
BGD_DECLARE(int)
2483
gdWebpWriteAddImage(gdWebpWritePtr webp, gdImagePtr image, int durationMs);
2484
2485
/**
2486
 * @brief Finish, write, and close a WebP animation writer.
2487
 *
2488
 * Use this for handles returned by gdWebpWriteOpen() or gdWebpWriteOpenCtx().
2489
 * For memory writers returned by gdWebpWriteOpenPtr(), use gdWebpWritePtrFinish().
2490
 *
2491
 * @param webp WebP writer handle to finish and close, or NULL.
2492
 */
2493
BGD_DECLARE(void) gdWebpWriteClose(gdWebpWritePtr webp);
2494
2495
/**
2496
 * @brief Finish a WebP memory writer and return the encoded buffer.
2497
 *
2498
 * This closes webp whether encoding succeeds or fails. The returned buffer is
2499
 * caller-owned and must be freed with gdFree().
2500
 *
2501
 * @param webp WebP memory writer handle returned by gdWebpWriteOpenPtr().
2502
 * @param size Pointer to an integer that receives the returned buffer size.
2503
 *
2504
 * @return Returns a pointer to the newly allocated WebP buffer, or NULL on failure.
2505
 */
2506
BGD_DECLARE(void *) gdWebpWritePtrFinish(gdWebpWritePtr webp, int *size);
2507
2508
/** @} */
2509
/** @} */
2510
2511
/**
2512
 * @defgroup gdCodecJxl JPEG XL
2513
 * @brief JPEG XL image reading, writing, and animation support.
2514
 * @ingroup gdCodecs
2515
 *
2516
 * JPEG XL support reads still images as truecolor gd images and writes still
2517
 * images with either lossy distance settings or lossless encoding. Animation
2518
 * readers can return coalesced full-canvas images or raw frame rectangles, and
2519
 * animation writers accept full-canvas truecolor frames.
2520
 *
2521
 * @code{.c}
2522
 *        gdImagePtr first, second, image;
2523
 *        gdJxlAnimWriteOptions write_options;
2524
 *        gdJxlWritePtr writer;
2525
 *        gdJxlReadPtr reader;
2526
 *        void *data;
2527
 *        int size, delay_ms, result;
2528
 *
2529
 *        first = gdImageCreateTrueColor(32, 24);
2530
 *        second = gdImageCreateTrueColor(32, 24);
2531
 *        if (first == NULL || second == NULL) {
2532
 *          exit(1);
2533
 *        }
2534
 *
2535
 *        gdImageFilledRectangle(first, 0, 0, 31, 23, gdTrueColor(255, 0, 0));
2536
 *        gdImageFilledRectangle(second, 0, 0, 31, 23, gdTrueColor(0, 0, 255));
2537
 *
2538
 *        gdJxlAnimWriteOptionsInit(&write_options);
2539
 *        write_options.lossless = 1;
2540
 *        write_options.loopCount = 0;
2541
 *        writer = gdJxlWriteOpenPtr(&write_options);
2542
 *        if (writer == NULL) {
2543
 *          gdImageDestroy(first);
2544
 *          gdImageDestroy(second);
2545
 *          exit(1);
2546
 *        }
2547
 *        gdJxlWriteAddImage(writer, first, 120);
2548
 *        gdJxlWriteAddImage(writer, second, 80);
2549
 *        data = gdJxlWritePtrFinish(writer, &size);
2550
 *
2551
 *        reader = gdJxlReadOpenPtr(size, data, NULL);
2552
 *        while ((result = gdJxlReadNextImage(reader, &delay_ms, &image)) == 1) {
2553
 *          gdImageDestroy(image);
2554
 *        }
2555
 *        gdJxlReadClose(reader);
2556
 *        gdFree(data);
2557
 *        gdImageDestroy(first);
2558
 *        gdImageDestroy(second);
2559
 * @endcode
2560
 *
2561
 * @{
2562
 */
2563
2564
/** @name JPEG XL Single-Image Reading */
2565
/** @{ */
2566
2567
/**
2568
 * @brief Create a truecolor image from a JPEG XL stdio file.
2569
 *
2570
 * gdImageCreateFromJxl() does not close inFile. The returned image is
2571
 * caller-owned and must be destroyed with @ref gdImageDestroy.
2572
 *
2573
 * @param inFile Pointer to the input FILE stream.
2574
 *
2575
 * @return Returns a gdImagePtr on success, or NULL on failure.
2576
 */
2577
BGD_DECLARE(gdImagePtr) gdImageCreateFromJxl(FILE *inFile);
2578
2579
/**
2580
 * @brief Create a truecolor image from a JPEG XL memory buffer.
2581
 *
2582
 * The data buffer is borrowed for the duration of the call. The returned image
2583
 * is caller-owned and must be destroyed with @ref gdImageDestroy.
2584
 *
2585
 * @param size Size of the JPEG XL memory buffer in bytes.
2586
 * @param data Pointer to the JPEG XL memory buffer.
2587
 *
2588
 * @return Returns a gdImagePtr on success, or NULL on failure.
2589
 */
2590
BGD_DECLARE(gdImagePtr) gdImageCreateFromJxlPtr(int size, void *data);
2591
2592
/**
2593
 * @brief Create a truecolor image from JPEG XL data read through a gdIOCtx.
2594
 *
2595
 * gdImageCreateFromJxlCtx() does not close infile. The returned image is
2596
 * caller-owned and must be destroyed with @ref gdImageDestroy.
2597
 *
2598
 * @param infile Pointer to the gdIOCtx input context.
2599
 *
2600
 * @return Returns a gdImagePtr on success, or NULL on failure.
2601
 */
2602
BGD_DECLARE(gdImagePtr) gdImageCreateFromJxlCtx(gdIOCtxPtr infile);
2603
2604
/** @} */
2605
2606
/** @name JPEG XL Single-Image Writing */
2607
/** @{ */
2608
2609
/**
2610
 * @brief Write an image as JPEG XL data to a stdio file.
2611
 *
2612
 * gdImageJxl() does not close outFile. The image is borrowed for the duration
2613
 * of the call; palette images may be converted to truecolor internally.
2614
 *
2615
 * @param im The image to write.
2616
 * @param outFile Pointer to the output FILE stream.
2617
 */
2618
BGD_DECLARE(void) gdImageJxl(gdImagePtr im, FILE *outFile);
2619
2620
/**
2621
 * @brief Write an image as JPEG XL data to a stdio file with encoder settings.
2622
 *
2623
 * gdImageJxlEx() does not close outFile. The image is borrowed for the
2624
 * duration of the call; palette images may be converted to truecolor
2625
 * internally.
2626
 *
2627
 * @param im The image to write.
2628
 * @param outFile Pointer to the output FILE stream.
2629
 * @param lossless Non-zero to use lossless JPEG XL encoding.
2630
 * @param distance Lossy encoding distance when lossless is zero.
2631
 * @param effort Encoder effort setting.
2632
 */
2633
BGD_DECLARE(void)
2634
gdImageJxlEx(gdImagePtr im, FILE *outFile, int lossless, float distance, int effort);
2635
2636
/**
2637
 * @brief Write an image as JPEG XL data to a newly allocated memory buffer.
2638
 *
2639
 * The image is borrowed for the duration of the call; palette images may be
2640
 * converted to truecolor internally. The returned buffer is caller-owned and
2641
 * must be freed with gdFree().
2642
 *
2643
 * @param im The image to write.
2644
 * @param size Pointer to an integer that receives the returned buffer size.
2645
 *
2646
 * @return Returns a pointer to the newly allocated JPEG XL buffer, or NULL on failure.
2647
 */
2648
BGD_DECLARE(void *) gdImageJxlPtr(gdImagePtr im, int *size);
2649
2650
/**
2651
 * @brief Write an image as JPEG XL data to a newly allocated memory buffer with encoder settings.
2652
 *
2653
 * The image is borrowed for the duration of the call; palette images may be
2654
 * converted to truecolor internally. The returned buffer is caller-owned and
2655
 * must be freed with gdFree().
2656
 *
2657
 * @param im The image to write.
2658
 * @param size Pointer to an integer that receives the returned buffer size.
2659
 * @param lossless Non-zero to use lossless JPEG XL encoding.
2660
 * @param distance Lossy encoding distance when lossless is zero.
2661
 * @param effort Encoder effort setting.
2662
 *
2663
 * @return Returns a pointer to the newly allocated JPEG XL buffer, or NULL on failure.
2664
 */
2665
BGD_DECLARE(void *)
2666
gdImageJxlPtrEx(gdImagePtr im, int *size, int lossless, float distance, int effort);
2667
2668
/**
2669
 * @brief Write an image as JPEG XL data to a gdIOCtx.
2670
 *
2671
 * gdImageJxlCtx() does not close outfile. The image is borrowed for the
2672
 * duration of the call; palette images may be converted to truecolor
2673
 * internally.
2674
 *
2675
 * @param im The image to write.
2676
 * @param outfile Pointer to the gdIOCtx output context.
2677
 */
2678
BGD_DECLARE(void) gdImageJxlCtx(gdImagePtr im, gdIOCtxPtr outfile);
2679
2680
/**
2681
 * @brief Write an image as JPEG XL data to a gdIOCtx with encoder settings.
2682
 *
2683
 * gdImageJxlCtxEx() does not close outfile. The image is borrowed for the
2684
 * duration of the call; palette images may be converted to truecolor
2685
 * internally.
2686
 *
2687
 * @param im The image to write.
2688
 * @param outfile Pointer to the gdIOCtx output context.
2689
 * @param lossless Non-zero to use lossless JPEG XL encoding.
2690
 * @param distance Lossy encoding distance when lossless is zero.
2691
 * @param effort Encoder effort setting.
2692
 */
2693
BGD_DECLARE(void)
2694
gdImageJxlCtxEx(gdImagePtr im, gdIOCtxPtr outfile, int lossless, float distance, int effort);
2695
2696
/**
2697
 * @brief JPEG XL still-image writer options.
2698
 */
2699
typedef struct {
2700
    int lossless;                         /**< Non-zero for lossless encoding. */
2701
    float distance;                       /**< Lossy distance, from 0 through 25. */
2702
    int effort;                           /**< Encoder effort, from 1 through 9. */
2703
    const gdImageMetadata *metadata;      /**< Optional EXIF/XMP metadata. */
2704
} gdJxlWriteOptions;
2705
2706
/**
2707
 * @brief Initialize JPEG XL still-image write options with gd defaults.
2708
 *
2709
 * @param options Pointer to the write options structure to initialize.
2710
 */
2711
BGD_DECLARE(void) gdJxlWriteOptionsInit(gdJxlWriteOptions *options);
2712
2713
/**
2714
 * @brief Write an image as JPEG XL data to a stdio file with write options.
2715
 *
2716
 * @param im The image to write.
2717
 * @param outFile Pointer to the output FILE stream.
2718
 * @param options Pointer to the JPEG XL write options.
2719
 *
2720
 * @return Returns non-zero on success, or zero on failure.
2721
 */
2722
BGD_DECLARE(int)
2723
gdImageJxlWithOptions(gdImagePtr im, FILE *outFile, const gdJxlWriteOptions *options);
2724
2725
/**
2726
 * @brief Write an image as JPEG XL data to a gdIOCtx with write options.
2727
 *
2728
 * @param im The image to write.
2729
 * @param outfile Pointer to the gdIOCtx output context.
2730
 * @param options Pointer to the JPEG XL write options.
2731
 *
2732
 * @return Returns non-zero on success, or zero on failure.
2733
 */
2734
BGD_DECLARE(int)
2735
gdImageJxlCtxWithOptions(gdImagePtr im, gdIOCtxPtr outfile, const gdJxlWriteOptions *options);
2736
2737
/**
2738
 * @brief Write an image as JPEG XL data to a newly allocated memory buffer with write options.
2739
 *
2740
 * The image is borrowed for the duration of the call; palette images may be
2741
 * converted to truecolor internally. The returned buffer is caller-owned and
2742
 * must be freed with gdFree().
2743
 *
2744
 * @param im The image to write.
2745
 * @param size Pointer to an integer that receives the returned buffer size.
2746
 * @param options Pointer to the JPEG XL write options.
2747
 *
2748
 * @return Returns a pointer to the newly allocated memory buffer, or NULL on failure.
2749
 */
2750
BGD_DECLARE(void *)
2751
gdImageJxlPtrWithOptions(gdImagePtr im, int *size, const gdJxlWriteOptions *options);
2752
2753
/** @} */
2754
2755
/** @name JPEG XL File Information And Animation Types */
2756
/** @{ */
2757
2758
/**
2759
 * @brief Opaque JPEG XL multi-image/animation reader handle.
2760
 *
2761
 * Handles returned by @ref gdJxlReadOpen, @ref gdJxlReadOpenCtx(), or
2762
 * @ref gdJxlReadOpenPtr must be closed with @ref gdJxlReadClose.
2763
 */
2764
typedef struct gdJxlRead *gdJxlReadPtr;
2765
2766
/**
2767
 * @brief Opaque JPEG XL multi-image/animation writer handle.
2768
 *
2769
 * Handles returned by @ref gdJxlWriteOpen or @ref gdJxlWriteOpenCtx must be closed
2770
 * with @ref gdJxlWriteClose. Handles returned by @ref gdJxlWriteOpenPtr must be
2771
 * finished with @ref gdJxlWritePtrFinish.
2772
 */
2773
typedef struct gdJxlWrite *gdJxlWritePtr;
2774
2775
/**
2776
 * @brief JPEG XL top-level file information.
2777
 *
2778
 * This describes the complete JPEG XL file, including whether it contains
2779
 * animation. It is not an animation-only structure.
2780
 */
2781
typedef struct {
2782
    int width;      /**< Canvas width in pixels. */
2783
    int height;     /**< Canvas height in pixels. */
2784
    int animated;   /**< Non-zero if the JPEG XL stream is animated. */
2785
    int loop_count; /**< Animation loop count, or 0 for infinite looping. */
2786
} gdJxlInfo;
2787
2788
/**
2789
 * @brief JPEG XL raw frame information.
2790
 */
2791
typedef struct {
2792
    int delay_ms;   /**< Frame duration in milliseconds. */
2793
    int x_offset;   /**< Frame rectangle X offset in pixels. */
2794
    int y_offset;   /**< Frame rectangle Y offset in pixels. */
2795
    int width;      /**< Frame rectangle width in pixels. */
2796
    int height;     /**< Frame rectangle height in pixels. */
2797
    int blend_mode; /**< Frame blend mode, one of the gdJxlBlend* constants. */
2798
    int is_last;    /**< Non-zero if this is the final frame. */
2799
} gdJxlFrameInfo;
2800
2801
/**
2802
 * @brief JPEG XL raw frame blend modes.
2803
 */
2804
enum {
2805
    gdJxlBlendReplace, /**< Replace the frame rectangle with the new frame. */
2806
    gdJxlBlendAdd,     /**< Add the new frame to the existing canvas. */
2807
    gdJxlBlendBlend,   /**< Blend the new frame over the existing canvas. */
2808
    gdJxlBlendMuladd,  /**< Multiply then add the new frame with the existing canvas. */
2809
    gdJxlBlendMul      /**< Multiply the new frame with the existing canvas. */
2810
};
2811
2812
/**
2813
 * @brief JPEG XL multi-image/animation reader options.
2814
 */
2815
typedef struct {
2816
    int coalesced;      /**< Non-zero to read full-canvas images, zero to read raw frame rectangles. */
2817
} gdJxlReadOptions;
2818
2819
/**
2820
 * @brief JPEG XL animation writer options.
2821
 */
2822
typedef struct {
2823
    int canvas_width;    /**< Canvas width in pixels, or 0 to use the first image width. */
2824
    int canvas_height;   /**< Canvas height in pixels, or 0 to use the first image height. */
2825
    int lossless;       /**< Non-zero to use lossless JPEG XL encoding. */
2826
    float distance;     /**< Lossy encoding distance when lossless is zero. */
2827
    int effort;         /**< Encoder effort setting. */
2828
    int loop_count;      /**< Animation loop count, or 0 for infinite looping. */
2829
} gdJxlAnimWriteOptions;
2830
2831
/**
2832
 * @brief Initialize JPEG XL multi-image/animation read options with gd defaults.
2833
 *
2834
 * The default reader mode is coalesced, so @ref gdJxlReadNextImage returns
2835
 * full-canvas rendered images. Set @ref gdJxlReadOptions::coalesced to zero before
2836
 * opening the reader to read raw frame rectangles with @ref gdJxlReadNextFrame.
2837
 *
2838
 * @param options Pointer to the read options structure to initialize.
2839
 */
2840
BGD_DECLARE(void) gdJxlReadOptionsInit(gdJxlReadOptions *options);
2841
2842
/**
2843
 * @brief Initialize JPEG XL multi-image/animation write options with gd defaults.
2844
 *
2845
 * The default writer infers the canvas size from the first frame, writes lossy
2846
 * JPEG XL with distance 1.0 and effort 7, and uses loopCount 0 for infinite
2847
 * looping.
2848
 *
2849
 * @param options Pointer to the write options structure to initialize.
2850
 */
2851
BGD_DECLARE(void) gdJxlAnimWriteOptionsInit(gdJxlAnimWriteOptions *options);
2852
2853
/** @} */
2854
2855
/** @name JPEG XL Multi-Image/Animation Reading */
2856
/** @{ */
2857
2858
/**
2859
 * @brief Open a JPEG XL multi-image/animation reader from a stdio file.
2860
 *
2861
 * gdJxlReadOpen() reads the JPEG XL data into the reader and does not close
2862
 * inFile. Pass NULL for options to use gd defaults. The returned handle must be
2863
 * closed with gdJxlReadClose().
2864
 *
2865
 * @param inFile Pointer to the input FILE stream.
2866
 * @param options Pointer to read options, or NULL for defaults.
2867
 *
2868
 * @return Returns a JPEG XL reader handle on success, or NULL on failure.
2869
 */
2870
BGD_DECLARE(gdJxlReadPtr) gdJxlReadOpen(FILE *inFile, const gdJxlReadOptions *options);
2871
2872
/**
2873
 * @brief Open a JPEG XL multi-image/animation reader from a gdIOCtx.
2874
 *
2875
 * gdJxlReadOpenCtx() reads the JPEG XL data into the reader and does not close
2876
 * inCtx. Pass NULL for options to use gd defaults. The returned handle must be
2877
 * closed with gdJxlReadClose().
2878
 *
2879
 * @param inCtx Pointer to the gdIOCtx input context.
2880
 * @param options Pointer to read options, or NULL for defaults.
2881
 *
2882
 * @return Returns a JPEG XL reader handle on success, or NULL on failure.
2883
 */
2884
BGD_DECLARE(gdJxlReadPtr) gdJxlReadOpenCtx(gdIOCtxPtr inCtx, const gdJxlReadOptions *options);
2885
2886
/**
2887
 * @brief Open a JPEG XL multi-image/animation reader from a memory buffer.
2888
 *
2889
 * The data buffer is borrowed for the duration of the call. Pass NULL for
2890
 * options to use gd defaults. The returned handle owns its copy of the JPEG XL
2891
 * data and must be closed with @ref gdJxlReadClose.
2892
 *
2893
 * @param size Size of the JPEG XL memory buffer in bytes.
2894
 * @param data Pointer to the JPEG XL memory buffer.
2895
 * @param options Pointer to read options, or NULL for defaults.
2896
 *
2897
 * @return Returns a JPEG XL reader handle on success, or NULL on failure.
2898
 */
2899
BGD_DECLARE(gdJxlReadPtr)
2900
gdJxlReadOpenPtr(int size, void *data, const gdJxlReadOptions *options);
2901
2902
/**
2903
 * @brief Get JPEG XL image or animation information from a reader.
2904
 *
2905
 * The returned gdJxlInfo describes the canvas size and animation loop count
2906
 * reported by the JPEG XL stream.
2907
 *
2908
 * @param reader JPEG XL reader handle.
2909
 * @param info Pointer to a gdJxlInfo structure to receive image information.
2910
 *
2911
 * @return Returns 1 on success, or 0 on failure.
2912
 */
2913
BGD_DECLARE(int) gdJxlReadGetInfo(gdJxlReadPtr reader, gdJxlInfo *info);
2914
2915
/**
2916
 * @brief Extract supported still-image metadata from a JPEG XL reader.
2917
 *
2918
 * The caller owns the metadata object. Animation metadata is not exposed.
2919
 */
2920
BGD_DECLARE(int) gdJxlReadGetMetadata(gdJxlReadPtr reader, gdImageMetadata *metadata);
2921
2922
/**
2923
 * @brief Read the next coalesced JPEG XL image.
2924
 *
2925
 * This function is used with coalesced readers. When image is not NULL and the
2926
 * function returns 1, *image receives a caller-owned full-canvas truecolor image
2927
 * that must be destroyed with @ref gdImageDestroy. Passing NULL for image advances
2928
 * the reader without returning the decoded image.
2929
 *
2930
 * @param reader JPEG XL reader handle opened with coalesced mode.
2931
 * @param delay_ms Pointer to receive the frame duration in milliseconds, or NULL.
2932
 * @param image Pointer to receive the caller-owned image, or NULL.
2933
 *
2934
 * @return Returns 1 when an image is read, 0 at end of stream, or -1 on error.
2935
 */
2936
BGD_DECLARE(int) gdJxlReadNextImage(gdJxlReadPtr reader, int *delay_ms, gdImagePtr *image);
2937
2938
/**
2939
 * @brief Read the next raw JPEG XL frame rectangle.
2940
 *
2941
 * This function is used with non-coalesced readers. When frame is not NULL and
2942
 * the function returns 1, *frame receives a caller-owned truecolor frame
2943
 * rectangle that must be destroyed with @ref gdImageDestroy. Passing NULL for frame
2944
 * advances the reader without returning the decoded image.
2945
 *
2946
 * @param reader JPEG XL reader handle opened with raw-frame mode.
2947
 * @param info Pointer to receive raw frame information.
2948
 * @param frame Pointer to receive the caller-owned frame image, or NULL.
2949
 *
2950
 * @return Returns 1 when a frame is read, 0 at end of stream, or -1 on error.
2951
 */
2952
BGD_DECLARE(int) gdJxlReadNextFrame(gdJxlReadPtr reader, gdJxlFrameInfo *info, gdImagePtr *frame);
2953
2954
/**
2955
 * @brief Close a JPEG XL multi-image reader.
2956
 *
2957
 * @param reader JPEG XL reader handle to close, or NULL.
2958
 */
2959
BGD_DECLARE(void) gdJxlReadClose(gdJxlReadPtr reader);
2960
2961
/** @} */
2962
2963
/** @name JPEG XL Multi-Image/Animation Writing */
2964
/** @{ */
2965
2966
/**
2967
 * @brief Open a JPEG XL multi-image/animation writer for a stdio file.
2968
 *
2969
 * @ref gdJxlWriteOpen does not close outFile. Pass NULL for options to use gd defaults.
2970
 * The returned handle must be closed with @ref gdJxlWriteClose.
2971
 *
2972
 * @param outFile Pointer to the output FILE stream.
2973
 * @param options Pointer to write options, or NULL for defaults.
2974
 *
2975
 * @return Returns a JPEG XL writer handle on success, or NULL on failure.
2976
 */
2977
BGD_DECLARE(gdJxlWritePtr) gdJxlWriteOpen(FILE *outFile, const gdJxlAnimWriteOptions *options);
2978
2979
/**
2980
 * @brief Open a JPEG XL multi-image/animation writer for a gdIOCtx.
2981
 *
2982
 * The output context is borrowed and is not closed by @ref gdJxlWriteClose. Pass
2983
 * NULL for options to use gd defaults. The returned handle must be closed with
2984
 * @ref gdJxlWriteClose.
2985
 *
2986
 * @param outCtx Pointer to the gdIOCtx output context.
2987
 * @param options Pointer to write options, or NULL for defaults.
2988
 *
2989
 * @return Returns a JPEG XL writer handle on success, or NULL on failure.
2990
 */
2991
BGD_DECLARE(gdJxlWritePtr) gdJxlWriteOpenCtx(gdIOCtxPtr outCtx, const gdJxlAnimWriteOptions *options);
2992
2993
/**
2994
 * @brief Open a JPEG XL multi-image/animation writer that returns a memory buffer.
2995
 *
2996
 * Pass NULL for options to use gd defaults. The returned handle must be finished
2997
 * with @ref gdJxlWritePtrFinish.
2998
 *
2999
 * @param options Pointer to write options, or NULL for defaults.
3000
 *
3001
 * @return Returns a JPEG XL memory writer handle on success, or NULL on failure.
3002
 */
3003
BGD_DECLARE(gdJxlWritePtr) gdJxlWriteOpenPtr(const gdJxlAnimWriteOptions *options);
3004
3005
/**
3006
 * @brief Add a full-canvas image to a JPEG XL multi-image/animation writer.
3007
 *
3008
 * The image is borrowed for the duration of the call and remains owned by the
3009
 * caller. The image must be truecolor. All images must match the resolved
3010
 * canvas size; when the writer canvas is zero, the first image sets it.
3011
 *
3012
 * @param writer JPEG XL writer handle.
3013
 * @param image Image to add as the next frame.
3014
 * @param delay_ms Frame duration in milliseconds.
3015
 *
3016
 * @return Returns 1 on success, or 0 on failure.
3017
 */
3018
BGD_DECLARE(int) gdJxlWriteAddImage(gdJxlWritePtr writer, gdImagePtr image, int delay_ms);
3019
3020
/**
3021
 * @brief Add a full-canvas image to a JPEG XL multi-image/animation writer.
3022
 *
3023
 * Use this for handles returned by gdJxlWriteOpen() or gdJxlWriteOpenCtx(). For
3024
 * memory writers returned by @ref gdJxlWriteOpenPtr, use @ref gdJxlWritePtrFinish.
3025
 *
3026
 * @param writer JPEG XL writer handle to finish and close, or NULL.
3027
 */
3028
BGD_DECLARE(void) gdJxlWriteClose(gdJxlWritePtr writer);
3029
3030
/**
3031
 * @brief Finish a JPEG XL multi-image/animation memory writer and return the encoded buffer.
3032
 *
3033
 * This closes writer whether encoding succeeds or fails. The returned buffer is
3034
 * caller-owned and must be freed with gdFree().
3035
 *
3036
 * @param writer JPEG XL memory writer handle returned by @ref gdJxlWriteOpenPtr.
3037
 * @param size Pointer to an integer that receives the returned buffer size.
3038
 *
3039
 * @return Returns a pointer to the newly allocated JPEG XL buffer, or NULL on failure.
3040
 */
3041
BGD_DECLARE(void *) gdJxlWritePtrFinish(gdJxlWritePtr writer, int *size);
3042
3043
/** @} */
3044
/** @} */
3045
3046
/**
3047
 * @defgroup gdCodecHeif HEIF
3048
 * @brief Read and write High Efficiency Image File Format images.
3049
 * @ingroup gdCodecs
3050
 *
3051
 * HEIF support reads HEIF-family files from stdio streams, memory buffers, or
3052
 * gd IO contexts and returns truecolor images. The reader accepts AVIF, MIF1,
3053
 * HEIC, and HEIX brands and decodes the primary image. HEIF writing accepts
3054
 * truecolor images and can write to stdio streams, memory buffers, or gd IO
3055
 * contexts with explicit codec, quality, lossless, and chroma-subsampling
3056
 * options.
3057
 *
3058
 * @code{.c}
3059
 * FILE *in;
3060
 * gdImagePtr im;
3061
 * gdHeifWriteOptions options;
3062
 * void *data;
3063
 * int size;
3064
 *
3065
 * in = fopen("input.heic", "rb");
3066
 * if (in == NULL) {
3067
 *     return 1;
3068
 * }
3069
 *
3070
 * im = gdImageCreateFromHeif(in);
3071
 * fclose(in);
3072
 * if (im == NULL) {
3073
 *     return 1;
3074
 * }
3075
 *
3076
 * gdHeifWriteOptionsInit(&options);
3077
 * options.quality = 90;
3078
 * options.codec = GD_HEIF_CODEC_HEVC;
3079
 * options.chroma = GD_HEIF_CHROMA_444;
3080
 *
3081
 * data = gdImageHeifPtrWithOptions(im, &size, &options);
3082
 * if (data != NULL) {
3083
 *     gdFree(data);
3084
 * }
3085
 *
3086
 * gdImageDestroy(im);
3087
 * @endcode
3088
 *
3089
 * @{
3090
 */
3091
3092
/** @name HEIF Constants and Options */
3093
/** @{ */
3094
3095
/** @brief HEIF coding formats for gdHeifWriteOptions::codec. */
3096
typedef enum {
3097
    GD_HEIF_CODEC_UNKNOWN = 0, /**< Unknown or unspecified HEIF codec. */
3098
    GD_HEIF_CODEC_HEVC,        /**< HEVC/H.265 HEIF codec. */
3099
    GD_HEIF_CODEC_AV1 = 4,     /**< AV1 HEIF codec. */
3100
} gdHeifCodec;
3101
3102
/** @brief HEIF chroma-subsampling string used by gdHeifWriteOptions::chroma. */
3103
typedef const char *gdHeifChroma;
3104
3105
/** Use 4:2:0 chroma subsampling for HEIF output. */
3106
#define GD_HEIF_CHROMA_420 "420"
3107
/** Use 4:2:2 chroma subsampling for HEIF output. */
3108
#define GD_HEIF_CHROMA_422 "422"
3109
/** Use 4:4:4 chroma subsampling for HEIF output. */
3110
#define GD_HEIF_CHROMA_444 "444"
3111
3112
/** @brief HEIF decoder options used by gdImageCreateFromHeifPtrWithOptions(). */
3113
typedef struct {
3114
    int ignore_transformations; /**< Nonzero to ignore HEIF image transformations while decoding. */
3115
} gdHeifReadOptions;
3116
3117
/** @brief HEIF encoder options used by gdImageHeifPtrWithOptions(). */
3118
typedef struct {
3119
    int quality;        /**< Lossy quality from 0 to 100, -1 for the default, or 200 for lossless. */
3120
    int lossless;       /**< Nonzero to request lossless encoding. */
3121
    gdHeifCodec codec;  /**< HEIF codec to use for output. */
3122
    gdHeifChroma chroma; /**< Chroma-subsampling string for output. */
3123
    const gdImageMetadata *metadata; /**< Optional metadata to embed in the HEIF file. */
3124
} gdHeifWriteOptions;
3125
3126
/**
3127
 * @brief Information extracted from a HEIF image and its container.
3128
 *
3129
 * Info reports facts available in the input independently of whether GD can
3130
 * decode or write every feature. Width, height, alpha, and bit depth describe
3131
 * the primary image. top_level_image_count and is_animation describe the
3132
 * container when available; they do not add a frame/page decoding API.
3133
 * metadata is caller-owned and is populated with canonical exif, xmp, and
3134
 * iptc profiles. HEIF's ICC color profile is intentionally not exposed as
3135
 * metadata.
3136
 */
3137
typedef struct {
3138
    int width;
3139
    int height;
3140
    int top_level_image_count;
3141
    int has_alpha;
3142
    int bit_depth;
3143
    int is_animation;
3144
    gdImageMetadata *metadata;
3145
} gdHeifInfo;
3146
3147
/**
3148
 * @brief Initialize HEIF information with gd defaults.
3149
 *
3150
 * Set info->metadata to a caller-owned metadata object after initialization
3151
 * when metadata should be collected.
3152
 */
3153
BGD_DECLARE(void) gdHeifInfoInit(gdHeifInfo *info);
3154
3155
/**
3156
 * @brief Read HEIF information from a stdio stream.
3157
 *
3158
 * The input stream is borrowed and remains open. Returns GD_META_OK on
3159
 * success; otherwise returns a GD_META_* error code.
3160
 */
3161
BGD_DECLARE(int) gdHeifGetInfo(FILE *inFile, gdHeifInfo *info);
3162
3163
/** @brief Read HEIF information from a gdIOCtx without closing it. */
3164
BGD_DECLARE(int) gdHeifGetInfoCtx(gdIOCtxPtr in, gdHeifInfo *info);
3165
3166
/** @brief Read HEIF information from an in-memory buffer without taking ownership. */
3167
BGD_DECLARE(int) gdHeifGetInfoPtr(int size, const void *data, gdHeifInfo *info);
3168
3169
3170
/**
3171
 * @brief Initialize HEIF read options with gd defaults.
3172
 *
3173
 * Call this before changing selected gdHeifReadOptions fields and passing the
3174
 * structure to gdImageCreateFromHeifPtrWithOptions().
3175
 *
3176
 * @param options Pointer to the read options structure to initialize.
3177
 */
3178
BGD_DECLARE(void) gdHeifReadOptionsInit(gdHeifReadOptions *options);
3179
3180
/**
3181
 * @brief Initialize HEIF write options with gd defaults.
3182
 *
3183
 * Call this before changing selected gdHeifWriteOptions fields and passing the
3184
 * structure to gdImageHeifPtrWithOptions().
3185
 *
3186
 * @param options Pointer to the write options structure to initialize.
3187
 */
3188
BGD_DECLARE(void) gdHeifWriteOptionsInit(gdHeifWriteOptions *options);
3189
3190
/** @} */
3191
3192
/** @name HEIF Reading */
3193
/** @{ */
3194
3195
/**
3196
 * @brief Create a truecolor image from HEIF data in a stdio stream.
3197
 *
3198
 * gdImageCreateFromHeif() does not close inFile. It decodes the primary image
3199
 * and returns a new truecolor image. On success, the returned image is owned by
3200
 * the caller and must be destroyed with @ref gdImageDestroy.
3201
 *
3202
 * @param inFile Pointer to the input FILE stream.
3203
 * @return A newly allocated truecolor image, or NULL on error.
3204
 */
3205
BGD_DECLARE(gdImagePtr) gdImageCreateFromHeif(FILE *inFile);
3206
3207
/**
3208
 * @brief Create a truecolor image from HEIF data in memory.
3209
 *
3210
 * gdImageCreateFromHeifPtr() reads size bytes from data without taking
3211
 * ownership of the buffer. It decodes the primary image and returns a new
3212
 * truecolor image. On success, the returned image is owned by the caller and
3213
 * must be destroyed with @ref gdImageDestroy.
3214
 *
3215
 * @param size Size of data in bytes.
3216
 * @param data Pointer to the HEIF data.
3217
 * @return A newly allocated truecolor image, or NULL on error.
3218
 */
3219
BGD_DECLARE(gdImagePtr) gdImageCreateFromHeifPtr(int size, void *data);
3220
3221
/**
3222
 * @brief Create a truecolor image from HEIF data in memory using read options.
3223
 *
3224
 * gdImageCreateFromHeifPtrWithOptions() reads size bytes from data without
3225
 * taking ownership of the buffer. It borrows options for the duration of the
3226
 * call; pass NULL for gd defaults. On success, the returned image is owned by
3227
 * the caller and must be destroyed with @ref gdImageDestroy.
3228
 *
3229
 * @param size Size of data in bytes.
3230
 * @param data Pointer to the HEIF data.
3231
 * @param options HEIF read options, or NULL for defaults.
3232
 * @return A newly allocated truecolor image, or NULL on error.
3233
 */
3234
BGD_DECLARE(gdImagePtr)
3235
gdImageCreateFromHeifPtrWithOptions(int size, void *data, const gdHeifReadOptions *options);
3236
3237
/**
3238
 * @brief Create a truecolor image from HEIF data in an IO context.
3239
 *
3240
 * gdImageCreateFromHeifCtx() reads from infile without closing it. It decodes
3241
 * the primary image and returns a new truecolor image. On success, the returned
3242
 * image is owned by the caller and must be destroyed with @ref gdImageDestroy.
3243
 *
3244
 * @param infile The input IO context.
3245
 * @return A newly allocated truecolor image, or NULL on error.
3246
 */
3247
BGD_DECLARE(gdImagePtr) gdImageCreateFromHeifCtx(gdIOCtxPtr infile);
3248
3249
/** @} */
3250
3251
/** @name HEIF Writing */
3252
/** @{ */
3253
3254
/**
3255
 * @brief Write a truecolor image as HEIF data to a stdio stream.
3256
 *
3257
 * gdImageHeifEx() writes im with explicit quality, codec, and chroma settings.
3258
 * Quality may be 0 to 100 for lossy output, -1 for gd's default lossy quality,
3259
 * or 200 for lossless output. The image is borrowed for the duration of the
3260
 * call, and outFile is not closed.
3261
 *
3262
 * @param im The truecolor image to write.
3263
 * @param outFile Pointer to the output FILE stream.
3264
 * @param quality Lossy quality from 0 to 100, -1 for the default, or 200 for lossless.
3265
 * @param codec HEIF codec to use for output.
3266
 * @param chroma Chroma-subsampling string for output.
3267
 */
3268
BGD_DECLARE(void)
3269
gdImageHeifEx(gdImagePtr im, FILE *outFile, int quality, gdHeifCodec codec, gdHeifChroma chroma);
3270
3271
/**
3272
 * @brief Write a truecolor image as HEIF data to a stdio stream.
3273
 *
3274
 * gdImageHeif() uses gd's default HEIF write settings: default lossy quality,
3275
 * HEVC codec, and 4:4:4 chroma subsampling. The image is borrowed for the
3276
 * duration of the call, and outFile is not closed.
3277
 *
3278
 * @param im The truecolor image to write.
3279
 * @param outFile Pointer to the output FILE stream.
3280
 */
3281
BGD_DECLARE(void) gdImageHeif(gdImagePtr im, FILE *outFile);
3282
3283
/**
3284
 * @brief Encode a truecolor image as HEIF data in memory.
3285
 *
3286
 * gdImageHeifPtr() uses gd's default HEIF write settings. The image is borrowed
3287
 * for the duration of the call. On success, the returned buffer is owned by the
3288
 * caller and must be freed with gdFree().
3289
 *
3290
 * @param im The truecolor image to encode.
3291
 * @param size Pointer that receives the encoded buffer size in bytes.
3292
 * @return A newly allocated HEIF data buffer, or NULL on error.
3293
 */
3294
BGD_DECLARE(void *) gdImageHeifPtr(gdImagePtr im, int *size);
3295
3296
/**
3297
 * @brief Encode a truecolor image as HEIF data in memory.
3298
 *
3299
 * gdImageHeifPtrEx() writes im with explicit quality, codec, and chroma
3300
 * settings. Quality may be 0 to 100 for lossy output, -1 for gd's default
3301
 * lossy quality, or 200 for lossless output. The image is borrowed for the
3302
 * duration of the call. On success, the returned buffer is owned by the caller
3303
 * and must be freed with gdFree().
3304
 *
3305
 * @param im The truecolor image to encode.
3306
 * @param size Pointer that receives the encoded buffer size in bytes.
3307
 * @param quality Lossy quality from 0 to 100, -1 for the default, or 200 for lossless.
3308
 * @param codec HEIF codec to use for output.
3309
 * @param chroma Chroma-subsampling string for output.
3310
 * @return A newly allocated HEIF data buffer, or NULL on error.
3311
 */
3312
BGD_DECLARE(void *)
3313
gdImageHeifPtrEx(gdImagePtr im, int *size, int quality, gdHeifCodec codec, gdHeifChroma chroma);
3314
3315
/**
3316
 * @brief Encode a truecolor image as HEIF data in memory using write options.
3317
 *
3318
 * gdImageHeifPtrWithOptions() borrows im and options for the duration of the
3319
 * call. Pass NULL for options to use gd defaults. On success, the returned
3320
 * buffer is owned by the caller and must be freed with gdFree().
3321
 *
3322
 * @param im The truecolor image to encode.
3323
 * @param size Pointer that receives the encoded buffer size in bytes.
3324
 * @param options HEIF encoder options, or NULL for defaults.
3325
 * @return A newly allocated HEIF data buffer, or NULL on error.
3326
 */
3327
BGD_DECLARE(void *)
3328
gdImageHeifPtrWithOptions(gdImagePtr im, int *size, const gdHeifWriteOptions *options);
3329
3330
/**
3331
 * @brief Write HEIF data to a stdio stream using write options.
3332
 *
3333
 * The stream is borrowed and remains open. Returns 0 on success and nonzero
3334
 * on failure. Pass NULL for options to use gd defaults.
3335
 */
3336
BGD_DECLARE(int)
3337
gdImageHeifWithOptions(gdImagePtr im, FILE *outFile, const gdHeifWriteOptions *options);
3338
3339
/**
3340
 * @brief Write HEIF data to a gdIOCtx using write options.
3341
 *
3342
 * The context is borrowed and remains open. Returns 0 on success and nonzero
3343
 * on failure. Pass NULL for options to use gd defaults.
3344
 */
3345
BGD_DECLARE(int)
3346
gdImageHeifCtxWithOptions(gdImagePtr im, gdIOCtxPtr outfile, const gdHeifWriteOptions *options);
3347
3348
/**
3349
 * @brief Write a truecolor image as HEIF data to an IO context.
3350
 *
3351
 * gdImageHeifCtx() writes im with explicit quality, codec, and chroma settings.
3352
 * Quality may be 0 to 100 for lossy output, -1 for gd's default lossy quality,
3353
 * or 200 for lossless output. The image is borrowed for the duration of the
3354
 * call, and outfile is not closed.
3355
 *
3356
 * @param im The truecolor image to write.
3357
 * @param outfile The output IO context.
3358
 * @param quality Lossy quality from 0 to 100, -1 for the default, or 200 for lossless.
3359
 * @param codec HEIF codec to use for output.
3360
 * @param chroma Chroma-subsampling string for output.
3361
 */
3362
BGD_DECLARE(void)
3363
gdImageHeifCtx(gdImagePtr im, gdIOCtxPtr outfile, int quality, gdHeifCodec codec,
3364
               gdHeifChroma chroma);
3365
3366
/** @} */
3367
3368
/** @} */
3369
3370
/**
3371
 * @defgroup gdCodecAvif AVIF
3372
 * @brief Read and write AV1 Image File Format images.
3373
 * @ingroup gdCodecs
3374
 *
3375
 * AVIF support reads AVIF data from stdio streams, memory buffers, or gd IO
3376
 * contexts and returns truecolor images. If the AVIF input contains an image
3377
 * sequence, gd reads the first image and ignores subsequent frames. AVIF writing
3378
 * accepts truecolor images and can write to stdio streams, memory buffers, or
3379
 * gd IO contexts with quality, speed, lossless, and chroma-subsampling options.
3380
 *
3381
 * @code{.c}
3382
 * FILE *in;
3383
 * gdImagePtr im;
3384
 * gdAvifWriteOptions options;
3385
 * void *data;
3386
 * int size;
3387
 *
3388
 * in = fopen("input.avif", "rb");
3389
 * if (in == NULL) {
3390
 *     return 1;
3391
 * }
3392
 *
3393
 * im = gdImageCreateFromAvif(in);
3394
 * fclose(in);
3395
 * if (im == NULL) {
3396
 *     return 1;
3397
 * }
3398
 *
3399
 * gdAvifWriteOptionsInit(&options);
3400
 * options.quality = 90;
3401
 * options.speed = 6;
3402
 * options.chroma_subsampling = GD_AVIF_CHROMA_SUBSAMPLING_YUV444;
3403
 *
3404
 * data = gdImageAvifPtrWithOptions(im, &size, &options);
3405
 * if (data != NULL) {
3406
 *     gdFree(data);
3407
 * }
3408
 *
3409
 * gdImageDestroy(im);
3410
 * @endcode
3411
 *
3412
 * @{
3413
 */
3414
3415
/** @name AVIF Reading */
3416
/** @{ */
3417
3418
/**
3419
 * @brief Create a truecolor image from AVIF data in a stdio stream.
3420
 *
3421
 * gdImageCreateFromAvif() does not close inFile. If the AVIF contains an image
3422
 * sequence, only the first image is decoded. On success, the returned image is
3423
 * owned by the caller and must be destroyed with @ref gdImageDestroy.
3424
 *
3425
 * @param inFile Pointer to the input FILE stream.
3426
 * @return A newly allocated truecolor image, or NULL on error.
3427
 */
3428
BGD_DECLARE(gdImagePtr) gdImageCreateFromAvif(FILE *inFile);
3429
3430
/**
3431
 * @brief Create a truecolor image from AVIF data in memory.
3432
 *
3433
 * gdImageCreateFromAvifPtr() reads size bytes from data without taking
3434
 * ownership of the buffer. If the AVIF contains an image sequence, only the
3435
 * first image is decoded. On success, the returned image is owned by the caller
3436
 * and must be destroyed with @ref gdImageDestroy.
3437
 *
3438
 * @param size Size of data in bytes.
3439
 * @param data Pointer to the AVIF data.
3440
 * @return A newly allocated truecolor image, or NULL on error.
3441
 */
3442
BGD_DECLARE(gdImagePtr) gdImageCreateFromAvifPtr(int size, void *data);
3443
3444
/**
3445
 * @brief Create a truecolor image from AVIF data in an IO context.
3446
 *
3447
 * gdImageCreateFromAvifCtx() reads from infile without closing it. If the AVIF
3448
 * contains an image sequence, only the first image is decoded. On success, the
3449
 * returned image is owned by the caller and must be destroyed with
3450
 * @ref gdImageDestroy.
3451
 *
3452
 * @param infile The input IO context.
3453
 * @return A newly allocated truecolor image, or NULL on error.
3454
 */
3455
BGD_DECLARE(gdImagePtr) gdImageCreateFromAvifCtx(gdIOCtxPtr infile);
3456
3457
/** @} */
3458
3459
/** @name AVIF Write Options */
3460
/** @{ */
3461
3462
/** AVIF chroma subsampling modes for gdAvifWriteOptions::chroma_subsampling. */
3463
enum {
3464
    GD_AVIF_CHROMA_SUBSAMPLING_AUTO = 0,  /**< Choose subsampling automatically from the quality settings. */
3465
    GD_AVIF_CHROMA_SUBSAMPLING_YUV420 = 1, /**< Use 4:2:0 chroma subsampling. */
3466
    GD_AVIF_CHROMA_SUBSAMPLING_YUV444 = 2  /**< Use 4:4:4 chroma subsampling. */
3467
};
3468
3469
/** AVIF YUV pixel formats reported by gdAvifInfo::yuv_format. */
3470
enum {
3471
    GD_AVIF_PIXEL_FORMAT_NONE = 0,
3472
    GD_AVIF_PIXEL_FORMAT_YUV444 = 1,
3473
    GD_AVIF_PIXEL_FORMAT_YUV422 = 2,
3474
    GD_AVIF_PIXEL_FORMAT_YUV420 = 3,
3475
    GD_AVIF_PIXEL_FORMAT_YUV400 = 4
3476
};
3477
3478
/** @brief AVIF encoder options used by gdImageAvifPtrWithOptions(). */
3479
typedef struct {
3480
    int quality;            /**< Compression quality from 0 to 100, or -1 for the default. */
3481
    int speed;              /**< Encoder speed; lower values are slower and may improve compression. */
3482
    int lossless;           /**< Nonzero to request lossless encoding. */
3483
    int chroma_subsampling; /**< One of the GD_AVIF_CHROMA_SUBSAMPLING_* values. */
3484
    const gdImageMetadata *metadata; /**< Optional EXIF/XMP metadata to embed. */
3485
} gdAvifWriteOptions;
3486
3487
/**
3488
 * @brief Information extracted from the AVIF image and container.
3489
 *
3490
 * The fields describe facts available from the input, independently of the
3491
 * features currently decoded or written by GD. Dimensions, alpha, bit depth,
3492
 * and YUV format describe the primary image. Sequence fields describe the
3493
 * AVIF input when present. metadata is caller-owned and contains canonical
3494
 * EXIF and XMP profiles; ICC is not part of the public metadata path.
3495
 */
3496
typedef struct {
3497
    int width; /**< Image width in pixels. */
3498
    int height; /**< Image height in pixels. */
3499
    int is_animation; /**< Nonzero if the image is an animation. */
3500
    int is_progressive; /**< Nonzero if the image is progressive. */
3501
    int frame_count; /**< Number of frames in the animation. */
3502
    double duration; /**< Duration of the animation in seconds. */
3503
    int has_alpha; /**< Nonzero if the image has an alpha channel. */
3504
    int bit_depth; /**< Bit depth of the image. */
3505
    int yuv_format; /**< One of the GD_AVIF_PIXEL_FORMAT_* values. */
3506
    gdImageMetadata *metadata; /**< Pointer to the image metadata. */
3507
} gdAvifInfo;
3508
3509
/** @brief Initialize AVIF information and clear the metadata pointer.
3510
 *
3511
 * @param info Pointer to the gdAvifInfo structure to initialize.
3512
 */
3513
BGD_DECLARE(void) gdAvifInfoInit(gdAvifInfo *info);
3514
3515
/** @brief Read AVIF information from a stdio stream without closing it. *
3516
 *
3517
 * @param inFile Pointer to the input FILE stream.
3518
 * @param info Pointer to the gdAvifInfo structure to populate.
3519
 *
3520
 * @return Returns 1 on success, or 0 on failure.
3521
 */
3522
BGD_DECLARE(int) gdAvifGetInfo(FILE *inFile, gdAvifInfo *info);
3523
3524
/** @brief Read AVIF information from a gdIOCtx without closing it.
3525
 *
3526
 * @param in Pointer to the gdIOCtx input context.
3527
 * @param info Pointer to the gdAvifInfo structure to populate.
3528
 *
3529
 * @return Returns 1 on success, or 0 on failure.
3530
  */
3531
BGD_DECLARE(int) gdAvifGetInfoCtx(gdIOCtxPtr in, gdAvifInfo *info);
3532
3533
/** @brief Read AVIF information from memory without taking ownership.
3534
 *
3535
 * @param size Size of the AVIF memory buffer in bytes.
3536
 * @param data Pointer to the AVIF memory buffer.
3537
 * @param info Pointer to the gdAvifInfo structure to populate.
3538
 *
3539
 * @return Returns 1 on success, or 0 on failure.
3540
*/
3541
BGD_DECLARE(int) gdAvifGetInfoPtr(int size, const void *data, gdAvifInfo *info);
3542
3543
/**
3544
 * @brief Initialize AVIF write options with gd defaults.
3545
 *
3546
 * Call this before changing selected gdAvifWriteOptions fields and passing the
3547
 * structure to gdImageAvifPtrWithOptions().
3548
 *
3549
 * @param options Pointer to the options structure to initialize.
3550
 */
3551
BGD_DECLARE(void) gdAvifWriteOptionsInit(gdAvifWriteOptions *options);
3552
3553
/** @} */
3554
3555
/** @name AVIF Writing */
3556
/** @{ */
3557
3558
/**
3559
 * @brief Write a truecolor image as AVIF data to a stdio stream.
3560
 *
3561
 * gdImageAvif() uses gd's default AVIF quality and speed settings. The image is
3562
 * borrowed for the duration of the call, and outFile is not closed.
3563
 *
3564
 * @param im The truecolor image to write.
3565
 * @param outFile Pointer to the output FILE stream.
3566
 */
3567
BGD_DECLARE(void) gdImageAvif(gdImagePtr im, FILE *outFile);
3568
3569
/**
3570
 * @brief Write a truecolor image as AVIF data to a stdio stream.
3571
 *
3572
 * gdImageAvifEx() writes im with explicit quality and speed settings. Quality
3573
 * values range from 0 to 100, with higher values improving quality and 100
3574
 * requesting lossless output; -1 selects the default. Speed is passed to the
3575
 * AVIF encoder and clamped to its supported range. The image is borrowed for
3576
 * the duration of the call, and outFile is not closed.
3577
 *
3578
 * @param im The truecolor image to write.
3579
 * @param outFile Pointer to the output FILE stream.
3580
 * @param quality Compression quality from 0 to 100, or -1 for the default.
3581
 * @param speed Encoder speed; lower values are slower and may improve compression.
3582
 */
3583
BGD_DECLARE(void)
3584
gdImageAvifEx(gdImagePtr im, FILE *outFile, int quality, int speed);
3585
3586
/**
3587
 * @brief Encode a truecolor image as AVIF data in memory.
3588
 *
3589
 * gdImageAvifPtr() uses gd's default AVIF quality and speed settings. The image
3590
 * is borrowed for the duration of the call. On success, the returned buffer is
3591
 * owned by the caller and must be freed with gdFree().
3592
 *
3593
 * @param im The truecolor image to encode.
3594
 * @param size Pointer that receives the encoded buffer size in bytes.
3595
 * @return A newly allocated AVIF data buffer, or NULL on error.
3596
 */
3597
BGD_DECLARE(void *) gdImageAvifPtr(gdImagePtr im, int *size);
3598
3599
/**
3600
 * @brief Encode a truecolor image as AVIF data in memory.
3601
 *
3602
 * gdImageAvifPtrEx() writes im with explicit quality and speed settings.
3603
 * Quality values range from 0 to 100, with higher values improving quality and
3604
 * 100 requesting lossless output; -1 selects the default. Speed is passed to the
3605
 * AVIF encoder and clamped to its supported range. The image is borrowed for the
3606
 * duration of the call. On success, the returned buffer is owned by the caller
3607
 * and must be freed with gdFree().
3608
 *
3609
 * @param im The truecolor image to encode.
3610
 * @param size Pointer that receives the encoded buffer size in bytes.
3611
 * @param quality Compression quality from 0 to 100, or -1 for the default.
3612
 * @param speed Encoder speed; lower values are slower and may improve compression.
3613
 * @return A newly allocated AVIF data buffer, or NULL on error.
3614
 */
3615
BGD_DECLARE(void *)
3616
gdImageAvifPtrEx(gdImagePtr im, int *size, int quality, int speed);
3617
3618
/**
3619
 * @brief Encode a truecolor image as AVIF data in memory using write options.
3620
 *
3621
 * gdImageAvifPtrWithOptions() borrows im and options for the duration of the
3622
 * call. Pass NULL for options to use gd defaults. On success, the returned
3623
 * buffer is owned by the caller and must be freed with gdFree().
3624
 *
3625
 * @param im The truecolor image to encode.
3626
 * @param size Pointer that receives the encoded buffer size in bytes.
3627
 * @param options AVIF encoder options, or NULL for defaults.
3628
 * @return A newly allocated AVIF data buffer, or NULL on error.
3629
 */
3630
BGD_DECLARE(void *)
3631
gdImageAvifPtrWithOptions(gdImagePtr im, int *size, const gdAvifWriteOptions *options);
3632
3633
/** @brief Write AVIF data to a stdio stream using write options. */
3634
BGD_DECLARE(int)
3635
gdImageAvifWithOptions(gdImagePtr im, FILE *outFile, const gdAvifWriteOptions *options);
3636
3637
/** @brief Write AVIF data to a gdIOCtx using write options. */
3638
BGD_DECLARE(int)
3639
gdImageAvifCtxWithOptions(gdImagePtr im, gdIOCtxPtr outfile, const gdAvifWriteOptions *options);
3640
3641
/**
3642
 * @brief Write a truecolor image as AVIF data to an IO context.
3643
 *
3644
 * gdImageAvifCtx() writes im with explicit quality and speed settings. Quality
3645
 * values range from 0 to 100, with higher values improving quality and 100
3646
 * requesting lossless output; -1 selects the default. Speed is passed to the
3647
 * AVIF encoder and clamped to its supported range. The image is borrowed for
3648
 * the duration of the call, and outfile is not closed.
3649
 *
3650
 * @param im The truecolor image to write.
3651
 * @param outfile The output IO context.
3652
 * @param quality Compression quality from 0 to 100, or -1 for the default.
3653
 * @param speed Encoder speed; lower values are slower and may improve compression.
3654
 */
3655
BGD_DECLARE(void)
3656
gdImageAvifCtx(gdImagePtr im, gdIOCtxPtr outfile, int quality, int speed);
3657
3658
/** @} */
3659
3660
/** @} */
3661
3662
/**
3663
 * @defgroup gdCodecTiff TIFF
3664
 * @brief TIFF image reading, writing, and multi-page support.
3665
 * @ingroup gdCodecs
3666
 *
3667
 * TIFF support reads single images through the gdImageCreateFromTiff*()
3668
 * functions and reads multi-page TIFF files through gdTiffRead*() readers.
3669
 * The multi-page reader returns one caller-owned gd image per page. TIFF
3670
 * writers can write one or more truecolor images using explicit bit-depth,
3671
 * colorspace, compression, alpha, and resolution options.
3672
 *
3673
 * @code{.c}
3674
 *        gdTiffReadPtr reader;
3675
 *        gdTiffInfo info;
3676
 *        gdTiffPageInfo page;
3677
 *        gdImagePtr image;
3678
 *        FILE *in;
3679
 *
3680
 *        in = fopen("input.tif", "rb");
3681
 *        if (in == NULL) {
3682
 *          fprintf(stderr, "cannot open input.tif\n");
3683
 *          exit(1);
3684
 *        }
3685
 *
3686
 *        reader = gdTiffReadOpen(in, NULL);
3687
 *        fclose(in);
3688
 *        if (reader == NULL) {
3689
 *          fprintf(stderr, "cannot read TIFF\n");
3690
 *          exit(1);
3691
 *        }
3692
 *
3693
 *        if (gdTiffReadGetInfo(reader, &info)) {
3694
 *          printf("pages: %d, first page: %dx%d\n",
3695
 *                 info.pageCount, info.width, info.height);
3696
 *        }
3697
 *
3698
 *        while (gdTiffReadNextImage(reader, &page, &image) == 1) {
3699
 *          printf("page %d: %dx%d\n", page.pageIndex, page.width, page.height);
3700
 *          gdImageDestroy(image);
3701
 *        }
3702
 *
3703
 *        gdTiffReadClose(reader);
3704
 * @endcode
3705
 *
3706
 * @{
3707
 */
3708
3709
/** @name TIFF Single-Image Reading */
3710
/** @{ */
3711
3712
/**
3713
 * @brief Create an image from a TIFF stdio file.
3714
 *
3715
 * gdImageCreateFromTiff() does not close inFile. The returned image is
3716
 * caller-owned and must be destroyed with @ref gdImageDestroy.
3717
 *
3718
 * @param inFile Pointer to the input FILE stream.
3719
 *
3720
 * @return Returns a gdImagePtr on success, or NULL on failure.
3721
 */
3722
BGD_DECLARE(gdImagePtr) gdImageCreateFromTiff(FILE *inFile);
3723
3724
/**
3725
 * @brief Create an image from TIFF data read through a gdIOCtx.
3726
 *
3727
 * gdImageCreateFromTiffCtx() does not close infile. The returned image is
3728
 * caller-owned and must be destroyed with @ref gdImageDestroy.
3729
 *
3730
 * @param infile Pointer to the gdIOCtx input context.
3731
 *
3732
 * @return Returns a gdImagePtr on success, or NULL on failure.
3733
 */
3734
BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffCtx(gdIOCtxPtr infile);
3735
3736
/**
3737
 * @brief Create an image from a TIFF memory buffer.
3738
 *
3739
 * The data buffer is borrowed for the duration of the call. The returned image
3740
 * is caller-owned and must be destroyed with @ref gdImageDestroy.
3741
 *
3742
 * @param size Size of the TIFF memory buffer in bytes.
3743
 * @param data Pointer to the TIFF memory buffer.
3744
 *
3745
 * @return Returns a gdImagePtr on success, or NULL on failure.
3746
 */
3747
BGD_DECLARE(gdImagePtr) gdImageCreateFromTiffPtr(int size, void *data);
3748
3749
/** @} */
3750
3751
/** @name TIFF Reading Types */
3752
/** @{ */
3753
3754
/**
3755
 * @brief Opaque TIFF multi-page reader handle.
3756
 *
3757
 * Handles returned by gdTiffReadOpen(), gdTiffReadOpenCtx(), or
3758
 * gdTiffReadOpenPtr() must be closed with gdTiffReadClose().
3759
 */
3760
typedef struct gdTiffReadStruct *gdTiffReadPtr;
3761
3762
/**
3763
 * @brief Options for reading TIFF data with the gdTiffRead*() API.
3764
 *
3765
 * Initialize with gdTiffReadOptionsInit() before setting fields.
3766
 */
3767
typedef struct {
3768
    int notused;  /**< Reserved for future read options; currently unused. */
3769
} gdTiffReadOptions;
3770
3771
/**
3772
 * @brief TIFF file information from the first page and container.
3773
 */
3774
typedef struct {
3775
    int width;              /**< First page width in pixels. */
3776
    int height;             /**< First page height in pixels. */
3777
    int page_count;         /**< Number of TIFF directories/pages in the file. */
3778
    int bits_per_sample;    /**< First page bits per sample. */
3779
    int samples_per_pixel;  /**< First page samples per pixel. */
3780
    int compression;        /**< First page compression tag value, usually a GD_TIFF_COMPRESSION_* enum value. */
3781
    int photometric;        /**< First page photometric tag value, usually a GD_TIFF_PHOTOMETRIC_* enum value. */
3782
    float x_resolution;     /**< First page horizontal resolution. */
3783
    float y_resolution;     /**< First page vertical resolution. */
3784
    int resolution_unit;    /**< First page resolution unit, one of the GD_TIFF_RESUNIT_* enum values. */
3785
} gdTiffInfo;
3786
3787
/**
3788
 * @brief TIFF page information returned while reading pages.
3789
 */
3790
typedef struct {
3791
    int page_index;         /**< Zero-based page index. */
3792
    int width;              /**< Page width in pixels. */
3793
    int height;             /**< Page height in pixels. */
3794
    int bits_per_sample;    /**< Page bits per sample. */
3795
    int samples_per_pixel;  /**< Page samples per pixel. */
3796
    int compression;        /**< Page compression tag value, usually a GD_TIFF_COMPRESSION_* enum value. */
3797
    int photometric;        /**< Page photometric tag value, usually a GD_TIFF_PHOTOMETRIC_* enum value. */
3798
    int planar;             /**< Page planar configuration, one of the GD_TIFF_PLANARCONFIG_* enum values. */
3799
    int has_alpha;          /**< Non-zero if the page has extra alpha samples. */
3800
    int is_tiled;           /**< Non-zero if the page is stored as TIFF tiles. */
3801
    float x_resolution;     /**< Page horizontal resolution. */
3802
    float y_resolution;     /**< Page vertical resolution. */
3803
    int resolution_unit;    /**< Page resolution unit, one of the GD_TIFF_RESUNIT_* enum values. */
3804
} gdTiffPageInfo;
3805
3806
/** @} */
3807
3808
/** @name TIFF Multi-Page Reading */
3809
/** @{ */
3810
3811
/**
3812
 * @brief Test whether a TIFF stdio file has more than one page.
3813
 *
3814
 * gdTiffIsMultiPage() reads from fd and attempts to restore its stream
3815
 * position before returning. It does not close fd.
3816
 *
3817
 * @param fd Pointer to the input FILE stream.
3818
 *
3819
 * @return Returns 1 for multi-page TIFF, 0 for single-page TIFF, or -1 on error.
3820
 */
3821
BGD_DECLARE(int) gdTiffIsMultiPage(FILE *fd);
3822
3823
/**
3824
 * @brief Test whether TIFF data read through a seekable gdIOCtx has more than one page.
3825
 *
3826
 * gdTiffIsMultiPageCtx() reads from in and attempts to restore its position
3827
 * before returning. It does not close in.
3828
 *
3829
 * @param in Pointer to the gdIOCtx input context.
3830
 *
3831
 * @return Returns 1 for multi-page TIFF, 0 for single-page TIFF, or -1 on error.
3832
 */
3833
BGD_DECLARE(int) gdTiffIsMultiPageCtx(gdIOCtxPtr in);
3834
3835
/**
3836
 * @brief Test whether a TIFF memory buffer has more than one page.
3837
 *
3838
 * The data buffer is borrowed for the duration of the call.
3839
 *
3840
 * @param size Size of the TIFF memory buffer in bytes.
3841
 * @param data Pointer to the TIFF memory buffer.
3842
 *
3843
 * @return Returns 1 for multi-page TIFF, 0 for single-page TIFF, or -1 on error.
3844
 */
3845
BGD_DECLARE(int) gdTiffIsMultiPagePtr(int size, void *data);
3846
3847
/**
3848
 * @brief Initialize TIFF read options with defaults.
3849
 *
3850
 * @param options Pointer to the options structure to initialize.
3851
 */
3852
BGD_DECLARE(void) gdTiffReadOptionsInit(gdTiffReadOptions *options);
3853
3854
/**
3855
 * @brief Open a TIFF multi-page reader from a stdio file.
3856
 *
3857
 * gdTiffReadOpen() reads the TIFF data into the reader and does not close fd.
3858
 * The returned handle must be closed with gdTiffReadClose().
3859
 *
3860
 * @param fd Pointer to the input FILE stream.
3861
 * @param options Pointer to read options, or NULL for defaults.
3862
 *
3863
 * @return Returns a TIFF reader handle on success, or NULL on failure.
3864
 */
3865
BGD_DECLARE(gdTiffReadPtr) gdTiffReadOpen(FILE *fd, const gdTiffReadOptions *options);
3866
3867
/**
3868
 * @brief Open a TIFF multi-page reader from a gdIOCtx.
3869
 *
3870
 * gdTiffReadOpenCtx() reads the TIFF data into the reader and does not close
3871
 * in. The returned handle must be closed with gdTiffReadClose().
3872
 *
3873
 * @param in Pointer to the gdIOCtx input context.
3874
 * @param options Pointer to read options, or NULL for defaults.
3875
 *
3876
 * @return Returns a TIFF reader handle on success, or NULL on failure.
3877
 */
3878
BGD_DECLARE(gdTiffReadPtr) gdTiffReadOpenCtx(gdIOCtxPtr in, const gdTiffReadOptions *options);
3879
3880
/**
3881
 * @brief Open a TIFF multi-page reader from a memory buffer.
3882
 *
3883
 * The data buffer is borrowed for the duration of the call. The returned
3884
 * reader owns its copy of the TIFF data and must be closed with
3885
 * gdTiffReadClose().
3886
 *
3887
 * @param size Size of the TIFF memory buffer in bytes.
3888
 * @param data Pointer to the TIFF memory buffer.
3889
 * @param options Pointer to read options, or NULL for defaults.
3890
 *
3891
 * @return Returns a TIFF reader handle on success, or NULL on failure.
3892
 */
3893
BGD_DECLARE(gdTiffReadPtr) gdTiffReadOpenPtr(int size, void *data, const gdTiffReadOptions *options);
3894
3895
/**
3896
 * @brief Close a TIFF multi-page reader.
3897
 *
3898
 * @param tiff TIFF reader handle to close, or NULL.
3899
 */
3900
BGD_DECLARE(void) gdTiffReadClose(gdTiffReadPtr tiff);
3901
3902
/**
3903
 * @brief Get TIFF file information from a reader.
3904
 *
3905
 * @param tiff TIFF reader handle.
3906
 * @param info Pointer to a gdTiffInfo structure to receive file information.
3907
 *
3908
 * @return Returns 1 on success, or 0 on failure.
3909
 */
3910
BGD_DECLARE(int) gdTiffReadGetInfo(gdTiffReadPtr tiff, gdTiffInfo *info);
3911
3912
/** Collect opaque metadata from the first TIFF directory. */
3913
BGD_DECLARE(int) gdTiffReadGetMetadata(gdTiffReadPtr tiff, gdImageMetadata *metadata);
3914
3915
/**
3916
 * @brief Read the next TIFF page image.
3917
 *
3918
 * When image is not NULL and the function returns 1, *image receives a
3919
 * caller-owned gd image that must be destroyed with @ref gdImageDestroy. Passing
3920
 * NULL for image advances the reader without returning the decoded image.
3921
 *
3922
 * @param tiff TIFF reader handle.
3923
 * @param info Pointer to a gdTiffPageInfo structure to receive page information, or NULL.
3924
 * @param image Pointer to receive the caller-owned page image, or NULL.
3925
 *
3926
 * @return Returns 1 when a page is read, 0 at end of file, or -1 on error.
3927
 */
3928
BGD_DECLARE(int)
3929
gdTiffReadNextImage(gdTiffReadPtr tiff, gdTiffPageInfo *info, gdImagePtr *image);
3930
3931
/** @} */
3932
3933
/** @name TIFF Constants */
3934
/** @{ */
3935
3936
/**
3937
 * @brief TIFF writer color spaces.
3938
 */
3939
typedef enum {
3940
    GD_TIFF_RGB = 1,     /**< Write RGB TIFF data. */
3941
    GD_TIFF_RGBA = 2,    /**< Write RGBA TIFF data with an alpha extra sample. */
3942
    GD_TIFF_GRAY = 3,    /**< Write grayscale TIFF data. */
3943
    GD_TIFF_BILEVEL = 4  /**< Write 1-bit bilevel TIFF data. */
3944
} gdTiffColorSpace;
3945
3946
/**
3947
 * @brief TIFF compression tag values.
3948
 */
3949
typedef enum {
3950
    GD_TIFF_COMPRESSION_NONE = 1,            /**< No TIFF compression. */
3951
    GD_TIFF_COMPRESSION_CCITT_RLE = 2,       /**< CCITT modified Huffman run-length encoding compression. */
3952
    GD_TIFF_COMPRESSION_CCITT_FAX3 = 3,      /**< CCITT Group 3 fax compression. */
3953
    GD_TIFF_COMPRESSION_CCITT_FAX4 = 4,      /**< CCITT Group 4 fax compression. */
3954
    GD_TIFF_COMPRESSION_LZW = 5,             /**< LZW compression. */
3955
    GD_TIFF_COMPRESSION_JPEG = 7,            /**< JPEG compression. */
3956
    GD_TIFF_COMPRESSION_ADOBE_DEFLATE = 8,   /**< Adobe-style Deflate compression. */
3957
    GD_TIFF_COMPRESSION_DEFLATE = 32946,     /**< Deflate compression. */
3958
    GD_TIFF_COMPRESSION_PACKBITS = 32773     /**< PackBits compression. */
3959
} gdTiffCompression;
3960
3961
/**
3962
 * @brief TIFF photometric interpretation tag values.
3963
 */
3964
typedef enum {
3965
    GD_TIFF_PHOTOMETRIC_MINISWHITE = 0,          /**< White is the minimum sample value. */
3966
    GD_TIFF_PHOTOMETRIC_MINISBLACK = 1,          /**< Black is the minimum sample value. */
3967
    GD_TIFF_PHOTOMETRIC_RGB = 2,                 /**< RGB photometric interpretation. */
3968
    GD_TIFF_PHOTOMETRIC_PALETTE = 3,             /**< Palette color photometric interpretation. */
3969
    GD_TIFF_PHOTOMETRIC_TRANSPARENCY_MASK = 4,   /**< Transparency mask photometric interpretation. */
3970
    GD_TIFF_PHOTOMETRIC_SEPARATED = 5,           /**< Separated photometric interpretation. */
3971
    GD_TIFF_PHOTOMETRIC_YCBCR = 6,               /**< YCbCr photometric interpretation. */
3972
    GD_TIFF_PHOTOMETRIC_CIELAB = 8               /**< CIE L*a*b* photometric interpretation. */
3973
} gdTiffPhotometric;
3974
3975
/**
3976
 * @brief TIFF planar configuration tag values.
3977
 */
3978
typedef enum {
3979
    GD_TIFF_PLANARCONFIG_CONTIG = 1,    /**< Store samples for each pixel contiguously. */
3980
    GD_TIFF_PLANARCONFIG_SEPARATE = 2   /**< Store samples in separate planes. */
3981
} gdTiffPlanarConfig;
3982
3983
/**
3984
 * @brief TIFF resolution unit tag values.
3985
 */
3986
typedef enum {
3987
    GD_TIFF_RESUNIT_NONE = 1,       /**< Resolution values have no absolute unit. */
3988
    GD_TIFF_RESUNIT_INCH = 2,       /**< Resolution values are pixels per inch. */
3989
    GD_TIFF_RESUNIT_CENTIMETER = 3  /**< Resolution values are pixels per centimeter. */
3990
} gdTiffResolutionUnit;
3991
3992
/**
3993
 * @brief TIFF alpha sample types.
3994
 */
3995
typedef enum {
3996
    GD_TIFF_ALPHA_UNASSOCIATED = 1,  /**< TIFF alpha samples are unassociated with color samples. */
3997
    GD_TIFF_ALPHA_ASSOCIATED = 2     /**< TIFF alpha samples are premultiplied into color samples. */
3998
} gdTiffAlphaType;
3999
4000
/** @} */
4001
4002
/** @name TIFF Writing Types */
4003
/** @{ */
4004
4005
/**
4006
 * @brief Options for writing TIFF data with the gdTiffWrite*() API.
4007
 *
4008
 * Initialize with gdTiffWriteOptionsInit() before setting fields. NULL options
4009
 * use defaults: 8-bit RGBA, Adobe Deflate compression, inch resolution units,
4010
 * 72x72 resolution, and unassociated alpha.
4011
 */
4012
typedef struct {
4013
    int bitDepth;        /**< Bits per sample: 1, 8, or 16. */
4014
    gdTiffColorSpace colorspace;       /**< Output colorspace. */
4015
    gdTiffCompression compression;     /**< TIFF compression. */
4016
    int jpegQuality;     /**< JPEG compression quality when compression is GD_TIFF_COMPRESSION_JPEG. */
4017
    int minIsWhite;      /**< Non-zero to use white as the minimum sample value for gray or bilevel output. */
4018
    gdTiffResolutionUnit resolutionUnit;  /**< Resolution unit. */
4019
    float xResolution;   /**< Horizontal resolution to store in the TIFF file. */
4020
    float yResolution;   /**< Vertical resolution to store in the TIFF file. */
4021
    gdTiffAlphaType alphaType;         /**< Alpha sample type. */
4022
    const gdImageMetadata *metadata;   /**< Opaque TIFF tag metadata. */
4023
} gdTiffWriteOptions;
4024
4025
/**
4026
 * @brief Opaque TIFF writer handle.
4027
 *
4028
 * Handles returned by gdTiffWriteOpen() or gdTiffWriteOpenCtx() must be closed
4029
 * with gdTiffWriteClose(). Handles returned by gdTiffWriteOpenPtr() must be
4030
 * finished with gdTiffWritePtrFinish().
4031
 */
4032
typedef struct gdTiffWriteStruct *gdTiffWritePtr;
4033
4034
/** @} */
4035
4036
/** @name TIFF Multi-Page Writing */
4037
/** @{ */
4038
4039
/**
4040
 * @brief Initialize TIFF write options with defaults.
4041
 *
4042
 * @param options Pointer to the options structure to initialize.
4043
 */
4044
BGD_DECLARE(void) gdTiffWriteOptionsInit(gdTiffWriteOptions *options);
4045
4046
/**
4047
 * @brief Open a TIFF writer for a stdio file.
4048
 *
4049
 * gdTiffWriteOpen() does not close outFile. The returned handle must be
4050
 * closed with gdTiffWriteClose().
4051
 *
4052
 * @param outFile Pointer to the output FILE stream.
4053
 * @param options Pointer to write options, or NULL for defaults.
4054
 *
4055
 * @return Returns a TIFF writer handle on success, or NULL on failure.
4056
 */
4057
BGD_DECLARE(gdTiffWritePtr)
4058
gdTiffWriteOpen(FILE *outFile, const gdTiffWriteOptions *options);
4059
4060
/**
4061
 * @brief Open a TIFF writer for a gdIOCtx.
4062
 *
4063
 * The output context is borrowed and is not closed by gdTiffWriteClose(). The
4064
 * returned handle must be closed with gdTiffWriteClose().
4065
 *
4066
 * @param out Pointer to the gdIOCtx output context.
4067
 * @param options Pointer to write options, or NULL for defaults.
4068
 *
4069
 * @return Returns a TIFF writer handle on success, or NULL on failure.
4070
 */
4071
BGD_DECLARE(gdTiffWritePtr)
4072
gdTiffWriteOpenCtx(gdIOCtxPtr out, const gdTiffWriteOptions *options);
4073
4074
/**
4075
 * @brief Open a TIFF writer that returns a memory buffer.
4076
 *
4077
 * The returned handle must be finished with gdTiffWritePtrFinish().
4078
 *
4079
 * @param options Pointer to write options, or NULL for defaults.
4080
 *
4081
 * @return Returns a TIFF memory writer handle on success, or NULL on failure.
4082
 */
4083
BGD_DECLARE(gdTiffWritePtr)
4084
gdTiffWriteOpenPtr(const gdTiffWriteOptions *options);
4085
4086
/**
4087
 * @brief Add an image as the next TIFF page.
4088
 *
4089
 * The image is borrowed for the duration of the call and remains owned by the
4090
 * caller. This writer API accepts truecolor images only.
4091
 *
4092
 * @param write TIFF writer handle.
4093
 * @param image Image to add as the next page.
4094
 *
4095
 * @return Returns 1 on success, or 0 on failure.
4096
 */
4097
BGD_DECLARE(int) gdTiffWriteAddImage(gdTiffWritePtr write, gdImagePtr image);
4098
4099
/**
4100
 * @brief Close a file or gdIOCtx TIFF writer.
4101
 *
4102
 * Use this for handles returned by gdTiffWriteOpen() or gdTiffWriteOpenCtx().
4103
 * For memory writers returned by gdTiffWriteOpenPtr(), use
4104
 * gdTiffWritePtrFinish().
4105
 *
4106
 * @param write TIFF writer handle to close, or NULL.
4107
 */
4108
BGD_DECLARE(void) gdTiffWriteClose(gdTiffWritePtr write);
4109
4110
/**
4111
 * @brief Finish a TIFF memory writer and return the encoded buffer.
4112
 *
4113
 * This closes write whether encoding succeeds or fails. The returned buffer is
4114
 * caller-owned and must be freed with gdFree().
4115
 *
4116
 * @param write TIFF memory writer handle returned by gdTiffWriteOpenPtr().
4117
 * @param size Pointer to an integer that receives the returned buffer size.
4118
 *
4119
 * @return Returns a pointer to the newly allocated TIFF buffer, or NULL on failure.
4120
 */
4121
BGD_DECLARE(void *) gdTiffWritePtrFinish(gdTiffWritePtr write, int *size);
4122
4123
/** @} */
4124
4125
/** @name TIFF Single-Image Writing */
4126
/** @{ */
4127
4128
/**
4129
 * @brief Write an image as TIFF data to a stdio file.
4130
 *
4131
 * gdImageTiff() does not close outFile. The image is borrowed for the duration
4132
 * of the call.
4133
 *
4134
 * @param im The image to write.
4135
 * @param outFile Pointer to the output FILE stream.
4136
 */
4137
BGD_DECLARE(void) gdImageTiff(gdImagePtr im, FILE *outFile);
4138
4139
/**
4140
 * @brief Write an image as TIFF data to a newly allocated memory buffer.
4141
 *
4142
 * The image is borrowed for the duration of the call. The returned buffer is
4143
 * caller-owned and must be freed with gdFree().
4144
 *
4145
 * @param im The image to write.
4146
 * @param size Pointer to an integer that receives the returned buffer size.
4147
 *
4148
 * @return Returns a pointer to the newly allocated TIFF buffer, or NULL on failure.
4149
 */
4150
BGD_DECLARE(void *) gdImageTiffPtr(gdImagePtr im, int *size);
4151
4152
/**
4153
 * @brief Write an image as TIFF data to a gdIOCtx.
4154
 *
4155
 * gdImageTiffCtx() does not close out. The image is borrowed for the duration
4156
 * of the call.
4157
 *
4158
 * @param image The image to write.
4159
 * @param out Pointer to the gdIOCtx output context.
4160
 */
4161
BGD_DECLARE(void) gdImageTiffCtx(gdImagePtr image, gdIOCtxPtr out);
4162
4163
/** @} */
4164
/** @} */
4165
4166
/**
4167
 * @defgroup gdCodecTga TGA
4168
 * @brief Read Truevision TGA images.
4169
 * @ingroup gdCodecs
4170
 *
4171
 * TGA support is read-only. The reader accepts stdio streams, gdIOCtx streams,
4172
 * and caller-provided memory buffers, and returns a new truecolor gd image.
4173
 * The returned image is owned by the caller and must be destroyed with
4174
 * gdImageDestroy().
4175
 *
4176
 * GD reads uncompressed and RLE-compressed color-mapped, truecolor, and
4177
 * grayscale TGA images. Supported inputs include 8-bit indexed data with
4178
 * 15-, 16-, 24-, or 32-bit color map entries, 16- and 24-bit truecolor data,
4179
 * 32-bit truecolor data with 8 alpha bits, and 8-bit grayscale data. Image
4180
 * origin flags are applied so the returned gd image has the expected
4181
 * orientation. When decoded alpha is present, alpha saving is enabled on the
4182
 * returned image.
4183
 *
4184
 * @code{.c}
4185
 * FILE *in;
4186
 * FILE *out;
4187
 * gdImagePtr im;
4188
 *
4189
 * in = fopen("input.tga", "rb");
4190
 * if (in == NULL) {
4191
 *     return 1;
4192
 * }
4193
 *
4194
 * im = gdImageCreateFromTga(in);
4195
 * fclose(in);
4196
 * if (im == NULL) {
4197
 *     return 1;
4198
 * }
4199
 *
4200
 * out = fopen("output.png", "wb");
4201
 * if (out == NULL) {
4202
 *     gdImageDestroy(im);
4203
 *     return 1;
4204
 * }
4205
 *
4206
 * gdImagePng(im, out);
4207
 * fclose(out);
4208
 * gdImageDestroy(im);
4209
 * @endcode
4210
 *
4211
 * @{
4212
 */
4213
4214
/** @name TGA Reading */
4215
/** @{ */
4216
4217
/**
4218
 * @brief Create an image from TGA data in a stdio stream.
4219
 *
4220
 * gdImageCreateFromTga() borrows fp for the duration of the call and does not
4221
 * close it. On success, the returned image is owned by the caller and must be
4222
 * destroyed with gdImageDestroy().
4223
 *
4224
 * @param fp Pointer to the input FILE stream.
4225
 * @return A newly allocated truecolor image, or NULL on error.
4226
 */
4227
BGD_DECLARE(gdImagePtr) gdImageCreateFromTga(FILE *fp);
4228
4229
/**
4230
 * @brief Create an image from TGA data in a gdIOCtx.
4231
 *
4232
 * gdImageCreateFromTgaCtx() borrows ctx for the duration of the call and does
4233
 * not close it. On success, the returned image is owned by the caller and must
4234
 * be destroyed with gdImageDestroy().
4235
 *
4236
 * @param ctx Pointer to the gdIOCtx input context.
4237
 * @return A newly allocated truecolor image, or NULL on error.
4238
 */
4239
BGD_DECLARE(gdImagePtr) gdImageCreateFromTgaCtx(gdIOCtxPtr ctx);
4240
4241
/**
4242
 * @brief Create an image from a TGA memory buffer.
4243
 *
4244
 * gdImageCreateFromTgaPtr() borrows data for the duration of the call. The
4245
 * caller retains ownership of the input buffer. On success, the returned image
4246
 * is owned by the caller and must be destroyed with gdImageDestroy().
4247
 *
4248
 * @param size Size of the TGA memory buffer in bytes.
4249
 * @param data Pointer to the TGA memory buffer.
4250
 * @return A newly allocated truecolor image, or NULL on error.
4251
 */
4252
BGD_DECLARE(gdImagePtr) gdImageCreateFromTgaPtr(int size, void *data);
4253
4254
/** @} */
4255
4256
/** @} */
4257
4258
/**
4259
 * @defgroup gdCodecBmp BMP
4260
 * @brief Read and write Microsoft Windows bitmap images.
4261
 * @ingroup gdCodecs
4262
 *
4263
 * BMP support reads stdio files, gdIOCtx streams, or caller-provided memory
4264
 * buffers into gd images. Indexed BMP inputs are returned as palette images
4265
 * when possible, while direct-color BMP inputs are returned as truecolor
4266
 * images. The returned image is owned by the caller and must be destroyed with
4267
 * @ref gdImageDestroy.
4268
 *
4269
 * BMP output can use the legacy automatic-bit-depth APIs or the extended APIs
4270
 * that select a specific BMP bit depth, compression mode, and writer flags.
4271
 * The writer supports 1, 4, 8, 16, 24, and 32 bits per pixel. RLE4 is valid
4272
 * only for 4 bpp output, and RLE8 is valid only for 8 bpp output. BMP output
4273
 * handles and FILE streams are borrowed and are not closed by gd.
4274
 *
4275
 * @code{.c}
4276
 * gdImagePtr im, roundtrip;
4277
 * void *data;
4278
 * int size;
4279
 *
4280
 * im = gdImageCreateTrueColor(32, 32);
4281
 * gdImageFilledRectangle(im, 0, 0, 31, 31, 0x336699);
4282
 *
4283
 * data = gdImageBmpPtrEx(im, &size, 24, GD_BMP_COMPRESS_NONE,
4284
 *                        GD_BMP_FLAG_NONE);
4285
 * if (data != NULL) {
4286
 *        roundtrip = gdImageCreateFromBmpPtr(size, data);
4287
 *        gdFree(data);
4288
 *        if (roundtrip != NULL) {
4289
 *                gdImageDestroy(roundtrip);
4290
 *        }
4291
 * }
4292
 *
4293
 * gdImageDestroy(im);
4294
 * @endcode
4295
 * @{
4296
 */
4297
4298
/**
4299
 * @brief Create an image from a BMP stdio file.
4300
 *
4301
 * gdImageCreateFromBmp() reads from the current position of inFile and does
4302
 * not close it. On success, the returned image is owned by the caller and must
4303
 * be destroyed with @ref gdImageDestroy.
4304
 *
4305
 * @param inFile Pointer to the BMP FILE stream to read.
4306
 * @return A newly allocated image, or NULL on error.
4307
 */
4308
BGD_DECLARE(gdImagePtr) gdImageCreateFromBmp(FILE *inFile);
4309
4310
/**
4311
 * @brief Create an image from a BMP memory buffer.
4312
 *
4313
 * gdImageCreateFromBmpPtr() borrows data for the duration of the call. The
4314
 * caller retains ownership of the input buffer. On success, the returned image
4315
 * is owned by the caller and must be destroyed with @ref gdImageDestroy.
4316
 *
4317
 * @param size Size of the BMP memory buffer in bytes.
4318
 * @param data Pointer to the BMP memory buffer.
4319
 * @return A newly allocated image, or NULL on error.
4320
 */
4321
BGD_DECLARE(gdImagePtr) gdImageCreateFromBmpPtr(int size, void *data);
4322
4323
/**
4324
 * @brief Create an image from BMP data read through a gdIOCtx.
4325
 *
4326
 * gdImageCreateFromBmpCtx() reads from infile and does not close it. On
4327
 * success, the returned image is owned by the caller and must be destroyed with
4328
 * @ref gdImageDestroy.
4329
 *
4330
 * @param infile Pointer to the gdIOCtx input context.
4331
 * @return A newly allocated image, or NULL on error.
4332
 */
4333
BGD_DECLARE(gdImagePtr) gdImageCreateFromBmpCtx(gdIOCtxPtr infile);
4334
4335
/** Descriptive facts read from a BMP file header. */
4336
typedef struct {
4337
  int header_type; /**< Type of the BMP header. */
4338
  int width; /**< Image width in pixels. */
4339
  int height; /**< Image height in pixels. */
4340
  int top_down; /**< Nonzero if the image is stored top-down. */
4341
  int planes; /**< Number of color planes. */
4342
  int bits_per_pixel; /**< Number of bits per pixel. */
4343
  int compression; /**< Compression method used. */
4344
  int image_size; /**< Size of the pixel data in bytes. */
4345
  int horizontal_resolution; /**< Horizontal resolution in pixels per meter. */
4346
  int vertical_resolution; /**< Vertical resolution in pixels per meter. */
4347
  int colors_used; /**< Number of colors used in the palette. */
4348
  int important_colors; /**< Number of important colors. */
4349
  int palette_type; /**< Type of the palette. */
4350
  int palette_entries; /**< Number of entries in the palette. */
4351
  unsigned int red_mask; /**< Red channel mask. */
4352
  unsigned int green_mask; /**< Green channel mask. */
4353
  unsigned int blue_mask; /**< Blue channel mask. */
4354
  unsigned int alpha_mask; /**< Alpha channel mask. */
4355
} gdBmpInfo;
4356
4357
/**
4358
 * @brief Initialize a gdBmpInfo structure to default values.
4359
 *
4360
 * @param info Pointer to the gdBmpInfo structure to initialize.
4361
 */
4362
BGD_DECLARE(void) gdBmpInfoInit(gdBmpInfo *info);
4363
4364
/**
4365
 * @brief Read BMP file information from a stdio file.
4366
 *
4367
 * @param infile Pointer to the BMP FILE stream to read.
4368
 * @param info Pointer to a gdBmpInfo structure to receive the BMP information.
4369
 *
4370
 * @return Returns 1 on success, or 0 on failure.
4371
 */
4372
BGD_DECLARE(int) gdBmpGetInfo(FILE *infile, gdBmpInfo *info);
4373
4374
/**
4375
 * @brief Read BMP file information from a gdIOCtx.
4376
 *
4377
 * @param infile Pointer to the gdIOCtx input context.
4378
 * @param info Pointer to a gdBmpInfo structure to receive the BMP information.
4379
 *
4380
 * @return Returns 1 on success, or 0 on failure.
4381
 */
4382
BGD_DECLARE(int) gdBmpGetInfoCtx(gdIOCtxPtr infile, gdBmpInfo *info);
4383
4384
/**
4385
 * @brief Read BMP file information from a memory buffer.
4386
 *
4387
 * @param size Size of the BMP memory buffer in bytes.
4388
 * @param data Pointer to the BMP memory buffer.
4389
 * @param info Pointer to a gdBmpInfo structure to receive the BMP information.
4390
 *
4391
 * @return Returns 1 on success, or 0 on failure.
4392
 */
4393
BGD_DECLARE(int) gdBmpGetInfoPtr(int size, const void *data, gdBmpInfo *info);
4394
4395
/**
4396
 * @brief Write an image as BMP data to a newly allocated memory buffer.
4397
 *
4398
 * gdImageBmpPtr() uses automatic BMP bit-depth selection. A zero compression
4399
 * value writes uncompressed BMP data; a nonzero value requests legacy RLE
4400
 * output when the automatically selected BMP bit depth supports it. The image
4401
 * is borrowed for the duration of the call. On success, the returned buffer
4402
 * must be freed with gdFree().
4403
 *
4404
 * @param im The image to write.
4405
 * @param size Output location for the returned buffer size in bytes.
4406
 * @param compression Legacy compression selector; zero disables RLE, nonzero
4407
 *        requests RLE when supported by the selected output bit depth.
4408
 * @return A newly allocated BMP buffer, or NULL on error.
4409
 */
4410
BGD_DECLARE(void *) gdImageBmpPtr(gdImagePtr im, int *size, int compression);
4411
4412
/**
4413
 * @brief Write an image as BMP data to a stdio file.
4414
 *
4415
 * gdImageBmp() uses automatic BMP bit-depth selection. A zero compression
4416
 * value writes uncompressed BMP data; a nonzero value requests legacy RLE
4417
 * output when the automatically selected BMP bit depth supports it. The image
4418
 * and outFile are borrowed for the duration of the call, and outFile is not
4419
 * closed by gd.
4420
 *
4421
 * @param im The image to write.
4422
 * @param outFile Pointer to the output FILE stream.
4423
 * @param compression Legacy compression selector; zero disables RLE, nonzero
4424
 *        requests RLE when supported by the selected output bit depth.
4425
 */
4426
BGD_DECLARE(void) gdImageBmp(gdImagePtr im, FILE *outFile, int compression);
4427
4428
/**
4429
 * @brief Write an image as BMP data to a gdIOCtx.
4430
 *
4431
 * gdImageBmpCtx() uses automatic BMP bit-depth selection. A zero compression
4432
 * value writes uncompressed BMP data; a nonzero value requests legacy RLE
4433
 * output when the automatically selected BMP bit depth supports it. The image
4434
 * and out context are borrowed for the duration of the call, and out is not
4435
 * closed by gd.
4436
 *
4437
 * @param im The image to write.
4438
 * @param out Pointer to the gdIOCtx output context.
4439
 * @param compression Legacy compression selector; zero disables RLE, nonzero
4440
 *        requests RLE when supported by the selected output bit depth.
4441
 */
4442
BGD_DECLARE(void) gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression);
4443
4444
/** @brief Write uncompressed BMP pixel data. */
4445
#define GD_BMP_COMPRESS_NONE 0
4446
/** @brief Write BI_RLE8 compressed pixel data; valid only for 8 bpp output. */
4447
#define GD_BMP_COMPRESS_RLE8 1
4448
/** @brief Write BI_RLE4 compressed pixel data; valid only for 4 bpp output. */
4449
#define GD_BMP_COMPRESS_RLE4 2
4450
4451
/** @brief Use default BMP writer behavior. */
4452
#define GD_BMP_FLAG_NONE 0
4453
/** @brief Force output to use a BITMAPV4HEADER. */
4454
#define GD_BMP_FLAG_FORCE_V4HDR (1 << 0)
4455
/** @brief Allow lossy truecolor-to-indexed conversion for 1, 4, or 8 bpp output. */
4456
#define GD_BMP_FLAG_QUANTIZE (1 << 1)
4457
/** @brief Use RGB555 bit masks instead of RGB565 for 16 bpp output. */
4458
#define GD_BMP_FLAG_RGB555 (1 << 2)
4459
4460
/**
4461
 * @brief Structured BMP writer options.
4462
 */
4463
typedef struct {
4464
  int bits_per_pixel; /**< Requested output bit depth, or 0 for automatic selection. */
4465
  int compression; /**< One of GD_BMP_COMPRESS_* values. */
4466
  int flags; /**< Bitwise OR of GD_BMP_FLAG_* values. */
4467
  const gdImageMetadata *metadata; /**< Reserved and ignored for BMP. */
4468
} gdBmpWriteOptions;
4469
4470
/**
4471
 * @brief Initialize a gdBmpWriteOptions structure to default values.
4472
 *
4473
 * Updates or changes to the default values are not guaranteed to be compatible with future versions of gd. Callers should not assume that the default values will remain the same across versions.
4474
 * This is not considered part of the API contract and may change without notice. Callers should always explicitly set the fields they care about after calling this function.
4475
 */
4476
BGD_DECLARE(void) gdBmpWriteOptionsInit(gdBmpWriteOptions *options);
4477
4478
/**
4479
 * @brief Write an image as BMP data to a stdio file with explicit options.
4480
 *
4481
 * @param im The image to write.
4482
 * @param outFile Pointer to the output FILE stream.
4483
 * @param options Pointer to a gdBmpWriteOptions structure specifying output options.
4484
 *
4485
 * @return 0 on success, or a nonzero error code on failure.
4486
 *
4487
 * @see gdBmpWriteOptionsInit gdBmpWriteOptions
4488
 */
4489
BGD_DECLARE(int) gdImageBmpWithOptions(gdImagePtr im, FILE *outFile, const gdBmpWriteOptions *options);
4490
4491
/**
4492
 * @brief Write an image as BMP data to a gdIOCtx with explicit options.
4493
 *
4494
 * @param im The image to write.
4495
 * @param out Pointer to the gdIOCtx output context.
4496
 * @param options Pointer to a gdBmpWriteOptions structure specifying output options.
4497
 *
4498
 * @return 0 on success, or a nonzero error code on failure.
4499
 * @see gdBmpWriteOptionsInit gdBmpWriteOptions
4500
 */
4501
BGD_DECLARE(int) gdImageBmpCtxWithOptions(gdImagePtr im, gdIOCtxPtr out, const gdBmpWriteOptions *options);
4502
4503
/**
4504
 * @brief Write an image as BMP data to a newly allocated memory buffer with explicit options.
4505
 *
4506
 * @param im The image to write.
4507
 * @param size Output location for the returned buffer size in bytes.
4508
 * @param options Pointer to a gdBmpWriteOptions structure specifying output options.
4509
 *
4510
 * @return A newly allocated BMP buffer, or NULL on error. The caller is responsible for freeing the buffer with gdFree().
4511
 *
4512
 * @see gdBmpWriteOptionsInit gdBmpWriteOptions
4513
 */
4514
BGD_DECLARE(void *) gdImageBmpPtrWithOptions(gdImagePtr im, int *size, const gdBmpWriteOptions *options);
4515
4516
/**
4517
 * @brief Write an image as BMP data to a newly allocated memory buffer.
4518
 *
4519
 * gdImageBmpPtrEx() writes BMP output with explicit control over output bit
4520
 * depth, compression, and writer flags. Pass bpp as 0 for automatic selection,
4521
 * or as one of 1, 4, 8, 16, 24, or 32. Explicit indexed output from a
4522
 * truecolor image is lossy and fails unless GD_BMP_FLAG_QUANTIZE is set. The
4523
 * image is borrowed for the duration of the call. On success, the returned
4524
 * buffer must be freed with gdFree().
4525
 *
4526
 * @param im The image to write.
4527
 * @param size Output location for the returned buffer size in bytes.
4528
 * @param bpp Requested output bit depth, or 0 for automatic selection.
4529
 * @param compression One of GD_BMP_COMPRESS_NONE, GD_BMP_COMPRESS_RLE8, or
4530
 *        GD_BMP_COMPRESS_RLE4.
4531
 * @param flags Bitwise OR of GD_BMP_FLAG_* values.
4532
 * @return A newly allocated BMP buffer, or NULL on error.
4533
 */
4534
BGD_DECLARE(void *)
4535
gdImageBmpPtrEx(gdImagePtr im, int *size, int bpp, int compression, int flags);
4536
4537
/**
4538
 * @brief Write an image as BMP data to a stdio file.
4539
 *
4540
 * gdImageBmpEx() writes BMP output with explicit control over output bit
4541
 * depth, compression, and writer flags. Pass bpp as 0 for automatic selection,
4542
 * or as one of 1, 4, 8, 16, 24, or 32. RLE4 is valid only for 4 bpp output and
4543
 * RLE8 is valid only for 8 bpp output. The image and outFile are borrowed for
4544
 * the duration of the call, and outFile is not closed by gd.
4545
 *
4546
 * @param im The image to write.
4547
 * @param outFile Pointer to the output FILE stream.
4548
 * @param bpp Requested output bit depth, or 0 for automatic selection.
4549
 * @param compression One of GD_BMP_COMPRESS_NONE, GD_BMP_COMPRESS_RLE8, or
4550
 *        GD_BMP_COMPRESS_RLE4.
4551
 * @param flags Bitwise OR of GD_BMP_FLAG_* values.
4552
 */
4553
BGD_DECLARE(void)
4554
gdImageBmpEx(gdImagePtr im, FILE *outFile, int bpp, int compression, int flags);
4555
4556
/**
4557
 * @brief Write an image as BMP data to a gdIOCtx.
4558
 *
4559
 * gdImageBmpCtxEx() writes BMP output with explicit control over output bit
4560
 * depth, compression, and writer flags. Pass bpp as 0 for automatic selection,
4561
 * or as one of 1, 4, 8, 16, 24, or 32. For 16 bpp output, RGB565 masks are
4562
 * used by default and GD_BMP_FLAG_RGB555 selects RGB555 masks. The image and
4563
 * out context are borrowed for the duration of the call, and out is not closed
4564
 * by gd.
4565
 *
4566
 * @param im The image to write.
4567
 * @param out Pointer to the gdIOCtx output context.
4568
 * @param bpp Requested output bit depth, or 0 for automatic selection.
4569
 * @param compression One of GD_BMP_COMPRESS_NONE, GD_BMP_COMPRESS_RLE8, or
4570
 *        GD_BMP_COMPRESS_RLE4.
4571
 * @param flags Bitwise OR of GD_BMP_FLAG_* values.
4572
 */
4573
BGD_DECLARE(void)
4574
gdImageBmpCtxEx(gdImagePtr im, gdIOCtxPtr out, int bpp, int compression, int flags);
4575
4576
/** @} */
4577
4578
/**
4579
 * @defgroup gdCodecUhdr UltraHDR
4580
 * @brief Read, transform, and write UltraHDR JPEG images.
4581
 * @ingroup gdCodecs
4582
 *
4583
 * UltraHDR stores an SDR base image plus a gain map that allows HDR
4584
 * reconstruction. gd's normal gdImage representation cannot safely represent
4585
 * that gain map because it is not an ordinary 8-bit or palette bitmap; it may
4586
 * be a floating-point-style image with metadata that must stay aligned with the
4587
 * SDR base image. For that reason, UltraHDR support uses a separate opaque
4588
 * gdUhdrImage handle and performs UltraHDR-aware operations through libultrahdr.
4589
 *
4590
 * Do not convert a gdUhdrImagePtr to gdImagePtr and expect to write it back as
4591
 * UltraHDR. gdUhdrImageGetSdr() intentionally returns only a standard SDR
4592
 * gdImagePtr view. That image can be inspected or saved as ordinary JPEG/PNG,
4593
 * but it no longer carries the gain map needed to recreate a valid UltraHDR
4594
 * image. Future GD versions may add internal image formats that can represent
4595
 * the gain map directly; until then, use gdUhdrImageResize(),
4596
 * gdUhdrImageCrop(), gdUhdrImageRotate(), and gdUhdrImageMirror() to queue
4597
 * supported UltraHDR-preserving transformations.
4598
 *
4599
 * @code{.c}
4600
 * gdUhdrImagePtr im;
4601
 * gdImagePtr sdr;
4602
 * gdUhdrError err;
4603
 * int rc;
4604
 *
4605
 * im = gdUhdrImageCreateFromFile("input.jpg", GD_UHDR_FORMAT_JPEG, &err);
4606
 * if (im == NULL) {
4607
 *     return 1;
4608
 * }
4609
 *
4610
 * rc = gdUhdrImageResize(im, 640, 360, &err);
4611
 * if (rc != GD_UHDR_SUCCESS) {
4612
 *     gdUhdrImageDestroy(im);
4613
 *     return 1;
4614
 * }
4615
 *
4616
 * rc = gdUhdrImageFile(im, "output.jpg", GD_UHDR_FORMAT_JPEG, 90, &err);
4617
 * if (rc != GD_UHDR_SUCCESS) {
4618
 *     gdUhdrImageDestroy(im);
4619
 *     return 1;
4620
 * }
4621
 *
4622
 * sdr = gdUhdrImageGetSdr(im, &err);
4623
 * if (sdr != NULL) {
4624
 *     gdImageDestroy(sdr);
4625
 * }
4626
 *
4627
 * gdUhdrImageDestroy(im);
4628
 * @endcode
4629
 *
4630
 * @{
4631
 */
4632
4633
/** @name UltraHDR Status Codes */
4634
/** @{ */
4635
4636
#define GD_UHDR_SUCCESS 0        /**< Operation succeeded. */
4637
#define GD_UHDR_NOT_AVAILABLE -1 /**< libgd was built without UltraHDR support. */
4638
#define GD_UHDR_E_INVALID -2     /**< Invalid argument or state. */
4639
#define GD_UHDR_E_UNSUPPORTED -3 /**< Unsupported format or operation. */
4640
#define GD_UHDR_E_ENCODE -4      /**< Encode failure. */
4641
#define GD_UHDR_E_DECODE -5      /**< Decode failure. */
4642
/** @} */
4643
4644
/** @name UltraHDR Transform Constants */
4645
/** @{ */
4646
4647
/** Mirror an UltraHDR image horizontally. */
4648
#define GD_UHDR_MIRROR_HORIZONTAL 0
4649
/** Mirror an UltraHDR image vertically. */
4650
#define GD_UHDR_MIRROR_VERTICAL 1
4651
4652
/** @} */
4653
4654
/** @name UltraHDR Types */
4655
/** @{ */
4656
4657
/** @brief UltraHDR container format selector. */
4658
typedef enum {
4659
    GD_UHDR_FORMAT_JPEG = 0, /**< UltraHDR JPEG, currently supported. */
4660
    GD_UHDR_FORMAT_WEBP = 1, /**< Reserved for future WebP-based UltraHDR support. */
4661
    GD_UHDR_FORMAT_HEIF = 2  /**< Reserved for future HEIF-based UltraHDR support. */
4662
} gdUhdrFormat;
4663
4664
/** @brief Opaque UltraHDR image handle. */
4665
typedef struct gdUhdrImageStruct gdUhdrImage;
4666
4667
/** @brief Pointer to an opaque UltraHDR image handle. */
4668
typedef gdUhdrImage *gdUhdrImagePtr;
4669
4670
/** @brief Structured error details for UltraHDR APIs. */
4671
typedef struct {
4672
    int code;           /**< libgd UltraHDR status code, one of the GD_UHDR_* values. */
4673
    int provider_code;  /**< Underlying libultrahdr provider error code, if any. */
4674
    char message[128];  /**< Optional human-readable error detail. */
4675
} gdUhdrError;
4676
4677
/** @brief Pointer to a gdUhdrError structure. */
4678
typedef gdUhdrError *gdUhdrErrorPtr;
4679
4680
/** @} */
4681
4682
/** @name UltraHDR Reading */
4683
/** @{ */
4684
4685
/**
4686
 * @brief Create an UltraHDR image handle from a file path.
4687
 *
4688
 * gdUhdrImageCreateFromFile() reads filename and validates that it is an
4689
 * UltraHDR image in the selected format. Currently only GD_UHDR_FORMAT_JPEG is
4690
 * supported. The returned handle is owned by the caller and must be destroyed
4691
 * with gdUhdrImageDestroy(). If err is not NULL, it receives status details.
4692
 *
4693
 * @param filename Path to the UltraHDR input file.
4694
 * @param format Input format, currently GD_UHDR_FORMAT_JPEG.
4695
 * @param err Optional pointer to receive detailed error information.
4696
 * @return A new UltraHDR image handle, or NULL on error.
4697
 */
4698
BGD_DECLARE(gdUhdrImagePtr)
4699
gdUhdrImageCreateFromFile(const char *filename, int format, gdUhdrErrorPtr err);
4700
4701
/**
4702
 * @brief Create an UltraHDR image handle from an IO context.
4703
 *
4704
 * gdUhdrImageCreateFromCtx() reads all data from ctx but does not close it.
4705
 * Currently only GD_UHDR_FORMAT_JPEG is supported. The returned handle is owned
4706
 * by the caller and must be destroyed with gdUhdrImageDestroy(). If err is not
4707
 * NULL, it receives status details.
4708
 *
4709
 * @param ctx Input IO context.
4710
 * @param format Input format, currently GD_UHDR_FORMAT_JPEG.
4711
 * @param err Optional pointer to receive detailed error information.
4712
 * @return A new UltraHDR image handle, or NULL on error.
4713
 */
4714
BGD_DECLARE(gdUhdrImagePtr)
4715
gdUhdrImageCreateFromCtx(gdIOCtxPtr ctx, int format, gdUhdrErrorPtr err);
4716
4717
/**
4718
 * @brief Create an UltraHDR image handle from memory.
4719
 *
4720
 * gdUhdrImageCreateFromPtr() reads size bytes from data without taking
4721
 * ownership of the buffer. Currently only GD_UHDR_FORMAT_JPEG is supported. The
4722
 * returned handle is owned by the caller and must be destroyed with
4723
 * gdUhdrImageDestroy(). If err is not NULL, it receives status details.
4724
 *
4725
 * @param size Size of data in bytes.
4726
 * @param data Pointer to UltraHDR data.
4727
 * @param format Input format, currently GD_UHDR_FORMAT_JPEG.
4728
 * @param err Optional pointer to receive detailed error information.
4729
 * @return A new UltraHDR image handle, or NULL on error.
4730
 */
4731
BGD_DECLARE(gdUhdrImagePtr)
4732
gdUhdrImageCreateFromPtr(int size, void *data, int format, gdUhdrErrorPtr err);
4733
4734
/**
4735
 * @brief Destroy an UltraHDR image handle.
4736
 *
4737
 * Use this for handles returned by gdUhdrImageCreateFromFile(),
4738
 * gdUhdrImageCreateFromCtx(), or gdUhdrImageCreateFromPtr(). Passing NULL is
4739
 * allowed.
4740
 *
4741
 * @param im UltraHDR image handle to destroy.
4742
 */
4743
BGD_DECLARE(void) gdUhdrImageDestroy(gdUhdrImagePtr im);
4744
4745
/** @} */
4746
4747
/** @} */
4748
4749
/*
4750
  Group: Types
4751
4752
  typedef: gdSource
4753
4754
  typedef: gdSourcePtr
4755
4756
        *Note:* This interface is *obsolete* and kept only for
4757
        *compatibility.  Use <gdIOCtx> instead.
4758
4759
        Represents a source from which a PNG can be read. Programmers who
4760
        do not wish to read PNGs from a file can provide their own
4761
        alternate input mechanism, using the @ref gdImageCreateFromPngSource
4762
        function. See the documentation of that function for an example of
4763
        the proper use of this type.
4764
4765
        > typedef struct {
4766
        >         int (*source) (void *context, char *buffer, int len);
4767
        >         void *context;
4768
        > } gdSource, *gdSourcePtr;
4769
4770
        The source function must return -1 on error, otherwise the number
4771
        of bytes fetched. 0 is EOF, not an error!
4772
4773
   'context' will be passed to your source function.
4774
4775
*/
4776
/** @deprecated in favor of gdIOCtx */
4777
typedef struct {
4778
    int (*source)(void *context, char *buffer, int len);
4779
    void *context;
4780
} gdSource, *gdSourcePtr;
4781
4782
/** @deprecated in favor of gdImageCreateFromPngCtx */
4783
BGD_DECLARE(gdImagePtr) gdImageCreateFromPngSource(gdSourcePtr in);
4784
4785
/** @deprecated for completeness with Sink 2.x APIs, will be removed in 3.0 with all Sink APIs */
4786
BGD_DECLARE(gdImagePtr) gdImageCreateFromQoiSource(gdSourcePtr in);
4787
4788
/**
4789
 * @defgroup gdCodecGd GD
4790
 * @brief Read and write libgd's native .gd image format.
4791
 * @ingroup gdCodecs
4792
 *
4793
 * The GD format is libgd's own historical image dump format. It is obsolete
4794
 * for interchange and should generally be used only for development, testing,
4795
 * or compatibility with existing .gd assets. For compressed or portable image
4796
 * exchange, prefer formats such as PNG, JPEG, WebP, or AVIF.
4797
 *
4798
 * gd can read GD 1.x palette .gd files and GD 2.x palette or truecolor .gd
4799
 * files. The writer always emits the GD 2.x .gd format, not the related GD2
4800
 * chunked format documented separately by the GD2 APIs. Returned gdImagePtr
4801
 * images are owned by the caller and must be destroyed with @ref gdImageDestroy.
4802
 *
4803
 * @code{.c}
4804
 * gdImagePtr im, roundtrip;
4805
 * void *data;
4806
 * int size;
4807
 *
4808
 * im = gdImageCreate(100, 100);
4809
 * gdImageColorAllocate(im, 255, 255, 255);
4810
 * gdImageColorAllocate(im, 0, 0, 0);
4811
 * gdImageLine(im, 0, 0, 99, 99, 1);
4812
 *
4813
 * data = gdImageGdPtr(im, &size);
4814
 * if (data != NULL) {
4815
 *        roundtrip = gdImageCreateFromGdPtr(size, data);
4816
 *        gdFree(data);
4817
 *        if (roundtrip != NULL) {
4818
 *                gdImageDestroy(roundtrip);
4819
 *        }
4820
 * }
4821
 *
4822
 * gdImageDestroy(im);
4823
 * @endcode
4824
 *
4825
 * @{
4826
 */
4827
4828
/** @name GD Reading */
4829
/** @{ */
4830
4831
/**
4832
 * @brief Create an image from a GD stdio file.
4833
 *
4834
 * gdImageCreateFromGd() reads from the current position of in and does not
4835
 * close it. On success, the returned image is owned by the caller and must be
4836
 * destroyed with @ref gdImageDestroy.
4837
 *
4838
 * @param in Pointer to the GD FILE stream to read.
4839
 * @return A newly allocated image, or NULL on error.
4840
 */
4841
BGD_DECLARE(gdImagePtr) gdImageCreateFromGd(FILE *in);
4842
4843
/**
4844
 * @brief Create an image from GD data read through a gdIOCtx.
4845
 *
4846
 * gdImageCreateFromGdCtx() reads from in and does not close it. On success,
4847
 * the returned image is owned by the caller and must be destroyed with
4848
 * @ref gdImageDestroy.
4849
 *
4850
 * @param in Pointer to the gdIOCtx input context.
4851
 * @return A newly allocated image, or NULL on error.
4852
 */
4853
BGD_DECLARE(gdImagePtr) gdImageCreateFromGdCtx(gdIOCtxPtr in);
4854
4855
/**
4856
 * @brief Create an image from a GD memory buffer.
4857
 *
4858
 * gdImageCreateFromGdPtr() borrows data for the duration of the call. The
4859
 * caller retains ownership of the input buffer. On success, the returned image
4860
 * is owned by the caller and must be destroyed with @ref gdImageDestroy.
4861
 *
4862
 * @param size Size of the GD memory buffer in bytes.
4863
 * @param data Pointer to the GD memory buffer.
4864
 * @return A newly allocated image, or NULL on error.
4865
 */
4866
BGD_DECLARE(gdImagePtr) gdImageCreateFromGdPtr(int size, void *data);
4867
4868
/** @} */
4869
4870
/** @name GD Writing */
4871
/** @{ */
4872
4873
/**
4874
 * @brief Write an image as GD data to a newly allocated memory buffer.
4875
 *
4876
 * gdImageGdPtr() writes the image in GD 2.x .gd format. The image is borrowed
4877
 * for the duration of the call. On success, the returned buffer is owned by the
4878
 * caller and must be freed with gdFree().
4879
 *
4880
 * @param im The image to write.
4881
 * @param size Output location for the returned buffer size in bytes.
4882
 * @return A newly allocated GD buffer, or NULL on error.
4883
 */
4884
BGD_DECLARE(void *) gdImageGdPtr(gdImagePtr im, int *size);
4885
4886
/**
4887
 * @brief Write an image as GD data to a stdio file.
4888
 *
4889
 * gdImageGd() writes the image in GD 2.x .gd format. The image and out stream
4890
 * are borrowed for the duration of the call, and out is not closed by gd.
4891
 *
4892
 * @param im The image to write.
4893
 * @param out Pointer to the output FILE stream.
4894
 */
4895
BGD_DECLARE(void) gdImageGd(gdImagePtr im, FILE *out);
4896
4897
/** @} */
4898
4899
/** @} */
4900
4901
/**
4902
 * @defgroup gdCodecGd2 GD2
4903
 * @brief Read and write libgd's chunked native .gd2 image format.
4904
 * @ingroup gdCodecs
4905
 * @deprecated GD and GD2 formats are in favor of more suitable and future proof like QOI or WebP for lossless usage or similar.
4906
 *
4907
 * GD2 is libgd's historical chunked image dump format. It is obsolete for
4908
 * general interchange and should generally be used only for development,
4909
 * testing, or compatibility with existing .gd2 assets. Unlike the simpler
4910
 * @ref gdCodecGd format, GD2 stores image data in chunks and can read a
4911
 * rectangular region without decoding the entire image. Compressed GD2 support
4912
 * requires libz; when GD2 support is not available the functions fail and
4913
 * report an error through gd's error mechanism.
4914
 *
4915
 * GD2 readers accept palette and truecolor GD2 files. The whole-image readers
4916
 * return the full image, while the part readers return a newly allocated image
4917
 * containing the requested rectangle. Returned gdImagePtr images are owned by
4918
 * the caller and must be destroyed with @ref gdImageDestroy. The writer emits GD2
4919
 * data using a public format selector of GD2_FMT_RAW or GD2_FMT_COMPRESSED;
4920
 * truecolor images are written with the corresponding internal truecolor GD2
4921
 * format automatically.
4922
 *
4923
 * @code{.c}
4924
 * gdImagePtr im, roundtrip;
4925
 * void *data;
4926
 * int size;
4927
 *
4928
 * im = gdImageCreate(100, 100);
4929
 * gdImageColorAllocate(im, 255, 255, 255);
4930
 * gdImageColorAllocate(im, 0, 0, 0);
4931
 * gdImageLine(im, 0, 0, 99, 99, 1);
4932
 *
4933
 * data = gdImageGd2Ptr(im, GD2_CHUNKSIZE, GD2_FMT_COMPRESSED, &size);
4934
 * if (data != NULL) {
4935
 *        roundtrip = gdImageCreateFromGd2PartPtr(size, data, 0, 0, 50, 50);
4936
 *        gdFree(data);
4937
 *        if (roundtrip != NULL) {
4938
 *                gdImageDestroy(roundtrip);
4939
 *        }
4940
 * }
4941
 *
4942
 * gdImageDestroy(im);
4943
 * @endcode
4944
 *
4945
 * @{
4946
 */
4947
4948
/** @name GD2 Reading */
4949
/** @{ */
4950
4951
/**
4952
 * @brief Create an image from a GD2 stdio file.
4953
 *
4954
 * gdImageCreateFromGd2() reads from the current position of in and does not
4955
 * close it. On success, the returned image is owned by the caller and must be
4956
 * destroyed with @ref gdImageDestroy.
4957
 *
4958
 * @param in Pointer to the GD2 FILE stream to read.
4959
 * @return A newly allocated image, or NULL on error.
4960
 */
4961
BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2(FILE *in);
4962
4963
/**
4964
 * @brief Create an image from GD2 data read through a gdIOCtx.
4965
 *
4966
 * gdImageCreateFromGd2Ctx() reads from in and does not close it. On success,
4967
 * the returned image is owned by the caller and must be destroyed with
4968
 * @ref gdImageDestroy.
4969
 *
4970
 * @param in Pointer to the gdIOCtx input context.
4971
 * @return A newly allocated image, or NULL on error.
4972
 */
4973
BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Ctx(gdIOCtxPtr in);
4974
4975
/**
4976
 * @brief Create an image from a GD2 memory buffer.
4977
 *
4978
 * gdImageCreateFromGd2Ptr() borrows data for the duration of the call. The
4979
 * caller retains ownership of the input buffer. On success, the returned image
4980
 * is owned by the caller and must be destroyed with @ref gdImageDestroy.
4981
 *
4982
 * @param size Size of the GD2 memory buffer in bytes.
4983
 * @param data Pointer to the GD2 memory buffer.
4984
 * @return A newly allocated image, or NULL on error.
4985
 */
4986
BGD_DECLARE(gdImagePtr) gdImageCreateFromGd2Ptr(int size, void *data);
4987
4988
/**
4989
 * @brief Create an image from a rectangular region of a GD2 stdio file.
4990
 *
4991
 * gdImageCreateFromGd2Part() reads the region beginning at srcx, srcy with
4992
 * dimensions w by h. The input stream is borrowed and is not closed by gd. On
4993
 * success, the returned image is owned by the caller and must be destroyed with
4994
 * @ref gdImageDestroy.
4995
 *
4996
 * @param in Pointer to the GD2 FILE stream to read.
4997
 * @param srcx Left coordinate of the source rectangle.
4998
 * @param srcy Top coordinate of the source rectangle.
4999
 * @param w Width of the source rectangle in pixels.
5000
 * @param h Height of the source rectangle in pixels.
5001
 * @return A newly allocated image containing the requested region, or NULL on error.
5002
 */
5003
BGD_DECLARE(gdImagePtr)
5004
gdImageCreateFromGd2Part(FILE *in, int srcx, int srcy, int w, int h);
5005
5006
/**
5007
 * @brief Create an image from a rectangular GD2 region read through a gdIOCtx.
5008
 *
5009
 * gdImageCreateFromGd2PartCtx() reads the region beginning at srcx, srcy with
5010
 * dimensions w by h. The input context is borrowed and is not closed by gd. On
5011
 * success, the returned image is owned by the caller and must be destroyed with
5012
 * @ref gdImageDestroy.
5013
 *
5014
 * @param in Pointer to the gdIOCtx input context.
5015
 * @param srcx Left coordinate of the source rectangle.
5016
 * @param srcy Top coordinate of the source rectangle.
5017
 * @param w Width of the source rectangle in pixels.
5018
 * @param h Height of the source rectangle in pixels.
5019
 * @return A newly allocated image containing the requested region, or NULL on error.
5020
 */
5021
BGD_DECLARE(gdImagePtr)
5022
gdImageCreateFromGd2PartCtx(gdIOCtxPtr in, int srcx, int srcy, int w, int h);
5023
5024
/**
5025
 * @brief Create an image from a rectangular region of a GD2 memory buffer.
5026
 *
5027
 * gdImageCreateFromGd2PartPtr() borrows data for the duration of the call. The
5028
 * caller retains ownership of the input buffer. On success, the returned image
5029
 * is owned by the caller and must be destroyed with @ref gdImageDestroy.
5030
 *
5031
 * @param size Size of the GD2 memory buffer in bytes.
5032
 * @param data Pointer to the GD2 memory buffer.
5033
 * @param srcx Left coordinate of the source rectangle.
5034
 * @param srcy Top coordinate of the source rectangle.
5035
 * @param w Width of the source rectangle in pixels.
5036
 * @param h Height of the source rectangle in pixels.
5037
 * @return A newly allocated image containing the requested region, or NULL on error.
5038
 */
5039
BGD_DECLARE(gdImagePtr)
5040
gdImageCreateFromGd2PartPtr(int size, void *data, int srcx, int srcy, int w, int h);
5041
5042
/** @} */
5043
5044
/** @} */
5045
5046
/**
5047
 * @defgroup gdCodecXbm XBM
5048
 * @brief Read and write X11 bitmap images.
5049
 * @ingroup gdCodecs
5050
 *
5051
 * XBM support reads X11 bitmap data from an open stdio stream and writes XBM
5052
 * text to a gd IO context. XBM images are 1-bit images stored as C source-style
5053
 * data, and gd maps them to palette images when reading. The reader returns a
5054
 * new image owned by the caller; the writer borrows both the image and output
5055
 * context for the duration of the call.
5056
 *
5057
 * @code{.c}
5058
 * FILE *in;
5059
 * gdImagePtr im;
5060
 * gdIOCtxPtr out;
5061
 *
5062
 * in = fopen("icon.xbm", "rb");
5063
 * if (in == NULL) {
5064
 *     return 1;
5065
 * }
5066
 *
5067
 * im = gdImageCreateFromXbm(in);
5068
 * fclose(in);
5069
 * if (im == NULL) {
5070
 *     return 1;
5071
 * }
5072
 *
5073
 * out = gdNewFileCtx(stdout);
5074
 * if (out == NULL) {
5075
 *     gdImageDestroy(im);
5076
 *     return 1;
5077
 * }
5078
 *
5079
 * gdImageXbmCtx(im, "icon.xbm", 1, out);
5080
 * out->gd_free(out);
5081
 * gdImageDestroy(im);
5082
 * @endcode
5083
 *
5084
 * @{
5085
 */
5086
5087
/** @name XBM Reading */
5088
/** @{ */
5089
5090
/**
5091
 * @brief Create an image from XBM data in a stdio stream.
5092
 *
5093
 * gdImageCreateFromXbm() rewinds in before reading and does not close it. X11
5094
 * XBM data with char arrays and X10 XBM data with short arrays are supported.
5095
 * On success, the returned image is owned by the caller and must be destroyed
5096
 * with @ref gdImageDestroy.
5097
 *
5098
 * @param in Pointer to the input FILE stream.
5099
 * @return A newly allocated image, or NULL on error.
5100
 */
5101
BGD_DECLARE(gdImagePtr) gdImageCreateFromXbm(FILE *in);
5102
5103
/** @} */
5104
5105
/** @name XBM Writing */
5106
/** @{ */
5107
5108
/**
5109
 * @brief Write an image to an IO context in X11 bitmap format.
5110
 *
5111
 * gdImageXbmCtx() does not close out. The image and output context are borrowed
5112
 * for the duration of the call. Pixels whose color index equals fg are written
5113
 * as set bits; all other pixels are written as unset bits.
5114
 *
5115
 * @param image The image to write.
5116
 * @param file_name Prefix for the generated XBM C identifiers. Path components,
5117
 *                  a trailing .xbm extension, and unsupported identifier
5118
 *                  characters are normalized before writing.
5119
 * @param fg Foreground color index to write as set bits.
5120
 * @param out The output IO context.
5121
 */
5122
BGD_DECLARE(void)
5123
gdImageXbmCtx(gdImagePtr image, char *file_name, int fg, gdIOCtxPtr out);
5124
5125
/** @} */
5126
5127
/** @} */
5128
5129
/**
5130
 * @defgroup gdCodecXpm XPM
5131
 * @brief Read and write X PixMap images.
5132
 * @ingroup gdCodecs
5133
 */
5134
 /**
5135
 * @brief Read X PixMap images.
5136
 * @ingroup gdCodecs
5137
 *
5138
 * XPM support reads X PixMap files through libXpm and returns palette images.
5139
 * Unlike most gd image readers, the XPM API takes a filename string rather than
5140
 * a FILE pointer, memory buffer, or gd IO context. The returned image is owned
5141
 * by the caller. gd does not provide an XPM writer.
5142
 *
5143
 * @code{.c}
5144
 * gdImagePtr im;
5145
 *
5146
 * im = gdImageCreateFromXpm("icon.xpm");
5147
 * if (im == NULL) {
5148
 *     return 1;
5149
 * }
5150
 *
5151
 * gdImageDestroy(im);
5152
 * @endcode
5153
*/
5154
5155
/** @name XPM Reading */
5156
/** @{ */
5157
5158
/**
5159
 * @brief Create a palette image from an XPM file.
5160
 *
5161
 * gdImageCreateFromXpm() reads filename directly through libXpm. The input is a
5162
 * filename, not an open FILE stream. On success, the returned image is owned by
5163
 * the caller and must be destroyed with @ref gdImageDestroy.
5164
 *
5165
 * @param filename Path to the XPM file to read.
5166
 * @return A newly allocated palette image, or NULL on error.
5167
 */
5168
BGD_DECLARE(gdImagePtr) gdImageCreateFromXpm(char *filename);
5169
5170
/** @} */
5171
5172
/** @} */
5173
5174
/**
5175
 * @brief Write an image as WBMP data to a stdio file.
5176
 * @ingroup gdCodecWbmp
5177
 *
5178
 * gdImageWBMP() does not close out. The image is borrowed for the duration of
5179
 * the call. Pixels whose color equals fg are written as black; all other
5180
 * pixels are written as white.
5181
 *
5182
 * @param image The image to write.
5183
 * @param fg Foreground color value to write as black.
5184
 * @param out Pointer to the output FILE stream.
5185
 */
5186
BGD_DECLARE(void) gdImageWBMP(gdImagePtr image, int fg, FILE *out);
5187
5188
/**
5189
 * @brief Write an image as WBMP data to a gdIOCtx.
5190
 * @ingroup gdCodecWbmp
5191
 *
5192
 * gdImageWBMPCtx() does not close out. The image is borrowed for the duration
5193
 * of the call. Pixels whose color equals fg are written as black; all other
5194
 * pixels are written as white.
5195
 *
5196
 * @param image The image to write.
5197
 * @param fg Foreground color value to write as black.
5198
 * @param out Pointer to the gdIOCtx output context.
5199
 */
5200
BGD_DECLARE(void) gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtxPtr out);
5201
5202
/**
5203
 * @addtogroup gdCodecUhdr
5204
 * @{
5205
 */
5206
5207
/** @name UltraHDR Availability and Inspection */
5208
/** @{ */
5209
5210
/**
5211
 * @brief Return whether UltraHDR support is available in this build.
5212
 * @ingroup gdCodecUhdr
5213
 *
5214
 * This reports whether libgd was built with libultrahdr support.
5215
 *
5216
 * @return 1 when UltraHDR support is available, or 0 otherwise.
5217
 */
5218
BGD_DECLARE(int) gdUhdrIsAvailable(void);
5219
5220
/**
5221
 * @brief Return the UltraHDR image width.
5222
 * @ingroup gdCodecUhdr
5223
 *
5224
 * @param im UltraHDR image handle.
5225
 * @return Image width in pixels, or 0 for NULL.
5226
 */
5227
BGD_DECLARE(int) gdUhdrImageWidth(gdUhdrImagePtr im);
5228
5229
/**
5230
 * @brief Return the UltraHDR image height.
5231
 * @ingroup gdCodecUhdr
5232
 *
5233
 * @param im UltraHDR image handle.
5234
 * @return Image height in pixels, or 0 for NULL.
5235
 */
5236
BGD_DECLARE(int) gdUhdrImageHeight(gdUhdrImagePtr im);
5237
5238
/**
5239
 * @brief Return whether the UltraHDR image has a gain map.
5240
 * @ingroup gdCodecUhdr
5241
 *
5242
 * @param im UltraHDR image handle.
5243
 * @return 1 if im has a gain map, or 0 otherwise.
5244
 */
5245
BGD_DECLARE(int) gdUhdrImageHasGainMap(gdUhdrImagePtr im);
5246
5247
/** @} */
5248
5249
/** @name UltraHDR Transform Queue */
5250
/** @{ */
5251
5252
/**
5253
 * @brief Queue an UltraHDR-preserving resize operation.
5254
 * @ingroup gdCodecUhdr
5255
 *
5256
 * The operation is recorded on im and applied when the image is written. The
5257
 * SDR base image and gain map are transformed together so the output can remain
5258
 * a valid UltraHDR image.
5259
 *
5260
 * @param im UltraHDR image handle.
5261
 * @param width Output width in pixels.
5262
 * @param height Output height in pixels.
5263
 * @param err Optional pointer to receive detailed error information.
5264
 * @return GD_UHDR_SUCCESS on success, or another GD_UHDR_* status code on error.
5265
 */
5266
BGD_DECLARE(int)
5267
gdUhdrImageResize(gdUhdrImagePtr im, int width, int height, gdUhdrErrorPtr err);
5268
5269
/**
5270
 * @brief Queue an UltraHDR-preserving crop operation.
5271
 * @ingroup gdCodecUhdr
5272
 *
5273
 * The operation is recorded on im and applied when the image is written. The
5274
 * SDR base image and gain map are cropped together so the output can remain a
5275
 * valid UltraHDR image.
5276
 *
5277
 * @param im UltraHDR image handle.
5278
 * @param left Left edge of the crop rectangle in pixels.
5279
 * @param top Top edge of the crop rectangle in pixels.
5280
 * @param width Crop width in pixels.
5281
 * @param height Crop height in pixels.
5282
 * @param err Optional pointer to receive detailed error information.
5283
 * @return GD_UHDR_SUCCESS on success, or another GD_UHDR_* status code on error.
5284
 */
5285
BGD_DECLARE(int)
5286
gdUhdrImageCrop(gdUhdrImagePtr im, int left, int top, int width, int height, gdUhdrErrorPtr err);
5287
5288
/**
5289
 * @brief Queue an UltraHDR-preserving right-angle rotation.
5290
 * @ingroup gdCodecUhdr
5291
 *
5292
 * The operation is recorded on im and applied when the image is written. The
5293
 * SDR base image and gain map are rotated together so the output can remain a
5294
 * valid UltraHDR image. Supported angles are 0, 90, 180, and 270 degrees.
5295
 *
5296
 * @param im UltraHDR image handle.
5297
 * @param degrees Rotation angle in degrees.
5298
 * @param err Optional pointer to receive detailed error information.
5299
 * @return GD_UHDR_SUCCESS on success, or another GD_UHDR_* status code on error.
5300
 */
5301
BGD_DECLARE(int)
5302
gdUhdrImageRotate(gdUhdrImagePtr im, int degrees, gdUhdrErrorPtr err);
5303
5304
/**
5305
 * @brief Queue an UltraHDR-preserving mirror operation.
5306
 * @ingroup gdCodecUhdr
5307
 *
5308
 * The operation is recorded on im and applied when the image is written. The
5309
 * SDR base image and gain map are mirrored together so the output can remain a
5310
 * valid UltraHDR image.
5311
 *
5312
 * @param im UltraHDR image handle.
5313
 * @param axis Mirror axis, GD_UHDR_MIRROR_HORIZONTAL or GD_UHDR_MIRROR_VERTICAL.
5314
 * @param err Optional pointer to receive detailed error information.
5315
 * @return GD_UHDR_SUCCESS on success, or another GD_UHDR_* status code on error.
5316
 */
5317
BGD_DECLARE(int)
5318
gdUhdrImageMirror(gdUhdrImagePtr im, int axis, gdUhdrErrorPtr err);
5319
5320
/** @} */
5321
5322
/** @name UltraHDR Writing */
5323
/** @{ */
5324
5325
/**
5326
 * @brief Write an UltraHDR image to a file path.
5327
 * @ingroup gdCodecUhdr
5328
 *
5329
 * Currently only GD_UHDR_FORMAT_JPEG is supported. If no transform operations
5330
 * were queued, gd writes the original compressed UltraHDR data through. If
5331
 * transforms were queued, gd uses libultrahdr to decode the base image and gain
5332
 * map, applies the queued operations to both, and re-encodes an UltraHDR JPEG.
5333
 *
5334
 * @param im UltraHDR image handle to write.
5335
 * @param filename Output file path.
5336
 * @param format Output format, currently GD_UHDR_FORMAT_JPEG.
5337
 * @param quality JPEG quality from 1 to 95.
5338
 * @param err Optional pointer to receive detailed error information.
5339
 * @return GD_UHDR_SUCCESS on success, or another GD_UHDR_* status code on error.
5340
 */
5341
BGD_DECLARE(int)
5342
gdUhdrImageFile(gdUhdrImagePtr im, const char *filename, int format, int quality,
5343
                gdUhdrErrorPtr err);
5344
5345
/**
5346
 * @brief Write an UltraHDR image to an IO context.
5347
 * @ingroup gdCodecUhdr
5348
 *
5349
 * gdUhdrImageCtx() does not close ctx. Currently only GD_UHDR_FORMAT_JPEG is
5350
 * supported. If no transform operations were queued, gd writes the original
5351
 * compressed UltraHDR data through. If transforms were queued, gd uses
5352
 * libultrahdr to produce a new UltraHDR JPEG with the gain map preserved.
5353
 *
5354
 * @param im UltraHDR image handle to write.
5355
 * @param ctx Output IO context.
5356
 * @param format Output format, currently GD_UHDR_FORMAT_JPEG.
5357
 * @param quality JPEG quality from 1 to 95.
5358
 * @param err Optional pointer to receive detailed error information.
5359
 * @return GD_UHDR_SUCCESS on success, or another GD_UHDR_* status code on error.
5360
 */
5361
BGD_DECLARE(int)
5362
gdUhdrImageCtx(gdUhdrImagePtr im, gdIOCtxPtr ctx, int format, int quality, gdUhdrErrorPtr err);
5363
5364
/**
5365
 * @brief Write an UltraHDR image to a newly allocated memory buffer.
5366
 * @ingroup gdCodecUhdr
5367
 *
5368
 * Currently only GD_UHDR_FORMAT_JPEG is supported. On success, the returned
5369
 * buffer is owned by the caller and must be freed with gdFree().
5370
 *
5371
 * @param im UltraHDR image handle to write.
5372
 * @param size Pointer that receives the encoded buffer size in bytes.
5373
 * @param format Output format, currently GD_UHDR_FORMAT_JPEG.
5374
 * @param quality JPEG quality from 1 to 95.
5375
 * @param err Optional pointer to receive detailed error information.
5376
 * @return A newly allocated UltraHDR data buffer, or NULL on error.
5377
 */
5378
BGD_DECLARE(void *)
5379
gdUhdrImageWritePtr(gdUhdrImagePtr im, int *size, int format, int quality, gdUhdrErrorPtr err);
5380
5381
/** @} */
5382
5383
/** @name UltraHDR SDR Extraction */
5384
/** @{ */
5385
5386
/**
5387
 * @brief Decode the SDR view of an UltraHDR image as a gdImage.
5388
 * @ingroup gdCodecUhdr
5389
 *
5390
 * The returned gdImagePtr is caller-owned and must be destroyed with
5391
 * @ref gdImageDestroy. It is an SDR image only: it does not contain the UltraHDR
5392
 * gain map and cannot be used to recreate an UltraHDR image. Use the
5393
 * gdUhdrImage* transform and write APIs when the gain map must be preserved.
5394
 *
5395
 * @param im UltraHDR image handle to decode.
5396
 * @param err Optional pointer to receive detailed error information.
5397
 * @return A newly allocated truecolor SDR image, or NULL on error.
5398
 */
5399
BGD_DECLARE(gdImagePtr)
5400
gdUhdrImageGetSdr(gdUhdrImagePtr im, gdUhdrErrorPtr err);
5401
5402
/** @} */
5403
5404
/** @} */
5405
5406
/**
5407
 * @defgroup gdImageFileIO Image File Convenience APIs
5408
 * @brief Convenience APIs for reading and writing images by filename or signature.
5409
 *
5410
 * These APIs are helpers around the format-specific codec functions. For
5411
 * reading existing files, @ref gdImageReadFile is preferred over
5412
 * @ref gdImageCreateFromFile because it detects image type from binary
5413
 * signatures rather than trusting the filename extension.
5414
 *
5415
 * @{
5416
 */
5417
5418
/**
5419
 * @brief Create an image from a file using the filename extension.
5420
 *
5421
 * gdImageCreateFromFile() chooses the reader from the filename extension using
5422
 * the same extension table as @ref gdSupportsFileType. The returned image is
5423
 * caller-owned and must be destroyed with gdImageDestroy().
5424
 *
5425
 * @note For new code, prefer @ref gdImageReadFile when reading existing files.
5426
 * gdImageReadFile() checks binary signatures instead of trusting the filename
5427
 * extension, which is safer and more reliable when files are mislabeled or
5428
 * supplied by users.
5429
 *
5430
 * @param filename Path to the input image file.
5431
 * @return A newly allocated image, or NULL on error or unsupported extension.
5432
 */
5433
BGD_DECLARE(gdImagePtr) gdImageCreateFromFile(const char *filename);
5434
5435
/**
5436
 * @brief Read an image file by probing its binary signature.
5437
 *
5438
 * gdImageReadFile() opens filename, reads the first bytes of the file, detects
5439
 * the image format by known binary signatures, and dispatches to the matching
5440
 * codec reader. It falls back to the filename-based XPM reader and FILE-based
5441
 * XBM reader for those formats because they do not have gdIOCtx readers.
5442
 *
5443
 * Supported detected formats depend on the codecs compiled into gd and include
5444
 * PNG, JPEG, GIF, BMP, TIFF, WebP, AVIF, HEIC, JXL, GD, GD2, QOI, XPM, and XBM.
5445
 * The returned image is caller-owned and must be destroyed with
5446
 * gdImageDestroy().
5447
 *
5448
 * @param filename Path to the input image file.
5449
 * @return A newly allocated image, or NULL on error, unknown format, disabled
5450
 *         codec, or decode failure.
5451
 */
5452
BGD_DECLARE(gdImagePtr) gdImageReadFile(const char *filename);
5453
5454
/**
5455
 * @brief Read an image from a gdIOCtx by probing its binary signature.
5456
 *
5457
 * gdImageReadCtx() borrows ctx for the duration of the call and does not close
5458
 * it. The input context must support seeking because gd reads a probe buffer
5459
 * and then seeks back to the start before dispatching to the codec reader.
5460
 * Formats without gdIOCtx readers, currently XPM and XBM, are not supported by
5461
 * this function; use @ref gdImageReadFile for those file formats.
5462
 *
5463
 * The returned image is caller-owned and must be destroyed with
5464
 * gdImageDestroy().
5465
 *
5466
 * @param ctx Pointer to the input gdIOCtx.
5467
 * @return A newly allocated image, or NULL on error, unknown format, disabled
5468
 *         codec, unsupported context reader, or decode failure.
5469
 */
5470
BGD_DECLARE(gdImagePtr) gdImageReadCtx(gdIOCtxPtr ctx);
5471
5472
/**
5473
 * @brief Status values for extended automatic image readers.
5474
 */
5475
typedef enum {
5476
  gdImageReadStatusOk = 0, /**< The image was read successfully. */
5477
  gdImageReadStatusUnrecognized, /**< No known binary signature matched the input. */
5478
  gdImageReadStatusUnsupportedFormat, /**< The signature matched a format that cannot be read through the requested API. */
5479
  gdImageReadStatusCodecUnavailable, /**< The matching codec is not available in this gd build. */
5480
  gdImageReadStatusDecodeFailed /**< The matching codec was available but failed to decode the image. */
5481
} gdImageReadStatus;
5482
5483
/** Restrict extended automatic reading to codec APIs that match the input source. */
5484
#define GD_IMAGE_READ_RESTRICT_CODEC_API 1
5485
5486
/**
5487
 * @brief Read an image from a gdIOCtx with extended status information.
5488
 *
5489
 * gdImageReadCtxEx() is the extended form of @ref gdImageReadCtx. It accepts
5490
 * option flags and can report a gdImageReadStatus value and detected format
5491
 * name to the caller. The input context is borrowed for the duration of the
5492
 * call and is not closed.
5493
 *
5494
 * @param ctx Pointer to the input gdIOCtx.
5495
 * @param flags Bitmask of GD_IMAGE_READ_* flags.
5496
 * @param status Optional pointer that receives the read status.
5497
 * @param format_name Optional pointer that receives the detected format name.
5498
 * @return A newly allocated image, or NULL on error.
5499
 */
5500
BGD_DECLARE(gdImagePtr) gdImageReadCtxEx(gdIOCtxPtr ctx, int flags, gdImageReadStatus *status, const char **format_name);
5501
5502
/**
5503
 * @brief Write an image to a file in the format indicated by the filename.
5504
 *
5505
 * File type is determined by the extension of the file name. See @ref gdSupportsFileType for an overview of the parsing.
5506
 * This is appropriate for writing, where the filename normally chooses the
5507
 * intended output format.
5508
 *
5509
 * For file types that require extra arguments, gdImageFile() attempts to use sane defaults:
5510
 * - @ref gdImageGd2 - chunk size = 0, compression is enabled.
5511
 * - @ref gdImageJpeg - quality = -1 (i.e. the reasonable default)
5512
 * - @ref gdImageWBMP - foreground is the darkest available color
5513
 *
5514
 * Everything else is called with the two-argument function and so will use the default values.
5515
 * @ref gdImageFile has some rudimentary error detection and will return GD_FALSE (0) if a detectable error occurred.
5516
 * However, the image loaders do not normally return their error status so a result of GD_TRUE (1) does **not** mean the file was saved successfully.
5517
 *
5518
 * @param im The image to save.
5519
 * @param filename The path to the file to which the image is saved.
5520
 * @return GD_TRUE on apparent success, or GD_FALSE if the filename extension
5521
 *         is unsupported or the output file could not be opened.
5522
 */
5523
BGD_DECLARE(int) gdImageFile(gdImagePtr im, const char *filename);
5524
5525
/**
5526
 * @brief Test if a given file type is supported by GD.
5527
 *
5528
 * gdSupportsFileType() tests the filename extension using the same extension
5529
 * table as @ref gdImageCreateFromFile and @ref gdImageFile. The file does not
5530
 * need to exist. If writing is nonzero, the function returns true only when
5531
 * gdImageFile() can write that extension; otherwise it returns true when
5532
 * gdImageCreateFromFile() can read that extension.
5533
 *
5534
 * Extension parsing has the same limitations as the extension-based helpers.
5535
 * Use @ref gdImageReadFile when reading an actual file and reliable format
5536
 * detection matters.
5537
 *
5538
 * @param filename Filename whose extension should be tested.
5539
 * @param writing Nonzero to test write support; zero to test read support.
5540
 * @return GD_TRUE (1) if the file type is supported, GD_FALSE (0) if not.
5541
 */
5542
BGD_DECLARE(int) gdSupportsFileType(const char *filename, int writing);
5543
5544
/** @} */
5545
5546
/* Guaranteed to correctly free memory returned by the gdImage*Ptr
5547
   functions */
5548
BGD_DECLARE(void) gdFree(void *m);
5549
5550
/**
5551
 * @brief Write an image as WBMP data to a newly allocated memory buffer.
5552
 * @ingroup gdCodecWbmp
5553
 *
5554
 * The image is borrowed for the duration of the call. Pixels whose color
5555
 * equals fg are written as black; all other pixels are written as white. The
5556
 * returned buffer is caller-owned and must be freed with gdFree().
5557
 *
5558
 * @param im The image to write.
5559
 * @param size Pointer to an integer that receives the returned buffer size.
5560
 * @param fg Foreground color value to write as black.
5561
 *
5562
 * @return Returns a pointer to the newly allocated WBMP buffer, or NULL on failure.
5563
 */
5564
BGD_DECLARE(void *) gdImageWBMPPtr(gdImagePtr im, int *size, int fg);
5565
5566
/**
5567
 * @addtogroup gdCodecJpeg
5568
 * @{
5569
 */
5570
5571
/**
5572
 * @brief Write an image as JPEG data to a stdio file.
5573
 *
5574
 * 100 is the highest quality (there is always a little loss with JPEG).
5575
 * 0 is the lowest quality. 10 is about the lowest useful setting.
5576
 * @param im The image to write.
5577
 * @param out The stdio file to write the JPEG data to.
5578
 * @param quality The JPEG quality (0-100).
5579
  */
5580
BGD_DECLARE(void) gdImageJpeg(gdImagePtr im, FILE *out, int quality);
5581
/**
5582
 * @brief Write an image as JPEG data to a gdIOCtx.
5583
 *
5584
 * @package im The image to write.
5585
 * @param out The gdIOCtx to write the JPEG data to.
5586
 * @param quality The JPEG quality (0-100).
5587
 */
5588
BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtxPtr out, int quality);
5589
5590
/**
5591
 * @brief Write an image as JPEG data to a gdIOCtx with metadata.
5592
 *
5593
 * @param im The image to write.
5594
 * @param out The gdIOCtx to write the JPEG data to.
5595
 * @param quality The JPEG quality (0-100).
5596
 * @param metadata Pointer to a gdImageMetadata structure containing the metadata to include in the JPEG file.
5597
 */
5598
/**
5599
 * @brief Write an image as JPEG data to a stdio file using write options.
5600
 *
5601
 * @param im The image to write.
5602
 * @param out The stdio file to write the JPEG data to.
5603
 * @param options Pointer to a gdJpegWriteOptions struct containing the desired write options.
5604
 *
5605
 * @return Returns 0 on success, or a negative value on error.
5606
 */
5607
BGD_DECLARE(int)
5608
gdImageJpegWithOptions(gdImagePtr im, FILE *out, const gdJpegWriteOptions *options);
5609
5610
/**
5611
 * @brief Write an image as JPEG data to a gdIOCtx using write options.
5612
 *
5613
 * @param im The image to write.
5614
 * @param out The gdIOCtx to write the JPEG data to.
5615
 * @param options Pointer to a gdJpegWriteOptions struct containing the desired write options.
5616
 *
5617
 * @return Returns 0 on success, or a negative value on error.
5618
 */
5619
BGD_DECLARE(int)
5620
gdImageJpegCtxWithOptions(gdImagePtr im, gdIOCtxPtr out, const gdJpegWriteOptions *options);
5621
5622
/**
5623
 * @brief Write an image as JPEG data to a newly allocated memory buffer.
5624
 *
5625
 * Result must be freed with gdFree(). The image is borrowed for the duration of the call.
5626
 *
5627
 * @param im The image to write.
5628
 * @param size Pointer to an integer that will receive the size of the returned buffer.
5629
 * @param quality The JPEG quality (0-100).
5630
 *
5631
 * @return A pointer to the newly allocated buffer containing the JPEG data, or NULL on failure.
5632
 */
5633
BGD_DECLARE(void *) gdImageJpegPtr(gdImagePtr im, int *size, int quality);
5634
/**
5635
 * @brief Write an image as JPEG data to a memory buffer with metadata.
5636
 *
5637
 * @param im The image to write.
5638
 * @param size Pointer to an integer that will receive the size of the returned buffer.
5639
 * @param quality The JPEG quality (0-100).
5640
 * @param metadata Pointer to a gdImageMetadata structure containing the metadata to include in the JPEG file.
5641
 *
5642
 * @return A pointer to the newly allocated buffer containing the JPEG data, or NULL on failure.
5643
 */
5644
5645
/**
5646
 * @brief Write an image as JPEG data to a memory buffer using write options.
5647
 *
5648
 * @param im The image to write.
5649
 * @param size Pointer to an integer that will receive the size of the returned buffer.
5650
 * @param options Pointer to a gdJpegWriteOptions struct containing the desired write options.
5651
 *
5652
 * @return A pointer to the newly allocated buffer containing the JPEG data, or NULL on failure.
5653
 */
5654
BGD_DECLARE(void *)
5655
gdImageJpegPtrWithOptions(gdImagePtr im, int *size, const gdJpegWriteOptions *options);
5656
/** @} */
5657
5658
/**
5659
 * @brief Lossless WebP quality threshold.
5660
 * @ingroup gdCodecWebp
5661
 *
5662
 * When the quantization value passed to gdImageWebpEx(), gdImageWebpCtx(), or
5663
 * gdImageWebpPtrEx() is greater than or equal to gdWebpLossless, the image is
5664
 * written in lossless WebP format.
5665
 */
5666
#define gdWebpLossless 101
5667
5668
/**
5669
 * @brief Write an image as WebP data to a stdio file with a quality setting.
5670
 * @ingroup gdCodecWebp
5671
 *
5672
 * gdImageWebpEx() does not close outFile. The image is borrowed for the
5673
 * duration of the call and must be a truecolor image.
5674
 *
5675
 * @param im The image to write.
5676
 * @param outFile Pointer to the output FILE stream.
5677
 * @param quantization WebP quality: -1 for default, 0-100 for lossy, or gdWebpLossless for lossless.
5678
 */
5679
BGD_DECLARE(void) gdImageWebpEx(gdImagePtr im, FILE *outFile, int quantization);
5680
5681
/**
5682
 * @brief Write an image as WebP data to a stdio file with default quality.
5683
 * @ingroup gdCodecWebp
5684
 *
5685
 * gdImageWebp() does not close outFile. The image is borrowed for the duration
5686
 * of the call and must be a truecolor image.
5687
 *
5688
 * @param im The image to write.
5689
 * @param outFile Pointer to the output FILE stream.
5690
 */
5691
BGD_DECLARE(void) gdImageWebp(gdImagePtr im, FILE *outFile);
5692
5693
/**
5694
 * @brief Write an image as WebP data to a newly allocated memory buffer.
5695
 * @ingroup gdCodecWebp
5696
 *
5697
 * The image is borrowed for the duration of the call and must be a truecolor
5698
 * image. The returned buffer is caller-owned and must be freed with gdFree().
5699
 *
5700
 * @param im The image to write.
5701
 * @param size Pointer to an integer that receives the returned buffer size.
5702
 *
5703
 * @return Returns a pointer to the newly allocated WebP buffer, or NULL on failure.
5704
 */
5705
BGD_DECLARE(void *) gdImageWebpPtr(gdImagePtr im, int *size);
5706
5707
/**
5708
 * @brief Write an image as WebP data to a newly allocated memory buffer with a quality setting.
5709
 * @ingroup gdCodecWebp
5710
 *
5711
 * The image is borrowed for the duration of the call and must be a truecolor
5712
 * image. The returned buffer is caller-owned and must be freed with gdFree().
5713
 *
5714
 * @param im The image to write.
5715
 * @param size Pointer to an integer that receives the returned buffer size.
5716
 * @param quantization WebP quality: -1 for default, 0-100 for lossy, or gdWebpLossless for lossless.
5717
 *
5718
 * @return Returns a pointer to the newly allocated WebP buffer, or NULL on failure.
5719
 */
5720
BGD_DECLARE(void *)
5721
gdImageWebpPtrEx(gdImagePtr im, int *size, int quantization);
5722
5723
/**
5724
 * @brief Write an image as WebP data to a stdio file using write options.
5725
 * @ingroup gdCodecWebp
5726
 *
5727
 * gdImageWebpWithOptions() does not close outFile. The image is borrowed for the
5728
 * duration of the call and must be a truecolor image.
5729
 *
5730
 * @param im The image to write.
5731
 * @param outFile Pointer to the output FILE stream.
5732
 * @param options Pointer to WebP write options, or NULL for defaults.
5733
 *
5734
 * @return Returns 0 on success, or 1 on failure.
5735
 */
5736
BGD_DECLARE(int)
5737
gdImageWebpWithOptions(gdImagePtr im, FILE *outFile, const gdWebpWriteOptions *options);
5738
5739
/**
5740
 * @brief Write an image as WebP data to a gdIOCtx using write options.
5741
 * @ingroup gdCodecWebp
5742
 *
5743
 * gdImageWebpCtxWithOptions() does not close outfile. The image is borrowed for
5744
 * the duration of the call and must be a truecolor image.
5745
 *
5746
 * @param im The image to write.
5747
 * @param outfile Pointer to the gdIOCtx output context.
5748
 * @param options Pointer to WebP write options, or NULL for defaults.
5749
 *
5750
 * @return Returns 0 on success, or 1 on failure.
5751
 */
5752
BGD_DECLARE(int)
5753
gdImageWebpCtxWithOptions(gdImagePtr im, gdIOCtxPtr outfile, const gdWebpWriteOptions *options);
5754
5755
/**
5756
 * @brief Write an image as WebP data to a newly allocated memory buffer using write options.
5757
 * @ingroup gdCodecWebp
5758
 *
5759
 * The image is borrowed for the duration of the call and must be a truecolor
5760
 * image. The returned buffer is caller-owned and must be freed with gdFree().
5761
 *
5762
 * @param im The image to write.
5763
 * @param size Pointer to an integer that receives the returned buffer size.
5764
 * @param options Pointer to WebP write options, or NULL for defaults.
5765
 *
5766
 * @return Returns a pointer to the newly allocated WebP buffer, or NULL on failure.
5767
 */
5768
BGD_DECLARE(void *)
5769
gdImageWebpPtrWithOptions(gdImagePtr im, int *size, const gdWebpWriteOptions *options);
5770
5771
/**
5772
 * @brief Write an image as WebP data to a gdIOCtx with a quality setting.
5773
 * @ingroup gdCodecWebp
5774
 *
5775
 * gdImageWebpCtx() does not close outfile. The image is borrowed for the
5776
 * duration of the call and must be a truecolor image.
5777
 *
5778
 * @param im The image to write.
5779
 * @param outfile Pointer to the gdIOCtx output context.
5780
 * @param quantization WebP quality: -1 for default, 0-100 for lossy, or gdWebpLossless for lossless.
5781
 */
5782
BGD_DECLARE(void)
5783
gdImageWebpCtx(gdImagePtr im, gdIOCtxPtr outfile, int quantization);
5784
5785
/*
5786
  Group: Types
5787
5788
  typedef: gdSink
5789
5790
  typedef: gdSinkPtr
5791
5792
        *Note:* This interface is *obsolete* and kept only for
5793
        *compatibility*.  Use <gdIOCtx> instead.
5794
5795
        Represents a "sink" (destination) to which a PNG can be
5796
        written. Programmers who do not wish to write PNGs to a file can
5797
        provide their own alternate output mechanism, using the
5798
        <gdImagePngToSink> function. See the documentation of that
5799
        function for an example of the proper use of this type.
5800
5801
        > typedef struct {
5802
        >     int (*sink) (void *context, char *buffer, int len);
5803
        >     void *context;
5804
        > } gdSink, *gdSinkPtr;
5805
5806
        The _sink_ function must return -1 on error, otherwise the number of
5807
        bytes written, which must be equal to len.
5808
5809
        _context_ will be passed to your sink function.
5810
5811
*/
5812
5813
/** @deprecated in favor of gdIOCtx */
5814
typedef struct {
5815
    int (*sink)(void *context, const char *buffer, int len);
5816
    void *context;
5817
} gdSink, *gdSinkPtr;
5818
5819
/** @deprecated in favor of gdIOCtx */
5820
BGD_DECLARE(void) gdImagePngToSink(gdImagePtr im, gdSinkPtr out);
5821
/** @deprecated in favor of gdIOCtx */
5822
BGD_DECLARE(void) gdImageQoiToSink(gdImagePtr im, gdSinkPtr out);
5823
5824
/**
5825
 * @addtogroup gdCodecGd2
5826
 * @{
5827
 */
5828
5829
/** @name GD2 Writing */
5830
/** @{ */
5831
5832
/**
5833
 * @brief Write an image as GD2 data to a stdio file.
5834
 * @deprecated
5835
 *
5836
 * gdImageGd2() borrows im and out for the duration of the call and does not
5837
 * close out. Pass cs as 0 to use GD2_CHUNKSIZE; otherwise values outside the
5838
 * GD2_CHUNKSIZE_MIN to GD2_CHUNKSIZE_MAX range are clamped. The public fmt
5839
 * values are GD2_FMT_RAW and GD2_FMT_COMPRESSED. For truecolor images, gd
5840
 * writes the corresponding internal truecolor GD2 format automatically.
5841
 *
5842
 * @param im The image to write.
5843
 * @param out Pointer to the output FILE stream.
5844
 * @param cs Requested chunk size in pixels, or 0 for GD2_CHUNKSIZE.
5845
 * @param fmt Output format, GD2_FMT_RAW or GD2_FMT_COMPRESSED.
5846
 */
5847
BGD_DECLARE(void) gdImageGd2(gdImagePtr im, FILE *out, int cs, int fmt);
5848
5849
/**
5850
 * @brief Write an image as GD2 data to a newly allocated memory buffer.
5851
 * @deprecated
5852
 *
5853
 * gdImageGd2Ptr() borrows im for the duration of the call. Pass cs as 0 to use
5854
 * GD2_CHUNKSIZE; otherwise values outside the GD2_CHUNKSIZE_MIN to
5855
 * GD2_CHUNKSIZE_MAX range are clamped. The public fmt values are GD2_FMT_RAW
5856
 * and GD2_FMT_COMPRESSED. On success, the returned buffer is owned by the
5857
 * caller and must be freed with gdFree().
5858
 *
5859
 * @param im The image to write.
5860
 * @param cs Requested chunk size in pixels, or 0 for GD2_CHUNKSIZE.
5861
 * @param fmt Output format, GD2_FMT_RAW or GD2_FMT_COMPRESSED.
5862
 * @param size Output location for the returned buffer size in bytes.
5863
 * @return A newly allocated GD2 buffer, or NULL on error.
5864
 */
5865
BGD_DECLARE(void *) gdImageGd2Ptr(gdImagePtr im, int cs, int fmt, int *size);
5866
5867
/** @} */
5868
5869
/** @} */
5870
5871
/**
5872
 * @brief Destroys an image and frees its memory
5873
 *
5874
 * @param im The image to destroy.
5875
 */
5876
BGD_DECLARE(void) gdImageDestroy(gdImagePtr im);
5877
5878
/**
5879
 * @brief Allocates a color
5880
 *
5881
 * This is a simplified variant of <gdImageColorAllocateAlpha> where the alpha
5882
 * channel is always opaque.
5883
 *
5884
 * @param im The image.
5885
 * @param r  The value of the red component.
5886
 * @param g  The value of the green component.
5887
 * @param b  The value of the blue component.
5888
 *
5889
 * @return The color value.
5890
 *
5891
 * @see gdImageColorDeallocate
5892
 */
5893
BGD_DECLARE(int) gdImageColorAllocate(gdImagePtr im, int r, int g, int b);
5894
5895
/**
5896
 * @brief Allocates a color
5897
 *
5898
 * This is typically used for palette images, but can be used for truecolor
5899
 * images as well.
5900
 *
5901
 * @param im The image.
5902
 * @param r  The value of the red component.
5903
 * @param g  The value of the green component.
5904
 * @param b  The value of the blue component.
5905
 *
5906
 * @return The color value.
5907
 *
5908
 * @see gdImageColorDeallocate
5909
 */
5910
BGD_DECLARE(int)
5911
gdImageColorAllocateAlpha(gdImagePtr im, int r, int g, int b, int a);
5912
5913
/** @brief Gets the closest color of the image
5914
 *
5915
 * This is a simplified variant of <gdImageColorClosestAlpha> where the alpha
5916
 * channel is always opaque.
5917
 *
5918
 * @param im The image.
5919
 * @param r  The value of the red component.
5920
 * @param g  The value of the green component.
5921
 * @param b  The value of the blue component.
5922
 *
5923
 * @return The closest color already available in the palette for palette images;
5924
 *         the color value of the given components for truecolor images.
5925
 *
5926
 * @see gdImageColorExact
5927
 */
5928
BGD_DECLARE(int) gdImageColorClosest(gdImagePtr im, int r, int g, int b);
5929
5930
/**
5931
 * @brief Gets the closest color of the image with alpha channel
5932
 *
5933
 * @param im The image.
5934
 * @param r  The value of the red component.
5935
 * @param g  The value of the green component.
5936
 * @param b  The value of the blue component.
5937
 * @param a  The value of the alpha component.
5938
 *
5939
 * @return The closest color already available in the palette for palette images;
5940
 *         the color value of the given components for truecolor images.
5941
 *
5942
 * @see gdImageColorExactAlpha
5943
 */
5944
BGD_DECLARE(int)
5945
gdImageColorClosestAlpha(gdImagePtr im, int r, int g, int b, int a);
5946
5947
/**
5948
 * @brief Gets the closest color of the image using HWB color space
5949
 *
5950
 * This function finds the closest color in the image's palette to the specified RGB color using the HWB (Hue, Whiteness, Blackness) color space. It is a more perceptually accurate method for color matching compared to simple RGB distance calculations.
5951
 *
5952
 * @param im The image.
5953
 * @param r  The value of the red component.
5954
 * @param g  The value of the green component.
5955
 * @param b  The value of the blue component.
5956
 *
5957
 * @return The closest color already available in the palette for palette images; if
5958
 *         there is no exact color, -1 is returned.
5959
 *         For truecolor images the color value of the given components is returned.
5960
 *
5961
 * @see gdImageColorExact
5962
 */
5963
BGD_DECLARE(int) gdImageColorClosestHWB(gdImagePtr im, int r, int g, int b);
5964
5965
/**
5966
 * @brief Gets the exact color of the image
5967
 *
5968
 * This is a simplified variant of <gdImageColorExactAlpha> where the alpha
5969
 * channel is always opaque.
5970
 *
5971
 * @param im The image.
5972
 * @param r  The value of the red component.
5973
 * @param g  The value of the green component.
5974
 * @param b  The value of the blue component.
5975
 *
5976
 * @return The exact color already available in the palette for palette images; if
5977
 *         there is no exact color, -1 is returned.
5978
 *         For truecolor images the color value of the given components is returned.
5979
 *
5980
 * @see gdImageColorClosest
5981
 */
5982
BGD_DECLARE(int) gdImageColorExact(gdImagePtr im, int r, int g, int b);
5983
5984
/**
5985
 * @brief Gets the exact color of the image
5986
 *
5987
 * This is a simplified variant of <gdImageColorExactAlpha> where the alpha
5988
 * channel is always opaque.
5989
 *
5990
 * @param im The image.
5991
 * @param r  The value of the red component.
5992
 * @param g  The value of the green component.
5993
 * @param b  The value of the blue component.
5994
 * @param a  The value of the alpha component.
5995
 *
5996
 * @return The exact color already available in the palette for palette images; if
5997
 *         there is no exact color, -1 is returned.
5998
 *         For truecolor images the color value of the given components is returned.
5999
 *
6000
 * @see gdImageColorClosestAlpha gdTrueColorAlpha
6001
 */
6002
BGD_DECLARE(int)
6003
gdImageColorExactAlpha(gdImagePtr im, int r, int g, int b, int a);
6004
6005
/**
6006
 * @brief Resolves a color in the image
6007
 * @see gdImageColorResolve is an alternative for the code fragment
6008
 * @code
6009
 *  if ((color=gdImageColorExact(im,R,G,B)) < 0)
6010
 *      if ((color=gdImageColorAllocate(im,R,G,B)) < 0)
6011
 *          color=gdImageColorClosest(im,R,G,B);
6012
 * @endcode
6013
 * in a single function.    Its advantage is that it is guaranteed to
6014
 * @return a color index in one search over the color table.
6015
  */
6016
BGD_DECLARE(int) gdImageColorResolve(gdImagePtr im, int r, int g, int b);
6017
6018
/**
6019
 * @brief Same as @ref gdImageColorResovle but with alpha
6020
 *
6021
 * @param im The image.
6022
 * @param r The red component.
6023
 * @param g The green component.
6024
 * @param b The blue component.
6025
 * @param a The alpha component.
6026
 *
6027
 * @return The color index of the closest color in the palette or the newly allocated color.
6028
 *
6029
 * @see gdImageColorExactAlpha gdImageColorClosestAlpha gdTrueColorAlpha
6030
 */
6031
BGD_DECLARE(int)
6032
gdImageColorResolveAlpha(gdImagePtr im, int r, int g, int b, int a);
6033
6034
/**
6035
 * @brief Compose a truecolor value from its components
6036
 *
6037
 * use it only when needed an actual truecolor value, for example when drawing on a truecolor image.
6038
 * @param r The red channel (0-255)
6039
 * @param g The green channel (0-255)
6040
 * @param b The blue channel (0-255)
6041
 *
6042
 * @see gdTrueColorAlpha gdTrueColorGetAlpha gdTrueColorGetRed gdTrueColorGetGreen gdTrueColorGetBlue
6043
 */
6044
0
#define gdTrueColor(r, g, b) (((r) << 16) + ((g) << 8) + (b))
6045
6046
/**
6047
 * Group: Color Composition
6048
 */
6049
6050
/**
6051
 * @brief Compose a truecolor value from its components
6052
 *
6053
 * @param r The red channel (0-255)
6054
 * @param g The green channel (0-255)
6055
 * @param b The blue channel (0-255)
6056
 * @param a The alpha channel (0-127, where 127 is fully transparent, and 0 is
6057
 *          completely opaque).
6058
 *
6059
 * @see gdTrueColorGetAlpha gdTrueColorGetRed gdTrueColorGetGreen gdTrueColorGetBlue gdImageColorExactAlpha
6060
 */
6061
0
#define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
6062
6063
/**
6064
 * @brief Removes a palette entry
6065
 *
6066
 * This is a no-op for truecolor images.
6067
 * The function does not alter the image data nor the transparent color or any
6068
 * other places where this color index could have been referenced.
6069
 * The index is marked as open and will be used too for any subsequent
6070
 * @ref gdImageColorAllocate or @ref gdImageColorAllocateAlpha calls. Other lower
6071
 * index may be open as well, the fist open index found will be used.
6072
 *
6073
 * @param im    The image.
6074
 * @param color The palette index.
6075
 *
6076
 * @see gdImageColorAllocate gdImageColorAllocateAlpha
6077
 */
6078
BGD_DECLARE(void) gdImageColorDeallocate(gdImagePtr im, int color);
6079
6080
/**
6081
 * @brief Bring the palette colors in im2 to be closer to im1.
6082
 *
6083
 * @param im1 The first image.
6084
 * @param im2 The second image.
6085
 *
6086
 * @return 0 on success, or -1 on failure.
6087
 */
6088
BGD_DECLARE(int) gdImageColorMatch(gdImagePtr im1, gdImagePtr im2);
6089
6090
/**
6091
 * @brief Sets the transparent color of the image
6092
 *
6093
 *
6094
 * Specifies a color index (if a palette image) or an
6095
 * RGB color (if a truecolor image) which should be
6096
 * considered 100% transparent. FOR TRUECOLOR IMAGES,
6097
 * THIS IS IGNORED IF AN ALPHA CHANNEL IS BEING
6098
 * SAVED. Use gdImageSaveAlpha(im, 0); to
6099
 * turn off the saving of a full alpha channel in
6100
 * a truecolor image. Note that gdImageColorTransparent
6101
 * is usually compatible with older browsers that
6102
 * do not understand full alpha channels well. TBB
6103
 *
6104
 * @param im    The image.
6105
 * @param color The color.
6106
 *
6107
 * @see gdImageGetTransparent
6108
 */
6109
BGD_DECLARE(void) gdImageColorTransparent(gdImagePtr im, int color);
6110
6111
/**
6112
 * @brief Copies the palette from one image to another
6113
 *
6114
 * @param dst The destination image.
6115
 * @param src The source image.
6116
 */
6117
BGD_DECLARE(void) gdImagePaletteCopy(gdImagePtr dst, gdImagePtr src);
6118
6119
typedef int (*gdCallbackImageColor)(gdImagePtr im, int src);
6120
6121
/**
6122
 * @brief Replaces a color in the image with another color
6123
 *
6124
 * @param im  The image.
6125
 * @param src The source color to be replaced.
6126
 * @param dst The destination color to replace with.
6127
 */
6128
BGD_DECLARE(int) gdImageColorReplace(gdImagePtr im, int src, int dst);
6129
6130
/**
6131
 * @brief Replaces colors in an image with a threshold for perceptual color distance.
6132
 *
6133
 * Note: threshold semantics changed in versions >=2.3.4 — the value now scales
6134
 * linearly with perceptual color distance. Callers using threshold values
6135
 * tuned against the old behavior should apply new_t = sqrt(old_t / 100) * 100
6136
 * to approximate the previous behavior. This is due to a bug fix in the color
6137
 * distance calculation, which previously did not take the square root
6138
 * of the sum of squares, and thus returned a value that was the square
6139
 * of the actual perceptual color distance.
6140
 * The new behavior is more intuitive and consistent with common color distance metrics
6141
 *
6142
 * @param im The image to operate on.
6143
 * @param src The source color to replace.
6144
 * @param dst The destination color to replace with.
6145
 * @param threshold The threshold for color matching. Colors within this distance from the source color will be replaced with the destination color.
6146
 * @return The number of pixels that were replaced.
6147
 */
6148
BGD_DECLARE(int)
6149
gdImageColorReplaceThreshold(gdImagePtr im, int src, int dst, float threshold);
6150
6151
/**
6152
 * @brief Replaces multiple colors in an image with corresponding destination colors.
6153
 *
6154
 * @param im The image to operate on.
6155
 * @param len The number of colors to replace.
6156
 * @param src An array of source colors to be replaced.
6157
 * @param dst An array of destination colors to replace with.
6158
 *
6159
 * @return The number of pixels that were replaced.
6160
 */
6161
BGD_DECLARE(int)
6162
gdImageColorReplaceArray(gdImagePtr im, int len, int *src, int *dst);
6163
6164
/**
6165
 * @brief Replaces colors in an image using a callback function to determine the replacement color.
6166
 *
6167
 * @param im The image to operate on.
6168
 * @param callback A callback function that takes the image and a source color as parameters and returns the destination color to replace with. @see gdCallbackImageColor
6169
 *
6170
 * @return The number of pixels that were replaced.
6171
 */
6172
BGD_DECLARE(int)
6173
gdImageColorReplaceCallback(gdImagePtr im, gdCallbackImageColor callback);
6174
6175
/**
6176
 * @defgroup  Per Pixel Operations
6177
 * @{ */
6178
6179
/**
6180
 * @brief Sets the pixel at the specified coordinates to the given color.
6181
 * Replaces or blends with the background depending on the
6182
 * most recent call to @ref gdImageAlphaBlending and the
6183
 * alpha channel value of 'color'; default is to overwrite.
6184
 * Tiling and line styling are also implemented
6185
 * here. All other gd drawing functions pass through this call,
6186
 * allowing for many useful effects.
6187
 * Overlay and multiply effects are used when @ref gdImageAlphaBlending
6188
 * is passed @ref gdEffectOverlay and @ref gdEffectMultiply
6189
 *
6190
 * @param im The image.
6191
 * @param x The x-coordinate of the pixel.
6192
 * @param y The y-coordinate of the pixel.
6193
 * @param color The color to set the pixel to. Color can be a palette index for palette images or a truecolor value for truecolor images.
6194
 *
6195
 * @see @ref gdImageGetPixel gdImageGetTrueColorPixel gdImageAlphaBlending gdImageCreateTruecolor gdImageCreatePalette
6196
 */
6197
BGD_DECLARE(void) gdImageSetPixel(gdImagePtr im, int x, int y, int color);
6198
6199
/**
6200
 * @brief Gets the color of the pixel at the specified coordinates.
6201
 *
6202
 * @param im The image.
6203
 * @param x The x-coordinate of the pixel.
6204
 * @param y The y-coordinate of the pixel.
6205
 *
6206
 * @return The color of the pixel. For palette images, this is the palette index. For truecolor images, this is the truecolor value.
6207
 */
6208
BGD_DECLARE(int) gdImageGetPixel(gdImagePtr im, int x, int y);
6209
6210
/**
6211
 * @brief Gets the truecolor value of the pixel at the specified coordinates.
6212
 *
6213
 * @param im The image.
6214
 * @param x The x-coordinate of the pixel.
6215
 * @param y The y-coordinate of the pixel.
6216
 *
6217
 * @return The truecolor value of the pixel. For palette images, this function will return the truecolor value corresponding to the palette index of the pixel.
6218
 */
6219
BGD_DECLARE(int) gdImageGetTrueColorPixel(gdImagePtr im, int x, int y);
6220
/** @} */
6221
6222
/**
6223
 * @brief Sets the resolution of an image.
6224
 *
6225
 * @param im    The image.
6226
 * @param res_x The horizontal resolution in DPI.
6227
 * @param res_y The vertical resolution in DPI.
6228
 *
6229
 * @see gdImageResolutionX gdImageResolutionY
6230
 */
6231
BGD_DECLARE(void)
6232
gdImageSetResolution(gdImagePtr im, const unsigned int res_x, const unsigned int res_y);
6233
6234
/**
6235
 * @defgroup Font Text Rendering, Bitmap Fonts
6236
 *
6237
 * @{ */
6238
6239
/**
6240
 * @brief Gets the built-in giant font.
6241
 */
6242
BGD_DECLARE(gdFontPtr) gdFontGetGiant(void);
6243
/**
6244
 * @brief Gets the built-in large font.
6245
 */
6246
BGD_DECLARE(gdFontPtr) gdFontGetLarge(void);
6247
/**
6248
 * @brief Gets the built-in medium bold font.
6249
 */
6250
BGD_DECLARE(gdFontPtr) gdFontGetMediumBold(void);
6251
/**
6252
 * @brief Gets the built-in small font.
6253
 */
6254
BGD_DECLARE(gdFontPtr) gdFontGetSmall(void);
6255
/**
6256
 * @brief Gets the built-in tiny font.
6257
 */
6258
BGD_DECLARE(gdFontPtr) gdFontGetTiny(void);
6259
/**
6260
 * @brief Draws a single character.
6261
 *
6262
 * @param im    The image to draw onto.
6263
 * @param f     The raster font.
6264
 * @param x     The x coordinate of the upper left pixel.
6265
 * @param y     The y coordinate of the upper left pixel.
6266
 * @param c     The character.
6267
 * @param color The color.
6268
 *
6269
 * Variants @ref gdImageCharUp
6270
 *
6271
 * @see gdFontPtr
6272
 */
6273
BGD_DECLARE(void) gdImageChar(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color);
6274
6275
/**
6276
 * @brief Draws a single character rotated 90 degrees counterclockwise.
6277
 *
6278
 * @param im    The image to draw onto.
6279
 * @param f     The raster font.
6280
 * @param x     The x coordinate of the upper left pixel.
6281
 * @param y     The y coordinate of the upper left pixel.
6282
 * @param c     The character.
6283
 * @param color The color.
6284
 */
6285
BGD_DECLARE(void) gdImageCharUp(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color);
6286
6287
/**
6288
 * @brief Draws a character string.
6289
 *
6290
 * @param im    The image to draw onto.
6291
 * @param f     The raster font.
6292
 * @param x     The x coordinate of the upper left pixel.
6293
 * @param y     The y coordinate of the upper left pixel.
6294
 * @param s     The character string.
6295
 * @param color The color.
6296
 *
6297
 * Variants:
6298
 *  - @ref gdImageStringUp
6299
 *  - @ref gdImageString16
6300
 *  - @ref gdImageStringUp16
6301
 *
6302
 * @see gdFontPtr gdImageStringTTF gdImageString
6303
 */
6304
BGD_DECLARE(void)
6305
gdImageString(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color);
6306
6307
/**
6308
 * @brief Draws a string rotated 90 degrees counterclockwise.
6309
 *
6310
 * @param im    The image to draw onto.
6311
 * @param f     The raster font.
6312
 * @param x     The x coordinate of the upper left pixel.
6313
 * @param y     The y coordinate of the upper left pixel.
6314
 * @param s     The string.
6315
 * @param color The color.
6316
 */
6317
BGD_DECLARE(void)
6318
gdImageStringUp(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color);
6319
6320
/**
6321
 * @brief Draws a character string with 16-bit characters.
6322
 *
6323
 * @param im    The image to draw onto.
6324
 * @param f     The raster font.
6325
 * @param x     The x coordinate of the upper left pixel.
6326
 * @param y     The y coordinate of the upper left pixel.
6327
 * @param s     The character string (16-bit).
6328
 * @param color The color.
6329
 */
6330
BGD_DECLARE(void)
6331
gdImageString16(gdImagePtr im, gdFontPtr f, int x, int y, unsigned short *s, int color);
6332
6333
/**
6334
 * @brief Draws a string rotated 90 degrees counterclockwise with 16-bit characters.
6335
 *
6336
 * @param im    The image to draw onto.
6337
 * @param f     The raster font.
6338
 * @param x     The x coordinate of the upper left pixel.
6339
 * @param y     The y coordinate of the upper left pixel.
6340
 * @param s     The string (16-bit).
6341
 * @param color The color.
6342
 */
6343
BGD_DECLARE(void)
6344
gdImageStringUp16(gdImagePtr im, gdFontPtr f, int x, int y, unsigned short *s, int color);
6345
/** @} */
6346
6347
/**
6348
 * @defgroup freetypefont Font Text Rendering, FreeType 2
6349
 * @{
6350
 */
6351
6352
/**
6353
 * @brief Set up the font cache.
6354
 *
6355
 * This is called automatically from the string rendering functions, if it
6356
 * has not already been called. So there's no need to call this function
6357
 * explicitly.
6358
 */
6359
BGD_DECLARE(int) gdFontCacheSetup(void);
6360
6361
/**
6362
 * @brief Shut down the font cache and free the allocated resources.
6363
 *
6364
 * @note This function has to be called whenever FreeType operations have been invoked, to avoid resource leaks. It doesn't harm to call this function multiple times.
6365
 */
6366
BGD_DECLARE(void) gdFontCacheShutdown(void);
6367
6368
/**
6369
 * @brief Alias of @ref gdFontCacheShutdown.
6370
 * @deprecated
6371
 */
6372
BGD_DECLARE(void) gdFreeFontCache(void);
6373
6374
6375
/**
6376
 * @brief Draws a string using FreeType 2 fonts. Alias of @ref gdImageStringFT. Provided for backwards compatibility only.
6377
 * @deprecated
6378
 */
6379
BGD_DECLARE(char *)
6380
gdImageStringTTF(gdImagePtr im, int *brect, int fg, const char *fontlist, double ptsize,
6381
                 double angle, int x, int y, const char *string);
6382
6383
6384
/**
6385
 * @brief Render an UTF-8 string onto a gd image.
6386
 *
6387
 * @param im       The image to draw onto.
6388
 * @param brect    The bounding rectangle as array of 8 integers where each pair
6389
 *                 represents the x- and y-coordinate of a point. The points
6390
 *                 specify the lower left, lower right, upper right and upper left
6391
 *                 corner.
6392
 * @param fg       The font color.
6393
 * @param fontlist The semicolon delimited list of font filenames to look for.
6394
 * @param ptsize   The height of the font in typographical points (pt).
6395
 * @param angle    The angle in radian to rotate the font counter-clockwise.
6396
 * @param x        The x-coordinate of the basepoint (roughly the lower left corner)
6397
 *                   of the first letter.
6398
 * @param y        The y-coordinate of the basepoint (roughly the lower left corner)
6399
 *                   of the first letter.
6400
 * @param string   The string to render.
6401
 *
6402
 * Variant @ref gdImageStringFTEx
6403
 *
6404
 * @see gdImageString
6405
 */
6406
BGD_DECLARE(char *)
6407
gdImageStringFT(gdImagePtr im, int *brect, int fg, const char *fontlist, double ptsize,
6408
                double angle, int x, int y, const char *string);
6409
6410
/* 2.0.5: provides an extensible way to pass additional parameters.
6411
   Thanks to Wez Furlong, sorry for the delay. */
6412
/**
6413
 * @brief Structure for passing additional parameters to FreeType 2 string rendering functions.
6414
 *
6415
 * This structure allows for fine-tuning of FreeType 2 string rendering, including line spacing, character mapping, resolution, and more. It is used with the @ref gdImageStringFTEx function.
6416
 */
6417
typedef struct {
6418
    int flags; /**< Logical OR of gdFTEX_* option flags. */
6419
    double linespacing; /**< Fine-tunes line spacing for newline-separated text. */
6420
    int charmap; /**< Character map to use when @ref gdFTEX_CHARMAP is set: @ref gdFTEX_Unicode,
6421
                      @ref gdFTEX_Shift_JIS, @ref gdFTEX_Big5, or @ref gdFTEX_Adobe_Custom.
6422
                      When not specified, maps are searched in that order. */
6423
    int hdpi; /**< Horizontal resolution in DPI when @ref gdFTEX_RESOLUTION is set. */
6424
    int vdpi; /**< Vertical resolution in DPI when @ref gdFTEX_RESOLUTION is set. */
6425
    char *xshow; /**< When @ref gdFTEX_XSHOW is set, receives a gd-allocated string
6426
                      containing xshow position data for the last string. The
6427
                      caller must free it with gdFree(). */
6428
    char *fontpath; /**< When @ref gdFTEX_RETURNFONTPATHNAME is set, receives a
6429
                         gd-allocated string containing the actual font file
6430
                         path used. This is useful when fontconfig selects the
6431
                         font. The caller must free it with gdFree(). */
6432
} gdFTStringExtra, *gdFTStringExtraPtr;
6433
6434
/**
6435
 * @name gdFTStringExtra option flags
6436
 *
6437
 * These flags are combined in gdFTStringExtra::flags and used by
6438
 * @ref gdImageStringFTEx.
6439
 *
6440
 * @{
6441
 */
6442
#define gdFTEX_LINESPACE 1 /**< Use gdFTStringExtra::linespacing for
6443
                                newline-separated text. The value is a multiple
6444
                                of the font height; without this flag, the
6445
                                default line spacing is 1.05. */
6446
#define gdFTEX_CHARMAP 2 /**< Use gdFTStringExtra::charmap as the preferred
6447
                              FreeType character map. If the requested map is
6448
                              not available, GD attempts compatible fallback
6449
                              maps where possible. */
6450
#define gdFTEX_RESOLUTION 4 /**< Use gdFTStringExtra::hdpi and
6451
                                 gdFTStringExtra::vdpi as the FreeType
6452
                                 rendering resolution. Without this flag, GD
6453
                                 uses `GD_RESOLUTION` for both axes. */
6454
#define gdFTEX_DISABLE_KERNING 8 /**< Disable FreeType kerning adjustments
6455
                                      between consecutive glyphs. */
6456
#define gdFTEX_XSHOW 16 /**< Return a gd-allocated xshow advance string in
6457
                             gdFTStringExtra::xshow. The caller must free that
6458
                             string with gdFree(). */
6459
#define gdFTEX_FONTPATHNAME 32 /**< Interpret the fontlist argument as a full
6460
                                    or partial font file path, even when
6461
                                    fontconfig has been enabled by default with
6462
                                    gdFTUseFontConfig(). */
6463
#define gdFTEX_FONTCONFIG 64 /**< Interpret the fontlist argument as a
6464
                                  fontconfig pattern for this call. This is not
6465
                                  needed when fontconfig has already been
6466
                                  enabled by default with gdFTUseFontConfig(). */
6467
#define gdFTEX_RETURNFONTPATHNAME 128 /**< Return a gd-allocated copy of the
6468
                                           actual font file path used in
6469
                                           gdFTStringExtra::fontpath. This is
6470
                                           useful when fontconfig selects the
6471
                                           font. The caller must free that string
6472
                                           with gdFree(). */
6473
/** @} */
6474
6475
/**
6476
 * @brief Enable or disable fontconfig by default.
6477
 *
6478
 * If flag is nonzero, the fontlist parameter to gdImageStringFT
6479
 * and @ref gdImageStringFTEx shall be assumed to be a fontconfig font pattern
6480
 * if fontconfig was compiled into gd. This function returns zero
6481
 * if fontconfig is not available, nonzero otherwise.
6482
 * If GD is built without libfontconfig support, this function is a NOP.
6483
 *
6484
 * @param flag Zero to disable, nonzero to enable.
6485
 *
6486
 * @see gdImageStringFTEx
6487
 */
6488
BGD_DECLARE(int) gdFTUseFontConfig(int flag);
6489
6490
/**
6491
 * @name gdFTStringExtra character map values
6492
 *
6493
 * These are not option flags. Set one value in gdFTStringExtra::charmap when
6494
 * gdFTStringExtra::flags includes @ref gdFTEX_CHARMAP. Without
6495
 * @ref gdFTEX_CHARMAP, GD prefers Unicode.
6496
 *
6497
 * @{
6498
 */
6499
#define gdFTEX_Unicode 0 /**< Prefer a Unicode character map. GD may fall back
6500
                              to symbol or Adobe maps when no Unicode map is
6501
                              available. */
6502
#define gdFTEX_Shift_JIS 1 /**< Prefer a Shift_JIS character map. */
6503
#define gdFTEX_Big5 2 /**< Prefer a Big5 character map. */
6504
#define gdFTEX_Adobe_Custom 3 /**< Prefer an Adobe Custom character map. GD may
6505
                                   fall back to Apple Roman when no Adobe Custom
6506
                                   map is available. */
6507
#define gdFTEX_MacRoman gdFTEX_Adobe_Custom /**< Deprecated compatibility name
6508
                                                 used by bundled PHP's
6509
                                                 historical libgd. */
6510
/** @} */
6511
6512
/**
6513
 * @brief Draws a string using FreeType 2 fonts with additional parameters.
6514
 *
6515
 * gdImageStringFTEx() extends @ref gdImageStringFT by accepting an optional
6516
 * @ref gdFTStringExtra structure. Pass NULL for strex to use the same behavior
6517
 * as gdImageStringFT().
6518
 *
6519
 * The gdFTStringExtra::flags field controls which extra fields are used:
6520
 * - @ref gdFTEX_LINESPACE uses gdFTStringExtra::linespacing for multiline
6521
 *   text. The value is expressed as a multiple of the font height; 1.0 is the
6522
 *   minimum spacing that normally prevents lines from colliding. Without this
6523
 *   flag, or when strex is NULL, line spacing defaults to 1.05.
6524
 *   @code{.c}
6525
 *   strex.flags |= gdFTEX_LINESPACE;
6526
 *   strex.linespacing = 1.2;
6527
 *   @endcode
6528
 * - @ref gdFTEX_CHARMAP uses gdFTStringExtra::charmap as the preferred
6529
 *   character map. Valid values are @ref gdFTEX_Unicode,
6530
 *   @ref gdFTEX_Shift_JIS, @ref gdFTEX_Big5, and
6531
 *   @ref gdFTEX_Adobe_Custom. Without this flag, GD tries Unicode first. If
6532
 *   the preferred map is unavailable, GD attempts compatible fallback maps
6533
 *   where possible.
6534
 *   @code{.c}
6535
 *   strex.flags |= gdFTEX_CHARMAP;
6536
 *   strex.charmap = gdFTEX_Unicode;
6537
 *   @endcode
6538
 * - @ref gdFTEX_RESOLUTION uses gdFTStringExtra::hdpi and
6539
 *   gdFTStringExtra::vdpi as the FreeType rendering resolution in dots per
6540
 *   inch. Without this flag, GD uses its default screen resolution.
6541
 *   @code{.c}
6542
 *   strex.flags |= gdFTEX_RESOLUTION;
6543
 *   strex.hdpi = 300;
6544
 *   strex.vdpi = 300;
6545
 *   @endcode
6546
 * - @ref gdFTEX_DISABLE_KERNING disables FreeType kerning adjustments between
6547
 *   consecutive glyphs.
6548
 *   @code{.c}
6549
 *   strex.flags |= gdFTEX_DISABLE_KERNING;
6550
 *   @endcode
6551
 * - @ref gdFTEX_XSHOW returns a gd-allocated string of character advance
6552
 *   values in gdFTStringExtra::xshow. The caller must free this string with
6553
 *   gdFree().
6554
 *   @code{.c}
6555
 *   strex.flags |= gdFTEX_XSHOW;
6556
 *   @endcode
6557
 * - @ref gdFTEX_RETURNFONTPATHNAME returns a gd-allocated copy of the actual
6558
 *   font file path used in gdFTStringExtra::fontpath. The caller must free this
6559
 *   string with gdFree().
6560
 *   @code{.c}
6561
 *   strex.flags |= gdFTEX_RETURNFONTPATHNAME;
6562
 *   @endcode
6563
 *
6564
 * Font selection normally treats fontlist as a semicolon-delimited list of font
6565
 * file names. When GD is built with fontconfig, @ref gdFTEX_FONTCONFIG makes
6566
 * fontlist a fontconfig pattern for this call, and gdFTUseFontConfig() can make
6567
 * fontconfig patterns the default. If fontconfig has been enabled by default,
6568
 * @ref gdFTEX_FONTPATHNAME forces fontlist to be interpreted as font path names
6569
 * for this call.
6570
 * @code{.c}
6571
 * strex.flags |= gdFTEX_FONTCONFIG;
6572
 * strex.flags |= gdFTEX_FONTPATHNAME;
6573
 * @endcode
6574
 *
6575
 * If brect is not NULL, it must point to an array of 8 integers. On success,
6576
 * GD fills it with the lower-left, lower-right, upper-right, and upper-left
6577
 * corners of the rendered text bounding rectangle. Passing NULL for im computes
6578
 * the bounding rectangle without drawing.
6579
 *
6580
 * @param im       The image to draw onto, or NULL to compute brect only.
6581
 * @param brect    Optional output array of 8 integers receiving the text
6582
 *                 bounding rectangle.
6583
 * @param fg       The font color. Negative values select monochrome rendering
6584
 *                 using -fg as the color.
6585
 * @param fontlist The semicolon-delimited list of font file names, or a
6586
 *                 fontconfig pattern when fontconfig mode is active.
6587
 * @param ptsize   The height of the font in typographical points.
6588
 * @param angle    The angle in radians to rotate the font counter-clockwise.
6589
 * @param x        The x-coordinate of the baseline starting point.
6590
 * @param y        The y-coordinate of the baseline starting point.
6591
 * @param string   The string to render.
6592
 * @param strex    Optional pointer to a gdFTStringExtra structure containing
6593
 *                 additional rendering options, or NULL.
6594
 *
6595
 * @return NULL on success, or a pointer to a static error message on failure.
6596
 */
6597
BGD_DECLARE(char *)
6598
gdImageStringFTEx(gdImagePtr im, int *brect, int fg, const char *fontlist, double ptsize,
6599
                  double angle, int x, int y, const char *string, gdFTStringExtraPtr strex);
6600
6601
/** @} */
6602
6603
/**
6604
 * @defgroup PixelDraw lines, ellipses, polygons and Arc Drawing pixel operations
6605
 *
6606
 * @note 2.4+ brings a 2D Vector APIs with high quality rendering and options. Similar to Canvas 2D APIs. We recommend it for new usages.
6607
 *
6608
 * @{
6609
 */
6610
6611
 /**
6612
 * @brief A point in the coordinate space of the image
6613
 */
6614
typedef struct {
6615
    int x, y; /**< The x and y coordinates of the point. */
6616
} gdPoint, *gdPointPtr; /**< A pointer to a <gdPoint>. */
6617
6618
/**
6619
 * @brief A rectangle in the coordinate space of the image
6620
  */
6621
typedef struct {
6622
    int x, y; /**< The x and y coordinates of the upper left corner. */
6623
    int width, height; /**< The width and height of the rectangle. */
6624
} gdRect, *gdRectPtr; /**< A pointer to a @ref gdRect. */
6625
6626
/**
6627
 * @brief Style flags for drawing arcs and chords
6628
 * Style is a bitwise OR ( | operator ) of these.
6629
 * gdArc and gdChord are mutually exclusive;
6630
 * gdChord just connects the starting and ending
6631
 * angles with a straight line, while gdArc produces
6632
 * a rounded edge. gdPie is a synonym for gdArc.
6633
 * gdNoFill indicates that the arc or chord should be
6634
 * outlined, not filled. gdEdged, used together with
6635
 * gdNoFill, indicates that the beginning and ending
6636
 * angles should be connected to the center; this is
6637
 * a good way to outline (rather than fill) a
6638
 * 'pie slice'.
6639
 */
6640
#define gdArc 0 /**< mutually exclusive with gdChord */
6641
#define gdPie gdArc /**< synonym for gdArc */
6642
0
#define gdChord 1 /**< mutually exclusive with gdArc */
6643
0
#define gdNoFill 2 /**< indicates that the arc or chord should be outlined, not filled */
6644
0
#define gdEdged 4 /**< used together with gdNoFill, indicates that the beginning and ending angles should be connected to the center */
6645
6646
/**
6647
 * @brief Draws a closed polygon
6648
 *
6649
 * @param  im The image.
6650
 * @param  p  The vertices as array of <gdPoint>s.
6651
 * @param  n  The number of vertices.
6652
 * @param  c  The color.
6653
 *
6654
 * @see gdImageOpenPolygon gdImageFilledPolygon
6655
 */
6656
BGD_DECLARE(void) gdImagePolygon(gdImagePtr im, gdPointPtr p, int n, int c);
6657
6658
/**
6659
 * @brief Draws an open polygon
6660
 *
6661
 * @param im The image.
6662
 * @param p  The vertices as array of <gdPoint>s.
6663
 * @param n  The number of vertices.
6664
 * @param c  The color.
6665
 *
6666
 * @see gdImagePolygon
6667
 */
6668
BGD_DECLARE(void) gdImageOpenPolygon(gdImagePtr im, gdPointPtr p, int n, int c);
6669
6670
6671
/**
6672
 * @brief Draws a filled polygon
6673
 *
6674
 * The polygon is filled using the even-odd fillrule what can leave unfilled
6675
 * regions inside of self-intersecting polygons. This behavior might change in
6676
 * a future version.
6677
 *
6678
 * @param  im The image.
6679
 * @param  p  The vertices as array of <gdPoint>s.
6680
 * @param  n  The number of vertices.
6681
 * @param  c  The color.
6682
 *
6683
 * @see gdImagePolygon
6684
 */
6685
BGD_DECLARE(void)
6686
gdImageFilledPolygon(gdImagePtr im, gdPointPtr p, int n, int c);
6687
6688
/**
6689
 * @brief Draws a filled arc or a filled chord
6690
 *
6691
 * @param  im    The image.
6692
 * @param  cx    The x-coordinate of the center.
6693
 * @param  cy    The y-coordinate of the center.
6694
 * @param  w     The width of the arc.
6695
 * @param  h     The height of the arc.
6696
 * @param  s     The starting angle in degrees.
6697
 * @param  e     The ending angle in degrees.
6698
 * @param  color The color of the arc. A color identifier created with one of the
6699
 *               image color allocate functions.
6700
 * @param  style The style of the arc. A bitwise OR of gdArc,
6701
 *
6702
 * @see gdImageArc
6703
 */
6704
BGD_DECLARE(void)
6705
gdImageFilledArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color, int style);
6706
6707
/**
6708
 * @brief Draws an arc or a chord
6709
 *
6710
 * @param  im    The image.
6711
 * @param  cx    The x-coordinate of the center.
6712
 * @param  cy    The y-coordinate of the center.
6713
 * @param  w     The width of the arc.
6714
 * @param  h     The height of the arc.
6715
 * @param  s     The starting angle in degrees.
6716
 * @param  e     The ending angle in degrees.
6717
 * @param  color The color of the arc. A color identifier created with one of the
6718
 *               image color allocate functions.
6719
 * @see gdImageFilledArc
6720
 */
6721
BGD_DECLARE(void)
6722
gdImageArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color);
6723
6724
/**
6725
 * @brief Draw an ellipse, stroke only.
6726
 *
6727
 * @note This function does not support @ref gdImageSetThickness. GD 3.0 supports
6728
 * actual 2D vectors operation, you may rely on it if you need better 2D drawing
6729
 * operations.
6730
 *
6731
 * @param  im   The destination image.
6732
 * @param  cx   x-coordinate of the center.
6733
 * @param  cy   y-coordinate of the center.
6734
 * @param  w    The ellipse width.
6735
 * @param  h    The ellipse height.
6736
 * @param  color The color of the ellipse. A color identifier created with one of the
6737
 * image color allocate functions.
6738
 *
6739
 * @see gdImageFilledEllipse
6740
 */
6741
BGD_DECLARE(void)
6742
gdImageEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color);
6743
6744
/**
6745
 * @brief Draw a filled ellipse.
6746
 *
6747
 * @param  im    The destination image.
6748
 * @param  cx    x-coordinate of the center.
6749
 * @param  cy    y-coordinate of the center.
6750
 * @param  w     The ellipse width.
6751
 * @param  h     The ellipse height.
6752
 * @param  color The color of the ellipse. A color identifier created with one of the
6753
 *               image color allocate functions.
6754
 */
6755
BGD_DECLARE(void)
6756
gdImageFilledEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color);
6757
6758
BGD_DECLARE(void) gdImageAABlend(gdImagePtr im);
6759
6760
BGD_DECLARE(void) gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
6761
6762
/* For backwards compatibility only. Use gdImageSetStyle()
6763
   for much more flexible line drawing. */
6764
BGD_DECLARE(void) gdImageDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
6765
6766
/**
6767
 * @brief Draws a rectangle.
6768
 *
6769
 * Corners are specified by their coordinates. The rectangle is drawn using the current line style and thickness.
6770
 *
6771
 * @param  im    The image.
6772
 * @param  x1    The x-coordinate of one of the corners.
6773
 * @param  y1    The y-coordinate of one of the corners.
6774
 * @param  x2    The x-coordinate of another corner.
6775
 * @param  y2    The y-coordinate of another corner.
6776
 * @param  color The color.
6777
 *
6778
 * @see gdImageFilledRectangle
6779
 */
6780
BGD_DECLARE(void) gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
6781
6782
/**
6783
 * @brief Draws a filled rectangle.
6784
 *
6785
 * @param  im    The image.
6786
 * @param  x1    The x-coordinate of one of the corners.
6787
 * @param  y1    The y-coordinate of one of the corners.
6788
 * @param  x2    The x-coordinate of another corner.
6789
 * @param  y2    The y-coordinate of another corner.
6790
 * @param  color The color.
6791
 */
6792
BGD_DECLARE(void) gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
6793
6794
/**
6795
 * @brief Sets the clipping rectangle
6796
 *
6797
 * The clipping rectangle restricts the drawing area for following drawing
6798
 * operations.
6799
 *
6800
 * @param  im - The image.
6801
 * @param  x1 - The x-coordinate of the upper left corner.
6802
 * @param  y1 - The y-coordinate of the upper left corner.
6803
 * @param  x2 - The x-coordinate of the lower right corner.
6804
 * @param  y2 - The y-coordinate of the lower right corner.
6805
 *
6806
 * @see gdImageGetClip
6807
 */
6808
BGD_DECLARE(void) gdImageSetClip(gdImagePtr im, int x1, int y1, int x2, int y2);
6809
6810
/**
6811
 * @brief Gets the current clipping rectangle
6812
 *
6813
 * @param  im   The image.
6814
 * @param  x1P  (out) The x-coordinate of the upper left corner.
6815
 * @param  y1P  (out) The y-coordinate of the upper left corner.
6816
 * @param  x2P  (out) The x-coordinate of the lower right corner.
6817
 * @param  y2P  (out) The y-coordinate of the lower right corner.
6818
 *
6819
 * @see gdImageSetClip
6820
 */
6821
BGD_DECLARE(void) gdImageGetClip(gdImagePtr im, int *x1P, int *y1P, int *x2P, int *y2P);
6822
6823
/**
6824
 * @brief Sets the brush for following drawing operations
6825
 *
6826
 * @param  im    The image.
6827
 * @param  brush The brush image.
6828
 */
6829
BGD_DECLARE(void) gdImageSetBrush(gdImagePtr im, gdImagePtr brush);
6830
6831
/**
6832
 * @brief Sets the tile for following drawing operations
6833
 *
6834
 * The tile is used for filling areas with a repeating pattern. The tile image is repeated to fill the area being drawn.
6835
 *
6836
 * @param  im   The image.
6837
 * @param  tile The tile image.
6838
 */
6839
BGD_DECLARE(void) gdImageSetTile(gdImagePtr im, gdImagePtr tile);
6840
6841
6842
/**
6843
 * @brief Set the color for subsequent anti-aliased drawing
6844
 *
6845
 * If @ref gdAntiAliased is passed as color to drawing operations that support
6846
 * anti-aliased drawing (such as @ref gdImageLine and @ref gdImagePolygon), the actual
6847
 * color to be used can be set with this function.
6848
 *
6849
 * Example: draw an anti-aliased blue line:
6850
 * @code
6851
 * gdImageSetAntiAliased(im, gdTrueColorAlpha(0, 0, gdBlueMax, gdAlphaOpaque));
6852
 * gdImageLine(im, 10,10, 20,20, gdAntiAliased);
6853
 * @endcode
6854
 *
6855
 * @param  im - The image.
6856
 * @param  c  - The color.
6857
 *
6858
 * @see gdImageSetAntiAliasedDontBlend
6859
 */
6860
BGD_DECLARE(void) gdImageSetAntiAliased(gdImagePtr im, int c);
6861
6862
/**
6863
 * Set the color and "dont_blend" color for subsequent anti-aliased drawing
6864
 *
6865
 * This extended variant of <gdImageSetAntiAliased> allows to also specify a
6866
 * (background) color that will not be blended in anti-aliased drawing
6867
 * operations.
6868
 *
6869
 * @param im         The image.
6870
 * @param c          The color.
6871
 * @param dont_blend Whether to blend.
6872
 */
6873
BGD_DECLARE(void) gdImageSetAntiAliasedDontBlend(gdImagePtr im, int c, int dont_blend);
6874
6875
/**
6876
 * @brief Sets the style for following drawing operations
6877
 *
6878
 * @param  im        The image.
6879
 * @param  style     An array of color values.
6880
 * @param  noOfPixel The number of color values.
6881
 */
6882
BGD_DECLARE(void) gdImageSetStyle(gdImagePtr im, int *style, int noOfPixels);
6883
6884
6885
/**
6886
 * Sets the thickness for following drawing operations
6887
 *
6888
 * @param  im        The image.
6889
 * @param  thickness The thickness in pixels.
6890
 */
6891
BGD_DECLARE(void) gdImageSetThickness(gdImagePtr im, int thickness);
6892
6893
6894
/** @} */
6895
6896
BGD_DECLARE(void)
6897
gdImageFillToBorder(gdImagePtr im, int x, int y, int border, int color);
6898
6899
/**
6900
 * @brief Flood fill an area of the image with a color
6901
 *
6902
 * @param im    The image.
6903
 * @param x     The x-coordinate of the starting point.
6904
 * @param y     The y-coordinate of the starting point.
6905
 * @param color The color to fill with.
6906
 */
6907
BGD_DECLARE(void) gdImageFill(gdImagePtr im, int x, int y, int color);
6908
6909
6910
/** @defgroup cloneandcopy Clone, copy and image properties
6911
 * @{ */
6912
/**
6913
 * @brief Copy an area of an image to another image
6914
 *
6915
 * @param dst  - The destination image.
6916
 * @param src  - The source image.
6917
 * @param dstX - The x-coordinate of the upper left corner to copy to.
6918
 * @param dstY - The y-coordinate of the upper left corner to copy to.
6919
 * @param srcX - The x-coordinate of the upper left corner to copy from.
6920
 * @param srcY - The y-coordinate of the upper left corner to copy from.
6921
 * @param w    - The width of the area to copy.
6922
 * @param h    - The height of the area to copy.
6923
 *
6924
 * @see gdImageCopyMerge gdImageCopyMergeGray gdImageCopyResized gdImageCopyResampled gdImageCopyRotated gdImageScale gdImageScaleWithOptions
6925
 */
6926
BGD_DECLARE(void)
6927
gdImageCopy(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h);
6928
6929
/**
6930
 * @brief Copy an area of an image to another image ignoring alpha
6931
 *
6932
 * The source area will be copied to the destination are by merging the pixels.
6933
 *
6934
 * @note This function is a substitute for real alpha channel operations, so it doesn't pay attention to the alpha channel.
6935
 *
6936
 * @param dst  The destination image.
6937
 * @param src  The source image.
6938
 * @param dstX The x-coordinate of the upper left corner to copy to.
6939
 * @param dstY The y-coordinate of the upper left corner to copy to.
6940
 * @param srcX The x-coordinate of the upper left corner to copy from.
6941
 * @param srcY The y-coordinate of the upper left corner to copy from.
6942
 * @param w    The width of the area to copy.
6943
 * @param h    The height of the area to copy.
6944
 * @param pct  The percentage in range 0..100.
6945
 *
6946
 * @see gdImageCopy  gdImageCopyMergeGray
6947
 */
6948
BGD_DECLARE(void)
6949
gdImageCopyMerge(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w,
6950
                 int h, int pct);
6951
6952
6953
/**
6954
 * @brief Copy an area of an image to another image ignoring alpha
6955
 *
6956
 * The source area will be copied to the grayscaled destination area by merging
6957
 * the pixels.
6958
 *
6959
 * @note This function is a substitute for real alpha channel operations, so it doesn't pay attention to the alpha channel.
6960
 *
6961
 * @param dst  - The destination image.
6962
 * @param src  - The source image.
6963
 * @param dstX - The x-coordinate of the upper left corner to copy to.
6964
 * @param dstY - The y-coordinate of the upper left corner to copy to.
6965
 * @param srcX - The x-coordinate of the upper left corner to copy from.
6966
 * @param srcY - The y-coordinate of the upper left corner to copy from.
6967
 * @param w    - The width of the area to copy.
6968
 * @param h    - The height of the area to copy.
6969
 * @param pct  - The percentage of the source color intensity in range 0..100.
6970
 *
6971
 * @see gdImageCopy gdImageCopyMerge
6972
 */
6973
BGD_DECLARE(void)
6974
gdImageCopyMergeGray(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w,
6975
                     int h, int pct);
6976
6977
6978
/**
6979
 * @brief Copy a resized area from an image to another image
6980
 *
6981
 * If the source and destination area differ in size, the area will be resized
6982
 * using nearest-neighbor interpolation.
6983
 *
6984
 * @param dst  The destination image.
6985
 * @param src  The source image.
6986
 * @param dstX The x-coordinate of the upper left corner to copy to.
6987
 * @param dstY The y-coordinate of the upper left corner to copy to.
6988
 * @param srcX The x-coordinate of the upper left corner to copy from.
6989
 * @param srcY The y-coordinate of the upper left corner to copy from.
6990
 * @param dstW The width of the area to copy to.
6991
 * @param dstH The height of the area to copy to.
6992
 * @param srcW The width of the area to copy from.
6993
 * @param srcH The height of the area to copy from.
6994
 *
6995
 * @see gdImageCopyResampled gdImageScale
6996
 */
6997
BGD_DECLARE(void)
6998
gdImageCopyResized(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW,
6999
                   int dstH, int srcW, int srcH);
7000
7001
/**
7002
 * @brief Copy a resampled area from an image to another image
7003
 *
7004
 * If the source and destination area differ in size, the area will be resized
7005
 * using bilinear interpolation for truecolor images, and nearest-neighbor
7006
 * interpolation for palette images.
7007
 *
7008
 * @param dst  The destination image.
7009
 * @param src  The source image.
7010
 * @param dstX The x-coordinate of the upper left corner to copy to.
7011
 * @param dstY The y-coordinate of the upper left corner to copy to.
7012
 * @param srcX The x-coordinate of the upper left corner to copy from.
7013
 * @param srcY The y-coordinate of the upper left corner to copy from.
7014
 * @param dstW The width of the area to copy to.
7015
 * @param dstH The height of the area to copy to.
7016
 * @param srcW The width of the area to copy from.
7017
 * @param srcH The height of the area to copy from.
7018
 *
7019
 * @see gdImageCopyResized gdImageScale
7020
 */
7021
BGD_DECLARE(void)
7022
gdImageCopyResampled(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY,
7023
                     int dstW, int dstH, int srcW, int srcH);
7024
7025
/**
7026
 * @brief Copy a rotated area from an image to another image
7027
 *
7028
 * The area is counter-clockwise rotated using nearest-neighbor interpolation.
7029
 *
7030
 * @param dst The destination image.
7031
 * @param src The source image.
7032
 * @param  dstX  The x-coordinate of the center of the area to copy to.
7033
 * @param  dstY  The y-coordinate of the center of the area to copy to.
7034
 * @param  srcX  The x-coordinate of the upper left corner to copy from.
7035
 * @param  srcY  The y-coordinate of the upper left corner to copy from.
7036
 * @param  srcW  The width of the area to copy from.
7037
 * @param  srcH  The height of the area to copy from.
7038
 * @param  angle The angle in degrees.
7039
 *
7040
 * @see gdImageRotateInterpolated
7041
 */
7042
BGD_DECLARE(void)
7043
gdImageCopyRotated(gdImagePtr dst, gdImagePtr src, double dstX, double dstY, int srcX, int srcY,
7044
                   int srcWidth, int srcHeight, int angle);
7045
7046
/**
7047
 * @brief Clones an image
7048
 *
7049
 * Creates an exact duplicate of the given image.
7050
 *
7051
 * @param src The source image.
7052
 *
7053
 * @returns The cloned image on success, NULL on failure.
7054
 */
7055
BGD_DECLARE(gdImagePtr) gdImageClone(gdImagePtr src);
7056
7057
/**
7058
 * @brief Sets whether an image is interlaced
7059
 *
7060
 * This is relevant only when saving the image in a format that supports
7061
 * interlacing.
7062
 *
7063
 * @param im            The image.
7064
 * @param interlaceArg  Whether the image is interlaced.
7065
 *
7066
 * @see gdImageGetInterlaced
7067
 */
7068
BGD_DECLARE(void) gdImageInterlace(gdImagePtr im, int interlaceArg);
7069
7070
/** @} */
7071
7072
/**
7073
 * @brief Sets the effect for subsequent drawing operations
7074
 *
7075
 * @note The effect is used for truecolor images only.
7076
 *
7077
 * @note in gd 2.4+, a configure flag is available to use the accurate and correct blending algorithm for truecolor images. This is the default behavior.
7078
 *       The old, faster, but less accurate algorithm can be used by configuring gd with --disable-accurate-blending.
7079
 * @param im The image.
7080
 * @param alphaBlendingArg The effect.
7081
 *
7082
 * Effects: @ref gdEffectOverlay, @ref gdEffectMultiply, @ref gdEffectNormal
7083
 *
7084
 *
7085
 */
7086
BGD_DECLARE(void) gdImageAlphaBlending(gdImagePtr im, int alphaBlendingArg);
7087
7088
/**
7089
 * @brief Sets the save alpha flag
7090
 *
7091
 * The save alpha flag specifies whether the alpha channel of the pixels should
7092
 * be saved. This is supported only for image formats that support full alpha
7093
 * transparency, e.g. PNG.
7094
 *
7095
 * @param im The image.
7096
 * @param saveAlphaArg The save alpha flag (1 to save alpha, 0 to not save alpha).
7097
 */
7098
BGD_DECLARE(void) gdImageSaveAlpha(gdImagePtr im, int saveAlphaArg);
7099
7100
/**
7101
 * @defgroup  Color Quantization
7102
 *
7103
 * @{ */
7104
7105
 /**
7106
 * Note that @ref GD_QUANT_JQUANT does not retain the alpha channel, and
7107
 * @ref GD_QUANT_NEUQUANT does not support dithering.
7108
 *
7109
 * @see gdImageTrueColorToPaletteSetMethod
7110
 */
7111
enum gdPaletteQuantizationMethod {
7112
    GD_QUANT_DEFAULT = 0, /**< Default quantization method */
7113
    GD_QUANT_JQUANT = 1, /**< libjpeg's old median cut */
7114
    GD_QUANT_NEUQUANT = 2, /**< NeuQuant - approximation using Kohonen neural network */
7115
    GD_QUANT_LIQ = 3 /**< libimagequant combination aiming for highest quality */
7116
};
7117
7118
7119
/**
7120
 * @brief Creates a new palette image from a truecolor image
7121
 *
7122
 * This is the same as calling @ref gdImageCreatePaletteFromTrueColor with the
7123
 * quantization method @ref GD_QUANT_NEUQUANT.
7124
 *
7125
 * @param im            - The image.
7126
 * @param max_color     - The number of desired palette entries.
7127
 * @param sample_factor - The quantization precision between 1 (highest quality) and
7128
 *                        10 (fastest).
7129
 *
7130
 * @returns A newly create palette image; NULL on failure.
7131
 */
7132
BGD_DECLARE(gdImagePtr)
7133
gdImageNeuQuant(gdImagePtr im, const int max_color, int sample_factor);
7134
7135
7136
/**  @brief Selects quantization method used for subsequent @ref gdImageTrueColorToPalette calls.
7137
 *
7138
 *   @details See @ref gdPaletteQuantizationMethod enum (e.g. @ref GD_QUANT_NEUQUANT,
7139
 *   @ref GD_QUANT_LIQ). Speed is from 1 (highest quality) to 10 (fastest). Speed 0
7140
 *   selects method-specific default (recommended).
7141
 *
7142
 *   @param im The image to set the quantization method for.
7143
 *   @param method The quantization method to use.
7144
 *   @param speed The speed/quality tradeoff for the selected method.
7145
 *
7146
 *   @returns FALSE if the given method is invalid or not available.
7147
 */
7148
BGD_DECLARE(int)
7149
gdImageTrueColorToPaletteSetMethod(gdImagePtr im, int method, int speed);
7150
7151
/**
7152
 * @brief Sets the quality range for subsequent @ref gdImageTrueColorToPalette calls.
7153
 * @details  Chooses quality range that subsequent call to @ref gdImageTrueColorToPalette will
7154
 * aim for. Min and max quality is in range 1-100 (1 = ugly, 100 = perfect). Max
7155
 * must be higher than min. If palette cannot represent image with at least
7156
 * min_quality, then image will remain true-color. If palette can represent image
7157
 * with quality better than max_quality, then lower number of colors will be
7158
 * used. This function has effect only when @ref GD_QUANT_LIQ method has been selected
7159
 * and the source image is true-color.
7160
 *
7161
 *   @param im           The image.
7162
 *   @param min_quality  The minimum quality in range 1-100 (1 = ugly, 100 = perfect).
7163
 *                       If the palette cannot represent the image with at least
7164
 *                       min_quality, then no conversion is done.
7165
 *   @param max_quality  The maximum quality in range 1-100 (1 = ugly, 100 = perfect),
7166
 *                       which must be higher than the min_quality. If the palette can
7167
 *                       represent the image with a quality better than max_quality,
7168
 *                       then fewer colors than requested will be used.
7169
 */
7170
BGD_DECLARE(void)
7171
gdImageTrueColorToPaletteSetQuality(gdImagePtr im, int min_quality, int max_quality);
7172
7173
7174
/**
7175
 * @brief Converts a truecolor image to a palette-based image.
7176
 * @details This function converts a truecolor image to a palette-based image
7177
 * using a high-quality two-pass quantization routine
7178
 * which attempts to preserve alpha channel information
7179
 * as well as R/G/B color information when creating
7180
 * a palette. If ditherFlag is set, the image will be
7181
 * dithered to approximate colors better, at the expense
7182
 * of some obvious "speckling." colorsWanted can be
7183
 * anything up to 256. If the original source image
7184
 * includes photographic information or anything that
7185
 * came out of a JPEG, 256 is strongly recommended.
7186
 *
7187
 * Better yet, don't use these function -- write real
7188
 * truecolor PNGs and JPEGs. The disk space gain of
7189
 * conversion to palette is not great (for small images
7190
 * it can be negative) and the quality loss is ugly.
7191
 *
7192
 * DIFFERENCES: @ref gdImageCreatePaletteFromTrueColor creates and
7193
 * returns a new image. @ref gdImageTrueColorToPalette modifies
7194
 * an existing image, and the truecolor pixels are discarded.
7195
 *
7196
 * @param im           The image.
7197
 * @param dither       Whether dithering should be applied.
7198
 * @param colorsWanted The number of desired palette entries.
7199
 *
7200
 * @returns a newly created palette image on success, NULL on failure.
7201
 */
7202
BGD_DECLARE(gdImagePtr)
7203
gdImageCreatePaletteFromTrueColor(gdImagePtr im, int ditherFlag, int colorsWanted);
7204
7205
7206
/**
7207
 * @brief Converts a truecolor image to a palette image
7208
 *
7209
 * @param im           The image.
7210
 * @param dither       Whether dithering should be applied.
7211
 * @param colorsWanted The number of desired palette entries.
7212
 *
7213
 * @return Non-zero if the conversion succeeded, zero otherwise.
7214
 *
7215
 * @see gdImageCreatePaletteFromTrueColor gdImageTrueColorToPaletteSetMethod  gdImagePaletteToTrueColor
7216
 */
7217
BGD_DECLARE(int)
7218
gdImageTrueColorToPalette(gdImagePtr im, int ditherFlag, int colorsWanted);
7219
7220
/** @brief Converts a palette-based image to a truecolor image
7221
 *
7222
 * @details This function converts a palette-based image to a truecolor image. The
7223
 * palette is discarded, and the image is converted to truecolor. The alpha channel
7224
 * information is preserved.
7225
 * @param src The source image.
7226
 * @return Non-zero if the conversion succeeded, zero otherwise.
7227
 *
7228
 */
7229
BGD_DECLARE(int) gdImagePaletteToTrueColor(gdImagePtr src);
7230
7231
/** @} */
7232
7233
/**
7234
 * @defgroup  ImageFilters Image Filters and convolutions
7235
 * @{ */
7236
/**
7237
 * @brief @ref gdImagePixelate options
7238
 *
7239
 * Negate the imag src, white becomes black,
7240
 * The red, green, and blue intensities of an image are negated.
7241
 * White becomes black, yellow becomes blue, etc.
7242
 */
7243
enum gdPixelateMode {
7244
    GD_PIXELATE_UPPERLEFT, /**< Use the upper-left pixel of each block */
7245
    GD_PIXELATE_AVERAGE /**< Use the average color of each block */
7246
};
7247
7248
/**
7249
 * @brief Pixelates an image
7250
 *
7251
 * Pixelates an image by dividing it into blocks of the specified size and replacing each block with a single color.
7252
 * The color can be determined by either the upper-left pixel of the block or the average color of all pixels in the block, depending on the mode specified.
7253
 *
7254
 * @param im The image to pixelate.
7255
 * @param block_size The size of the blocks to use for pixelation. Must be greater than 0.
7256
 * @param mode The mode to use for determining the color of each block. @ref gdPixelateMode
7257
 *
7258
 * @return Non-zero on success, zero on failure. Failure: Returns zero if im is NULL or block_size is less than or equal to 0.
7259
 */
7260
BGD_DECLARE(int)
7261
gdImagePixelate(gdImagePtr im, int block_size, const unsigned int mode);
7262
7263
/**
7264
 * @brief Options to Scatter an image
7265
 */
7266
typedef struct {
7267
    int sub; /**< The subtraction value for scattering. */
7268
    int plus; /**< The addition value for scattering. */
7269
    unsigned int num_colors; /**< The number of colors to use for scattering. */
7270
    int *colors; /**< The array of colors to use for scattering. */
7271
    unsigned int seed; /**< The seed for the random number generator. */
7272
} gdScatter, *gdScatterPtr;
7273
7274
/**
7275
 * @brief Scatter an image
7276
 *
7277
 * Scatters the pixels of an image by randomly adjusting their color values based on the specified subtraction and addition values.
7278
 *
7279
 * @param im The image to scatter.
7280
 * @param sub The subtraction value for scattering. Must be greater than or equal to 0.
7281
 * @param plus The addition value for scattering. Must be greater than or equal to 0
7282
 *
7283
 * @return Non-zero on success, zero on failure. Failure: Returns zero if im is NULL, sub or plus is less than 0.
7284
 */
7285
BGD_DECLARE(int) gdImageScatter(gdImagePtr im, int sub, int plus);
7286
7287
/**
7288
 * @brief Scatter an image with specified colors
7289
 *
7290
 * Scatters the pixels of an image by randomly adjusting their color values based on the specified subtraction and addition values, using a specified set of colors.
7291
 *
7292
 * @param im The image to scatter.
7293
 * @param sub The subtraction value for scattering. Must be greater than or equal to 0.
7294
 * @param plus The addition value for scattering. Must be greater than or equal to 0.
7295
 * @param colors An array of colors to use for scattering. Must not be NULL.
7296
 * @param num_colors The number of colors in the colors array. Must be greater than
7297
 *
7298
 * @return Non-zero on success, zero on failure. Failure: Returns zero if im is NULL, sub or plus is less than 0, colors is NULL, or num_colors is 0.
7299
 */
7300
BGD_DECLARE(int) gdImageScatterColor(gdImagePtr im, int sub, int plus, int colors[], unsigned int num_colors);
7301
7302
/**
7303
 * @brief Scatter an image with extended options
7304
 *
7305
 * Scatters the pixels of an image using the specified scattering options.
7306
 *
7307
 * @param im The image to scatter.
7308
 * @param s A pointer to a gdScatter structure containing the scattering options. Must not be NULL.
7309
 *
7310
 * @return
7311
 */
7312
BGD_DECLARE(int) gdImageScatterEx(gdImagePtr im, gdScatterPtr s);
7313
7314
/**
7315
 * @brief Smooth an image
7316
 *
7317
 * (see smooth.jpg)
7318
 *
7319
 * @param im     The image.
7320
 * @param weight The strength of the smoothing.
7321
 *
7322
 * @return Non-zero on success, zero on failure.
7323
 *
7324
 * @see gdImageConvolution
7325
 */
7326
7327
/**
7328
 * @brief Smooth an image
7329
 *
7330
 * Smooth an image
7331
 *
7332
 * (see smooth.jpg)
7333
 *
7334
 * @param src        The source image.
7335
 * @param weight     The strength of the smoothing.
7336
 *
7337
 * @return Non-zero on success, zero on failure. Failure: Returns zero if im is NULL or weight is invalid.
7338
 *
7339
 * @see gdImageConvolution @ref gdImageGaussianBlur gdImageEmboss gdImageMeanRemoval
7340
 */
7341
 BGD_DECLARE(int) gdImageSmooth(gdImagePtr im, float weight);
7342
7343
/**
7344
 * @brief Mean removal of an image
7345
 *
7346
 * (see mean_removal.jpg)
7347
 *
7348
 * @param im The image.
7349
 *
7350
 * @return Non-zero on success, zero on failure.
7351
 *
7352
 * @see gdImageEdgeDetectQuick gdImageConvolution
7353
 */
7354
 BGD_DECLARE(int) gdImageMeanRemoval(gdImagePtr im);
7355
7356
/**
7357
 * @brief Emboss an image
7358
 *
7359
 * (see emboss.jpg)
7360
 *
7361
 * @param im The image.
7362
 *
7363
 * @return Non-zero on success, zero on failure.
7364
 *
7365
 * @see gdImageConvolution
7366
 */
7367
BGD_DECLARE(int) gdImageEmboss(gdImagePtr im);
7368
7369
/**
7370
 * @brief Gaussian blur of an image
7371
* Performs a Gaussian blur of radius 1 on the
7372
* image.  The image is modified in place.
7373
*
7374
* *NOTE:* You will almost certain want to use
7375
* @ref gdImageCopyGaussianBlurred instead, as it allows you to change
7376
* your kernel size and sigma value.  Future versions of this
7377
* function may fall back to calling it instead of
7378
* @ref gdImageConvolution, causing subtle changes so be warned.
7379
*
7380
* @param im The image to blur.
7381
*
7382
* @returns GD_TRUE (1) on success, GD_FALSE (0) on failure.
7383
*
7384
* @see @gdImageConvolution for more information on how to use convolution matrices to achieve different effects.
7385
*/
7386
BGD_DECLARE(int) gdImageGaussianBlur(gdImagePtr im);
7387
7388
/**
7389
 * @brief Edge detection of an image
7390
 *
7391
 * (see edge_detect_quick.jpg)
7392
 *
7393
 * @param src The image.
7394
 *
7395
 * @return Non-zero on success, zero on failure.
7396
 *
7397
 * @see gdImageMeanRemoval gdImageConvolution
7398
 */
7399
BGD_DECLARE(int) gdImageEdgeDetectQuick(gdImagePtr src);
7400
7401
/**
7402
 * @brief Selective blur of an image
7403
 *
7404
 * @param src The image.
7405
 *
7406
 * @return Non-zero on success, zero on failure.
7407
 */
7408
BGD_DECLARE(int) gdImageSelectiveBlur(gdImagePtr src);
7409
7410
/**
7411
 * @brief Apply a convolution matrix to an image.
7412
 *
7413
 * Depending on the matrix, a wide range of effects can be accomplished, e.g.
7414
 * blurring, sharpening, embossing, and edge detection.
7415
 *
7416
 * @param src        The image.
7417
 * @param filter     The 3x3 convolution matrix.
7418
 * @param filter_div The value to divide the convoluted channel values by.
7419
 * @param offset     The value to add to the convoluted channel values.
7420
 *
7421
 * @return Non-zero on success, zero on failure.
7422
 *
7423
 * @see gdImageEdgeDetectQuick
7424
 * @see gdImageGaussianBlur
7425
 * @see gdImageEmboss
7426
 * @see gdImageMeanRemoval
7427
 * @see gdImageSmooth
7428
 */
7429
BGD_DECLARE(int)
7430
gdImageConvolution(gdImagePtr src, float filter[3][3], float filter_div, float offset);
7431
7432
/**
7433
 * @brief Change channel values of an image
7434
 *
7435
 * @param src   The image.
7436
 * @param red   The value to add to the red channel of all pixels.
7437
 * @param green The value to add to the green channel of all pixels.
7438
 * @param blue  The value to add to the blue channel of all pixels.
7439
 * @param alpha The value to add to the alpha channel of all pixels.
7440
 *
7441
 * @return Non-zero on success, zero on failure.
7442
 *
7443
 * @see gdImageBrightness
7444
 */
7445
BGD_DECLARE(int)
7446
gdImageColor(gdImagePtr src, const int red, const int green, const int blue, const int alpha);
7447
7448
/**
7449
 * @brief Change the contrast of an image
7450
 *
7451
 * @param src      The image.
7452
 * @param contrast The contrast adjustment value. Negative values increase, positive
7453
 *                 values decrease the contrast. The larger the absolute value, the
7454
 *                 stronger the effect.
7455
 *
7456
 * @return Non-zero on success, zero on failure.
7457
 *
7458
 * @see gdImageBrightness
7459
 */
7460
BGD_DECLARE(int) gdImageContrast(gdImagePtr src, double contrast);
7461
7462
/**
7463
 * @brief Change the brightness of an image
7464
 *
7465
 * @param src        The image.
7466
 * @param brightness The value to add to the color channels of all pixels.
7467
 *
7468
 * @return Non-zero on success, zero on failure.
7469
 *
7470
 * @see gdImageContrast gdImageColor
7471
 */
7472
BGD_DECLARE(int) gdImageBrightness(gdImagePtr src, int brightness);
7473
7474
7475
/**
7476
 * @brief Convert an image to grayscale
7477
 *
7478
 * The red, green and blue components of each pixel are replaced by their
7479
 * weighted sum using the same coefficients as the REC.601 luma (Y')
7480
 * calculation. The alpha components are retained.
7481
 *
7482
 * For palette images the result may differ due to palette limitations.
7483
 *
7484
 * @param src The image.
7485
 *
7486
 * @return Non-zero on success, zero on failure.
7487
 */
7488
BGD_DECLARE(int) gdImageGrayScale(gdImagePtr src);
7489
7490
/**
7491
 * @brief Invert an image
7492
 *
7493
 * @param src The image.
7494
 *
7495
 * @return Non-zero on success, zero on failure.
7496
 */
7497
BGD_DECLARE(int) gdImageNegate(gdImagePtr src);
7498
7499
/**
7500
 * @brief Return a copy of the source image _src_ blurred according to the parameters using the Gaussian Blur algorithm.
7501
 * Return a copy of the source image _src_ blurred according to the
7502
 * parameters using the Gaussian Blur algorithm.
7503
 *
7504
 * _radius_ is a radius, not a diameter so a radius of 2 (for
7505
 * example) will blur across a region 5 pixels across (2 to the
7506
 * center, 1 for the center itself and another 2 to the other edge).
7507
 *
7508
 * _sigma_ represents the "fatness of the curve (lower == fatter).
7509
 * If _sigma_ is less than or equal to 0,
7510
 * <gdImageCopyGaussianBlurred> ignores it and instead computes an
7511
 * "optimal" value.  Be warned that future versions of this function
7512
 * may compute sigma differently.
7513
 *
7514
 * The resulting image is always truecolor.
7515
 *
7516
 * More Details:
7517
 *
7518
 * A Gaussian Blur is generated by replacing each pixel's color
7519
 * values with the average of the surrounding pixels' colors.  This
7520
 * region is a circle whose radius is given by argument _radius_.
7521
 * Thus, a larger radius will yield a blurrier image.
7522
 *
7523
 * This average is not a simple mean of the values.  Instead, values
7524
 * are weighted using the Gaussian function (roughly a bell curve
7525
 * centered around the destination pixel) giving it much more
7526
 * influence on the result than its neighbours.  Thus, a fatter curve
7527
 * will give the center pixel more weight and make the image less
7528
 * blurry; lower _sigma_ values will yield flatter curves.
7529
 *
7530
 * Currently, <gdImageCopyGaussianBlurred> computes the default sigma
7531
 * as
7532
 *
7533
 * (2/3)*radius
7534
 *
7535
 * Note, however that we reserve the right to change this if we find
7536
 * a better ratio.  If you absolutely need the current sigma value,
7537
 * you should set it yourself.
7538
 *
7539
 * @param  src  the source image
7540
 * @param  radius  the blur radius (*not* diameter--range is 2*radius + 1)
7541
 * @param  sigma   the sigma value or a value <= 0.0 to use the computed default
7542
 *
7543
 * @return The new image or NULL if an error occurred.  The result is always truecolor.
7544
 *
7545
 * Example:
7546
 * @code
7547
 *
7548
 * FILE *in;
7549
 * gdImagePtr result, src;
7550
 *
7551
 * in = fopen("foo.png", "rb");
7552
 * src = gdImageCreateFromPng(in);
7553
 *
7554
 * result = gdImageCopyGaussianBlurred(im, src->sx / 10, -1.0);
7555
 *
7556
 * @endcode
7557
 */
7558
BGD_DECLARE(gdImagePtr)
7559
gdImageCopyGaussianBlurred(gdImagePtr src, int radius, double sigma);
7560
/** @} */
7561
7562
/**
7563
 * Group: Accessor Macros
7564
 */
7565
7566
/**
7567
 * Macro: gdImageTrueColor
7568
 *
7569
 * Whether an image is a truecolor image.
7570
 *
7571
 * Parameters:
7572
 *   im - The image.
7573
 *
7574
 * Returns:
7575
 *   Non-zero if the image is a truecolor image, zero for palette images.
7576
 */
7577
#define gdImageTrueColor(im) ((im)->trueColor)
7578
7579
/**
7580
 * Macro: gdImageSX
7581
 *
7582
 * Gets the width (in pixels) of an image.
7583
 *
7584
 * Parameters:
7585
 *   im - The image.
7586
 */
7587
0
#define gdImageSX(im) ((im)->sx)
7588
7589
/**
7590
 * Macro: gdImageSY
7591
 *
7592
 * Gets the height (in pixels) of an image.
7593
 *
7594
 * Parameters:
7595
 *   im - The image.
7596
 */
7597
0
#define gdImageSY(im) ((im)->sy)
7598
7599
/**
7600
 * Macro: gdImageColorsTotal
7601
 *
7602
 * Gets the number of colors in the palette.
7603
 *
7604
 * This macro is only valid for palette images.
7605
 *
7606
 * Parameters:
7607
 *   im - The image
7608
 */
7609
0
#define gdImageColorsTotal(im) ((im)->colorsTotal)
7610
7611
/**
7612
 * Macro: gdImageRed
7613
 *
7614
 * Gets the red component value of a given color.
7615
 *
7616
 * Parameters:
7617
 *   im - The image.
7618
 *   c  - The color.
7619
 */
7620
0
#define gdImageRed(im, c) ((im)->trueColor ? gdTrueColorGetRed(c) : (im)->red[(c)])
7621
7622
/**
7623
 * Macro: gdImageGreen
7624
 *
7625
 * Gets the green component value of a given color.
7626
 *
7627
 * Parameters:
7628
 *   im - The image.
7629
 *   c  - The color.
7630
 */
7631
0
#define gdImageGreen(im, c) ((im)->trueColor ? gdTrueColorGetGreen(c) : (im)->green[(c)])
7632
7633
/**
7634
 * Macro: gdImageBlue
7635
 *
7636
 * Gets the blue component value of a given color.
7637
 *
7638
 * Parameters:
7639
 *   im - The image.
7640
 *   c  - The color.
7641
 */
7642
0
#define gdImageBlue(im, c) ((im)->trueColor ? gdTrueColorGetBlue(c) : (im)->blue[(c)])
7643
7644
/**
7645
 * Macro: gdImageAlpha
7646
 *
7647
 * Gets the alpha component value of a given color.
7648
 *
7649
 * Parameters:
7650
 *   im - The image.
7651
 *   c  - The color.
7652
 */
7653
0
#define gdImageAlpha(im, c) ((im)->trueColor ? gdTrueColorGetAlpha(c) : (im)->alpha[(c)])
7654
7655
/**
7656
 * Macro: gdImageGetTransparent
7657
 *
7658
 * Gets the transparent color of the image.
7659
 *
7660
 * Parameters:
7661
 *   im - The image.
7662
 *
7663
 * See also:
7664
 *   - <gdImageColorTransparent>
7665
 */
7666
0
#define gdImageGetTransparent(im) ((im)->transparent)
7667
7668
/**
7669
 * Macro: gdImageGetInterlaced
7670
 *
7671
 * Whether an image is interlaced.
7672
 *
7673
 * Parameters:
7674
 *   im - The image.
7675
 *
7676
 * Returns:
7677
 *   Non-zero for interlaced images, zero otherwise.
7678
 *
7679
 * See also:
7680
 *   - <gdImageInterlace>
7681
 */
7682
#define gdImageGetInterlaced(im) ((im)->interlace)
7683
7684
/**
7685
 * Macro: gdImagePalettePixel
7686
 *
7687
 * Gets the color of a pixel.
7688
 *
7689
 * Calling this macro is only valid for palette images.
7690
 * No bounds checking is done for the coordinates.
7691
 *
7692
 * Parameters:
7693
 *   im - The image.
7694
 *   x  - The x-coordinate.
7695
 *   y  - The y-coordinate.
7696
 *
7697
 * See also:
7698
 *   - <gdImageTrueColorPixel>
7699
 *   - <gdImageGetPixel>
7700
 */
7701
0
#define gdImagePalettePixel(im, x, y) (im)->pixels[(y)][(x)]
7702
7703
/**
7704
 * Macro: gdImageTrueColorPixel
7705
 *
7706
 * Gets the color of a pixel.
7707
 *
7708
 * Calling this macro is only valid for truecolor images.
7709
 * No bounds checking is done for the coordinates.
7710
 *
7711
 * Parameters:
7712
 *   im - The image.
7713
 *   x  - The x-coordinate.
7714
 *   y  - The y-coordinate.
7715
 *
7716
 * See also:
7717
 *   - <gdImagePalettePixel>
7718
 *   - <gdImageGetTrueColorPixel>
7719
 */
7720
0
#define gdImageTrueColorPixel(im, x, y) (im)->tpixels[(y)][(x)]
7721
7722
/**
7723
 * Macro: gdImageResolutionX
7724
 *
7725
 * Gets the horizontal resolution in DPI.
7726
 *
7727
 * Parameters:
7728
 *   im - The image.
7729
 *
7730
 * See also:
7731
 *   - <gdImageResolutionY>
7732
 *   - <gdImageSetResolution>
7733
 */
7734
#define gdImageResolutionX(im) (im)->res_x
7735
7736
/**
7737
 * Macro: gdImageResolutionY
7738
 *
7739
 * Gets the vertical resolution in DPI.
7740
 *
7741
 * Parameters:
7742
 *   im - The image.
7743
 *
7744
 * See also:
7745
 *   - <gdImageResolutionX>
7746
 *   - <gdImageSetResolution>
7747
 */
7748
#define gdImageResolutionY(im) (im)->res_y
7749
7750
/* I/O Support routines. */
7751
/**
7752
 * @defgroup gdIOCtx I/O Contexts
7753
 * @{
7754
 */
7755
/**
7756
 * @brief Creates a new I/O context for reading/writing to a file
7757
 *
7758
 * returns a new I/O context for reading/writing to the specified file. The caller is responsible for closing the file when done.
7759
 * @param file A pointer to a FILE object that identifies the file to be used for I/O.
7760
 * @return A pointer to a new gdIOCtx structure, or NULL on failure.
7761
 */
7762
BGD_DECLARE(gdIOCtxPtr) gdNewFileCtx(FILE *);
7763
7764
/**
7765
 * @brief Creates a new I/O context for reading/writing to a dynamic memory buffer
7766
 *
7767
 * If data is null, size is ignored and an initial data buffer is allocated automatically.
7768
 * This function assumes gd has the right to free or reallocate "data" at will!
7769
 * Also note that gd will free "data" when the IO context is freed.
7770
 * If data is not null, it must point to memory allocated with gdMalloc, or
7771
 * by a call to gdImage[something]Ptr. If not, see gdNewDynamicCtxEx for an alternative.
7772
 *
7773
 * @param size The initial size of the dynamic memory buffer.
7774
 * @param data A pointer to a memory buffer, or NULL to allocate a new buffer.
7775
 * @return A pointer to a new gdIOCtx structure, or NULL on failure.
7776
 */
7777
BGD_DECLARE(gdIOCtxPtr) gdNewDynamicCtx(int size, void *data);
7778
7779
/**
7780
 * @brief Creates a new I/O context for reading/writing to a dynamic memory buffer with control over memory management
7781
 * 2.0.21: if freeFlag is nonzero, gd will free and/or reallocate "data" as
7782
 * needed as described above. If freeFlag is zero, gd will never free
7783
 * or reallocate "data", which means that the context should only be used
7784
 * for *reading* an image from a memory buffer, or writing an image to a
7785
 * memory buffer which is already large enough. If the memory buffer is
7786
 * not large enough and an image write is attempted, the write operation
7787
 * will fail. Those wishing to write an image to a buffer in memory have
7788
 * a much simpler alternative in the gdImage[something]Ptr functions
7789
 *
7790
 * @param size The initial size of the dynamic memory buffer.
7791
 * @param data A pointer to a memory buffer, or NULL to allocate a new buffer.
7792
 * @param freeFlag A flag indicating whether gd should manage the memory of "data". If nonzero, gd will free and/or reallocate "data" as needed. If zero, gd will not free or reallocate "data".
7793
 *
7794
 * @return A pointer to a new gdIOCtx structure, or NULL on failure.
7795
 */
7796
BGD_DECLARE(gdIOCtxPtr) gdNewDynamicCtxEx(int size, void *data, int freeFlag);
7797
7798
/** @deprecated will be removed in a future version in favor of gdNewDynamicCtxEx and retated CTX APIs */
7799
BGD_DECLARE(gdIOCtxPtr) gdNewSSCtx(gdSourcePtr in, gdSinkPtr out);
7800
BGD_DECLARE(void *) gdDPExtractData(gdIOCtxPtr ctx, int *size);
7801
/** @} */
7802
7803
/**
7804
 * @addtogroup gdCodecGd2
7805
 * @{
7806
 */
7807
7808
/** @name GD2 Constants */
7809
/** @{ */
7810
7811
/** Default GD2 chunk size in pixels. */
7812
#define GD2_CHUNKSIZE 128
7813
/** Minimum accepted GD2 chunk size in pixels. */
7814
#define GD2_CHUNKSIZE_MIN 64
7815
/** Maximum accepted GD2 chunk size in pixels. */
7816
#define GD2_CHUNKSIZE_MAX 4096
7817
7818
/** Current GD2 file format version written by gd. */
7819
#define GD2_VERS 2
7820
/** GD2 file signature string. */
7821
#define GD2_ID "gd2"
7822
/** Write uncompressed GD2 chunks. */
7823
#define GD2_FMT_RAW 1
7824
/** Write zlib-compressed GD2 chunks. */
7825
#define GD2_FMT_COMPRESSED 2
7826
7827
/** @} */
7828
7829
/** @} */
7830
7831
7832
/**
7833
 * @defgroup imagecomparison Image Comparison
7834
 * @{ */
7835
0
#define GD_CMP_IMAGE 1 /**< Actual image IS different */
7836
0
#define GD_CMP_NUM_COLORS 2 /**< Number of colors in palette differ */
7837
0
#define GD_CMP_COLOR 4 /**< Image colors differ */
7838
0
#define GD_CMP_SIZE_X 8 /**< Image width differs */
7839
0
#define GD_CMP_SIZE_Y 16 /**< Image heights differ */
7840
0
#define GD_CMP_TRANSPARENT 32 /**< Transparent color differs */
7841
#define GD_CMP_BACKGROUND 64 /**< Background color differs */
7842
0
#define GD_CMP_INTERLACE 128 /**< Interlaced setting differs */
7843
0
#define GD_CMP_TRUECOLOR 256 /**< Truecolor vs palette differs */
7844
7845
/**
7846
 * @brief Compare two images
7847
 *
7848
 * compare two images and some of its attributes. The images must be of the same size, otherwise the function will return -1.
7849
 * For accurate image content comparison, use @ref gdImagePerceptualDiff instead.
7850
 *
7851
 * @param im1 An image.
7852
 * @param im2 Another image.
7853
 *
7854
 * @return A bitmask of @ref <Image Comparison> flags where each set flag signals which attributes of the images are different.
7855
 */
7856
BGD_DECLARE(int) gdImageCompare(gdImagePtr im1, gdImagePtr im2);
7857
7858
/** @brief Options for perceptual image comparison mode
7859
 */
7860
typedef enum {
7861
    GD_IMAGE_DIFF_NONE, /**< No difference */
7862
    GD_IMAGE_DIFF_OVERLAY, /**< Overlay difference */
7863
    GD_IMAGE_DIFF_MASK /**< Mask difference */
7864
} gdImageDiffMode;
7865
7866
/** @brief Options for perceptual image comparison */
7867
typedef struct {
7868
    gdImageDiffMode mode; /**< The mode of the perceptual difference. */
7869
    int highlight_color; /**< The color used to highlight differences. */
7870
} gdImagePerceptualDiffOptions;
7871
7872
/** @brief Result of perceptual image comparison */
7873
typedef struct {
7874
    unsigned int pixels_changed; /**< Number of pixels that changed. */
7875
    /* Largest normalized perceptual distance, in the range 0.0 to 1.0. */
7876
    double maximum_delta; /**< The maximum perceptual distance found. */
7877
} gdImagePerceptualDiffResult;
7878
7879
/*
7880
 * Compare two equally sized images using a perceptual YIQ distance.
7881
 *
7882
 * A NULL options pointer selects an overlay with opaque red highlights. A
7883
 * non-NULL diff_image receives a newly allocated truecolor image for overlay
7884
 * and mask modes; the caller owns it and must call @ref gdImageDestroy. Passing
7885
 * NULL for diff_image computes statistics only. The result is always reset,
7886
 * including on failure.
7887
 *
7888
 * Returns 1 on success, or 0 for invalid arguments or allocation failure.
7889
 */
7890
BGD_DECLARE(int)
7891
gdImagePerceptualDiff(gdImagePtr image1, gdImagePtr image2, double threshold,
7892
                      const gdImagePerceptualDiffOptions *options,
7893
                      gdImagePtr *diff_image,
7894
                      gdImagePerceptualDiffResult *result);
7895
/** @} */
7896
7897
7898
/**
7899
 * @defgroup TransformScaleRotate Transform, scale and rotate
7900
 *
7901
 * Image transformation APIs for interpolation, scaling, rotation and affine
7902
 * mapping.
7903
 *
7904
 * Affine matrices use a six-element double array:
7905
 *
7906
 * matrix[0] == xx
7907
 * matrix[1] == yx
7908
 * matrix[2] == xy
7909
 * matrix[3] == yy
7910
 * matrix[4] == x0
7911
 * matrix[5] == y0
7912
 *
7913
 * A point (x, y) is transformed as:
7914
 *
7915
 * x_new = xx * x + xy * y + x0
7916
 * y_new = yx * x + yy * y + y0
7917
 *
7918
 * @{
7919
 */
7920
7921
/**
7922
 * @brief Flip an image vertically
7923
 *
7924
 * The image is mirrored upside-down.
7925
 *
7926
 * @param im The image.
7927
 *
7928
 * @see gdImageFlipHorizontal, gdImageFlipBoth
7929
 */
7930
BGD_DECLARE(void) gdImageFlipHorizontal(gdImagePtr im);
7931
7932
/**
7933
 * @brief Flip an image horizontally
7934
 *
7935
 * The image is mirrored left-right.
7936
 *
7937
 * @param im The image.
7938
 * @see gdImageFlipVertical, gdImageFlipBoth
7939
 */
7940
BGD_DECLARE(void) gdImageFlipVertical(gdImagePtr im);
7941
7942
/**
7943
 * @brief Flip an image vertically and horizontally
7944
 *
7945
 * The image is mirrored upside-down and left-right.
7946
 *
7947
 * @param im The image.
7948
 * @see gdImageFlipVertical, gdImageFlipHorizontal
7949
 */
7950
BGD_DECLARE(void) gdImageFlipBoth(gdImagePtr im);
7951
7952
/**
7953
 * Group: Crop
7954
 *
7955
 * @see gdImageCropAuto gdImageCropThreshold gdCrop
7956
 **/
7957
enum gdCropMode {
7958
    GD_CROP_DEFAULT = 0, /**< Same as GD_CROP_TRANSPARENT */
7959
    GD_CROP_TRANSPARENT, /**< Crop using the transparent color */
7960
    GD_CROP_BLACK, /**< Crop black borders */
7961
    GD_CROP_WHITE, /**< Crop white borders */
7962
    GD_CROP_SIDES, /**< Crop using colors of the 4 corners */
7963
    GD_CROP_THRESHOLD /**< Crop using a threshold */
7964
};
7965
7966
/**
7967
 * @brief Crop an image to a given rectangle
7968
 *
7969
 * @param src The image.
7970
 * @param crop The cropping rectangle, @ref gdRect.
7971
 *
7972
 * @returns The newly created cropped image, or NULL on failure.
7973
 *
7974
  * @see gdImageCrop gdImageCropThreshold gdImageAutoCropWithOptions
7975
 */
7976
BGD_DECLARE(gdImagePtr) gdImageCrop(gdImagePtr src, const gdRect *crop);
7977
7978
7979
/**
7980
 * @brief Crop an image automatically
7981
 *
7982
 * This function detects the cropping area according to the given _mode_.
7983
 *
7984
 * @param im The image.
7985
 * @param mode The cropping mode, @ref gdCropMode.
7986
 *
7987
 * @returns The newly created cropped image, or NULL on failure.
7988
 *
7989
 * @see gdImageCrop gdImageCropThreshold gdImageAutoCropWithOptions
7990
 */
7991
BGD_DECLARE(gdImagePtr) gdImageCropAuto(gdImagePtr im, const unsigned int mode);
7992
7993
7994
/**
7995
 * @brief Crop an image using a given color
7996
 *
7997
 * The _threshold_ defines the tolerance to be used while comparing the image
7998
 * color and the color to crop. The method used to calculate the color
7999
 * difference is based on the color distance in the RGB(A) cube.
8000
 *
8001
 * @param im The image.
8002
 * @param color The crop color.
8003
 * @param threshold The crop threshold.
8004
 *
8005
 * @returns The newly created cropped image, or NULL on failure.
8006
 *
8007
 * @see gdImageCrop gdImageCropThreshold gdImageAutoCropWithOptions
8008
 */
8009
BGD_DECLARE(gdImagePtr)
8010
gdImageCropThreshold(gdImagePtr im, const unsigned int color, const float threshold);
8011
8012
/**
8013
 * @brief Options for automatic cropping
8014
 *
8015
 * This structure defines the options for automatic cropping.
8016
 */
8017
typedef struct {
8018
    enum gdCropMode mode; /**< The cropping mode, @see gdCropMode. */
8019
    float threshold; /**< The crop threshold. */
8020
    int color; /**< The crop color. */
8021
} gdAutoCropOptions;
8022
8023
/**
8024
 * @brief Crop an image automatically with options
8025
 *
8026
 * This function detects the cropping area according to the given options.
8027
 *
8028
 * @param src The image.
8029
 * @param options The cropping options, @ref gdAutoCropOptions.
8030
 *
8031
 * @returns The newly created cropped image, or NULL on failure.
8032
 */
8033
BGD_DECLARE(gdImagePtr)
8034
gdImageAutoCropWithOptions(gdImagePtr src, const gdAutoCropOptions *options);
8035
8036
/**
8037
 * @brief Set the interpolation method stored on an image.
8038
 *
8039
 * Scaling, rotation and affine transformation functions use this value when
8040
 * they sample pixels from the image. Newly-created images default to
8041
 * @ref GD_BILINEAR_FIXED. Passing @ref GD_DEFAULT is accepted and stores
8042
 * @ref GD_LINEAR.
8043
 *
8044
 * Some transform APIs have optimized paths for specific methods. In
8045
 * particular, @ref gdImageScale uses @ref GD_TRIANGLE when downscaling or doing a
8046
 * mixed-axis scale with the fixed compatibility methods.
8047
 *
8048
 * Parameters:
8049
 *   im - The image.
8050
 *   id - The interpolation method.
8051
 *
8052
 * Returns:
8053
 *   Non-zero on success, zero on failure.
8054
 *
8055
 * See also:
8056
 *   - @see gdInterpolationMethod
8057
 *   - @see gdImageGetInterpolationMethod
8058
 */
8059
BGD_DECLARE(int)
8060
gdImageSetInterpolationMethod(gdImagePtr im, gdInterpolationMethod id);
8061
8062
/**
8063
 * @brief Return the interpolation method currently stored on an image.
8064
 *
8065
 * Parameters:
8066
 *   im - The image.
8067
 *
8068
 * Returns:
8069
 *   The current interpolation method.
8070
 *
8071
 * See also:
8072
 *   - @see gdInterpolationMethod
8073
 *   - @see gdImageSetInterpolationMethod
8074
 */
8075
BGD_DECLARE(gdInterpolationMethod) gdImageGetInterpolationMethod(gdImagePtr im);
8076
8077
/**
8078
 * @brief Scale an image to an exact width and height using the source image's current
8079
 * @ref gdInterpolationMethod.
8080
 *
8081
 * The returned image is newly allocated and must be destroyed with
8082
 * @ref gdImageDestroy. If the requested dimensions match the source dimensions,
8083
 * this function returns a clone of the source image. Width and height must be
8084
 * greater than zero.
8085
 *
8086
 * Notes:
8087
 *   @ref GD_WEIGHTED4 is not supported by this function. For downscales and
8088
 *   mixed-axis scales, the fixed compatibility methods are sampled with
8089
 *   @ref GD_TRIANGLE for better filtering.
8090
 *
8091
 * Parameters:
8092
 *   src        - The source image.
8093
 *   new_width  - The requested output width.
8094
 *   new_height - The requested output height.
8095
 *
8096
 * Returns:
8097
 *   The scaled image on success, or NULL on failure.
8098
 *
8099
 * See also:
8100
 *   - @see gdImageSetInterpolationMethod
8101
 *   - @see gdImageScaleWithOptions
8102
 *   - @see gdImageCopyResampled
8103
 *   - @see gdImageCopyResized
8104
 */
8105
BGD_DECLARE(gdImagePtr)
8106
gdImageScale(const gdImagePtr src, const unsigned int new_width, const unsigned int new_height);
8107
8108
/**
8109
 * Constants: gdScaleFit
8110
 *
8111
 * Controls how @ref gdImageScaleWithOptions maps the source aspect ratio into the
8112
 * requested output size.
8113
 *
8114
  * Defaults:
8115
 *   When @ref gdImageScaleWithOptions receives NULL options, the fit defaults to
8116
 *   @ref GD_SCALE_FIT_COVER.
8117
 */
8118
typedef enum {
8119
    GD_SCALE_FIT_COVER,   /**< Preserve aspect ratio, fill requested size, crop overflow. */
8120
    GD_SCALE_FIT_CONTAIN, /**< Preserve aspect ratio, fit inside requested size, pad the rest. */
8121
    GD_SCALE_FIT_FILL,    /**< Stretch to requested size without preserving aspect ratio. */
8122
    GD_SCALE_FIT_INSIDE,  /**< Preserve aspect ratio; output is no larger than requested size. */
8123
    GD_SCALE_FIT_OUTSIDE  /**< Preserve aspect ratio; output is no smaller than requested size. */
8124
} gdScaleFit;
8125
8126
/**
8127
 * Chooses the anchor used when @ref gdImageScaleWithOptions pads or crops an
8128
 * image. North and south refer to the top and bottom of the output; west and
8129
 * east refer to the left and right.
8130
 *
8131
 * Defaults:
8132
 *   When @ref gdImageScaleWithOptions receives NULL options, gravity defaults to
8133
 *   @ref GD_SCALE_GRAVITY_CENTER.
8134
 */
8135
typedef enum {
8136
8137
    GD_SCALE_GRAVITY_NORTHWEST, /**< Anchor to the top-left corner. */
8138
    GD_SCALE_GRAVITY_NORTH,     /**< Anchor to the top edge. */
8139
    GD_SCALE_GRAVITY_NORTHEAST, /**< Anchor to the top-right corner. */
8140
    GD_SCALE_GRAVITY_WEST,      /**< Anchor to the left edge. */
8141
    GD_SCALE_GRAVITY_CENTER,    /**< Anchor to the center. */
8142
    GD_SCALE_GRAVITY_EAST,      /**< Anchor to the right edge. */
8143
    GD_SCALE_GRAVITY_SOUTHWEST, /**< Anchor to the bottom-left corner. */
8144
    GD_SCALE_GRAVITY_SOUTH,     /**< Anchor to the bottom edge. */
8145
    GD_SCALE_GRAVITY_SOUTHEAST  /**< Anchor to the bottom-right corner. */
8146
} gdScaleGravity;
8147
8148
/**
8149
 * Constants: gdScaleStrategy
8150
 *
8151
 * Optional crop strategy for @ref GD_SCALE_FIT_COVER in
8152
 * @ref gdImageScaleWithOptions.
8153
 *
8154
 *
8155
 * Notes:
8156
 *   Entropy and attention strategies are valid only with
8157
 *   @ref GD_SCALE_FIT_COVER. If a strategy cannot find an interesting crop,
8158
 *   @ref gdImageScaleWithOptions falls back to the normal gravity-based cover
8159
 *   crop.
8160
 *
8161
 * Defaults:
8162
 *   When @ref gdImageScaleWithOptions receives NULL options, strategy defaults to
8163
 *   @ref GD_SCALE_STRATEGY_NONE.
8164
 */
8165
typedef enum {
8166
    GD_SCALE_STRATEGY_NONE,      /**< Crop using gravity only. */
8167
    GD_SCALE_STRATEGY_ENTROPY,   /**< Prefer a high-entropy crop region. */
8168
    GD_SCALE_STRATEGY_ATTENTION  /**< Prefer a likely visual-attention crop region. */
8169
} gdScaleStrategy;
8170
8171
/**
8172
 * Struct: gdScaleOptions
8173
 *
8174
 * Options for @ref gdImageScaleWithOptions.
8175
 *
8176
 * Members:
8177
 *   fit              - Aspect-ratio behavior. See @ref gdScaleFit.
8178
 *   gravity          - Crop or padding anchor. See @ref gdScaleGravity.
8179
 *   strategy         - Optional cover-crop strategy. See @ref gdScaleStrategy.
8180
 *   background_color - Truecolor ARGB background used for padding and for transparent palette pixels.
8181
 *   interpolation    - A gdInterpolationMethod value (see @ref gdImageSetInterpolationMethod),
8182
 *                      or @ref GD_SCALE_INTERPOLATION_AUTO.
8183
 *
8184
 * Defaults:
8185
 *   If the options pointer passed to @ref gdImageScaleWithOptions is NULL, gd uses
8186
 *   @ref GD_SCALE_FIT_COVER, @ref GD_SCALE_GRAVITY_CENTER,
8187
 *   @ref GD_SCALE_STRATEGY_NONE, background color 0x7f000000 and
8188
 *   @ref GD_SCALE_INTERPOLATION_AUTO.
8189
 *
8190
 * Notes:
8191
 *   For palette sources, gd prepares a truecolor working copy. Transparent
8192
 *   pixels are replaced with background_color before scaling; if
8193
 *   background_color is a valid palette index, that palette entry is converted
8194
 *   to truecolor.
8195
 */
8196
typedef struct {
8197
    gdScaleFit fit;            /**< Aspect-ratio behavior. */
8198
    gdScaleGravity gravity;    /**< Crop or padding anchor. */
8199
    gdScaleStrategy strategy;  /**< Optional cover-crop strategy. */
8200
    int background_color;      /**< Truecolor ARGB background used for padding and palette transparency. */
8201
    int interpolation;         /**< Interpolation method, or @ref GD_SCALE_INTERPOLATION_AUTO. */
8202
} gdScaleOptions;
8203
8204
/**
8205
 * @brief Scale an image using aspect-ratio, gravity, crop-strategy and interpolation options.
8206
 *
8207
 * Scale an image using aspect-ratio, gravity, crop-strategy and interpolation
8208
 * options.
8209
 *
8210
 * This is the higher-level scaling API. It can stretch, contain, cover, pad or
8211
 * return an inside/outside size while preserving the source aspect ratio. The
8212
 * returned image is newly allocated and must be destroyed with  @ref gdImageDestroy.
8213
 *
8214
 * @param src        - The source image.
8215
 * @param new_width  - The requested width.
8216
 * @param new_height - The requested height.
8217
 * @param options    - Scaling options, or NULL for the default options.
8218
 *
8219
 * @returns The scaled image on success, or NULL on failure. Caller owns the returned image and must call @ref gdImageDestroy when done.
8220
 *          Returns NULL if src is NULL, either dimension is zero, interpolation is
8221
 *          invalid, or an entropy/attention strategy is used with a fit other than @ref GD_SCALE_FIT_COVER.
8222
 *
8223
 * @see gdScaleOptions gdImageScale gdImageInterestingCropRegion
8224
 */
8225
BGD_DECLARE(gdImagePtr)
8226
gdImageScaleWithOptions(const gdImagePtr src, const unsigned int new_width,
8227
                        const unsigned int new_height, const gdScaleOptions *options);
8228
8229
/**
8230
 * @brief  Methods used to find an interesting crop region.
8231
 *  @see gdImageInterestingCropRegion gdImageScaleWithOptions
8232
 */
8233
typedef enum {
8234
    GD_INTERESTING_ENTROPY,    /**< Prefer regions with higher image entropy. */
8235
    GD_INTERESTING_ATTENTION   /**< Prefer regions with likely visual attention. */
8236
} gdInterestingMethod;
8237
8238
/**
8239
 * @brief Find a source crop region with the requested aspect ratio using an
8240
 * interesting-region method.
8241
 *
8242
 * @param src           - The source image.
8243
 * @param target_width  - The target width used to compute the crop aspect ratio.
8244
 * @param target_height - The target height used to compute the crop aspect ratio.
8245
 * @param method        - The interesting-region method.
8246
 * @param crop          - Receives the crop rectangle on success.
8247
 *
8248
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8249
 *
8250
 * @see gdInterestingMethod gdImageEntropyCropRegion gdImageScaleWithOptions
8251
 */
8252
BGD_DECLARE(int)
8253
gdImageInterestingCropRegion(const gdImagePtr src, unsigned int target_width,
8254
                             unsigned int target_height, gdInterestingMethod method,
8255
                             gdRectPtr crop);
8256
8257
/**
8258
 * @brief Find a high-entropy source crop region with the requested aspect ratio.
8259
 *
8260
 * @param src           - The source image.
8261
 * @param target_width  - The target width used to compute the crop aspect ratio.
8262
 * @param target_height - The target height used to compute the crop aspect ratio.
8263
 * @param crop          - Receives the crop rectangle on success.
8264
 *
8265
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8266
 *
8267
 * @see gdImageInterestingCropRegion
8268
 */
8269
BGD_DECLARE(int)
8270
gdImageEntropyCropRegion(const gdImagePtr src, unsigned int target_width,
8271
                         unsigned int target_height, gdRectPtr crop);
8272
8273
/**
8274
 * @brief Rotate an image by an arbitrary angle using the source image's current @ref gdInterpolationMethod.
8275
 *
8276
 * The returned image is newly allocated and must be destroyed with
8277
 * @ref gdImageDestroy. Palette sources are converted to truecolor internally for
8278
 * arbitrary-angle rotation. Angles that are multiples of 90 degrees use the
8279
 * optimized rotate paths.
8280
 *
8281
 * @param src     - The source image.
8282
 * @param angle   - Rotation angle in degrees.
8283
 * @param bgcolor - Background color for uncovered pixels. For palette sources, this may be a palette index.
8284
 *
8285
 * @returns The rotated image on success, or NULL on failure.
8286
 *
8287
 * @note bgcolor must be non-negative. The source interpolation method must be a valid concrete method; @ref GD_DEFAULT is not accepted by this function.
8288
 *
8289
 * @see gdImageSetInterpolationMethod gdImageRotate90 gdImageRotate180 gdImageRotate270
8290
  */
8291
BGD_DECLARE(gdImagePtr)
8292
gdImageRotateInterpolated(const gdImagePtr src, const float angle, int bgcolor);
8293
8294
/**
8295
 * @brief Standard affine matrix operations.
8296
 */
8297
typedef enum {
8298
    GD_AFFINE_TRANSLATE = 0,   /**< Translation matrix. */
8299
    GD_AFFINE_SCALE,           /**< Scale matrix. */
8300
    GD_AFFINE_ROTATE,          /**< Rotation matrix. */
8301
    GD_AFFINE_SHEAR_HORIZONTAL,/**< Horizontal shear matrix. */
8302
    GD_AFFINE_SHEAR_VERTICAL   /**< Vertical shear matrix. */
8303
} gdAffineStandardMatrix;
8304
8305
/**
8306
 * @brief Apply an affine matrix to a floating-point point.
8307
 *
8308
 * @param dst    - Receives the transformed point.
8309
 * @param src    - Source point.
8310
 * @param affine - Matrix in gd's six-value affine form.
8311
 *
8312
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8313
 */
8314
BGD_DECLARE(int)
8315
gdAffineApplyToPointF(gdPointFPtr dst, const gdPointFPtr src, const double affine[6]);
8316
8317
/**
8318
 * @brief Invert an affine matrix.
8319
 *
8320
 * @param dst - Receives the inverse matrix.
8321
 * @param src - Source matrix.
8322
 *
8323
 * @returns GD_TRUE on success, or GD_FALSE if the matrix cannot be inverted.
8324
 */
8325
BGD_DECLARE(int) gdAffineInvert(double dst[6], const double src[6]);
8326
8327
/**
8328
 * @brief Build a horizontal and/or vertical flip from an affine matrix.
8329
 *
8330
 * @param dst_affine - Receives the flipped matrix.
8331
 * @param src_affine - Source matrix.
8332
 * @param flip_h     - Non-zero to flip horizontally.
8333
 * @param flip_v     - Non-zero to flip vertically.
8334
 *
8335
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8336
 */
8337
BGD_DECLARE(int)
8338
gdAffineFlip(double dst_affine[6], const double src_affine[6], const int flip_h, const int flip_v);
8339
8340
/**
8341
 * @brief Concatenate two affine matrices.
8342
 *
8343
 * The result is equivalent to applying m1 and then m2. The destination may be
8344
 * the same array as either input.
8345
 *
8346
 * @param dst - Receives the concatenated matrix.
8347
 * @param m1  - First matrix.
8348
 * @param m2  - Second matrix.
8349
 *
8350
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8351
 */
8352
BGD_DECLARE(int)
8353
gdAffineConcat(double dst[6], const double m1[6], const double m2[6]);
8354
8355
/**
8356
 * @brief Store an identity affine matrix.
8357
 *
8358
 * @param dst - Receives the identity matrix.
8359
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8360
 */
8361
BGD_DECLARE(int) gdAffineIdentity(double dst[6]);
8362
8363
/**
8364
 * @brief Store a scale affine matrix.
8365
 *
8366
 * @param dst     - Receives the scale matrix.
8367
 * @param scale_x - Horizontal scale factor.
8368
 * @param scale_y - Vertical scale factor.
8369
 *
8370
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8371
 */
8372
BGD_DECLARE(int)
8373
gdAffineScale(double dst[6], const double scale_x, const double scale_y);
8374
8375
/**
8376
 * @brief Store a rotation affine matrix.
8377
 *
8378
 * In gd's image coordinate system, increasing y moves downward; positive
8379
 * angles rotate counterclockwise in that system.
8380
 *
8381
 * @param dst   - Receives the rotation matrix.
8382
 * @param angle - Rotation angle in degrees.
8383
 *
8384
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8385
 */
8386
BGD_DECLARE(int) gdAffineRotate(double dst[6], const double angle);
8387
8388
/**
8389
 * @brief Store a horizontal shear affine matrix.
8390
 *
8391
 * @param dst   - Receives the shear matrix.
8392
 * @param angle - Shear angle in degrees.
8393
 *
8394
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8395
 */
8396
BGD_DECLARE(int) gdAffineShearHorizontal(double dst[6], const double angle);
8397
8398
/**
8399
 * @brief Store a vertical shear affine matrix.
8400
 *
8401
 * @param dst   - Receives the shear matrix.
8402
 * @param angle - Shear angle in degrees.
8403
 *
8404
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8405
 */
8406
BGD_DECLARE(int) gdAffineShearVertical(double dst[6], const double angle);
8407
8408
/**
8409
 * @brief Store a translation affine matrix.
8410
 *
8411
 * @param dst      - Receives the translation matrix.
8412
 * @param offset_x - Horizontal offset.
8413
 * @param offset_y - Vertical offset.
8414
 *
8415
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8416
 */
8417
BGD_DECLARE(int)
8418
gdAffineTranslate(double dst[6], const double offset_x, const double offset_y);
8419
8420
/**
8421
 * @brief Return the linear expansion factor of an affine matrix.
8422
 *
8423
 * This is the square root of the factor by which the matrix changes area.
8424
 *
8425
 * @param src - Source matrix.
8426
 *
8427
 * @returns The expansion factor.
8428
 */
8429
BGD_DECLARE(double) gdAffineExpansion(const double src[6]);
8430
8431
/**
8432
 * @brief Test whether an affine matrix preserves axis-aligned rectangles.
8433
 *
8434
 * @param src - Source matrix.
8435
 *
8436
 * @returns GD_TRUE if the matrix is rectilinear, otherwise GD_FALSE.
8437
 */
8438
BGD_DECLARE(int) gdAffineRectilinear(const double src[6]);
8439
8440
/**
8441
 * @brief Compare two affine matrices.
8442
 *
8443
 * @param matrix1 - First matrix.
8444
 * @param matrix2 - Second matrix.
8445
 *
8446
 * @returns GD_TRUE if the matrices are equal within gd's affine tolerance, otherwise
8447
 *          GD_FALSE.
8448
 */
8449
BGD_DECLARE(int)
8450
gdAffineEqual(const double matrix1[6], const double matrix2[6]);
8451
8452
/**
8453
 * @brief Apply an affine transform to a source region and create an image containing
8454
 * the complete transformed result.
8455
 *
8456
 * The new image is truecolor with alpha saving enabled. Areas not covered by
8457
 * the transformed source are transparent. The source image's current
8458
 * @ref gdInterpolationMethod controls sampling. Palette sources may be converted
8459
 * to truecolor internally.
8460
 *
8461
 * @param dst      - Receives the newly-created destination image.
8462
 * @param src      - Source image.
8463
 * @param src_area - Source rectangle, or NULL to transform the full image.
8464
 * @param affine   - Matrix in gd's six-value affine form.
8465
 *
8466
 * @returns GD_TRUE on success, or GD_FALSE on failure. On failure, *dst is NULL.
8467
 *
8468
 * @see gdTransformAffineCopy gdTransformAffineBoundingBox gdImageSetInterpolationMethod
8469
 */
8470
BGD_DECLARE(int)
8471
gdTransformAffineGetImage(gdImagePtr *dst, const gdImagePtr src, gdRectPtr src_area,
8472
                          const double affine[6]);
8473
8474
/**
8475
 * @brief Apply an affine transform to a source region and copy the transformed pixels
8476
 * into an existing destination image.
8477
 *
8478
 * The source image's current @ref gdInterpolationMethod controls sampling.
8479
 * Transparent samples are skipped. Destination bounds and alpha-blending
8480
 * settings are honored.
8481
 *
8482
 * @param dst        - Destination image.
8483
 * @param dst_x      - Destination x offset.
8484
 * @param dst_y      - Destination y offset.
8485
 * @param src        - Source image.
8486
 * @param src_region - Source rectangle to transform.
8487
 * @param affine     - Matrix in gd's six-value affine form.
8488
 *
8489
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8490
 *
8491
 * @see gdTransformAffineGetImage  gdTransformAffineBoundingBox gdImageSetInterpolationMethod
8492
 */
8493
BGD_DECLARE(int)
8494
gdTransformAffineCopy(gdImagePtr dst, int dst_x, int dst_y, const gdImagePtr src,
8495
                      gdRectPtr src_region, const double affine[6]);
8496
/*
8497
gdTransformAffineCopy(gdImagePtr dst, int x0, int y0, int x1, int y1,
8498
                          const gdImagePtr src, int src_width, int src_height,
8499
                          const double affine[6]);
8500
*/
8501
/**
8502
 * @brief Compute the bounding box of a source rectangle after applying an affine
8503
 * transform.
8504
 *
8505
 * @param src    - Source rectangle.
8506
 * @param affine - Matrix in gd's six-value affine form.
8507
 * @param bbox   - Receives the transformed bounding box.
8508
 *
8509
 * @returns GD_TRUE on success, or GD_FALSE on failure.
8510
 */
8511
BGD_DECLARE(int)
8512
gdTransformAffineBoundingBox(gdRectPtr src, const double affine[6], gdRectPtr bbox);
8513
8514
/** @} */
8515
8516
/* resolution affects ttf font rendering, particularly hinting */
8517
902
#define GD_RESOLUTION 96 /* pixels per inch */
8518
8519
/* Version information functions */
8520
BGD_DECLARE(int) gdMajorVersion(void);
8521
BGD_DECLARE(int) gdMinorVersion(void);
8522
BGD_DECLARE(int) gdReleaseVersion(void);
8523
BGD_DECLARE(const char *) gdExtraVersion(void);
8524
BGD_DECLARE(const char *) gdVersionString(void);
8525
8526
/* newfangled special effects */
8527
#include "gdfx.h"
8528
8529
#ifdef __cplusplus
8530
}
8531
#endif
8532
8533
#endif /* GD_H */