Fundamentals 8 min read

Master libjpeg: Understanding JPEG Compression and Cross‑Compiling on Ubuntu 16.04

libjpeg, the open‑source C library from the Independent JPEG Group, provides comprehensive JPEG encoding and decoding capabilities, explains JPEG format fundamentals, details its modular architecture, demonstrates encoding/decoding code examples, and guides cross‑compiling on Ubuntu 16.04 with configuration and make steps.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master libjpeg: Understanding JPEG Compression and Cross‑Compiling on Ubuntu 16.04

1. libjpeg Introduction

libjpeg is an open‑source C library for handling JPEG image files, offering both encoding (compression) and decoding (decompression) functions. It was created by the Independent JPEG Group (IJG), a non‑formal organization that released the first version on October 7, 1991. The latest release, version 9f, was published on January 14, 2024.

2. JPEG Format Overview

JPEG is a lossy image format designed to achieve high visual quality at minimal file size. Its compression pipeline consists of:

Color space conversion : Transform RGB to YCbCr, separating luminance (Y) from chrominance (Cb, Cr).

Sub‑sampling : Reduce the resolution of chroma components to lower data volume.

Discrete Cosine Transform (DCT) : Convert image blocks from the spatial domain to the frequency domain.

Quantization : Apply stronger compression to high‑frequency components, discarding less noticeable details.

Entropy coding : Use Huffman or arithmetic coding to further compress the data.

3. Main Features of libjpeg

Full JPEG encode/decode support for standard files.

Handles multiple image formats, including grayscale, color, and various subsampling schemes.

Customizable compression quality to balance size and visual fidelity.

Extensible architecture allowing insertion of custom markers (metadata) and manipulation of the image stream.

4. Technical Architecture

The library is organized into several modules:

Data I/O processor

Standard input and output stream interfaces.

Supports files, memory buffers, and other data sources.

Compression/Decompression modules

Compression: Converts pixel data into a JPEG bitstream.

Decompression: Parses the JPEG bitstream and reconstructs pixel data.

Color converter

Provides RGB‑to‑YCbCr conversion.

Configurable subsampling rates.

DCT and quantization module

Performs the discrete cosine transform and frequency‑domain quantization.

Supports standard or custom quantization tables.

Entropy coding module

Implements Huffman encoding/decoding.

Optional arithmetic coding (requires enable flag).

5. Using libjpeg

5.1 Encoding (Compression)

The following C function demonstrates how to compress 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
", 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);
}

5.2 Decoding (Decompression)

The following function reads a JPEG file into memory and returns the raw pixel buffer:

#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
", 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;
}

6. Advantages and Disadvantages

Advantages

Lightweight and suitable for embedded systems.

High performance with fast encode/decode speeds.

Open‑source and well‑supported across platforms.

Disadvantages

Does not support newer formats such as JPEG2000, WebP, or AVIF.

Limited extensibility; lacks advanced image‑processing features.

7. Cross‑Compiling libjpeg on Ubuntu 16.04

Official site: https://www.ijg.org/

Download source: http://www.ijg.org/files/jpegsrc.v9b.tar.gz

7.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

7.2 Build and Install

make -j4
make install

After these steps, the cross‑compiled libjpeg libraries and headers are installed under /opt/libjpeg-9f, ready for integration into ARM‑based applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

image compressionJPEGlibjpegC librarycross-compilation
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.