Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/DICOM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,28 @@ function dcm_write(
write(st, "DICM")
end
(is_explicit, endian) = determine_explicitness_and_endianness(dcm)
for gelt in sort(collect(keys(dcm)))
sorted_keys = sort(collect(keys(dcm)))
meta_len = findlast(k -> k[1] == (0x0002), sorted_keys)
write_meta_elements(st, view(sorted_keys, 1:meta_len), dcm, is_explicit, aux_vr)
for gelt in view(sorted_keys, meta_len+1:length(sorted_keys))
write_element(st, gelt, dcm[gelt], is_explicit, aux_vr)
end
return
end

function write_meta_elements(st::IO, keys::AbstractVector, dcm, is_explicit, aux_vr)
# Write (0x0002, 0x0000) first with whatever is in dcm and correct it at the end of the meta block
write_element(st, keys[1], dcm[keys[1]], is_explicit, aux_vr)
p = position(st)
for gelt in keys[2:end]
write_element(st, gelt, dcm[gelt], is_explicit, aux_vr)
end
endp = position(st)
seek(st, p - 4)
write(st, UInt32(endp - p))
seek(st, endp)
end

function write_element(st::IO, gelt::Tuple{UInt16,UInt16}, data, is_explicit, aux_vr)
if haskey(aux_vr, gelt)
vr = aux_vr[gelt]
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ end
@test size(dcmMR_UnspecifiedLength[tag"Pixel Data"]) === (256, 256, 27)
end

@testset "File Meta Information Group Length update" begin
fileMR = download_dicom("MR_Implicit_Little.dcm")
dcmMR = dcm_parse(fileMR)

# Get the meta data length
meta_length = dcmMR[(0x0002, 0x0000)]

# Update the meta data by reducing the Implementation Version Name
dcmMR[(0x0002, 0x0013)] = dcmMR[(0x0002, 0x0013)][1:end-2]

# Write the updated meta data to a new file
outMR = joinpath(data_folder, "outMR.dcm")
dcm_write(outMR, dcmMR)

# Read the new file and check if the meta data length has changed
dcmMR_out = dcm_parse(outMR)
@test Int(dcmMR_out[(0x0002, 0x0000)]) == meta_length - 2
end

@testset "Test big endian" begin
fileUS = download_dicom("US_Explicit_Big_RGB.dcm")
dcmUS = dcm_parse(fileUS)
Expand Down