Skip to content

feat: BlurHash placeholders (image.blurhash, decodeBlurhash, fm=blurhash) - #1567

Merged
hipstersmoothie merged 1 commit into
mainfrom
sesh/blurhash-placeholders
Nov 25, 2026
Merged

hipstersmoothie merged 1 commit into
mainfrom
sesh/blurhash-placeholders

Conversation

@hipstersmoothie

Copy link
Copy Markdown
Collaborator

Adds BlurHash encoding and decoding to @jimp/plugin-hash, for tiny image placeholders.

API

import { Jimp, decodeBlurhash } from "jimp";

const image = await Jimp.read("photo.jpg");
image.background = 0xffffffff;         // BlurHash has no alpha
const hash = image.blurhash();           // 4x3 components, 28 chars
image.blurhash({ componentsX: 3, componentsY: 4 });

const placeholder = Jimp.fromBitmap(decodeBlurhash(hash, { w: 32, h: 24, punch: 1 }));
  • image.blurhash(options?): a value-returning plugin method, like hash/pHash. Options are a zod schema; componentsX/componentsY are whole numbers from 1 to 9, defaulting to 4 and 3.
  • decodeBlurhash(hash, { w, h, punch? }): exported from the plugin and re-exported from jimp, like distance/compareHashes. It returns { data: Uint8ClampedArray, width, height }, which fits both Jimp.fromBitmap and a canvas ImageData without a copy. Plugins don't add statics to the Jimp class, so this is a function rather than a factory.
  • Types BlurhashOptions and DecodeBlurhashOptions are exported from jimp.
  • apply() and @jimp/worker need no changes. JimpEdit already drops methods that don't return the image, and apply throws at runtime. Type tests now cover blurhash.

Implementation

  • The algorithm is written in-house in plugins/plugin-hash/src/blurhash.ts. It follows the reference TS package's arithmetic step by step, including the order of sums and products, so the strings match to the character. It encodes in one pass over the pixels for all components, with cosine and sRGB-to-linear lookup tables.
  • It works in linear light using the sRGB transfer function, the same formulas as the reference.
  • Images larger than 64px on their longest side are scaled down with the default resize first, without copying the full-size pixels. A 10000×783 JPEG encodes in about 30 ms.
  • Transparent pixels are drawn source-over image.background, the same rule getBuffer uses for JPEG.
  • Display P3 and other colour spaces are converted to sRGB before encoding.

@jimp/server: fm=blurhash

This was small, so it's implemented. ?fm=blurhash runs the other params as usual, then responds with the hash as text/plain; charset=utf-8. It gets the same ETag and Cache-Control as image responses, and HEAD works. As with formats without alpha, transparent pixels are drawn over white unless bg is set. A Jimp class without blurhash gets a 415.

Docs

  • New method pages blurhash and decode-blurhash, in "Loading & saving", registered in methods.ts.
  • A blurhash playground op in its own ops/placeholder.ts, which also gives the OG card a preview.
  • A "BlurHash placeholders" section in the image server guide.

Tests

  • plugins/plugin-hash/src/blurhash.test.ts compares against the reference blurhash package (new devDependency).
    • Encode matches exactly for all 81 component combinations, for noise, and for photos at full size.
    • image.blurhash() matches the reference for images up to 64px, and for the reference run on the same 64px resize.
    • Decode matches the reference within 1 level per channel.
    • Also covers: the image isn't changed, alpha and background handling, P3 conversion, invalid hashes and options.
  • Server tests for fm=blurhash, HEAD, bg, and the 415.
  • Ran pnpm build (including the browser bundle check), pnpm lint, the plugin-hash, jimp (including type tests) and server suites, and the docs build.

Worth a look

  • Reference quirks. The npm blurhash package takes the AC maximum without abs(), while the C and Swift encoders use the magnitude. I copied the TS behaviour so strings match that package. Its decoder also does punch | 1, so 1.5 becomes 3. We use punch as given, and the docs say so.
  • Default background. The default image.background is transparent, so fully transparent pixels turn black, the same as JPEG output. The docs and playground recommend setting image.background. The server defaults to white.
  • Components are fixed at 4x3 by default rather than following the aspect ratio. fm=blurhash doesn't take component params yet.

🤖 Generated with Claude Code

@hipstersmoothie hipstersmoothie added the minor Increment the minor version when merged label Nov 25, 2026
image.blurhash({ componentsX, componentsY }) encodes a BlurHash (default
4x3) and decodeBlurhash(hash, { w, h, punch }) turns one back into a bitmap
for Jimp.fromBitmap or a canvas ImageData. The algorithm is in-house and
matches the reference blurhash package exactly; large images are scaled to
64px first, transparent pixels are drawn over image.background, and other
colour spaces are converted to sRGB.

@jimp/server gets fm=blurhash, which responds with the edited image's hash
as text/plain. Docs pages, a playground op and type tests are included.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie
hipstersmoothie force-pushed the sesh/blurhash-placeholders branch from 1c92dc4 to 9455762 Compare November 25, 2026 03:38
@hipstersmoothie
hipstersmoothie merged commit 8719cfb into main Nov 25, 2026
7 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/blurhash-placeholders branch November 25, 2026 03:43
@hipstersmoothie

Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.2.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Increment the minor version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant