pluot_zarr 0.1.16

Format-specific Pluot layers for rendering Zarr data
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
603
use serde::{Deserialize, Serialize};
use ome_zarr_metadata::v0_5::{
    Axis, AxisType, AxisUnit, AxisUnitSpace,
};
use pluot_core::layers::bitmask_layer::BitmaskChannelSettings;
use pluot_core::render_traits::{ColorMode, EmphasisCriteria, OpacityMode, SizeMode};

pub fn axis_unit_space_to_coefficient_and_exponent(unit: &AxisUnitSpace) -> (f64, i32) {
    // Returns the coefficient and exponent for converting non-SI units to meters
    // (in scientific notation format where the tuple is `(coefficient, exponent)` meaning `coefficient x 10^exponent` meters)
    // Reference: https://github.com/hms-dbmi/viv/blob/6cf019ac1608242682109ffe93d412103667271d/packages/layers/src/utils.js#L158C1-L181C1
    match unit {
        // SI prefixes with positive exponents (multiples of meter)
        AxisUnitSpace::Yottameter => (1.0, 24),
        AxisUnitSpace::Zettameter => (1.0, 21),
        AxisUnitSpace::Exameter => (1.0, 18),
        AxisUnitSpace::Petameter => (1.0, 15),
        AxisUnitSpace::Terameter => (1.0, 12),
        AxisUnitSpace::Gigameter => (1.0, 9),
        AxisUnitSpace::Megameter => (1.0, 6),
        AxisUnitSpace::Kilometer => (1.0, 3),
        AxisUnitSpace::Hectometer => (1.0, 2),
        // TODO: decameter is not currently part of AxisUnitSpace, but it would be (1.0, 1).
        AxisUnitSpace::Meter => (1.0, 0),
        // SI prefixes with negative exponents (submultiples of meter)
        AxisUnitSpace::Decimeter => (1.0, -1),
        AxisUnitSpace::Centimeter => (1.0, -2),
        AxisUnitSpace::Millimeter => (1.0, -3),
        AxisUnitSpace::Micrometer => (1.0, -6),
        AxisUnitSpace::Nanometer => (1.0, -9),
        AxisUnitSpace::Angstrom => (1.0, -10), // Note: not SI since between -9 to -12 exponents.
        AxisUnitSpace::Picometer => (1.0, -12),
        AxisUnitSpace::Femtometer => (1.0, -15),
        AxisUnitSpace::Attometer => (1.0, -18),
        AxisUnitSpace::Zeptometer => (1.0, -21),
        AxisUnitSpace::Yoctometer => (1.0, -24),
        // Non-SI units with coefficients relative to meter
        AxisUnitSpace::Inch => (2.54, -2),      // 0.0254 m = 2.54 x 10⁻² m
        AxisUnitSpace::Foot => (3.048, -1),     // 0.3048 m = 3.048 x 10⁻¹ m
        AxisUnitSpace::Yard => (9.144, -1),     // 0.9144 m = 9.144 x 10⁻¹ m
        AxisUnitSpace::Mile => (1.609344, 3),   // 1609.344 m = 1.609344 x 10³ m
        AxisUnitSpace::Parsec => (3.0857, 16),  // ~3.0857 x 10¹⁶ m
        // TODO: would it be better to just interpret as meters if unrecognized?
        _ => panic!("Unrecognized AxisUnitSpace unit: {:?}", unit),
    }
}


// These utils are shared between ome_zarr_bitmap_layer and ome_zarr_multiscale_layer,
// so we put them in a separate module to avoid circular dependencies.

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OmeZarrChannelSetting {
    /// Index in the C dimension of the zarr array.
    // TODO: also support specifying channel identifiers by their string name.
    pub c_index: u32,
    /// Min/max window for normalization.
    pub window: (f32, f32),
    /// RGB color as floats in [0.0, 1.0].
    pub color: (f32, f32, f32),
}

// The render settings below are inlined from `BitmaskChannelSettings` rather
// than nested under it, so they default to the same values as they would
// there. These delegate to `BitmaskChannelSettings::default()` instead of
// repeating its literals, so the two cannot drift apart.
fn default_channel_stroked() -> bool {
    BitmaskChannelSettings::default().stroked
}
fn default_channel_filled() -> bool {
    BitmaskChannelSettings::default().filled
}
fn default_channel_stroke_color() -> Option<ColorMode> {
    BitmaskChannelSettings::default().stroke_color
}
fn default_channel_stroke_width() -> Option<SizeMode> {
    BitmaskChannelSettings::default().stroke_width
}
fn default_channel_stroke_opacity() -> Option<OpacityMode> {
    BitmaskChannelSettings::default().stroke_opacity
}
fn default_channel_fill_color() -> Option<ColorMode> {
    BitmaskChannelSettings::default().fill_color
}
fn default_channel_fill_opacity() -> Option<OpacityMode> {
    BitmaskChannelSettings::default().fill_opacity
}
fn default_channel_background_fill_opacity() -> Option<f32> {
    BitmaskChannelSettings::default().background_fill_opacity
}
fn default_channel_background_stroke_opacity() -> Option<f32> {
    BitmaskChannelSettings::default().background_stroke_opacity
}
fn default_channel_background_stroke_width() -> Option<f32> {
    BitmaskChannelSettings::default().background_stroke_width
}
fn default_channel_enable_background_fill_color() -> bool {
    BitmaskChannelSettings::default().enable_background_fill_color
}
fn default_channel_enable_background_stroke_color() -> bool {
    BitmaskChannelSettings::default().enable_background_stroke_color
}
fn default_channel_enable_background_fill_opacity() -> bool {
    BitmaskChannelSettings::default().enable_background_fill_opacity
}
fn default_channel_enable_background_stroke_opacity() -> bool {
    BitmaskChannelSettings::default().enable_background_stroke_opacity
}
fn default_channel_enable_background_stroke_width() -> bool {
    BitmaskChannelSettings::default().enable_background_stroke_width
}

/// Per-channel settings for [`crate::layers::ome_zarr_bitmask_layer::OmeZarrBitmaskLayer`]
/// and [`crate::layers::ome_zarr_bitmask_multiscale_layer::OmeZarrBitmaskMultiscaleLayer`].
/// The bitmask counterpart of [`OmeZarrChannelSetting`]: instead of an
/// intensity window and pseudocolor, carries the [`BitmaskChannelSettings`]
/// (stroked/filled, plus each one's color and opacity) used to render this
/// channel's segmentation mask, inlined alongside `c_index` rather than nested
/// under it.
///
/// Only `c_index` is required -- every render setting falls back to the same
/// default [`BitmaskChannelSettings`] uses.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OmeZarrBitmaskChannelSetting {
    /// Index in the C dimension of the zarr array.
    // TODO: also support specifying channel identifiers by their string name.
    pub c_index: u32,

    /// Whether to stroke the outline of each object in this channel.
    #[serde(default = "default_channel_stroked")]
    pub stroked: bool,

    /// Whether to fill the interior of each object in this channel.
    #[serde(default = "default_channel_filled")]
    pub filled: bool,

    /// How to color each object's outline.
    #[serde(default = "default_channel_stroke_color")]
    pub stroke_color: Option<ColorMode>,

    /// Outline thickness, in the units given by the layer's
    /// `stroke_width_unit_mode`, used when `stroked` is true.
    #[serde(default = "default_channel_stroke_width")]
    pub stroke_width: Option<SizeMode>,

    /// Opacity multiplier for the outline (0.0 to 1.0).
    #[serde(default = "default_channel_stroke_opacity")]
    pub stroke_opacity: Option<OpacityMode>,

    /// How to color each object's interior.
    #[serde(default = "default_channel_fill_color")]
    pub fill_color: Option<ColorMode>,

    /// Opacity multiplier for the interior (0.0 to 1.0).
    #[serde(default = "default_channel_fill_opacity")]
    pub fill_opacity: Option<OpacityMode>,

    /// Criteria AND-ed together to determine the selected ("foreground") /
    /// filtered-in ("background") set of objects in this channel. An empty
    /// list means every object is included.
    #[serde(default)]
    pub selection_criteria: Vec<EmphasisCriteria>,
    #[serde(default)]
    pub filtering_criteria: Vec<EmphasisCriteria>,

    /// Fill/stroke colors used for filter-included, but selection-excluded
    /// ("background") objects in this channel.
    #[serde(default)]
    pub background_fill_color: Option<(u8, u8, u8)>,
    #[serde(default)]
    pub background_stroke_color: Option<(u8, u8, u8)>,

    /// Fill/stroke opacity and stroke width used for filter-included, but
    /// selection-excluded ("background") objects in this channel.
    #[serde(default = "default_channel_background_fill_opacity")]
    pub background_fill_opacity: Option<f32>,
    #[serde(default = "default_channel_background_stroke_opacity")]
    pub background_stroke_opacity: Option<f32>,
    #[serde(default = "default_channel_background_stroke_width")]
    pub background_stroke_width: Option<f32>,

    /// When true, "background" objects in this channel have the fill/stroke
    /// color specified via `background_fill_color`/`background_stroke_color`.
    #[serde(default = "default_channel_enable_background_fill_color")]
    pub enable_background_fill_color: bool,
    #[serde(default = "default_channel_enable_background_stroke_color")]
    pub enable_background_stroke_color: bool,
    /// When true, "background" objects in this channel have the fill/stroke
    /// opacity specified via `background_fill_opacity`/`background_stroke_opacity`.
    #[serde(default = "default_channel_enable_background_fill_opacity")]
    pub enable_background_fill_opacity: bool,
    #[serde(default = "default_channel_enable_background_stroke_opacity")]
    pub enable_background_stroke_opacity: bool,
    /// When true, "background" objects in this channel have the stroke width
    /// specified via `background_stroke_width`.
    #[serde(default = "default_channel_enable_background_stroke_width")]
    pub enable_background_stroke_width: bool,
}

/// Drops `c_index` -- which selects *which* slice of the C dimension to load,
/// not how to render it -- and passes the rest through to the inner
/// [`crate::layers::ome_zarr_bitmask_layer::OmeZarrBitmaskLayer`]'s
/// `BitmaskLayer`.
///
/// Written out field-by-field (no `..Default::default()`) so that a new
/// [`BitmaskChannelSettings`] field fails to compile here until it is either
/// inlined above or deliberately left out.
impl From<&OmeZarrBitmaskChannelSetting> for BitmaskChannelSettings {
    fn from(cs: &OmeZarrBitmaskChannelSetting) -> Self {
        Self {
            stroked: cs.stroked,
            filled: cs.filled,
            stroke_color: cs.stroke_color.clone(),
            stroke_width: cs.stroke_width.clone(),
            stroke_opacity: cs.stroke_opacity.clone(),
            fill_color: cs.fill_color.clone(),
            fill_opacity: cs.fill_opacity.clone(),
            selection_criteria: cs.selection_criteria.clone(),
            filtering_criteria: cs.filtering_criteria.clone(),
            background_fill_color: cs.background_fill_color,
            background_stroke_color: cs.background_stroke_color,
            background_fill_opacity: cs.background_fill_opacity,
            background_stroke_opacity: cs.background_stroke_opacity,
            background_stroke_width: cs.background_stroke_width,
            enable_background_fill_color: cs.enable_background_fill_color,
            enable_background_stroke_color: cs.enable_background_stroke_color,
            enable_background_fill_opacity: cs.enable_background_fill_opacity,
            enable_background_stroke_opacity: cs.enable_background_stroke_opacity,
            enable_background_stroke_width: cs.enable_background_stroke_width,
        }
    }
}

/// Axis-aligned physical rectangle for a tile.
#[derive(Debug, Clone, Copy)]
pub struct PhysicalRect {
    pub x0: f64,
    pub y0: f64,
    pub x1: f64,
    pub y1: f64,
}

impl PhysicalRect {
    /// Returns true if `other` is entirely contained within `self`.
    pub fn contains(&self, other: &PhysicalRect) -> bool {
        self.x0 <= other.x0 && self.x1 >= other.x1 && self.y0 <= other.y0 && self.y1 >= other.y1
    }
}

/// Check if two axis-aligned rects overlap (share any area).
pub fn rects_overlap(a: &PhysicalRect, b: &PhysicalRect) -> bool {
    a.x0 < b.x1 && a.x1 > b.x0 && a.y0 < b.y1 && a.y1 > b.y0
}

/// Compute the bounding box of a set of rects.
pub fn bounding_box(rects: &[&PhysicalRect]) -> PhysicalRect {
    let mut x0 = f64::INFINITY;
    let mut y0 = f64::INFINITY;
    let mut x1 = f64::NEG_INFINITY;
    let mut y1 = f64::NEG_INFINITY;
    for r in rects {
        x0 = x0.min(r.x0);
        y0 = y0.min(r.y0);
        x1 = x1.max(r.x1);
        y1 = y1.max(r.y1);
    }
    PhysicalRect { x0, y0, x1, y1 }
}


/// A single OME-NGFF dimension axis.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub enum OmeDim { T, Z, C, Y, X }

impl OmeDim {
    pub fn as_char(self) -> char {
        match self {
            OmeDim::T => 'T',
            OmeDim::Z => 'Z',
            OmeDim::C => 'C',
            OmeDim::Y => 'Y',
            OmeDim::X => 'X',
        }
    }

    pub fn from_char(c: char) -> Option<Self> {
        match c {
            'T' | 't' => Some(OmeDim::T),
            'Z' | 'z' => Some(OmeDim::Z),
            'C' | 'c' => Some(OmeDim::C),
            'Y' | 'y' => Some(OmeDim::Y),
            'X' | 'x' => Some(OmeDim::X),
            _ => None,
        }
    }
}

impl std::fmt::Display for OmeDim {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_char())
    }
}

/// Ordered list of unique OME-NGFF dimensions, e.g. `[T, Z, C, Y, X]` for `"TZCYX"`.
///
/// Invariants enforced by the constructor:
/// - All elements are unique.
/// - Both `X` and `Y` are present.
/// - At most 5 dimensions (one of each variant).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OmeDimensionOrder(Vec<OmeDim>);

impl OmeDimensionOrder {
    /// Construct from an ordered list of `OmeDim` values.
    /// Panics if invariants are violated.
    pub fn new(dims: Vec<OmeDim>) -> Self {
        assert!(dims.len() <= 5, "OmeDimensionOrder cannot have more than 5 dimensions");

        // Check for duplicates.
        for i in 0..dims.len() {
            for j in (i + 1)..dims.len() {
                assert_ne!(dims[i], dims[j], "Duplicate dimension '{}'", dims[i]);
            }
        }

        // X and Y must both be present.
        assert!(dims.contains(&OmeDim::X), "OmeDimensionOrder must contain X");
        assert!(dims.contains(&OmeDim::Y), "OmeDimensionOrder must contain Y");

        Self(dims)
    }

    /// Returns the number of dimensions.
    pub fn num_dims(&self) -> usize {
        self.0.len()
    }

    /// Returns `true` if the given dimension is present.
    pub fn has_dim(&self, dim: OmeDim) -> bool {
        self.0.contains(&dim)
    }

    /// Returns the index (position in the order) of the given dimension, if present.
    pub fn index_of(&self, dim: OmeDim) -> Option<usize> {
        self.0.iter().position(|&d| d == dim)
    }

    /// Returns a slice of the ordered dimensions.
    pub fn dims(&self) -> &[OmeDim] {
        &self.0
    }
}

impl std::fmt::Display for OmeDimensionOrder {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for d in &self.0 {
            write!(f, "{}", d)?;
        }
        Ok(())
    }
}

impl TryFrom<&str> for OmeDimensionOrder {
    type Error = String;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        let dims: Result<Vec<OmeDim>, _> = s
            .chars()
            .map(|c| OmeDim::from_char(c).ok_or_else(|| format!("Invalid dimension character '{}'", c)))
            .collect();
        let dims = dims?;

        // Reuse new() for invariant checks, converting panics to errors.
        if dims.len() > 5 {
            return Err(format!("Too many dimensions: {}", dims.len()));
        }
        for i in 0..dims.len() {
            for j in (i + 1)..dims.len() {
                if dims[i] == dims[j] {
                    return Err(format!("Duplicate dimension '{}'", dims[i]));
                }
            }
        }
        if !dims.contains(&OmeDim::X) {
            return Err("OmeDimensionOrder must contain X".to_string());
        }
        if !dims.contains(&OmeDim::Y) {
            return Err("OmeDimensionOrder must contain Y".to_string());
        }

        Ok(Self(dims))
    }
}

impl TryFrom<String> for OmeDimensionOrder {
    type Error = String;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        OmeDimensionOrder::try_from(s.as_str())
    }
}

impl From<OmeDimensionOrder> for String {
    fn from(order: OmeDimensionOrder) -> String {
        order.to_string()
    }
}

impl Serialize for OmeDimensionOrder {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(&self.to_string())
    }
}

impl<'de> Deserialize<'de> for OmeDimensionOrder {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = String::deserialize(d)?;
        OmeDimensionOrder::try_from(s.as_str()).map_err(serde::de::Error::custom)
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ome_dim_order_new() {
        let order = OmeDimensionOrder::new(vec![OmeDim::T, OmeDim::Z, OmeDim::C, OmeDim::Y, OmeDim::X]);
        assert_eq!(order.num_dims(), 5);
        assert_eq!(order.index_of(OmeDim::X), Some(4));
        assert_eq!(order.index_of(OmeDim::T), Some(0));
        assert!(order.has_dim(OmeDim::C));
        assert_eq!(order.to_string(), "TZCYX");
    }

    #[test]
    fn test_ome_dim_order_from_str() {
        let order = OmeDimensionOrder::try_from("CZYX").unwrap();
        assert_eq!(order.num_dims(), 4);
        assert_eq!(order.index_of(OmeDim::C), Some(0));
        assert_eq!(order.index_of(OmeDim::Z), Some(1));
        assert_eq!(order.index_of(OmeDim::Y), Some(2));
        assert_eq!(order.index_of(OmeDim::X), Some(3));
        assert!(!order.has_dim(OmeDim::T));
        assert_eq!(order.to_string(), "CZYX");
    }

    #[test]
    fn test_ome_dim_order_lowercase() {
        // Lowercase input is accepted; order is preserved, output is uppercase.
        let order = OmeDimensionOrder::try_from("tczyx").unwrap();
        assert_eq!(order.to_string(), "TCZYX");
    }

    #[test]
    fn test_ome_dim_order_into_string() {
        let order = OmeDimensionOrder::new(vec![OmeDim::C, OmeDim::Y, OmeDim::X]);
        let s: String = order.into();
        assert_eq!(s, "CYX");
    }

    #[test]
    fn test_ome_dim_order_err_no_x() {
        assert!(OmeDimensionOrder::try_from("CY").is_err());
    }

    #[test]
    fn test_ome_dim_order_err_no_y() {
        assert!(OmeDimensionOrder::try_from("CX").is_err());
    }

    #[test]
    fn test_ome_dim_order_err_duplicate() {
        assert!(OmeDimensionOrder::try_from("XYXY").is_err());
    }

    #[test]
    fn test_ome_dim_order_err_invalid_char() {
        assert!(OmeDimensionOrder::try_from("AXY").is_err());
    }

    #[test]
    #[should_panic]
    fn test_ome_dim_order_new_panics_on_duplicate() {
        OmeDimensionOrder::new(vec![OmeDim::X, OmeDim::Y, OmeDim::X]);
    }

    /// The render settings are inlined alongside `c_index` rather than nested
    /// under a `settings` key.
    #[test]
    fn test_bitmask_channel_setting_inlined_fields() {
        let cs: OmeZarrBitmaskChannelSetting = serde_json::from_str(
            r#"{
                "c_index": 3,
                "stroked": true,
                "filled": true,
                "stroke_color": {"color_mode": "UniformRgb", "color_params": [0, 0, 255]},
                "stroke_width": {"size_mode": "UniformSize", "size_params": 2.0},
                "stroke_opacity": {"opacity_mode": "UniformOpacity", "opacity_params": 0.5},
                "fill_color": {"color_mode": "UniformRgb", "color_params": [255, 0, 0]},
                "fill_opacity": {"opacity_mode": "UniformOpacity", "opacity_params": 0.25}
            }"#,
        )
        .unwrap();

        assert_eq!(cs.c_index, 3);
        assert!(cs.stroked);
        assert!(cs.filled);
        assert!(matches!(cs.stroke_color, Some(ColorMode::UniformRgb((0, 0, 255)))));
        assert!(matches!(cs.stroke_width, Some(SizeMode::UniformSize(w)) if w == 2.0));
        assert!(matches!(cs.stroke_opacity, Some(OpacityMode::UniformOpacity(a)) if a == 0.5));
        assert!(matches!(cs.fill_color, Some(ColorMode::UniformRgb((255, 0, 0)))));
        assert!(matches!(cs.fill_opacity, Some(OpacityMode::UniformOpacity(a)) if a == 0.25));

        // Everything but c_index passes through to the inner layer's settings.
        let inner = BitmaskChannelSettings::from(&cs);
        assert!(inner.stroked);
        assert!(inner.filled);
        assert!(matches!(inner.stroke_color, Some(ColorMode::UniformRgb((0, 0, 255)))));
        assert!(matches!(inner.stroke_width, Some(SizeMode::UniformSize(w)) if w == 2.0));
        assert!(matches!(inner.stroke_opacity, Some(OpacityMode::UniformOpacity(a)) if a == 0.5));
        assert!(matches!(inner.fill_color, Some(ColorMode::UniformRgb((255, 0, 0)))));
        assert!(matches!(inner.fill_opacity, Some(OpacityMode::UniformOpacity(a)) if a == 0.25));
    }

    /// Only `c_index` is required; the inlined render settings fall back to
    /// the same defaults `BitmaskChannelSettings` uses.
    ///
    /// Compared as serialized JSON rather than field by field: the render
    /// settings' modes carry `NumericData` and so have no `PartialEq`, and this
    /// way the assertion covers *every* field at once -- an inlined field whose
    /// `#[serde(default = ...)]` stops delegating to
    /// `BitmaskChannelSettings::default()` fails here without the test needing
    /// to name it.
    #[test]
    fn test_bitmask_channel_setting_defaults_match_bitmask_channel_settings() {
        let cs: OmeZarrBitmaskChannelSetting =
            serde_json::from_str(r#"{"c_index": 2}"#).unwrap();
        assert_eq!(cs.c_index, 2);

        let from_defaults = serde_json::to_value(BitmaskChannelSettings::from(&cs)).unwrap();
        let defaults = serde_json::to_value(BitmaskChannelSettings::default()).unwrap();
        assert_eq!(from_defaults, defaults);
    }

    /// `c_index` selects which slice of the C dimension to load, so unlike the
    /// render settings it has no meaningful default.
    #[test]
    fn test_bitmask_channel_setting_requires_c_index() {
        let result: Result<OmeZarrBitmaskChannelSetting, _> =
            serde_json::from_str(r#"{"filled": true}"#);
        assert!(result.is_err());
    }

    #[test]
    fn test_bitmask_channel_setting_serde_roundtrip() {
        let cs = OmeZarrBitmaskChannelSetting {
            c_index: 1,
            stroked: true,
            filled: false,
            stroke_color: Some(ColorMode::UniformRgb((255, 0, 0))),
            stroke_width: Some(SizeMode::UniformSize(3.0)),
            stroke_opacity: Some(OpacityMode::UniformOpacity(0.25)),
            fill_color: None,
            fill_opacity: None,
            selection_criteria: vec![],
            filtering_criteria: vec![],
            background_fill_color: Some((200, 200, 200)),
            background_stroke_color: Some((200, 200, 200)),
            background_fill_opacity: None,
            background_stroke_opacity: None,
            background_stroke_width: None,
            enable_background_fill_color: true,
            enable_background_stroke_color: true,
            enable_background_fill_opacity: false,
            enable_background_stroke_opacity: false,
            enable_background_stroke_width: false,
        };
        let json = serde_json::to_string(&cs).unwrap();
        // Inlined, i.e. no nested `settings` object.
        assert!(!json.contains("settings"), "unexpected nesting in {json}");

        let decoded: OmeZarrBitmaskChannelSetting = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.c_index, 1);
        assert!(decoded.stroked);
        assert!(!decoded.filled);
        assert!(matches!(decoded.stroke_color, Some(ColorMode::UniformRgb((255, 0, 0)))));
        assert!(matches!(decoded.stroke_width, Some(SizeMode::UniformSize(w)) if w == 3.0));
        assert!(matches!(decoded.stroke_opacity, Some(OpacityMode::UniformOpacity(a)) if a == 0.25));
        assert!(decoded.fill_color.is_none());
        assert!(decoded.fill_opacity.is_none());
    }

    #[test]
    fn test_ome_dim_order_serde_roundtrip() {
        let order = OmeDimensionOrder::new(vec![OmeDim::T, OmeDim::C, OmeDim::Z, OmeDim::Y, OmeDim::X]);
        let json = serde_json::to_string(&order).unwrap();
        assert_eq!(json, "\"TCZYX\"");
        let decoded: OmeDimensionOrder = serde_json::from_str(&json).unwrap();
        assert_eq!(order, decoded);
    }
}