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 public header defines jpeg_create_compress(cinfo) as a macro that calls jpeg_CreateCompress, passing the compile-time JPEG_LIB_VERSION and sizeof(struct jpeg_compress_struct) as well as the structure pointer. Those values let the library check compatibility between the headers used to compile the application and the linked implementation. See the libjpeg-turbo public header.
Initialize the error manager before creating the compressor: creating the object can allocate memory, and libjpeg needs an error handler available if that operation fails. The header explicitly says, “NB: you must set up the error-manager BEFORE calling jpeg_create_xxx.”
Minimal initialization
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
This uses the default error manager. It reports fatal errors to standard error and exits; applications that need to recover rather than terminate should implement an appropriate custom handler. Consult the libjpeg-turbo library usage guide for error-handling guidance.
Recommended Free Tools
#1 Best Overall
Where initialization fits in JPEG encoding
- Initialize the error manager and compressor. Set
cinfo.err, then calljpeg_create_compress(&cinfo). - Choose the destination. For a standard file destination, open the file in binary write mode and pass it to
jpeg_stdio_dest(&cinfo, outfile). Binary mode avoids text-mode transformations that could alter output bytes on some systems. A custom destination manager is an option when output must go somewhere other than aFILEstream. - Set the source image parameters. Assign
image_width,image_height,input_components, andin_color_space. For example, the documented RGB setup uses three components andJCS_RGB; grayscale uses one component. Setin_color_spacebefore callingjpeg_set_defaults(&cinfo), because the defaults depend on the color space. - Start compression. Once the destination and parameters are ready, call
jpeg_start_compress(&cinfo, TRUE). - Write scanlines. Feed image data with
jpeg_write_scanlinesin top-to-bottom order, repeating until all required scanlines have been supplied. - Finish and release the object. Call
jpeg_finish_compress(&cinfo)after the scanlines; it flushes final output and releases working memory associated with the JPEG object. Then calljpeg_destroy_compress(&cinfo)when the object is no longer needed.
Diagnosing a version or structure-size mismatch
If creation reports a version or structure-size mismatch, check that the headers used to compile the application match the libjpeg implementation being linked. The macro passes the caller’s compile-time version and structure size, and the implementation checks those against its own expectations. In builds with multiple libjpeg installations, verify which header and library the build actually selects rather than assuming they come from the same installation.
Quick Recap
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.




