#!/bin/sh
#
# Reconstruct an image compressed by vector quantizing its wavelet
# transform

usage() {
    echo "megawave_fwivq [-h] [-b1 BiFilt1] [-b2 BiFilt2] [-o OrthoFilt] [-e EdgeFilt] compressfile codebook"
    echo "input:  the compressed file, the wavelet filters,"
    echo "        the codebook sequence(s)"
    echo "output: the reconstructed image"
}

if [ $# -le 1 ]; then
    usage
    exit 1
fi

SHARE=/usr/share/megawave
DATA=$SHARE/data

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

COMPRESS=""
CBOPT=""

while [ "$1" != "" ]; do
    case "$1" in
        # Help
	-h)
            usage
	    exit
	    ;;
	
        # 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
	    break
	    ;;

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

# Check that wavelet filters are available
if [ ! -f $WAVELETFILTER ]; then 
    exit 1
fi

if [ "$WAVELETEDGEFILTER" = "no filter" ]; then
    if [ "$WAVELETFILTER2" != "" ]; then
	if [ ! -f $WAVELETFILTER2 ]; then
	    echo "Cannot find $WAVELETFILTER2 in MegaWave data directories!"
	    exit 1
	fi
    fi
else
    if [ ! -f $WAVELETEDGEFILTER ]; then
	echo "Cannot find $WAVELETEDGEFILTER in MegaWave data directories!"
	exit 1
    fi
fi

# Reconstruct image
megawave fwivq $WAVELETOPT $CBOPT $COMPRESS $CODEBOOK $WAVELETFILTER ${PREFIX}r.rim
