Recursive AI

oneimage-go

All oneimage Go docs

Zero third-party dependencies: oneimage-go depends only on the Go standard library and Recursive AI's own pure-Go modules (onefont and onepdf), with no cgo and no unsafe.

A pure-Go image library: decode and encode PNG, JPEG, GIF, WebP, BMP, ICO and TIFF; colour management with ICC profiles; resize, filters, drawing and compositing; SVG, rich text, Photoshop files, Windows metafiles and PDF pages rendered to pixels. Every codec and the compression underneath it is written here, and only the standard library's image, image/color, image/draw and io types are used for interop.

GO
package main
 
import (
	"log"
	"os"
 
	"recursiveai.org/go/oneimage"
	"recursiveai.org/go/oneimage/pdf"
)
 
func main() {
	photo, err := os.Open("photo.jpg")
	if err != nil {
		log.Fatal(err)
	}
	defer photo.Close()
	thumb, err := oneimage.Thumbnail(photo, 400, 400) // decoded small, fitted, upright
	if err != nil {
		log.Fatal(err)
	}
	out, err := os.Create("thumb.webp")
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()
	if err := oneimage.EncodeWebP(out, thumb, &oneimage.WebPOptions{Quality: 80}); err != nil {
		log.Fatal(err)
	}
 
	report, err := os.Open("report.pdf")
	if err != nil {
		log.Fatal(err)
	}
	defer report.Close()
	page, err := pdf.RenderPage(report, 0, pdf.WithDPI(150)) // page 1 at 150 dpi
	if err != nil {
		log.Fatal(err)
	}
	png, err := os.Create("page1.png")
	if err != nil {
		log.Fatal(err)
	}
	defer png.Close()
	if err := oneimage.EncodePNG(png, page, nil); err != nil {
		log.Fatal(err)
	}
}

That program is run by the tests (Example_readme in readme_test.go), and a test keeps it identical to this one.

go get recursiveai.org/go/oneimage

Go 1.24 or newer. It builds from the module proxy alone, with no workspace or replace directives; the only modules it requires are our own: recursiveai.org/go/onefont v1.2.0 and recursiveai.org/go/onepdf v1.0.1 (with its fonts module). Licence: MIT OR Apache-2.0, © Recursive AI, LLC.

What is in it

PackageWhat it does
oneimageThe pixel model (8-bit, 16-bit and float samples; gray, RGB, CMYK, Lab, indexed; straight or premultiplied alpha), the format registry, PNG and APNG, JPEG, GIF, WebP (lossy, lossless, animated), BMP, ICO and CUR, TIFF (classic and BigTIFF); metadata (ICC, Exif, XMP) carried through decode and encode; ICC v2 and v4 colour management; resize, crop, rotate, affine and perspective transforms, tone, filters, effects, dithering and quantizers; drawing with paths, strokes, brushes and blend modes; compositing
oneimage/layersLayered documents: pixel, group, fill and adjustment layers with masks, clipping, blend modes and layer styles, rendered tile by tile on demand
oneimage/svgSVG: shapes, paths, gradients, patterns, clips, masks, markers, filters, a CSS subset and text, rendered at any output size and for any region
oneimage/textRich text on onefont: shaping, bidi, line breaking, fallback, decorations, text on a path, colour glyphs
oneimage/psdPhotoshop PSD and PSB: merged image, layers, masks and layer styles, read and written
oneimage/metafileWindows EMF, EMF+ and WMF: a command stream and a raster renderer
oneimage/pdfPDF pages drawn as rasters on onepdf: vector art, text, images (with JPEG, JPEG 2000 and JBIG2 decoded here), shadings, patterns, transparency, forms
oneimage/webA net/http middleware that serves resized and converted images from URL commands, with signed URLs, presets, limits and a disk cache with ETag and single-flight
oneimage/stdregA blank import that lets the standard library's image.Decode read every format above

Two layers, one engine

The happy path is a few calls on io.Reader and io.Writer: Decode, Identify, Thumbnail, Convert and the Encode… functions. Under it is the low layer: a registry of formats with decoders and encoders that take configurations, region and scale hints, limits and contexts; frames, layers and records as iterators; and operations as immutable values with names and parameters you can list and serialise. Decoded images implement image.Image, and every encoder and operation accepts any image.Image, with fast paths for the standard types.

Safe on hostile input

Files, SVG documents, fonts and PDFs are treated as untrusted. Dimensions, pixel counts, frame counts, metadata, expansion ratios and total allocation are limited by defaults you can change, and checked before anything is allocated; decompression bombs stop at their budget with an error. Parsers never recurse on input depth. The library imports no net or os/exec (only oneimage/web serves HTTP) and opens no files: input and output are readers and writers you hand it. Every decoder and parser has fuzz targets.

Deterministic

The same bytes and options give the same pixels and the same encoded bytes, on any number of goroutines. Lossless decoders are bit-exact; lossy decoders and float operations stay within one level at 8 bits.

Conformance

The library is judged by our internal conformance kit, a language-neutral set of test cases with pixel tolerances, run against adapters for each implementation. oneimage-go passes every case the C# library (RecursiveAI.OneImage 1.11.2) passes. A recorded deviation is a case where oneimage-go differs from the kit's expectation and the kit records why (a bug, or behaviour the library documents). The counts are those of oneimage-go at commit 9bb9cc4 against kit commit 7d6eea54.

SuiteCasesPassesRecorded deviations
core3633621
decode101510141
drawing2682680
encode3373370
icc4204200
layers1641640
metafile4644640
ops6056050
pdf5575561
psd3243240
safety3983980
svg18571729128
text4704700
web1401400
Total73827251131

Speed

Medians of five runs on one machine (AMD Ryzen 9 7950X3D, Linux, Go 1.26.8, 8 threads), every row checked for the same result before it is timed. The comparison is the fastest pure-Go competitor for the task; where there is none, the C# library at the same degree of parallelism. A ratio under 1 means oneimage-go took less time. Method, machine records and every row, with allocations: bench/RESULTS.md.

Taskoneimage-gocompared withtimeratio
PNG decode, photo27.35 msstandard library53.44 ms0.51
PNG encode, photo114.52 msstandard library422.65 ms0.27
JPEG decode, 24 MP 4:2:089.54 msstandard library168.14 ms0.53
JPEG encode, 24 MP 4:2:068.41 msstandard library322.02 ms0.21
GIF decode4.56 msstandard library8.68 ms0.53
GIF encode8.50 msstandard library12.60 ms0.68
WebP decode, lossy photo25.14 msgolang.org/x/image32.56 ms0.77
WebP encode, lossless photo247.50 msnativewebp447.31 ms0.55
WebP encode, lossy 2000×150039.43 msC# library49.10 ms0.80
BMP decode3.68 msgolang.org/x/image3.89 ms0.95
TIFF decode, LZW7.37 msgolang.org/x/image63.67 ms0.12
Thumbnail from a 24 MP JPEG66.74 msdisintegration/imaging215.97 ms0.31
Resize 6000×4000 to 400 wide, Lanczos319.49 msdisintegration/imaging39.10 ms0.50
Inflate, mixed data31.39 msstandard library37.56 ms0.84
Deflate level 6, mixed data18.96 msstandard library101.12 ms0.19
ICC conversion, sRGB to Display P3, 1 MP2.12 msmandykoh/prism9.54 ms0.22
ICC conversion, CMYK to sRGB, 4 MP7.10 msC# library17.81 ms0.40
SVG render, scene5.80 msoksvg43.54 ms0.13
Fill a 1000-point polygon, 2000×20000.76 msfogleman/gg7.68 ms0.10
Shaped text wrap, 24,000 characters3.29 msgo-text/typesetting3.68 ms0.89
PSD merged image, 3000×20003.04 msoov/psd4.49 ms0.68
Metafile render5.99 msC# library6.95 ms0.86
Flatten ten 24 MP layers34.61 msC# library40.85 ms0.85
PDF vector page, 150 dpi48.30 mspdfview69.99 ms0.69
PDF text page, 150 dpi13.94 mspdfview40.61 ms0.34
PDF image page, 150 dpi74.52 mspdfview105.62 ms0.71

The PDF rows time bytes to pixels for page 1 against github.com/richardwilkes/pdfview, which renders a page on one thread; oneimage-go uses up to 8 workers per page. On one thread (GOMAXPROCS=1) the same pages take 169.79 ms, 36.84 ms and 132.78 ms (vector, text, image), so the vector and image pages are slower than pdfview there; the table in bench/RESULTS.md shows both.

Where to look next

go doc recursiveai.org/go/oneimage and the other packages; every exported identifier states its defaults and edge behaviour, and the public features have runnable Example functions. CHANGELOG.md lists what each release provides.