/* -*- c-basic-offset: 3 -*-
 *
 * PANTHEON Project
 *
 * GREYC IMAGE
 * 6 Boulevard Maréchal Juin
 * F-14050 Caen Cedex France
 *
 * This file is free software. You can use it, distribute it
 * and/or modify it. However, the entire risk to the quality
 * and performance of this program is with you.
 *
 * Please email any bugs, comments, and/or additions to this file to:
 * pandore@greyc.ensicaen.fr
 * http://www.greyc.ensicaen.fr/EquipeImage/Pandore/
 */

/**
 * @author Ludovic SOLTYS - 02-02-2003
 */

#include <pandore.h>
using namespace pandore;

/**
 * @file pgetsubband.cpp
 * Extract subimage from dwt image.
 */

/**
 * cx,cy les coordonnees du coin superieur gauche de
 * la sous image ŕ extraire.
 */
Errc GetSubband( const Img2dsf &ims, Img2dsf &imd, int cy, int cx ) {
   Point2d p,q;
   
   q.x=cx; q.y=cy;
   for (p.y=0;p.y<imd.Height();p.y++)
      for (p.x=0;p.x<imd.Width();p.x++)
	 imd[p] = ims[p+q];
   
   return SUCCESS;
}

#ifdef MAIN
#define	USAGE	"usage: %s scale number [im_in|-][im_out|-]"
#define	PARC	2
#define	FINC	1
#define	FOUTC	1
#define	MASK	0

int main( int argc, char *argv[] ) {
  Errc result;              // The result code of the execution.
  Pobject *mask;            // The mask.
  Pobject *objin[FINC+1];   // The input objects;
  Pobject *objs[FINC+1];    // The source objects masked by mask.
  Pobject *objout[FOUTC+1]; // The output object unmasked by mask.
  Pobject *objd[FOUTC+1];   // The result object.
  char* parv[PARC+1];       // The input parameters.
  
  ReadArgs(argc,argv,PARC,FINC,FOUTC,&mask,objin,objs,objout,objd,parv,USAGE,MASK);
  
  switch(objs[0]->Type()) {
  case Po_Img2dsf : {
     int scale=atoi(parv[0]);
     Img2dsf* const ims=(Img2dsf*)objs[0];
     
     int I=(ims->Height()) >> (scale);
     int J=(ims->Width()) >> (scale);
     
     objd[0]=new Img2dsf(I,J);
     
     Img2dsf* const imd=(Img2dsf*)objd[0];
     
     if (atoi(parv[1])==1)
	result=GetSubband(*ims, *imd, 0, 0);
     
     else if (atoi(parv[1])==2)
	result=GetSubband(*ims, *imd, 0, J);
     
     else if (atoi(parv[1])==3)
	result=GetSubband(*ims, *imd, I, 0);
     
     else if (atoi(parv[1])==4)
	result=GetSubband(*ims, *imd, I, J);
  } break;
  default :
     PrintErrorFormat(objin,FINC);
     result =FAILURE;
  }
  
  WriteArgs(argc,argv,PARC,FINC,FOUTC,&mask,objin,objs,objout,objd,MASK);
  Exit(result);
  return 0;
}

#endif
