Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 50 additions & 6 deletions crates/oak-app/src/oakui/scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<f32>>,
/// Per-pixel chroma `(Cb, Cr)` in `0..=1`, centered on `0.5`.
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -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];
Expand All @@ -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);
Expand All @@ -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);
}
}
12 changes: 12 additions & 0 deletions crates/oak-app/src/panels/program_viewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f32>,
/// Per-pixel chroma `(Cb, Cr)` of the current frame (`0..=1`).
Expand All @@ -70,6 +74,10 @@ impl LumaDataSource for ScopeState {
fn luma_samples(&self) -> Vec<f32> {
self.luma.clone()
}

fn luma_dimensions(&self) -> (usize, usize) {
(self.width as usize, self.height as usize)
}
}

impl ChromaDataSource for ScopeState {
Expand Down Expand Up @@ -222,6 +230,8 @@ impl<E: AppEngine> ProgramViewerPanel<E> {
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(),
});
Expand Down Expand Up @@ -393,6 +403,8 @@ impl<E: AppEngine> ProgramViewerPanel<E> {
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();
Expand Down
11 changes: 8 additions & 3 deletions crates/oak-app/src/panels/program_viewer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32>() as usize, pixels);
Expand Down
Loading
Loading