#! /bin/bash
#
# Script for viewing a minc file.
# uses display from ImageMagick to display the images.


PGM_CODE="P5"
PPM_CODE="P6"

# Check arguments
if [ $# -lt "1" ] || \
   [ $# -gt "2" ] || \
   [ "$1" = "-help" ] || \
   [ "$1" = "-h" ]
then
   cat <<END
Usage: $0 <infile.mnc> [<slice number>]

Images are displayed with the patients left on the left side
of the screen.

END
   exit -1
fi

infile="$1"

# Create temporary directory
tmpdir="/tmp/mincview-$$"

# cleanup nicely
trap "rm -rf $tmpdir" 0 1 2 3 13 15

mkdir -p $tmpdir

# Get dimension names
dims=( $(mincinfo -vardims image $infile ) )

echo " + Dimensions: ${dims[*]}"

# Check for vector dimension
pnm_code=$PGM_CODE
if [ "${dims[${#dims[*]} - 1]}" = "vector_dimension" ]
then
   ndims=$(( ${#dims[*]} - 1 ))
   nvec=$(mincinfo $infile -dimlength $dims[$#dims])
   start_suffix=",0"
   if [ $nvec != 3 ]
   then
      count_suffix=",1"
   else
      count_suffix=",3"
      pnm_code=$PPM_CODE
   fi
else
   ndims="${#dims[*]}"
   start_suffix=""
   count_suffix=""
fi

echo " + ndims: $ndims"

if [ $ndims -gt 3 ]
then
   nprefix=$(( $ndims - 3 ))
   start_prefix=$(awk 'BEGIN{for (i=0;i<$nprefix;i++) print "0,"}' < /dev/null)
   count_prefix=$(awk 'BEGIN{for (i=0;i<$nprefix;i++) print "1,"}' < /dev/null)
elif [ $ndims -lt "2" ]
then
   echo "No image found in file $infile"
   exit -1
else
   start_prefix=""
   count_prefix=""
fi

# Get number of slices and image dimensions
ind1=$(( $ndims - 3 ))
ind2=$(( $ndims - 2 ))
ind3=$(( $ndims - 1 ))
if [ $ind1 -gt "-1" ]
then
   nslices=$(mincinfo $infile -dimlength ${dims[$ind1]})
else
   nslices=1
fi

# figure out which slices to get
allslices=$(seq 0 $(($nslices - 1)))
slices_to_get=${2:-"$allslices"}

if [ "$slices_to_get" = "$2" ]
then
   if [ "$slices_to_get" -ge "$nslices" ] || [ "$slices_to_get" -lt "0" ]
   then
      echo ""
      echo "$0: Slice number $slices_to_get out of range (0-$nslices)"
      echo ""
      exit -1
   fi
fi

# Check for inverting images to get standard orientation
imgsize=( $(mincinfo -dimlength ${dims[$ind2]} -dimlength ${dims[$ind3]} $infile) )
echo " + imgsize: ${imgsize[0]}x${imgsize[1]}"
echo " + nslices $nslices"

# Loop through slices, if needed
echo -n "Loading slices"
for slice in $slices_to_get
do
   echo -n "."
   if [ $ndims -gt 2 ]
   then
      start="${start_prefix}$slice,0,0${start_suffix}"
      count="${count_prefix}1,${imgsize[0]},${imgsize[1]}${count_suffix}"
   else
      start="0,0$start_suffix"
      count="${imgsize[0]},${imgsize[1]}$count_suffix"
   fi
   output_file=$(printf "$tmpdir/slice-%04d.pgm" $slice)
   echo "$pnm_code" > $output_file
   echo "${imgsize[1]} ${imgsize[0]}" >> $output_file
   echo "255" >> $output_file
   mincextract -byte \
      -start $start \
      -count $count \
      -positive_direction \
      $infile >> $output_file
   
   slice=$(($slice + 1))
done
echo "Done"

cat <<END
--------------------------------------------------------------
Images are now being displayed with display from ImageMagick.

Use <space> to advance to the next image

The image number is displayed in the title of the window
--------------------------------------------------------------

END

# Display the images
display -monitor -geometry 512x512 -flip $tmpdir/slice-*.pgm 
