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