Skip to content

Commit

Permalink
Merge pull request #494 from Enet4/imp/parser/new_with_methods
Browse files Browse the repository at this point in the history
[parser] add ::new_with_ts functions
  • Loading branch information
Enet4 authored Apr 12, 2024
2 parents 938d818 + 0bd6266 commit 0af744f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
11 changes: 4 additions & 7 deletions object/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub type DefaultDicomObject<D = StandardDataDictionary> = FileDicomObject<mem::I

use dicom_core::header::Header;
use dicom_encoding::adapters::{PixelDataObject, RawPixelData};
use dicom_encoding::{text::SpecificCharacterSet, transfer_syntax::TransferSyntaxIndex};
use dicom_encoding::transfer_syntax::TransferSyntaxIndex;
use dicom_parser::dataset::{DataSetWriter, IntoTokens};
use dicom_transfer_syntax_registry::TransferSyntaxRegistry;
use smallvec::SmallVec;
Expand Down Expand Up @@ -424,8 +424,7 @@ where
.with_context(|| WriteUnsupportedTransferSyntaxSnafu {
uid: self.meta.transfer_syntax.clone(),
})?;
let cs = SpecificCharacterSet::default();
let mut dset_writer = DataSetWriter::with_ts_cs(to, ts, cs).context(CreatePrinterSnafu)?;
let mut dset_writer = DataSetWriter::with_ts(to, ts).context(CreatePrinterSnafu)?;

// We use the default options, because only the inner object knows if something needs to change
dset_writer
Expand Down Expand Up @@ -457,8 +456,7 @@ where
.with_context(|| WriteUnsupportedTransferSyntaxSnafu {
uid: self.meta.transfer_syntax.clone(),
})?;
let cs = SpecificCharacterSet::default();
let mut dset_writer = DataSetWriter::with_ts_cs(to, ts, cs).context(CreatePrinterSnafu)?;
let mut dset_writer = DataSetWriter::with_ts(to, ts).context(CreatePrinterSnafu)?;

// We use the default options, because only the inner object knows if something needs to change
dset_writer
Expand Down Expand Up @@ -488,8 +486,7 @@ where
.with_context(|| WriteUnsupportedTransferSyntaxSnafu {
uid: self.meta.transfer_syntax.clone(),
})?;
let cs = SpecificCharacterSet::default();
let mut dset_writer = DataSetWriter::with_ts_cs(to, ts, cs).context(CreatePrinterSnafu)?;
let mut dset_writer = DataSetWriter::with_ts(to, ts).context(CreatePrinterSnafu)?;

// write object
dset_writer
Expand Down
10 changes: 6 additions & 4 deletions object/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,8 @@ where

// read rest of data according to metadata, feed it to object
if let Some(ts) = ts_index.get(&meta.transfer_syntax) {
let cs = SpecificCharacterSet::default();
let mut dataset =
DataSetReader::new_with_ts_cs(file, ts, cs).context(CreateParserSnafu)?;
DataSetReader::new_with_ts(file, ts).context(CreateParserSnafu)?;

Ok(FileDicomObject {
meta,
Expand Down Expand Up @@ -458,9 +457,8 @@ where

// read rest of data according to metadata, feed it to object
if let Some(ts) = ts_index.get(&meta.transfer_syntax) {
let cs = SpecificCharacterSet::default();
let mut dataset =
DataSetReader::new_with_ts_cs(file, ts, cs).context(CreateParserSnafu)?;
DataSetReader::new_with_ts(file, ts).context(CreateParserSnafu)?;
let obj = InMemDicomObject::build_object(
&mut dataset,
dict,
Expand Down Expand Up @@ -1387,6 +1385,7 @@ where
///
/// If the attribute _Specific Character Set_ is found in the data set,
/// the last parameter is overridden accordingly.
/// See also [`write_dataset_with_ts`](Self::write_dataset_with_ts).
pub fn write_dataset_with_ts_cs<W>(
&self,
to: W,
Expand Down Expand Up @@ -1427,6 +1426,9 @@ where
///
/// **Note:** this method will not adjust the file meta group
/// to be semantically valid for the object.
/// Namely, the _Media Storage SOP Instance UID_
/// and _Media Storage SOP Class UID_
/// are not updated based on the receiving data set.
pub fn with_exact_meta(self, meta: FileMetaTable) -> FileDicomObject<Self> {
FileDicomObject { meta, obj: self }
}
Expand Down
19 changes: 17 additions & 2 deletions parser/src/dataset/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,23 @@ pub struct DataSetReader<S> {
}

impl<R> DataSetReader<DynStatefulDecoder<R>> {
/// Create a new iterator with the given byte source,
/// while considering the given transfer syntax and specific character set.
/// Create a new data set token reader with the given byte source,
/// while considering the given transfer syntax specifier.
#[inline]
pub fn new_with_ts(source: R, ts: &TransferSyntax) -> Result<Self>
where
R: Read,
{
Self::new_with_ts_cs_options(source, ts, Default::default(), Default::default())
}

/// Create a new data set token reader with the given byte source,
/// while considering the given transfer syntax specifier
/// and the specific character set to assume by default.
///
/// Note that the data set being read
/// can override the character set with the presence of a
/// _Specific Character Set_ data element.
#[inline]
pub fn new_with_ts_cs(source: R, ts: &TransferSyntax, cs: SpecificCharacterSet) -> Result<Self>
where
Expand Down
17 changes: 17 additions & 0 deletions parser/src/dataset/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,23 @@ impl<'w, W: 'w> DataSetWriter<W, DynEncoder<'w, W>>
where
W: Write,
{
/// Create a new data set writer
/// with the given transfer syntax specifier.
pub fn with_ts(to: W, ts: &TransferSyntax) -> Result<Self> {
let encoder = ts.encoder_for().context(UnsupportedTransferSyntaxSnafu {
ts_uid: ts.uid(),
ts_alias: ts.name(),
})?;
Ok(DataSetWriter::new_with_codec(to, encoder, SpecificCharacterSet::default()))
}

/// Create a new data set writer
/// with the given transfer syntax specifier
/// and the specific character set to assume by default.
///
/// Note that the data set being written
/// can override the character set with the presence of a
/// _Specific Character Set_ data element.
pub fn with_ts_cs(to: W, ts: &TransferSyntax, charset: SpecificCharacterSet) -> Result<Self> {
let encoder = ts.encoder_for().context(UnsupportedTransferSyntaxSnafu {
ts_uid: ts.uid(),
Expand Down

0 comments on commit 0af744f

Please sign in to comment.