Skip to content

About

Focus stacking in plain JavaScript: ECC alignment, Laplacian-pyramid / depth-map merge, retouching, 16-bit TIFF. Browser and Node, no dependencies. The engine of focusstacks.com.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

focus-stack-engine

Focus stacking in plain JavaScript. It aligns a focus bracket, takes each region from the frame where it is sharpest, and writes one picture that is sharp from front to back. It has no dependencies, no WebAssembly and no native code. It runs in a browser tab (Web Workers) and in Node ≥ 22.

This is the engine behind Focus Stacks, a free focus stacker that runs entirely in the browser. Nothing is uploaded: the frames never leave the visitor's computer. If you only want to stack a series, use the site. If you want to read how it is done, or build on it, this repository is the code.

What it does

  • Alignment. A coarse translation search, then affine ECC (Evangelidis & Psarakis, 2008) on gradient pictures at ⅛ and ¼ scale. Each frame starts from its neighbour's warp, which follows focus breathing and drift across long brackets. Frames that do not align are skipped and reported, not merged.
  • Merge.
    • Pyramid (default): a Laplacian-pyramid vote per level, with a majority filter on the winner maps. Best for fine detail such as hairs, bristles and crossing edges.
    • Depth map: each point is taken whole from the frame where it was sharpest, with a smoothed depth. Best for smooth surfaces such as coins, shells and petals.
  • Two passes, bounded memory. Frames stream through twice (vote, then gather), so memory does not grow with the number of frames.
  • Byte-identical helpers. worker.js can split the work over helper workers (parallel.js, helper.js), and the result is byte for byte the single-thread one.
  • Frame conformance (conform.js). A frame turned a quarter, by an EXIF tilt tag or in an editor, is turned back by comparing its picture with the reference. A frame of the same shape at another size is resampled. Anything else is left out with its reason.
  • Around the merge:
    • the retouch brush (retouch.js), which paints a region back from one aligned source frame, with undo;
    • likely-halo hints (halo.js);
    • a depth map (depthMapRgba);
    • optional unsharp sharpening;
    • linear-light merging for RAW input, tone-mapped once (colour.js).
  • Files.
    • a baseline TIFF reader: 8/16-bit, uncompressed, PackBits, LZW, Deflate (tiff-decode.js);
    • a 16-bit TIFF writer (tiff.js);
    • EXIF read and write for the camera fields, never GPS (exif.js).

RAW decoding is not in this repository. The site reads RAW files with LibRaw compiled to WebAssembly, which is under its own licence. Feed this engine 8-bit RGBA (rgba) or 16-bit RGB (rgb16) frames from whatever decoder you use.

Quick start (Node)

git clone https://github.com/vitalybibikov/focus-stack-engine
cd focus-stack-engine
npm test                                    # 42 tests, no install needed
node examples/stack-tiffs.mjs test/fixtures/pcb-tiff stacked.tif
node examples/stack-tiffs.mjs <your-folder-of-tiffs> out.tif depthmap

The API, in short (see examples/stack-tiffs.mjs):

import { Stacker, cropPlanes } from "./src/stack.js";
import { encodeTiff16 } from "./src/tiff.js";

// frames[i] = { index: i, width, height, rgb16 }   (or rgba: Uint8ClampedArray)
const ref = Math.floor(frames.length / 2);
const stacker = new Stacker({ width, height, frameCount: frames.length, referenceIndex: ref, method: "pyramid" });
stacker.setReference(frames[ref]);
for (let i = ref + 1; i < frames.length; i++) stacker.vote(frames[i]);   // pass 1: align and vote, walking outward
stacker.resetWalk();
for (let i = ref - 1; i >= 0; i--) stacker.vote(frames[i]);
stacker.finalizeVotes();
for (const f of frames) stacker.gather(f);                              // pass 2: take each region from its winner
const result = cropPlanes(stacker.result(), stacker.inset);             // { width, height, planes: [R, G, B] } in 0..1
const tiff = encodeTiff16(result);

In the browser

src/worker.js is the stacking worker the site runs (new Worker(url, { type: "module" })). The page posts the following messages:

  1. start with { width, height, frameCount, referenceIndex, method, linear, parallel };
  2. vote for each frame (pass 1), then finalize;
  3. gather for each frame (pass 2);
  4. result with { sharpen }, which returns the RGBA picture.

After that, tiff, depth, halo-spots, source-at, retouch, undo and sharpen-result work on the result. Frames are transferred as rgba or rgb16 buffers, never copied. The page around it (file intake, the decode pool, the inspector, the downloads) belongs to the site and is not part of this repository.

Status

This is the code that runs on focusstacks.com, with its tests. Its comments sometimes cite the site's internal design notes (docs/…), which are not published. The API is not frozen yet (0.x). Issues and pull requests are welcome, especially:

  • series that stack badly, with a halo, a soft area or a misalignment, if you can share the frames;
  • speed (SIMD, WebGPU).

Licence

Apache License 2.0, see LICENSE and NOTICE. The test fixtures are derived from PetteriAimonen/focus-stack's PCB example, under the MIT License (test/fixtures/pcb-tiff/LICENSE.txt).

About

Focus stacking in plain JavaScript: ECC alignment, Laplacian-pyramid / depth-map merge, retouching, 16-bit TIFF. Browser and Node, no dependencies. The engine of focusstacks.com.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages