/* -*- mode: c++; c-basic-offset: 3 -*-
 *
 * PANDORE (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 Jalal Fadili - 02-02-2005
 * @author Christophe Glinel - 20-07-2005
 */

/**
 * @file sumbands.cpp
 * 2D Undecimated Discrete Wavelet Transform (UDWT).
 * The UDWT is implemented only with the 
 * 2D gray level images.
 *
 * Transformée discrète par ondelettes non décimées. (UDWT)
 * La UDWT implémentée ici ne traite que le cas des images
 * 2D de niveaux de gris.
 */

#include <pandore.h>
using namespace pandore;

/**
 * SumBands
 * Inputs :
 * 	ims : The input image.
 * 	imd : The output image.
 */
template <typename U,typename V>
Errc SumBands(Img3d<U>& ims,Img2d<V>& imd){
   Long width   =  ims.Width();
   Long height  =  ims.Height();
   Long depth   =  ims.Depth();
   Long i,j,d;
	
   for(i = 0; i < height; i++)
      for(j = 0; j < width; j++)
	 for(d = 0 , imd[i][j] = 0 ; d < depth; d++)
	    imd[i][j] +=  ims[d][i][j];
	
   return SUCCESS;
}


#ifdef MAIN
#define USAGE   "USAGE : %s  [im_in|-] [im_out|-]"
#define PARC    0
#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 region map.
   Pobject* objin[FINC+1];   // The input objects;
   Pobject* objs[FINC+1];    // The source objects masked.
   Pobject* objout[FOUTC+1]; // The output object.
   Pobject* objd[FOUTC+1];   // The result object of the execution.
   char* parv[PARC+1];       // The input parameters.

   ReadArgs(argc,argv,PARC,FINC,FOUTC,&mask,objin,objs,objout,objd,parv,USAGE,MASK);
if ( objs[0]->Type() == Po_Img3duc){
   Img3duc* const ims   = (Img3duc *)objs[0];
   objd[0]  = new Img2dsf(ims->Height(), ims->Width());
   Img2dsf* const imd = (Img2dsf*)objd[0];
   result = SumBands(*ims, *imd);
   goto end;
      
}
else 
if ( objs[0]->Type() == Po_Img3dsl){
   Img3dsl* const ims   = (Img3dsl *)objs[0];
   objd[0]  = new Img2dsf(ims->Height(), ims->Width());
   Img2dsf* const imd = (Img2dsf*)objd[0];
   result = SumBands(*ims, *imd);
   goto end;
      
}
else 
if ( objs[0]->Type() == Po_Img3dsf){
   Img3dsf* const ims   = (Img3dsf *)objs[0];
   objd[0]  = new Img2dsf(ims->Height(), ims->Width());
   Img2dsf* const imd = (Img2dsf*)objd[0];
   result = SumBands(*ims, *imd);
   goto end;
      
}
else 
  {
     PrintErrorFormat(objin,FINC);
     result = FAILURE;
  }	

end:
  if (result)
	WriteArgs(argc,argv,PARC,FINC,FOUTC,&mask,objin,objs,objout,objd,MASK);
  Exit(result);
  return 0;
}
#endif
