Use libjpeg to compress images

Source: Internet
Author: User

Libjpeg is a library fully written in C language. It includes the widely used JPEG decoding, JPEG encoding, and other JPEG functions. Libjpeg not only converts BMP to JPG, but also supports converting other formats into JPG (such as ppm, GIF, Targa, and RLE ).

The conversion process is as follows:

(1) Allocate and initialize a JPEG compressed object

In libjpeg, the JPEG compression object is defined as "struct jpeg_compress_struct" (this struct contains some child structures which are allocated space through "malloc, our program will not directly use these struct ).

We also need to specify the error processor. Here we only use the default error processor. The default error processor will output the warning/error information to stderr. If a fatal error occurs, exit () will be called to exit the program.

The code for binding an error processor is as follows:

       struct jpeg_compress_struct cinfo;       struct jpeg_error_mgr jerr;       ...       cinfo.err = jpeg_std_error(&jerr);       jpeg_create_compress(&cinfo);

Jpeg_create_compress will allocate a small amount of memory. If our machine has too little memory, the allocation will fail. In this case, error handler will handle the error and exit the program. This is why error handler needs initialization first.

 

(2) specify the location where the compressed data is stored

The libjpeg library transfers compressed data to the "Data destination" module. This database contains the "Data destination" module that writes data to the standard stream. If you want to write data to other places (such as memory), you can customize the "Data destination" module. The following is the sample code:

 

     FILE * outfile;       ...       if ((outfile = fopen(filename, "wb")) == NULL) {       writeLog(…);       }

Pai_stdio_dest (& Cinfo, OUTFILE );

The last function calls the standard "Data destination" module.

You can choose to set the parameter (Step 3) before the data destination, but you cannot change the data destination between pai_start_compress () and pai_finish_compress.

(3) set the parameters of the compressed object

We must set the following information for the source image in the JPEG compression object:

The image width in pixels.

The height of the image_height image, in pixels.

Input_components color channels (sampling per pixel)

Color Space of in_color_space source file

The value of input_components is 1, indicating a grayscale image. In the age of 3, the color bitmap image is displayed.

The in_color_space parameter is jcs_grayscale, indicating the grayscale image. In the era of jcs_rgb, the color bitmap image is displayed.

JPEG requires a large number of parameters to determine how to encode the parameters. In most cases, we do not need to know all the parameters. We can use pai_set_defaults () to set the parameters to a reasonable default value. Then, set the parameters we need to change.

Pai_set_defaults () depends on the Color Space of the source image. We need to set in_color_space correctly before calling pai_set_defaults. The following is a reference code for setting the parameters of a compressed object:

    cinfo.image_width = Width;         cinfo.image_height = Height;    cinfo.input_components = 3;    cinfo.in_color_space = JCS_RGB;    jpeg_set_defaults(&cinfo);

// Set the number of Compression Parameters

(4) pai_start_compress (...)

After setting all the information and parameters of the Data destination and source image, we can start a compression cycle by calling pai_start_compress. Jpeg_start_compress () initializes the internal status and allocates the workspace. And generate the data stream of the JPEG file header. Sample Code:

Performance_start_compress (& Cinfo, true );

Here, "true" indicates that we need to write a complete data stream.

(5) compressing images row by row

Now we can write image data by calling pai_write_scanlines. Image data should be scanned in ascending order.

(6__finish_compress

When all the image data is written, we call pai_finish_compress to end the compression cycle. This is to ensure that all data is written into the data. Pai_finish_compress is also responsible for releasing the memory opened by JPEG objects. The general code is as follows:

Pai_finish_compress (& Cinfo );

(7) Release JPEG compression objects

After compressing a jpeg object, call pai_destroy_compress to release the memory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.