From 939c9bf9c257a50136135942f1bdecabebf518e8 Mon Sep 17 00:00:00 2001 From: liangsheng12333 Date: Thu, 19 Nov 2026 23:09:57 +0800 Subject: [PATCH] fix(scopes): compute waveform luma envelopes by image X column (#82) --- crates/oak-app/src/oakui/scopes.rs | 56 ++++- crates/oak-app/src/panels/program_viewer.rs | 12 + .../src/panels/program_viewer/tests.rs | 11 +- crates/oak-app/src/widgets/scopes/math.rs | 209 +++++++++++++++--- crates/oak-app/src/widgets/scopes/mod.rs | 35 ++- 5 files changed, 270 insertions(+), 53 deletions(-) diff --git a/crates/oak-app/src/oakui/scopes.rs b/crates/oak-app/src/oakui/scopes.rs index 9d304e045e..5f6c7274fc 100644 --- a/crates/oak-app/src/oakui/scopes.rs +++ b/crates/oak-app/src/oakui/scopes.rs @@ -44,6 +44,10 @@ const KB: f32 = 0.0722; /// from the engine's frame cache to a panel copies two pointers. #[derive(Debug, Clone, Default)] pub struct ScopeData { + /// Frame width in pixels. + pub width: u32, + /// Frame height in pixels. + pub height: u32, /// Per-pixel luma in `0..=1` (BT.709), row-major. pub luma: Arc>, /// Per-pixel chroma `(Cb, Cr)` in `0..=1`, centered on `0.5`. @@ -78,6 +82,8 @@ pub(crate) fn analyze_f32_rgba(width: u32, height: u32, samples: &[f32]) -> Scop chroma.push((cb.clamp(0.0, 1.0), cr.clamp(0.0, 1.0))); } ScopeData { + width, + height, luma: Arc::new(luma), chroma: Arc::new(chroma), } @@ -108,6 +114,8 @@ pub(crate) fn analyze_bgra8(width: u32, height: u32, bytes: &[u8]) -> ScopeData chroma.push((cb.clamp(0.0, 1.0), cr.clamp(0.0, 1.0))); } ScopeData { + width, + height, luma: Arc::new(luma), chroma: Arc::new(chroma), } @@ -159,9 +167,8 @@ mod tests { #[test] fn analyzed_samples_feed_the_scope_math() { // A black top half and a white bottom half: the histogram puts every - // sample into the two edge bins, and the waveform envelope (slicing - // the row-major luma stream) rises from black to white across the - // columns. + // sample into the two edge bins, and every X column of the waveform + // spans from black (0.0) to white (1.0). let width = 8u32; let height = 4u32; let mut samples = vec![0.0f32; (width * height * 4) as usize]; @@ -176,14 +183,21 @@ mod tests { } } let data = analyze_f32_rgba(width, height, &samples); + assert_eq!(data.width, width); + assert_eq!(data.height, height); let bins = crate::widgets::scopes::histogram_bins(&data.luma, 4); assert_eq!(bins, vec![16, 0, 0, 16]); - let envelope = crate::widgets::scopes::waveform_envelope(&data.luma, 2); + let envelope = crate::widgets::scopes::waveform_envelope( + &data.luma, + data.width as usize, + data.height as usize, + 2, + ); assert_eq!(envelope.len(), 2); - assert!((envelope[0].0 - 0.0).abs() < 1e-6 && (envelope[0].1 - 0.0).abs() < 1e-6); - assert!((envelope[1].0 - 1.0).abs() < 1e-6 && (envelope[1].1 - 1.0).abs() < 1e-6); + assert!((envelope[0].0 - 0.0).abs() < 1e-6 && (envelope[0].1 - 1.0).abs() < 1e-6); + assert!((envelope[1].0 - 0.0).abs() < 1e-6 && (envelope[1].1 - 1.0).abs() < 1e-6); // Neutral gray chroma projects to the vectorscope's center. let points = crate::widgets::scopes::vectorscope_points(&data.chroma); @@ -193,4 +207,34 @@ mod tests { .all(|&(u, v)| u.abs() < 1e-6 && v.abs() < 1e-6) ); } + + #[test] + fn left_right_split_keeps_waveform_columns_distinct() { + let width = 8u32; + let height = 4u32; + let mut bytes = vec![0u8; (width * height * 4) as usize]; + for y in 0..height { + for x in 0..width { + let i = ((y * width + x) * 4) as usize; + let v = if x >= width / 2 { 255u8 } else { 0u8 }; + bytes[i] = v; + bytes[i + 1] = v; + bytes[i + 2] = v; + bytes[i + 3] = 255; + } + } + let data = analyze_bgra8(width, height, &bytes); + assert_eq!(data.width, width); + assert_eq!(data.height, height); + + let envelope = crate::widgets::scopes::waveform_envelope( + &data.luma, + data.width as usize, + data.height as usize, + 2, + ); + assert_eq!(envelope.len(), 2); + assert!((envelope[0].0 - 0.0).abs() < 1e-6 && (envelope[0].1 - 0.0).abs() < 1e-6); + assert!((envelope[1].0 - 1.0).abs() < 1e-6 && (envelope[1].1 - 1.0).abs() < 1e-6); + } } diff --git a/crates/oak-app/src/panels/program_viewer.rs b/crates/oak-app/src/panels/program_viewer.rs index 486dcde5b1..42b88e1657 100644 --- a/crates/oak-app/src/panels/program_viewer.rs +++ b/crates/oak-app/src/panels/program_viewer.rs @@ -60,6 +60,10 @@ enum ProgramViewTab { /// the displayed frame changes. The scope widgets read this entity through /// the [`LumaDataSource`] / [`ChromaDataSource`] traits. struct ScopeState { + /// Frame width in pixels. + width: u32, + /// Frame height in pixels. + height: u32, /// Per-pixel luma of the current frame (`0..=1`). luma: Vec, /// Per-pixel chroma `(Cb, Cr)` of the current frame (`0..=1`). @@ -70,6 +74,10 @@ impl LumaDataSource for ScopeState { fn luma_samples(&self) -> Vec { self.luma.clone() } + + fn luma_dimensions(&self) -> (usize, usize) { + (self.width as usize, self.height as usize) + } } impl ChromaDataSource for ScopeState { @@ -222,6 +230,8 @@ impl ProgramViewerPanel { let context_menu = ContextMenuHandle::new(Self::on_local_menu_item, window, cx); let scope_state = cx.new(|_cx| ScopeState { + width: 0, + height: 0, luma: Vec::new(), chroma: Vec::new(), }); @@ -393,6 +403,8 @@ impl ProgramViewerPanel { self.scope_frame = Some(frame.clone()); let scope = self.engine.read(cx).scope_data(Monitor::Program, cx); self.scope_state.update(cx, |state, cx| { + state.width = scope.width; + state.height = scope.height; state.luma = (*scope.luma).clone(); state.chroma = (*scope.chroma).clone(); cx.notify(); diff --git a/crates/oak-app/src/panels/program_viewer/tests.rs b/crates/oak-app/src/panels/program_viewer/tests.rs index 7e7df4b656..2f784d3eb0 100644 --- a/crates/oak-app/src/panels/program_viewer/tests.rs +++ b/crates/oak-app/src/panels/program_viewer/tests.rs @@ -378,17 +378,22 @@ async fn scopes_tab_renders_from_the_current_frame(cx: &mut TestAppContext) { window.draw(cx).clear(cx); }); - let (luma_len, chroma_len, bins, envelope) = cx.read(|app| { + let (scope_width, scope_height, luma_len, chroma_len, bins, envelope) = cx.read(|app| { let panel = panel.read(app); + let state = panel.scope_state.read(app); ( - panel.scope_state.read(app).luma.len(), - panel.scope_state.read(app).chroma.len(), + state.width, + state.height, + state.luma.len(), + state.chroma.len(), panel.histogram.read(app).bins(app), panel.waveform.read(app).envelope(app), ) }); let pixels = (crate::oakui::frames::SYNTH_FRAME_WIDTH * crate::oakui::frames::SYNTH_FRAME_HEIGHT) as usize; + assert_eq!(scope_width, crate::oakui::frames::SYNTH_FRAME_WIDTH); + assert_eq!(scope_height, crate::oakui::frames::SYNTH_FRAME_HEIGHT); assert_eq!(luma_len, pixels); assert_eq!(chroma_len, pixels); assert_eq!(bins.iter().sum::() as usize, pixels); diff --git a/crates/oak-app/src/widgets/scopes/math.rs b/crates/oak-app/src/widgets/scopes/math.rs index e79661a356..ac40f1044f 100644 --- a/crates/oak-app/src/widgets/scopes/math.rs +++ b/crates/oak-app/src/widgets/scopes/math.rs @@ -22,31 +22,52 @@ pub fn histogram_bins(samples: &[f32], bins: usize) -> Vec { out } -/// Compute the min/max envelope of `samples` over `columns` vertical slices. -/// Each column covers a contiguous slice of the input; empty columns report -/// `(0.0, 0.0)`. -pub fn waveform_envelope(samples: &[f32], columns: usize) -> Vec<(f32, f32)> { +/// Compute the min/max luma envelope across `columns` output columns for a +/// row-major `width * height` frame. Each output column aggregates every `Y` +/// row at its corresponding image `X` columns; empty frames, zero dimensions, +/// and non-finite samples fall back safely to `(0.0, 0.0)`. +pub fn waveform_envelope( + samples: &[f32], + width: usize, + height: usize, + columns: usize, +) -> Vec<(f32, f32)> { let mut out = vec![(0.0f32, 0.0f32); columns.max(1)]; - if columns == 0 || samples.is_empty() { + if columns == 0 || width == 0 || height == 0 || samples.is_empty() { return out; } - for column in 0..columns { - let start = column * samples.len() / columns; - let end = ((column + 1) * samples.len() / columns) - .max(start + 1) - .min(samples.len()); - let mut min = f32::MAX; - let mut max = f32::MIN; - let mut has_finite = false; - for &sample in &samples[start..end] { - if sample.is_finite() { - min = min.min(sample); - max = max.max(sample); - has_finite = true; + + let ranges: Vec<(usize, usize)> = (0..columns) + .map(|column| { + let x_start = ((column as u128 * width as u128) / columns as u128) as usize; + let x_end = (((column as u128 + 1) * width as u128) / columns as u128) + .max(x_start as u128 + 1) + .min(width as u128) as usize; + (x_start, x_end) + }) + .collect(); + + let mut acc = vec![(f32::MAX, f32::MIN, false); columns]; + for row in samples.chunks(width).take(height) { + for (column, &(x_start, x_end)) in ranges.iter().enumerate() { + let row_end = x_end.min(row.len()); + if x_start >= row_end { + continue; + } + let (min, max, has_finite) = &mut acc[column]; + for &sample in &row[x_start..row_end] { + if sample.is_finite() { + *min = min.min(sample); + *max = max.max(sample); + *has_finite = true; + } } } - if end > start && has_finite { - out[column] = (min, max); + } + + for (dst, (min, max, has_finite)) in out.iter_mut().zip(acc) { + if has_finite { + *dst = (min, max); } } out @@ -118,16 +139,129 @@ mod tests { } #[test] - fn waveform_envelope_slices() { - let samples: Vec = (0..100).map(|i| i as f32 / 100.0).collect(); - let env = waveform_envelope(&samples, 10); - assert_eq!(env.len(), 10); - // First column covers [0, 10): min 0.0, max 0.09. - assert!((env[0].0 - 0.0).abs() < 0.001); - assert!((env[0].1 - 0.09).abs() < 0.001); - // Last column covers [90, 100): min 0.9, max 0.99. - assert!((env[9].0 - 0.9).abs() < 0.001); - assert!((env[9].1 - 0.99).abs() < 0.001); + fn waveform_envelope_by_x_column_cases() { + struct Case { + name: &'static str, + width: usize, + height: usize, + columns: usize, + samples: Vec, + expected: Vec<(f32, f32)>, + } + + let cases = [ + Case { + name: "black top half, white bottom half spans (0, 1) in every X column", + width: 4, + height: 2, + columns: 4, + samples: vec![ + 0.0, 0.0, 0.0, 0.0, // y = 0 + 1.0, 1.0, 1.0, 1.0, // y = 1 + ], + expected: vec![(0.0, 1.0); 4], + }, + Case { + name: "black left half, white right half keeps columns distinct", + width: 4, + height: 2, + columns: 2, + samples: vec![ + 0.0, 0.0, 1.0, 1.0, // y = 0 + 0.0, 0.0, 1.0, 1.0, // y = 1 + ], + expected: vec![(0.0, 0.0), (1.0, 1.0)], + }, + Case { + name: "per-column gradients aggregate vertically by X", + width: 3, + height: 2, + columns: 3, + samples: vec![ + 0.1, 0.5, 0.9, // y = 0 + 0.2, 0.5, 0.8, // y = 1 + ], + expected: vec![(0.1, 0.2), (0.5, 0.5), (0.8, 0.9)], + }, + Case { + name: "non-divisible width partitions X columns cleanly", + width: 5, + height: 2, + columns: 2, + // X partitions: [0..2] -> (0.1, 0.3), [2..5] -> (0.6, 0.9) + samples: vec![ + 0.1, 0.2, 0.6, 0.7, 0.9, // y = 0 + 0.3, 0.2, 0.8, 0.6, 0.7, // y = 1 + ], + expected: vec![(0.1, 0.3), (0.6, 0.9)], + }, + Case { + name: "tiny image narrower than output columns repeats nearest X columns", + width: 2, + height: 2, + columns: 4, + samples: vec![ + 0.2, 0.8, // y = 0 + 0.4, 0.9, // y = 1 + ], + expected: vec![(0.2, 0.4), (0.2, 0.4), (0.8, 0.9), (0.8, 0.9)], + }, + Case { + name: "1x1 tiny image expands safely across columns", + width: 1, + height: 1, + columns: 3, + samples: vec![0.42], + expected: vec![(0.42, 0.42); 3], + }, + Case { + name: "empty frame returns zeroed envelopes", + width: 4, + height: 2, + columns: 3, + samples: vec![], + expected: vec![(0.0, 0.0); 3], + }, + Case { + name: "zero columns returns single zeroed fallback slot", + width: 2, + height: 2, + columns: 0, + samples: vec![0.5; 4], + expected: vec![(0.0, 0.0)], + }, + Case { + name: "zero image width or height returns zeroed envelopes", + width: 0, + height: 2, + columns: 3, + samples: vec![0.5; 4], + expected: vec![(0.0, 0.0); 3], + }, + Case { + name: "short buffer does not panic and aggregates available pixels", + width: 4, + height: 2, + columns: 2, + samples: vec![0.1, 0.2, 0.7, 0.9, 0.3], + expected: vec![(0.1, 0.3), (0.7, 0.9)], + }, + ]; + + for case in cases { + let env = waveform_envelope(&case.samples, case.width, case.height, case.columns); + assert_eq!(env.len(), case.expected.len(), "case: {}", case.name); + for (col, (&got, &want)) in env.iter().zip(case.expected.iter()).enumerate() { + assert!( + (got.0 - want.0).abs() < 1e-6 && (got.1 - want.1).abs() < 1e-6, + "case '{}' column {}: got {:?}, expected {:?}", + case.name, + col, + got, + want + ); + } + } } #[test] @@ -163,13 +297,18 @@ mod tests { #[test] fn waveform_envelope_handles_nan_and_inf() { - let samples = [f32::NAN, f32::INFINITY, -f32::INFINITY]; - let env = waveform_envelope(&samples, 1); + let samples = [f32::NAN, f32::INFINITY, -f32::INFINITY, f32::NAN]; + let env = waveform_envelope(&samples, 2, 2, 1); assert_eq!(env, vec![(0.0, 0.0)]); - let mixed = [f32::NAN, 0.2, 0.8, f32::INFINITY]; - let env_mixed = waveform_envelope(&mixed, 1); - assert_eq!(env_mixed, vec![(0.2, 0.8)]); + let mixed = [ + f32::NAN, + 0.2, // y = 0 + 0.8, + f32::INFINITY, // y = 1 + ]; + let env_mixed = waveform_envelope(&mixed, 2, 2, 2); + assert_eq!(env_mixed, vec![(0.8, 0.8), (0.2, 0.2)]); } #[test] diff --git a/crates/oak-app/src/widgets/scopes/mod.rs b/crates/oak-app/src/widgets/scopes/mod.rs index dc6095e042..fae6a992e3 100644 --- a/crates/oak-app/src/widgets/scopes/mod.rs +++ b/crates/oak-app/src/widgets/scopes/mod.rs @@ -16,8 +16,11 @@ use gpui_kit::{ /// Provides luma samples (`0..1`) for the histogram and waveform scopes. pub trait LumaDataSource: 'static { - /// Luma samples for the current frame. + /// Luma samples for the current frame, row-major. fn luma_samples(&self) -> Vec; + + /// Dimensions `(width, height)` of the row-major frame in [`Self::luma_samples`]. + fn luma_dimensions(&self) -> (usize, usize); } /// Provides chroma samples for the vectorscope. @@ -126,12 +129,14 @@ impl Waveform { /// The current envelope columns, one `(min, max)` luma pair per column. /// - /// `cx` provides the data source; the frame's samples are split into 128 - /// equal runs and each run is reduced to its min and max in `0..1`. Both - /// values are 0 for an empty source or an empty run, so the render pass - /// can draw every column unconditionally. + /// `cx` provides the data source; the frame's samples are grouped by image + /// `X` column into 128 output columns and each column is reduced to its min + /// and max in `0..1`. Both values are 0 for an empty source or an empty + /// frame, so the render pass can draw every column unconditionally. pub fn envelope(&self, cx: &App) -> Vec<(f32, f32)> { - waveform_envelope(&self.data.read(cx).luma_samples(), 128) + let data = self.data.read(cx); + let (width, height) = data.luma_dimensions(); + waveform_envelope(&data.luma_samples(), width, height, 128) } } @@ -260,10 +265,18 @@ mod tests { use super::*; use gpui_kit::{Entity, Render, TestAppContext, Window, div, px, size}; - struct MockLuma(Vec); + struct MockLuma { + width: usize, + height: usize, + samples: Vec, + } impl LumaDataSource for MockLuma { fn luma_samples(&self) -> Vec { - self.0.clone() + self.samples.clone() + } + + fn luma_dimensions(&self) -> (usize, usize) { + (self.width, self.height) } } struct MockChroma(Vec<(f32, f32)>); @@ -293,7 +306,11 @@ mod tests { use gpui_kit::VisualTestContext; cx.update(|cx| cx.init_colors()); let window = cx.open_window(size(px(300.0), px(200.0)), |window, cx| { - let luma = cx.new(|_| MockLuma((0..100).map(|i| i as f32 / 100.0).collect())); + let luma = cx.new(|_| MockLuma { + width: 10, + height: 10, + samples: (0..100).map(|i| i as f32 / 100.0).collect(), + }); let chroma = cx.new(|_| MockChroma(vec![(0.5, 0.5), (0.75, 0.25), (0.25, 0.75)])); let histogram = cx.new(|cx| Histogram::new(1, luma.clone(), window, cx)); let waveform = cx.new(|cx| Waveform::new(2, luma.clone(), window, cx));