← Back to list

How I Built a Next-Gen Image Codec in Rust from Scratch (And Beat JPEG)

Merging Turing morphogenesis, Fibonacci sequences, and fearless concurrency to reinvent image compression.

Symphoenix · 2026-03-25 15:18 · 2 claps · 8.8 min read
#codec #mathematics #image-compression #true-story
Open on Medium ↗
Wiki topics: 📐 · Mathematics 📰 · Journalism & News

How I Built a Next-Gen Image Codec in Rust from Scratch (And Beat JPEG)

Merging Turing morphogenesis, Fibonacci sequences, and fearless concurrency to reinvent image compression.

The 30-Year Wall

JPEG turned 32 this year. Let that sink in.

In 1992, while the world was dialing into bulletin boards at 14.4 kbps, a committee of engineers settled on an 8x8 block transform, a fixed quantization matrix, and Huffman coding. That architecture became the most deployed compression standard in human history. Trillions of images. Every camera, every browser, every phone. JPEG won so thoroughly that most people forgot it was even a choice.

But if you have ever zoomed into a sunset photo and watched the sky dissolve into a staircase of color bands, you have seen the cracks. If you have ever stared at a dark portrait and noticed ghostly 8x8 blocks floating across the shadows, you know the truth: JPEG is showing its age.

Modern alternatives exist. AVIF repurposes a video codec. JPEG XL assembles a decade of committee work. WebP borrows from VP8 intra-frames. They are all impressive. They are all the product of hundreds of engineers, millions of dollars, and inherited assumptions stretching back to the same DNA as MPEG.

2 weeks ago, I asked a different question.

What if you started with nothing? No legacy code. No borrowed transforms. No inherited design decisions. Just a blank main.rs, a mathematical intuition, and a willingness to be wrong a hundred times before being right once.

The result is AUREA, a lossy image codec written from scratch in Rust. Every algorithm in its pipeline was developed through successive iteration. It borrows nothing from any existing codec. And on the Kodak 24 benchmark, the standard test set the compression community has used for decades, it beats JPEG on 22 out of 24 images.

This is the story of how it works.

The Golden Thread

Every architecture needs a spine. For JPEG, it is the Discrete Cosine Transform. For AUREA, it is a number.

φ = 1.618033988749895

The golden ratio is not decoration here. It is not a logo or a name. It is the mathematical constant that unifies the entire codec, from the first operation on a raw pixel to the last bit written to disk.

The pipeline begins with what I call the Golden Color Transform. Traditional codecs convert RGB to YCbCr using weights derived from empirical measurements of the human visual system back in the analog television era. AUREA derives its luminance weights directly from φ:

L = (R + φ·G + φ⁻¹·B) / 2φ

The resulting weights, 0.309 for red, 0.500 for green, 0.191 for blue, land remarkably close to the BT.601 standard. But they were not tuned to match it. They fell out of the math. And the inverse transform uses only φ⁻¹ and φ⁻², which means reconstruction is numerically clean, with no accumulated rounding drift.

Then comes a decision that sounds small but changes everything. The two chroma channels are not treated symmetrically. The blue chroma (C1) is subsampled at 4:2:0, half resolution in both directions. The red chroma (C2) keeps full vertical resolution at 4:2:2. This asymmetry is not arbitrary. The human eye has fewer S-cones (blue-sensitive) than L-cones (red-sensitive), and red edges against dark backgrounds are perceptually sharper than blue ones. A symmetric 4:2:0 on both channels, which is what every mainstream codec does, throws away red detail that the eye can actually see.

The golden ratio reappears in the DC prediction, where a DPCM predictor weights left, top, and diagonal neighbors by φ⁻¹, φ⁻², and φ⁻³. It reappears in the quantization dead zones. It reappears in the step modulation. Not because I forced it to, but because φ kept producing better results than the alternatives I tested.

Some choices in engineering are principled. Some are empirical. When a single constant keeps winning across unrelated subsystems, it stops being a coincidence and starts being architecture.

Teaching the Codec to See

JPEG treats every 8x8 block the same way. A photograph of a cloudless sky gets the same quantization matrix as a photograph of a forest canopy. The codec is blind. It does not know what it is compressing.

AUREA’s most unconventional idea is that the codec can learn to see the image’s structure before deciding how to compress it, and it can do this without spending a single extra bit.

Here is how.

The pipeline starts by splitting the image into 16x16 blocks and computing a DC value for each, the average intensity of the block. This is standard. But then, instead of moving straight to frequency-domain quantization, AUREA runs a Turing morphogenesis simulation on that DC grid.

Alan Turing’s 1952 paper on morphogenesis showed that two interacting chemicals, an activator and an inhibitor diffusing at different rates, spontaneously generate stable patterns: stripes, spots, the markings on a leopard’s skin. AUREA applies the same principle to the image. It computes two Gaussian blurs of the DC grid at different scales: a tight activator (σ = 1.5 blocks) and a wider inhibitor (σ = φ² × 1.5 ≈ 3.9 blocks). The difference between them produces a field that highlights structural ridges, the edges, contours, and texture boundaries of the image, while identifying smooth valleys where the eye is forgiving.

The critical insight is this: the decoder already has the DC values. It decoded them first. So it can run the exact same Turing simulation and produce the exact same field. The structural map costs zero bits to transmit.

That zero-bit field then drives a Psychovisual Pivot that flips the codec’s strategy depending on the bitrate. At low bitrate, when bits are scarce, the codec tightens quantization on the ridges (preserving edges) and loosens it in the valleys (sacrificing smooth areas the eye forgives). At high bitrate, it inverts the strategy: it protects smooth gradients against banding and lets high-frequency texture absorb more noise, because the eye masks quantization errors in complex regions.

The math is a single power function. The behavioral switch is a smoothstep on quality. But the effect is dramatic: the codec adapts to the image at two levels simultaneously, spatial structure and perceptual priority, and the adaptation is free.

Sequencing the Image

If the Turing field gives the codec eyes, the next layer gives it a vocabulary.

Traditional codecs work in the frequency domain: transform a block, quantize the coefficients, entropy-code the result. AUREA does this too, using a 16x16 Lapped Orthogonal Transform. But it adds a layer of biological metaphor that turned out to be more than a metaphor.

Each block’s local structure is classified into a codon, borrowing terminology from genetics. A codon encodes the directional energy of the block across three gradient directions, each classified by magnitude:

A (Adenine): near-zero gradient. Silence. Smooth sky, flat wall, empty background.

C (Cytosine): weak gradient. Gentle texture. Fabric, skin, sand.

G (Guanine): strong gradient. Clear directional edge. A window frame, a jawline, a horizon.

T (Thymine): very strong gradient. High-contrast boundary. Text on white, a silhouette against the sun.

An all-A codon is an intron, junk DNA, a block the codec can compress aggressively because nothing perceptually important is happening. A codon containing G or T is structural, a region where fidelity matters.

This classification drives the quantization step at the block level. Introns get 3x coarser steps: huge savings, perceptually invisible. Structural codons get the base step: every edge preserved. The DNA metaphor is not forced onto the architecture. It is the architecture. The codec literally sequences the image before compressing it, reading its structure the way a polymerase reads a strand.

Why Rust Changed the Math

I need to be honest about something. AUREA would not exist without Rust.

This is not a language-war statement. It is a practical one. The codec’s inner loop, the Lapped Orthogonal Transform across three channels with variable block sizes and overlap, is embarrassingly parallel but dangerously stateful. Every block reads from its neighbors’ overlap zones. Every channel shares the Turing field. In C, this is a minefield of data races. In C++, you reach for mutexes and pray. In Rust, you write this:

l.par_chunks_mut(chunk_size)
.zip(c1.par_chunks_mut(chunk_size))
.zip(c2.par_chunks_mut(chunk_size))
.enumerate()
.for_each(|(chunk_idx, ((l_chunk, c1_chunk), c2_chunk))| {
// Each chunk is an exclusive mutable borrow.
// The compiler proves at compile time that no two
// threads touch the same memory.
transform_block(l_chunk, c1_chunk, c2_chunk, chunk_idx);
});

The par_chunks_mut from Rayon splits the work across cores. The borrow checker guarantees at compile time that no two threads write to the same slice. No locks. No atomics. No prayer. The parallelism is proven safe before the program ever runs.

This is not a marginal advantage. It is what allowed me to iterate on the architecture a hundred times without introducing the kind of subtle, non-reproducible threading bugs that would have killed the project in its first days. When your feedback loop is “change the transform, re-encode 24 test images, compare BD-Rate curves,” you need that loop to be fast and you need it to never silently corrupt your results. Rust gave me both.

The entropy coder is a custom rANS implementation: byte-aligned, 32-bit state, 128 adaptive contexts in v12. It is the kind of code where a single off-by-one error produces output that looks compressed but decodes to garbage. Rust’s bounds checking caught three of those during development. In C, they would have been shipped.

The Numbers

Talk is cheap. Here are the measurements.

BD-Rate is the standard metric in the compression community. It answers the question: “At the same objective quality (PSNR), how many more or fewer bits does codec A need compared to codec B?” Negative means fewer bits. Negative means better.

On the Kodak 24 dataset, 24 images at 768×512 that the compression community has used as a benchmark for decades:

AUREA v12 vs. JPEG: -5.9% average BD-Rate. Wins on 22 of 24 images.

The range spans from -14.3% (kodim23, a complex outdoor scene) to +1.7% (kodim08, one of only two images where JPEG edges ahead by a hair). On 6 higher-resolution test images (2K), the advantage widens to -17.6% on average, peaking at -32.9%.

What does -32.9% look like in practice? It means AUREA delivers the same measured quality as JPEG while using a third fewer bits. Or equivalently: at the same file size, AUREA produces a visibly cleaner image.

Here is what the eye sees. Take a dark, atmospheric scene: neon lights bleeding through fog, a face half-lit in cyan and red, wet surfaces reflecting color. JPEG at 0.25 bpp produces 31.4 dB. AUREA at the same bitrate produces 36.9 dB. That is a 5.5 dB gap. In the world of lossy compression, 0.5 dB is noticeable. 5.5 dB is a generation gap.

The advantage concentrates at low bitrate, exactly where it matters most. When bits are abundant, every codec looks good. When bits are scarce, architecture shows. The Psychovisual Pivot, the Turing field, the asymmetric chroma subsampling, the Weber-Fechner perceptual transfer, they all converge on the same goal: spend the few available bits where the human eye is looking.

What This Is (and What It Is Not)

I want to be precise about the claim.

AUREA beats JPEG baseline. Consistently, measurably, across standard benchmarks. For a codec written from scratch by one person, that is a result I am proud of.

AUREA does not claim to beat JPEG XL, AVIF, or VVC intra-frame. Those codecs represent thousands of engineer-years of work, decades of committee optimization, hardware-accelerated decoder pipelines, and a level of maturity that no solo project can replicate. The point was never to compete with Big Tech on their terms.

The point was to prove that there is still room for radical, ground-up algorithmic innovation in a field that most people consider solved. That a single mathematical principle, explored with enough patience and enough willingness to throw away what does not work, can produce a competitive compression architecture without borrowing a single line from the existing canon.

AUREA is MIT-licensed, written in Rust, and available on GitHub. It encodes and decodes real images. The CLI accepts PNG, JPEG, and BMP input. A Windows shell extension provides native thumbnail previews for .aur files.

If you have ever looked at an 8x8 blocking artifact and thought there must be a better way, there is. I spent 2 weeks building it.

https://github.com/5ymph0en1x/Aurea

If you enjoyed this deep dive into the intersection of biology, mathematics, and systems programming, follow me for updates on the next chapter: extending AUREA into a video codec using tensor network compression for temporal redundancy. The golden ratio is not done yet.


메타데이터
post_id
d1b6f6a53beb
slug
how-i-built-a-next-gen-image-codec-in-rust-from-scratch-and-beat-jpeg-d1b6f6a53beb
url
https://medium.com/@5imph03n1x/how-i-built-a-next-gen-image-codec-in-rust-from-scratch-and-beat-jpeg-d1b6f6a53beb
canonical_url
https://medium.com/@5imph03n1x/how-i-built-a-next-gen-image-codec-in-rust-from-scratch-and-beat-jpeg-d1b6f6a53beb
author_url
https://medium.com/@5imph03n1x
status
ok
fetched_at
2026-06-23 03:48:11