canvas-w33: net change of 1 commits

- canvas-w33: written from its sheet
This commit is contained in:
bflatastic committed 2026-11-21 21:26:32 +07:00
1 parent d3750889fb
commit bda01e8fd3
1 file changed
+81 -11
+81 -11
View File
@@ -23,6 +23,9 @@ pub type DisplayId = Option<u32>;
/// Bytes per conversion chunk of [`DisplayTransform::apply_rgba8`] (a whole number of pixels).
const CHUNK_BYTES: usize = 4 * 65536;
/// Bytes of the stack scratch of [`DisplayTransform::apply_premultiplied_rgba8`] (4096 whole pixels).
const SCRATCH_BYTES: usize = 4 * 4096;
/// Most cached transforms before the cache is cleared.
const MAX_TRANSFORMS: usize = 32;
@@ -67,6 +70,11 @@ fn usable_monitor(profile: Profile) -> Result<Arc<Profile>, MonitorProblem> {
Ok(Arc::new(profile))
}
/// `icc` parsed and validated as a monitor profile.
fn parse_monitor(icc: &[u8]) -> Result<Arc<Profile>, MonitorProblem> {
Profile::parse(icc).map_err(|e| MonitorProblem::Unreadable { reason: e.to_string() }).and_then(usable_monitor)
}
fn srgb_entry(status: MonitorStatus) -> MonitorEntry {
MonitorEntry { profile: Arc::new(Builtin::Srgb.profile().clone()), status }
}
@@ -139,17 +147,48 @@ impl DisplayTransform {
/// Premultiplied RGBA8 pixels converted in place through a fixed scratch of whole pixels:
/// alpha 0 is skipped, partial alpha is divided out and multiplied back, a failed chunk is
/// left as it was; nothing happens for the identity.
// new: the canvas frames hold premultiplied pixels; `display_profile::usable` shares the
// monitor check through `check_monitor`.
pub fn apply_premultiplied_rgba8(&self, pixels: &mut [u8]) {
let _ = pixels;
todo!("cv.F42")
let Some(t) = &self.transform else { return };
let mut scratch = [0u8; SCRATCH_BYTES];
for chunk in pixels.chunks_mut(SCRATCH_BYTES).map(|c| {
let n = c.len() / 4 * 4;
&mut c[..n]
}) {
let Some(src) = scratch.get_mut(..chunk.len()) else { continue };
src.copy_from_slice(chunk);
for px in src.chunks_exact_mut(4) {
let a = u32::from(px[3]);
if a != 0 && a != 255 {
for c in &mut px[..3] {
*c = ((u32::from(*c) * 255 + a / 2) / a).min(255) as u8;
}
}
}
// A failed conversion writes nothing (the buffers are checked first): the chunk stays.
if t.convert_u8(src, 4, chunk, 4, true).is_err() {
continue;
}
for (d, s) in chunk.chunks_exact_mut(4).zip(src.chunks_exact(4)) {
let a = u32::from(d[3]);
if a == 0 {
d.copy_from_slice(s);
} else if a != 255 {
for c in &mut d[..3] {
*c = ((u32::from(*c) * a + 127) / 255) as u8;
}
}
}
}
}
}
/// Whether `icc` is usable as a monitor profile: [`Profile::parse`] plus the monitor checks, a
/// parse error giving [`MonitorProblem::Unreadable`].
// new: `display_profile::usable` in the app tries each profile source until one passes.
pub fn check_monitor(icc: &[u8]) -> Result<(), MonitorProblem> {
let _ = icc;
todo!("cv.F42")
parse_monitor(icc).map(drop)
}
/// Why the monitor profile is what it is.
@@ -231,18 +270,16 @@ impl DisplayCaches {
pub fn set_monitor(&self, display: DisplayId, icc: Option<&[u8]>) {
let entry = match icc {
None => srgb_entry(MonitorStatus::NoProfile),
Some(bytes) => match Profile::parse(bytes) {
Ok(p) => match usable_monitor(p) {
Ok(profile) => MonitorEntry { profile, status: MonitorStatus::Detected },
Err(reason) => srgb_entry(MonitorStatus::Unusable(reason)),
},
Err(e) => {
Some(bytes) => match parse_monitor(bytes) {
Ok(profile) => MonitorEntry { profile, status: MonitorStatus::Detected },
Err(problem @ MonitorProblem::Unreadable { .. }) => {
if let std::collections::hash_map::Entry::Vacant(v) = lock(&self.monitors).entry(display) {
v.insert(srgb_entry(MonitorStatus::Unusable(MonitorProblem::Unreadable { reason: e.to_string() })));
v.insert(srgb_entry(MonitorStatus::Unusable(problem)));
lock(&self.transforms).clear();
}
return;
}
Err(problem) => srgb_entry(MonitorStatus::Unusable(problem)),
},
};
lock(&self.monitors).insert(display, entry);
@@ -325,6 +362,39 @@ mod tests {
Arc::new(Builtin::Srgb.profile().clone())
}
/// Premultiplied conversion agrees with the straight one, leaves alpha and empty pixels alone.
#[trial]
fn a_premultiplied_conversion_matches_the_straight_one() {
let t = DisplayTransform::new(srgb(), p3()).unwrap();
assert!(!t.is_identity());
let colours = [[255u8, 0, 0], [200, 120, 40], [10, 220, 90]];
for c in colours {
let mut straight = [c[0], c[1], c[2], 255];
t.apply_rgba8(&mut straight);
let mut opaque = [c[0], c[1], c[2], 255];
t.apply_premultiplied_rgba8(&mut opaque);
assert_eq!(opaque, straight, "opaque pixels agree");
let a = 128u32;
let mut half = [0, 1, 2].map(|i| ((u32::from(c[i]) * a + 127) / 255) as u8).to_vec();
half.push(a as u8);
t.apply_premultiplied_rgba8(&mut half);
assert_eq!(half[3], a as u8, "alpha stays");
for i in 0..3 {
let want = (u32::from(straight[i]) * a + 127) / 255;
assert!(want.abs_diff(u32::from(half[i])) <= 1, "channel {i}: {} vs {want}", half[i]);
}
}
let mut empty = [9u8, 8, 7, 0];
t.apply_premultiplied_rgba8(&mut empty);
assert_eq!(empty, [9, 8, 7, 0]);
let same = DisplayTransform::new(srgb(), srgb()).unwrap();
let mut px = [50u8, 60, 70, 100, 1, 2, 3, 255];
same.apply_premultiplied_rgba8(&mut px);
assert_eq!(px, [50, 60, 70, 100, 1, 2, 3, 255]);
}
/// Choosing another monitor profile changes the signature and drops the cached transform.
#[trial]
fn choosing_a_monitor_profile_changes_the_signature() {