pub struct Reader { /* private fields */ }Expand description
Consolidated MRC reader with automatic mmap/buffered backend selection. MRC file reader with automatic backend selection.
Opens files via memory mapping (zero-copy for large files) or buffered
I/O (in-memory for smaller files). Accepts custom std::io::Read sources
via from_reader.
All iteration methods (slices, slabs, tiles, subregion, etc.)
are inherent methods — no trait imports needed.
§Zero-copy access
When backed by a memory map (which open selects
automatically for files), the default reader methods return a
DataBlock whose DataView
borrows directly from the mapped memory with no allocation. The same
zero-copy access is available for buffered readers (created via
from_bytes, from_reader,
or compressed-file constructors) when the block is a native-endian
contiguous full-row slab.
§Example
use mrc::Reader;
let reader = Reader::open("density.mrc")?;
for slice in reader.slices() {
let block = slice?;
match block.data() {
mrc::DataView::Float32(data) => println!("f32 slice: {} voxels", data.len()),
mrc::DataView::Int16(data) => println!("i16 slice: {} voxels", data.len()),
_ => {}
}
}Implementations§
Source§impl Reader
impl Reader
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self, Error>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, Error>
Open an MRC file, auto-detecting gzip/bzip2 compression.
For plain files, selects memory-mapped I/O when available (the mmap
feature) and falls back to buffered I/O otherwise.
§Examples
let reader = mrc::Reader::open("density.mrc")?;
println!("Dimensions: {}x{}x{}", reader.shape().nx, reader.shape().ny, reader.shape().nz);Sourcepub fn open_permissive<P: AsRef<Path>>(
path: P,
) -> Result<(Self, Vec<String>), Error>
pub fn open_permissive<P: AsRef<Path>>( path: P, ) -> Result<(Self, Vec<String>), Error>
Open in permissive mode.
Non-fatal header issues are collected as warnings instead of errors.
§Examples
let (reader, warnings) = mrc::Reader::open_permissive("density.mrc")?;
for w in &warnings {
eprintln!("Warning: {w}");
}Sourcepub fn open_plain<P: AsRef<Path>>(path: P) -> Result<Self, Error>
pub fn open_plain<P: AsRef<Path>>(path: P) -> Result<Self, Error>
Open a plain (uncompressed) MRC file via buffered I/O.
§Examples
let reader = mrc::Reader::open_plain("density.mrc")?;
println!("Shape: {:?}", reader.shape());Sourcepub fn from_reader<R: Read>(reader: R) -> Result<Self, Error>
pub fn from_reader<R: Read>(reader: R) -> Result<Self, Error>
Read an MRC file from any std::io::Read source.
The entire source is read into memory, then parsed.
§Examples
use std::io::Cursor;
let reader = mrc::Reader::from_reader(Cursor::new(buf))?;
assert_eq!(reader.shape().nx, 4);Sourcepub fn from_reader_permissive<R: Read>(
reader: R,
) -> Result<(Self, Vec<String>), Error>
pub fn from_reader_permissive<R: Read>( reader: R, ) -> Result<(Self, Vec<String>), Error>
Read from any std::io::Read source in permissive mode.
§Examples
use std::io::Cursor;
let (reader, warnings) = mrc::Reader::from_reader_permissive(Cursor::new(buf))?;
assert!(warnings.is_empty());Source§impl Reader
impl Reader
Sourcepub fn shape(&self) -> VolumeShape
pub fn shape(&self) -> VolumeShape
Sourcepub fn endian(&self) -> FileEndian
pub fn endian(&self) -> FileEndian
Sourcepub fn raw_bytes(&self) -> &[u8] ⓘ
pub fn raw_bytes(&self) -> &[u8] ⓘ
Raw voxel data bytes.
For memory-mapped readers this returns a zero-copy &[u8] view.
For buffered readers this borrows from the internal Vec<u8>.
§Examples
let bytes = reader.raw_bytes();
assert_eq!(bytes.len(), 64);Sourcepub fn ext_header_bytes(&self) -> &[u8] ⓘ
pub fn ext_header_bytes(&self) -> &[u8] ⓘ
Extended header bytes (empty slice if none).
§Examples
assert!(reader.ext_header_bytes().is_empty());Sourcepub fn is_truncated(&self) -> bool
pub fn is_truncated(&self) -> bool
Returns true when the file is shorter than the header’s declared data
size (only possible when opened in permissive mode).
§Examples
assert!(!reader.is_truncated());Sourcepub fn is_single_image(&self) -> bool
pub fn is_single_image(&self) -> bool
Returns true if the file represents a single 2D image (nz == 1).
§Examples
assert!(reader.is_single_image());Sourcepub fn is_image_stack(&self) -> bool
pub fn is_image_stack(&self) -> bool
Returns true if the file is an image stack (ispg == 0).
§Examples
assert!(reader.is_image_stack());
assert!(!reader.is_volume());Sourcepub fn is_volume(&self) -> bool
pub fn is_volume(&self) -> bool
Returns true if the file represents a single 3D volume.
This is true when the file is neither an image stack nor a volume stack.
§Examples
assert!(reader.is_volume());Sourcepub fn is_volume_stack(&self) -> bool
pub fn is_volume_stack(&self) -> bool
Sourcepub fn logical_shape(&self) -> [usize; 4]
pub fn logical_shape(&self) -> [usize; 4]
Return the logical 4D shape [nvolumes, mz, ny, nx].
For non-stack files this is [1, nz, ny, nx].
For volume stacks it is [nz / mz, mz, ny, nx].
§Examples
assert_eq!(reader.logical_shape(), [2, 30, 64, 64]);Source§impl Reader
impl Reader
Sourcepub fn read_block_bytes(
&self,
offset: [usize; 3],
shape: [usize; 3],
) -> Result<Vec<u8>, Error>
pub fn read_block_bytes( &self, offset: [usize; 3], shape: [usize; 3], ) -> Result<Vec<u8>, Error>
Read a block of raw voxel bytes.
§Examples
let block = reader.read_block_bytes([0, 0, 0], [4, 4, 1])?;
assert_eq!(block.len(), 64);Sourcepub fn slices(&self) -> impl Iterator<Item = Result<DataBlock<'_>, Error>> + '_
pub fn slices(&self) -> impl Iterator<Item = Result<DataBlock<'_>, Error>> + '_
Return a region iterator over Z-slices.
The returned crate::DataBlock borrows from the reader’s internal buffer
(zero-copy for native-endian contiguous blocks).
Sourcepub fn slabs(
&self,
k: usize,
) -> impl Iterator<Item = Result<DataBlock<'_>, Error>> + '_
pub fn slabs( &self, k: usize, ) -> impl Iterator<Item = Result<DataBlock<'_>, Error>> + '_
Return a region iterator over Z-slabs of k slices.
Sourcepub fn tiles(
&self,
tile_shape: [usize; 3],
) -> Result<impl Iterator<Item = Result<DataBlock<'_>, Error>> + '_, Error>
pub fn tiles( &self, tile_shape: [usize; 3], ) -> Result<impl Iterator<Item = Result<DataBlock<'_>, Error>> + '_, Error>
Return a region iterator over 3D tiles of the given shape.
Sourcepub fn volumes(
&self,
) -> Result<impl Iterator<Item = Result<DataBlock<'_>, Error>> + '_, Error>
pub fn volumes( &self, ) -> Result<impl Iterator<Item = Result<DataBlock<'_>, Error>> + '_, Error>
Iterate over sub-volumes in a volume stack.
Each sub-volume has shape [nx, ny, mz]. Returns
Error::NotAVolumeStack if the file is not a volume stack.
Sourcepub fn subregion(
&self,
offset: [usize; 3],
block_shape: [usize; 3],
) -> Result<DataBlock<'_>, Error>
pub fn subregion( &self, offset: [usize; 3], block_shape: [usize; 3], ) -> Result<DataBlock<'_>, Error>
Read a single 3D sub-region at offset with block_shape.
Returns a crate::DataBlock whose crate::DataView variant matches the file’s
on-disk mode. The data borrows from the reader’s internal buffer
when possible (zero-copy for native-endian contiguous blocks).
Sourcepub fn read_volume(&self) -> Result<DataBlock<'_>, Error>
pub fn read_volume(&self) -> Result<DataBlock<'_>, Error>
Read the entire volume as a single block.
Sourcepub fn slices_u8(
&self,
) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>
pub fn slices_u8( &self, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>
Iterate over Z-slices as u8 (Uint16 narrowing or Packed4Bit unpack).
§Examples
for slice in reader.slices_u8() {
let block = slice?;
println!("u8 slice at z={}", block.offset[2]);
}Sourcepub fn slabs_u8(
&self,
k: usize,
) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>
pub fn slabs_u8( &self, k: usize, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>
Iterate over Z-slabs as u8.
§Examples
for slab in reader.slabs_u8(2) {
let block = slab?;
println!("u8 slab depth: {}", block.shape[2]);
}Sourcepub fn slices_mode0(
&self,
interp: M0Interpretation,
) -> Box<dyn Iterator<Item = Result<VoxelBlock<f32>, Error>> + '_>
pub fn slices_mode0( &self, interp: M0Interpretation, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<f32>, Error>> + '_>
Iterate over Z-slices of a Mode 0 file with configurable signed/unsigned interpretation.
§Examples
for slice in reader.slices_mode0(mrc::M0Interpretation::Signed) {
let block = slice?;
println!("Mode 0 slice at z={}", block.offset[2]);
}Sourcepub fn slabs_mode0(
&self,
k: usize,
interp: M0Interpretation,
) -> Box<dyn Iterator<Item = Result<VoxelBlock<f32>, Error>> + '_>
pub fn slabs_mode0( &self, k: usize, interp: M0Interpretation, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<f32>, Error>> + '_>
Iterate over Z-slabs of a Mode 0 file.
§Examples
for slab in reader.slabs_mode0(2, mrc::M0Interpretation::Signed) {
let block = slab?;
println!("Mode 0 slab depth: {}", block.shape[2]);
}Sourcepub fn convert<T>(&self) -> ConvertReader<'_, T>
pub fn convert<T>(&self) -> ConvertReader<'_, T>
Return a wrapper that auto-converts all reads to type T.
§Examples
let converter = reader.convert::<f32>();
for slice in converter.slices() {
let block = slice?;
println!("Converted slice: {} voxels", block.data.len());
}Sourcepub fn read_volume_u8(&self) -> Result<VoxelBlock<u8>, Error>
pub fn read_volume_u8(&self) -> Result<VoxelBlock<u8>, Error>
Read the entire volume as u8 (Packed4Bit unpack).
§Examples
let block = reader.read_volume_u8()?;
assert_eq!(block.data.len(), 16);Sourcepub fn parse_extended_header(&self) -> ExtHeaderData
pub fn parse_extended_header(&self) -> ExtHeaderData
Auto-dispatch extended header parsing.
§Examples
let ext = reader.parse_extended_header();
assert_eq!(ext, mrc::ExtHeaderData::None);Sourcepub fn fei1_metadata(&self) -> Option<Vec<Fei1Metadata>>
pub fn fei1_metadata(&self) -> Option<Vec<Fei1Metadata>>
Parse FEI1 metadata records.
§Examples
let fei1 = reader.fei1_metadata();
assert!(fei1.is_none()); // no FEI1 extended header presentSourcepub fn fei2_metadata(&self) -> Option<Vec<Fei2Metadata>>
pub fn fei2_metadata(&self) -> Option<Vec<Fei2Metadata>>
Sourcepub fn ccp4_records(&self) -> Option<Vec<Ccp4Record>>
pub fn ccp4_records(&self) -> Option<Vec<Ccp4Record>>
Sourcepub fn mrco_records(&self) -> Option<Vec<MrcoRecord>>
pub fn mrco_records(&self) -> Option<Vec<MrcoRecord>>
Sourcepub fn seri_records(&self) -> Option<Vec<SeriRecord>>
pub fn seri_records(&self) -> Option<Vec<SeriRecord>>
Sourcepub fn agar_records(&self) -> Option<Vec<AgarRecord>>
pub fn agar_records(&self) -> Option<Vec<AgarRecord>>
Sourcepub fn imod_metadata(&self) -> Option<ImodMetadata>
pub fn imod_metadata(&self) -> Option<ImodMetadata>
Source§impl Reader
impl Reader
Sourcepub fn open_gzip<P: AsRef<Path>>(path: P) -> Result<Self, Error>
pub fn open_gzip<P: AsRef<Path>>(path: P) -> Result<Self, Error>
Open a gzip-compressed MRC file.
Requires the gzip feature (enabled by default).
The file is decompressed into memory with a safety limit of
DEFAULT_MAX_DECOMPRESSED_BYTES (256 GiB). To set a custom limit,
use open_gzip_with_limit.
Sourcepub fn open_gzip_with_limit<P: AsRef<Path>>(
path: P,
max_bytes: u64,
) -> Result<Self, Error>
pub fn open_gzip_with_limit<P: AsRef<Path>>( path: P, max_bytes: u64, ) -> Result<Self, Error>
Open a gzip-compressed MRC file with a custom decompression byte limit.
The decompressed stream is capped at max_bytes before the header is
parsed. If the stream exceeds this limit, a decompression-bomb error
is returned. Use the default open_gzip for the
built-in 256 GiB safety limit.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Reader
impl RefUnwindSafe for Reader
impl Send for Reader
impl Sync for Reader
impl Unpin for Reader
impl UnsafeUnpin for Reader
impl UnwindSafe for Reader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more