Skip to main content

Reader

Struct Reader 

Source
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

Source

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.

Source

pub fn open_plain<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Open a plain (uncompressed) MRC file.

Source

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.

Source

pub fn shape(&self) -> VolumeShape

Volume dimensions of the opened file.

Source

pub fn mode(&self) -> Mode

Voxel data mode of the opened file.

Source

pub fn header(&self) -> &Header

A reference to the parsed MRC header.

Source

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.

Source

pub fn read_block<T: Voxel>( &self, offset: [usize; 3], shape: [usize; 3], ) -> Result<VoxelBlock<T>, Error>

👎Deprecated since 0.2.4:

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

Source

pub fn data_bytes(&self) -> &[u8]

Get a reference to the raw data bytes

Source

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 %.

Source

pub fn endian(&self) -> FileEndian

Get the file endianness

Source

pub fn ext_header_bytes(&self) -> &[u8]

Get the extended header bytes, if any.

Source§

impl Reader

Source

pub fn slices<T: Voxel>(&self) -> RegionIter<'_, T, Reader, SliceStepper>

Source

pub fn slabs<T: Voxel>( &self, k: usize, ) -> RegionIter<'_, T, Reader, SlabStepper>

Source

pub fn tiles<T: Voxel>( &self, tile_shape: [usize; 3], ) -> Result<RegionIter<'_, T, Reader, TileStepper>, Error>

Source

pub fn volumes<T: Voxel>( &self, ) -> Result<RegionIter<'_, T, Reader, SlabStepper>, Error>

Source

pub fn subregion<T: Voxel>( &self, offset: [usize; 3], shape: [usize; 3], ) -> Result<VoxelBlock<T>, Error>

Source

pub fn read_volume<T: Voxel>(&self) -> Result<VoxelBlock<T>, Error>

Source

pub fn read_volume_u8(&self) -> Result<VoxelBlock<u8>, Error>

Source

pub fn slices_u8( &self, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>

Source

pub fn slabs_u8( &self, k: usize, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>

Source

pub fn slices_mode0( &self, interp: M0Interpretation, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<f32>, Error>> + '_>

Source

pub fn slabs_mode0( &self, k: usize, interp: M0Interpretation, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<f32>, Error>> + '_>

Source

pub fn convert<T>(&self) -> ConvertReader<'_, Reader, T>
where T: Voxel + ConvertFrom<f32>,

Source

pub fn parse_extended_header(&self) -> ExtHeaderData

See [ReaderCore::parse_extended_header]

Source

pub fn fei1_metadata(&self) -> Option<Vec<Fei1Metadata>>

See [ReaderCore::fei1_metadata]

Source

pub fn fei2_metadata(&self) -> Option<Vec<Fei2Metadata>>

See [ReaderCore::fei2_metadata]

Source

pub fn ccp4_records(&self) -> Option<Vec<Ccp4Record>>

See [ReaderCore::ccp4_records]

Source

pub fn mrco_records(&self) -> Option<Vec<MrcoRecord>>

See [ReaderCore::mrco_records]

Source

pub fn seri_records(&self) -> Option<Vec<SeriRecord>>

See [ReaderCore::seri_records]

Source

pub fn agar_records(&self) -> Option<Vec<AgarRecord>>

See [ReaderCore::agar_records]

Source

pub fn imod_metadata(&self) -> Option<ImodMetadata>

See [ReaderCore::imod_metadata]

Source§

impl Reader

Source

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.

Source

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.

Source

pub fn open_gzip_permissive<P: AsRef<Path>>( path: P, ) -> Result<(Self, Vec<String>), Error>

Open a gzip-compressed MRC file in permissive mode.

Trait Implementations§

Source§

impl Debug for Reader

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<R> ConvertMethods for R
where R: VoxelSource + ReaderCore,

Source§

fn convert<T>(&self) -> ConvertReader<'_, R, T>
where T: Voxel + ConvertFrom<f32>,

Return a wrapper that auto-converts all reads to type T.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<R> ReaderMethods for R
where R: VoxelSource + ReaderCore,

Source§

fn slices<T: Voxel>(&self) -> RegionIter<'_, T, Self, SliceStepper>

Iterate over Z-slices (1 voxel thick along Z) as VoxelBlocks. Read more
Source§

fn slabs<T: Voxel>(&self, k: usize) -> RegionIter<'_, T, Self, SlabStepper>

Iterate over Z-slabs of k slices as VoxelBlocks. Read more
Source§

fn tiles<T: Voxel>( &self, tile_shape: [usize; 3], ) -> Result<RegionIter<'_, T, Self, TileStepper>, Error>

Iterate over 3D tiles of the given shape as VoxelBlocks. Read more
Source§

fn volumes<T: Voxel>( &self, ) -> Result<RegionIter<'_, T, Self, SlabStepper>, Error>
where Self: Sized,

Iterate over sub-volumes of a volume-stack file. Read more
Source§

fn subregion<T: Voxel>( &self, offset: [usize; 3], shape: [usize; 3], ) -> Result<VoxelBlock<T>, Error>

Read a single arbitrary 3D sub-region as a VoxelBlock. Read more
Source§

fn read_volume<T: Voxel>(&self) -> Result<VoxelBlock<T>, Error>

Read the entire volume as a VoxelBlock<T>. Read more
Source§

fn read_volume_u8(&self) -> Result<VoxelBlock<u8>, Error>

Read the entire volume as u8, unpacking from Mode 101 (Packed4Bit). Read more
Source§

fn slices_u8( &self, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>

Iterate over Z-slices as u8, narrowing from Mode 6 (Uint16) or unpacking from Mode 101 (Packed4Bit). Read more
Source§

fn slabs_u8( &self, k: usize, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<u8>, Error>> + '_>

Iterate over Z-slabs as u8, narrowing from Mode 6 (Uint16) or unpacking from Mode 101 (Packed4Bit). Read more
Source§

fn slices_mode0( &self, interp: M0Interpretation, ) -> Box<dyn Iterator<Item = Result<VoxelBlock<f32>, Error>> + '_>

Iterate over Z-slices of a Mode 0 file, interpreting as signed or unsigned. Read more
Source§

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, interpreting as signed or unsigned. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more