fovea-display 0.1.1

Display strategies, GPU texture metadata, and debug windows for fovea images
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! Debug histogram window for quick inspection of [`Histogram`] data.
//!
//! This module provides an `imshow`-style entry point for visualising
//! histograms during development. It is gated behind the `debug-window`
//! feature flag and is **not intended for production use**.
//!
//! Histograms produced by [`fovea::analyze::histogram::histogram()`]
//! carry a `bins()` slice that is strategy-independent — that is the
//! only datum the renderer needs. One or more histograms are
//! rasterised into a small [`Srgba8`](fovea::pixel::Srgba8) bar-chart
//! image and handed off to the same [`Framebuffer`] / event-loop
//! pipeline as [`crate::show`].
//!
//! # Single histogram
//!
//! ```no_run
//! use fovea::analyze::histogram::{histogram, Histogram, NaturalBins};
//! use fovea::image::Image;
//! use fovea::pixel::Mono8;
//! use fovea_display::debug_histogram;
//!
//! let img = Image::<Mono8>::zero(64, 64);
//! let h: Histogram<NaturalBins, _> = histogram(&img, &NaturalBins).unwrap();
//! debug_histogram("Sample histogram", &h);
//! ```
//!
//! # Multiple translucent layers in one window
//!
//! Each layer carries its own colour (with alpha) and its own `bins`
//! slice. All layers in a call share the chart frame, padding, axis,
//! background, and y-scale — so an RGB image's three channels can be
//! overlaid in one plot, or two transforms of the same data (e.g.
//! linear vs. log) can be compared side-on-side.
//!
//! ```no_run
//! use fovea::analyze::histogram::{histogram, Histogram, NaturalBins};
//! use fovea::image::Image;
//! use fovea::pixel::{Rgb8, Srgba8};
//! use fovea_display::{debug_histogram_layers, HistogramLayer, HistogramPlotOptions};
//!
//! let img = Image::<Rgb8>::zero(64, 64);
//! let chans: [Histogram<NaturalBins, _>; 3] =
//!     histogram(&img, &NaturalBins).unwrap();
//!
//! let layers = [
//!     HistogramLayer::new(chans[0].bins(), Srgba8::new(220,  60,  60, 160)),
//!     HistogramLayer::new(chans[1].bins(), Srgba8::new( 60, 200,  80, 160)),
//!     HistogramLayer::new(chans[2].bins(), Srgba8::new( 80, 120, 230, 160)),
//! ];
//! debug_histogram_layers("RGB histogram", &layers, &HistogramPlotOptions::default());
//! ```

use fovea::analyze::histogram::Histogram;
use fovea::image::{Image, ImageViewMut};
use fovea::pixel::Srgba8;

use crate::DisplayContext;
use crate::strategy::{Framebuffer, Identity};

// ═══════════════════════════════════════════════════════════════════════════════
// Plot-level configuration (chart frame, shared by all layers)
// ═══════════════════════════════════════════════════════════════════════════════

/// Frame-level configuration for [`render_histogram_layers`].
///
/// Holds everything that is *shared* between all layers in a single
/// chart: the canvas size, padding, background colour, axis colour,
/// and whether the y-axis is log-scaled.
///
/// Per-layer properties (bin counts and bar colour) live on
/// [`HistogramLayer`].
#[derive(Debug, Clone, Copy)]
pub struct HistogramPlotOptions {
    /// Width of the rendered chart in pixels.
    pub width: u32,
    /// Height of the rendered chart in pixels.
    pub height: u32,
    /// Padding around the plot area, in pixels.
    pub padding: u32,
    /// Background fill colour. Use a translucent value if you intend
    /// to composite the chart over something else later — the
    /// renderer will honour the alpha channel verbatim.
    pub background: Srgba8,
    /// Axis / baseline colour.
    pub axis: Srgba8,
    /// If `true`, every layer's bar heights are scaled by
    /// `log10(1 + count)`. Useful for natural images where a few
    /// bins dominate.
    pub log_scale: bool,
}

impl Default for HistogramPlotOptions {
    fn default() -> Self {
        Self {
            width: 512,
            height: 256,
            padding: 8,
            background: Srgba8::new(24, 24, 28, 255),
            axis: Srgba8::new(96, 96, 104, 255),
            log_scale: false,
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Layer
// ═══════════════════════════════════════════════════════════════════════════════

/// One translucent histogram layer in a multi-layer plot.
///
/// `bins` is borrowed (it's just a slice into a [`Histogram`]'s bin
/// counts) so layers are cheap to construct and keep no ownership of
/// the histogram itself.
///
/// The renderer treats `color.a` as straight (non-premultiplied)
/// alpha. Lower alpha values let underlying layers show through —
/// pick `~120..180` for typical 2–3 layer overlays.
#[derive(Debug, Clone, Copy)]
pub struct HistogramLayer<'a> {
    /// Bin counts to draw, one entry per bar.
    pub bins: &'a [u64],
    /// Bar fill colour (RGBA, straight alpha).
    pub color: Srgba8,
}

impl<'a> HistogramLayer<'a> {
    /// Build a layer from raw bins and a colour.
    #[inline]
    pub fn new(bins: &'a [u64], color: Srgba8) -> Self {
        Self { bins, color }
    }

    /// Build a layer from a [`Histogram`] and a colour.
    ///
    /// Equivalent to `HistogramLayer::new(h.bins(), color)`. Generic
    /// over both the strategy `S` and the channel value type `V`,
    /// because only the strategy-independent `bins()` slice is read.
    #[inline]
    pub fn from_histogram<S, V>(h: &'a Histogram<S, V>, color: Srgba8) -> Self {
        Self::new(h.bins(), color)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Single-histogram convenience: HistogramRenderOptions
// ═══════════════════════════════════════════════════════════════════════════════

/// Visual configuration for the single-histogram entry points.
///
/// Combines [`HistogramPlotOptions`] with one `bar` colour for the
/// classic "render exactly one histogram" use case. Multi-histogram
/// plotting goes through [`HistogramPlotOptions`] +
/// [`HistogramLayer`] directly.
#[derive(Debug, Clone, Copy)]
pub struct HistogramRenderOptions {
    /// Width of the rendered chart in pixels.
    pub width: u32,
    /// Height of the rendered chart in pixels.
    pub height: u32,
    /// Padding around the plot area, in pixels.
    pub padding: u32,
    /// Background fill colour.
    pub background: Srgba8,
    /// Bar fill colour. Alpha is honoured but with one layer it
    /// simply blends against the background.
    pub bar: Srgba8,
    /// Axis / baseline colour.
    pub axis: Srgba8,
    /// If `true`, scale bar heights logarithmically (`log10(1 + count)`).
    pub log_scale: bool,
}

impl Default for HistogramRenderOptions {
    fn default() -> Self {
        Self {
            width: 512,
            height: 256,
            padding: 8,
            background: Srgba8::new(24, 24, 28, 255),
            bar: Srgba8::new(180, 200, 230, 255),
            axis: Srgba8::new(96, 96, 104, 255),
            log_scale: false,
        }
    }
}

impl HistogramRenderOptions {
    /// Split into the frame-level [`HistogramPlotOptions`] and the
    /// single bar colour. Used internally to delegate to the layered
    /// renderer.
    #[inline]
    fn split(&self) -> (HistogramPlotOptions, Srgba8) {
        (
            HistogramPlotOptions {
                width: self.width,
                height: self.height,
                padding: self.padding,
                background: self.background,
                axis: self.axis,
                log_scale: self.log_scale,
            },
            self.bar,
        )
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Renderer
// ═══════════════════════════════════════════════════════════════════════════════

/// Render any number of histogram layers into one [`Image<Srgba8>`].
///
/// All layers share the chart frame and a single y-scale: the maximum
/// (transformed) bin value is taken across *every* layer so relative
/// heights stay comparable. Per-layer bars are alpha-blended into the
/// canvas in the order the slice provides them — the last layer is
/// drawn on top.
///
/// This is the workhorse renderer; [`render_histogram`] delegates here
/// with a single layer.
///
/// # Notes
///
/// - Bin → column mapping uses max-pooling: when columns are narrower
///   than the bin count, several adjacent bins collapse onto one
///   column and the column adopts their max so spikes are preserved.
/// - NaN / underflow / overflow counters are not drawn (they have no
///   in-range bin); callers that care can read those fields off the
///   [`Histogram`].
/// - Blending uses sRGB straight-alpha. This is a debug visualiser,
///   not a colour-managed pipeline — the goal is "translucent layers
///   look translucent", not photometric accuracy.
pub fn render_histogram_layers(
    layers: &[HistogramLayer<'_>],
    opts: &HistogramPlotOptions,
) -> Image<Srgba8> {
    let w = opts.width.max(1) as usize;
    let height_px = opts.height.max(1) as usize;
    let pad = (opts.padding as usize).min(w / 4).min(height_px / 4);

    let mut img = Image::fill(w, height_px, opts.background);

    // Plot rectangle in image coordinates (top-left origin).
    let plot_x0 = pad;
    let plot_y0 = pad;
    let plot_x1 = w.saturating_sub(pad).max(plot_x0 + 1);
    let plot_y1 = height_px.saturating_sub(pad).max(plot_y0 + 1);
    let plot_w = plot_x1 - plot_x0;
    let plot_h = plot_y1 - plot_y0;

    // Baseline (axis).
    let baseline_y = plot_y1.saturating_sub(1);
    for x in plot_x0..plot_x1 {
        *img.pixel_at_mut(x, baseline_y) = opts.axis;
    }

    if layers.is_empty() || plot_w == 0 || plot_h == 0 {
        return img;
    }

    let transform = |c: u64| -> f64 {
        if opts.log_scale {
            (1.0 + c as f64).ln()
        } else {
            c as f64
        }
    };

    // Shared y-scale: max across all layers and all bins. Empty layers
    // contribute 0 and do not affect the scale.
    let mut max_val = 0.0_f64;
    for layer in layers {
        for &c in layer.bins {
            let v = transform(c);
            if v > max_val {
                max_val = v;
            }
        }
    }
    if max_val <= 0.0 {
        return img;
    }

    let usable_h = plot_h.saturating_sub(1); // reserve baseline row
    if usable_h == 0 {
        return img;
    }

    // Draw each layer in order. Later layers blend on top.
    for layer in layers {
        let n = layer.bins.len();
        if n == 0 {
            continue;
        }

        for col in 0..plot_w {
            let lo = (col * n) / plot_w;
            let hi_excl = (((col + 1) * n) / plot_w).max(lo + 1).min(n);

            let mut local_max = 0.0_f64;
            for b in &layer.bins[lo..hi_excl] {
                let v = transform(*b);
                if v > local_max {
                    local_max = v;
                }
            }
            if local_max <= 0.0 {
                continue;
            }

            let bar_h = ((local_max / max_val) * usable_h as f64).round() as usize;
            let bar_h = bar_h.min(usable_h);
            if bar_h == 0 {
                continue;
            }

            let x = plot_x0 + col;
            let top = baseline_y.saturating_sub(bar_h);
            for y in top..baseline_y {
                let dst = img.pixel_at_mut(x, y);
                *dst = blend_over(layer.color, *dst);
            }
        }
    }

    img
}

/// Render a single histogram's bin counts to an [`Image<Srgba8>`].
///
/// Convenience wrapper around [`render_histogram_layers`] for the
/// classic single-bar-colour case. See that function for the rendering
/// model.
pub fn render_histogram<S, V>(h: &Histogram<S, V>, opts: &HistogramRenderOptions) -> Image<Srgba8> {
    let (plot, bar) = opts.split();
    let layers = [HistogramLayer::new(h.bins(), bar)];
    render_histogram_layers(&layers, &plot)
}

// ── Compositing ─────────────────────────────────────────────────────────────

/// Straight-alpha "source over destination" blend in 8-bit sRGB space.
///
/// This is intentionally **not** colour-managed: gamma-correct blending
/// would require linearising both operands. For a debug visualiser the
/// approximation is good enough and keeps everything in the same byte
/// space as the [`Framebuffer`].
#[inline]
fn blend_over(src: Srgba8, dst: Srgba8) -> Srgba8 {
    let sa = src.a.0 as u32;
    if sa == 0 {
        return dst;
    }
    if sa == 255 {
        return src;
    }
    let inv = 255 - sa;

    // Round-to-nearest division by 255 via the classic
    // `(x * 0x8081) >> 23` trick; the simpler `(x + 127) / 255` is
    // plenty fast for the debug path and easier to audit.
    let mix = |s: u8, d: u8| -> u8 {
        let v = (s as u32) * sa + (d as u32) * inv;
        ((v + 127) / 255) as u8
    };

    let da = dst.a.0 as u32;
    let out_a = sa + (da * inv + 127) / 255;
    let out_a = out_a.min(255) as u8;

    Srgba8::new(
        mix(src.r.0, dst.r.0),
        mix(src.g.0, dst.g.0),
        mix(src.b.0, dst.b.0),
        out_a,
    )
}

// ═══════════════════════════════════════════════════════════════════════════════
// Public entry points — single histogram
// ═══════════════════════════════════════════════════════════════════════════════

/// Display a histogram in a debug window using default render options.
///
/// This is the histogram counterpart of [`crate::show`]: it blocks
/// until the user presses a key or closes the window. See
/// [`debug_histogram_with`] for control over the rendered appearance,
/// and [`debug_histogram_layers`] for multi-layer plots.
///
/// # Platform notes
///
/// On macOS, this function **must** be called from the main thread.
/// Internally it goes through the same one-thread-per-process winit
/// event loop used by [`crate::show`], so do not mix this with
/// [`crate::DebugDisplay::run`] in the same process.
pub fn debug_histogram<S, V>(title: &str, h: &Histogram<S, V>) {
    debug_histogram_with(title, h, &HistogramRenderOptions::default());
}

/// Display a histogram in a debug window with custom render options.
pub fn debug_histogram_with<S, V>(title: &str, h: &Histogram<S, V>, opts: &HistogramRenderOptions) {
    let img = render_histogram(h, opts);
    crate::show(title, &img, Identity);
}

// ═══════════════════════════════════════════════════════════════════════════════
// Public entry points — layered histograms
// ═══════════════════════════════════════════════════════════════════════════════

/// Display any number of histogram layers in a single debug window.
///
/// All layers share one chart frame and one y-scale; per-layer colour
/// (with alpha) controls overlay appearance. See
/// [`render_histogram_layers`] for the rendering model and
/// [`crate::show`] for the windowing semantics.
pub fn debug_histogram_layers(
    title: &str,
    layers: &[HistogramLayer<'_>],
    opts: &HistogramPlotOptions,
) {
    let img = render_histogram_layers(layers, opts);
    crate::show(title, &img, Identity);
}

// ═══════════════════════════════════════════════════════════════════════════════
// DisplayContext extension
// ═══════════════════════════════════════════════════════════════════════════════

impl DisplayContext {
    /// Display a histogram inside a [`DebugDisplay::run`](crate::DebugDisplay::run)
    /// session, using default render options.
    ///
    /// Non-blocking. Uses the same window-update semantics as
    /// [`DisplayContext::show`]: passing the same `title` again
    /// updates the existing window in place.
    pub fn show_histogram<S, V>(&self, title: &str, h: &Histogram<S, V>) {
        self.show_histogram_with(title, h, &HistogramRenderOptions::default());
    }

    /// Display a histogram inside a [`DebugDisplay::run`](crate::DebugDisplay::run)
    /// session with custom render options.
    pub fn show_histogram_with<S, V>(
        &self,
        title: &str,
        h: &Histogram<S, V>,
        opts: &HistogramRenderOptions,
    ) {
        let img = render_histogram(h, opts);
        let fb = Framebuffer::from_image(&img, Identity);
        self.show_framebuffer(title, fb);
    }

    /// Display a multi-layer histogram inside a
    /// [`DebugDisplay::run`](crate::DebugDisplay::run) session.
    ///
    /// Non-blocking. See [`render_histogram_layers`] for the
    /// rendering model.
    pub fn show_histogram_layers(
        &self,
        title: &str,
        layers: &[HistogramLayer<'_>],
        opts: &HistogramPlotOptions,
    ) {
        let img = render_histogram_layers(layers, opts);
        let fb = Framebuffer::from_image(&img, Identity);
        self.show_framebuffer(title, fb);
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use fovea::analyze::histogram::{NaturalBins, histogram};
    use fovea::image::ImageView;
    use fovea::pixel::Mono8;

    fn build_hist() -> Histogram<NaturalBins, std::num::Saturating<u8>> {
        // 4×4 image with one pixel at value 0 and the rest at 255.
        let mut img: Image<Mono8> = Image::fill(4, 4, Mono8::new(255));
        *img.pixel_at_mut(0, 0) = Mono8::new(0);
        histogram(&img, &NaturalBins).unwrap()
    }

    // ── Single-histogram renderer ────────────────────────────────────────

    #[test]
    fn render_default_size_matches_options() {
        let h = build_hist();
        let opts = HistogramRenderOptions::default();
        let img = render_histogram(&h, &opts);
        assert_eq!(img.width(), opts.width as usize);
        assert_eq!(img.height(), opts.height as usize);
    }

    #[test]
    fn render_zero_size_padded_does_not_panic() {
        let h = build_hist();
        let opts = HistogramRenderOptions {
            width: 1,
            height: 1,
            padding: 0,
            ..HistogramRenderOptions::default()
        };
        let img = render_histogram(&h, &opts);
        assert_eq!(img.width(), 1);
        assert_eq!(img.height(), 1);
    }

    #[test]
    fn render_paints_background_outside_bars() {
        let h = build_hist();
        let opts = HistogramRenderOptions::default();
        let img = render_histogram(&h, &opts);
        // Top-left corner is well inside the padding region, which is
        // never overwritten by bars.
        assert_eq!(img.pixel_at(0, 0), opts.background);
    }

    #[test]
    fn render_log_scale_runs() {
        let h = build_hist();
        let opts = HistogramRenderOptions {
            log_scale: true,
            ..HistogramRenderOptions::default()
        };
        let _ = render_histogram(&h, &opts);
    }

    #[test]
    fn render_empty_bins_is_safe() {
        let h = build_hist();
        let opts = HistogramRenderOptions {
            width: 4,
            height: 4,
            padding: 2,
            ..HistogramRenderOptions::default()
        };
        let img = render_histogram(&h, &opts);
        assert_eq!(img.width(), 4);
        assert_eq!(img.height(), 4);
    }

    // ── Layered renderer ─────────────────────────────────────────────────

    #[test]
    fn layered_no_layers_yields_background_and_axis_only() {
        let opts = HistogramPlotOptions::default();
        let img = render_histogram_layers(&[], &opts);
        assert_eq!(img.width(), opts.width as usize);
        assert_eq!(img.height(), opts.height as usize);
        // Top-left padding pixel is background.
        assert_eq!(img.pixel_at(0, 0), opts.background);
    }

    #[test]
    fn layered_two_translucent_layers_blend() {
        let h = build_hist();
        let opts = HistogramPlotOptions::default();
        let layers = [
            HistogramLayer::from_histogram(&h, Srgba8::new(255, 0, 0, 128)),
            HistogramLayer::from_histogram(&h, Srgba8::new(0, 0, 255, 128)),
        ];
        let img = render_histogram_layers(&layers, &opts);
        assert_eq!(img.width(), opts.width as usize);
        // No panics, dimensions match.
        assert_eq!(img.height(), opts.height as usize);
    }

    // ── Compositing ──────────────────────────────────────────────────────

    #[test]
    fn blend_over_zero_alpha_is_passthrough() {
        let dst = Srgba8::new(10, 20, 30, 200);
        let src = Srgba8::new(255, 0, 0, 0);
        assert_eq!(blend_over(src, dst), dst);
    }

    #[test]
    fn blend_over_full_alpha_replaces_destination() {
        let dst = Srgba8::new(10, 20, 30, 200);
        let src = Srgba8::new(255, 0, 0, 255);
        assert_eq!(blend_over(src, dst), src);
    }

    #[test]
    fn blend_over_half_alpha_mixes_components() {
        // 50% red over solid black → mid red, fully opaque (since dst
        // alpha is also 255).
        let dst = Srgba8::new(0, 0, 0, 255);
        let src = Srgba8::new(255, 0, 0, 128);
        let out = blend_over(src, dst);
        // 255*128/255 ≈ 128 (with rounding).
        assert!(out.r.0 >= 127 && out.r.0 <= 129, "r = {}", out.r.0);
        assert_eq!(out.g.0, 0);
        assert_eq!(out.b.0, 0);
        assert_eq!(out.a.0, 255);
    }
}