October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Use libjpeg’s jpeg_create_compress()

Initialize libjpeg’s compressor with an error manager in place, then configure the destination and image parameters before encoding scanlines.
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 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.

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

Where initialization fits in JPEG encoding

  1. Initialize the error manager and compressor. Set cinfo.err, then call jpeg_create_compress(&cinfo).
  2. 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 a FILE stream.
  3. Set the source image parameters. Assign image_width, image_height, input_components, and in_color_space. For example, the documented RGB setup uses three components and JCS_RGB; grayscale uses one component. Set in_color_space before calling jpeg_set_defaults(&cinfo), because the defaults depend on the color space.
  4. Start compression. Once the destination and parameters are ready, call jpeg_start_compress(&cinfo, TRUE).
  5. Write scanlines. Feed image data with jpeg_write_scanlines in top-to-bottom order, repeating until all required scanlines have been supplied.
  6. 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 call jpeg_destroy_compress(&cinfo) when the object is no longer needed.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.