// Berechnet Diskrete Fourier Transformation eines Bildes,
// daraus das konjugiert komplexe
// und danach die Rücktransformation

#include <opencv/cv.h>
extern "C"
{
#include <opencv/highgui.h>
}
#include <cmath>
#include <iostream>
#include "../libseemicro/seemicro.h"

using namespace std;
using namespace seemicro;

IplImage *img;

int main(int argc, char** argv)
{
	if(argc != 2 && argc != 3)
	{
		cerr << "Call me as: " << argv[0] << " <inputimage> [<outputimage>]\n";
		return 1;
	}
	ImageFile i(argv[1]);

     	IplImage* iGray = cvCreateImage(cvSize(i.img->width, i.img->height), IPL_DEPTH_8U, 1);
     	IplImage* in = cvCreateImage(cvSize(i.img->width, i.img->height), IPL_DEPTH_32F, 1);
     	IplImage* freqBild = cvCreateImage(cvSize(i.img->width, i.img->height), IPL_DEPTH_32F, 1);
     	IplImage* out = cvCreateImage(cvSize(i.img->width, i.img->height), IPL_DEPTH_32F, 1);

	cvConvertImage(i.img, iGray);// 8u3 -> 8u1
	cvConvert(iGray, in);// 8u1 -> 32f1

	cvDFT(in, freqBild, CV_DXT_FORWARD);

	// konjugiert komplexes berechnen
	for(int x=2;x < i.img->width; x+=2)
	  for(int y=0;y < i.img->height; y++)
	  {
	    float p = getPixel32f_1(freqBild, x, y);
	    putPixel32f_1(freqBild, x, y, -p);
	  }
	
	cvDFT(freqBild, out, CV_DXT_INVERSE_SCALE);
        cvConvertScale(out, out, 1./float(i.img->width));
	
	if(argc==2)
	{
	  cvNamedWindow("Back", 1);
	  cvShowImage("Back", out);
          cvWaitKey();
	}
	else cvSaveImage(argv[2], out);

	cvReleaseImage(&img);
	cvReleaseImage(&out);
	cvDestroyWindow("Back");
	return 0;
}

