@colorhythm/libraw-wasm

中文文档
1.1.1发布于 2026/08/04MITTypeScriptES Module构建来源已验证近 30 天下载 1.4万

LibRaw WebAssembly bindings with camera-raw analysis surfaces for browser and Node.js applications.

原始使用文档
文档目录12 节
  1. What Colorhythm adds
  2. Install
  3. Inspect a camera-raw file
  4. 1.1.0 compatibility note
  5. Error handling
  6. Analysis API
  7. Processing policy
  8. Worker use
  9. WebAssembly loading and lifecycle
  10. Develop
  11. Lineage and thanks
  12. License

@colorhythm/libraw-wasm

npm version

TypeScript bindings for LibRaw, compiled to WebAssembly for browser and Node.js camera-raw decoding. Colorhythm maintains this fork for applications that need direct, analysis-oriented access to sensor data—not only a rendered RGB image.

The package is intentionally low-level. It can expose the stored sensor buffer, visible image geometry, color-filter facts, camera characterization, black and saturation levels, embedded metadata, thumbnails, and LibRaw's processed output. Heavy decoding and pixel statistics should normally run in a Worker.

What Colorhythm adds

This fork preserves the original API and adds surfaces needed for browser-side camera-raw analysis:

  • Stored and visible geometry, including margins, orientation, pixel aspect, and row pitch
  • CFA and Foveon source facts, plus 16-bit one-, three-, and four-component buffers
  • Camera matrices, channel descriptions, and white-balance multipliers
  • Rawpy-compatible adjusted black levels and reported per-channel linear maxima
  • Processing setters for half_size and use_camera_wb
  • Typed LibRawError diagnostics with stable numeric LibRaw error codes
  • LibRaw's X3F decoder compiled into the WebAssembly build

Install

npm install @colorhythm/libraw-wasm

The generated module loads libraw.wasm beside itself through import.meta.url in Node.js and compatible bundlers. The WebAssembly binary is also exported as @colorhythm/libraw-wasm/libraw.wasm for applications with a custom asset pipeline.

Inspect a camera-raw file

import { LibRaw } from "@colorhythm/libraw-wasm";

async function inspectCameraRaw(file: File) {
    await LibRaw.initialize();

    const decoder = new LibRaw();
    await decoder.waitUntilReady();

    try {
        decoder.open(await file.arrayBuffer());
        decoder.unpack();

        const source =
            decoder.getRawImage() ??
            decoder.getColor3Image() ??
            decoder.getColor4Image();

        if (source === null) {
            throw new Error("LibRaw did not expose an unpacked source buffer");
        }

        const camera = decoder.getIParams();

        return {
            blackLevels: [0, 1, 2, 3].map((index) =>
                decoder.getBlackLevel(index),
            ),
            camera: `${camera.normalized_make} ${camera.normalized_model}`,
            geometry: {
                activeHeight: decoder.getActiveHeight(),
                activeWidth: decoder.getActiveWidth(),
                marginLeft: decoder.getLeftMargin(),
                marginTop: decoder.getTopMargin(),
                storedHeight: decoder.getRawHeight(),
                storedWidth: decoder.getRawWidth(),
            },
            sourceSamples: source.length,
        };
    } finally {
        decoder.dispose();
    }
}

getRawImage(), getColor3Image(), and getColor4Image() return owned Uint16Array copies after unpack(). They remain valid after another decode, recycle(), WebAssembly memory growth, or dispose(). A method returns null when LibRaw does not expose that source layout for the file. The current binding does not expose LibRaw's floating-point source buffers.

1.1.0 compatibility note

Version 1.1.0 corrects the source-pixel return contract introduced in 1.0.0. The TypeScript return type is Uint16Array | null, each non-null array has the exact unpacked length without row padding, and every array owns its storage. Code that assumed the earlier non-null type should select the available source layout explicitly, as in the example above.

Error handling

LibRaw failures throw LibRawError, which preserves the numeric LibRaw error code alongside its diagnostic message:

import { LibRawError } from "@colorhythm/libraw-wasm";

try {
    decoder.open(buffer);
} catch (error) {
    if (error instanceof LibRawError) {
        console.error(error.code, error.message);
    }
    throw error;
}

Analysis API

AreaMethodsNotes
GeometrygetActiveHeight(), getActiveWidth(), getRawHeight(), getRawWidth(), getRawPitch(), margin getters, getFlip(), getPixelAspect()Raw dimensions describe stored data; LibRaw's visible dimensions and margins locate the image area. These values are not necessarily the literal DNG ActiveArea. Pitch is measured in bytes.
Source layoutgetColors(), getCdesc(), getFilters(), getIsFoveon(), color()Describes CFA and multi-channel source organization. getCdesc() returns a character code.
Source pixelsgetRawImage(), getColor3Image(), getColor4Image()Returns the unpacked 16-bit source buffer that LibRaw exposes for the file.
LevelsgetBlackLevel(), getLinearMax(), getColorMaximum(), getDataMaximum()Exposes adjusted per-channel black levels, per-channel linear maxima, and LibRaw's common and observed maxima. A per-channel linear maximum can be 0 when the file does not provide one.
CharacterizationgetCamMul(), getPreMul(), getCamXyz(), getCmatrix(), getRgbCam()Exposes camera white-balance multipliers and numeric color matrices.
Descriptive metadatagetIParams(), getImgOther(), getLensInfo(), getMakernotes(), getShootingInfo()Reads LibRaw's typed metadata structures. Use a dedicated metadata parser when complete tag coverage is required.
Thumbnails and renderingunpackThumb(), dcrawMakeMemThumb(), dcrawProcess(), dcrawMakeMemImage()Provides embedded previews and LibRaw's processed output path.

Static methods expose the compiled LibRaw version, supported camera count, and camera list:

await LibRaw.initialize();

console.log(LibRaw.version());
console.log(LibRaw.cameraCount());
console.log(LibRaw.cameraList());

Processing policy

Set processing parameters before dcrawProcess(). setHalfSize(1) requests half linear dimensions, while setUseCameraWb(1) requests the camera's as-shot white-balance multipliers. Applications should also choose their output color space explicitly with setOutputColor() rather than inherit an implicit LibRaw default.

The unpacked source path does not call dcrawProcess() and therefore remains appropriate for sensor-native analysis.

Worker use

Camera-raw decoding and full-resolution statistics can consume substantial CPU time and memory. In browser applications, create and use LibRaw inside a dedicated Worker. Transfer the input ArrayBuffer to the Worker, perform analysis there, and return only the summaries or pixel data the interface needs.

Each successful open() keeps its copied input alive until another open(), recycle(), or dispose() releases it. Reuse a decoder for sequential jobs or dispose it when the Worker is finished. Source-pixel getters return owned copies that can be transferred or retained independently.

WebAssembly loading and lifecycle

LibRaw.initialize() is shared within a JavaScript realm, and its first call selects the WebAssembly payload. Supply any custom Response or ArrayBuffer before constructing a decoder:

const response = await fetch("/assets/libraw.wasm");

if (!response.ok) {
    throw new Error(`Unable to load LibRaw WASM: ${response.status}`);
}

await LibRaw.initialize(await response.arrayBuffer());

Passing an ArrayBuffer avoids a dependency on the server's WebAssembly MIME type. Passing a Response enables streaming compilation and requires the server to send application/wasm.

Develop

Clone with submodules, install the workspace, and run the checks:

git clone --recurse-submodules https://github.com/colorhythm/libraw-wasm.git
cd libraw-wasm
corepack enable
pnpm install
pnpm check
pnpm test

Rebuilding the WebAssembly binary requires make and Emscripten. Put emcc and em++ on PATH, set SYSROOT to Emscripten's cache/sysroot directory, then run pnpm build. The repository's build setup action is the executable reference for the supported toolchain.

Lineage and thanks

This is Colorhythm's maintained fork of libraw.wasm by TOMIKAWA Sotaro. His TypeScript bindings and WebAssembly build made browser-side LibRaw practical to extend. Thank you, Sotaro.

LibRaw is developed by LibRaw LLC and its contributors.

License

The TypeScript and WebAssembly wrapper is available under the MIT License. The included LibRaw source is available under either LGPL 2.1 or CDDL 1.0. See LibRaw's copyright and notices for its included third-party components. The npm tarball includes all three LibRaw files under LibRaw/.

版本与发布记录

@colorhythm/libraw-wasm 共发布 3 个版本,首次发布于 2026/07/02;过去 12 个月发布了 3 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.1latest2026/08/043 个月前
1.1.02026/08/043 个月前
1.0.02026/07/024 个月前

关于 @colorhythm/libraw-wasm 的常见问题

@colorhythm/libraw-wasm 是什么?

LibRaw WebAssembly bindings with camera-raw analysis surfaces for browser and Node.js applications.

如何安装 @colorhythm/libraw-wasm?

在项目目录运行 npm install @colorhythm/libraw-wasm,或使用 pnpm add / yarn add / bun add。

@colorhythm/libraw-wasm 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@colorhythm/libraw-wasm 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@colorhythm/libraw-wasm 可以商用吗?

@colorhythm/libraw-wasm 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@colorhythm/libraw-wasm 的下载量有多少?

根据 npm 官方下载统计,@colorhythm/libraw-wasm 在 2026/10/11 – 2026/11/09 这 30 天内被下载 14,113 次,日均约 470 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@colorhythm/libraw-wasm 还在维护吗?

最新版本 1.1.1 发布于 2026/08/04(3 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/07/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。