blib/lib/PDL/IO/Dicom.pm untested

filter: Statementstmt 0.0% 0 / 243 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 321 CC 31 SCAR 62.9
CC
31
Cov
0%
rdcm
756.0
clean_time
30.0
clean_date
2.0
CRAP 541.4
1 =pod
2  
3 =head1 NAME
4  
5 PDL::IO::Dicom - a module for reading DICOM images.
6  
7 =head1 DESCRIPTION
8  
9 The PDL::IO::Dicom module enables reading 16-bit gray level Dicom
10 images into PDL. As Dicom is an extremely complex format, this module
11 can unfortunately not handle all different image types included in the
12 DICOM standard. One common format that is currently not supported is
13 the Papyrus format.
14  
15 =head1 USAGE
16  
17 use PDL;
18 use PDL::IO::Dicom;
19  
20 $img = rdcm("image.dcm");
21  
22 =head1 AUTHOR
23  
24 Copyright (C) Dov Grobgeld <dov@imagic.weizmann.ac.il> 2002.
25 All rights reserved. There is no warranty. You are allowed
26 to redistribute this software / documentation under certain
27 conditions. For details, see the file COPYING in the PDL
28 distribution. If this file is separated from the PDL distribution,
29 the copyright notice should be included in the file.
30  
31 =cut
32  
33 package PDL::IO::Dicom;
34  
35 use PDL;
36 use PDL::Core;
37 use PDL::IO::Misc;
38  
39 use Exporter;
40 @ISA = qw( Exporter );
41 @EXPORT = qw( rdcm );
42  
43 use strict;
44  
45 my $debug = 0;
46  
47 my %element_table =
48 (
49 "0002,0000" => ["Group length","UL"],
50 "0002,0001" => ["File Meta Information Version","OB"],
51 "0002,0002" => ["Media Storage SOP Class UID","UI"],
52 "0002,0003" => ["Media Storage SOP Instance UID","UI"],
53 "0002,0010" => ["Transfer Syntax UID","UI"],
54 "0002,0012" => ["Implementation Class UID","UI"],
55 "0002,0013" => ["Implementation Version Name","SH"],
56 "0002,0016" => ["Source Application Entity Title","AE"],
57 "0008,0000" => ["Identifying Group Length", "UL"],
58 "0008,0001" => ["Length to End"],
59 "0008,0005" => ["Specific Character Set","CS"],
60 "0008,0008" => ["Image Type","CS"],
61 "0008,0010" => ["Recognition Code"],
62 "0008,0012" => ["Instance Creation Date"],
63 "0008,0013" => ["Instance Creation Time"],
64 "0008,0014" => ["Instance Creator UID"],
65 "0008,0016" => ["SOP Class UID","UI"],
66 "0008,0018" => ["SOP Instance UID","UI"],
67 "0008,0020" => ["Study Date","DA"],
68 "0008,0021" => ["Series date","DA"],
69 "0008,0022" => ["Acquisition Date","DA"],
70 "0008,0023" => ["Image Date","DA"],
71 "0008,0030" => ["Study Time","TM"],
72 "0008,0031" => ["Series Time","TM"],
73 "0008,0032" => ["Acquisition Time","TM"],
74 "0008,0033" => ["Image Time","TM"],
75 "0008,103e" => ["Series Description"],
76 "0008,1030" => ["Study Description"],
77 "0008,0050" => ["Accession Number","SH"],
78 "0008,0060" => ["Modality","CS"],
79 "0008,0070" => ["Manufacturer","LO"],
80 "0008,0080" => ["Institution Name","LO"],
81 "0008,0090" => ["Referring Physician's Name","PN"],
82 "0008,1010" => ["Station Name","SH"],
83 "0008,1030" => ["Study Description","LO"],
84 "0008,103e" => ["Series Description","LO"],
85 "0008,1060" => ["Name of Physician(s)","PN"],
86 "0008,1090" => ["Manufacturers Model Name",],
87 "0010,0000" => ["Patient Information Group"],
88 "0010,0010" => ["Patient Name","PN"],
89 "0010,0020" => ["Patient ID"],
90 "0010,0030" => ["Patient Birth Date"],
91 "0010,0040" => ["Patient's Sex"],
92 "0010,1010" => ["Patient Age"],
93 "0010,1030" => ["Patient Weight"],
94 "0018,0000" => ["Acquisition Gnformation Group"],
95 "0018,0010" => ["Contrast/Bolus Agent"],
96 "0018,0020" => ["Scanning Sequence"],
97 "0018,0022" => ["Scan Options"],
98 "0018,0050" => ["Slice thickness"],
99 "0018,1050" => ["Spatial Resolution", "lO"],
100 "0008,1060" => ["Name of Physician(s) Reading Study"],
101 "0008,1070" => ["Operator's Name"],
102 "0018,0080" => ["Repetition Time"],
103 "0018,0081" => ["Echo Time"],
104 "0018,0082" => ["Inversion Time"],
105 "0018,0083" => ["Number of Averages"],
106 "0018,0084" => ["Imaging Frequency"],
107 "0018,0085" => ["Imaged Nucleus"],
108 "0018,0086" => ["Echo Number(s)"],
109 "0018,0087" => ["Magnetic Field Strength"],
110 "0018,0088" => ["Spacing Between Slices"],
111 "0018,0089" => ["Number of Phase Encoding Steps"],
112 "0018,0090" => ["Data Collection Diameter"],
113 "0018,0091" => ["Echo Train Length"],
114 "0018,0093" => ["Percent Sampling"],
115 "0018,0094" => ["Percent Phase Field of View"],
116 "0018,0095" => ["Pixel Bandwidth"],
117 "0018,1088" => ["Heart Rate","US"],
118 "0018,1090" => ["Cardiac Number of Images","US"],
119 "0018,1094" => ["Trigger Window","US"],
120 "0018,1100" => ["Reconstruction Diameter"],
121 "0018,1314" => ["Flip Angle"],
122 "0018,1315" => ["Variable Flip Angle Flag"],
123 "0018,1316" => ["SAR"],
124 "0020,0000" => ["Relationship information group"],
125 "0020,000d" => ["Study Instance UID"],
126 "0020,000e" => ["Series Instance UID"],
127 "0020,0010" => ["Study ID"],
128 "0020,0011" => ["Series Number"],
129 "0020,0012" => ["Acquisition Number"],
130 "0020,0013" => ["Image Number"],
131 "0020,0014" => ["Isotope Number"],
132 "0020,0015" => ["Phase Number"],
133 "0020,0016" => ["Interval Number"],
134 "0020,0017" => ["Time Slot Number"],
135 "0020,0018" => ["Angle Number"],
136 "0020,0020" => ["Patient Orientation"],
137 "0020,0022" => ["Overlay Number"],
138 "0020,0024" => ["Curve Number"],
139 "0020,0026" => ["LUT Number"],
140 "0020,0030" => ["Image Position"],
141 "0020,0032" => ["Image Position (Patient)"],
142 "0020,0035" => ["Image Orientation"],
143 "0020,0037" => ["Image Orientation (Patient)"],
144 "0020,0050" => ["Location"],
145 "0020,0052" => ["Frame of Reference UID"],
146 "0020,0060" => ["Laterality"],
147 "0020,1002" => ["Images in acqusition"],
148 "0020,1040" => ["Position Reference Indicator"],
149 "0020,1041" => ["Slice Location"],
150 "0028,0000" => ["Relationship information group"],
151 "0028,0002" => ["Samples per Pixel"],
152 "0028,0004" => ["Photometric Interpretation"],
153 "0028,0005" => ["Image Dimensions"],
154 "0028,0010" => ["Rows", "US"],
155 "0028,0011" => ["Columns", "US"],
156 "0028,0030" => ["Pixel Spacing"],
157 "0028,0100" => ["Bits Allocated","US"],
158 "0028,0101" => ["Bits Stored", "US"],
159 "0028,0102" => ["High Bit","US"],
160 "0028,0103" => ["Pixel Representation","US"],
161 "0028,1052" => ["Rescale Intercept"],
162 "0028,1053" => ["Rescale Slope"],
163 "0033,1002" => ["IMGF"],
164 "7fe0,0000" => ["Pixel Data Information Group", "UL"],
165 "7fe0,0010" => ["Pixel Data","OW"]
166 );
167  
168 =head1 FUNCTIONS
169  
170 =head2 rdcm
171  
172 =for ref
173  
174 Read a dicom image.
175  
176 =for usage
177  
178 $dcm = rdcm("filename")
179  
180 =cut
181
182 sub rdcm {
183 my $file = shift;
184 my $options = shift;
185 my $do_print_info = 0;
186 my (%info, %bin);
187 my $do_raw = 0; # Only for debugging
188 my ($rescale_intercept, $rescale_slope) = (0, 1);
189 my $do_explicit = 0;
190 my $do_guess_endian = 1;
191  
192 # options
193 if ($options) {
194 if (defined $options->{do_print_info}) {
195 $do_print_info = $options->{do_print_info};
196 }
197 }
198  
199 open(IN, $file) || die "Failed opening image $file!\n";
200 binmode IN;
201 # read the whole image
202 my $header;
203 read(IN, $header, -s $file);
204
205 # File preamble - a fixed 128 byte field
206 my $hpos = 0x80;
207
208 # Next four bytes should be DICM
209 if (substr($header, $hpos, 4) ne 'DICM') {
210 die "This is not a DICM file!\n";
211 }
212 $hpos+= 4;
213  
214 # Precheck if the first entry has explicit vr. Unfortunately this
215 # is not enough to determine if the file always has explicit value
216 # representation.
217 if (substr($header, $hpos+4, 2)=~ /[A-Z]{2}/) {
218 $do_explicit++;
219 }
220  
221 while($hpos < length($header)) {
222 my $is_binary = 0;
223 my $groupword = unpack("v", substr($header, $hpos, 2)); $hpos+=2;
224 my $elementword = unpack("v", substr($header, $hpos, 2)); $hpos+=2;
225 my $value_rep = substr($header, $hpos, 2);
226 my $key = sprintf("%04x,%04x", $groupword, $elementword);
227 my ($lookup) = $element_table{$key};
228 my $override_vr = 0;
229
230 # Check for explicit value representation. There must be a different
231 # way to figure this out, but I still haven't figured out how!
232 if ($value_rep =~ /[A-Z][A-Z]/) {
233 $hpos+=2;
234 }
235 else {
236 $override_vr++;
237 $value_rep = $lookup->[1] || "UN";
238 }
239  
240 my $elementlength;
241 if (substr($header, $hpos, 4) eq "IMGF") {
242 die "No support for IMGF files at the moment!\n";
243 }
244  
245 # The following calculation agrees with dicom3tools
246 if ($override_vr) {
247 $elementlength = unpack("V", substr($header, $hpos, 4));
248 $hpos+= 4;
249 }
250 elsif (grep($value_rep eq $_, qw(OB OW SQ UN))) {
251 # Long length
252 $is_binary = 1;
253 $hpos+= 2; # Always zero
254 $elementlength = unpack("V", substr($header, $hpos, 4));
255 $hpos+= 4;
256 }
257 else {
258 # Short length
259 $elementlength = unpack("v", substr($header, $hpos, 2));
260 $hpos+= 2;
261 }
262  
263 my ($descr) = "";
264 my $contents = substr($header, $hpos, $elementlength) . "";
265
266 ($descr) = @$lookup if $lookup;
267
268 # recode contents
269 if ($value_rep eq "UL") { $contents = unpack("V", $contents); }
270 elsif ($value_rep eq "US") { $contents = unpack("v", $contents); }
271 elsif ($value_rep eq "TM") { $contents = clean_time($contents); }
272 elsif ($value_rep eq "DA") { $contents = clean_date($contents); }
273 elsif (!$is_binary) {
274 $contents=~ s/\s+$//;
275 }
276
277 # store the contents
278 if ($key eq "7fe0,0010") { # Pixel data
279 $bin{$descr} = $contents;
280 }
281 else {
282 $info{$descr} = $contents if $descr;
283 }
284
285 if ($do_print_info) {
286 $contents = "" unless $contents;
287 $contents = "<<Binary>>" if $is_binary;
288 $contents=~ tr/\0-\037\200-\377//d;
289 $contents= substr($contents, 0, 40) unless $do_raw;
290 $contents=~ s/[\0-\037\200]/?/g;
291 $value_rep=~ s/[\0-\037]/?/g;
292 printf STDERR "%04x> %04x,%04x (%04x,$value_rep) %-30s : %s\n",
293 $hpos, $groupword, $elementword, $elementlength, $descr, $contents;
294 }
295
296 $hpos+= $elementlength;
297 }
298
299 my($width, $height) = ($info{Columns}, $info{Rows});
300 my($bs) = $info{"Bits Allocated"}/8;
301 my $img = $bin{"Pixel Data"};
302 # The following logics works on Intel endian
303 my $do_toggle_endian = $info{"Pixel Representation"};
304 $rescale_intercept = $info{"Rescale Intercept"} if defined $info{"Rescale Intercept"};
305 $rescale_slope = $info{"Rescale Slope"} if defined $info{"Rescale Slope"};
306  
307 my $pdl;
308
309 # Create a pdl from the raw data
310 if ($bs == 2) {
311 $pdl = zeroes(ushort, $width,$height);
312 my $hdr = $pdl->gethdr;
313 $pdl->make_physical();
314  
315 # Store the pixel data
316 ${$pdl->get_dataref()} = $img;
317  
318 # Guess endian
319 if ($do_guess_endian) {
320 # Compare spread of high byte with low byte
321 my $high_byte = ($pdl>>8)->byte;
322 my $low_byte = ($pdl & 0xff)->byte;
323 my $max_high = $high_byte->max;
324 my $max_low = $low_byte->max;
325  
326 # print STDERR "max_high max-low = $max_high $max_low\n";
327  
328 # The following might need to be adjusted on different
329 # architectures.
330 $do_toggle_endian = $max_high > $max_low;
331 }
332
333 # Endian swap
334 if ($do_toggle_endian) {
335 bswap2($pdl);
336 }
337  
338 # Rescale and convert to double
339 if ($rescale_intercept != 0
340 || $rescale_slope != 1) {
341 print STDERR "scaling with $rescale_slope and $rescale_intercept\n";
342 $pdl = 1.0*($pdl * $rescale_slope) + $rescale_intercept;
343 }
344 } else {
345 die "Sorry! PDL::IO::Dicom currently only supported DICOM with bs=2. bs = $bs\n";
346 }
347  
348 # Store the info in the pdl header
349 $pdl->sethdr(\%info);
350
351 return $pdl;
352 }
353  
354 sub clean_time {
355 my $time = shift;
356 my ($hour, $min, $sec, $sec_frac);
357 if ($time=~ /(\d+):(\d+):(\d+)\.?(\d*)/) {
358 ($hour,$min,$sec,$sec_frac) = ($1,$2,$3,$4);
359 }
360 elsif ($time=~ /(\d\d)(\d\d)(\d\d)\.?(\d+)/) {
361 ($hour,$min,$sec,$sec_frac) = ($1,$2,$3,$4);
362 }
363  
364 if (defined $hour) {
365 $time = sprintf("%02d:%02d:%02d", $hour, $min, $sec);
366 $time .= ".$sec_frac" if $sec_frac;
367 }
368 return $time;
369 }
370  
371 sub clean_date {
372 my $date = shift;
373 $date=~ s/(\d\d\d\d)(\d\d)(\d\d)/$1-$2-$3/;
374 return $date;
375 }
376  
377 =head1 AUTHOR
378  
379 Copyright (C) Dov Grobgeld <dov@imagic.weizmann.ac.il> 1997.
380 All rights reserved. There is no warranty. You are allowed
381 to redistribute this software / documentation under certain
382 conditions. For details, see the file COPYING in the PDL
383 distribution. If this file is separated from the PDL distribution,
384 the copyright notice should be included in the file.
385  
386 =cut
387  
388 1;