# GD IO

Read and write GD images.

The GD image format is a proprietary image format of libgd. It has to be regarded as being obsolete, and should only be used for development and testing purposes.

#### Structure of a GD image file

- file header
- color header (either truecolor or palette)
- image data

All numbers are stored in big-endian format. Note that all GD output is done in the GD 2.x format (not to be confused with the GD2 format), but input may also be in the GD 1.x format.

#### GD 1.x file header structure

width | 1 word
height | 1 word

#### GD 1.x color header (palette only)

count | 1 byte (the number of used palette colors)
transparent | 1 word (257 signals no transparency)
palette | 256Ã3 bytes (RGB triplets)

#### GD 2.x file header structure

signature | 1 word (“\xFF\xFE” for truecolor, “\xFF\xFF” for palette)
width | 1 word
height | 1 word

#### GD 2.x truecolor image color header

truecolor | 1 byte (always “\001”)
transparent | 1 dword (ARGB color); “\377\377\377\377” means that no transparent color is set

#### GD 2.x palette image color header

truecolor | 1 byte (always “\0”)
count | 1 word (the number of used palette colors)
transparent | 1 dword (palette index); “\377\377\377\377” means that no transparent color is set
palette | 256 dwords (RGBA colors)

#### Image data

Sequential pixel data; row-major from top to bottom, left to right:

- 1 byte per pixel for palette images
- 1 dword (ARGB) per pixel for truecolor images

Summary

GD IO | Read and write GD images.
Functions |
gdImageCreateFromGd | gdImageCreateFromGd is called to load images from gd format files.
gdImageCreateFromGdPtr |
gdImageCreateFromGdCtx | Reads in a GD image via a gdIOCtx struct.
gdImageGd |
gdImageGdPtr |

### Functions

### gdImageCreateFromGd

> gdImagePtr gdImageCreateFromGd ( | FILE | * | inFile | )

gdImageCreateFromGd is called to load images from gd format files. Invoke gdImageCreateFromGd with an already opened pointer to a file containing the desired image in the gd file format, which is specific to gd and intended for very fast loading. (It is not intended for compression; for compression, use PNG or JPEG.)

gdImageCreateFromGd returns a gdImagePtr to the new image, or NULL if unable to load the image (most often because the file is corrupt or does not contain a gd format image). gdImageCreateFromGd does not close the file. You can inspect the sx and sy members of the image to determine its size. The image must eventually be destroyed using gdImageDestroy.

#### Variants

gdImageCreateFromGdPtr creates an image from GD data (i.e. the contents of a GD file) already in memory.

gdImageCreateFromGdCtx reads in an image using the functions in a gdIOCtx struct.

#### Parameters

infile | The input FILE pointer

#### Returns

A pointer to the new image or NULL if an error occurred.

#### Example

>     gdImagePtr im;
>     FILE *in;
>     in = fopen("mygd.gd", "rb");
>     im = gdImageCreateFromGd(in);
>     fclose(in);
>     // ... Use the image ...
>     gdImageDestroy(im);

### gdImageCreateFromGdPtr

> gdImagePtr gdImageCreateFromGdPtr ( | int |  | size,
> | void | * | data | )

#### Parameters

size | size of GD data in bytes.
data | GD data (i.e. contents of a GIF file).

Reads in GD data from memory. See gdImageCreateFromGd.

### gdImageCreateFromGdCtx

> gdImagePtr gdImageCreateFromGdCtx ( | gdIOCtxPtr | in | )

Reads in a GD image via a gdIOCtx struct. See gdImageCreateFromGd.

### gdImageGd

> void gdImageGd ( | gdImagePtr |  | im,
> | FILE | * | outFile | )

### gdImageGdPtr

> void * gdImageGdPtr ( | gdImagePtr |  | im,
> | int | * | size | )

> gdImagePtr gdImageCreateFromGd ( | FILE | * | inFile | )

gdImageCreateFromGd is called to load images from gd format files.

> gdImagePtr gdImageCreateFromGdPtr ( | int |  | size,
> | void | * | data | )

> gdImagePtr gdImageCreateFromGdCtx ( | gdIOCtxPtr | in | )

Reads in a GD image via a gdIOCtx struct.
gdIOCtx structures hold function pointers for doing image IO.

> void gdImageGd ( | gdImagePtr |  | im,
> | FILE | * | outFile | )

> void * gdImageGdPtr ( | gdImagePtr |  | im,
> | int | * | size | )

The data structure in which gd stores images.

> void gdImageDestroy ( | gdImagePtr | im | )

gdImageDestroy is used to free the memory associated with an image.
Close