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:
- Using RGB2Spec::load to load the model from a file
- Using optimize to compute the model (slow)
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
impl RGB2Spec
Sourcepub fn load<P: AsRef<Path>>(path: P) -> Result<RGB2Spec, Error>
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?
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}
Sourcepub fn from_reader<R: Read>(reader: &mut R) -> Result<RGB2Spec, Error>
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.
Sourcepub fn save<P: AsRef<Path>>(&self, path: P) -> Result<(), Error>
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.
Sourcepub fn to_writer<W: Write>(&self, writer: &mut W) -> Result<(), Error>
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.
Sourcepub fn fetch(&self, rgb: [f32; 3]) -> [f32; 3]
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?
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}