gd_jpeg.c

Summary
gd_jpeg.c
Functions
gdImageJpeggdImageJpeg outputs the specified image to the specified file in JPEG format.
gdImageJpegPtrIdentical to gdImageJpeg except that it returns a pointer to a memory area with the JPEG data.
gdImageJpegCtxWrite the image as JPEG data via a gdIOCtx.
gdImageCreateFromJpegSee gdImageCreateFromJpegEx.
gdImageCreateFromJpegExgdImageCreateFromJpegEx is called to load truecolor images from JPEG format files.
gdImageCreateFromJpegPtr
gdImageCreateFromJpegPtrEx
gdImageCreateFromJpegCtxSee gdImageCreateFromJpeg.
gdImageCreateFromJpegCtxExSee gdImageCreateFromJpeg.

Functions

gdImageJpeg

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.

Variants

gdImageJpegCtx stores the image using a gdIOCtx struct.

gdImageJpegPtr stores the image to RAM.

Parameters

imThe image to save
outFileThe FILE pointer to write to.
qualityCompression quality (0-95, 0 means use the default).

Returns

Nothing.

Example

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);

gdImageJpegPtr

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.

Parameters

imThe image to write
sizeOutput: the size of the resulting image.
qualityCompression quality.

Returns

A pointer to the JPEG data or NULL if an error occurred.

gdImageJpegCtx

void gdImageJpegCtx(gdImagePtr im,
gdIOCtx *outfile,
int quality)

Write the image as JPEG data via a gdIOCtx.  See gdImageJpeg for more details.

Parameters

imThe image to write.
outfileThe output sink.
qualityImage quality.

gdImageCreateFromJpeg

gdImagePtr gdImageCreateFromJpeg(FILE *inFile)

See gdImageCreateFromJpegEx.

gdImageCreateFromJpegEx

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.

Variants

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).

Parameters

infileThe input FILE pointer.
ignore_warningFlag.  If true, ignores recoverable warnings.

Returns

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.

Example

gdImagePtr im;
FILE *in;
in = fopen("myjpeg.jpg", "rb");
im = gdImageCreateFromJpegEx(in, GD_TRUE);
fclose(in);
// ... Use the image ...
gdImageDestroy(im);

gdImageCreateFromJpegPtr

gdImagePtr gdImageCreateFromJpegPtr(int size,
void *data)

Parameters

sizesize of JPEG data in bytes.
datapointer to JPEG data.

See gdImageCreateFromJpegEx.

gdImageCreateFromJpegPtrEx

gdImagePtr gdImageCreateFromJpegPtrEx(int size,
void *data,
int ignore_warning)

Parameters

sizesize of JPEG data in bytes.
datapointer to JPEG data.
ignore_warningif true, ignore recoverable warnings

See gdImageCreateFromJpegEx.

gdImageCreateFromJpegCtx

gdImagePtr gdImageCreateFromJpegCtx(gdIOCtx *infile)

See gdImageCreateFromJpeg.

gdImageCreateFromJpegCtxEx

gdImagePtr gdImageCreateFromJpegCtxEx(gdIOCtx *infile,
int ignore_warning)

See gdImageCreateFromJpeg.

void gdImageJpeg(gdImagePtr im,
FILE *outFile,
int quality)
gdImageJpeg outputs the specified image to the specified file in JPEG format.
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.
void gdImageJpegCtx(gdImagePtr im,
gdIOCtx *outfile,
int quality)
Write the image as JPEG data via a gdIOCtx.
gdIOCtx structures hold function pointers for doing image IO.
gdImagePtr gdImageCreateFromJpeg(FILE *inFile)
See gdImageCreateFromJpegEx.
gdImagePtr gdImageCreateFromJpegEx(FILE *inFile,
int ignore_warning)
gdImageCreateFromJpegEx is called to load truecolor images from JPEG format files.
gdImagePtr gdImageCreateFromJpegPtr(int size,
void *data)
gdImagePtr gdImageCreateFromJpegPtrEx(int size,
void *data,
int ignore_warning)
gdImagePtr gdImageCreateFromJpegCtx(gdIOCtx *infile)
See gdImageCreateFromJpeg.
gdImagePtr gdImageCreateFromJpegCtxEx(gdIOCtx *infile,
int ignore_warning)
See gdImageCreateFromJpeg.
void gdImageInterlace (gdImagePtr im,
int interlaceArg)
void gdFree (void *ptr)
The data structure in which gd stores images.
void gdImageDestroy (gdImagePtr im)
gdImageDestroy is used to free the memory associated with an image.
Close