gamma-table-macros

Crates.iogamma-table-macros
lib.rsgamma-table-macros
version0.1.0
created_at2025-06-07 18:55:20.817866+00
updated_at2025-06-07 18:55:20.817866+00
descriptionA procedural macro for generating gamma correction lookup tables
homepage
repositoryhttps://github.com/liebman/gamma-table-macros
max_upload_size
id1704325
size66,750
(liebman)

documentation

https://docs.rs/gamma-table-macros

README

Gamma Table Macros

Crates.io Documentation License Coverage Status

A Rust procedural macro crate for generating compile-time gamma lookup tables with support for both gamma encoding and gamma correction/decoding.

Overview

This crate provides a proc macro that generates efficient lookup tables for gamma processing at compile time. By default, it uses gamma encoding (input^gamma). It also supports gamma correction/decoding (input^(1/gamma)) when the decoding parameter is set to true. This is particularly useful for graphics applications, LED control, and any scenario where you need fast gamma processing without runtime computation.

Features

  • Compile-time generation: Tables are computed at compile time, resulting in zero runtime overhead
  • Dual gamma modes: Gamma encoding (default) and gamma correction/decoding
  • Flexible parameters: Configurable gamma values, table sizes, entry types, and brightness limits
  • Multiple data types: Support for u8, u16, u32, and u64 entry types
  • Brightness limiting: Optional max_value parameter to cap output brightness

Usage

Add this to your Cargo.toml:

[dependencies]
gamma-table-macros = "0.1.0"

Gamma Encoding Example (Default)

use gamma_table_macros::gamma_table;

// Generate a gamma encoding table (default behavior)
gamma_table! {
    name: GAMMA_ENCODED_TABLE,
    entry_type: u8,
    gamma: 2.2,
    size: 256
}

fn main() {
    let input = 128u8;  // 50% brightness
    let encoded = GAMMA_ENCODED_TABLE[input as usize];
    println!("Input: {}, Gamma encoded: {}", input, encoded);  // Will be darker
}

Gamma Correction/Decoding Example

use gamma_table_macros::gamma_table;

// Generate a gamma correction/decoding table
gamma_table! {
    name: GAMMA_DECODED_TABLE,
    entry_type: u8,
    gamma: 2.2,
    size: 256,
    decoding: true
}

fn main() {
    let input = 128u8;  // 50% brightness
    let corrected = GAMMA_DECODED_TABLE[input as usize];
    println!("Input: {}, Gamma corrected: {}", input, corrected);  // Will be brighter
}

LED Control Example

For LED control where you want to limit maximum brightness:

gamma_table! {
    name: LED_GAMMA_TABLE,
    entry_type: u8,
    gamma: 2.5,
    size: 256,
    max_value: 128  // Limit to 50% max brightness
}

Parameters

  • name (required): The name of the const table to be generated
  • entry_type (required): The unsigned integer type for each entry (u8, u16, u32, u64)
  • gamma (required): The gamma value (positive float)
  • size (required): Number of table entries (minimum 3)
  • max_value (optional): Maximum output value to limit brightness (defaults to size-1)
  • decoding (optional): Use gamma correction/decoding instead of encoding (defaults to false)

Mathematics

Gamma Encoding (Default)

output = (input / max_input)^gamma * max_value

Gamma Correction/Decoding (decoding: true)

output = (input / max_input)^(1/gamma) * max_value

Performance

Since tables are generated at compile time, runtime performance is simply a single array lookup operation - O(1) with no floating-point computation needed.

Examples

Run the examples to see the macro in action:

cargo run --example basic_usage

Testing

Run the test suite:

cargo test

License

Licensed under either of

at your option.

Commit count: 14

cargo fmt