1use crate::dataset::{DataToken, SeqTokenType};
10use crate::stateful::encode::StatefulEncoder;
11use dcmnorm_core::header::Header;
12use dcmnorm_core::{DataElementHeader, Length, Tag, VR};
13use dcmnorm_encoding::encode::EncodeTo;
14use dcmnorm_encoding::text::SpecificCharacterSet;
15use dcmnorm_encoding::transfer_syntax::DynEncoder;
16use dcmnorm_encoding::TransferSyntax;
17use snafu::{Backtrace, OptionExt, ResultExt, Snafu};
18use std::io::Write;
19
20#[derive(Debug, Snafu)]
21#[non_exhaustive]
22pub enum Error {
23 #[snafu(display("Unsupported transfer syntax {} ({})", ts_uid, ts_alias))]
25 UnsupportedTransferSyntax {
26 ts_uid: &'static str,
27 ts_alias: &'static str,
28 backtrace: Backtrace,
29 },
30 #[snafu(display("Unsupported character set {:?}", charset))]
32 UnsupportedCharacterSet {
33 charset: SpecificCharacterSet,
34 backtrace: Backtrace,
35 },
36 #[snafu(display("Unexpected token {:?} without element header", token))]
38 UnexpectedToken {
39 token: DataToken,
40 backtrace: Backtrace,
41 },
42 #[snafu(display("Could not write element header tagged {}", tag))]
43 WriteHeader {
44 tag: Tag,
45 #[snafu(backtrace)]
46 source: crate::stateful::encode::Error,
47 },
48 #[snafu(display("Could not write item header"))]
49 WriteItemHeader {
50 #[snafu(backtrace)]
51 source: crate::stateful::encode::Error,
52 },
53
54 #[snafu(display("Could not write sequence delimiter"))]
55 WriteSequenceDelimiter {
56 #[snafu(backtrace)]
57 source: crate::stateful::encode::Error,
58 },
59
60 #[snafu(display("Could not write item delimiter"))]
61 WriteItemDelimiter {
62 #[snafu(backtrace)]
63 source: crate::stateful::encode::Error,
64 },
65
66 #[snafu(display("Could not write element value"))]
67 WriteValue {
68 #[snafu(backtrace)]
69 source: crate::stateful::encode::Error,
70 },
71 #[snafu(display("Could not flush buffer"))]
72 FlushBuffer {
73 source: std::io::Error,
74 backtrace: Backtrace,
75 },
76}
77
78pub type Result<T> = std::result::Result<T, Error>;
79
80#[derive(Debug)]
82struct SeqToken {
83 typ: SeqTokenType,
85 len: Length,
88}
89
90#[derive(Debug, Default, Copy, Clone, Eq, Hash, PartialEq)]
93#[non_exhaustive]
94pub enum ExplicitLengthSqItemStrategy {
95 #[default]
106 SetUndefined,
107 NoChange,
117 }
122
123#[derive(Debug, Default, Copy, Clone, Eq, Hash, PartialEq)]
125#[non_exhaustive]
126pub struct DataSetWriterOptions {
127 pub explicit_length_sq_item_strategy: ExplicitLengthSqItemStrategy,
129}
130
131impl DataSetWriterOptions {
132 pub fn explicit_length_sq_item_strategy(
134 mut self,
135 exp_length: ExplicitLengthSqItemStrategy,
136 ) -> Self {
137 self.explicit_length_sq_item_strategy = exp_length;
138 self
139 }
140}
141
142#[derive(Debug)]
146pub struct DataSetWriter<W, E, T = SpecificCharacterSet> {
147 printer: StatefulEncoder<W, E, T>,
148 seq_tokens: Vec<SeqToken>,
149 last_de: Option<DataElementHeader>,
150 options: DataSetWriterOptions,
151}
152
153impl<'w, W: 'w> DataSetWriter<W, DynEncoder<'w, W>>
154where
155 W: Write,
156{
157 pub fn with_ts(to: W, ts: &TransferSyntax) -> Result<Self> {
162 Self::with_ts_options(to, ts, DataSetWriterOptions::default())
163 }
164
165 pub fn with_ts_cs(to: W, ts: &TransferSyntax, charset: SpecificCharacterSet) -> Result<Self> {
171 Self::with_ts_cs_options(to, ts, charset, DataSetWriterOptions::default())
172 }
173
174 pub fn with_ts_options(
178 to: W,
179 ts: &TransferSyntax,
180 options: DataSetWriterOptions,
181 ) -> Result<Self> {
182 let encoder = ts.encoder_for().context(UnsupportedTransferSyntaxSnafu {
183 ts_uid: ts.uid(),
184 ts_alias: ts.name(),
185 })?;
186 Ok(DataSetWriter::new_with_codec_options(
187 to,
188 encoder,
189 SpecificCharacterSet::default(),
190 options,
191 ))
192 }
193
194 pub fn with_ts_cs_options(
198 to: W,
199 ts: &TransferSyntax,
200 charset: SpecificCharacterSet,
201 options: DataSetWriterOptions,
202 ) -> Result<Self> {
203 let encoder = ts.encoder_for().context(UnsupportedTransferSyntaxSnafu {
204 ts_uid: ts.uid(),
205 ts_alias: ts.name(),
206 })?;
207 Ok(DataSetWriter::new_with_codec_options(
208 to, encoder, charset, options,
209 ))
210 }
211}
212
213impl<W, E> DataSetWriter<W, E> {
214 #[inline]
217 pub fn new(to: W, encoder: E) -> Self {
218 DataSetWriter::new_with_options(to, encoder, DataSetWriterOptions::default())
219 }
220
221 #[inline]
224 pub fn new_with_options(to: W, encoder: E, options: DataSetWriterOptions) -> Self {
225 DataSetWriter {
226 printer: StatefulEncoder::new(to, encoder, SpecificCharacterSet::default()),
227 seq_tokens: Vec::new(),
228 last_de: None,
229 options,
230 }
231 }
232}
233
234impl<W, E, T> DataSetWriter<W, E, T> {
235 #[inline]
238 pub fn new_with_codec(to: W, encoder: E, text: T) -> Self {
239 DataSetWriter::new_with_codec_options(to, encoder, text, DataSetWriterOptions::default())
240 }
241
242 #[inline]
245 pub fn new_with_codec_options(
246 to: W,
247 encoder: E,
248 text: T,
249 options: DataSetWriterOptions,
250 ) -> Self {
251 DataSetWriter {
252 printer: StatefulEncoder::new(to, encoder, text),
253 seq_tokens: Vec::new(),
254 last_de: None,
255 options,
256 }
257 }
258}
259
260impl<W, E> DataSetWriter<W, E>
261where
262 W: Write,
263 E: EncodeTo<W>,
264{
265 #[inline]
267 pub fn write_sequence<I>(&mut self, tokens: I) -> Result<()>
268 where
269 I: IntoIterator<Item = DataToken>,
270 {
271 for token in tokens {
272 self.write(token)?;
273 }
274
275 Ok(())
276 }
277
278 pub fn write(&mut self, token: DataToken) -> Result<()> {
280 match token {
281 DataToken::SequenceStart { tag, len, .. } => {
282 match self.options.explicit_length_sq_item_strategy {
283 ExplicitLengthSqItemStrategy::SetUndefined => {
284 self.seq_tokens.push(SeqToken {
285 typ: SeqTokenType::Sequence,
286 len: Length::UNDEFINED,
287 });
288 self.write_impl(&DataToken::SequenceStart {
289 tag,
290 len: Length::UNDEFINED,
291 })?;
292 }
293 ExplicitLengthSqItemStrategy::NoChange => {
294 self.seq_tokens.push(SeqToken {
295 typ: SeqTokenType::Sequence,
296 len,
297 });
298 self.write_impl(&token)?;
299 }
300 }
301 Ok(())
302 }
303 DataToken::ItemStart { len } => {
304 match self.options.explicit_length_sq_item_strategy {
305 ExplicitLengthSqItemStrategy::SetUndefined => {
306 let len = if self
309 .last_de
310 .map(|h| h.is_encapsulated_pixeldata())
311 .unwrap_or(false)
312 {
313 len
314 } else {
315 Length::UNDEFINED
316 };
317 self.seq_tokens.push(SeqToken {
318 typ: SeqTokenType::Item,
319 len,
320 });
321 self.write_impl(&DataToken::ItemStart { len })?;
322 }
323 ExplicitLengthSqItemStrategy::NoChange => {
324 self.seq_tokens.push(SeqToken {
325 typ: SeqTokenType::Item,
326 len,
327 });
328 self.write_impl(&token)?;
329 }
330 }
331 Ok(())
332 }
333 DataToken::ItemEnd => {
334 if let Some(seq_start) = self.seq_tokens.pop() {
336 if seq_start.typ == SeqTokenType::Item && seq_start.len.is_undefined() {
337 self.write_impl(&token)?;
338 }
339 }
340 Ok(())
341 }
342 DataToken::SequenceEnd => {
343 if let Some(seq_start) = self.seq_tokens.pop() {
345 if seq_start.typ == SeqTokenType::Sequence && seq_start.len.is_undefined() {
346 self.write_impl(&token)?;
347 }
348 }
349 Ok(())
350 }
351 DataToken::ElementHeader(de) => {
352 self.last_de = Some(de);
354
355 Ok(())
357 }
358 token @ DataToken::PixelSequenceStart => {
359 self.last_de = Some(DataElementHeader {
362 tag: Tag(0x7fe0, 0x0010),
363 vr: VR::OB,
364 len: Length::UNDEFINED,
365 });
366
367 self.seq_tokens.push(SeqToken {
368 typ: SeqTokenType::Sequence,
369 len: Length::UNDEFINED,
370 });
371 self.write_impl(&token)
372 }
373 token @ DataToken::ItemValue(_)
374 | token @ DataToken::PrimitiveValue(_)
375 | token @ DataToken::OffsetTable(_) => self.write_impl(&token),
376 }
377 }
378
379 pub fn write_primitive_element(
384 &mut self,
385 header: DataElementHeader,
386 value: &dcmnorm_core::value::PrimitiveValue,
387 ) -> Result<()> {
388 self.printer
389 .encode_primitive_element(&header, value)
390 .context(WriteValueSnafu)?;
391 self.last_de = None;
392 Ok(())
393 }
394
395 pub fn write_item_value(&mut self, data: &[u8]) -> Result<()> {
398 self.printer.write_bytes(data).context(WriteValueSnafu)
399 }
400
401 pub fn write_element_header(&mut self, header: DataElementHeader) -> Result<()> {
404 self.printer
405 .encode_element_header(header)
406 .context(WriteHeaderSnafu { tag: header.tag })?;
407 self.last_de = None;
408 Ok(())
409 }
410
411 pub fn write_raw_bytes(&mut self, data: &[u8]) -> Result<()> {
413 self.printer.write_raw_bytes(data).context(WriteValueSnafu)
414 }
415
416 pub fn bytes_written(&self) -> u64 {
418 self.printer.bytes_written()
419 }
420
421 fn write_impl(&mut self, token: &DataToken) -> Result<()> {
422 match token {
423 DataToken::ElementHeader(header) => {
424 self.printer
425 .encode_element_header(*header)
426 .context(WriteHeaderSnafu { tag: header.tag })?;
427 }
428 DataToken::SequenceStart { tag, len } => {
429 self.printer
430 .encode_element_header(DataElementHeader::new(*tag, VR::SQ, *len))
431 .context(WriteHeaderSnafu { tag: *tag })?;
432 }
433 DataToken::PixelSequenceStart => {
434 let tag = Tag(0x7fe0, 0x0010);
435 self.printer
436 .encode_element_header(DataElementHeader::new(tag, VR::OB, Length::UNDEFINED))
437 .context(WriteHeaderSnafu { tag })?;
438 }
439 DataToken::SequenceEnd => {
440 self.printer
441 .encode_sequence_delimiter()
442 .context(WriteSequenceDelimiterSnafu)?;
443 }
444 DataToken::ItemStart { len } => {
445 self.printer
446 .encode_item_header(len.0)
447 .context(WriteItemHeaderSnafu)?;
448 }
449 DataToken::ItemEnd => {
450 self.printer
451 .encode_item_delimiter()
452 .context(WriteItemDelimiterSnafu)?;
453 }
454 DataToken::PrimitiveValue(ref value) => {
455 let last_de = self.last_de.take().with_context(|| UnexpectedTokenSnafu {
456 token: token.clone(),
457 })?;
458
459 self.printer
460 .encode_primitive_element(&last_de, value)
461 .context(WriteValueSnafu)?;
462 self.last_de = None;
463 }
464 DataToken::OffsetTable(table) => {
465 self.printer
466 .encode_offset_table(table)
467 .context(WriteValueSnafu)?;
468 }
469 DataToken::ItemValue(data) => {
470 self.printer.write_bytes(data).context(WriteValueSnafu)?;
471 }
472 }
473 Ok(())
474 }
475
476 pub fn flush(&mut self) -> Result<()> {
478 self.printer.flush().context(FlushBufferSnafu)
479 }
480}
481
482#[cfg(test)]
483mod tests {
484 use super::super::DataToken;
485 use super::{DataSetWriter, DataSetWriterOptions, ExplicitLengthSqItemStrategy};
486 use dcmnorm_core::{
487 header::{DataElementHeader, Length},
488 value::PrimitiveValue,
489 Tag, VR,
490 };
491 use dcmnorm_encoding::encode::{explicit_le::ExplicitVRLittleEndianEncoder, EncoderFor};
492
493 fn validate_dataset_writer<I>(
494 tokens: I,
495 ground_truth: &[u8],
496 writer_options: DataSetWriterOptions,
497 ) where
498 I: IntoIterator<Item = DataToken>,
499 {
500 let mut raw_out: Vec<u8> = vec![];
501 let encoder = EncoderFor::new(ExplicitVRLittleEndianEncoder::default());
502 let mut dset_writer =
503 DataSetWriter::new_with_options(&mut raw_out, encoder, writer_options);
504
505 dset_writer.write_sequence(tokens).unwrap();
506
507 assert_eq!(raw_out, ground_truth);
508 }
509
510 #[test]
511 fn write_sequence_explicit() {
512 let tokens = vec![
513 DataToken::SequenceStart {
514 tag: Tag(0x0018, 0x6011),
515 len: Length(46),
516 },
517 DataToken::ItemStart { len: Length(20) },
518 DataToken::ElementHeader(DataElementHeader {
519 tag: Tag(0x0018, 0x6012),
520 vr: VR::US,
521 len: Length(2),
522 }),
523 DataToken::PrimitiveValue(PrimitiveValue::U16([1].as_ref().into())),
524 DataToken::ElementHeader(DataElementHeader {
525 tag: Tag(0x0018, 0x6014),
526 vr: VR::US,
527 len: Length(2),
528 }),
529 DataToken::PrimitiveValue(PrimitiveValue::U16([2].as_ref().into())),
530 DataToken::ItemEnd,
531 DataToken::ItemStart { len: Length(10) },
532 DataToken::ElementHeader(DataElementHeader {
533 tag: Tag(0x0018, 0x6012),
534 vr: VR::US,
535 len: Length(2),
536 }),
537 DataToken::PrimitiveValue(PrimitiveValue::U16([4].as_ref().into())),
538 DataToken::ItemEnd,
539 DataToken::SequenceEnd,
540 DataToken::ElementHeader(DataElementHeader {
541 tag: Tag(0x0020, 0x4000),
542 vr: VR::LT,
543 len: Length(4),
544 }),
545 DataToken::PrimitiveValue(PrimitiveValue::Str("TEST".into())),
546 ];
547
548 #[rustfmt::skip]
549 static GROUND_TRUTH_LENGTH_UNDEFINED: &[u8] = &[
550 0x18, 0x00, 0x11, 0x60, b'S', b'Q', 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x18, 0x00, 0x12, 0x60, b'U', b'S', 0x02, 0x00, 0x01, 0x00, 0x18, 0x00, 0x14, 0x60, b'U', b'S', 0x02, 0x00, 0x02, 0x00, 0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x18, 0x00, 0x12, 0x60, b'U', b'S', 0x02, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xdd, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x40, b'L', b'T', 0x04, 0x00, b'T', b'E', b'S', b'T', ];
573
574 #[rustfmt::skip]
575 static GROUND_TRUTH_NO_CHANGE: &[u8] = &[
576 0x18, 0x00, 0x11, 0x60, b'S', b'Q', 0x00, 0x00, 0x2e, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0xe0, 0x14, 0x00, 0x00, 0x00, 0x18, 0x00, 0x12, 0x60, b'U', b'S', 0x02, 0x00, 0x01, 0x00, 0x18, 0x00, 0x14, 0x60, b'U', b'S', 0x02, 0x00, 0x02, 0x00, 0xfe, 0xff, 0x00, 0xe0, 0x0a, 0x00, 0x00, 0x00, 0x18, 0x00, 0x12, 0x60, b'U', b'S', 0x02, 0x00, 0x04, 0x00, 0x20, 0x00, 0x00, 0x40, b'L', b'T', 0x04, 0x00, b'T', b'E', b'S', b'T', ];
596
597 let no_change = DataSetWriterOptions {
598 explicit_length_sq_item_strategy: ExplicitLengthSqItemStrategy::NoChange,
599 };
600 validate_dataset_writer(tokens.clone(), GROUND_TRUTH_NO_CHANGE, no_change);
601 validate_dataset_writer(
602 tokens,
603 GROUND_TRUTH_LENGTH_UNDEFINED,
604 DataSetWriterOptions::default(),
605 );
606 }
607
608 #[test]
609 fn write_element_overrides_len() {
610 let tokens = vec![
611 DataToken::ElementHeader(DataElementHeader {
612 tag: Tag(0x0008, 0x0005),
614 vr: VR::CS,
615 len: Length(10),
616 }),
617 DataToken::PrimitiveValue(PrimitiveValue::from("ISO_IR 100")),
618 DataToken::ElementHeader(DataElementHeader {
619 tag: Tag(0x0008, 0x0090),
621 vr: VR::PN,
622 len: Length("Simões^João".len() as u32),
624 }),
625 DataToken::PrimitiveValue(PrimitiveValue::from("Simões^João")),
626 ];
627
628 #[rustfmt::skip]
629 static GROUND_TRUTH: &[u8] = &[
630 0x08, 0x00, 0x05, 0x00, b'C', b'S', 0x0a, 0x00, b'I', b'S', b'O', b'_', b'I', b'R', b' ', b'1', b'0', b'0', 0x08, 0x00, 0x90, 0x00, b'P', b'N', 0x0c, 0x00, b'S', b'i', b'm', 0xF5, b'e', b's', b'^', b'J', b'o', 0xE3, b'o', b' '
641 ];
642 validate_dataset_writer(tokens, GROUND_TRUTH, DataSetWriterOptions::default());
643 }
644
645 #[test]
646 fn write_sequence_implicit() {
647 let tokens = vec![
648 DataToken::SequenceStart {
649 tag: Tag(0x0018, 0x6011),
650 len: Length::UNDEFINED,
651 },
652 DataToken::ItemStart {
653 len: Length::UNDEFINED,
654 },
655 DataToken::ElementHeader(DataElementHeader {
656 tag: Tag(0x0018, 0x6012),
657 vr: VR::US,
658 len: Length(2),
659 }),
660 DataToken::PrimitiveValue(PrimitiveValue::U16([1].as_ref().into())),
661 DataToken::ElementHeader(DataElementHeader {
662 tag: Tag(0x0018, 0x6014),
663 vr: VR::US,
664 len: Length(2),
665 }),
666 DataToken::PrimitiveValue(PrimitiveValue::U16([2].as_ref().into())),
667 DataToken::ItemEnd,
668 DataToken::ItemStart {
669 len: Length::UNDEFINED,
670 },
671 DataToken::ElementHeader(DataElementHeader {
672 tag: Tag(0x0018, 0x6012),
673 vr: VR::US,
674 len: Length(2),
675 }),
676 DataToken::PrimitiveValue(PrimitiveValue::U16([4].as_ref().into())),
677 DataToken::ItemEnd,
678 DataToken::SequenceEnd,
679 DataToken::ElementHeader(DataElementHeader {
680 tag: Tag(0x0020, 0x4000),
681 vr: VR::LT,
682 len: Length(4),
683 }),
684 DataToken::PrimitiveValue(PrimitiveValue::Str("TEST".into())),
685 ];
686
687 #[rustfmt::skip]
688 static GROUND_TRUTH: &[u8] = &[
689 0x18, 0x00, 0x11, 0x60, b'S', b'Q', 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x18, 0x00, 0x12, 0x60, b'U', b'S', 0x02, 0x00, 0x01, 0x00, 0x18, 0x00, 0x14, 0x60, b'U', b'S', 0x02, 0x00, 0x02, 0x00, 0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x18, 0x00, 0x12, 0x60, b'U', b'S', 0x02, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xdd, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x40, b'L', b'T', 0x04, 0x00, b'T', b'E', b'S', b'T', ];
715
716 let no_change = DataSetWriterOptions {
717 explicit_length_sq_item_strategy: ExplicitLengthSqItemStrategy::NoChange,
718 };
719 validate_dataset_writer(tokens.clone(), GROUND_TRUTH, no_change);
720 validate_dataset_writer(tokens, GROUND_TRUTH, DataSetWriterOptions::default());
721 }
722
723 #[test]
724 fn write_sequence_explicit_with_implicit_item_len() {
725 let tokens = vec![
726 DataToken::SequenceStart {
727 tag: Tag(0x0018, 0x6011),
728 len: Length(60),
729 },
730 DataToken::ItemStart {
731 len: Length::UNDEFINED,
732 },
733 DataToken::ElementHeader(DataElementHeader {
734 tag: Tag(0x0018, 0x6012),
735 vr: VR::US,
736 len: Length(2),
737 }),
738 DataToken::PrimitiveValue(PrimitiveValue::U16([1].as_ref().into())),
739 DataToken::ElementHeader(DataElementHeader {
740 tag: Tag(0x0018, 0x6014),
741 vr: VR::US,
742 len: Length(2),
743 }),
744 DataToken::PrimitiveValue(PrimitiveValue::U16([2].as_ref().into())),
745 DataToken::ItemEnd,
746 DataToken::ItemStart {
747 len: Length::UNDEFINED,
748 },
749 DataToken::ElementHeader(DataElementHeader {
750 tag: Tag(0x0018, 0x6012),
751 vr: VR::US,
752 len: Length(2),
753 }),
754 DataToken::PrimitiveValue(PrimitiveValue::U16([4].as_ref().into())),
755 DataToken::ItemEnd,
756 DataToken::SequenceEnd,
757 DataToken::ElementHeader(DataElementHeader {
758 tag: Tag(0x0020, 0x4000),
759 vr: VR::LT,
760 len: Length(4),
761 }),
762 DataToken::PrimitiveValue(PrimitiveValue::Str("TEST".into())),
763 ];
764
765 #[rustfmt::skip]
766 static GROUND_TRUTH_LENGTH_UNDEFINED: &[u8] = &[
767 0x18, 0x00, 0x11, 0x60, b'S', b'Q', 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x18, 0x00, 0x12, 0x60, b'U', b'S', 0x02, 0x00, 0x01, 0x00, 0x18, 0x00, 0x14, 0x60, b'U', b'S', 0x02, 0x00, 0x02, 0x00, 0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x18, 0x00, 0x12, 0x60, b'U', b'S', 0x02, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xdd, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x40, b'L', b'T', 0x04, 0x00, b'T', b'E', b'S', b'T', ];
784
785 #[rustfmt::skip]
786 static GROUND_TRUTH_NO_CHANGE: &[u8] = &[
787 0x18, 0x00, 0x11, 0x60, b'S', b'Q', 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x18, 0x00, 0x12, 0x60, b'U', b'S', 0x02, 0x00, 0x01, 0x00, 0x18, 0x00, 0x14, 0x60, b'U', b'S', 0x02, 0x00, 0x02, 0x00, 0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x18, 0x00, 0x12, 0x60, b'U', b'S', 0x02, 0x00, 0x04, 0x00, 0xfe, 0xff, 0x0d, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x40, b'L', b'T', 0x04, 0x00, b'T', b'E', b'S', b'T', ];
811 let no_change = DataSetWriterOptions {
812 explicit_length_sq_item_strategy: ExplicitLengthSqItemStrategy::NoChange,
813 };
814
815 validate_dataset_writer(tokens.clone(), GROUND_TRUTH_NO_CHANGE, no_change);
816 validate_dataset_writer(
817 tokens,
818 GROUND_TRUTH_LENGTH_UNDEFINED,
819 DataSetWriterOptions::default(),
820 );
821 }
822
823 #[test]
824 fn write_encapsulated_pixeldata() {
825 let tokens = vec![
826 DataToken::PixelSequenceStart,
827 DataToken::ItemStart { len: Length(0) },
828 DataToken::ItemEnd,
829 DataToken::ItemStart { len: Length(32) },
830 DataToken::ItemValue(vec![0x99; 32]),
831 DataToken::ItemEnd,
832 DataToken::SequenceEnd,
833 DataToken::ElementHeader(DataElementHeader::new(
834 Tag(0xfffc, 0xfffc),
835 VR::OB,
836 Length(8),
837 )),
838 DataToken::PrimitiveValue(PrimitiveValue::U8([0x00; 8].as_ref().into())),
839 ];
840
841 #[rustfmt::skip]
845 static GROUND_TRUTH: &[u8] = &[
846 0xe0, 0x7f, 0x10, 0x00, b'O', b'B', 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x00, 0xe0, 0x20, 0x00, 0x00, 0x00, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
858 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
859 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
860 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
861 0xfe, 0xff, 0xdd, 0xe0, 0x00, 0x00, 0x00, 0x00,
864 0xfc, 0xff, 0xfc, 0xff, b'O', b'B', 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
870 ];
871 let no_change = DataSetWriterOptions {
872 explicit_length_sq_item_strategy: ExplicitLengthSqItemStrategy::NoChange,
873 };
874 validate_dataset_writer(tokens.clone(), GROUND_TRUTH, no_change);
875 validate_dataset_writer(tokens, GROUND_TRUTH, DataSetWriterOptions::default());
876 }
877}