linear-srgb 0.2.0

Fast linear↔sRGB color space conversion with FMA acceleration and LUT support
Documentation

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);