| 1 |
#! /usr/bin/env perl |
| 2 |
# |
| 3 |
# Andrew Janke - [email protected] |
| 4 |
# McConnell Brain Imaging Center |
| 5 |
# Montreal Neurological Institute |
| 6 |
# |
| 7 |
# MINC is a free file format from the MNI: www.bic.mni.mcgill.ca |
| 8 |
# MIF4 is a file format of a lab from UBC - www.medicine.ubc.ca |
| 9 |
# The specs for MIF4 were kindly provided by Andrew Riddehough |
| 10 |
# |
| 11 |
# NB: this is not a complete translation only fields that seemed |
| 12 |
# pertinent at the time of writing this are used... |
| 13 |
# |
| 14 |
# Mon Jan 7 14:18:37 EST 2002 - initial version |
| 15 |
# Wed Feb 13 16:22:05 EST 2002 - Added byte_swapping aka ana2mnc |
| 16 |
|
| 17 |
require 5.0; |
| 18 |
|
| 19 |
use strict; |
| 20 |
use warnings "all"; |
| 21 |
|
| 22 |
use Getopt::Tabular; |
| 23 |
$SIG{__DIE__} = sub { &cleanup; die $_[0]; }; |
| 24 |
|
| 25 |
my(%Unpack_codes) = ('a', 1, 'A', 1, |
| 26 |
's', 2, 'S', 2, |
| 27 |
'i', 4, 'I', 4, |
| 28 |
'f', 4, 'd', 8, |
| 29 |
'l', 4, 'L', 4, |
| 30 |
); |
| 31 |
|
| 32 |
my(%dtypes) = ( |
| 33 |
2 => ['-short', '-unsigned'], |
| 34 |
4 => ['-int', '-unsigned'], |
| 35 |
8 => ['-float'], |
| 36 |
16 => ['-double'], |
| 37 |
); |
| 38 |
|
| 39 |
chomp(my($me) = `basename $0`); |
| 40 |
my($tmpfile) = "/tmp/$me-$$.raw"; |
| 41 |
my($clobber) = 0; |
| 42 |
my($verbose) = 0; |
| 43 |
my(@opt_table) = ( |
| 44 |
["-verbose", "boolean", 0, \$verbose, "Be Verbose"], |
| 45 |
["-clobber", "boolean", 0, \$clobber, "clobber existing files"], |
| 46 |
); |
| 47 |
|
| 48 |
my($Help) = <<HELP; |
| 49 |
This script can be invoked as: |
| 50 |
mif2mnc Convert a MIF4 file to a MINC |
| 51 |
HELP |
| 52 |
|
| 53 |
my($Usage) = "Usage: $me [options] <in.MIF> <out.mnc>\n". |
| 54 |
" $me -help to list options\n\n"; |
| 55 |
|
| 56 |
my($miffile, $mncfile, $files, $nfiles, @args, $history); |
| 57 |
|
| 58 |
# create the history string |
| 59 |
chomp($history = `date`); |
| 60 |
$history .= '>>>> ' . join(' ', $me, @ARGV) . "\n"; |
| 61 |
|
| 62 |
# Check arguments |
| 63 |
&Getopt::Tabular::SetHelp ($Help, $Usage); |
| 64 |
&GetOptions (\@opt_table, \@ARGV) || exit 1; |
| 65 |
if ($#ARGV < 1){ die $Usage; } |
| 66 |
($miffile, $mncfile) = @ARGV[0..1]; |
| 67 |
|
| 68 |
if(!-e "$miffile"){ |
| 69 |
die "$me: Couldn't find $miffile\n"; |
| 70 |
} |
| 71 |
if(-e $mncfile && !$clobber){ |
| 72 |
die "$me: $mncfile exists! use -clobber to overwrite\n"; |
| 73 |
} |
| 74 |
|
| 75 |
# Read in the file header |
| 76 |
open(HDR, $miffile) or die "$me: Error opening file: $miffile\n"; |
| 77 |
read(HDR, my($hdr), 512); |
| 78 |
|
| 79 |
my %h; |
| 80 |
|
| 81 |
# check for byte-swapping |
| 82 |
my($bs) = 0; |
| 83 |
if(destruct(\$hdr, 44,'s', 0) != 4){ |
| 84 |
if($verbose){ print "Hrm, let's try byte swapping\n"; } |
| 85 |
$bs = 1; |
| 86 |
} |
| 87 |
|
| 88 |
$h{MIF_ID} = destruct(\$hdr, 0,'a20', $bs); |
| 89 |
$h{MIF_Version} = destruct(\$hdr, 44,'s', $bs); |
| 90 |
$h{MIF_SubVersion}= destruct(\$hdr, 46,'s', $bs); |
| 91 |
$h{MIF_Name} = destruct(\$hdr, 48,'a80', $bs); |
| 92 |
|
| 93 |
$h{FileDateTime} = destruct(\$hdr, 132,'l', $bs); |
| 94 |
$h{ConvExeTime} = destruct(\$hdr, 140,'l', $bs); |
| 95 |
$h{SiteDataSet} = destruct(\$hdr, 146,'a32', $bs); |
| 96 |
|
| 97 |
$h{Pat_Number} = destruct(\$hdr, 192,'a25', $bs); |
| 98 |
$h{Pat_Name} = destruct(\$hdr, 217,'a25', $bs); |
| 99 |
$h{Pat_Initials} = destruct(\$hdr, 242,'a8', $bs); |
| 100 |
$h{Pat_DOB} = destruct(\$hdr, 250,'a12', $bs); |
| 101 |
$h{Pat_Gender} = destruct(\$hdr, 262,'a1', $bs); |
| 102 |
$h{Hosp_Name} = destruct(\$hdr, 263,'a33', $bs); |
| 103 |
|
| 104 |
$h{SeriesDateTime}= destruct(\$hdr, 300,'l', $bs); |
| 105 |
$h{Scan_ID} = destruct(\$hdr, 304,'l', $bs); |
| 106 |
|
| 107 |
|
| 108 |
$h{Num_Images} = destruct(\$hdr, 332,'s', $bs); |
| 109 |
$h{Num_Slices} = destruct(\$hdr, 334,'s', $bs); |
| 110 |
$h{Num_Echoes} = destruct(\$hdr, 336,'s', $bs); |
| 111 |
$h{Image_Dim_x} = destruct(\$hdr, 338,'s', $bs); |
| 112 |
$h{Image_Dim_y} = destruct(\$hdr, 340,'s', $bs); |
| 113 |
$h{Bytes_per_Pix} = destruct(\$hdr, 342,'s', $bs); |
| 114 |
$h{Def_Slice_Gap} = destruct(\$hdr, 344,'l', $bs); |
| 115 |
|
| 116 |
# orientation info |
| 117 |
$h{Orientation} = destruct(\$hdr, 356,'a1', $bs); |
| 118 |
$h{Contrast_Flag} = destruct(\$hdr, 369,'s', $bs); |
| 119 |
$h{Mag_Strength} = destruct(\$hdr, 438,'s', $bs); |
| 120 |
|
| 121 |
$h{Translation_x} = destruct(\$hdr, 484,'l', $bs); |
| 122 |
$h{Translation_y} = destruct(\$hdr, 488,'l', $bs); |
| 123 |
$h{Translation_z} = destruct(\$hdr, 492,'l', $bs); |
| 124 |
$h{Rotation_x} = destruct(\$hdr, 496,'f', $bs); |
| 125 |
$h{Rotation_y} = destruct(\$hdr, 500,'f', $bs); |
| 126 |
$h{Rotation_z} = destruct(\$hdr, 504,'f', $bs); |
| 127 |
|
| 128 |
# munge a few values |
| 129 |
$h{FileDateTime} = localtime($h{FileDateTime}); |
| 130 |
$h{ConvExeTime} = localtime($h{ConvExeTime}); |
| 131 |
$h{SeriesDateTime} = localtime($h{SeriesDateTime}); |
| 132 |
$h{Def_Slice_Gap} /= 1000; |
| 133 |
$h{Mag_Strength} /= 10000; |
| 134 |
|
| 135 |
# print a few things out |
| 136 |
if($verbose){ |
| 137 |
foreach (sort(keys(%h))){ |
| 138 |
print STDOUT "$_:\t$h{$_}\n"; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
# create the temporary file for rawtominc to read in |
| 144 |
open(TMP, ">$tmpfile"); |
| 145 |
|
| 146 |
my($c, $position, $pix_size_x, $pix_size_y, $size_x, $size_y, $type, $thickness, $data); |
| 147 |
|
| 148 |
for($c=0; $c<$h{Num_Slices}; $c++){ |
| 149 |
read(HDR, my($hdr), 256); |
| 150 |
|
| 151 |
$position = destruct(\$hdr, 109,'f', $bs); |
| 152 |
$pix_size_x = destruct(\$hdr, 113,'l', $bs) / 1000; |
| 153 |
$pix_size_y = destruct(\$hdr, 113,'l', $bs) / 1000; |
| 154 |
|
| 155 |
$size_x = destruct(\$hdr, 121,'s', $bs); |
| 156 |
$size_y = destruct(\$hdr, 123,'s', $bs); |
| 157 |
$type = destruct(\$hdr, 125,'s', $bs); |
| 158 |
|
| 159 |
$thickness = destruct(\$hdr, 127,'l', $bs) / 1000; |
| 160 |
|
| 161 |
if($verbose){ |
| 162 |
print STDOUT "Slice $c pos: $position $thickness $size_x:$size_y $pix_size_x:$pix_size_y\n"; |
| 163 |
} |
| 164 |
|
| 165 |
read(HDR, $data, $size_x*$size_y*$type); |
| 166 |
syswrite(TMP, $data, $size_x*$size_y*$type); |
| 167 |
} |
| 168 |
close(HDR); |
| 169 |
close(TMP); |
| 170 |
|
| 171 |
|
| 172 |
# byte swapping time |
| 173 |
@args = ('dd', "if=$tmpfile", "of=$tmpfile.swp", 'conv=swab'); |
| 174 |
if ($verbose){ print STDOUT "@args\n"; } |
| 175 |
system(@args) == 0 or die "Died during byte swapping\n"; |
| 176 |
|
| 177 |
# Check the data type |
| 178 |
if (!defined($dtypes{$h{Bytes_per_Pix}})){ |
| 179 |
die "Unknown data type: $h{Bytes_per_Pix}\n"; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
# Set up rawtominc command |
| 184 |
@args = ('rawtominc'); |
| 185 |
if($clobber){ |
| 186 |
push(@args, "-clobber"); |
| 187 |
} |
| 188 |
push(@args, @{$dtypes{$h{Bytes_per_Pix}}}); |
| 189 |
|
| 190 |
# Get step info |
| 191 |
if (($h{Orientation} eq "O") || |
| 192 |
($h{Orientation} eq "T")){ # Transverse |
| 193 |
push(@args, '-transverse'); |
| 194 |
} |
| 195 |
elsif ($h{Orientation} eq "C"){ # Coronal |
| 196 |
push(@args, '-coronal'); |
| 197 |
} |
| 198 |
elsif ($h{Orientation} eq "S"){ # Sagittal |
| 199 |
push(@args, '-sagittal'); |
| 200 |
} |
| 201 |
else{ # Unknown |
| 202 |
warn "Unknown data orientation: assuming transverse\n"; |
| 203 |
push(@args, '-transverse'); |
| 204 |
} |
| 205 |
|
| 206 |
# range |
| 207 |
push(@args, '-scan_range', '-range', 0, 4095); |
| 208 |
|
| 209 |
# figger out starts and origins |
| 210 |
$pix_size_y *= -1; |
| 211 |
|
| 212 |
push(@args, '-xstep', $pix_size_x, |
| 213 |
'-ystep', $pix_size_y, |
| 214 |
'-zstep', $thickness+$h{Def_Slice_Gap}, |
| 215 |
'-xstart', -$pix_size_x*$h{Image_Dim_x}/2, |
| 216 |
'-ystart', -$pix_size_y*$h{Image_Dim_y}/2, |
| 217 |
'-zstart', -$thickness*$h{Num_Slices}/2, |
| 218 |
|
| 219 |
'-sattribute', ":history=$history", |
| 220 |
'-input', "$tmpfile.swp", $mncfile, |
| 221 |
"$h{Num_Slices}", "$h{Image_Dim_x}", "$h{Image_Dim_y}"); |
| 222 |
|
| 223 |
if ($verbose){ print STDOUT join(" ", @args) . "\n"; } |
| 224 |
system(@args) == 0 or die "Died during rawtominc system command\n"; |
| 225 |
|
| 226 |
# insert all the MIF stuff in the header |
| 227 |
undef(@args); |
| 228 |
push(@args, "minc_modify_header"); |
| 229 |
foreach (sort(keys(%h))){ |
| 230 |
push(@args, ("-sinsert", "MIF4:$_=$h{$_}")); |
| 231 |
} |
| 232 |
push(@args, $mncfile); |
| 233 |
|
| 234 |
if ($verbose){ print STDOUT join(" ", @args) . "\n"; } |
| 235 |
system(@args) == 0 or die "Died during minc_modify_header command\n"; |
| 236 |
&cleanup; |
| 237 |
|
| 238 |
|
| 239 |
# Unpack a value from a string (passed by reference) |
| 240 |
# A legendary bit of code nicked from dicom_to_minc |
| 241 |
sub destruct{ |
| 242 |
my($stringref, $offset, $type, $bs) = @_; |
| 243 |
|
| 244 |
my($code, $number, $size, $tempstring, $iloop, $max); |
| 245 |
|
| 246 |
# Check for byte swapping |
| 247 |
if($bs){ |
| 248 |
if($type !~ /^\s*([a-zA-Z])(\d+)?\s*$/){ |
| 249 |
die "Unrecognized data type \"$type\" on little-endian machine.\n"; |
| 250 |
} |
| 251 |
$code = $1; |
| 252 |
$number =(defined($2) ? $2 : 1); |
| 253 |
if(!defined($Unpack_codes{$code})){ |
| 254 |
die "Unrecognized unpack code \"$code\" on little-endian machine.\n"; |
| 255 |
} |
| 256 |
$size = $Unpack_codes{$code}; |
| 257 |
$tempstring = substr($$stringref, $offset, $number * $size); |
| 258 |
if($size > 1){ |
| 259 |
$max = $number * $size; |
| 260 |
for($iloop=0; $iloop < $max; $iloop+=$size){ |
| 261 |
substr($tempstring, $iloop, $size) = |
| 262 |
reverse(substr($tempstring, $iloop, $size)); |
| 263 |
} |
| 264 |
} |
| 265 |
return unpack("$type", $tempstring); |
| 266 |
} |
| 267 |
|
| 268 |
# No byte swapping required |
| 269 |
else{ |
| 270 |
return unpack("x$offset $type", $$stringref); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
# cleanup before dying |
| 275 |
sub cleanup { |
| 276 |
if ($verbose){ print STDOUT "Cleaning up ......\n"; } |
| 277 |
my @args = ('rm', '-r', '-f', "$tmpfile", "$tmpfile.swp"); |
| 278 |
system @args; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|