100% 安全的 Rust OpenEXR 文件库
This library is a 100% Rust and 100% safe code library for reading and writing OpenEXR images.
OpenEXR is the de-facto standard image format in animation, VFX, and other computer graphics pipelines, for it can represent an immense variety of pixel data with lossless compression.
Features include:
This library has matured quite a bit, but should still be considered incomplete. For example, deep data and DWA compression algorithms are not supported yet.
If you encounter an exr file that cannot be opened by this crate but should be, please leave an issue on this repository, containing the image file.
The focus is set on supporting all feature and correctness; some performance optimizations are to be done.
What we can do:
Supported OpenEXR Features
Nice Things
read_rgba_file, write_rgba_file, read_all_data_from_file),
plus embracing common high-level Rust abstractionsread_all_data_from_file)
without knowing anything about the file in advancestd::io::Read and std::io::Write traits[!TIP] If you want to use the newest version of
exrswith an older Rust version, you can still do that, by forcing Rust to use an older version of thehalfcrate viacargo update -p half --precise 2.2.1, or downgrade all dependencies viacargo +nightly -Zminimal-versions generate-lockfile. Versionhalf 2.3.0and higher have an MSRV above 1.61.
Add this to your Cargo.toml:
[dependencies]
exr = "1.74.2"
# also, optionally add this to your crate for smaller binary size
# and better runtime performance
[profile.release]
lto = trueThe master branch of this repository always matches the crates.io version,
so you could also link the github repository master branch.
Example: generate an rgb exr file.
…See the the examples folder for more examples.
Or read the guide.
exrs aims to provide a safe and convenient
interface to the OpenEXR file format. It is designed
to minimize the possibility of invalid files and runtime errors.
It contains a full-fledged image data structure that can contain any exr image,
but also grants access a low level block interface.
This library does not try to be a general purpose image file or image processing library. Therefore, color conversion, beautiful subsampling, and mip map generation are left to other crates for now. As the original OpenEXR implementation supports those operations, this library may choose to support them later. Furthermore, this implementation does not try to produce byte-exact file output matching the original implementation, instead, it is only aimed for correct output.
This library uses no unsafe code. In fact, this crate is annotated with #[forbid(unsafe_code)].
Some dependencies use unsafe code, though this is minimized by selecting dependencies carefully.
All information from a file is handled with caution. Allocations have a safe maximum size that will not be exceeded at once, to reduce memory exhaustion attacks.
This crate supports the wasm32-unknown-unknown target for use in browsers and Node.js.
For JavaScript/TypeScript projects, install the exrs package from npm:
npm install exrsimport { init, encodeExr, decodeExr } from 'exrs';
await init();
const bytes = encodeExr({
width: 1920,
height: 1080,
layers: [{ name: 'rgba', channels: 'rgba', data: rgbaPixels }]
});See the exrs-wasm documentation for the full API reference.
Uint8Array) instead of file handlesThis library does not support the toxic mindset of rewriting existing C++ code in Rust just for the sake of switching the language. The OpenEXR image format is defined by a proven and battle-tested reference implementation.
However, as an alternative to the official reference implementation, this library has the opportunity to explore radically different designs, no matter what language it is written in. Neat!
Also, I really wanted to have a library which had an 'X' in its name in my git repositories.
Keep in mind that there are official Rust bindings to the C++ reference implementation, and they offer several advantages over this Rust implementation:
This library is modeled after the
official OpenEXR Documentation
document. Saved older PDFs in /specification. Unspecified behavior is concluded from the C++ library.
This project has awesome contributors and is welcoming for contributions on Github.
This repository uses Rustfmt's standard style. Continuous Integration checks formatting on every pull request and will fail if files are not properly formatted.
How to format locally:
cargo +nightly fmt --allcargo +nightly fmt --all -- --checkIf cargo +nightly fmt is not found, install the Rustfmt component via Rustup:
rustup component add rustfmtrustup toolchain install nightlyrustup component add --toolchain nightly rustfmtYou may also configure your editor to run cargo +nightly fmt on save.
To run all fast tests on your native system, use cargo test.
To start fuzzing on your native system indefinitely,
use cargo test --package exr --test fuzz fuzz -- --exact --ignored.
To run all fast tests on an emulated system, use one of the following commands.
Each command requires a running docker instance,
and cross-rs to be installed on your machine (cargo install cross).
cross test --target powerpc-unknown-linux-gnu --verboseYou may also need to install the toolchain beforehand, using
rustup toolchain add stable-x86_64-unknown-linux-gnu --profile minimal --force-non-host
and rustup target add powerpc-unknown-linux-gnu.
To benchmark the library, simply run cargo bench.
The DWA DCT/IDCT code has SIMD kernels (AVX2 and SSE2) that are selected at
runtime via pulp, mirroring OpenEXR's own
cpuid dispatch. Each tier has its own feature-gated unit-test module in
src/compression/dwa/idct.rs:
mod avx2_tests — requires the avx2-tests feature, exercises the AVX2 kernelmod sse2_tests — requires the sse2-tests feature, exercises the SSE2 kernelThese tests are opt-in and are excluded from the normal cargo test run. A
test only passes if the requested tier is actually available on the CPU it runs
on, so the AVX2 test needs an AVX2-capable CPU and the SSE2 test additionally
asserts that AVX2 is not present (so it verifies the fallback path).
Because most machines only expose their native tier, we run these tests under
Intel SDE
(Software Development Emulator), which emulates a chosen microarchitecture. This
is exactly what the SIMD tests CI workflow does: Cargo still builds a generic
x86/x86-64 binary, and SDE only changes the runtime CPUID/features seen by
pulp. To reproduce it locally:
Install Intel SDE. Download it from the link above and put the sde64
binary on your PATH (or note its full path). SDE runs on x86-64 Linux,
macOS, and Windows.
Build the library test binary without running it, then locate the executable:
# AVX2
cargo test --lib --features avx2-tests --no-run
# SSE2
cargo test --lib --features sse2-tests --no-runCargo prints the path to the compiled unit-test binary (under
target/debug/deps/exr-). Since these are now in-crate unit tests, you
select a tier by passing a test-name filter (there is no per-file test
binary). A short substring like avx2/sse2 is enough — it matches the
respective mod avx2_tests/mod sse2_tests.
Run that binary under SDE, selecting a chip that exposes the target tier and filtering to the tier's test module:
# AVX2 — Haswell is the first microarchitecture with AVX2 + FMA
sde64 -hsw -- target/debug/deps/exr- --nocapture avx2
# SSE2 — Merom is the lowest 64-bit chip SDE models: it has SSE2 but no AVX,
# so pulp falls back to the SSE2 kernel
sde64 -mrm -- target/debug/deps/exr- --nocapture sse2The CI workflow asserts the run reports the exact expected number of passing tests, so a mistyped filter (which libtest
暂无开放 Issues,或尚未同步最近议题。