#/bin/bash

# dostali jsme cislo slice?
if [ ! $# -eq 1 ]; then
	echo "Add the number of slice to be extracted."
	exit 0
fi

# jake jsou rozmery obrazku
XYZ=`of_imageinfo scenePhantoms000.ics | grep 'Image size' | sed 's/^.*(\(.*\),\(.*\),\(.*\)).*$/\1 \2 \3/' `

# kolik je obrazku
NO=`ls scenePhantoms???.ics | wc -l`

# vysledky si naskladanem jako parametry tohoto skriptu (aby jsme k nim dobre mohli pristupovat)
set -- $1 $XYZ $((NO-1))

# je cislo slice OK?
if [ $1 -ge $4 ]; then
	echo "Can't use slice no. $1 when the images have only $4 slices."
	exit 1
fi

# vytvorime si pomocny adresar, kam si ulozime meziprodukty
mkdir "slice_z=$1"

# vytahneme ty jednotlive slices
for i in scenePhantoms*; do
	echo "Extracting from image $i."
	echo
	of_crop $i slice_z\=${1}/`basename $i .ics`_z=${1}.ics 0 0 ${1} $2 $3 1
done

# a zase to spojime v jeden ST obrazek
if [ $5 -lt 100 ]; then
	of_build3D  scenePhantomsST_z=${1}.ics  slice_z\=${1}/scenePhantoms0 2 _z=${1}.ics 0 $5 0.123
else
	of_build3D  scenePhantomsST_z=${1}.ics  slice_z\=${1}/scenePhantoms  3 _z=${1}.ics 0 $5 0.123
fi
