Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

Hokusai examples

Runnable demos for Hokusai, a Swift library for server-side image processing built on libvips. Open an image, build an ordered pipeline, see the result, and copy the Swift that reproduces it. Documentation: https://ivantokar.com/oss/hokusai/; a step-by-step guide to running, testing and adopting these demos is at https://ivantokar.com/oss/hokusai/integrations/examples/.

What Where
Web playground served by a Vapor backend (Hokusai Vapor) vapor-server/
The same playground served by a Hummingbird backend (Hokusai Hummingbird) hummingbird-server/
Shared recipes, operation catalog, Swift code generator, bounded upload store recipes/
Native macOS playground (SwiftUI, offline, no server) macos/

The web playground has an Inspect view (metadata verified on exported bytes, raw pixels in and out, pages and animation frames), a Runtime Lab (streaming, cancel, queue limits, failure examples) and an Export / PDF view (encoder comparison, raster PDF report, HokusaiPDF document builder). What each demo shows, and what it does not yet, is in the capability coverage.

Contents: Try it · How it is built · HTTP API · Capability coverage · Checks · Testing unreleased library changes · Known limitations · Related

Both backends serve one web UI and answer the same HTTP API, so you can compare how each integration behaves. The active backend is shown in the top bar.

Try it

Docker (Linux, libvips 8.16 built from source)

docker compose up vapor          # http://localhost:8081
docker compose up hummingbird    # http://localhost:8082

Both can run together; they publish different ports. The first build compiles libvips and takes a few minutes. The image reports what it can really do at /api/capabilities; codec support is never assumed from the image name.

Natively on macOS

brew install vips cairo pango pkg-config
cd vapor-server && swift run App          # http://127.0.0.1:8081
cd hummingbird-server && swift run App    # http://127.0.0.1:8082

Needs Swift 6.2+ (the Docker image builds with 6.2; macOS CI uses 6.4) and libvips 8.16+. Run from inside the server folder: the web files and samples are found relative to it. Environment variables: PORT, HOST, WEB_ROOT (default ../web), FIXTURES_ROOT (default ../fixtures), HOKUSAI_MAX_CONCURRENT (default 2) and HOKUSAI_MAX_QUEUED (default 2). The two Hokusai limits are small on purpose, so the Runtime Lab can show a queue filling and rejecting; Hokusai freezes them at the first image, so change them by restarting.

The macOS app

A native SwiftUI app that runs Hokusai locally through the public API: offline, no Docker, no server.

The macOS playground: a pipeline with Smart crop in the sidebar, the result on a checkerboard canvas, the inspector on the right and the generated Swift below

brew install vips cairo pango pkg-config
cd macos && swift run MacPlayground      # or open macos/Package.swift in Xcode and run the MacPlayground scheme

Open or drop an image, pick a sample or a recipe, add, reorder, disable and edit steps in the sidebar and inspector, compare with the original, and copy or export. It uses the same recipe model, catalog and code generator as the Web demo (recipes/), so a recipe means the same thing in both. Undo and redo, per-step reset and disable, and the original comparison never touch your file. Evaluation runs off the main actor, edits are debounced and superseded work is cancelled, and a result is published only if it belongs to the newest edit; if an edit fails, the last good image stays on screen with the error beside it. The reduced preview never reaches the Swift you copy or the export.

It is a Swift package, not a signed .app: a redistributable app needs a decision on bundling and signing the native libraries, which is out of scope here (it links the Homebrew libvips).

The minimal integration

Each server has one self-contained route you can read without the demo around it: upload → resize → response.

curl --data-binary @photo.jpg 'http://localhost:8081/api/minimal/resize?width=800' -o small.webp

It is ten lines in vapor-server/Sources/App/main.swift (minimalRoutes) and in hummingbird-server/Sources/App/main.swift.

How it is built

  • A recipe is an application-owned, versioned description of ordered steps plus an output setting. It is plain JSON, compiles into ordinary public Hokusai calls, and never serialises Hokusai's own plan. Repeated steps and their order are kept.
  • The catalog (recipes/Sources/HokusaiRecipes/Catalog.swift) defines every operation once: its public API mapping, description, parameters with units, defaults and ranges, how to apply it and how to print it as Swift. The web UI is generated from GET /api/catalog.
  • The servers only adapt HTTP. Evaluation, the upload store and the JSON answers live in HokusaiDemoServices and are shared.
  • Preview is Hokusai output from the server. The recipe always runs on the full original; the preview only shrinks the result for display, so crop and text stay exact. Full size and Export render the real result.
  • A recipe can start from a generated source instead of an upload: recipe.source is { "kind": "text", "params": { … } } (see sources in GET /api/catalog). Then no uploadId is sent, and the generated Swift is Hokusai.text(...), or a GET route in Vapor and Hummingbird. The Web playground offers it as Start from text and the Text card recipe.
  • Uploads are held in memory with a size limit, a total budget, a count limit and a 10-minute lifetime. An upload id is a random token, never a path.
  • The code panel shows Swift for Hokusai alone, or inside a Vapor or Hummingbird route. A placeholder (input.jpg) stands in for your file; it never contains a server path.

HTTP API

Request Answer
GET /api/backend which backend answered, Hokusai version
GET /api/catalog operations, outputs, presets, samples
GET /api/capabilities libvips and Cairo versions, readable and writable formats
GET /api/status running and queued evaluations, the frozen runtime configuration, stored uploads
POST /api/uploads (raw body) { uploadId, width, height, orientedWidth, … }
POST /api/render { uploadId?, recipe, previewMaxEdge?, stopBefore?, viewable? } the encoded image; X-Hokusai-* and Server-Timing headers. stopBefore (a step id) renders only the steps before it: the image a crop enters
POST /api/pdf { uploadId?, document, previewPage? } a vector PDF built with HokusaiPDF, or a PNG of one page decoded from it; X-Hokusai-Pdf-Pages, X-Hokusai-Selectable-Text
POST /api/compare { uploadId, recipe } the recipe through every encoder this runtime has: size, dimensions, time, or the reason one is missing
POST /api/raw/output { uploadId, recipe, format, maxEdge? } the recipe's raw pixels with X-Raw-* headers for the layout, bit depth, stride and alpha convention
POST /api/raw/input?width&height&format&stride? (raw body) declares the bytes as raw pixels, answers a PNG, reads them back in the same layout and reports X-Raw-Roundtrip
POST /api/inspect { uploadId, recipe?, page? } facts about the original and, with a recipe, about the bytes it exports, read back
POST /api/code { recipe, target, uploadId? } { code }; with an upload, a mask's size is named in the code
POST /api/minimal/resize?width= (raw body) the minimal integration

Errors are { "error": { "code", "message" } } with a status you can act on: 404 expired upload, 413 too large, 422 not an image or a recipe that cannot apply (for example a crop outside the image).

Capability coverage

coverage/COVERAGE.md maps every current public capability to where the demo shows it, and says honestly what is implemented, partial or still planned. CI regenerates it from coverage/coverage.json and fails if the catalog and the matrix disagree.

Checks

swift test --package-path recipes      # catalog, compiler, code generator, upload store
swift test --package-path macos        # the macOS app's model: undo/redo, revision gating, preview vs export
scripts/check-snippets.sh              # the Swift the playground prints compiles and runs, for every preset and the PDF template
scripts/coverage.py --check            # the coverage matrix is current
scripts/smoke.sh http://127.0.0.1:8081 vapor   # HTTP smoke test against a running backend
scripts/parity.sh http://127.0.0.1:8081 http://127.0.0.1:8082   # both backends answer identically (bytes, headers, errors)

CI builds both Docker images, starts each and runs the same smoke test; a macOS job runs the rest, starts both servers natively and checks that they answer identically.

Testing unreleased library changes

The packages depend on released versions and commit their resolved dependencies. To try local changes to a library, check it out next to this repository and use SwiftPM's edit mode in the package you are running:

cd vapor-server
swift package edit hokusai --path ../../hokusai          # or hokusai-vapor
swift run App
swift package unedit hokusai                             # restore the released version

Never commit an edited checkout.

Known limitations

  • Layers and masks use pictures from a fixed set of bundled samples or from your second image. A picture mask is stretched to the size of the image entering its step, so the demo measures that size by running the earlier steps (and the generated Swift names it). Convolution kernels (3×3 to 7×7), colour matrices (3×3) and affine transforms (2×2 with offsets) are edited as grids of numbers with presets; an affine transform's output area is not exposed.

  • Measured by the Runtime Lab on both backends: aborting a /api/render request in the browser does not stop the evaluation on the server; it finishes the work first. Treat browser-side cancellation as not proving native cancellation.

  • macOS: no text source and no on-canvas crop yet, and the window UI has been built and its model tested but not driven by an automated UI test.

  • There is no in-page switch between the two backends: run the one you want. scripts/parity.sh is what keeps them equal, by sending the same requests to both. The macOS app has numeric crop only, no vector document builder yet, and takes its second layer image through a file picker (its output panel does offer the raster PDF and every encoder).

  • Raw pixels: 16-bit values are shown as little-endian, the server's native order here (checked against the 8-bit output); the library has no statistics or histogram API and the demo does not invent one. Animations can be inspected and read frame by frame but not edited or encoded as animations: that is not in the library.

  • Vector documents: pictures come from the bundled samples or your upload; there is no font upload, so text uses the system's sans, serif and monospace. The "selectable text" indicator in the Web UI is a heuristic (the PDF carries a text font with a character map); the unit tests confirm it with a real PDF reader on macOS.

  • Browsers do not display TIFF, HEIF or PDF as images, so the playground shows a labelled PNG decoded from the real bytes; Export always downloads the real file.

  • The playground previews at 1280 px; there is no downsampled-input fast path yet, because it is not equivalent for blur, trim, text and geometry steps.

  • Multi-architecture images are not claimed: build and test on the architecture you run.

  • Fonts: the Docker image bundles DejaVu (free licence) for text; native runs use your system fonts.

  • Vector PDF: rasters are held in memory until the document is written and file I/O blocks the calling task (open issues #159 and #160 in the Hokusai repository).

Related

MIT licensed, see LICENSE. Sample images in fixtures/ are generated for this project.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages