bioformats 0.1.3

Pure Rust reimplementation of Bio-Formats — read/write scientific image formats
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! Norpix StreamPix SEQ and IPLab format readers.

use std::collections::HashMap;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};

use crate::common::error::{BioFormatsError, Result};
use crate::common::metadata::{DimensionOrder, ImageMetadata, MetadataValue};
use crate::common::pixel_type::PixelType;
use crate::common::reader::FormatReader;
use crate::common::region::crop_full_plane;

fn r_u32_le(b: &[u8], off: usize) -> u32 {
    u32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]])
}
fn r_i32_le(b: &[u8], off: usize) -> i32 {
    i32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]])
}

fn positive_i32_dim(value: i32, label: &str) -> Result<u32> {
    if value <= 0 {
        return Err(BioFormatsError::UnsupportedFormat(format!(
            "IPLab {label} is non-positive ({value})"
        )));
    }
    Ok(value as u32)
}

fn positive_u32_seq_dim(value: u32, label: &str) -> Result<u32> {
    if value == 0 {
        return Err(BioFormatsError::UnsupportedFormat(format!(
            "Norpix SEQ {label} is non-positive"
        )));
    }
    Ok(value)
}

/// Read a StreamPix per-frame timestamp at `off`: u32 seconds since the Unix
/// epoch followed by u16 milliseconds and u16 microseconds. Returns seconds as
/// f64, or 0.0 if the timestamp lies past EOF.
fn read_seq_timestamp(f: &mut File, off: u64, file_len: u64) -> f64 {
    if off + 8 > file_len {
        return 0.0;
    }
    if f.seek(SeekFrom::Start(off)).is_err() {
        return 0.0;
    }
    let mut buf = [0u8; 8];
    if f.read_exact(&mut buf).is_err() {
        return 0.0;
    }
    let secs = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]) as f64;
    let millis = u16::from_le_bytes([buf[4], buf[5]]) as f64;
    let micros = u16::from_le_bytes([buf[6], buf[7]]) as f64;
    secs + millis / 1_000.0 + micros / 1_000_000.0
}

fn printable_ascii(bytes: &[u8]) -> String {
    let end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
    String::from_utf8_lossy(&bytes[..end]).trim().to_string()
}

// ─── Norpix StreamPix SEQ ─────────────────────────────────────────────────────
//
// StreamPix .seq files have a 1024-byte header with the following layout:
//   Offset   0: Description (24 bytes), often "Norpix seq\0..."
//   Offset  24: Version (i64)
//   Offset  32: Header size (i32)
//   Offset 548: Allocated frames (u32)
//   Offset 572: True image size (u32) = width * height * bytes_per_pixel
//   Offset 592: Description format (u32): 0=mono8, 1=mono16, 2=color24, 100=jpg
//   Offset 596: Width (u32)
//   Offset 600: Height (u32)
//   Offset 604: Bit depth (u32) — bits per pixel (8 or 16)
//   Offset 612: Compression (u32): 0=uncompressed
//
// Pixel data starts at offset 1024.
// Each frame may be preceded by a 4-byte offset table if indexed,
// but for uncompressed data frames are tightly packed.

pub struct NorpixReader {
    path: Option<PathBuf>,
    meta: Option<ImageMetadata>,
    frame_size: usize,
    /// True when frames are JPEG-compressed (description format 100/102).
    compressed: bool,
    /// Per-frame absolute byte offsets of the image payload (excludes the
    /// trailing 8/10-byte timestamp). Empty for the uncompressed fast path.
    frame_offsets: Vec<u64>,
    /// Per-frame timestamps in seconds since the Unix epoch.
    timestamps: Vec<f64>,
}

impl NorpixReader {
    pub fn new() -> Self {
        NorpixReader {
            path: None,
            meta: None,
            frame_size: 0,
            compressed: false,
            frame_offsets: Vec::new(),
            timestamps: Vec::new(),
        }
    }
}
impl Default for NorpixReader {
    fn default() -> Self {
        Self::new()
    }
}

impl FormatReader for NorpixReader {
    fn is_this_type_by_name(&self, path: &Path) -> bool {
        path.extension()
            .and_then(|e| e.to_str())
            .map(|e| e.eq_ignore_ascii_case("seq"))
            .unwrap_or(false)
    }

    fn is_this_type_by_bytes(&self, header: &[u8]) -> bool {
        if header.len() < 24 {
            return false;
        }
        // Check description starts with "Norpix seq"
        let desc = std::str::from_utf8(&header[..24]).unwrap_or("");
        desc.starts_with("Norpix seq") || desc.starts_with("Norpix SEQ")
    }

    fn set_id(&mut self, path: &Path) -> Result<()> {
        self.close()?;
        let mut f = File::open(path).map_err(BioFormatsError::Io)?;
        let mut hdr = vec![0u8; 1024];
        f.read_exact(&mut hdr).map_err(BioFormatsError::Io)?;

        let n_frames = positive_u32_seq_dim(r_u32_le(&hdr, 548), "frame count")?;
        let true_image_size = r_u32_le(&hdr, 572);
        let desc_fmt = r_u32_le(&hdr, 592);
        let width = positive_u32_seq_dim(r_u32_le(&hdr, 596), "width")?;
        let height = positive_u32_seq_dim(r_u32_le(&hdr, 600), "height")?;
        // StreamPix description-format codes: 0=mono8, 1=mono16, 2=BGR24,
        // 100=JPEG mono8, 101=mono16 (uncompressed), 102=JPEG BGR24.
        let compressed = matches!(desc_fmt, 100 | 102);

        let (pixel_type, bpp, channels): (PixelType, u8, u32) = match desc_fmt {
            0 | 100 => (PixelType::Uint8, 8, 1), // mono 8-bit (raw / JPEG)
            1 => (PixelType::Uint16, 16, 1),     // mono 16-bit
            2 | 102 => (PixelType::Uint8, 8, 3), // color BGR24 (raw / JPEG)
            101 => (PixelType::Uint16, 16, 1),   // mono 16-bit alt
            _ => {
                return Err(BioFormatsError::UnsupportedFormat(format!(
                    "Norpix SEQ unsupported description format {desc_fmt}"
                )))
            }
        };

        let bps = pixel_type.bytes_per_sample();
        // Uncompressed (raw) plane payload in bytes.
        let plane_bytes = (width as usize)
            .checked_mul(height as usize)
            .and_then(|v| v.checked_mul(bps))
            .and_then(|v| v.checked_mul(channels as usize))
            .ok_or_else(|| BioFormatsError::Format("Norpix SEQ plane size overflows".into()))?;
        // trueImageSize is the padded per-frame stride (image payload + trailing
        // timestamp + alignment) for uncompressed data.
        let frame_size = if !compressed && true_image_size as usize >= plane_bytes {
            true_image_size as usize
        } else {
            plane_bytes
        };
        let is_rgb = channels == 3;

        // Build the per-frame offset table and read timestamps. For uncompressed
        // data frames are at fixed stride; for JPEG data each frame is stored as
        // a 4-byte little-endian size followed by the JPEG codestream.
        let file_len = f.metadata().map_err(BioFormatsError::Io)?.len();
        let mut frame_offsets = Vec::with_capacity(n_frames as usize);
        let mut timestamps = Vec::with_capacity(n_frames as usize);
        if compressed {
            let mut pos = 1024u64;
            for _ in 0..n_frames {
                if pos + 4 > file_len {
                    break;
                }
                f.seek(SeekFrom::Start(pos)).map_err(BioFormatsError::Io)?;
                let mut size_buf = [0u8; 4];
                f.read_exact(&mut size_buf).map_err(BioFormatsError::Io)?;
                let jpeg_size = u32::from_le_bytes(size_buf) as u64;
                let img_off = pos + 4;
                frame_offsets.push(img_off);
                // Timestamp follows the JPEG payload.
                let ts = read_seq_timestamp(&mut f, img_off + jpeg_size, file_len);
                timestamps.push(ts);
                pos = img_off + jpeg_size;
                // Some writers pad/align; advance past the 8-byte timestamp too.
                pos += 8;
            }
        } else {
            let required_len = 1024u64
                .checked_add(
                    (n_frames as u64 - 1)
                        .checked_mul(frame_size as u64)
                        .and_then(|v| v.checked_add(plane_bytes as u64))
                        .ok_or_else(|| {
                            BioFormatsError::Format("Norpix SEQ payload size overflows".into())
                        })?,
                )
                .ok_or_else(|| {
                    BioFormatsError::Format("Norpix SEQ payload offset overflows".into())
                })?;
            if file_len < required_len {
                return Err(BioFormatsError::UnsupportedFormat(format!(
                    "Norpix SEQ pixel payload is shorter than declared: need {required_len} bytes, found {file_len}"
                )));
            }
            for i in 0..n_frames as u64 {
                let img_off = 1024 + i * frame_size as u64;
                frame_offsets.push(img_off);
                // Timestamp sits immediately after the raw image payload.
                let ts = read_seq_timestamp(&mut f, img_off + plane_bytes as u64, file_len);
                timestamps.push(ts);
            }
        }

        let mut meta_map: HashMap<String, MetadataValue> = HashMap::new();
        meta_map.insert(
            "format".into(),
            MetadataValue::String("Norpix StreamPix SEQ".into()),
        );

        self.meta = Some(ImageMetadata {
            size_x: width,
            size_y: height,
            size_z: n_frames,
            size_c: channels,
            size_t: 1,
            pixel_type,
            bits_per_pixel: bpp,
            image_count: n_frames,
            dimension_order: DimensionOrder::XYZCT,
            is_rgb,
            is_interleaved: true,
            is_indexed: false,
            is_little_endian: true,
            resolution_count: 1,
            series_metadata: meta_map,
            lookup_table: None,
            modulo_z: None,
            modulo_c: None,
            modulo_t: None,
        });
        self.frame_size = frame_size;
        self.compressed = compressed;
        self.frame_offsets = frame_offsets;
        self.timestamps = timestamps;
        self.path = Some(path.to_path_buf());
        Ok(())
    }

    fn close(&mut self) -> Result<()> {
        self.path = None;
        self.meta = None;
        self.compressed = false;
        self.frame_offsets.clear();
        self.timestamps.clear();
        Ok(())
    }
    fn series_count(&self) -> usize {
        usize::from(self.meta.is_some())
    }
    fn set_series(&mut self, s: usize) -> Result<()> {
        if self.meta.is_none() {
            return Err(BioFormatsError::NotInitialized);
        }
        if s != 0 {
            Err(BioFormatsError::SeriesOutOfRange(s))
        } else {
            Ok(())
        }
    }
    fn series(&self) -> usize {
        0
    }
    fn metadata(&self) -> &ImageMetadata {
        self.meta
            .as_ref()
            .unwrap_or(crate::common::reader::uninitialized_metadata())
    }

    fn open_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        if plane_index >= meta.image_count {
            return Err(BioFormatsError::PlaneOutOfRange(plane_index));
        }
        let bps = meta.pixel_type.bytes_per_sample();
        let plane_bytes = (meta.size_x * meta.size_y * meta.size_c) as usize * bps;
        let path = self.path.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let mut f = File::open(path).map_err(BioFormatsError::Io)?;

        if self.compressed {
            // Decode the JPEG frame at the recorded offset. The next frame's
            // offset (minus the 4-byte size prefix) bounds the payload; for the
            // last frame, read to EOF.
            let start = *self
                .frame_offsets
                .get(plane_index as usize)
                .ok_or(BioFormatsError::PlaneOutOfRange(plane_index))?;
            let file_len = f.metadata().map_err(BioFormatsError::Io)?.len();
            let end = self
                .frame_offsets
                .get(plane_index as usize + 1)
                .map(|next| next.saturating_sub(4))
                .unwrap_or(file_len);
            let len = end.saturating_sub(start) as usize;
            f.seek(SeekFrom::Start(start))
                .map_err(BioFormatsError::Io)?;
            let mut jpeg = vec![0u8; len];
            f.read_exact(&mut jpeg).map_err(BioFormatsError::Io)?;
            let decoded = crate::common::codec::decompress_jpeg(&jpeg)?;
            // jpeg-decoder returns interleaved samples in the natural order; for
            // BGR24 frames StreamPix stores blue-first, but the JPEG codec yields
            // RGB, so return as-is (matching the channel order of the decoder).
            return Ok(decoded);
        }

        let frame = if self.frame_size > 0 {
            self.frame_size
        } else {
            plane_bytes
        };
        let offset = self
            .frame_offsets
            .get(plane_index as usize)
            .copied()
            .unwrap_or(1024u64 + plane_index as u64 * frame as u64);
        f.seek(SeekFrom::Start(offset))
            .map_err(BioFormatsError::Io)?;
        let mut buf = vec![0u8; plane_bytes];
        f.read_exact(&mut buf).map_err(BioFormatsError::Io)?;
        Ok(buf)
    }

    fn open_bytes_region(
        &mut self,
        plane_index: u32,
        x: u32,
        y: u32,
        w: u32,
        h: u32,
    ) -> Result<Vec<u8>> {
        let full = self.open_bytes(plane_index)?;
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let spp = meta.size_c as usize;
        crop_full_plane("Norpix SEQ", &full, meta, spp, x, y, w, h)
    }

    fn open_thumb_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let (tw, th) = (meta.size_x.min(256), meta.size_y.min(256));
        let (tx, ty) = ((meta.size_x - tw) / 2, (meta.size_y - th) / 2);
        self.open_bytes_region(plane_index, tx, ty, tw, th)
    }

    fn ome_metadata(&self) -> Option<crate::common::ome_metadata::OmeMetadata> {
        use crate::common::ome_metadata::{OmeMetadata, OmePlane};
        let meta = self.meta.as_ref()?;
        let mut ome = OmeMetadata::from_image_metadata(meta);
        if !self.timestamps.is_empty() {
            // Expose per-frame DeltaT relative to the first frame's timestamp.
            let base = self.timestamps[0];
            let img = &mut ome.images[0];
            img.planes = (0..meta.image_count)
                .map(|i| {
                    let z = i % meta.size_z;
                    OmePlane {
                        the_z: z,
                        the_c: 0,
                        the_t: 0,
                        delta_t: self.timestamps.get(i as usize).map(|t| t - base),
                        ..Default::default()
                    }
                })
                .collect();
        }
        Some(ome)
    }
}

// ─── IPLab ────────────────────────────────────────────────────────────────────
//
// IPLab (.ipl) is a format from Scanalytics used for multi-dimensional images.
//
// Header layout (little-endian):
//   Offset  0: magic — "ipl bina" (8 bytes) for binary data files
//   Offset  8: version (i32)
//   Offset 12: width (i32)
//   Offset 16: height (i32)
//   Offset 20: depth (i32) — number of z planes
//   Offset 24: n_channels (i32)
//   Offset 28: n_frames (i32) — time points
//   Offset 32: data_type (i32): 0=int8, 1=uint16, 2=int16, 3=float32, 4=uint8, 5=RGB, ...
//   Offset 36: color_mode (i32)
//   Pixel data starts at offset 96.

pub struct IplabReader {
    path: Option<PathBuf>,
    meta: Option<ImageMetadata>,
}

impl IplabReader {
    pub fn new() -> Self {
        IplabReader {
            path: None,
            meta: None,
        }
    }
}
impl Default for IplabReader {
    fn default() -> Self {
        Self::new()
    }
}

fn read_iplab_tags(path: &Path, offset: u64) -> Result<HashMap<String, MetadataValue>> {
    let mut meta_map = HashMap::new();
    let mut f = File::open(path).map_err(BioFormatsError::Io)?;
    let file_len = f.metadata().map_err(BioFormatsError::Io)?.len();
    if offset + 8 > file_len {
        return Ok(meta_map);
    }

    f.seek(SeekFrom::Start(offset))
        .map_err(BioFormatsError::Io)?;
    while f.stream_position().map_err(BioFormatsError::Io)? + 4 <= file_len {
        let mut tag = [0u8; 4];
        f.read_exact(&mut tag).map_err(BioFormatsError::Io)?;
        if &tag == b"fini" {
            break;
        }
        if f.stream_position().map_err(BioFormatsError::Io)? + 4 > file_len {
            break;
        }

        let mut size_bytes = [0u8; 4];
        f.read_exact(&mut size_bytes).map_err(BioFormatsError::Io)?;
        let size = u32::from_le_bytes(size_bytes) as usize;
        if f.stream_position().map_err(BioFormatsError::Io)? + size as u64 > file_len {
            break;
        }

        let mut payload = vec![0u8; size];
        f.read_exact(&mut payload).map_err(BioFormatsError::Io)?;
        let tag_name = printable_ascii(&tag);
        meta_map.insert(
            format!("iplab.tag.{tag_name}.size"),
            MetadataValue::Int(size as i64),
        );

        match &tag {
            b"clut" if size == 8 => {
                let lut_types = [
                    "monochrome",
                    "reverse monochrome",
                    "BGR",
                    "classify",
                    "rainbow",
                    "red",
                    "green",
                    "blue",
                    "cyan",
                    "magenta",
                    "yellow",
                    "saturated pixels",
                ];
                let kind = r_i32_le(&payload, 4);
                let label = lut_types
                    .get(kind as usize)
                    .copied()
                    .unwrap_or("unknown")
                    .to_string();
                meta_map.insert("LUT type".into(), MetadataValue::String(label));
            }
            b"head" => {
                for chunk in payload.chunks_exact(22) {
                    let num = i16::from_le_bytes([chunk[0], chunk[1]]);
                    meta_map.insert(
                        format!("Header{num}"),
                        MetadataValue::String(printable_ascii(&chunk[2..22])),
                    );
                }
            }
            b"note" if size >= 576 => {
                meta_map.insert(
                    "Descriptor".into(),
                    MetadataValue::String(printable_ascii(&payload[..64])),
                );
                meta_map.insert(
                    "Notes".into(),
                    MetadataValue::String(printable_ascii(&payload[64..576])),
                );
            }
            _ => {}
        }
    }

    Ok(meta_map)
}

impl FormatReader for IplabReader {
    fn is_this_type_by_name(&self, path: &Path) -> bool {
        path.extension()
            .and_then(|e| e.to_str())
            .map(|e| e.eq_ignore_ascii_case("ipl") || e.eq_ignore_ascii_case("ipm"))
            .unwrap_or(false)
    }

    fn is_this_type_by_bytes(&self, header: &[u8]) -> bool {
        header.len() >= 8 && &header[..8] == b"ipl bina"
    }

    fn set_id(&mut self, path: &Path) -> Result<()> {
        self.close()?;
        let mut f = File::open(path).map_err(BioFormatsError::Io)?;
        let mut hdr = vec![0u8; 96];
        f.read_exact(&mut hdr).map_err(BioFormatsError::Io)?;

        let width = positive_i32_dim(r_i32_le(&hdr, 12), "width")?;
        let height = positive_i32_dim(r_i32_le(&hdr, 16), "height")?;
        let depth = positive_i32_dim(r_i32_le(&hdr, 20), "depth")?;
        let n_channels = positive_i32_dim(r_i32_le(&hdr, 24), "channel count")?;
        let n_frames = positive_i32_dim(r_i32_le(&hdr, 28), "frame count")?;
        let data_type = r_i32_le(&hdr, 32);

        let (pixel_type, bpp, spp): (PixelType, u8, u32) = match data_type {
            0 => (PixelType::Uint8, 8, 1), // int8 → report as uint8
            1 => (PixelType::Uint16, 16, 1),
            2 => (PixelType::Int16, 16, 1),
            3 => (PixelType::Float32, 32, 1),
            4 => (PixelType::Uint8, 8, 1),
            5 => (PixelType::Uint8, 8, 3), // RGB
            _ => {
                return Err(BioFormatsError::UnsupportedFormat(format!(
                    "IPLab unsupported data type {data_type}"
                )))
            }
        };
        let is_rgb = spp == 3;
        let image_count = depth * n_channels * n_frames;
        let plane_bytes = (width as u64)
            .checked_mul(height as u64)
            .and_then(|v| v.checked_mul(spp as u64))
            .and_then(|v| v.checked_mul(bpp as u64 / 8))
            .ok_or_else(|| BioFormatsError::Format("IPLab plane byte count overflows".into()))?;
        let pixel_bytes = plane_bytes
            .checked_mul(image_count as u64)
            .ok_or_else(|| BioFormatsError::Format("IPLab pixel byte count overflows".into()))?;
        let file_len = f.metadata().map_err(BioFormatsError::Io)?.len();
        let required_len = 96u64
            .checked_add(pixel_bytes)
            .ok_or_else(|| BioFormatsError::Format("IPLab payload offset overflows".into()))?;
        if file_len < required_len {
            return Err(BioFormatsError::Format(format!(
                "IPLab pixel payload is truncated: need {required_len} bytes, found {file_len}"
            )));
        }

        let mut meta_map: HashMap<String, MetadataValue> = HashMap::new();
        meta_map.insert("format".into(), MetadataValue::String("IPLab".into()));
        meta_map.insert(
            "iplab.version".into(),
            MetadataValue::Int(r_i32_le(&hdr, 8) as i64),
        );
        meta_map.insert(
            "iplab.data_type".into(),
            MetadataValue::Int(data_type as i64),
        );
        meta_map.insert(
            "iplab.color_mode".into(),
            MetadataValue::Int(r_i32_le(&hdr, 36) as i64),
        );
        meta_map.extend(read_iplab_tags(path, 96 + pixel_bytes).unwrap_or_default());

        self.meta = Some(ImageMetadata {
            size_x: width,
            size_y: height,
            size_z: depth,
            size_c: n_channels * spp,
            size_t: n_frames,
            pixel_type,
            bits_per_pixel: bpp,
            image_count,
            dimension_order: DimensionOrder::XYZCT,
            is_rgb,
            is_interleaved: is_rgb,
            is_indexed: false,
            is_little_endian: true,
            resolution_count: 1,
            series_metadata: meta_map,
            lookup_table: None,
            modulo_z: None,
            modulo_c: None,
            modulo_t: None,
        });
        self.path = Some(path.to_path_buf());
        Ok(())
    }

    fn close(&mut self) -> Result<()> {
        self.path = None;
        self.meta = None;
        Ok(())
    }
    fn series_count(&self) -> usize {
        usize::from(self.meta.is_some())
    }
    fn set_series(&mut self, s: usize) -> Result<()> {
        if self.meta.is_none() {
            return Err(BioFormatsError::NotInitialized);
        }
        if s != 0 {
            Err(BioFormatsError::SeriesOutOfRange(s))
        } else {
            Ok(())
        }
    }
    fn series(&self) -> usize {
        0
    }
    fn metadata(&self) -> &ImageMetadata {
        self.meta
            .as_ref()
            .unwrap_or(crate::common::reader::uninitialized_metadata())
    }

    fn open_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        if plane_index >= meta.image_count {
            return Err(BioFormatsError::PlaneOutOfRange(plane_index));
        }
        let bps = meta.pixel_type.bytes_per_sample();
        let spp = if meta.is_rgb { 3usize } else { 1usize };
        let plane_bytes = (meta.size_x * meta.size_y) as usize * spp * bps;
        let offset = 96u64 + plane_index as u64 * plane_bytes as u64;
        let path = self.path.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let mut f = File::open(path).map_err(BioFormatsError::Io)?;
        f.seek(SeekFrom::Start(offset))
            .map_err(BioFormatsError::Io)?;
        let mut buf = vec![0u8; plane_bytes];
        f.read_exact(&mut buf).map_err(BioFormatsError::Io)?;
        Ok(buf)
    }

    fn open_bytes_region(
        &mut self,
        plane_index: u32,
        x: u32,
        y: u32,
        w: u32,
        h: u32,
    ) -> Result<Vec<u8>> {
        let full = self.open_bytes(plane_index)?;
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let spp = if meta.is_rgb { 3usize } else { 1usize };
        crop_full_plane("IPLab", &full, meta, spp, x, y, w, h)
    }

    fn open_thumb_bytes(&mut self, plane_index: u32) -> Result<Vec<u8>> {
        let meta = self.meta.as_ref().ok_or(BioFormatsError::NotInitialized)?;
        let (tw, th) = (meta.size_x.min(256), meta.size_y.min(256));
        let (tx, ty) = ((meta.size_x - tw) / 2, (meta.size_y - th) / 2);
        self.open_bytes_region(plane_index, tx, ty, tw, th)
    }
}