Crate linear_srgb

Crate linear_srgb 

Source
Expand description

Fast linear↔sRGB color space conversion.

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

§Module Organization

  • default - Recommended API with optimal implementations for each use case
  • simd - SIMD-accelerated functions with full control over dispatch
  • scalar - Single-value conversion functions (f32/f64)
  • lut - Lookup table types for custom bit depths

§Quick Start

use linear_srgb::default::{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);

§Batch Processing (SIMD)

For maximum throughput on slices:

use linear_srgb::default::{srgb_to_linear_slice, linear_to_srgb_slice};

let mut values = vec![0.5f32; 10000];
srgb_to_linear_slice(&mut values);  // SIMD-accelerated
linear_to_srgb_slice(&mut values);

§Custom Gamma

For non-sRGB gamma (pure power function without linear segment):

use linear_srgb::default::{gamma_to_linear, linear_to_gamma};

let linear = gamma_to_linear(0.5, 2.2);  // gamma 2.2
let encoded = linear_to_gamma(linear, 2.2);

§LUT-based Conversion

For batch processing with pre-computed lookup tables:

use linear_srgb::default::SrgbConverter;

let conv = SrgbConverter::new();  // Zero-cost, const tables

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

§Choosing the Right API

Use CaseRecommended Function
Single f32 valuedefault::srgb_to_linear
Single u8 valuedefault::srgb_u8_to_linear
f32 slice (in-place)default::srgb_to_linear_slice
u8 slice → f32 slicedefault::srgb_u8_to_linear_slice
Manual SIMD (8 values)default::srgb_to_linear_x8
Inside #[multiversed]default::inline::srgb_to_linear_x8
Custom bit depth LUTlut::LinearTable16

§Feature Flags

  • std (default): Enable std library support
  • unsafe_simd: Enable unsafe optimizations for maximum performance

§no_std Support

This crate is no_std compatible. Disable the std feature:

linear-srgb = { version = "0.2", default-features = false }

Modules§

default
Recommended API with optimal implementations for each use case.
lut
Lookup table types for sRGB conversion.
scalar
Scalar (single-value) conversion functions.
simd
SIMD-accelerated conversion 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).