projects

JPEG from Scratch

2026 ยท github

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

  • 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

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