Skip to content

Commit

Permalink
Ensure aux_vr passed to SQ
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronGhost committed May 17, 2024
1 parent cfee5eb commit da8e41a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
14 changes: 7 additions & 7 deletions src/DICOM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ function write_element(st::IO, gelt::Tuple{UInt16,UInt16}, data, is_explicit, au

if vr == "SQ"
vr = is_explicit ? vr : empty_vr
return dcm_store(st, gelt, s -> sequence_write(s, map(d -> d.meta, data), is_explicit), vr)
return dcm_store(st, gelt, s -> sequence_write(s, map(d -> d.meta, data), is_explicit, aux_vr), vr)
end

# Pack data into array container. This is to undo "data = data[1]" from read_element().
Expand Down Expand Up @@ -665,20 +665,20 @@ function dcm_store(st::IO, gelt::Tuple{UInt16,UInt16}, writef::Function, vr::Str
end
end

sequence_write(st::IO, items::Array{Any,1}, evr::Bool) =
sequence_write(st, convert(Array{Dict{Tuple{UInt16,UInt16},Any},1}, items), evr)
function sequence_write(st::IO, items::Array{Dict{Tuple{UInt16,UInt16},Any},1}, evr)
sequence_write(st::IO, items::Array{Any,1}, evr::Bool, aux_vr::Dict{Tuple{UInt16, UInt16}}) =
sequence_write(st, convert(Array{Dict{Tuple{UInt16,UInt16},Any},1}, items), evr, aux_vr)
function sequence_write(st::IO, items::Array{Dict{Tuple{UInt16,UInt16},Any},1}, evr, aux_vr::Dict{Tuple{UInt16, UInt16}})
for subitem in items
if length(subitem) > 0
dcm_store(st, (0xFFFE, 0xE000), s -> sequence_item_write(s, subitem, evr))
dcm_store(st, (0xFFFE, 0xE000), s -> sequence_item_write(s, subitem, evr, aux_vr))
end
end
write(st, UInt16[0xFFFE, 0xE0DD, 0x0000, 0x0000])
end

function sequence_item_write(st::IO, items::Dict{Tuple{UInt16,UInt16},Any}, evr)
function sequence_item_write(st::IO, items::Dict{Tuple{UInt16,UInt16},Any}, evr, aux_vr::Dict{Tuple{UInt16, UInt16}})
for gelt in sort(collect(keys(items)))
write_element(st, gelt, items[gelt], evr, empty_dcm_dict)
write_element(st, gelt, items[gelt], evr, aux_vr)
end
write(st, UInt16[0xFFFE, 0xE00D, 0x0000, 0x0000])
end
Expand Down
26 changes: 22 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const dicom_samples = Dict(
"https://github.com/notZaki/DICOMSamples/raw/master/DICOMSamples/OT_Implicit_Little_Headless.dcm",
"US_Explicit_Big_RGB.dcm" =>
"https://github.com/notZaki/DICOMSamples/raw/master/DICOMSamples/US_Explicit_Big_RGB.dcm",
"DX_Implicit_Little_Interleaved.dcm" =>
"https://github.com/OHIF/viewer-testdata/raw/master/dcm/zoo-exotic/5.dcm",
# "DX_Implicit_Little_Interleaved.dcm" =>
# "https://github.com/OHIF/viewer-testdata/raw/master/dcm/zoo-exotic/5.dcm",
)

function download_dicom(filename; folder = data_folder)
Expand Down Expand Up @@ -185,11 +185,13 @@ end
@test size(dcmUS[(0x7fe0, 0x0010)]) == (480, 640, 3)
end

#==
@testset "Test interleaved" begin
fileDX = download_dicom("DX_Implicit_Little_Interleaved.dcm")
dcmDX = dcm_parse(fileDX)
@test size(dcmDX[(0x7fe0, 0x0010)]) == (1590, 2593, 3)
end
==#

@testset "Test Compressed" begin
fileCT = download_dicom("CT_JPEG70.dcm")
Expand Down Expand Up @@ -233,8 +235,8 @@ end
end

# First, test the isdicom() function
fileDX = download_dicom("DX_Implicit_Little_Interleaved.dcm")
@test DICOM.isdicom(fileDX) == true
fileCT = download_dicom("CT_JPEG70.dcm")
@test DICOM.isdicom(fileCT) == true
@test DICOM.isdicom(notdicomfile) == false

# Second, test if all valid dicom file can be parsed
Expand All @@ -255,3 +257,19 @@ end
@test macroexpand(Main, :(tag"Modality")) === (0x0008, 0x0060)
@test_throws LoadError macroexpand(Main, :(tag"nonsense"))
end

@testset "Test VR in Sequence" begin
# Test auxillary VR passed to nested tags
fileMG = download_dicom("MG_Explicit_Little.dcm")
dcmMG = dcm_parse(fileMG)
# Set value of CodeMeaning for both tags to something arbitrary
codemeaning_vr = Dict{Tuple{UInt16, UInt16}, String}((0x008, 0x104) => "US")
dcmMG[(0x0008, 0x2218)][1][(0x0008, 0x0104)] = 0x0001
dcmMG[(0x0054, 0x0220)][1][(0x0008, 0x0104)] = 0x0002

outMG3 = joinpath(data_folder, "outMG3.dcm")
dcm_write(outMG3, dcmMG; aux_vr = codemeaning_vr)
dcmMG3 = dcm_parse(outMG3)
@test dcmMG3[(0x0008, 0x2218)][1][(0x0008, 0x0104)] == 0x0001
@test dcmMG3[(0x0054, 0x0220)][1][(0x0008, 0x0104)] == 0x0002
end

0 comments on commit da8e41a

Please sign in to comment.