JPEG from Scratch
Feb 2026vibe-coded
A from-scratch JPEG encoder and decoder in Go, built with only the standard library.
Tech Stack
GolangImage Processing
Overview
I built jpeg-go while writing a paper on image compression. I wanted to understand what JPEG actually does under the hood, so I wrote the whole encoder and decoder from scratch with no external deps. This was completely vibe-coded.
The implementation lives in a small "jpeg" package and includes the full pipeline: RGB to YCbCr conversion, chroma subsampling, DCT, quantization, zigzag + RLE, and Huffman coding.
How JPEG works (high level):
- RGB -> YCbCr: split brightness from color
- Chroma subsampling: shrink Cb/Cr to 1/4 size
- 8x8 blocks: process in small blocks
- DCT: convert pixels to frequencies
- Quantization: divide + round, lots of zeros
- Zigzag: group zeros together
- RLE: compress zero runs
- Huffman: entropy coding for final size
If you want references, see ITU-T T.81, the JFIF spec, IJG docs, and this video: https://youtu.be/Kv1Hiv3ox8I?si=k8buw5nTt6nOM1nr
Technical Details
- •Constants for markers, quant tables, and Huffman tables
- •RGB to YCbCr conversion with subsampling
- •DCT implementation
- •Quantization, zigzag ordering, and RLE
- •Huffman encoder/decoder
- •Simple encode/decode examples in main.go