Skip to content

feat: colorGrade() in @jimp/plugin-lut, 3D LUT color grading from .cube files and Hald images - #1570

Merged
hipstersmoothie merged 5 commits into
mainfrom
sesh/lut-plugin
Nov 25, 2026
Merged

hipstersmoothie merged 5 commits into
mainfrom
sesh/lut-plugin

Conversation

@hipstersmoothie

@hipstersmoothie hipstersmoothie commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Adds image.colorGrade(lut, { strength }), which color grades an image with a 3D LUT, and parseCubeLut(text), which reads .cube files.

API

import { Jimp, parseCubeLut } from "jimp";

const lut = parseCubeLut(await readFile("film.cube", "utf8"));
image.colorGrade(lut);                                       // a .cube LUT
image.colorGrade(await Jimp.read("kodachrome.png"), { strength: 0.5 }); // a Hald CLUT image
  • colorGrade(lut: Lut | JimpInstance, options?: ColorGradeOptions)
    • Uses tetrahedral interpolation. It wasn't much more code than trilinear: one 6-way branch to order the fractions, then 4 corners instead of 8. It's also faster.
    • strength blends with the original. It's clamped to 0–1, like sepia(amount).
    • Alpha is unchanged. Outputs are clamped to 0–255, and inputs outside the LUT's domain are clamped.
    • Hald images (L³ × L³, so 512² for level 8) are read in place, with no conversion. Any other size throws an error that gives the image's size.
  • parseCubeLut(text): Lut
    • Reads LUT_3D_SIZE (2–256), TITLE, DOMAIN_MIN/DOMAIN_MAX, Resolve's LUT_3D_INPUT_RANGE, # comments, blank lines, a BOM and CRLF line endings.
    • Unknown keywords are ignored, as other readers do.
    • Rejects LUT_1D_SIZE and LUT_1D_INPUT_RANGE (1D LUTs and shapers), plus malformed files. Each error names the line.
  • Lut is plain data: { title?, size, domainMin?, domainMax?, data }. data is a Float32Array, but colorGrade() also accepts a number[] (from JSON).
  • Validated with zod, like the other plugins.
  • Naming: the method was first called lut() and was renamed to colorGrade() (b7208bc) so the name says what it does. parseCubeLut, Lut and the package name @jimp/plugin-lut keep "LUT", because they're about the LUT itself.

Speed on a 12 MP image: about 150 ms with a .cube LUT and about 195 ms with a Hald image (sepia takes about 370 ms). Parsing a 17³ .cube takes about 3 ms.

Bundle size and the default jimp bundle

min gzip
@jimp/plugin-lut alone (zod/types external) 4.5 KB 2.2 KB
…methods only (the parser tree-shaken out) 2.5 KB 1.3 KB
jimp browser bundle, current main → this PR 723,912 → 728,478 B (+0.6%) 234,586 → 236,397 B (+0.8%)
For comparison: plugin-dither / plugin-morphology 2.2 / 1.6 KB 1.2 / 0.8 KB

I've added it to the default plugins, in its own commit (3105b19) so it's easy to drop. It costs +1.8 KB gzipped, and with it the default Jimp works with colorGrade in apply(), @jimp/worker and @jimp/server without needing createJimp. It also lets the docs page have a live playground. If you'd rather keep it out, drop that commit. The docs examples would then need createJimp, and the playground op would need to go.

apply / worker / server

  • apply(): ["colorGrade", lut, { strength }] works. A Float32Array doesn't survive JSON.stringify, so the docs show Array.from(lut.data) for storing a LUT as JSON.
  • @jimp/worker: a parsed LUT and a Hald image both work in edits, because the worker's protocol already copies typed arrays and images. Tested in worker.node.test.ts.
  • @jimp/server: a LUT is too large to put in a URL, so colorGrade stays out of defaultAllowedEdits. The recommended approach, documented and tested in handler.test.ts, needs no server changes. Load the LUTs on the server and register a small custom method, e.g. look(image, name, options), then allow it with allowedEdits: [...defaultAllowedEdits, "look"]. Clients request a look by name through the signed edits param.
    • Possible follow-up: an unsigned, allow-listed presets option (preset=warm → a stored edit list) would serve named looks without signatures. That would be a general feature for any edits, not specific to LUTs, so I left it out of this PR.

Tests

  • An identity LUT is a no-op: sizes 2, 3, 17 and 33, Hald levels 2, 4 and 8, and an identity .cube fixture.
  • An affine LUT gives exact results (.cube and Hald).
  • Domain mapping and clamping, output clamping, the strength blend and clamping, number[] data from JSON, grading a Hald image with itself, and apply.
  • Reference output: the results are compared with FFmpeg's lut3d and haldclut (interp=tetrahedral) on a 64×64 RGBA sweep. Red, green and blue are always equal to FFmpeg's or exactly 1 higher, because FFmpeg truncates to 8 bits where Jimp rounds. Alpha is identical. The fixture LUT is non-separable, so FFmpeg's trilinear output differs from the tetrahedral reference by up to 40 levels. That means the test can tell the two methods apart.
  • Example images: image snapshots of test-utils' dice.png graded with the teal and orange .cube LUT and with the Hald image of the same look, in plugins/plugin-lut/src/__image_snapshots__/ (about 230 KB each). The dice are on a transparent background, so the snapshots also show alpha is kept.
  • Every fixture is made by plugins/plugin-lut/scripts/generate-fixtures.mjs, and every docs look by packages/docs/scripts/lut-images.mjs. No third-party LUTs are included.
  • Each parser error case is tested.

Root pnpm build (with the browser bundle check), pnpm lint, the plugin-lut, jimp (including type tests), server and worker tests, and the docs build (astro check) all pass.

Docs

  • New method pages: methods/color-grade.mdx (before/after images, a playground op with three looks plus a strength slider, and apply/worker/server sections) and methods/parse-cube-lut.mdx.
  • Both are registered in data/methods.ts, and plugin-lut has a row in the custom-Jimp package table.

🤖 Generated with Claude Code

@hipstersmoothie hipstersmoothie added the minor Increment the minor version when merged label Nov 25, 2026
@hipstersmoothie hipstersmoothie changed the title feat: @jimp/plugin-lut, 3D LUT color grading from .cube files and Hald images feat: colorGrade() in @jimp/plugin-lut, 3D LUT color grading from .cube files and Hald images Nov 25, 2026
hipstersmoothie and others added 5 commits November 24, 2026 19:44
image.lut(lut, { strength }) maps every pixel through a 3D LUT with
tetrahedral interpolation and blends with the original by strength.
Alpha is unchanged.

- parseCubeLut(text) reads .cube files: LUT_3D_SIZE, TITLE, DOMAIN_MIN,
  DOMAIN_MAX, Resolve's LUT_3D_INPUT_RANGE, comments, a BOM and CRLF.
  It rejects 1D LUTs (LUT_1D_SIZE, LUT_1D_INPUT_RANGE) and malformed
  files with errors that name the line.
- The LUT is plain data (Float32Array, or a number[] from JSON), so it
  works in apply() and can be posted to a worker.
- A Hald CLUT image (L³ × L³ pixels) can be passed instead, and is read
  in place without converting it.
- Outputs are clamped to 0-255, inputs outside the domain are clamped,
  and strength is clamped to 0-1.

The fixtures, including the reference outputs from FFmpeg's lut3d and
haldclut filters, are made by scripts/generate-fixtures.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds lut() to Jimp and exports parseCubeLut and the Lut and LutOptions
types from jimp. It adds 4.6 KB (1.8 KB gzipped, about 0.8%) to the
browser bundle, less than plugin-dither, and lets apply(), @jimp/worker
and @jimp/server use lut with the default Jimp class.

Tests that a LUT and a Hald image can be sent to a worker in edits, and
that the image server can apply LUTs by name through a custom method
(LUTs are too large for URL params, so lut isn't in defaultAllowedEdits).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- lut() page with before/after images, a playground op with three Hald
  CLUT looks and a strength slider, and sections on apply/JSON, workers
  and serving named looks from @jimp/server.
- parseCubeLut() page with the returned fields, the .cube format and its
  errors.
- scripts/lut-images.mjs makes the looks (defined in the script, so no
  third-party LUTs) and the images in public/lut/.
- @jimp/plugin-lut in the custom Jimp package table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The method's name now says what it does, next to sepia, greyscale and color. Its options type is ColorGradeOptions, and the docs page moves to /methods/color-grade/. parseCubeLut, the Lut type and @jimp/plugin-lut keep their names, since they're about LUTs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Grades test-utils' dice.png with the teal and orange .cube LUT and with the Hald image of the same look, and snapshots both. The dice are on a transparent background, so the snapshots also show alpha is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie
hipstersmoothie merged commit 2c66465 into main Nov 25, 2026
7 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/lut-plugin branch November 25, 2026 03:49
@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