Crate linear_srgb

Crate linear_srgb 

Source
Expand description

Fast linear↔sRGB color space conversion.

This crate is no_std compatible by default. Enable the std feature if you need std library support.

This crate provides efficient conversion between linear light values and sRGB gamma-encoded values following the IEC 61966-2-1:1999 standard.

§Features

  • Direct computation: Single value conversion with piecewise functions
  • FMA acceleration: Uses hardware FMA when available (x86 FMA, ARM64 NEON)
  • LUT-based conversion: Pre-computed tables for batch processing
  • Multiple precisions: f32, f64, and u8/u16 conversions

§Quick Start

use linear_srgb::{srgb_to_linear, linear_to_srgb};

// Convert sRGB 0.5 to linear
let linear = srgb_to_linear(0.5);
assert!((linear - 0.214).abs() < 0.001);

// Convert back to sRGB
let srgb = linear_to_srgb(linear);
assert!((srgb - 0.5).abs() < 0.001);

§LUT-based Conversion

For batch processing, use SrgbConverter which pre-computes lookup tables:

use linear_srgb::SrgbConverter;

let conv = SrgbConverter::new();

// Fast 8-bit conversions
let linear = conv.srgb_u8_to_linear(128);
let srgb = conv.linear_to_srgb_u8(linear);

§Performance

The implementation uses several optimizations:

  • Piecewise functions avoid pow() for ~1.2% of values in the linear segment
  • Early exit for out-of-range values avoids expensive transcendentals
  • FMA instructions combine multiply+add into single-cycle operations
  • Pre-computed LUTs trade memory for compute time

§Feature Flags

  • fast-math: Use faster but slightly less accurate pow approximation for extended range conversions (affects linear_to_srgb_extended only)

§SIMD Acceleration

For maximum throughput on large batches, use the simd module:

use linear_srgb::simd;

let mut values = vec![0.5f32; 10000];
simd::srgb_to_linear_slice(&mut values);

Re-exports§

pub use lut::EncodeTable8;
pub use lut::EncodeTable12;
pub use lut::EncodeTable16;
pub use lut::EncodingTable;
pub use lut::LinearTable8;
pub use lut::LinearTable10;
pub use lut::LinearTable12;
pub use lut::LinearTable16;
pub use lut::LinearizationTable;
pub use lut::SrgbConverter;
pub use lut::lut_interp_linear_float;
pub use lut::lut_interp_linear_u16;
pub use transfer::linear_to_srgb;
pub use transfer::linear_to_srgb_extended;
pub use transfer::linear_to_srgb_f64;
pub use transfer::linear_to_srgb_u8;
pub use transfer::srgb_to_linear;
pub use transfer::srgb_to_linear_extended;
pub use transfer::srgb_to_linear_f64;
pub use transfer::srgb_u8_to_linear;

Modules§

lut
Lookup table (LUT) based sRGB conversions.
simd
SIMD-accelerated sRGB ↔ linear conversion.
transfer
Core sRGB transfer functions.

Macros§

simd_multiversion
Primary SIMD targets for most functions (x86_64 version).
simd_multiversion_extended
Extended SIMD targets (x86_64 version).
simd_multiversion_full
Full SIMD targets (x86_64 version).

Functions§

linear_to_srgb_slice
Convert a slice of linear f32 values to sRGB in-place.
srgb_to_linear_slice
Convert a slice of sRGB f32 values to linear in-place.