1
0
Fork 0
forked from wry/wry

cmm: store render intent

This commit is contained in:
Julian Orth 2026-03-29 15:34:53 +02:00
parent a4928d8ed6
commit dca0df2555
16 changed files with 207 additions and 60 deletions

View file

@ -5,6 +5,7 @@ use {
cmm_luminance::{Luminance, TargetLuminance, white_balance},
cmm_manager::Shared,
cmm_primaries::{NamedPrimaries, Primaries},
cmm_render_intent::RenderIntent,
cmm_transform::{ColorMatrix, Local, Xyz, bradford_adjustment},
},
utils::ordered_float::F64,
@ -39,12 +40,17 @@ pub struct ColorDescription {
}
impl LinearColorDescription {
pub fn color_transform(&self, target: &Self) -> ColorMatrix {
pub fn color_transform(&self, target: &Self, intent: RenderIntent) -> ColorMatrix {
let mut mat = target.local_from_xyz;
if self.luminance != target.luminance {
mat *= white_balance(&self.luminance, &target.luminance, target.primaries.wp);
mat *= white_balance(
&self.luminance,
&target.luminance,
target.primaries.wp,
intent,
);
}
if self.primaries.wp != target.primaries.wp {
if self.primaries.wp != target.primaries.wp && intent.bradford_adjustment() {
mat *= bradford_adjustment(self.primaries.wp, target.primaries.wp);
}
mat * self.xyz_from_local

View file

@ -1,5 +1,8 @@
use crate::{
cmm::cmm_transform::{ColorMatrix, Xyz},
cmm::{
cmm_render_intent::RenderIntent,
cmm_transform::{ColorMatrix, Xyz},
},
utils::ordered_float::F64,
};
@ -68,12 +71,19 @@ impl Default for Luminance {
}
#[expect(non_snake_case)]
pub fn white_balance(from: &Luminance, to: &Luminance, w_to: (F64, F64)) -> ColorMatrix<Xyz, Xyz> {
pub fn white_balance(
from: &Luminance,
to: &Luminance,
w_to: (F64, F64),
intent: RenderIntent,
) -> ColorMatrix<Xyz, Xyz> {
let a = ((from.max - from.min) / (to.max - to.min) * (to.white - to.min)
/ (from.white - from.min))
.0;
// let d = ((from.min - to.min) / (to.max - to.min)).0.max(0.0);
let d = 0.0;
let d = match intent.black_point_compensation() {
true => 0.0,
false => ((from.min - to.min) / (to.max - to.min)).0,
};
let s = a - d;
let (F64(x_to), F64(y_to)) = w_to;
let X_to = x_to / y_to;

View file

@ -0,0 +1,29 @@
use crate::{ifs::color_management::RENDER_INTENT_PERCEPTUAL, object::Version};
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Default)]
pub enum RenderIntent {
#[default]
Perceptual,
}
impl RenderIntent {
pub fn from_wayland(intent: u32, _version: Version) -> Option<Self> {
let res = match intent {
RENDER_INTENT_PERCEPTUAL => Self::Perceptual,
_ => return None,
};
Some(res)
}
pub fn black_point_compensation(self) -> bool {
match self {
RenderIntent::Perceptual => true,
}
}
pub fn bradford_adjustment(self) -> bool {
match self {
RenderIntent::Perceptual => true,
}
}
}

View file

@ -136,7 +136,7 @@ mod matrices {
mod transforms {
use crate::cmm::{
cmm_eotf::Eotf, cmm_luminance::Luminance, cmm_manager::ColorManager,
cmm_primaries::Primaries,
cmm_primaries::Primaries, cmm_render_intent::RenderIntent,
};
fn check(p1: Primaries, p2: Primaries, expected: [[f64; 4]; 3]) {
@ -155,7 +155,9 @@ mod transforms {
};
let d1 = d(p1);
let d2 = d(p2);
let m = d1.linear.color_transform(&d2.linear);
let m = d1
.linear
.color_transform(&d2.linear, RenderIntent::Perceptual);
println!("{:#?}", m);
assert!((m.0[0][0].0 - expected[0][0]).abs() < 0.001);
assert!((m.0[0][1].0 - expected[0][1]).abs() < 0.001);