if [ $# -lt 3 ]; then 
 echo ''
 echo 'LinearlyRegisterImgA2ImgB-byHeadMappingViaHAMMER'
 echo 'input:  ImgA_seg.img  ImgB_seg.img  ImgA_seg.img.rig  IterationNum  R/r/g(R: Rotation-only matrix; r: rigid transformation; g: general; default: general)  OtherTypeOfImgA(to be transformed by same transformation, such as mask)'
 echo ''
 echo 'This program will output two files "GlobalTransformation.Affine" and "Field.1.img.ImgA2ImgB"'
 echo ''
 echo ''
 exit
fi


# Img A
MdlSeg="$1"

# Img B
Subj_Seg="$2"


# action ... 
Iter="3"
 if [ $# -gt 3 ]; #greater than 3 == greater/equal 4
 then
 Iter="$4"
 fi 
 echo 'Iter='$Iter''
cp ${MdlSeg}   MdlSeg.BrainImg 
cp ${Subj_Seg} SubjSeg.BranImg
HeadMappingByHAMMER_Hierarchical_InputMdlSeg-SubjSeg  MdlSeg.BrainImg  SubjSeg.BranImg   $Iter
\rm MdlSeg.BrainImg  Warped.*  SubjSeg.BranImg SubjSeg.BranImg.rig 


# Deformation field from Model to subject
echo ''
echo 'Deformation field from Model to subject'
Transform3DVectorField Field.1.img Field.1.img.trans -v256
mv Field.1.img.trans Field.1.img.ImgA2ImgB
\rm Field.1.img 
\rm GlobalTransformation.Affine


# From deformation field Field.1.img.ImgA2ImgB to estimate the linear transformation for A to B
echo ''
echo 'Compute Linear Transform From Displacement Field'
Constr=""
 if [ $# -gt 4 ]; #greater than 4, should be 5 or bigger
 then
 Constr="-$5"
 fi
 echo 'Constr='${Constr}''                                 


 ComputeLinearTransformFromDisplacementField Field.1.img.ImgA2ImgB  ${MdlSeg}  GlobalTransformation.Affine -v256 -D $Constr  -A
#if [ "$6". == . ]; 
# then                                                                                                               #-D
# ComputeLinearTransformFromDisplacementField Field.1.img.ImgA2ImgB  ${MdlSeg}  GlobalTransformation.Affine -v256 -D $Constr  -A
# fi
 #if [ "$6". != . ]; # only care the transformation on brain 
 # then
 # echo 'By Head-Mapping ... '                                            
 # File_Mask="$6"
 # Maskout3Dimg  ${MdlSeg}  File_Mask  ${MdlSeg}.brain
 # ComputeLinearTransformFromDisplacementField Field.1.img.ImgA2ImgB ${MdlSeg}.brain  GlobalTransformation.Affine -v256 -D $Constr -A
 # \rm ${MdlSeg}.brain 
 # fi
#\rm Field.1.img.ImgA2ImgB           


# transform images by using "GlobalTransformation.Affine"
#Transform3DImg $MdlSeg "$3" -v256 -I
     # compare slice No
     SliceNo_MdlFile=`getFileSize $MdlSeg`
      SliceNo_MdlFile=`expr ${SliceNo_MdlFile} / 65536 - 1`
       echo 'SliceNo_MdlFile='$SliceNo_MdlFile''
     SliceNo_FileSegment=`getFileSize $Subj_Seg`
      SliceNo_FileSegment=`expr ${SliceNo_FileSegment} / 65536 - 1`
       echo 'SliceNo_FileSegment='$SliceNo_FileSegment''
     cp $MdlSeg  MdlSeg.Crop
     if [ $SliceNo_MdlFile -lt $SliceNo_FileSegment ]; 
       then
           CropSlices  MdlSeg.Crop MdlSeg.Crop  -c0,${SliceNo_FileSegment}  
           Transform3DImg MdlSeg.Crop "$3" -v256 -I
       else
           Transform3DImg MdlSeg.Crop "$3" -v256 -I
           CropSlices  "$3"  "$3"  -c0,${SliceNo_FileSegment}  
     fi 
     \rm MdlSeg.Crop

    # To transform second image?
    if [ "$6". != . ]; 
    then
       echo 'transform seconding image ...'
       SecondImg="$6"
       cp $SecondImg  SecondImg.Crop
       if [ $SliceNo_MdlFile -lt $SliceNo_FileSegment ]; 
        then
            CropSlices  SecondImg.Crop SecondImg.Crop  -c0,${SliceNo_FileSegment}  
            Transform3DImg SecondImg.Crop ${SecondImg}.rig -v256 -I
        else	
            Transform3DImg SecondImg.Crop ${SecondImg}.rig -v256 -I
            CropSlices  ${SecondImg}.rig  ${SecondImg}.rig  -c0,${SliceNo_FileSegment}  
       fi 
       \rm SecondImg.Crop
    fi 


# -D was added on Nov 20, 2002
# -D was removed on Nov 25 again!