Raw-rs: Fix naming convention of matrices (#2071)

Fix naming convention of matrices
This commit is contained in:
Elbert Ronnie
2024-10-26 00:39:07 +05:30
committed by GitHub
parent 442937c13f
commit 36fe9bf31b
47 changed files with 54 additions and 59 deletions

View File

@@ -31,7 +31,6 @@ pub fn decode_a100<R: Read + Seek>(ifd: Ifd, file: &mut TiffRead<R>) -> RawImage
camera_white_balance: None,
white_balance: None,
camera_to_rgb: None,
rgb_to_camera: None,
}
}

View File

@@ -55,7 +55,6 @@ pub fn decode<R: Read + Seek>(ifd: Ifd, file: &mut TiffRead<R>) -> RawImage {
camera_white_balance: ifd.white_balance_levels.map(|arr| arr.map(|x| x as f64)),
white_balance: None,
camera_to_rgb: None,
rgb_to_camera: None,
}
}

View File

@@ -63,6 +63,5 @@ pub fn decode<R: Read + Seek>(ifd: Ifd, file: &mut TiffRead<R>) -> RawImage {
camera_white_balance: ifd.white_balance_levels.map(|arr| arr.map(|x| x as f64)),
white_balance: None,
camera_to_rgb: None,
rgb_to_camera: None,
}
}

View File

@@ -39,7 +39,6 @@ pub struct RawImage {
pub camera_white_balance: Option<[f64; 4]>,
pub white_balance: Option<[f64; 4]>,
pub camera_to_rgb: Option<[[f64; 3]; 3]>,
pub rgb_to_camera: Option<[[f64; 3]; 3]>,
}
pub struct Image<T> {

View File

@@ -4,20 +4,20 @@ use build_camera_data::build_camera_data;
pub struct CameraData {
pub black: u16,
pub maximum: u16,
pub camera_to_xyz: [i16; 9],
pub xyz_to_camera: [i16; 9],
}
impl CameraData {
const DEFAULT: CameraData = CameraData {
black: 0,
maximum: 0,
camera_to_xyz: [0; 9],
xyz_to_camera: [0; 9],
};
}
const CAMERA_DATA: [(&str, CameraData); 40] = build_camera_data!();
const XYZ_TO_RGB: [[f64; 3]; 3] = [
const RGB_TO_XYZ: [[f64; 3]; 3] = [
// Matrix:
[0.412453, 0.357580, 0.180423],
[0.212671, 0.715160, 0.072169],
@@ -29,26 +29,26 @@ impl RawImage {
let Some(ref camera_model) = self.camera_model else { return };
let camera_name_needle = camera_model.make.to_owned() + " " + &camera_model.model;
let camera_to_xyz = CAMERA_DATA
let xyz_to_camera = CAMERA_DATA
.iter()
.find(|(camera_name_haystack, _)| camera_name_needle == *camera_name_haystack)
.map(|(_, data)| data.camera_to_xyz.map(|x| (x as f64) / 10_000.));
let Some(camera_to_xyz) = camera_to_xyz else { return };
.map(|(_, data)| data.xyz_to_camera.map(|x| (x as f64) / 10_000.));
let Some(xyz_to_camera) = xyz_to_camera else { return };
let mut camera_to_rgb = [[0.; 3]; 3];
let mut rgb_to_camera = [[0.; 3]; 3];
for i in 0..3 {
for j in 0..3 {
for k in 0..3 {
camera_to_rgb[i][j] += camera_to_xyz[i * 3 + k] * XYZ_TO_RGB[k][j];
rgb_to_camera[i][j] += RGB_TO_XYZ[k][j] * xyz_to_camera[i * 3 + k];
}
}
}
let white_balance_multiplier = camera_to_rgb.map(|x| 1. / x.iter().sum::<f64>());
for (index, row) in camera_to_rgb.iter_mut().enumerate() {
let white_balance_multiplier = rgb_to_camera.map(|x| 1. / x.iter().sum::<f64>());
for (index, row) in rgb_to_camera.iter_mut().enumerate() {
*row = row.map(|x| x * white_balance_multiplier[index]);
}
let rgb_to_camera = transpose(pseudoinverse(camera_to_rgb));
let camera_to_rgb = transpose(pseudoinverse(rgb_to_camera));
let cfa_white_balance_multiplier = if let Some(white_balance) = self.camera_white_balance {
white_balance
@@ -58,7 +58,6 @@ impl RawImage {
self.white_balance = Some(cfa_white_balance_multiplier);
self.camera_to_rgb = Some(camera_to_rgb);
self.rgb_to_camera = Some(rgb_to_camera);
}
}

View File

@@ -2,11 +2,11 @@ use crate::{Pixel, RawImage, CHANNELS_IN_RGB};
impl RawImage {
pub fn convert_to_rgb_fn(&self) -> impl Fn(Pixel) -> [u16; CHANNELS_IN_RGB] {
let Some(rgb_to_camera) = self.rgb_to_camera else { todo!() };
let Some(camera_to_rgb) = self.camera_to_rgb else { todo!() };
move |pixel: Pixel| {
std::array::from_fn(|i| i)
.map(|i| rgb_to_camera[i].iter().zip(pixel.values.iter()).map(|(&coeff, &value)| coeff * value as f64).sum())
.map(|i| camera_to_rgb[i].iter().zip(pixel.values.iter()).map(|(&coeff, &value)| coeff * value as f64).sum())
.map(|x: f64| (x as u16).clamp(0, u16::MAX))
}
}