dcmnorm-object 0.20.0

In-house DICOM in-memory object model and Part 10 file I/O, replacing dicom-object. See README.md.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Whole-file DICOM objects: a [`FileMetaTable`] plus an object (normally [`InMemDicomObject`]),
//! and the [`OpenFileOptions`] builder for reading them from disk/a byte source.

use std::fs::File;
use std::io::{BufReader, Cursor, Read, Write};
use std::path::Path;

use dcmnorm_encoding::transfer_syntax::TransferSyntaxIndex;
use dcmnorm_parser::dataset::read::DataSetReaderOptions;
use dcmnorm_transcode::TransferSyntaxRegistry;

use crate::error::{ReadError, WriteError};
use crate::mem::InMemDicomObject;
use crate::meta::{FileMetaTable, DICM_MAGIC, PREAMBLE_LEN};

/// A DICOM object with its accompanying File Meta Information.
#[derive(Debug, Clone, PartialEq)]
pub struct FileDicomObject<T> {
    pub(crate) meta: FileMetaTable,
    pub(crate) object: T,
}

/// The default, in-memory DICOM file object type - a [`FileMetaTable`] plus an
/// [`InMemDicomObject`].
pub type DefaultDicomObject = FileDicomObject<InMemDicomObject>;

impl<T> FileDicomObject<T> {
    pub fn meta(&self) -> &FileMetaTable {
        &self.meta
    }

    pub fn meta_mut(&mut self) -> &mut FileMetaTable {
        &mut self.meta
    }

    pub fn into_inner(self) -> T {
        self.object
    }
}

impl std::ops::Deref for DefaultDicomObject {
    type Target = InMemDicomObject;
    fn deref(&self) -> &Self::Target {
        &self.object
    }
}

impl std::ops::DerefMut for DefaultDicomObject {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.object
    }
}

impl DefaultDicomObject {
    /// Construct a new, empty object already carrying the given meta table.
    pub fn new_empty_with_meta(meta: FileMetaTable) -> Self {
        FileDicomObject {
            meta,
            object: InMemDicomObject::new_empty(),
        }
    }

    /// Insert or replace a text element by tag and VR, creating it if absent.
    pub fn put_str(
        &mut self,
        tag: dcmnorm_core::Tag,
        vr: dcmnorm_core::VR,
        value: impl Into<String>,
    ) {
        self.object.put_str(tag, vr, value);
    }

    /// Read a Part 10 file (128-byte preamble + "DICM" magic + meta group + data set) from
    /// `path`.
    pub fn open_file(path: impl AsRef<Path>) -> Result<Self, ReadError> {
        let file = File::open(path.as_ref()).map_err(|source| ReadError::Io {
            source,
            context: "opening file",
        })?;
        Self::from_reader(BufReader::new(file))
    }

    /// Read a Part 10 object (128-byte preamble + "DICM" magic + meta group + data set) from
    /// any [`Read`] source.
    pub fn from_reader(mut source: impl Read) -> Result<Self, ReadError> {
        let mut meta = FileMetaTable::read_from(&mut source)?.ok_or(ReadError::NotDicom)?;
        let ts = meta
            .transfer_syntax_ts()
            .ok_or_else(|| ReadError::UnsupportedTransferSyntax {
                uid: meta.transfer_syntax.clone(),
            })?;
        let object = InMemDicomObject::read_dataset_with_ts(source, ts)?;
        backfill_media_storage_uids(&mut meta, &object);
        Ok(FileDicomObject { meta, object })
    }

    /// Write this object as a full Part 10 file (preamble + "DICM" magic + meta group + data
    /// set) to `path`.
    pub fn write_to_file(&self, path: impl AsRef<Path>) -> Result<(), WriteError> {
        let file = File::create(path.as_ref()).map_err(|source| WriteError::Io {
            source,
            context: "creating file",
        })?;
        self.write_all(&mut std::io::BufWriter::new(file))
    }

    /// Write this object as a full Part 10 stream (preamble + "DICM" magic + meta group + data
    /// set) to any [`Write`] sink.
    pub fn write_all(&self, mut to: impl Write) -> Result<(), WriteError> {
        self.meta.write_to(&mut to)?;
        let ts = TransferSyntaxRegistry
            .get(crate::meta::io_util::trim_uid(&self.meta.transfer_syntax))
            .ok_or_else(|| WriteError::UnsupportedTransferSyntax {
                uid: self.meta.transfer_syntax.clone(),
            })?;
        self.object.write_dataset_with_ts(&mut to, ts)
    }

    /// Write this object as a full Part 10 stream, delegating `(7FE0,0010) PixelData` to
    /// `write_pixel_data` at its exact tag-order position. The second argument passed to the
    /// closure is the byte offset within `to` where the data set starts (right after the Part 10
    /// preamble + File Meta Information).
    pub fn write_all_with_pixel_data<W, E, F>(
        &self,
        mut to: W,
        write_pixel_data: F,
    ) -> Result<(), E>
    where
        W: Write,
        E: From<WriteError>,
        F: for<'w> FnOnce(
            &mut dcmnorm_parser::dataset::DataSetWriter<
                Box<dyn Write + 'w>,
                dcmnorm_encoding::transfer_syntax::DynEncoder<'w, Box<dyn Write + 'w>>,
            >,
            u64,
        ) -> Result<(), E>,
    {
        let mut meta_sink = CountingWriter {
            inner: &mut to,
            count: 0,
        };
        self.meta.write_to(&mut meta_sink)?;
        let dataset_start = meta_sink.count;
        let ts = TransferSyntaxRegistry
            .get(crate::meta::io_util::trim_uid(&self.meta.transfer_syntax))
            .ok_or_else(|| WriteError::UnsupportedTransferSyntax {
                uid: self.meta.transfer_syntax.clone(),
            })?;
        self.object
            .write_dataset_with_ts_and_pixel_data(&mut to, ts, |writer| {
                write_pixel_data(writer, dataset_start)
            })
    }
}

/// Whether to require/skip the 128-byte preamble when opening a file.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub enum ReadPreamble {
    #[default]
    Auto,
    Always,
}

/// Builder for reading a DICOM file with non-default options (an explicit preamble
/// requirement, or an early-stop tag for partial/fast reads).
#[derive(Debug, Clone, Default)]
pub struct OpenFileOptions {
    read_preamble: ReadPreamble,
    read_until: Option<dcmnorm_core::Tag>,
    flexible_decoding: bool,
}

impl OpenFileOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn read_preamble(mut self, mode: ReadPreamble) -> Self {
        self.read_preamble = mode;
        self
    }

    /// Stop reading as soon as an element with a tag greater than `tag` is encountered
    /// (inclusive of `tag` itself) - a fast path for partial/filtered reads that don't need
    /// the rest of the data set. Matches `dcmnorm`'s `--filter` CLI early-stop optimization.
    pub fn read_until(mut self, tag: dcmnorm_core::Tag) -> Self {
        self.read_until = Some(tag);
        self
    }

    /// Detect from the data set itself whether a Little Endian file is encoded with explicit or
    /// implicit VRs, instead of trusting its declared transfer syntax - for non-conformant files
    /// that declare Explicit VR Little Endian but write Implicit VR (see
    /// `DataSetReaderOptions::flexible_decoding`). Applies to [`Self::open_file`] and
    /// [`Self::from_reader`]. Off by default.
    pub fn flexible_decoding(mut self, flexible_decoding: bool) -> Self {
        self.flexible_decoding = flexible_decoding;
        self
    }

    pub fn open_file(self, path: impl AsRef<Path>) -> Result<DefaultDicomObject, ReadError> {
        let file = File::open(path.as_ref()).map_err(|source| ReadError::Io {
            source,
            context: "opening file",
        })?;
        self.from_reader(BufReader::new(file))
    }

    pub fn from_reader(self, mut source: impl Read) -> Result<DefaultDicomObject, ReadError> {
        let mut meta = self.read_meta(&mut source)?;
        let ts = meta
            .transfer_syntax_ts()
            .ok_or_else(|| ReadError::UnsupportedTransferSyntax {
                uid: meta.transfer_syntax.clone(),
            })?;

        let options = DataSetReaderOptions::default().flexible_decoding(self.flexible_decoding);
        let object = if let Some(stop_tag) = self.read_until {
            crate::mem::read_dataset_until(source, ts, stop_tag, options)?
        } else {
            InMemDicomObject::read_dataset_with_ts_options(source, ts, options)?
        };
        backfill_media_storage_uids(&mut meta, &object);
        Ok(FileDicomObject { meta, object })
    }

    /// Read everything in `path` up to (not including) a native PixelData value, and report
    /// where that value sits in the file instead of loading it - the building block for reading
    /// one frame of a large multi-frame object (seek to it) without paying for all the others.
    ///
    /// Returns `Ok(None)` whenever this shortcut doesn't apply and the caller should do a full
    /// read instead: an encapsulated or dataset-compressed (deflated) transfer syntax (no
    /// stable byte offsets into native pixels), or no top-level, defined-length PixelData.
    /// `read_until` is ignored. Elements after PixelData (trailing padding, digital signatures)
    /// are not read.
    pub fn open_file_until_pixel_data(
        self,
        path: impl AsRef<Path>,
    ) -> Result<Option<(DefaultDicomObject, PixelDataValueLocation)>, ReadError> {
        let file = File::open(path.as_ref()).map_err(|source| ReadError::Io {
            source,
            context: "opening file",
        })?;
        let mut source = CountingReader {
            inner: BufReader::new(file),
            count: 0,
        };
        let mut meta = self.read_meta(&mut source)?;
        let ts = meta
            .transfer_syntax_ts()
            .ok_or_else(|| ReadError::UnsupportedTransferSyntax {
                uid: meta.transfer_syntax.clone(),
            })?;
        if ts.is_encapsulated_pixel_data()
            || !matches!(ts.codec(), dcmnorm_encoding::transfer_syntax::Codec::None)
        {
            return Ok(None);
        }

        let (object, header) = crate::mem::read_dataset_until_pixel_data_header(&mut source, ts)?;
        let Some(header) = header else {
            return Ok(None);
        };
        let Some(length) = header.len.get() else {
            return Ok(None);
        };
        backfill_media_storage_uids(&mut meta, &object);
        Ok(Some((
            FileDicomObject { meta, object },
            PixelDataValueLocation {
                vr: header.vr,
                length,
                offset: source.count,
            },
        )))
    }

    /// Read `path` in full except for the large values `defer` picks out - plus, when
    /// `defer_pixel_sequences` is set, encapsulated pixel data - which are skipped by seeking
    /// past them and returned as file locations in [`DeferredValues`] instead (e.g. to emit as
    /// DICOM JSON BulkDataURIs without ever reading them). Deferred elements stay in the object
    /// with an empty placeholder value; see [`DeferredValues`].
    ///
    /// Returns `Ok(None)` when byte offsets in the file can't be relied on, so the caller should
    /// read in full instead: a dataset-compressed (deflated) transfer syntax, a data set that
    /// isn't a seekable Part 10 file, or a deferred range that runs past the end of the file
    /// (truncation - a full read reports that properly). `read_until` is ignored.
    pub fn open_file_deferring(
        self,
        path: impl AsRef<Path>,
        defer: &dyn Fn(&dcmnorm_core::header::DataElementHeader) -> bool,
        defer_pixel_sequences: bool,
    ) -> Result<Option<(DefaultDicomObject, crate::mem::DeferredValues)>, ReadError> {
        let file = File::open(path.as_ref()).map_err(|source| ReadError::Io {
            source,
            context: "opening file",
        })?;
        let file_len = file
            .metadata()
            .map_err(|source| ReadError::Io {
                source,
                context: "reading file metadata",
            })?
            .len();
        self.from_reader_deferring(
            BufReader::new(file),
            Some(file_len),
            defer,
            defer_pixel_sequences,
            false,
        )
    }

    /// [`Self::open_file_deferring`] over any seekable Part 10 source, positioned at its
    /// preamble: a byte slice in a `Cursor`, or a forward-only stream behind an adapter that
    /// implements forward `Seek` by reading and discarding (the reader only ever seeks forward,
    /// relative to its current position). `source_len`, when known, is checked against the
    /// deferred ranges as the file length is; with `None` that check is the caller's.
    ///
    /// With `stop_at_pixel_data`, nothing past the top-level PixelData header is read: PixelData
    /// is recorded as deferred where its value starts (length
    /// [`crate::DeferredValueLocation::UNDEFINED_LENGTH`] when encapsulated) and later elements
    /// are left out, as `dcm4che`'s `readDatasetUntilPixelData` does.
    pub fn from_reader_deferring<R: Read + std::io::Seek>(
        self,
        mut source: R,
        source_len: Option<u64>,
        defer: &dyn Fn(&dcmnorm_core::header::DataElementHeader) -> bool,
        defer_pixel_sequences: bool,
        stop_at_pixel_data: bool,
    ) -> Result<Option<(DefaultDicomObject, crate::mem::DeferredValues)>, ReadError> {
        let mut meta = self.read_meta(&mut source)?;
        let ts = meta
            .transfer_syntax_ts()
            .ok_or_else(|| ReadError::UnsupportedTransferSyntax {
                uid: meta.transfer_syntax.clone(),
            })?;
        if matches!(
            ts.codec(),
            dcmnorm_encoding::transfer_syntax::Codec::Dataset(_)
        ) {
            return Ok(None);
        }
        let base_offset = source.stream_position().map_err(|source| ReadError::Io {
            source,
            context: "locating data set",
        })?;

        let mut reader = dcmnorm_parser::dataset::DataSetReader::new_with_ts(source, ts)
            .map_err(|source| ReadError::Dataset { source })?;
        let (object, deferred) = crate::mem::build_dataset_deferring(
            &mut reader,
            false,
            defer,
            defer_pixel_sequences,
            stop_at_pixel_data,
            base_offset,
        )?;
        if source_len.is_some_and(|len| deferred.max_end() > len) {
            return Ok(None);
        }
        backfill_media_storage_uids(&mut meta, &object);
        Ok(Some((FileDicomObject { meta, object }, deferred)))
    }

    /// Read `path` in full except for top-level `(7FE0,0010) PixelData`, which is left on disk
    /// and described by [`crate::DeferredPixelData`] (native offset/length, or encapsulated
    /// Basic Offset Table plus per-fragment offsets/lengths). Every other element - including
    /// nested `IconImageSequence` pixels and trailing elements after `PixelData` - is loaded
    /// normally.
    ///
    /// Returns `Ok(None)` when the file cannot be read this way (dataset-compressed / deflated
    /// transfer syntax, or a deferred range running past EOF) so the caller can fall back to a
    /// full read.
    pub fn open_file_deferring_pixel_data(
        self,
        path: impl AsRef<Path>,
    ) -> Result<Option<(DefaultDicomObject, crate::mem::DeferredPixelData, File)>, ReadError> {
        use std::io::Seek;

        let mut file = File::open(path.as_ref()).map_err(|source| ReadError::Io {
            source,
            context: "opening file",
        })?;
        let file_len = file
            .metadata()
            .map_err(|source| ReadError::Io {
                source,
                context: "reading file metadata",
            })?
            .len();
        let (mut meta, object, pixel_data) = {
            let mut source = BufReader::new(&mut file);
            let meta = self.read_meta(&mut source)?;
            let ts =
                meta.transfer_syntax_ts()
                    .ok_or_else(|| ReadError::UnsupportedTransferSyntax {
                        uid: meta.transfer_syntax.clone(),
                    })?;
            if matches!(
                ts.codec(),
                dcmnorm_encoding::transfer_syntax::Codec::Dataset(_)
            ) {
                return Ok(None);
            }
            let base_offset = source.stream_position().map_err(|source| ReadError::Io {
                source,
                context: "locating data set",
            })?;

            let mut reader = dcmnorm_parser::dataset::DataSetReader::new_with_ts(source, ts)
                .map_err(|source| ReadError::Dataset { source })?;
            let (object, pixel_data) =
                crate::mem::build_dataset_deferring_pixel_data(&mut reader, base_offset)?;
            if pixel_data.max_end() > file_len {
                return Err(ReadError::Io {
                    source: std::io::Error::new(
                        std::io::ErrorKind::UnexpectedEof,
                        "deferred PixelData extends past end of file",
                    ),
                    context: "validating PixelData bounds",
                });
            }
            (meta, object, pixel_data)
        };
        backfill_media_storage_uids(&mut meta, &object);
        Ok(Some((FileDicomObject { meta, object }, pixel_data, file)))
    }

    fn read_meta(&self, mut source: impl Read) -> Result<FileMetaTable, ReadError> {
        Ok(match self.read_preamble {
            // Peek-and-detect: falls back to treating `source` as meta-less if the preamble or
            // "DICM" magic aren't there.
            ReadPreamble::Auto => {
                FileMetaTable::read_from(&mut source)?.ok_or(ReadError::NotDicom)?
            }
            // No detection/fallback: the preamble and magic are required, and their absence (or
            // mismatch) is a hard error rather than a silent reinterpretation as meta-less -
            // matching what a caller who explicitly opted out of auto-detection would expect.
            ReadPreamble::Always => {
                let mut preamble = [0u8; PREAMBLE_LEN];
                source
                    .read_exact(&mut preamble)
                    .map_err(|source| ReadError::Io {
                        source,
                        context: "reading 128-byte preamble",
                    })?;
                let mut magic = [0u8; 4];
                source
                    .read_exact(&mut magic)
                    .map_err(|source| ReadError::Io {
                        source,
                        context: "reading \"DICM\" magic",
                    })?;
                if &magic != DICM_MAGIC {
                    return Err(ReadError::NotDicom);
                }
                FileMetaTable::read_meta_group(&mut source)?
            }
        })
    }
}

/// Where a native PixelData value sits in a file, as found by
/// [`OpenFileOptions::open_file_until_pixel_data`]: its declared VR and byte length, and the
/// absolute byte offset of its first value byte.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PixelDataValueLocation {
    pub vr: dcmnorm_core::VR,
    pub length: u32,
    pub offset: u64,
}

/// Counts bytes handed to the parser. Both the meta reader and the data set reader consume
/// exactly what they parse (no read-ahead past the element in hand), so the count after
/// stopping at PixelData's header is the file offset of its value.
struct CountingReader<R> {
    inner: R,
    count: u64,
}

impl<R: Read> Read for CountingReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let read = self.inner.read(buf)?;
        self.count += read as u64;
        Ok(read)
    }
}

struct CountingWriter<W> {
    inner: W,
    count: u64,
}

impl<W: Write> Write for CountingWriter<W> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let written = self.inner.write(buf)?;
        self.count += written as u64;
        Ok(written)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.inner.flush()
    }
}

const SOP_CLASS_UID: dcmnorm_core::Tag = dcmnorm_core::Tag(0x0008, 0x0016);
const SOP_INSTANCE_UID: dcmnorm_core::Tag = dcmnorm_core::Tag(0x0008, 0x0018);

/// Some non-conformant DICOM writers (seen from fo-dicom-generated files) omit
/// MediaStorageSOPClassUID/MediaStorageSOPInstanceUID (0002,0002)/(0002,0003) from the file
/// meta group entirely, even though they're Type 1 per PS3.10 - `dicom-object`'s reader
/// tolerated this by backfilling them from the data set's own SOPClassUID/SOPInstanceUID
/// (0008,0016)/(0008,0018), which are almost always present redundantly. Preserved here for
/// parity - confirmed against a real fixture (`test/files/sr.dcm`, written by fo-dicom 4.0.7).
fn backfill_media_storage_uids(meta: &mut FileMetaTable, object: &InMemDicomObject) {
    if meta.media_storage_sop_class_uid.is_empty() {
        if let Some(uid) = object.get(SOP_CLASS_UID).and_then(|e| e.to_str().ok()) {
            meta.media_storage_sop_class_uid = uid.trim_end_matches(['\0', ' ']).to_owned();
        }
    }
    if meta.media_storage_sop_instance_uid.is_empty() {
        if let Some(uid) = object.get(SOP_INSTANCE_UID).and_then(|e| e.to_str().ok()) {
            meta.media_storage_sop_instance_uid = uid.trim_end_matches(['\0', ' ']).to_owned();
        }
    }
}

/// Read a bare data set (no meta group) and attach the given meta table to it. Used for
/// `dcmnorm`'s meta-less/preamble-less trial-parse fallback path.
pub fn with_meta_from_bare_dataset(
    object: InMemDicomObject,
    meta: FileMetaTable,
) -> DefaultDicomObject {
    FileDicomObject { meta, object }
}

/// Read raw bytes as a bare data set, trying Implicit VR LE, then Explicit VR LE, then
/// Explicit VR BE in that order and taking the first one that parses successfully - the same
/// permissive fallback `dcmnorm` already relies on for meta-less raw dumps.
pub fn read_dataset_trial_parse(bytes: &[u8]) -> Option<(InMemDicomObject, &'static str)> {
    const CANDIDATES: &[&str] = &[
        dcmnorm_dictionary::uids::IMPLICIT_VR_LITTLE_ENDIAN,
        dcmnorm_dictionary::uids::EXPLICIT_VR_LITTLE_ENDIAN,
        "1.2.840.10008.1.2.2",
    ];
    for uid in CANDIDATES {
        let Some(ts) = TransferSyntaxRegistry.get(uid) else {
            continue;
        };
        if let Ok(object) = InMemDicomObject::read_dataset_with_ts(Cursor::new(bytes), ts) {
            return Some((object, uid));
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::{OpenFileOptions, ReadPreamble};
    use dcmnorm_dictionary::tags;
    use std::path::PathBuf;

    fn fixture(name: &str) -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .unwrap()
            .parent()
            .unwrap()
            .join("test")
            .join("files")
            .join(name)
    }

    #[test]
    fn read_preamble_always_accepts_a_real_part10_file() {
        let object = OpenFileOptions::new()
            .read_preamble(ReadPreamble::Always)
            .open_file(fixture("mr_small.dcm"))
            .expect("a real Part 10 file has a preamble and DICM magic");
        assert_eq!(
            object
                .get(tags::MODALITY)
                .and_then(|e| e.to_str().ok())
                .as_deref(),
            Some("MR")
        );
    }

    #[test]
    fn read_preamble_always_rejects_a_file_with_no_preamble() {
        // nometa_explicit_le.dcm is a bare data set: no 128-byte preamble, no "DICM" magic.
        // ReadPreamble::Always must not silently fall back to auto-detection - it should fail
        // cleanly instead of misinterpreting the first bytes of the data set as a preamble.
        let result = OpenFileOptions::new()
            .read_preamble(ReadPreamble::Always)
            .open_file(fixture("nometa_explicit_le.dcm"));
        assert!(result.is_err(), "expected an error, got {result:?}");
    }

    #[test]
    fn read_preamble_auto_does_not_fall_back_to_bare_dataset_parsing() {
        // OpenFileOptions::from_reader/open_file never did meta-less fallback (that's a
        // separate, explicit opt-in via read_dataset_trial_parse) - Auto only controls whether
        // detection happens, not whether a failed detection is tolerated.
        let result = OpenFileOptions::new().open_file(fixture("nometa_explicit_le.dcm"));
        assert!(result.is_err(), "expected an error, got {result:?}");
    }

    #[test]
    fn explicit_vr_big_endian_file_reads_with_correct_values() {
        let object = OpenFileOptions::new()
            .open_file(fixture("explicit_vr_be.dcm"))
            .expect("should read");
        assert_eq!(
            object.meta().transfer_syntax.trim_end_matches('\0'),
            "1.2.840.10008.1.2.2"
        );
        assert_eq!(
            object
                .get(tags::MODALITY)
                .and_then(|e| e.to_str().ok())
                .as_deref(),
            Some("US")
        );
    }
}