mrc 0.2.2

Zero-copy, zero-allocation MRC-2014 file format reader/writer for Rust
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
//! Memory-mapped MRC file reader with zero-copy API

use crate::engine::block::{VolumeShape, VoxelBlock};
use crate::engine::codec::{EndianCodec, decode_slice};
use crate::engine::convert::Convert;
use crate::engine::endian::FileEndian;
use crate::engine::pipeline::{ConversionPath, get_conversion_path, is_zero_copy};
use crate::{Error, Header, Mode};

use alloc::vec::Vec;

/// Memory-mapped MRC file reader.
///
/// Provides zero-copy access to MRC files by memory-mapping them into the process address space.
/// This is ideal for reading large files that don't fit in RAM, as the OS handles paging.
///
/// # Example
/// ```ignore
/// use mrc::MmapReader;
///
/// let reader = MmapReader::open("large_file.mrc")?;
/// println!("Dimensions: {:?}", reader.shape());
///
/// // Iterate over slices with zero-copy when possible
/// for slice in reader.slices::<f32>() {
///     let block = slice?;
///     // process block.data
/// }
/// ```
#[cfg(feature = "mmap")]
pub struct MmapReader {
    mmap: memmap2::Mmap,
    header: Header,
    data_offset: usize,
    endian: FileEndian,
    shape: VolumeShape,
    bytes_per_voxel: usize,
}

#[cfg(feature = "mmap")]
impl MmapReader {
    /// Open an MRC file via memory mapping.
    ///
    /// The file is mapped read-only into the process address space.
    /// The OS will page data in/out as needed, making this efficient for large files.
    pub fn open(path: &str) -> Result<Self, Error> {
        use std::fs::File;
        use std::io::Read;

        let mut file = File::open(path).map_err(|_| Error::Io("open file for mmap".into()))?;

        // Read header first (not mapped, since we need to parse it)
        let mut header_bytes = [0u8; 1024];
        file.read_exact(&mut header_bytes)
            .map_err(|_| Error::Io("read header".into()))?;

        let header = Header::decode_from_bytes(&header_bytes);

        if !header.validate() {
            return Err(Error::InvalidHeader);
        }

        // Map the entire file
        let mmap = unsafe {
            memmap2::MmapOptions::new()
                .map(&file)
                .map_err(|_| Error::Mmap)?
        };

        let endian = header.detect_endian();
        let shape =
            VolumeShape::new(header.nx as usize, header.ny as usize, header.nz as usize);

        let mode = Mode::from_i32(header.mode).ok_or(Error::UnsupportedMode)?;
        let bytes_per_voxel = mode.byte_size();

        Ok(Self {
            mmap,
            header,
            data_offset: header.data_offset(),
            endian,
            shape,
            bytes_per_voxel,
        })
    }

    /// Get the volume shape (dimensions).
    pub fn shape(&self) -> VolumeShape {
        self.shape
    }

    /// Get the voxel mode (data type) of the file.
    pub fn mode(&self) -> Mode {
        Mode::from_i32(self.header.mode).unwrap_or(Mode::Float32)
    }

    /// Get a reference to the header.
    pub fn header(&self) -> &Header {
        &self.header
    }

    /// Get the file endianness.
    pub fn endian(&self) -> FileEndian {
        self.endian
    }

    /// Get the raw data bytes from the memory map.
    ///
    /// This returns a slice starting at the beginning of voxel data
    /// (after the header and extended header).
    pub fn data_bytes(&self) -> &[u8] {
        let data_size = self.header.data_size();
        &self.mmap[self.data_offset..self.data_offset + data_size]
    }

    /// Read a block of voxels as raw bytes from the mmap.
    fn read_voxel_bytes(&self, offset: [usize; 3], shape: [usize; 3]) -> Result<&[u8], Error> {
        let [nx, ny, nz] = [self.shape.nx, self.shape.ny, self.shape.nz];
        let [ox, oy, oz] = offset;
        let [sx, sy, sz] = shape;

        if ox + sx > nx || oy + sy > ny || oz + sz > nz {
            return Err(Error::BoundsError);
        }

        let start_byte = self.data_offset
            + (ox + oy * nx + oz * nx * ny) * self.bytes_per_voxel;
        let end_byte = start_byte + sx * sy * sz * self.bytes_per_voxel;

        if end_byte > self.mmap.len() {
            return Err(Error::BoundsError);
        }

        Ok(&self.mmap[start_byte..end_byte])
    }

    /// Check if zero-copy decode is possible.
    ///
    /// Zero-copy requires: file mode matches T's mode AND file endian is native.
    pub fn can_zero_copy<T: crate::mode::Voxel>(&self) -> bool {
        is_zero_copy(self.mode(), T::MODE, self.endian)
    }

    /// Decode a block of voxels to the specified type.
    ///
    /// Uses zero-copy fast path when mode and endian match.
    pub(crate) fn decode_block<T: EndianCodec + Send + Copy + Default + crate::mode::Voxel>(
        &self,
        bytes: &[u8],
    ) -> Result<Vec<T>, Error> {
        if self.can_zero_copy::<T>() {
            return self.decode_block_zero_copy(bytes);
        }

        Ok(decode_slice(bytes, self.endian))
    }

    /// Zero-copy decode: transmute bytes directly to Vec<T>.
    fn decode_block_zero_copy<T: EndianCodec + Copy>(&self, bytes: &[u8]) -> Result<Vec<T>, Error> {
        let n = bytes.len() / T::BYTE_SIZE;

        let mut result = Vec::with_capacity(n);
        unsafe {
            core::ptr::copy_nonoverlapping(
                bytes.as_ptr(),
                result.as_mut_ptr() as *mut u8,
                bytes.len(),
            );
            result.set_len(n);
        }
        Ok(result)
    }

    /// Decode and convert bytes to destination type.
    pub(crate) fn decode_and_convert<S, D>(&self, bytes: &[u8]) -> Result<Vec<D>, Error>
    where
        S: EndianCodec + Send + Copy + Default + 'static,
        D: Convert<S> + 'static,
    {
        let src_data = decode_slice::<S>(bytes, self.endian);

        #[cfg(feature = "simd")]
        {
            use crate::engine::convert::try_simd_convert;
            if let Some(result) = try_simd_convert::<S, D>(&src_data) {
                return Ok(result);
            }
        }

        let mut dst_data = Vec::with_capacity(src_data.len());
        for src in src_data {
            dst_data.push(D::convert(src));
        }

        Ok(dst_data)
    }

    /// Read and convert voxels from file mode to target type D.
    pub fn read_converted<S, D>(
        &self,
        offset: [usize; 3],
        shape: [usize; 3],
    ) -> Result<VoxelBlock<D>, Error>
    where
        S: EndianCodec + Send + Copy + Default + crate::mode::Voxel,
        D: Convert<S> + EndianCodec + Copy + Default + crate::mode::Voxel,
    {
        let bytes = self.read_voxel_bytes(offset, shape)?;
        let data = self.decode_and_convert::<S, D>(bytes)?;
        Ok(VoxelBlock { offset, shape, data })
    }

    /// Get the optimal conversion path.
    pub fn conversion_path(&self, dst_mode: Mode) -> ConversionPath {
        get_conversion_path(self.mode(), dst_mode, self.endian)
    }

    /// Iterate over slices (Z axis).
    pub fn slices<T>(&self) -> MmapSliceIter<'_, T> {
        MmapSliceIter::new(self, self.shape)
    }

    /// Iterate over slabs (k slices at a time).
    pub fn slabs<T>(&self, k: usize) -> MmapSlabIter<'_, T> {
        MmapSlabIter::new(self, self.shape, k)
    }

    /// Iterate over arbitrary blocks.
    pub fn blocks<T>(&self, block_shape: [usize; 3]) -> MmapBlockIter<'_, T> {
        MmapBlockIter::new(self, self.shape, block_shape)
    }

    /// Iterate over slices with type conversion.
    pub fn slices_converted<S, D>(&self) -> MmapSliceIterConverted<'_, S, D>
    where
        S: crate::mode::Voxel,
        D: Convert<S> + crate::mode::Voxel,
    {
        MmapSliceIterConverted::new(self, self.shape)
    }

    /// Iterate over slabs with type conversion.
    pub fn slabs_converted<S, D>(&self, k: usize) -> MmapSlabIterConverted<'_, S, D>
    where
        S: crate::mode::Voxel,
        D: Convert<S> + crate::mode::Voxel,
    {
        MmapSlabIterConverted::new(self, self.shape, k)
    }
}

// ============================================================================
// MmapReader Iterators
// ============================================================================

/// Slice iterator for MmapReader.
pub struct MmapSliceIter<'a, T> {
    reader: &'a MmapReader,
    z: usize,
    nz: usize,
    nx: usize,
    ny: usize,
    _phantom: core::marker::PhantomData<T>,
}

impl<'a, T> MmapSliceIter<'a, T> {
    pub fn new(reader: &'a MmapReader, shape: VolumeShape) -> Self {
        Self {
            reader,
            z: 0,
            nz: shape.nz,
            nx: shape.nx,
            ny: shape.ny,
            _phantom: core::marker::PhantomData,
        }
    }
}

impl<'a, T> Iterator for MmapSliceIter<'a, T>
where
    T: EndianCodec + Send + Copy + Default + crate::mode::Voxel,
{
    type Item = Result<VoxelBlock<T>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.z >= self.nz {
            return None;
        }

        let z = self.z;
        self.z += 1;

        let offset = [0, 0, z];
        let shape = [self.nx, self.ny, 1];

        match self.reader.read_voxel_bytes(offset, shape) {
            Ok(bytes) => {
                match self.reader.decode_block::<T>(bytes) {
                    Ok(data) => Some(Ok(VoxelBlock { offset, shape, data })),
                    Err(e) => Some(Err(e)),
                }
            }
            Err(e) => Some(Err(e)),
        }
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.z = n;
        self.next()
    }
}

/// Slab iterator for MmapReader.
pub struct MmapSlabIter<'a, T> {
    reader: &'a MmapReader,
    z: usize,
    nz: usize,
    nx: usize,
    ny: usize,
    slab_size: usize,
    _phantom: core::marker::PhantomData<T>,
}

impl<'a, T> MmapSlabIter<'a, T> {
    pub fn new(reader: &'a MmapReader, shape: VolumeShape, k: usize) -> Self {
        Self {
            reader,
            z: 0,
            nz: shape.nz,
            nx: shape.nx,
            ny: shape.ny,
            slab_size: k,
            _phantom: core::marker::PhantomData,
        }
    }
}

impl<'a, T> Iterator for MmapSlabIter<'a, T>
where
    T: EndianCodec + Send + Copy + Default + crate::mode::Voxel,
{
    type Item = Result<VoxelBlock<T>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.z >= self.nz {
            return None;
        }

        let z = self.z;
        let size = self.slab_size.min(self.nz - z);
        self.z += size;

        let offset = [0, 0, z];
        let shape = [self.nx, self.ny, size];

        match self.reader.read_voxel_bytes(offset, shape) {
            Ok(bytes) => {
                match self.reader.decode_block::<T>(bytes) {
                    Ok(data) => Some(Ok(VoxelBlock { offset, shape, data })),
                    Err(e) => Some(Err(e)),
                }
            }
            Err(e) => Some(Err(e)),
        }
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.z = n * self.slab_size;
        self.next()
    }
}

/// Block iterator for MmapReader.
pub struct MmapBlockIter<'a, T> {
    reader: &'a MmapReader,
    position: [usize; 3],
    shape: VolumeShape,
    block_shape: [usize; 3],
    _phantom: core::marker::PhantomData<T>,
}

impl<'a, T> MmapBlockIter<'a, T> {
    pub fn new(reader: &'a MmapReader, shape: VolumeShape, block_shape: [usize; 3]) -> Self {
        Self {
            reader,
            position: [0, 0, 0],
            shape,
            block_shape,
            _phantom: core::marker::PhantomData,
        }
    }
}

impl<'a, T> Iterator for MmapBlockIter<'a, T>
where
    T: EndianCodec + Send + Copy + Default + crate::mode::Voxel,
{
    type Item = Result<VoxelBlock<T>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        let [nx, ny, nz] = [self.shape.nx, self.shape.ny, self.shape.nz];
        let [bx, by, bz] = self.block_shape;
        let [px, py, pz] = self.position;

        if pz >= nz {
            return None;
        }

        let sx = bx.min(nx - px);
        let sy = by.min(ny - py);
        let sz = bz.min(nz - pz);

        // Update position for next iteration
        self.position[0] += bx;
        if self.position[0] >= nx {
            self.position[0] = 0;
            self.position[1] += by;
            if self.position[1] >= ny {
                self.position[1] = 0;
                self.position[2] += bz;
            }
        }

        let offset = [px, py, pz];
        let shape = [sx, sy, sz];

        match self.reader.read_voxel_bytes(offset, shape) {
            Ok(bytes) => {
                match self.reader.decode_block::<T>(bytes) {
                    Ok(data) => Some(Ok(VoxelBlock { offset, shape, data })),
                    Err(e) => Some(Err(e)),
                }
            }
            Err(e) => Some(Err(e)),
        }
    }
}

// ============================================================================
// Conversion-enabled Iterators for MmapReader
// ============================================================================

/// Slice iterator with type conversion for MmapReader.
pub struct MmapSliceIterConverted<'a, S, D> {
    reader: &'a MmapReader,
    z: usize,
    nz: usize,
    nx: usize,
    ny: usize,
    _phantom: core::marker::PhantomData<(S, D)>,
}

impl<'a, S, D> MmapSliceIterConverted<'a, S, D> {
    pub fn new(reader: &'a MmapReader, shape: VolumeShape) -> Self {
        Self {
            reader,
            z: 0,
            nz: shape.nz,
            nx: shape.nx,
            ny: shape.ny,
            _phantom: core::marker::PhantomData,
        }
    }
}

impl<'a, S, D> Iterator for MmapSliceIterConverted<'a, S, D>
where
    S: EndianCodec + Send + Copy + Default + crate::mode::Voxel,
    D: Convert<S> + EndianCodec + Copy + Default + crate::mode::Voxel,
{
    type Item = Result<VoxelBlock<D>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.z >= self.nz {
            return None;
        }

        let z = self.z;
        self.z += 1;

        Some(self.reader.read_converted::<S, D>([0, 0, z], [self.nx, self.ny, 1]))
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.z = n;
        self.next()
    }
}

/// Slab iterator with type conversion for MmapReader.
pub struct MmapSlabIterConverted<'a, S, D> {
    reader: &'a MmapReader,
    z: usize,
    nz: usize,
    nx: usize,
    ny: usize,
    slab_size: usize,
    _phantom: core::marker::PhantomData<(S, D)>,
}

impl<'a, S, D> MmapSlabIterConverted<'a, S, D> {
    pub fn new(reader: &'a MmapReader, shape: VolumeShape, k: usize) -> Self {
        Self {
            reader,
            z: 0,
            nz: shape.nz,
            nx: shape.nx,
            ny: shape.ny,
            slab_size: k,
            _phantom: core::marker::PhantomData,
        }
    }
}

impl<'a, S, D> Iterator for MmapSlabIterConverted<'a, S, D>
where
    S: EndianCodec + Send + Copy + Default + crate::mode::Voxel,
    D: Convert<S> + EndianCodec + Copy + Default + crate::mode::Voxel,
{
    type Item = Result<VoxelBlock<D>, Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.z >= self.nz {
            return None;
        }

        let z = self.z;
        let size = self.slab_size.min(self.nz - z);
        self.z += size;

        Some(self.reader.read_converted::<S, D>([0, 0, z], [self.nx, self.ny, size]))
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.z = n * self.slab_size;
        self.next()
    }
}