Read and write PNG images.
PNG IO | Read and write PNG images. |
Functions | |
gdImageCreateFromPng | gdImageCreateFromPng is called to load images from PNG format files. |
gdImageCreateFromPngPtr | See gdImageCreateFromPng. |
gdImageCreateFromPngCtx | See gdImageCreateFromPng. |
gdImagePngEx | gdImagePngEx outputs the specified image to the specified file in PNG format. |
gdImagePng | Equivalent to calling gdImagePngEx with compression of -1. |
gdImagePngPtr | Equivalent to calling gdImagePngPtrEx with compression of -1. |
gdImagePngPtrEx | Identical to gdImagePngEx except that it returns a pointer to a memory area with the PNG data. |
gdImagePngCtx | Equivalent to calling gdImagePngCtxEx with compression of -1. |
gdImagePngCtxEx | Outputs the given image as PNG data, but using a gdIOCtx instead of a file. |
gdImagePtr gdImageCreateFromPng ( FILE * inFile )
gdImageCreateFromPng is called to load images from PNG format files. Invoke gdImageCreateFromPng with an already opened pointer to a FILE containing the desired image. gdImageCreateFromPng 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 PNG image). gdImageCreateFromPng 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().
If the PNG image being loaded is a truecolor image, the resulting gdImagePtr will refer to a truecolor image. If the PNG image being loaded is a palette or grayscale image, the resulting gdImagePtr will refer to a palette image. gd retains only 8 bits of resolution for each of the red, green and blue channels, and only 7 bits of resolution for the alpha channel. The former restriction affects only a handful of very rare 48-bit color and 16-bit grayscale PNG images. The second restriction affects all semitransparent PNG images, but the difference is essentially invisible to the eye. 7 bits of alpha channel resolution is, in practice, quite a lot.
gdImageCreateFromPngPtr creates an image from PNG data (i.e. the contents of a PNG file) already in memory.
gdImageCreateFromPngCtx reads in an image using the functions in a gdIOCtx struct.
gdImageCreateFromPngSource is similar to gdImageCreateFromPngCtx but uses the old gdSource interface. It is obsolete.
infile | The input FILE pointer. |
A pointer to the new image or NULL if an error occurred.
gdImagePtr im; ... inside a function ... FILE *in; in = fopen("mypng.png", "rb"); im = gdImageCreateFromPng(in); fclose(in); // ... Use the image ... gdImageDestroy(im);
gdImagePtr gdImageCreateFromPngPtr ( int size, void * data )
See gdImageCreateFromPng.
gdImagePtr gdImageCreateFromPngCtx ( gdIOCtx * infile )
See gdImageCreateFromPng.
void gdImagePngEx ( gdImagePtr im, FILE * outFile, int level )
gdImagePngEx outputs the specified image to the specified file in PNG format. The file must be open for writing. Under MSDOS and all versions of Windows, it is important to use “wb” as opposed to simply “w” as the mode when opening the file, and under Unix there is no penalty for doing so. gdImagePngEx does not close the file; your code must do so.
In addition, gdImagePngEx allows the level of compression to be specified. A compression level of 0 means “no compression.” A compression level of 1 means “compressed, but as quickly as possible.” A compression level of 9 means “compressed as much as possible to produce the smallest possible file.” A compression level of -1 will use the default compression level at the time zlib was compiled on your system.
gdImagePng is equivalent to calling gdImagePngEx with compression of -1.
gdImagePngCtx and gdImagePngCtxEx write via a gdIOCtx instead of a file handle.
gdImagePngPtr and gdImagePngPtrEx store the image file to memory.
im | the image to write |
outFile | the output FILE* object. |
level | compression level: 0 -> none, 1-9 -> level, -1 -> default |
Nothing.
gdImagePtr im; int black, white; FILE *out; im = gdImageCreate(100, 100); // Create the image white = gdImageColorAllocate(im, 255, 255, 255); // Alloc background black = gdImageColorAllocate(im, 0, 0, 0); // Allocate drawing color gdImageRectangle(im, 0, 0, 99, 99, black); // Draw rectangle out = fopen("rect.png", "wb"); // Open output file (binary) gdImagePngEx(im, out, 9); // Write PNG, max compression fclose(out); // Close file gdImageDestroy(im); // Destroy image
void gdImagePng ( gdImagePtr im, FILE * outFile )
Equivalent to calling gdImagePngEx with compression of -1.
im | the image to save. |
outFile | the output FILE*. |
Nothing.
void * gdImagePngPtr ( gdImagePtr im, int * size )
Equivalent to calling gdImagePngPtrEx with compression of -1.
See gdImagePngEx for more information.
im | the image to save. |
size | Output: size in bytes of the result. |
A pointer to memory containing the image data or NULL on error.
void * gdImagePngPtrEx ( gdImagePtr im, int * size, int level )
Identical to gdImagePngEx except that it returns a pointer to a memory area with the PNG data. This memory must be freed by the caller when it is no longer needed. **The caller must invoke gdFree(), not free()**
The ‘size’ parameter receives the total size of the block of memory.
See gdImagePngEx for more information.
im | the image to save. |
size | Output: size in bytes of the result. |
level | compression level: 0 -> none, 1-9 -> level, -1 -> default |
A pointer to memory containing the image data or NULL on error.
void gdImagePngCtx ( gdImagePtr im, gdIOCtx * outfile )
Equivalent to calling gdImagePngCtxEx with compression of -1. See gdImagePngEx for more information.
im | the image to save. |
outfile | the gdIOCtx to write to. |
Nothing.
gdImageCreateFromPng is called to load images from PNG format files.
gdImagePtr gdImageCreateFromPng ( FILE * inFile )
See gdImageCreateFromPng.
gdImagePtr gdImageCreateFromPngPtr ( int size, void * data )
See gdImageCreateFromPng.
gdImagePtr gdImageCreateFromPngCtx ( gdIOCtx * infile )
gdImagePngEx outputs the specified image to the specified file in PNG format.
void gdImagePngEx ( gdImagePtr im, FILE * outFile, int level )
Equivalent to calling gdImagePngEx with compression of -1.
void gdImagePng ( gdImagePtr im, FILE * outFile )
Equivalent to calling gdImagePngPtrEx with compression of -1.
void * gdImagePngPtr ( gdImagePtr im, int * size )
Identical to gdImagePngEx except that it returns a pointer to a memory area with the PNG data.
void * gdImagePngPtrEx ( gdImagePtr im, int * size, int level )
Equivalent to calling gdImagePngCtxEx with compression of -1.
void gdImagePngCtx ( gdImagePtr im, gdIOCtx * outfile )
Outputs the given image as PNG data, but using a gdIOCtx instead of a file.
void gdImagePngCtxEx ( gdImagePtr im, gdIOCtx * outfile, int level )
See gdImageCreateFromPng for documentation.
gdImagePtr gdImageCreateFromPngSource ( gdSourcePtr inSource )