//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "fourier.h"
#include "fftw3.h"
#include "utilidades.h"
#include "math.h"
#include "Math.hpp"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Spin"
#pragma resource "*.dfm"
TForm13 *Form13;

fftw_complex *datos_fft;   //datos de la imagen en bruto
double *datos_img;
fftw_plan plan;
double *amp,*amp2;
	int tamaY;

//---------------------------------------------------------------------------
__fastcall TForm13::TForm13(TComponent* Owner)
	: TForm(Owner)
{

}
//---------------------------------------------------------------------------

void __fastcall TForm13::setImagen(TPicture* img){


}

void __fastcall TForm13::Button1Click(TObject *Sender)
{
  fftw_destroy_plan(plan);
  fftw_free(datos_img);
  fftw_free(datos_fft);
}
//---------------------------------------------------------------------------
void __fastcall TForm13::Button2Click(TObject *Sender)
{
  fftw_destroy_plan(plan);
  fftw_free(datos_img);
  fftw_free(datos_fft);
}
//---------------------------------------------------------------------------
int __fastcall TForm13::maximo(int v1,int v2){
 int ret;
 if (v1>v2) {
	 ret=v1;
 } else {
	ret=v2;
 }
 return ret;
}

int __fastcall TForm13::minimo(int v1,int v2){
int ret;
 if (v1<v2) {
	 ret=v1;
 } else {
	ret=v2;
 }
 return ret;
}

void __fastcall TForm13::Button3Click(TObject *Sender)
{
	int i,j,k,l;
	float max,min;
	Byte *pixel;
	TPicture* imgtmp;

	imgtmp=new TPicture;

	Label1->Caption="Calculando...";

	max=0;min=100;

	datos_img=new double[sizeof(double)*anchura*altura];

	for (i = 0; i < altura; i++) {
		for (j = 0; j < anchura; j++) {
		  datos_img[j*anchura+i]=(double)(doc->dameFinal()->Bitmap->Canvas->Pixels[i][j]);
		}
	}

	tamaY=(altura/2)+1;
	datos_fft=new fftw_complex[sizeof(fftw_complex)*anchura*tamaY];
	amp=new double[sizeof(fftw_complex)*anchura*tamaY];
	amp2=new double[sizeof(fftw_complex)*anchura*tamaY];

	plan=fftw_plan_dft_r2c_2d(anchura,altura,datos_img,datos_fft,FFTW_ESTIMATE);
	fftw_execute(plan);

	Image1->Picture->LoadFromFile("./res/paletafft.bmp");    //cargar paleta
	imgtmp->LoadFromFile("./res/paletafft.bmp");    //cargar paleta


	Image1->Picture->Bitmap->Width=anchura;
	Image1->Picture->Bitmap->Height=tamaY*2;
	imagen->Bitmap->Width=anchura;
	imagen->Bitmap->Height=tamaY;
	imgtmp->Bitmap->Width=anchura;
	imgtmp->Bitmap->Height=tamaY*4;
	Memo1->Lines->Add(IntToStr(anchura)+"x"+IntToStr(tamaY));
	for (i = 0; i < anchura-1; i++) {
		for (j=0; j<  tamaY; j++) {
			//sacamos el modulo  (magnitud)
			amp[tamaY*i+j]=log10((sqrt(datos_fft[tamaY*i+j][0]*datos_fft[tamaY*i+j][0]+datos_fft[tamaY*i+j][1]*datos_fft[tamaY*i+j][1])));
			Memo1->Lines->Add("["+IntToStr(tamaY*i+j)+"] >>> "+FloatToStr(datos_fft[tamaY*i+j][0])+"  +("+FloatToStr(datos_fft[tamaY*i+j][1])+"i)     ||"+FloatToStr(amp[tamaY*i+j]));
			if (amp[tamaY*i+j]>max) {
				max=amp[tamaY*i+j];
			}
			if (amp[tamaY*i+j]<min) {
				min=amp[tamaY*i+j];
			}
		}
	}
  Memo1->Lines->Add("COLORES");
	 for (i = 0; i < anchura-1; i++) {
	 for (j=0; j<  tamaY; j++) {
			//sacamos el modulo  (magnitud)
//			amp2[tamaY*i+j]=(((amp[tamaY*i+j]-1)/max)*100000)/390;
			amp2[tamaY*i+j]=(((amp[tamaY*i+j]-min-1)/max)*256);
			Memo1->Lines->Add("["+IntToStr(tamaY*i+j)+"]   Color:"+FloatToStr(amp2[tamaY*i+j]));
		}
	}




	for (k=0; k < anchura; k++) {
	 for (l=0; l < tamaY; l++) {
		i=(k+(anchura-1)/2)%(anchura-1);   //centramos la imagen
		j=(l+(tamaY)/2)%tamaY;
	  // i=k;
	  j=l;

		pixel=(byte *)imagen->Bitmap->ScanLine[l];

			if ((256*(amp2[i*tamaY+j]/tamaY))>255) {
			pixel[k]=255;
		} else
		if ((256*(amp2[i*tamaY+j]/tamaY))<0) {
		   pixel[k]=0;
		} else
		{


				//pixel[k]=(byte)(multi->Value*(amp2[i*tamaY+j]));
				pixel[k]=(byte)(minimo((int)255,(int)maximo((int)0,(int)(multi->Value*(amp2[i*tamaY+j])))));
		}

	 }

	}


	imgtmp->Bitmap->Canvas->CopyRect(Rect(0,tamaY-1,anchura,-1),imagen->Bitmap->Canvas,Rect(0,0,anchura,tamaY));
	imgtmp->Bitmap->Canvas->Draw(0,tamaY,imagen->Bitmap);


//	documento[actual].dameHandle(),X,Y,buffer->Picture->Bitmap->Width,buffer->Picture->Bitmap->Height,dc,0,0,SRCERASE);
	Memo1->Lines->Add("Max: "+FloatToStr(max)+"  Min:"+FloatToStr(min));
	Image1->Canvas->Draw(0,0,imgtmp->Bitmap);
	Label1->Caption="terminado";
	Memo1->Visible=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm13::SpinEdit1Change(TObject *Sender)
{
  if (SpinEdit1->Value!=1) {
	  Image1->Stretch=true;
	  Image1->Width=anchura*SpinEdit1->Value;
	  Image1->Height=altura*SpinEdit1->Value;
  } else {
	  Image1->Stretch=false;
	  Image1->Width=anchura;
	  Image1->Height=tamaY;

  }
}
//---------------------------------------------------------------------------
void __fastcall TForm13::setDoc(utilidades* documento){
	doc=documento;
	doc->reinicia();
	anchura=doc->dameAnchura();
	altura=doc->dameAltura();
	imagen=new TPicture;
	imagen->Assign(doc->dameFinal());
}



