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

#include <vcl.h>
#pragma hdrstop

#include "filtrousr.h"
#include "histograma.h"
#include "main.h"
#include  "malloc.h"
#include <fstream.h>
#include <iostream.h>
#include "utilidades.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm6 *Form6;
//---------------------------------------------------------------------------
__fastcall TForm6::TForm6(TComponent* Owner)
	: TForm(Owner)
{
  creaRejilla(true);
}
//---------------------------------------------------------------------------
void __fastcall TForm6::creaRejilla(bool nueva){
	int i,j;

	if (nueva==false) {
	  free(mascara);
	}

	  rejilla->ColCount=StrToInt(Edit1->Text);
	  rejilla->RowCount=StrToInt(Edit1->Text);
	  rejilla->Repaint();



	   mascara=(int **)calloc(StrToInt(Edit1->Text),sizeof(int));
	   for (i = 0; i < StrToInt(Edit1->Text); i++) {
		   mascara[i]=(int *) calloc(StrToInt(Edit1->Text),sizeof(int));
	   }

       borraRejilla();
  }


void __fastcall TForm6::borraRejilla(){
	int i,j;

	  for (i = 0; i < StrToInt(Edit1->Text); i++) {
		  for (j = 0; j < StrToInt(Edit1->Text); j++) {
			  mascara[i][j]=0;
		  }
	  }

	   for (i = 0; i < rejilla->ColCount; i++) {
	   for (j = 0; j < rejilla->RowCount; j++) {
		  rejilla->Cells[i][j]="0";
	  }

	}
}

void __fastcall TForm6::Button1Click(TObject *Sender)
{


  if ((StrToInt(Edit1->Text)%2!=0)) {
	   creaRejilla(false);
  } else {
	  ShowMessage("El tamaņo de la mascara debe ser impar!!!");
  }

}
//---------------------------------------------------------------------------
void __fastcall TForm6::Button3Click(TObject *Sender)
{
  menu->Popup(Form6->Left+Button3->Left,Form6->Top+Button3->Top-50);
}
//---------------------------------------------------------------------------
void __fastcall TForm6::rejillaDrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
          TGridDrawState State)
{
  if ((ARow==rejilla->ColCount/2)&&(ACol==rejilla->RowCount/2)) {
	  rejilla->Canvas->Pen->Color=clGreen;
	  rejilla->Canvas->Brush->Style=bsClear;
	  rejilla->Canvas->Rectangle(Rect.Left,Rect.Top,Rect.Right,Rect.Bottom);
	  rejilla->Canvas->Font->Color=clWhite;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm6::Abrir1Click(TObject *Sender)
{
	ifstream f;
  int i,j;
  char tmp[255];


 abrir->InitialDir=ExtractFilePath(ParamStr(0))+"mascaras/";
 if (abrir->Execute()){
 	f.open(abrir->FileName.c_str());
	if (!f) {
		ShowMessage("Error leyendo el archivo: "+abrir->FileName);
	}
	else {
	  f>>tmp;
	  Edit1->Text=tmp;
	  f>>tmp;
	  Edit2->Text=tmp;

	  rejilla->ColCount=StrToInt(Edit1->Text);
	  rejilla->RowCount=StrToInt(Edit1->Text);

  free(mascara);
  mascara=(int **)calloc(StrToInt(Edit1->Text),sizeof(int));
  for (i = 0; i < StrToInt(Edit1->Text); i++) {
   mascara[i]=(int *) calloc(StrToInt(Edit1->Text),sizeof(int));
  }


	  for (i = 0; i < rejilla->ColCount; i++) {
		  for (j = 0; j < rejilla->RowCount; j++) {
			  f>>tmp;
			  mascara[i][j]=StrToInt(tmp);
		  }
	  }
	  f.close();

	  pintaRejilla();



	}
 }



}
//---------------------------------------------------------------------------
void __fastcall TForm6::Guardar1Click(TObject *Sender)
{
 ofstream f;
  int i,j;


 guardar->InitialDir=ExtractFilePath(ParamStr(0))+"mascaras/";
 if (guardar->Execute()){
 cargaMatriz();
		f.open(guardar->FileName.c_str());
	if (!f) {
		ShowMessage("Error creando el archivo: "+guardar->FileName);
	}
	else {
	  f<<StrToInt(Edit1->Text)<<endl;     //tamaņo
	  f<<StrToInt(Edit2->Text)<<endl;     //divisor
	  for (i = 0; i < rejilla->ColCount; i++) {
		  for (j = 0; j < rejilla->RowCount; j++) {
			  f<<mascara[i][j]<<endl;
		  }
	  }
	  f.close();
	}
 }




}
//---------------------------------------------------------------------------
void __fastcall TForm6::Borrar1Click(TObject *Sender)
{
 borraRejilla();
}
//---------------------------------------------------------------------------
void __fastcall TForm6::cargaMatriz(){
	int i,j;
   	for (i = 0; i < rejilla->ColCount; i++) {
		for (j=0; j<rejilla->RowCount; j++) {
			mascara[i][j]=StrToInt(rejilla->Cells[i][j]);
		}
	}

}

void __fastcall TForm6::pintaRejilla(){
	int i,j;

	for (i = 0; i<rejilla->ColCount; i++) {
		for (j = 0;j<rejilla->RowCount;j++){
			rejilla->Cells[i][j]=IntToStr(mascara[i][j]);
		}
	}
}

void __fastcall TForm6::setDoc(utilidades* documento){
	doc=documento;
	doc->reinicia();
	anchura=doc->dameAnchura();
	altura=doc->dameAltura();
}

void __fastcall TForm6::pinta(){
	Image1->Picture=doc->dameProcesada();
}

void __fastcall TForm6::Button4Click(TObject *Sender)
{
  //FILTRAR!!!!
  doc->filtroUsr(mascara,StrToInt(Edit1->Text),StrToInt(Edit2->Text));
  Image1->Picture=doc->dameProcesada();
}
//---------------------------------------------------------------------------

void __fastcall TForm6::Button2Click(TObject *Sender)
{
	 int i,j;

	 divisor=0;
	 for (i=0; i < rejilla->ColCount; i++) {
		for (j=0; j < rejilla->RowCount; j++) {
			divisor=divisor+StrToInt(rejilla->Cells[i][j]);
		}
	 }
	 Edit2->Text=divisor;
}
//---------------------------------------------------------------------------

void __fastcall TForm6::FormHide(TObject *Sender)
{
  free(mascara);
}
//---------------------------------------------------------------------------

void __fastcall TForm6::Button5Click(TObject *Sender)
{
  doc->validarImagen();
  histo_win->calcular(doc);
  histo_win->dibujar();
  Form1->actualizaImagen(doc->dameFinal());
  Form6->Hide();
}
//---------------------------------------------------------------------------

void __fastcall TForm6::Button6Click(TObject *Sender)
{
	Form6->Hide();
}
//---------------------------------------------------------------------------

void __fastcall TForm6::Button7Click(TObject *Sender)
{
 int i,j;

	for (i = 0; i<rejilla->ColCount; i++) {
		for (j = 0;j<rejilla->RowCount;j++){
			rejilla->Cells[i][j]=IntToStr(rand()%1001);
		}
	}
	cargaMatriz();
}
//---------------------------------------------------------------------------

