libjpeg library: The core technology for image compression and decompression
1. Introduction to libjpegunsetunset
libjpeg is an open-source C library for processing JPEG (Joint Photographic Experts Group) image files, primarily featuring encoding (compression) and decoding (decompression) of JPEG images. Developed by the Independent JPEG Group (IJG), it is widely used in embedded systems, image editing software, browsers, and other scenarios requiring JPEG image processing.
IJG is an informal organization responsible for writing and distributing widely used free JPEG image compression libraries. The first version was released on October 7, 1991. The current version is 9f, released on January 14, 2024. This has laid a stable and solid foundation for JPEG support in many applications.
unsetunset2. Introduction to the JPEG Formatunsetunset
JPEG is a lossy compressed image format designed to retain the highest possible visual quality at the smallest possible file size. The core of JPEG compression is based on the following steps:
- Color Space Conversion: Converts from RGB to YCbCr color space, separating luminance (Y) from chrominance (Cb, Cr) information.
- Downsampling: Reduces the resolution of chrominance components to decrease data volume.
- Discrete Cosine Transform (DCT): Transforms image blocks from the spatial domain to the frequency domain.
- Quantization: Reduces less noticeable details by applying stronger compression to high-frequency components.
- Entropy Encoding: Further compresses data using Huffman coding or arithmetic coding.
unsetunset3. Key Features of libjpegunsetunset
libjpeg provides a rich set of interfaces supporting the following features:
- JPEG Encoding and Decoding: Supports reading and writing standard JPEG files.
- Multiple Image Format Support: Supports grayscale, color, subsampling, and other formats.
- Custom Compression Quality: Allows users to adjust compression quality to balance file size and image quality.
- Extensibility: Supports inserting custom metadata and allows users to manipulate image streams.
unsetunset4. Technical Architecture of libjpegunsetunset
The core architecture of libjpeg is divided into the following parts:
- Data Input/Output Handlers:
- Provides standard input and output stream interfaces.
- Supports various data sources such as files and memory buffers.
- Compression and Decompression Modules:
- Compression module: Converts pixel data into JPEG data streams.
- Decompression module: Parses JPEG data streams and restores them to pixel data.
- Color Converters:
- Provides conversion from RGB to YCbCr.
- Supports configurations for different subsampling rates.
- DCT and Quantization Modules:
- Handles discrete cosine transform and quantization of frequency domain data.
- Uses standard or custom quantization tables.
- Entropy Encoding Module:
- Provides Huffman encoding and decoding functionality.
- Optional arithmetic coding (requires enabling).
unsetunset5. Usage of libjpegunsetunset
1. Encoding (Compression)
Below is the basic process for using libjpeg to encode an RGB image into a JPEG file:
#include <stdio.h> #include <jpeglib.h> void write_jpeg(const char *filename, unsigned char *image, int width, int height, int quality) { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; FILE *outfile = fopen(filename, "wb"); if (!outfile) { fprintf(stderr, "Error: Cannot open file %s\n", filename); return; } cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = 3; // RGB cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE); jpeg_start_compress(&cinfo, TRUE); JSAMPROW row_pointer; while (cinfo.next_scanline < cinfo.image_height) { row_pointer = &image[cinfo.next_scanline * width * 3]; jpeg_write_scanlines(&cinfo, &row_pointer, 1); } jpeg_finish_compress(&cinfo); fclose(outfile); jpeg_destroy_compress(&cinfo); }
2. Decoding (Decompression)
Decoding a JPEG file into memory:
#include <stdio.h> #include <jpeglib.h> unsigned char* read_jpeg(const char *filename, int *width, int *height) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; FILE *infile = fopen(filename, "rb"); if (!infile) { fprintf(stderr, "Error: Cannot open file %s\n", filename); return NULL; } cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, infile); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); *width = cinfo.output_width; *height = cinfo.output_height; int row_stride = cinfo.output_width * cinfo.output_components; unsigned char *image = malloc(cinfo.output_height * row_stride); JSAMPROW row_pointer[1]; while (cinfo.output_scanline < cinfo.output_height) { row_pointer[0] = &image[cinfo.output_scanline * row_stride]; jpeg_read_scanlines(&cinfo, row_pointer, 1); } jpeg_finish_decompress(&cinfo); fclose(infile); jpeg_destroy_decompress(&cinfo); return image; }
unsetunset6. Pros and Cons of libjpegunsetunset
Pros:
- Lightweight, suitable for embedded systems.
- High performance with efficient encoding and decoding.
- Open-source with good cross-platform support.
Cons:
- Does not support more modern formats like JPEG2000.
- Limited extensibility, lacking complex image processing features.
unsetunset7. Cross-Compiling libjpeg on Ubuntu 16.04unsetunset
Official Website: https://www.ijg.org/
Download jpegsrc.v9b.tar.gz: http://www.ijg.org/files/jpegsrc.v9b.tar.gz
Compilation Steps
1. Configuration
./configure RANLIB=/opt/SDK/arm-linux-gnueabihf-ranlib --prefix=/opt/libjpeg-9f --exec-prefix=/opt/libjpeg-9f --enable-shared --enable-static -host=arm-linux-gnueabihf
2. Compilation
make -j4 make install
libjpeg is a classic tool in the field of image processing. With its efficient and stable characteristics, it has become the industry standard for JPEG image compression and decompression. Despite the emergence of modern image formats (such as WebP and AVIF), libjpeg remains the preferred choice for many systems. For developers, mastering libjpeg is a fundamental skill for efficiently handling JPEG images.