Skip to content

Commit

Permalink
More robust DICOM reading (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszbaran authored Jun 7, 2024
1 parent 6ece51f commit d37b427
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/DICOM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ end
function dcmdir_parse(dir; kwargs...)
dicom_files = find_dicom_files(dir)
unsorted_dicoms = [dcm_parse(file; kwargs...) for file in dicom_files]
dicoms = sort!(unsorted_dicoms, by = dicom -> dicom[tag"Instance Number"])
dicoms = sort!(unsorted_dicoms, by = dicom -> haskey(dicom, tag"Instance Number") ? dicom[tag"Instance Number"] : -1)
return dicoms
end

Expand Down Expand Up @@ -261,6 +261,21 @@ function read_element(st::IO, dcm::DICOMData)
return (read_element(st::IO, dcm))
end

function parse_is(x)
if x == ""
return 0
end

try
return parse(Int, x)
catch e
@warn """Cannot parse data in element $gelt:
$e
"""
return 0
end
end

data = vr == "ST" || vr == "LT" || vr == "UT" || vr == "AS" ?
String(read!(st, Array{UInt8}(undef, sz))) :
sz == 0 || vr == "XX" ? Any[] :
Expand All @@ -281,7 +296,7 @@ function read_element(st::IO, dcm::DICOMData)
vr == "DS" ?
map(x -> x == "" ? 0.0 : parse(Float64, x), string_parse(st, sz, 16, false)) :
vr == "IS" ?
map(x -> x == "" ? 0 : parse(Int, x), string_parse(st, sz, 12, false)) :
map(parse_is, string_parse(st, sz, 12, false)) :
vr == "AE" ? string_parse(st, sz, 16, false) :
vr == "CS" ? string_parse(st, sz, 16, false) :
vr == "SH" ? string_parse(st, sz, 16, false) :
Expand Down

0 comments on commit d37b427

Please sign in to comment.