

/**
 * Forward and reverse DWT using filter banks
 * @author  jwr
 */
public class Jpeg2000Procesor {
    
   public double[][] sData;

    public int filterType;

public Jpeg2000Procesor( int filterType){
    this.filterType=filterType;
}

public void setData(double[][] sData){	
    this.sData=sData;
}



public double[][] procesIDWT(double[][] sData, int filterType, boolean rows){
        int h=sData.length;      
        int w=sData[0].length;
        /*
        if ((h%2)!=0) h=h-1;
        if ((w%2)!=0) w=w-1;
         */
        //System.out.println("Oto w,k="+w+","+h);
       
        Convolution con=new Convolution();
        double [] hfFilter;
        double [] lfFilter;
        WaveletFilters wf= new WaveletFilters();
        lfFilter=wf.getG0(filterType);
        hfFilter=wf.getG1(filterType);
        // 
        double[][] results=new double[h][w];
        if(!rows){
            int tmp=w;
            w=h;
            h=tmp;
        }
        
        double[] hfData=new double[w];
        double[] lfData=new double[w];
        int half=w/2;
        double [] buffer1=new double[half];
        double [] buffer2=new double[half];
        //double [] buffer1Zero=new double[w];
        //double [] buffer2Zero=new double[w];
        
        
        for(int i=0; i<h;i++){
            for(int j=0; j<w;j++){
                if(rows)
                    if(j<half){
                        buffer1[j]=sData[i][j];
                    } else {
                        buffer2[j-half]=sData[i][j];
                    }
                else 
                    if(j<half){
                     buffer1[j]=sData[j][i];
                     } else {
                        buffer2[j-half]=sData[j][i];
                    }
            }
            
            //res2=wf.zeroFill(res2,2,1);
            hfData=con.convolve1D(wf.zeroFill(buffer2,2,1),hfFilter,Convolution.SYMMETRIC,false);
            lfData=con.convolve1D(wf.zeroFill(buffer1,2,0),lfFilter,Convolution.SYMMETRIC,false);
            //hfData=wf.sample(hfData,2,0);
            //lfData=wf.sample(lfData,2,1);
            //System.out.println("Oto dane w,h, lfData.length"+w+", "+h+", "+lfData.length);
            for(int l=0; l<lfData.length;l++){
                if(rows){
                    results[i][l]=lfData[l]+hfData[l];
                    //results[i][l+lfData.length]=hfData[l];
                }else {
                    results[l][i]=lfData[l]+hfData[l];
                    //results[l+lfData.length][i]=hfData[l];
                }//end if rows
            }//end for l

       }//end for i
       return results;
 }
 


public double[][] procesDWT(double[][] sData, int filterType, boolean rows){
        int h=sData.length;      
        int w=sData[0].length;
        //System.out.println("Oto w,k="+w+","+h);
       
        Convolution con=new Convolution();
        double [] hfFilter;
        double [] lfFilter;
        WaveletFilters wf= new WaveletFilters();
        
        lfFilter=wf.getH0(filterType);
        hfFilter=wf.getH1(filterType);
        // 
        double[][] results=new double[h][w];
        if(!rows){
            int tmp=w;
            w=h;
            h=tmp;
        }
        
        double[] hfData=new double[w];
        double[] lfData=new double[w];
        
        double [] buffer=new double[w];
        
        for(int i=0; i<h;i++){
            for(int j=0; j<w;j++){
                if(rows)
                    buffer[j]=sData[i][j];
                else 
                    buffer[j]=sData[j][i];
            }
            hfData=con.convolve1D(buffer,hfFilter,Convolution.SYMMETRIC,false);
            lfData=con.convolve1D(buffer,lfFilter,Convolution.SYMMETRIC,false);
            hfData=wf.sample(hfData,2,0);
            lfData=wf.sample(lfData,2,1);

            for(int l=0; l<lfData.length;l++){
                if(rows){
                    results[i][l]=lfData[l];
                    results[i][l+lfData.length]=hfData[l];
                }else {
                    results[l][i]=lfData[l];
                    results[l+lfData.length][i]=hfData[l];
                }//end if rows
            }//end for l

       }//end for i
       return results;
}// end of procesDWT

public void processJpeg(int noOfBanks){
	
Quantization q= new Quantization();
sData=procesDWT(procesDWT(sData,filterType,true),filterType,false);
//sData=q.quantizy(sData);


}

}//end of class
