Read and write JPEG images.
JPEG IO | Read and write JPEG images. |
Functions | |
gdImageJpeg | gdImageJpeg outputs the specified image to the specified file in JPEG format. |
gdImageJpegPtr | Identical to gdImageJpeg except that it returns a pointer to a memory area with the JPEG data. |
gdImageJpegCtx | Write the image as JPEG data via a gdIOCtx. |
gdImageCreateFromJpeg | See gdImageCreateFromJpegEx. |
gdImageCreateFromJpegEx | gdImageCreateFromJpegEx is called to load truecolor images from JPEG format files. |
gdImageCreateFromJpegPtr | |
gdImageCreateFromJpegPtrEx | |
gdImageCreateFromJpegCtx | See gdImageCreateFromJpeg. |
gdImageCreateFromJpegCtxEx | See gdImageCreateFromJpeg. |
void gdImageJpeg( gdImagePtr im, FILE * outFile, int quality )
gdImageJpeg outputs the specified image to the specified file in JPEG 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. gdImageJpeg does not close the file; your code must do so.
If quality is negative, the default IJG JPEG quality value (which should yield a good general quality / size tradeoff for most situations) is used. Otherwise, for practical purposes, quality should be a value in the range 0-95, higher quality values usually implying both higher quality and larger image sizes.
If you have set image interlacing using gdImageInterlace, this function will interpret that to mean you wish to output a progressive JPEG. Some programs (e.g., Web browsers) can display progressive JPEGs incrementally; this can be useful when browsing over a relatively slow communications link, for example. Progressive JPEGs can also be slightly smaller than sequential (non-progressive) JPEGs.
gdImageJpegCtx stores the image using a gdIOCtx struct.
gdImageJpegPtr stores the image to RAM.
im | The image to save |
outFile | The FILE pointer to write to. |
quality | Compression quality (0-95, 0 means use the default). |
Nothing.
gdImagePtr im; int black, white; FILE *out; // Create the image im = gdImageCreate(100, 100); // Allocate background white = gdImageColorAllocate(im, 255, 255, 255); // Allocate drawing color black = gdImageColorAllocate(im, 0, 0, 0); // Draw rectangle gdImageRectangle(im, 0, 0, 99, 99, black); // Open output file in binary mode out = fopen("rect.jpg", "wb"); // Write JPEG using default quality gdImageJpeg(im, out, -1); // Close file fclose(out); // Destroy image gdImageDestroy(im);
void * gdImageJpegPtr( gdImagePtr im, int * size, int quality )
Identical to gdImageJpeg except that it returns a pointer to a memory area with the JPEG data. This memory must be freed by the caller when it is no longer needed.
The caller must invoke gdFree, not free(). This is because it is not guaranteed that libgd will use the same implementation of malloc, free, etc. as your proggram.
The ‘size’ parameter receives the total size of the block of memory.
im | The image to write |
size | Output: the size of the resulting image. |
quality | Compression quality. |
A pointer to the JPEG data or NULL if an error occurred.
void gdImageJpegCtx( gdImagePtr im, gdIOCtx * outfile, int quality )
Write the image as JPEG data via a gdIOCtx. See gdImageJpeg for more details.
im | The image to write. |
outfile | The output sink. |
quality | Image quality. |
gdImagePtr gdImageCreateFromJpegEx( FILE * inFile, int ignore_warning )
gdImageCreateFromJpegEx is called to load truecolor images from JPEG format files. Invoke gdImageCreateFromJpegEx with an already opened pointer to a file containing the desired image. gdImageCreateFromJpegEx returns a gdImagePtr to the new truecolor image, or NULL if unable to load the image (most often because the file is corrupt or does not contain a JPEG image). gdImageCreateFromJpegEx 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.
The returned image is always a truecolor image.
gdImageCreateFromJpegPtrEx creates an image from JPEG data already in memory.
gdImageCreateFromJpegCtxEx reads its data via the function pointers in a gdIOCtx structure.
gdImageCreateFromJpeg, gdImageCreateFromJpegPtr and gdImageCreateFromJpegCtx are equivalent to calling their Ex-named counterparts with an ignore_warning set to 1 (i.e. TRUE).
infile | The input FILE pointer. |
ignore_warning | Flag. If true, ignores recoverable warnings. |
A pointer to the new truecolor image. This will need to be destroyed with gdImageDestroy once it is no longer needed.
On error, returns NULL.
gdImagePtr im; FILE *in; in = fopen("myjpeg.jpg", "rb"); im = gdImageCreateFromJpegEx(in, GD_TRUE); fclose(in); // ... Use the image ... gdImageDestroy(im);
gdImageJpeg outputs the specified image to the specified file in JPEG format.
void gdImageJpeg( gdImagePtr im, FILE * outFile, int quality )
Identical to gdImageJpeg except that it returns a pointer to a memory area with the JPEG data.
void * gdImageJpegPtr( gdImagePtr im, int * size, int quality )
Write the image as JPEG data via a gdIOCtx.
void gdImageJpegCtx( gdImagePtr im, gdIOCtx * outfile, int quality )
See gdImageCreateFromJpegEx.
gdImagePtr gdImageCreateFromJpeg( FILE * inFile )
gdImageCreateFromJpegEx is called to load truecolor images from JPEG format files.
gdImagePtr gdImageCreateFromJpegEx( FILE * inFile, int ignore_warning )
gdImagePtr gdImageCreateFromJpegPtr( int size, void * data )
gdImagePtr gdImageCreateFromJpegPtrEx( int size, void * data, int ignore_warning )
See gdImageCreateFromJpeg.
gdImagePtr gdImageCreateFromJpegCtx( gdIOCtx * infile )
See gdImageCreateFromJpeg.
gdImagePtr gdImageCreateFromJpegCtxEx( gdIOCtx * infile, int ignore_warning )
Sets whether an image is interlaced
void gdImageInterlace ( gdImagePtr im, int interlaceArg )
Frees memory that has been allocated by libgd functions.
void gdFree ( void * ptr )
gdImageDestroy is used to free the memory associated with an image.
void gdImageDestroy ( gdImagePtr im )