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 and Nikon D300S 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.
rgb, version = irradiance.develop(data)               # float32[H, W, 3] linear sRGB, [0, 1]

# Demosaic only: camera-space RGB, raw values / 65535.
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: outputs cover the full plane, including the masked border. To keep only active pixels, use meta.active_area = (top, left, bottom, right) (halve it for pack_bayer_4ch output when top/left are even).

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.

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: 29 output hashes on synthetic DNGs (all 4 CFA phases; unpack, pack, normalise, demosaic, develop on both colour paths). 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

tests/ci.sh                    # clippy, cargo test, build, all tests on synthetic DNGs
tests/linux_docker.sh linux/amd64   # the same inside a Linux container

tests/ci.sh needs torch in the venv (uv pip install torch). The scripts in tests/ also run on real files; see CLAUDE.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.2

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.2
File Interpreter ABI Platform Actions
irradiance-0.0.2-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.2-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
irradiance-0.0.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.9 abi3 Linux glibc 2.17+ ARM64 Details for irradiance-0.0.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
irradiance-0.0.2-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.2-cp39-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Total release size: 1.3 MB

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

Download URL irradiance-0.0.2-cp39-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 343.6 kB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
cc57e2e6932249675f54fac57614f752159b44a88eaa0b47cd3aa052f97f74f4
BLAKE2b-256 checksum
How to use checksums
e9744fce48afbab1402b61d557c98c5f0d1138774762dd2987138dec45d192bd
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 6, 2026.

Transparency log

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

Download URL irradiance-0.0.2-cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 330.9 kB
Tags CPython 3.9 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
4382ea709fc75a00ac336e07ae66b554c1096f67f6256d71ad25b8d96eb305f9
BLAKE2b-256 checksum
How to use checksums
327893d77939821124e42949890849a7d92c95dc4698cf45b954d1b08e5b6aff
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 6, 2026.

Transparency log

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

Download URL irradiance-0.0.2-cp39-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl
Size 664.1 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
0ed44fd2e91433f5d712222da3000fc98472b43cb6175891a304a92407e9b039
BLAKE2b-256 checksum
How to use checksums
62e1466331d3024e621227a9f71d3f6eac1c7e775f3128fa60f795badee670db
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 6, 2026.

Transparency log

Security for irradiance 0.0.2

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.2-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 6, 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