In Hokusai 1.0, Hokusai(url:) had already opened the JPEG by the time I called .resize(width: 400). The decode settings were already in place, even though the chain only needed a thumbnail. Hokusai is my Swift library for image processing with libvips, and this was one of the reasons I changed what a pipeline holds in 2.0.
I first shared Hokusai here when I used libvips for image processing and ImageMagick for text. In the next post I showed how I moved text rendering to libvips and added a CLI for benchmarks. In that discussion, u/catbrane pointed out libvips’ thumbnail path, which can shrink certain images during loading. By 1.0 I had a dedicated thumbnail API, and I wanted to use the same decoder feature for a regular resize inside a chain of operations. Original post · Follow-up · u/catbrane’s comment
What a pipeline holds now
The Swift API already returned a new value for every transformation in 1.0. Those values held native libvips images, so calls like resize() and blur() built parts of the native graph on the calling thread. Most of the pixel work could still wait until something requested the result. The library had already picked how to open the file, though, and the caller was still adding operations to the chain.
In 2.0, the Swift value holds the source and an ordered plan. A resize appends an operation to that plan, and .jpeg() sets the output options. Then data() or write(to:) hands the plan to an evaluator that opens the source and builds the native graph. libvips still computes pixels on demand, and Hokusai can see the full operation order before deciding how to decode the image.
Why the order matters
Here’s the same JPEG with the same two operations. Assume photoURL points to a 6000 × 4000 JPEG.
let source = try Hokusai(url: photoURL)
let resizeFirst = try source
.resize(width: 400)
.blur(sigma: 3)
.jpeg(quality: 85)
let blurFirst = try source
.blur(sigma: 3)
.resize(width: 400)
.jpeg(quality: 85)
At this point I have two plans derived from source. The try covers checks that happen while building the plans, such as rejecting a width of zero. The transforms run later. When I call data() on resizeFirst, the evaluator can read the JPEG header, see the resize at the start of the chain, and reopen the file with decoder shrink enabled. In this case, the decoder can produce a 750 × 500 image at one-eighth of the original width and height. The remaining resize brings the image to 400 pixels wide before the blur runs.
When I call data() on blurFirst, the blur needs pixels at full resolution. Shrinking during decode would change what that operation sees, so Hokusai uses the full-size decode path. Both plans produce an image 400 pixels wide. Applying the blur at different resolutions can change the result. Even with the same resizeFirst plan, pixel values can differ slightly between decoding at a reduced size and decoding at full resolution before resizing. In the first case, resize() resamples the smaller image produced by the decoder.
u/catbrane also brought up sequential access. That setting tells libvips what order to expect for pixel requests. Decoder shrink changes the size of the image the decoder produces. Sequential access remains an explicit input option. The JPEG example uses the default access mode, and the shrink decision comes from the source dimensions and the operation order.
There’s also a server-side effect. In 1.0, the Swift task assembling the pipeline could also build the native graph. Now the output call dispatches that blocking work to Hokusai’s bounded scheduler, outside Swift’s cooperative executor. The scheduler limits how many pipelines run concurrently, while libvips manages its own worker threads.
The trade-off is when errors surface. In the example, a missing file is reported when data() tries to open it. If I only need the original dimensions up front, metadata() opens the source to read its header without running the transforms.
This change made me think about how much information the wrapper has when it calls into the native library. Keeping the operation order in Swift until evaluation lets the decoder and resize work together, and callers can still write the pipeline as the same chain of operations.
If you’ve written a Swift wrapper around a native library, when do you open the native object? What trade-offs did that timing create for your callers?
Hokusai on GitHub