Skip to content

Commit

Permalink
Merge pull request #102 from qbio/feature/remove-esize-from-extension…
Browse files Browse the repository at this point in the history
…-interface

Feature/remove esize from extension interface
  • Loading branch information
Enet4 authored Jun 6, 2023
2 parents 0b912cb + db7335d commit 759bb6c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 38 deletions.
2 changes: 1 addition & 1 deletion examples/gen_nifti/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
// generate some test data 256x256 float32
let data = ndarray::Array3::<f32>::zeros((256, 256, 1));

let extension1 = Extension::new(8 + 4, 3, vec![0, 0, 0, 0]);
let extension1 = Extension::new(2, vec![0, 0, 0, 0]);

let extension2 = Extension::from_str(6, "Hello World!");

Expand Down
41 changes: 4 additions & 37 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,8 @@
use crate::error::{NiftiError, Result};
use byteordered::{ByteOrdered, Endian};
use num_derive::FromPrimitive;
use std::io::{ErrorKind as IoErrorKind, Read};

/// Data type for representing a NIfTI-1.1 extension code
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, FromPrimitive)]
#[repr(u32)]
pub enum NiftiEcode {
/// Ignore the extension
NiftEcodeIgnore = 0,
/// DICOM
NiftiEcodeDicom = 2,
/// AFNI extension in XML format
NiftiEcodeAFNI = 4,
/// String Comment
NiftiEcodeComment = 6,
/// XCEDE extension in XML format
NiftiEcodeXCEDE = 8,
/// JimDimInfo
NiftiEcodeJimDimInfo = 10,
/// WorkflowFWDS
NiftiEcodeWorkflowFWDS = 12,
/// Freesurfer
NiftiEcodeFreesurfer = 14,
}

/// Data type for the extender code.
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub struct Extender([u8; 4]);
Expand Down Expand Up @@ -85,18 +62,8 @@ pub struct Extension {

impl Extension {
/// Create an extension out of its main components.
///
/// # Panics
/// If `esize` does not correspond to the full size
/// of the extension in bytes: `8 + edata.len()`
pub fn new(esize: i32, ecode: i32, edata: Vec<u8>) -> Self {
if esize as usize != 8 + edata.len() {
panic!(
"Illegal extension size: esize is {}, but full size is {}",
esize,
edata.len()
);
}
pub fn new(ecode: i32, edata: Vec<u8>) -> Self {
let esize = 8 + edata.len() as i32;

Extension {
esize,
Expand All @@ -112,7 +79,7 @@ impl Extension {
let padded_esize = (esize + 15) & !15;
let mut edata = edata.as_bytes().to_vec();
edata.resize(padded_esize as usize - 8, 0);
Extension::new(padded_esize, ecode, edata)
Extension::new(ecode, edata)
}

/// Obtain the claimed extension raw size (`esize` field).
Expand Down Expand Up @@ -201,7 +168,7 @@ impl ExtensionSequence {
return Err(NiftiError::IncompatibleLength(nb_bytes_written, data_size));
}

extensions.push(Extension::new(i32::max(esize, 8), ecode, edata));
extensions.push(Extension::new(ecode, edata));
offset += esize as usize;
}
}
Expand Down

0 comments on commit 759bb6c

Please sign in to comment.