pub struct Reader { /* private fields */ }Expand description
Buffered MRC reader with lazy slice/slab iterators. In-memory buffered MRC file reader.
The entire file is read into memory on open, making this suitable for smaller files or when random access to any slice is needed.
For large files that don’t fit in RAM, consider MmapReader
(requires the mmap feature).
§Example
use mrc::Reader;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let reader = Reader::open("protein.mrc")?;
for slice in reader.slices::<f32>() {
let block = slice?;
// block.data is Vec<f32>
}
Ok(())
}§Opening compressed files
Reader::open auto-detects gzip and bzip2 compression from magic bytes.
Use open_plain to force plain (uncompressed) reading,
open_gzip for gzip-only, or
open_bzip2 for bzip2-only.
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 from magic bytes.
For plain files this is equivalent to open_plain.
For gzip files it delegates to open_gzip; for bzip2
files to open_bzip2.
Decompression safety: Gzip and bzip2 files are decompressed with a
hard cap of DEFAULT_MAX_DECOMPRESSED_BYTES (256 GiB) to prevent
decompression bombs. Use open_gzip_with_limit
or open_bzip2_with_limit for a custom limit.
Note: gzip and bzip2 support require the gzip and bzip2 feature
flags respectively. Both are enabled by default. If a compressed file is
opened without the corresponding feature, it will be misinterpreted as
plain and fail with InvalidHeader.
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.
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, auto-detecting compression.
Non-fatal header issues (unusual MAP field, unexpected nversion,
non-standard axis mapping, etc.) are collected as warning strings
instead of causing a hard error. Only genuinely unreadable files
(negative dimensions, unsupported mode, IO failure) return Err.
Sourcepub fn shape(&self) -> VolumeShape
pub fn shape(&self) -> VolumeShape
Volume dimensions of the opened file.
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 from the file.
Supports arbitrary sub-blocks; non-contiguous regions are gathered into a contiguous buffer automatically.
Sourcepub fn read_block<T: Voxel>(
&self,
offset: [usize; 3],
shape: [usize; 3],
) -> Result<VoxelBlock<T>, Error>
👎Deprecated since 0.2.4: use subregion instead
pub fn read_block<T: Voxel>( &self, offset: [usize; 3], shape: [usize; 3], ) -> Result<VoxelBlock<T>, Error>
use subregion instead
Read and decode a block of voxels to the specified type.
Returns an error if T does not match the file’s voxel mode.
Use subregion instead — it is
available on all reader types and behaves identically.
Source§impl Reader
impl Reader
Sourcepub fn data_bytes(&self) -> &[u8] ⓘ
pub fn data_bytes(&self) -> &[u8] ⓘ
Get a reference to the raw data bytes
Sourcepub fn validate_header_stats(&self) -> Result<(), Error>
pub fn validate_header_stats(&self) -> Result<(), Error>
Cross-check header statistics against actual data.
Computes dmin, dmax, dmean and rms from the data block and
compares them with the header values using a 1 % relative tolerance
(matching Python mrcfile’s np.isclose(rtol=0.01)).
§Errors
Returns Error::StatsMismatch if any statistic deviates by more than 1 %.
Sourcepub fn endian(&self) -> FileEndian
pub fn endian(&self) -> FileEndian
Get the file endianness
Sourcepub fn ext_header_bytes(&self) -> &[u8] ⓘ
pub fn ext_header_bytes(&self) -> &[u8] ⓘ
Get the extended header bytes, if any.
Source§impl Reader
impl Reader
Sourcepub fn slices<T: Voxel>(&self) -> RegionIter<'_, T, Reader, SliceStepper> ⓘ
pub fn slices<T: Voxel>(&self) -> RegionIter<'_, T, Reader, SliceStepper> ⓘ
Sourcepub fn slabs<T: Voxel>(
&self,
k: usize,
) -> RegionIter<'_, T, Reader, SlabStepper> ⓘ
pub fn slabs<T: Voxel>( &self, k: usize, ) -> RegionIter<'_, T, Reader, SlabStepper> ⓘ
Sourcepub fn tiles<T: Voxel>(
&self,
tile_shape: [usize; 3],
) -> Result<RegionIter<'_, T, Reader, TileStepper>, Error>
pub fn tiles<T: Voxel>( &self, tile_shape: [usize; 3], ) -> Result<RegionIter<'_, T, Reader, TileStepper>, Error>
Sourcepub fn volumes<T: Voxel>(
&self,
) -> Result<RegionIter<'_, T, Reader, SlabStepper>, Error>
pub fn volumes<T: Voxel>( &self, ) -> Result<RegionIter<'_, T, Reader, SlabStepper>, Error>
Sourcepub fn subregion<T: Voxel>(
&self,
offset: [usize; 3],
shape: [usize; 3],
) -> Result<VoxelBlock<T>, Error>
pub fn subregion<T: Voxel>( &self, offset: [usize; 3], shape: [usize; 3], ) -> Result<VoxelBlock<T>, Error>
Sourcepub fn read_volume<T: Voxel>(&self) -> Result<VoxelBlock<T>, Error>
pub fn read_volume<T: Voxel>(&self) -> Result<VoxelBlock<T>, Error>
Sourcepub fn read_volume_u8(&self) -> Result<VoxelBlock<u8>, Error>
pub fn read_volume_u8(&self) -> Result<VoxelBlock<u8>, Error>
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>> + '_>
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>> + '_>
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>> + '_>
Sourcepub fn parse_extended_header(&self) -> ExtHeaderData
pub fn parse_extended_header(&self) -> ExtHeaderData
See [ReaderCore::parse_extended_header]
Sourcepub fn fei1_metadata(&self) -> Option<Vec<Fei1Metadata>>
pub fn fei1_metadata(&self) -> Option<Vec<Fei1Metadata>>
See [ReaderCore::fei1_metadata]
Sourcepub fn fei2_metadata(&self) -> Option<Vec<Fei2Metadata>>
pub fn fei2_metadata(&self) -> Option<Vec<Fei2Metadata>>
See [ReaderCore::fei2_metadata]
Sourcepub fn ccp4_records(&self) -> Option<Vec<Ccp4Record>>
pub fn ccp4_records(&self) -> Option<Vec<Ccp4Record>>
See [ReaderCore::ccp4_records]
Sourcepub fn mrco_records(&self) -> Option<Vec<MrcoRecord>>
pub fn mrco_records(&self) -> Option<Vec<MrcoRecord>>
See [ReaderCore::mrco_records]
Sourcepub fn seri_records(&self) -> Option<Vec<SeriRecord>>
pub fn seri_records(&self) -> Option<Vec<SeriRecord>>
See [ReaderCore::seri_records]
Sourcepub fn agar_records(&self) -> Option<Vec<AgarRecord>>
pub fn agar_records(&self) -> Option<Vec<AgarRecord>>
See [ReaderCore::agar_records]
Sourcepub fn imod_metadata(&self) -> Option<ImodMetadata>
pub fn imod_metadata(&self) -> Option<ImodMetadata>
See [ReaderCore::imod_metadata]
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<R> ConvertMethods for Rwhere
R: VoxelSource + ReaderCore,
impl<R> ConvertMethods for Rwhere
R: VoxelSource + ReaderCore,
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R> ReaderMethods for Rwhere
R: VoxelSource + ReaderCore,
impl<R> ReaderMethods for Rwhere
R: VoxelSource + ReaderCore,
Source§fn slices<T: Voxel>(&self) -> RegionIter<'_, T, Self, SliceStepper> ⓘ
fn slices<T: Voxel>(&self) -> RegionIter<'_, T, Self, SliceStepper> ⓘ
VoxelBlocks. Read moreSource§fn slabs<T: Voxel>(&self, k: usize) -> RegionIter<'_, T, Self, SlabStepper> ⓘ
fn slabs<T: Voxel>(&self, k: usize) -> RegionIter<'_, T, Self, SlabStepper> ⓘ
Source§fn tiles<T: Voxel>(
&self,
tile_shape: [usize; 3],
) -> Result<RegionIter<'_, T, Self, TileStepper>, Error>
fn tiles<T: Voxel>( &self, tile_shape: [usize; 3], ) -> Result<RegionIter<'_, T, Self, TileStepper>, Error>
VoxelBlocks. Read moreSource§fn volumes<T: Voxel>(
&self,
) -> Result<RegionIter<'_, T, Self, SlabStepper>, Error>where
Self: Sized,
fn volumes<T: Voxel>(
&self,
) -> Result<RegionIter<'_, T, Self, SlabStepper>, Error>where
Self: Sized,
Source§fn subregion<T: Voxel>(
&self,
offset: [usize; 3],
shape: [usize; 3],
) -> Result<VoxelBlock<T>, Error>
fn subregion<T: Voxel>( &self, offset: [usize; 3], shape: [usize; 3], ) -> Result<VoxelBlock<T>, Error>
VoxelBlock. Read moreSource§fn read_volume<T: Voxel>(&self) -> Result<VoxelBlock<T>, Error>
fn read_volume<T: Voxel>(&self) -> Result<VoxelBlock<T>, Error>
VoxelBlock<T>. Read moreSource§fn read_volume_u8(&self) -> Result<VoxelBlock<u8>, Error>
fn read_volume_u8(&self) -> Result<VoxelBlock<u8>, Error>
u8, unpacking from Mode 101 (Packed4Bit). Read moreSource§fn slices_u8(
&self,
) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>
fn slices_u8( &self, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>
u8, narrowing from Mode 6 (Uint16)
or unpacking from Mode 101 (Packed4Bit). Read moreSource§fn slabs_u8(
&self,
k: usize,
) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>
fn slabs_u8( &self, k: usize, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>
u8, narrowing from Mode 6 (Uint16)
or unpacking from Mode 101 (Packed4Bit). Read more