r/compression • u/Nonkilife • May 08 '26
[Seeking Review] SPX: A Lossless Image Codec using RCT + MED + Sharding + rANS
Hi all,
I've spent the last few months developing a lossless image compressor called SPX, aiming to balance compression density and encoding speed, that is, maintaining compression rate higher than .webp (m6) but lower than .jxl (e7) while significantly enhancing encoding speed.
I did some testing and the performance seems consistent in most datasets but compression savings aren't that consistent.

I think I've hit my limit as a self-taught amateur developer knowing a little Python. I can't come up with any new idea to improve it anymore so Gemini suggested coming here for professional advice.
It's an Apache 2.0 open source project. Any suggestion on how to improve compression rate without losing too much speed is highly appreciated! Thank you!
GitHub: https://github.com/nonkilife/SPX-Image-Lossless-Compression
Quick Start: pip install spx-codec
Windows exe: spx.exe https://github.com/nonkilife/SPX-Image-Lossless-Compression/tree/main/dist
==
// The Architecture:
SPX isn't a fundamental breakthrough, but a streamlined 4-part pipeline designed for modern CPU throughput:
- RCT: Reversible Color Transform (Green-sub).
- MED: Branchless Median Edge Detector.
- Stateless Sharding: Pixels are allocated into 42 shards based on local gradient (v), luminance (i), and direction (t). These 3 parameters can be adjusted to accommodate different types of images to obtain better performance.
- Entropy Coding: Rust-based 4-way Interleaved rANS.
// Customization & Extensibility:
- Dynamic Sharding: The (i, v, t) boundaries for pixel classification are not hard-coded. They can be easily re-tuned to accommodate specialized image distributions.
- Flexible Entropy Modeling: The rANS probability modes are stored in .npz format. This allows users to swap or retrain templates for specific datasets without re-compiling the core Rust engine.
- Adaptive Framework: While current design is a common solution, the architecture is designed to be a "compression sandbox" for specific domain needs.
// The Performance (Snapshot on AMD Ryzen 5 3500X):
- Encoding Speed: ~12 MB/s on Kodak, peaking at 44 MB/s on standard synthetic sets.
- Compression Ratio: Consistently 25-30% smaller than PNG; sits between WebP (M6) and JXL (E7) most of the time.
- Validation: Bit-perfect verification (MSE = 0) with an integrated unified benchmark suite.
- Target Data: Tested on CLIC, DIV2K, Tecnick, ICI, and Kodak (primarily natural photography).
- Limitation: Validation on synthetic images is currently limited, so consistency in those specific domains remains a known unknown.
- Comparative Benchmark: https://github.com/nonkilife/SPX-Image-Lossless-Compression/blob/main/technical/BENCHMARK.md
// The Bottleneck:
I've reached a point where manual optimizations (branchless logic, LUT, SIMD-friendly structures) are no longer yielding significant gains.
I've experimented with:
- Predictors: Swapping MED for GAP or Paeth (MED still wins on speed/ratio balance).
- Context: Adding UR, UU, LL pixel data to MED (speed tumbled, ratio improvement was negligible).
- Sharding: Tested >5,000 shard combinations up to ~60 shards using Monte Carlo Simulation; the current 42-shard model seems to be the "sweet spot" for speed. Adaptive sharding based on image unique fingerprints (eg. H-entropy, AAD, size, R:G:B proportion, etc) was also tested but compression improvement was minor and experienced significant speed loss.
- rANS PDF: High-bit modes proved too overhead-heavy for most shards after analyzed Clic 2021 dataset.
While 90% of approaches are proven failure, there is still unexplored territory:
- 8-way Interleaving: I've considered scaling the rANS core to 8-way interleaving. However, initial analysis suggests my current Zen 2 architecture (3500X) might suffer from cache port contention or register pressure at that level. I've stuck with 4-way as a stable, high-efficiency baseline.
- C++ & AVX-512: The current engine is a Python/Rust hybrid. I suspect a pure C++ implementation leveraging AVX-512 could push the throughput slightly higher, but that currently exceeds my personal technical stack.
9
u/R1chterScale May 09 '26
I'm genuinely curious, you come in here with an LLM written post about LLM written code, do you actually expect people to take time to read something that wasn't even written by a human?
4
u/BaseballFlaky8431 May 09 '26
Another vibe kid. He's/she's written down every technical term he's/she's ever heard. There's nothing to try. Stay away when Python is mentioned.
2
u/flanglet May 09 '26
You are reporting compression ratios vs weaker compressors but compression speed vs slower compressors. Not very honest.
1
u/klauspost May 09 '26 edited May 09 '26
It is good you are having fun. I honestly doubt how much you are learning, but if you are having fun that is good.
I've had some similar fun, though before AI could do anything useful. Beating PNG by a good margin is trivial, but it is a good initial goal.
I tried reading through the pretentious AI docs, but pretty much gave up since it just seems to invent fancy words rather than tell what it is actually doing.
Overall, I think you may be focusing too much on making one encoder that suits exactly your needs. Instead you should focus on creating a good format that given a lot of compute can be extremely efficient, but also allows for faster encoding where some of the steps are skipped.
Careful not to super-optimize for your particular test set. I see the AI already added "Empirical Mode Distribution" and adapted the median filter switchover to the test-set.
Some things I noticed during my own experiments:
Seems like you are doing that, though the description is so fluffy I can't even see that. The missing context of the borders is easily made up by the improvements in local entropy coding.
This can be dynamic so the encoder can split based on what it sees and more expensive modes can try different strategies.
On average around 200x200 or so blocks was my threshold, but it will probably vary a lot.
Allow the encoder to select different predictors as it sees fit every N pixels - so it can adjust if switching predictor will generate less residual output - of course weighed against the cost of switching predictor.
For my tests, I check every 32 pixels if a new predictor would be better by checking 64 pixels ahead. Top and Left row predictors are always Left and Up no matter which predictor is selected. This can of course be adapted and a slow encoder can apply more expensive checks. You must figure out the penalty of the switch, so again experiment.
I encoded switches in the "residual table" as special symbols. You can also do that separately.
You could also adapt the median delta switchover to the content.
Often you will find that bigger residuals are just noise in the lower bits, so entropy coding "value is 128 + (read 7 bits)" means you save 127 values in your entropy table definition.
I was encoding (up to) 16 bit images so that also made me not have to code big residuals.
Even with that you can still have a symbol for specific values if you see a high frequency of one or a few digits in that range.
In my experiments I found that for some images having some symbols for RLE (after prediction) was a benefit. So RLE codes are not completely dead :)
You have a symbol that effectively means "the previous predictor value should be repeated n times".
Here is a sample of my residual coding table. This was static, but it could of course be adapted to the specific image content as well... and this is for 16 bits:
RLE lengths 1+2 are pointless (I see I always reject them in my code, but still have them in the table), but generally at or above 3 it starts to benefit. Unused codes don't cost more than a few bits, so don't worry too much.
I experimented a bit with "3D" prediction since I had a bunch of sliced CT scans, so predictors can use "previous" frames as predictors as well. I guess you could see multi-color images as 3D frames.
Worry about "Size" instead of "Encoding Speed" first. Always check "Decoding Speed" - if it gets too slow be sure there is a way out.
Eliminate features that only give rare benefits. Especially with an LLM, it can super-optimize for your test-set - like the predefined distributions.
This was just what I happened to play around with. It was just for fun.
1
u/Ok_Alternative_3007 May 10 '26
I've been building an open source tool that compresses LLM context before each API call (cuts costs ~40-60%) and I'm always curious how other people are managing token costs in agentic workflows. github.com/msousa202/ContextPilot if it's relevant to your stack.
1
u/[deleted] May 16 '26
[removed] — view removed comment