Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

jpeg_create_compress(&cinfo) initializes a libjpeg compression object. Set up its error manager first, then configure an output destination and image parameters before starting compression. The “(1)” in the supplied title is not a function argument: the documented call takes a pointer to a jpeg_compress_struct.

What jpeg_create_compress() does

The function prepares a struct jpeg_compress_struct for encoding. In the public header, jpeg_create_compress(cinfo) is a macro that expands to jpeg_CreateCompress(cinfo, JPEG_LIB_VERSION, sizeof(struct jpeg_compress_struct)). Passing the compile-time library version and structure size lets the implementation check that the application’s headers and linked library are compatible. See the libjpeg public header.

The argument is a pointer to the structure, commonly written &cinfo. The macro itself does not configure the image or write a JPEG file.

Initialize the compressor and error manager

Provide an error manager before creating the compressor because object creation can allocate memory. The standard setup is:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);

The libjpeg header documentation explicitly says: “NB: you must set up the error-manager BEFORE calling jpeg_create_xxx.” The default error manager reports fatal errors to standard error and exits. If the application must recover from fatal errors instead, use an appropriate custom handler; consult the libjpeg-turbo library usage guide.

Configure output and image parameters

After creation, choose where encoded bytes should go and describe the input image. For a file destination, open the file in binary write mode, then call jpeg_stdio_dest(&cinfo, outfile). Binary mode avoids text-stream transformations that could corrupt output on systems that translate bytes. A custom destination manager is an alternative when the output should be integrated with another I/O mechanism.

Set the image dimensions, component count, and input color space. For example, the documented typical RGB setup uses three components and JCS_RGB; grayscale uses one component. Set in_color_space before jpeg_set_defaults, because the defaults depend on the color space.

cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);

The values width and height above represent the application’s source image dimensions; provide values and input data consistent with the selected color space.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Complete the compression sequence

  1. Create the object: initialize the error manager, then call jpeg_create_compress(&cinfo).
  2. Set the destination: open a file in binary write mode and call jpeg_stdio_dest, or install a custom destination manager.
  3. Set input parameters: provide width, height, component count, and input color space; then call jpeg_set_defaults.
  4. Start encoding: call jpeg_start_compress(&cinfo, TRUE) after the destination and parameters are ready.
  5. Write scanlines: pass image rows to jpeg_write_scanlines in top-to-bottom order until the required scanlines have been supplied.
  6. Finish and clean up: call jpeg_finish_compress(&cinfo), then jpeg_destroy_compress(&cinfo) when the object is no longer needed.

Finishing is essential: it flushes the final buffered output. The libjpeg-turbo usage guide also notes that jpeg_finish_compress() releases working memory associated with the JPEG object; destroy the object as part of cleanup.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Diagnose a version or structure-size mismatch

If creation fails with a version or structure-size mismatch, check that the headers used to compile the application correspond to the libjpeg implementation being linked. The macro supplies the version and structure size from the build’s header, while the library checks those values against its own expectations. Because the macro can vary by installation, inspect the actual header and linked library used by the build rather than assuming every libjpeg distribution uses identical values.

Best Value

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.