Struct RGB2Spec

Source
pub struct RGB2Spec { /* private fields */ }
Expand description

A precomputed model used to convert RGB data to a coefficient representation of reflectance spectra.

This crate provides the following methods of instantiating this struct:

The crate also includes a CLI program that can be used to compute a model and save it to a file. Use cargo run in the crate’s root to execute it.

Implementations§

Source§

impl RGB2Spec

Source

pub fn load<P: AsRef<Path>>(path: P) -> Result<RGB2Spec, Error>

Loads a RGB2Spec model from a file.

The binary format is compatible with the original implementation.

Because the binary format doesn’t contain information on which Gamut was used to generate the model there is no interface to retrieve this. When this information is required the user will need to keep track of it manually.

Returns a std::io::Error if the file cannot be opened or does not comply with the format.

Examples found in repository?
examples/load.rs (line 7)
6fn main() {
7    match RGB2Spec::load("examples/out.spec") {
8        Ok(rgb2spec) => {
9            let rgb = [1.0, 0.0, 0.0];
10            let coefficients = rgb2spec.fetch(rgb);
11            println!("Coefficients: {coefficients:?}");
12
13            for i in 0..SAMPLES {
14                let lambda = LAMBDA_MIN + i as f64 / (SAMPLES - 1) as f64 * LAMBDA_RANGE;
15                println!(
16                    "{lambda},{}",
17                    rgb2spec::eval_precise(coefficients, lambda as f32)
18                );
19            }
20        }
21        Err(e) => {
22            println!("Something went wrong: {}", e);
23        }
24    }
25}
Source

pub fn from_reader<R: Read>(reader: &mut R) -> Result<RGB2Spec, Error>

Loads a RGB2Spec model from a reader.

The binary format is compatible with the original implementation.

Because the binary format doesn’t contain information on which Gamut was used to generate the model there is no interface to retrieve this. When this information is required the user will need to keep track of it manually.

Returns a std::io::Error if the reader cannot be read or does not comply with the format.

Source

pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>

Saves the model to a file.

The binary format is compatible with the original implementation.

Returns a std::io::Error if the file cannot be written to.

Source

pub fn to_writer<W: Write>(&self, writer: &mut W) -> Result<(), Error>

Writes the model to a writer.

The binary format is compatible with the original implementation.

Returns a std::io::Error if the writer cannot be written to.

Source

pub fn fetch(&self, rgb: [f32; 3]) -> [f32; 3]

Convert an RGB tuple into a RGB2Spec coefficient representation.

The spectrum that the coefficients represent can then be sampled by passing the coefficents to eval_precise.

Examples found in repository?
examples/load.rs (line 10)
6fn main() {
7    match RGB2Spec::load("examples/out.spec") {
8        Ok(rgb2spec) => {
9            let rgb = [1.0, 0.0, 0.0];
10            let coefficients = rgb2spec.fetch(rgb);
11            println!("Coefficients: {coefficients:?}");
12
13            for i in 0..SAMPLES {
14                let lambda = LAMBDA_MIN + i as f64 / (SAMPLES - 1) as f64 * LAMBDA_RANGE;
15                println!(
16                    "{lambda},{}",
17                    rgb2spec::eval_precise(coefficients, lambda as f32)
18                );
19            }
20        }
21        Err(e) => {
22            println!("Something went wrong: {}", e);
23        }
24    }
25}

Trait Implementations§

Source§

impl Debug for RGB2Spec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.