Enet4 / dicom-rs

Rust implementation of the DICOM standard
https://dicom-rs.github.io
Apache License 2.0
414 stars 82 forks source link

[core] dicom_value! macro creates broken element when used for with DateTime type #311

Closed ikaros closed 1 year ago

ikaros commented 1 year ago

Hi,
many thanks for the great support in the chat :+1:
I thought I might could help by creating an issue :slightly_smiling_face:

Description

dicom_value! macro with the <type> DateTime produces a Element that, when added, leads to a broken DICOM file.

Example

This fragment could be used to reproduce the problem.

use dicom_core::{
    chrono::FixedOffset,
    dicom_value,
    value::{DicomDate, DicomDateTime, DicomTime, C},
    DataElement, PrimitiveValue, VR,
};
use dicom_dictionary_std::tags;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let dt = {
        let d = DicomDate::from_ymd(2022, 2, 2)?;
        let t = DicomTime::from_hms(22, 22, 22)?;
        let offset = FixedOffset::east(3600);
        DicomDateTime::from_date_and_time(d, t, offset)?
    };
    let mut obj = dicom_object::open_file("./sample.dcm")?;
    let element = DataElement::new(
        tags::INSTANCE_COERCION_DATE_TIME,
        VR::DT,
        dicom_value!(DateTime, dt),
    );
    obj.put(element);
    obj.write_to_file("./foo.dcm")?;

    Ok(())
}

Output of dcmdump

$> dcmdump foo.dcm
W: DcmItem: Non-standard VR 'I ' (49\1c) encountered while parsing element (1600,5500), assuming 2 byte length field
W: DcmItem: Non-standard VR 'EK' (45\4b) encountered while parsing element (6573,2f6d), assuming 4 byte length field
W: DcmItem: Length of element (6573,2f6d) is odd
E: DcmElement: Unknown Tag & Data (6573,2f6d) larger (1970565217) than remaining bytes in file
E: dcmdump: I/O suspension or premature end of stream: reading file: foo.dcm

Many thanks for the great work!

Enet4 commented 1 year ago

This is a problem with the DICOM writing routines for date times created in this way, rather than an issue with dicom_value! proper. In any case, it should be solved in #309!

ikaros commented 1 year ago

That was almost an instant reaction time :rocket: Many many thanks :slightly_smiling_face: :+1: