]> Creatis software - clitk.git/blob - scripts/compress_mhd.sh
f2e89128668cf1eac237471568a59b8fcb4a0a3a
[clitk.git] / scripts / compress_mhd.sh
1 #! /bin/bash 
2
3 compress_file()
4 {
5   local f=$1
6   if [ "$verbose" = "1" ]; then
7     echo "Compressing $f..."
8   fi
9
10   local msg=`clitkImageConvert -i $f -o $f -c`
11   if [ -z "$msg" ]; then
12     raw=`echo $f | sed 's/\.mhd/\.raw/'`
13     if test -e $raw; then
14       rm $raw
15     fi
16   fi
17 }
18
19 compress_directory()
20 {
21   local input=$1
22
23   files=$(find $input -name "*.mhd" | sort)
24   for f in $files; do
25     if ! grep -iq 'LIST' $f; then
26       # process all except 4D files
27       compress_file $f
28     else
29       # process only 4D files (just update pointers to new zraw files)
30       # assumes each .RAW in the list has a corresponding .MHD, which
31       # must be compressed separately
32       sed -i 's/\.raw/\.zraw/g' $f
33       if grep -q "CompressedData" $f; then
34         sed -i 's/CompressedData.*/CompressedData = True/' $f
35       else
36         tmp=/tmp/$RANDOM.mhd
37         echo "CompressedData = True" > $tmp
38         cat $f >> $tmp
39         mv $tmp $f
40       fi 
41     fi
42   done
43 }
44
45 # main program
46
47 if [ $# -lt 1 -o $# -gt 2 ]; then
48   echo "Invalid params. `basename $0` [-h | --help] for help" 1>&2
49   exit -1
50 fi
51
52 if [ -n "`echo $@ | grep '\-h\|--help'`" ]; then
53   echo "Usage: `basename $0` {FILE | DIRECTORY | -h | --help | -v}"
54   exit 0
55 fi
56
57 if [ -n "`echo $@ | grep '\-v'`" ]; then
58   echo Verbose mode : ON
59   verbose=1
60 fi  
61
62 input=$1
63 if test -d $input; then
64   compress_directory $input
65 elif test -f $input; then
66   compress_file $input
67 else 
68   echo "Unknow input file type." 1>&2
69 fi