Skip to main content

irradiance (Python)

Python/NumPy bindings for irradiance, a pure-Rust camera RAW reader. Built as a dataloader primitive for PyTorch/ML:

  • Hostile files raise ValueError. Truncated, corrupted or forged files never panic, abort or kill a DataLoader worker.
  • The GIL is released while decoding, so threads scale.
  • Deterministic output, with no C/C++ toolchain or system libraries.

Status: prototype (0.0.x). The API may change. Wheels are on PyPI and the releases page.

Scope

Input Result
Uncompressed Bayer DNG (any of RGGB, GRBG, GBRG, BGGR) decodes
Uncompressed monochrome (LinearRaw) DNG unpack_bayer decodes; pack_bayer_4ch raises ValueError
Compressed DNG (lossless JPEG, compression 7), vendor RAW (PEF, CR2, NEF, ...) ValueError ("compression N is not supported")
Tiled DNG, non-Bayer CFA (X-Trans, ...) ValueError

Upstream unpacks 8, 12, 14 and 16-bit samples. Tested here on real files: Canon EOS 5D Mark III, Nikon D300S and Nikon D3300 DNGs (Bayer, 16-bit), Leica M Monochrom (mono, 16-bit) and Leica Q2 Monochrom (mono, 14-bit). Bayer files below 16-bit are untested here. The Leica M Monochrom Typ 246 and Pentax K-3 III DNGs are lossless-JPEG compressed and are refused for now.

develop output is linear sRGB (white balance from AsShotNeutral, the DNG colour matrices, no tone curve or gamma), clipped to [0, 1]. Files with a ForwardMatrix (Canon 5D3) and with ColorMatrix only (Nikon D300S) both work. demosaic output is camera-space linear RGB: raw values, no black/white levels, white balance or colour matrix.

Install

pip install irradiance

Prebuilt wheels (Python 3.9+, no Rust needed) for Linux x86_64, Linux arm64 and macOS (Intel and Apple Silicon); each passed the full test suite in CI before release. Windows isn't built yet, and there's no source distribution until the upstream crate is on crates.io, so other platforms build from source.

From source

Needs Rust 1.90+ and the irradiance repo checked out at ../../crustimg_redo_plus/irradiance relative to this one (Cargo path dep).

uv venv .venv && uv pip install maturin numpy
.venv/bin/maturin develop --release

The package is irradiance both to install and to import (this repo is pyirradiance; not on PyPI yet).

API

import irradiance

data = open("photo.dng", "rb").read()

# Raw sensor plane, unmodified (masked border included).
plane, meta = irradiance.unpack_bayer(data)          # uint16[H, W], SensorMeta

# ML input: 2x2 tiles -> 4 half-resolution channels, always [R, Gr, Gb, B]
# (Gr = the green on red's row), whatever the camera's CFA phase.
x = irradiance.pack_bayer_4ch(plane, meta)                  # uint16[4, H/2, W/2]
x = irradiance.pack_bayer_4ch(plane, meta, normalize=True)  # float32, see below

# Full develop: levels, Hamilton-Adams demosaic, white balance, colour matrix,
# cropped (ActiveArea, DefaultCrop) and rotated (Orientation) to the final image.
rgb, version = irradiance.develop(data)               # float32[h, w, 3] linear sRGB, [0, 1]
rgb, version = irradiance.develop(data, threads=8)    # same bytes, ~6x faster on one image

# Demosaic only: camera-space RGB, raw values / 65535, full sensor plane.
rgb, version = irradiance.demosaic(data)              # float32[H, W, 3]

normalize=True returns (v - black) / (white - black) per channel in float32, without clipping: noise goes slightly below 0 and highlights can exceed 1. black is the per-site BlackLevel pattern (origin at the ActiveArea top-left, per the DNG spec), white is WhiteLevel (or 2**bits - 1). BlackLevelDeltaH/V and a BlackLevelRepeatDim that doesn't fit the 2x2 tile are not supported (the latter raises ValueError).

Cropping: develop returns the final image: cropped to ActiveArea, then DefaultCrop, then rotated by Orientation (since DEVELOP_VERSION 4; earlier versions returned the full plane). The other functions cover the full plane, including the masked border. To keep only active pixels there, use meta.active_area = (top, left, bottom, right) (halve it for pack_bayer_4ch output when top/left are even).

threads= (develop, demosaic; default 1) splits one image into row bands. The output is byte-identical for every value. Use it for single-image latency (a 23 MP develop: 412 ms on 1 thread, 61 ms on 8, Apple M4 Pro); keep the default inside DataLoader workers, which already run one decode per core.

ActiveArea origin: the CFA and BlackLevel patterns start at the ActiveArea top-left (DNG spec; LibRaw does the same); every function handles odd origins.

out=: unpack_bayer(data, out=arr) and pack_bayer_4ch(..., out=arr) write into a preallocated, C-contiguous, writable array of the right dtype and shape, and return it. For torch, pass tensor.numpy() (works for pinned tensors). Wrong shape/order/read-only raises ValueError; wrong dtype raises TypeError. On a decode error out may be partly written.

SensorMeta fields: width, height, bits_per_sample, black_level, black_levels, black_level_repeat_dim, white_level, cfa_pattern (list[int], 0=R 1=G 2=B), cfa_repeat_dim, cfa_plane_color, active_area.

DataLoader

class RawDataset(torch.utils.data.Dataset):
    def __getitem__(self, i):
        with open(self.paths[i], "rb") as f:
            plane, meta = irradiance.unpack_bayer(f.read())
        return torch.from_numpy(irradiance.pack_bayer_4ch(plane, meta, normalize=True))

A corrupt file raises ValueError in the worker, which torch re-raises in the main process. Catch it in __getitem__ to skip bad files instead.

Performance vs rawpy

Compared with rawpy (LibRaw) on the same DNGs, on an Apple M4 Pro (full results and method):

irradiance rawpy
Raw to normalised [4, H/2, W/2] tensor, 23 MP 11 ms 26 ms
Same, 8 threads 506 files/s 238 files/s
Full develop to linear sRGB, 23 MP 398 ms 477 ms
212 corrupt files: crashes / hangs 0 / 0 0 / 0
Peak memory, decode + develop to float32, 23 MP 705 MB 990 MB
Wheel size 0.3 MB 2-3 MB
Formats uncompressed DNG nearly all RAW

Guarantees and how they're checked

Claim Evidence
Hostile input -> ValueError, never a crash tests/smoke.py (83 truncated/corrupted files), tests/hostile_dims.py (forged sizes up to 2^32 x 2^32, each in a subprocess), tests/test_dataloader.py (corrupt files mixed into a 4-worker DataLoader)
Allocation bounded by input size Before allocating, the decoder requires the pixel data to fit in the file; a forged header can't request more than ~8 samples per input byte
Deterministic Repeat decodes and 4 processes produce identical hashes
Same bits on every platform tests/golden.py: 34 output hashes on synthetic DNGs (all 4 CFA phases; unpack, pack, normalise, demosaic, develop on both colour paths; pack, demosaic and develop with an odd ActiveArea origin). Match verified on macOS arm64 (also on the real corpus), Linux x86_64 and Linux arm64 (synthetic files only). CI runs this on every push, and wheels.yml runs it against each built wheel, including under Python 3.9.

Development

Tasks are in the justfile (just lists them):

just setup        # .venv with maturin, numpy, torch, rawpy
just build        # build the extension into .venv
just ci           # clippy, cargo test, build, all tests on synthetic DNGs (what CI runs)
just test-corpus  # the tests on real camera files
just docker       # the CI tests inside a Linux container
just bench        # irradiance vs rawpy (bench/README.md)

Releasing is just release X.Y.Z; see RELEASING.md.

License

Licensed under either of MIT or Apache-2.0 at your option, the same as upstream irradiance.

Metadata

Release files for irradiance 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for irradiance 0.0.4
File Interpreter ABI Platform Actions
irradiance-0.0.4-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details for irradiance-0.0.4-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
irradiance-0.0.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for irradiance-0.0.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
irradiance-0.0.4-cp39-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 10.12+ universal2 (ARM64, x86-64), macOS 11.0+ ARM64, macOS 10.12+ x86-64 Details for irradiance-0.0.4-cp39-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Total release size: 1.5 MB

Release files / irradiance-0.0.4-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL irradiance-0.0.4-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 395.3 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
db8486c038fcdb4ceec8b28284b80f5fd3fa6a56cf4e0a24ce09c5654751bd88
BLAKE2b-256 checksum
How to use checksums
b9ba46f01865c76aa54ac121ef0b900fa224fdb6aafeb30c7025b2621ffaf6a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / irradiance-0.0.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL irradiance-0.0.4-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 379.4 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
364b379f7627a45646919685e8de3a29a8739389c336b698faa77e935573d4d8
BLAKE2b-256 checksum
How to use checksums
4ca473a79e9dd16f463277480adc0fb64614d2b79c23e39649e6196669b2340b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / irradiance-0.0.4-cp39-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Download URL irradiance-0.0.4-cp39-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl
Size 770.2 kB
Tags CPython 3.9 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
d1b24be08cbc7117c4d00d37eb2ae4c2b5b5fd3862a87eb33a81892c07490356
BLAKE2b-256 checksum
How to use checksums
5c3386b2c7a6b6426a9b082917e3585e29ee11a3c885db50424e4a88d60cdae4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for irradiance 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 3 files in this release have been uploaded with provenance metadata.

Select from 3 files

Attestation for irradiance-0.0.4-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page