PureJPEG - Pure Ruby JPEG encoder and decoder library
Convert PNG or other pixel data to JPEG. Or the other way! Implements baseline JPEG encoding (DCT, Huffman, 4:2:0 chroma subsampling) and decodes both baseline and progressive JPEGs. Exposes a variety of encoding options to adjust parts of the JPEG pipeline not normally available (I needed this to recreate the JPEG compression styles of older digital cameras - don't ask..)
It works on CRuby 3.0+, TruffleRuby 33.0, and JRuby 10.0. There's almost 100% test coverage - I need to find some "broken" JPEGs to do the rest (hit me up if you have any sources..)
[!NOTE] Rubyists might find the AI Disclosure section below of interest.
Installation
You know the drill:
gem "pure_jpeg"
gem install pure_jpeg
There are no runtime dependencies. ChunkyPNG is optional (though quite useful) if you want to use from_chunky_png.
examples/ contains some useful example scripts for basic JPEG to PNG and PNG to JPEG conversion if you want to do some quick tests without writing code.
Encoding (making JPEGs!)
From ChunkyPNG (easiest to get started)
require "chunky_png"
require "pure_jpeg"
image = ChunkyPNG::Image.from_file("photo.png")
PureJPEG.from_chunky_png(image, quality: 80).write("photo.jpg")
If you want transparent pixels composited against a solid color instead of using the PNG's hidden RGB values, pass an RGB background:
PureJPEG.from_chunky_png(image, background: [255, 255, 255], quality: 80).write("photo.jpg")
From any pixel source
PureJPEG accepts any object that responds to width, height, and [x, y] (returning an object with .r, .g, .b in 0-255):
require "pure_jpeg"
encoder = PureJPEG.encode(source, quality: 85)
encoder.write("output.jpg")
# Or get raw bytes
jpeg_data = encoder.to_bytes
From raw pixel data
source = PureJPEG::Source::RawSource.new(width, height) do |x, y|
[r, g, b] # return RGB values 0-255
end
PureJPEG.encode(source).write("output.jpg")
Grayscale
PureJPEG.encode(source, grayscale: true).write("gray.jpg")
Encoder options
PureJPEG.encode(source,
quality: 85, # 1-100, overall compression level
grayscale: false, # single-channel grayscale mode
chroma_quality: nil, # 1-100, independent Cb/Cr quality (defaults to quality)
luminance_table: nil, # custom 64-element quantization table for Y
chrominance_table: nil, # custom 64-element quantization table for Cb/Cr
quantization_modifier: nil, # proc(table, :luminance/:chrominance) -> modified table
scramble_quantization: false, # intentionally misordered quant tables (creative effect)
optimize_huffman: false # slower 2-pass encode, usually smaller files
)
See CREATIVE.md for detailed examples of the creative encoding options.
Here's a quick example of sort of the "old digital camera" effect I was looking for though:
| Normal | Scrambled quantization |
![]() |
![]() |
And here's what happens when you convert a PNG with transparency without a background: — JPEG doesn't support alpha, so the hidden RGB data behind transparent pixels bleeds through:
| PNG with transparency | Converted to JPEG |
![]() |
![]() |
Pass background: [255, 255, 255] to composite transparent pixels over white, or any other [r, g, b] color you prefer.
Note that each stage of the JPEG pipeline is a separate module, so individual components (DCT, quantization, Huffman coding) can be replaced or extended independently which is kinda my plan here as I made this to play around with effects.
Decoding (reading JPEGs!)
From file
image = PureJPEG.read("photo.jpg")
image.width # => 1024
image.height # => 768
pixel = image[100, 200]
pixel.r # => 182
pixel.g # => 140
pixel.b # => 97
From binary data
image = PureJPEG.read(jpeg_bytes)
Read dimensions and metadata only
info = PureJPEG.info("photo.jpg")
info.width # => 1024
info.height # => 768
info.component_count # => 3
info.progressive # => false
Iterating pixels
image.each_pixel do |x, y, pixel|
puts "#{x},#{y}: rgb(#{pixel.r}, #{pixel.g}, #{pixel.b})"
end
Re-encoding
A decoded PureJPEG::Image implements the same pixel source interface, so it can be passed directly back to the encoder:
image = PureJPEG.read("input.jpg")
PureJPEG.encode(image, quality: 60).write("recompressed.jpg")
Converting to PNG (with ChunkyPNG)
image = PureJPEG.read("photo.jpg")
png = ChunkyPNG::Image.new(image.width, image.height)
image.each_pixel do |x, y, pixel|
png[x, y] = ChunkyPNG::Color.rgb(pixel.r, pixel.g, pixel.b)
end
png.save("photo.png")
Format support
Encoding:
- Baseline DCT (SOF0)
- 8-bit precision
- Grayscale (1 component) and YCbCr color (3 components)
- 4:2:0 chroma subsampling (color) or no subsampling (grayscale)
- Standard Huffman tables (Annex K) by default
- Optional image-specific optimized Huffman tables
Decoding:
- Baseline DCT (SOF0) and Progressive DCT (SOF2)
- 8-bit precision
- 1-component (grayscale) and 3-component (YCbCr) images
- Any chroma subsampling factor (4:4:4, 4:2:2, 4:2:0, etc.)
- Restart markers (DRI/RST)
Not supported: arithmetic coding, 12-bit precision, EXIF/ICC profile preservation, adding a default background for transparent sources (see what happens above!). Largely because I don't need these, but they are all do-able, especially with how loosely coupled this library is internally. Raise an issue if you really care about them!
Possible future improvements: ICC profile rendering/conversion.
Performance
On a 1024x1024 image (Apple M5, 5 runs after warmup):
| Operation | CRuby 4.0.2 (YJIT) | TruffleRuby 33.0.1 |
|---|---|---|
| Encode (color, q85) | ~0.16s | ~0.08s |
| Decode (baseline) | ~0.14s | ~0.05s |
| Decode (progressive) | ~0.18s | ~0.09s |
The encoder and decoder use an integer-scaled AAN (Arai-Agui-Nakajima) DCT with fixed-point arithmetic throughout — no Float operations in the hot path. Color space conversion uses fixed-point integer math, and pixel data is stored as packed integers to avoid per-pixel object allocation. TruffleRuby's Graal JIT compiler can optimize these tight integer loops particularly well, resulting in 2-3x faster performance once warmed up.
Example scripts
The examples/ directory contains ready-to-run scripts. All accept JPEG or PNG input (PNG requires the chunky_png gem).
png_to_jpeg.rb -- Convert a PNG to JPEG.
ruby examples/png_to_jpeg.rb [--grayscale] INPUT.png OUTPUT.jpg [quality]
jpeg_to_png.rb -- Convert a JPEG to PNG.
ruby examples/jpeg_to_png.rb INPUT.jpg OUTPUT.png
kodak.rb -- Apply the "scrambled quantization" effect that recreates the gritty look of early digicams like the Casio QV-10. See CREATIVE.md for more on this.
ruby examples/kodak.rb INPUT.(jpg|png) [OUTPUT.jpg] [quality]
lumacrush.rb -- Crush luminance while preserving chrominance. Produces a soft, oil-painting quality where detail is blocky but colors remain accurate.
ruby examples/lumacrush.rb INPUT.(jpg|png) [OUTPUT.jpg] [luma_quality] [chroma_quality]
chromacrush.rb -- The opposite: sharp detail with collapsed, blocky color patches.
ruby examples/chromacrush.rb INPUT.(jpg|png) [OUTPUT.jpg] [luma_quality] [chroma_quality]
loopy.rb -- Re-encode a JPEG through multiple passes to see how artifacts accumulate.
ruby examples/loopy.rb INPUT.jpg [quality] [iterations]
Some useful rake tasks
bundle install
rake test # run the test suite
rake benchmark # basic benchmark of encoding and decoding (5 runs after warmup)
rake profile # CPU profile with StackProf (requires the stackprof gem)
Full benchmark script
benchmark/run.rb is a more thorough benchmark that exercises the encode and decode paths in several ways. It auto-enables YJIT, warms up before measuring, and reports object allocations, throughput (iterations/second via benchmark-ips), best-of-N wall-clock times, and a sustained mixed workload across encode (q85, q95 optimized, grayscale) and decode (baseline and progressive).
ruby benchmark/run.rb # standard run
ruby benchmark/run.rb --quick # single-shot wall-clock + allocations
ruby benchmark/run.rb --full # longer run, includes YJIT runtime stats
ruby benchmark/run.rb --profile # CPU profile with Vernier (writes JSON to /tmp)
ruby benchmark/run.rb --profile-alloc # retained-object profile with Vernier
AI Disclosure
Claude Code did the majority of the work. The math of JPEG encoding/decoding is beyond me, except 'getting it' at a high level. I understand it like I understand the engine in my car :-) Later update: OpenAI Codex is also reviewing and adding features now. It feels stronger in many areas.
I have read all of the code produced up to v0.2.0. The algorithms are above my paygrade, but I'm OK with what has been produced, and I manually fixed a variety of stylistic things along the way. For example, CC seems to like wrapping entire functions in if statements rather than bailing on the opposite condition. Later update: I have not read the ICC and optimized Huffman code yet, but it is heavily tested.
CC needed a lot of guidance. Its initial JPEG algorithm was somewhat naive and output odd looking JPEGs akin to those of my Casio QV-10 digital camera from the late 1990s. After some back and forth and image comparisons, we figured out it was doing the quantization entirely wrong (specifically not using the zigzag approach during quanitization but just going in raster order). I like this aesthetic, but fixed it up so that it works as a generally usable JPEG library, while adding ways to customize things so you can recreate the effect, if preferred (see CREATIVE.md for more on that).
CC is lazy. The initial implementation was VERY SLOW. It took 15 seconds to turn a 1024x1024 PNG into a JPEG, so we went down the profiling rabbit hole and found many optimizations to make it ~6x faster. CC is poor at considering the role of Ruby's GC when implementing low level algorithms and needs some prodding to make the correct optimizations. CC is also lazy to the point of recommending that you just use another language (e.g. Go or Rust) rather than do a pure Ruby version of something - despite it being possible with some extra work.
CC's testing and cleanliness leaves a bit to be desired. The CC-created tests were superficial, so I worked on getting them beefed up to tackle a variety of edge cases. They could still get better. It also didn't do RDoc comments, use Minitest, and a variety of other things I coerced it into working on. A good CLAUDE.md file could probably avoid many of these problems. I worked without one.
The overall experience was good. I enjoyed this project, but CC clearly requires an experienced developer to keep it on the rails and to not end up with a bunch of buggy half-working crap. Getting to the basic 'turn a PNG into a JPEG' took only twenty minutes, but the rest of making it actually widely useful took several hours more.
The final 10% still takes 90% of the time. As mentioned above, the first run was quick, but getting things right has taken much longer. v0.1->0.2 has taken longer than 0.1 did! But we now have progressive JPEG support, even more optimizations, better tests, etc. etc.
Credits
- Ufuk Kayserilioglu - Major performance optimizations including integer-scaled AAN DCT, fixed-point color space conversion, and YJIT-targeted improvements.
- Keith R. Bennett - Coverage testing and adding SimpleCov to the project.
License
MIT



