Skip to content

MaxReit-dev/image-codec-utilities

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

JPEG Image Codec

A from-scratch implementation of the JPEG image compression pipeline in Java. This project implements the core algorithms behind the JPEG standard (ITU-T T.81), including the Discrete Cosine Transform (DCT), quantization, zigzag scanning, and entropy coding — demonstrating a deep understanding of lossy image compression fundamentals.

Pipeline

Input Image → Color Space (YCbCr) → 8×8 Block Split → DCT → Quantization → Zigzag Scan → Entropy Coding → Compressed Bitstream

Compression Stages

  1. Color Space Conversion: RGB → YCbCr with chroma subsampling
  2. Block Decomposition: Image partitioned into 8×8 pixel blocks
  3. DCT (Discrete Cosine Transform): Spatial domain → frequency domain transformation
  4. Quantization: Frequency coefficients divided by standard JPEG quantization matrices
  5. Zigzag Scanning: 2D coefficient matrix linearized in zigzag order for optimal run-length encoding
  6. Entropy Coding: Huffman coding of DC (differential) and AC (run-length) coefficients

Decompression (Inverse Pipeline)

Compressed Bitstream → Entropy Decode → Inverse Zigzag → Dequantization → IDCT → YCbCr → RGB → Reconstructed Image

Source Structure

The source code is organized into three progressive exercise stages, each building on the previous one:

src/
├── jpeg1/     Stage 1 — Image I/O
│   └── PPMReader.java              # Reads raw PPM image files into pixel arrays
│
├── jpeg2/     Stage 2 — Preprocessing
│   ├── ColorSpaceConverter.java     # RGB ↔ YCbCr color space conversion
│   ├── ImageScalator.java           # Image scaling / block alignment to 8×8 grid
│   └── SubSampler.java              # Chroma subsampling (4:2:0, 4:2:2, 4:4:4)
│
└── jpeg3/     Stage 3 — Core Compression
    ├── StandardDCT.java             # Forward and inverse Discrete Cosine Transform
    └── Quantizer.java               # Quantization / dequantization with standard JPEG tables

Each stage also includes a spec/ subfolder with Java interfaces that define the expected API contracts.

Technical Highlights

  • Full encode/decode pipeline: Both compression and decompression implemented end-to-end
  • Standard-compliant quantization: Uses the standard JPEG luminance and chrominance quantization tables
  • Quality control: Adjustable quantization scaling factor for quality vs. compression trade-off
  • PSNR analysis: Built-in Peak Signal-to-Noise Ratio computation for quality assessment

Build & Run

Each stage can be compiled and run independently:

# Stage 3 (full pipeline)
cd src/jpeg3
make          # or: javac -d bin/ src/dmms/jpeg/*.java src/dmms/jpeg/spec/*.java
java -cp bin/ dmms.jpeg.Quantizer lena_122x123.ppm

Test images (lena.ppm, lena_122x123.ppm) are included in each stage folder.

Tests

# Compile and run JUnit tests
javac -cp junit-platform-console-standalone.jar tests/TestCodec.java
java -cp .:junit-platform-console-standalone.jar org.junit.jupiter.api.Test TestCodec

References

  • ITU-T T.81: Information technology — Digital compression and coding of continuous-tone still images
  • Wallace, G. K. (1992). The JPEG still picture compression standard. IEEE Transactions on Consumer Electronics.

License

MIT License

About

JPEG image compression codec built from scratch: DCT transforms, quantization matrices, zigzag scanning, and Huffman entropy coding (Java)

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages