#!/bin/sh
#--------------------------- Shell MegaWave2 Macro --------------------------#
_sccsid="%Z%MegaWave2 %R%, %M% %I%, %G%";
_Prog="Fwivq"
_Vers="1.3"
_Date="1997"
_Func="Reconstruct an image compressed by vector quantizing its wavelet transform";
_Auth="Jean-Pierre D'Ales";
_Usage="[-b1 BiFilt1] [-b2 BiFilt2] [-o OrthoFilt] [-e EdgeFilt] compressfile codebook"

#---- MegaWave2 - Copyright (C) 1992-94 J.F. & S.P. All Rights Reserved. ----#

#	input:		the compressed file, the wavelet filters, 
#                       the codebook sequence(s)
#	output:		the reconstructed image
#
#       MegaWave2 modules used: fwivq

# v1.2: fixed owave directory bug (L.Moisan)
# v1.3: fixed $BIN and ${MY_MEGAWAVE2}/tmp bugs (JF)

#----- Check syntax -----
if [ $# -le 1 ] 
then
s	. .mw2_help_lg_com
fi

MWDATA="${MEGAWAVE2}/data"
MY_MWDATA="${MY_MEGAWAVE2}/data"
GROUP=compression/vqwave


# Default wavelet filters
WAVELETFILTER="wave/biortho/h/sd07.ir"
WAVELETFILTER2="wave/biortho/htilde/sd09.ir"
WAVELETOPT="-b wave/biortho/htilde/sd09.ir"
WAVELETEDGEFILTER="no filter"

COMPRESS=""
CBOPT=""

STOP="FALSE"

while [ "$1" != "" -a "$STOP" = "FALSE" ]
do
  case "$1" in
    # Use customized wavelet filters
    -b1)        shift
		WAVELETFILTER="$1"
		shift
		;;
    -b2)        shift
		WAVELETOPT="-b $1"
		shift
		;;
    -o)         shift
		WAVELETFILTER="$1"
		shift
		;;
    -e)         shift
		WAVELETOPT="-e $1"
		shift
		;;

    # Input compressed file
    [!-]*)	COMPRESS=$1
		PREFIX=`echo "${COMPRESS}" | cut -f2 -d.`
		NGROUP=1
		NGSUFFIX=2
		while [ "${PREFIX}" != "" ]
		do
		  NGROUP=`echo "${NGROUP} 1 + p" | dc`
		  NGSUFFIX=`echo "${NGROUP} 1 + p" | dc`
		  PREFIX=`echo "${COMPRESS}" | cut -f"${NGSUFFIX}" -d.`
		done
		PREFIX=`echo "${COMPRESS}" | cut -f"${NGROUP}" -d.`
		if [ "${PREFIX}" = "comp" -a ${NGROUP} -ge 2 ]; then
		  NGROUP=`echo "${NGROUP} 1 - p" | dc`
                  PREFIX=`echo "${COMPRESS}" | cut -f1-"${NGROUP}" -d.`
		  NCAR=`echo "${PREFIX}" | wc -c`
		  NCAR=`echo "${NCAR} 2 - p" | dc`
                  PREFIX=`echo "${PREFIX}" | cut -c 1-"${NCAR}"`
		else
                  PREFIX="${COMPRESS}"
		fi
		shift

    # Prefix of codebooks
		PREFCODEBOOK=$1
		CODEBOOK=${PREFCODEBOOK}.cb
		if  [ ! -f $CODEBOOK ]; then
		  echo "Cannot find codebookfile ${CODEBOOK}"
		  exit 0
		fi
		CODEBOOKQ=${PREFCODEBOOK}_q.cb
		if  [ -f $CODEBOOKQ ]; then
		  CBOPT="${CBOPT} -A ${CODEBOOKQ}"
		fi
		CODEBOOKQR=${PREFCODEBOOK}_qr.cb
		if  [ -f $CODEBOOKQR ]; then
		  CBOPT="${CBOPT} -D ${CODEBOOKQR}"
		fi
		CODEBOOKX=${PREFCODEBOOK}_x.cb
		if  [ -f $CODEBOOKX ]; then
		  CBOPT="${CBOPT} -x ${CODEBOOKX}"
		fi
		CODEBOOKXQ=${PREFCODEBOOK}_xq.cb
		if  [ -f $CODEBOOKXQ ]; then
		  CBOPT="${CBOPT} -B ${CODEBOOKXQ}"
		fi
		CODEBOOKXQR=${PREFCODEBOOK}_xqr.cb
		if  [ -f $CODEBOOKXQR ]; then
		  CBOPT="${CBOPT} -E ${CODEBOOKXQR}"
		fi
		CODEBOOKY=${PREFCODEBOOK}_y.cb
		if  [ -f $CODEBOOKY ]; then
		  CBOPT="${CBOPT} -y ${CODEBOOKY}"
		fi
		CODEBOOKYQ=${PREFCODEBOOK}_yq.cb
		if  [ -f $CODEBOOKYQ ]; then
		  CBOPT="${CBOPT} -C ${CODEBOOKYQ}"
		fi
		STOP="TRUE"
		;;

    # Error
    *)		echo "Unrecognized option $1"
		exit 1
		;;
  esac
done



# Check that wavelet filters are available

if [ ! -f "${WAVELETFILTER}" ] && [ ! -f "${MWDATA}/${WAVELETFILTER}" ] && [ ! -f "${MY_MWDATA}/${WAVELETFILTER}" ] && [ ! -f "${MY_MWDATA}/${GROUP}/${WAVELETFILTER}" ]; then
  echo "Cannot find ${WAVELETFILTER} in Megawave2 data directories!"
  exit 1
fi

if [ "${WAVELETEDGEFILTER}" = "no filter" ]; then

  if [ "${WAVELETFILTER2}" != "" ]; then
    if [ ! -f "${WAVELETFILTER2}" ] && [ ! -f "${MWDATA}/${WAVELETFILTER2}" ] && [ ! -f "${MY_MWDATA}/${WAVELETFILTER2}" ] && [ ! -f "${MY_MWDATA}/${GROUP}/${WAVELETFILTER2}" ]; then
      echo "Cannot find ${WAVELETFILTER2} in Megawave2 data directories!"
      exit 1
    fi
  fi

else

  if [ ! -f ${WAVELETEDGEFILTER} ] && [ ! -f ${MWDATA}/${WAVELETEDGEFILTER} ] && [ ! -f ${MY_MWDATA}/${WAVELETEDGEFILTER} ] && [ ! -f ${MY_MWDATA}/${GROUP}/${WAVELETEDGEFILTER} ]; then
    echo "Cannot find ${WAVELETEDGEFILTER} in Megawave2 data directories!"
    exit 1
  fi

fi



### Reconstruct image ###

#echo "fwivq $WAVELETOPT $CBOPT ${COMPRESS} $CODEBOOK $WAVELETFILTER ${PREFIX}r.rim"

fwivq $WAVELETOPT $CBOPT ${COMPRESS} $CODEBOOK $WAVELETFILTER ${PREFIX}r.rim
	






