-
Notifications
You must be signed in to change notification settings - Fork 13
/
dicom_move.m
129 lines (105 loc) · 4.16 KB
/
dicom_move.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
% Advance to the point in the file where the target group and
% element are and return the length of the field.
% Orginally:
% Greg Reynolds 26-January-2005.
% Vastly updated:
% Greg Reynolds 17-June-2005.
%
% Now with in-sequence data support.
function length = dicom_move(fd, strGroup, strElement)
%fprintf('\nSearching for target element (%s, %s)...', strGroup, strElement');
% these are all the VRs that have length 2
VR_short_length = struct('strings', ...
{'AE', 'AS', 'AT', 'CS', 'DA', 'DS', 'DT', ...
'FL', 'FD', 'IS', 'LO', 'LT', 'OF', 'PN', 'SH', ...
'SL', 'ST', 'SS', 'TM', 'UI', 'UL', 'US' });
dims = size(VR_short_length);
number_of_short_vrs = dims(2);
% these are all the VRs so that we can establish implicit VRs
% without lots of DICOM knowledge
VRs = struct('strings', ...
{'AE', 'AS', 'AT','CS', 'DA', 'DS', 'DT', 'FL', ...
'FD', 'IS', 'LO', 'LT', 'OB', 'OF', 'OW', 'PN', ...
'SH', 'SL', 'SQ', 'ST', 'SS', 'TM', 'UI', 'UL', ...
'UN', 'US', 'UT'});
dims = size(VRs);
number_of_vrs = dims(2);
done = 0;
while ~done,
current_tag = fread(fd, 2, 'uint16','l');
current_vr = fread(fd, 2, 'schar','l');
if feof(fd)
done = 1;
length = 0;
fprintf('\nReached end of file without match.');
break;
else
strGroupCurrent = sprintf('%X', current_tag(1));
strElementCurrent = sprintf('%X', current_tag(2));
strVRCurrent = sprintf('%c', current_vr);
% first of all, check with this is an implicit VR
explicit_vr = 0;
for n = 1:1:number_of_vrs
if strcmp(VRs(n).strings, strVRCurrent)
explicit_vr = 1;
break;
end
end
% it was an implicit VR
if explicit_vr == 0
% adjust the file pointer back the two-bytes we tentatively
% read in as being the VR
fseek(fd, -2, 'cof');
% possibly need to read in zero padding here?
current_length = fread(fd, 1, 'uint32','l');
% if the length is undefined, just drop out and move
% to next element...
if ~strcmp(sprintf('%X', current_length), 'FFFFFFFF')
if strcmp(strGroupCurrent, strGroup)
if strcmp(strElementCurrent, strElement)
length = current_length;
done = 1;
break;
end
end
if done == 0
fread(fd, current_length, 'uchar','l');
end
end
% it was an explicit VR
else
size_length = 4;
% check to see whether it has a short length
for n = 1:1:number_of_short_vrs
if strcmp(VR_short_length(n).strings, strVRCurrent)
size_length = 2;
end
end
% note that implicit VRs always have 32-bit length
if size_length == 2
current_length = fread(fd, 1, 'uint16','l');
else
zeropadding = fread(fd, 2, 'uchar','l');
current_length = fread(fd, 1, 'uint32','l');
end
% now see if this was the field we wanted
if strcmp(strGroupCurrent, strGroup)
if strcmp(strElementCurrent, strElement)
length = current_length;
done = 1;
break;
end
end
% some implicit VRs, e.g. an SQ can have undefined
% length if they have 32-bit length, so if that
% isn't the case, proceed and read, otherwise just
% skip to the next element
if ~strcmp(sprintf('%X', current_length), 'FFFFFFFF')
% it wasn't, so advance pointer
if done == 0
fread(fd, current_length, 'uchar','l');
end
end
end
end
end