Skip to content
HarpeLmPublic

About

Pure-Rust HEIC/HEIF decoder & encoder (0 unsafe, 0 C, WASM-ready, image crate)

Topics

Resources

Contributing

Security policy

Stars

9 stars

Watchers

1 watching

Forks

Repository files navigation

heifer

Heifer — Pure-Rust HEIC, HEIF & AVIF Decoder & Encoder

HEIC, HEIF, AVIF and .heics decoding and encoding in pure, safe Rust.
No C, no libheif, no dav1d. Runs in the browser.

Demo · Documentation · crates.io · Changelog


Why

HEIC is the default photo format on iPhones and many Android phones. Reading it from Rust has mostly meant binding to libheif and libde265: a C toolchain, painful cross-compilation, no wasm32-unknown-unknown, and a large C parser in front of untrusted files.

Heifer is an independent implementation of HEIF, HEVC and AV1 in safe Rust (#![forbid(unsafe_code)]), checked against the official conformance bitstreams. It builds anywhere Rust builds, with nothing to install.

Features

  • Correct. Identical to ffmpeg on 62 of 62 HEVC conformance bitstreams. Every AV1 item of the av1-avif test set (238 of 238) matches dav1d.
  • Real photos. iPhone grids, rotation and spatial photos. Samsung 200 MP, Xiaomi, Sony.
  • Safe. No unsafe, size limits against hostile files, fuzzed around the clock.
  • Complete. Alpha, overlays, crop, rotation, mirror, 8 to 12 bits, EXIF, XMP, ICC, IPTC, depth maps, HDR gain maps, conversion to sRGB.
  • Encoder. HEIC photos and .heics sequences with P and B frames, in 4:2:0, 4:2:2, 4:4:4 or lossless, 8 or 10 bits. Files are smaller than x265 slow at equal quality.
  • AVIF. AV1 decoding with intra and inter frames, layered images, film grain, every bit depth and chroma format.
  • Portable. Optional image crate integration, and a WebAssembly build of about 250 KB.

Install

cargo install heifer-cli
heifer IMG_1234.heic photo.jpg           # convert
heifer photo.png photo.heic -q 80        # encode
heifer frames/*.png burst.heics --fps 24 # image sequence
heifer info IMG_1234.heic                # size, tiles, camera, metadata

As a library:

[dependencies]
heifer = "0.10"

Decoding only: heifer = { version = "0.10", default-features = false }.

Usage

Decode:

let bytes = std::fs::read("photo.heic")?;
let image = heifer::decode(&bytes)?;      // primary image, transforms applied
let rgba: Vec<u8> = image.to_rgba8();

let srgb = heifer::decode_srgb(&bytes)?;  // ICC profile or nclx converted to sRGB
let hdr = heifer::decode_hdr(&bytes)?;    // linear light, gain map applied

Encode:

let options = heifer::EncodeOptions::new().with_quality(80);
let heic = heifer::encode_rgba8(width, height, &rgba, &options)?;

Read without decoding pixels:

let info = heifer::read_info(&bytes)?;      // size, bit depth, alpha
let meta = heifer::read_metadata(&bytes)?;  // EXIF, XMP, IPTC, ICC

With the image crate (features = ["image"]):

heifer::image_crate::register_image_decoder_hooks();
let img = image::open("photo.heic")?;

Limit threads and size when decoding untrusted uploads:

let options = heifer::Options::new().with_max_threads(1).with_max_pixels(50_000_000);
let image = heifer::decode_with_options(&bytes, &options)?;

Coming from libheif-rs? See the migration guide.

Support

Supported Not yet
Container grids, overlays, alpha, auxiliary images, unci, clap irot imir, thumbnails, sequence tracks
HEVC Main, Main 10, Main Still Picture, P and B frames, Range Extensions to 16 bits, 4:0:0 to 4:4:4 extended precision
AV1 intra and inter frames, layered images, 8 to 12 bits, 4:0:0 to 4:4:4, every loop filter, film grain, quantizer matrices encoding
Colour BT.601, 709, 2020, full and limited range, matrix and LUT RGB ICC profiles and grey ICC profiles to sRGB CMYK and Lab ICC input
HDR Apple and ISO 21496-1 gain maps, PQ and HLG to linear light or tone-mapped to sRGB tone mapping to an HDR display
Metadata EXIF, XMP, ICC, IPTC, region annotations
Encoding HEVC Main and Main 10, 4:2:0 to lossless, sequences, alpha, gain maps, metadata AV1

Conformance

Check Result
HEVC conformance streams identical to ffmpeg 62 / 62
HEVC streams verified against the reference picture hash 42
AV1 items of the av1-avif test set identical to dav1d 238 / 238
AVIF files identical to dav1d after RGB conversion 180 / 180

Eleven fuzz targets run continuously on CI (3,689+ hours accumulated so far). Corrupted and truncated files return errors, never panics.

Fuzz target CI Local Total
hevc 603 0 603
decode 603 0 603
container 601 0 601
cabac 601 0 601
parallel 597 0 597
encode 416 0 416
info 63 0 63
image_decoder 63 0 63
gain_map 48 0 48
av1 47 0 47
preview 47 0 47
Total 3,689 0 3,689

Performance

Decode time on a 10-core Apple Silicon Mac, against ffmpeg on the same file.

Image heifer 1 thread ffmpeg
Xiaomi, 5 MP 13 ms 64 ms 11 ms
iPhone 13 Pro, 12 MP 19 ms 104 ms 21 ms
8000 × 6000, 48 MP 106 ms 0.62 s 95 ms
Galaxy S24 Ultra, 200 MP 0.19 s 0.88 s fails

Full comparisons with libheif, heic-rs, heic, still265, x265 and Apple are in docs/BENCHMARKS.md.

heifer (0.9) libheif-rs (C/C++) heic-rs (0.1.5) heic (0.1.6)
Safety & toolchain 100 % safe Rust, WASM C/C++ FFI, no WASM Pure Rust Pure Rust
License MIT OR Apache-2.0 LGPL-3.0 / GPL-2.0+ MIT OR Apache-2.0 AGPL-3.0
Scope HEIC dec/enc + .heics + unci + AVIF dec HEIC + AVIF / VVC / AVC HEIC decode HEIC decode
12 MP Apple photo (1t / 10t) 83 ms / 15 ms (decode_rgba8) 378 ms (heif-convert) 107 ms / 19 ms 176 ms / 177 ms
Colours vs Apple (median / worst) 50.2 / 49.3 dB 47.4–50.3 dB 48.1 / 39.7 dB 47.0 / 32.6 dB
HDR gain maps (Apple + ISO) ✓ (decode + encode) ✓ – extraction only

Crates

heifer-cli           the heifer command
heifer               decode, encode, read_info, read_metadata, image integration
├── heifer-isobmff   HEIF and AVIF containers
├── heifer-hevc-dec  HEVC decoder
├── heifer-hevc-enc  HEVC encoder
└── heifer-av1-dec   AV1 decoder

Development

Every command used day to day is in COMMANDS.md.

./scripts/fetch-fixtures.sh
cargo test --workspace --all-features
Conformance, fuzzing, demo and releases
./scripts/fetch-conformance.sh                                # about 23 MB from itu.int
cargo test --release -p heifer-hevc-dec --test conformance    # reference picture hashes
cargo build --release -p heifer-hevc-dec --example decode
./scripts/conformance.py                                      # comparison with ffmpeg

./scripts/fetch-fuzz-corpus.sh                                # nightly and cargo-fuzz
./scripts/fuzz.sh 8h

./scripts/build-demo.sh && python3 -m http.server -d demo/web 8000

git tag -a v0.9.0 -m "0.9.0 — what changed" && git push origin v0.9.0

The tag starts the Release workflow, which creates the GitHub release from the changelog and publishes the crates to crates.io.

Contributions are welcome: see CONTRIBUTING.md. Versioning follows semver; breaking changes only land in a 0.x minor release and are listed first in the changelog.

License

MIT or Apache-2.0, at your option.

HEVC is covered by patents. Heifer is an independent implementation for research and interoperability; users are responsible for patent licensing where they live.

Validation relies on the ITU-T HEVC conformance bitstreams, the AOMedia av1-avif test files, the test files of pillow-heif and Nokia, the fuzzing corpus of libheif, and ffmpeg and dav1d as reference decoders.

About

Pure-Rust HEIC/HEIF decoder & encoder (0 unsafe, 0 C, WASM-ready, image crate)

Topics

Resources

Contributing

Security policy

Stars

9 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages