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.
Input Image → Color Space (YCbCr) → 8×8 Block Split → DCT → Quantization → Zigzag Scan → Entropy Coding → Compressed Bitstream
- Color Space Conversion: RGB → YCbCr with chroma subsampling
- Block Decomposition: Image partitioned into 8×8 pixel blocks
- DCT (Discrete Cosine Transform): Spatial domain → frequency domain transformation
- Quantization: Frequency coefficients divided by standard JPEG quantization matrices
- Zigzag Scanning: 2D coefficient matrix linearized in zigzag order for optimal run-length encoding
- Entropy Coding: Huffman coding of DC (differential) and AC (run-length) coefficients
Compressed Bitstream → Entropy Decode → Inverse Zigzag → Dequantization → IDCT → YCbCr → RGB → Reconstructed Image
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.
- 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
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.ppmTest images (lena.ppm, lena_122x123.ppm) are included in each stage folder.
# 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- 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.
MIT License