From ab56ed2d0d5f361ec272775844d08ed9c288ad62 Mon Sep 17 00:00:00 2001 From: Bastien Lecoeur Date: Wed, 22 May 2024 17:44:53 +0100 Subject: [PATCH] String padding and sequence undefined length --- src/DICOM.jl | 4 ++-- test/runtests.jl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/DICOM.jl b/src/DICOM.jl index ae29d9c..5e73121 100644 --- a/src/DICOM.jl +++ b/src/DICOM.jl @@ -658,10 +658,10 @@ function dcm_store(st::IO, gelt::Tuple{UInt16,UInt16}, writef::Function, vr::Str sz += 1 end seek(st, p) - write(st, convert(lentype, max(0, sz))) + vr == "SQ" || gelt == (0xFFFE, 0xE000) ? write(st, convert(lentype, 0xffffffff)) : write(st, convert(lentype, max(0, sz))) seek(st, endp) if szWasOdd - write(st, UInt8(0)) + vr in ("AE", "CS", "SH", "LO", "PN", "DA", "DT", "TM") ? write(st, UInt8(0x20)) : write(st, UInt8(0)) end end diff --git a/test/runtests.jl b/test/runtests.jl index 5c195e0..9c8600b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -273,3 +273,37 @@ end @test dcmMG3[(0x0008, 0x2218)][1][(0x0008, 0x0104)] == 0x0001 @test dcmMG3[(0x0054, 0x0220)][1][(0x0008, 0x0104)] == 0x0002 end + +function write_to_string(writef) + io = IOBuffer() + writef(io) + return String(take!(io)) +end + +@testset "Padding string values" begin + empty_vr_dict = Dict{Tuple{UInt16, UInt16}, String}() + # Test that UI strings are padded with '\0' to even length + SOPClassUID = write_to_string(io -> DICOM.write_element(io, (0x0008, 0x0016), "1.2.840.10008.5.1.4.1.1.4", true, empty_vr_dict)) + @test length(SOPClassUID) % 2 == 0 + @test SOPClassUID[end] == '\0' + # Test that SH strings are padded with ' ' to even length + StudyDescription = write_to_string(io -> DICOM.write_element(io, (0x0008, 0x1030), "BestImageEver", true, empty_vr_dict)) + @test length(StudyDescription) % 2 == 0 + @test StudyDescription[end] == ' ' +end + +@testset "Writing Sequence" begin + empty_vr_dict = Dict{Tuple{UInt16, UInt16}, String}() + # Setup internal SQ DICOMData + sq_meta = Dict{Tuple{UInt16, UInt16}, Any}([(0x0008, 0x0100) => "UNDEFINED", (0x0008, 0x010b) => 'N', (0x0008, 0x0104) => "UNDEFINED"]) + sq_el = DICOM.DICOMData(sq_meta, :little, true, empty_vr_dict) + + # Setup external SQ DICOMData + meta = Dict{Tuple{UInt16, UInt16}, Any}((0x0040, 0x0260) => [sq_el]) + el = DICOM.DICOMData(meta, :little, true, empty_vr_dict) + + PerformedProtocolSequence = write_to_string(io -> DICOM.write_element(io, (0x0040, 0x0260), el[(0x0040, 0x0260)], true, empty_vr_dict)) + # Check that the length is (0xffffffff) for undefined length SQ + @test PerformedProtocolSequence[9:12] == "\xff\xff\xff\xff" + @test PerformedProtocolSequence[end-7:end] == "\xfe\xff\xdd\xe0\x00\x00\x00\x00" +end \ No newline at end of file