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

#include <vcl.h>
#pragma hdrstop

#include "histograma.h"
#include "utilidades.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
Thisto_win *histo_win;
//---------------------------------------------------------------------------
__fastcall Thisto_win::Thisto_win(TComponent* Owner)
	: TForm(Owner)
{
}

__fastcall Thisto_win::calcular_nuevo(TPicture* imgIn){
	int i,j,max,maxR,maxG,maxB;
	TRGBTriple pixel;

	for (i = 0; i < 256; i++) {
		hdata[i]=0;
		hdataR[i]=0;
		hdataG[i]=0;
		hdataB[i]=0;
	}

	for (i = 0; i < imgIn->Bitmap->Width; i++) {
		for (j=0; j < imgIn->Bitmap->Height; j++) {
		 if (doc->dameModo()==8) {
			hdata[0+1]=hdata[0+1]+1;
		 } else {
		   //	pixel=graficos->getPixel24(i,j,imgIn);
			hdataR[0+1]=hdataR[0+1]+1;
			hdataG[0+1]=hdataG[0+1]+1;
			hdataB[0+1]=hdataB[0+1]+1;
		 }
		}
	}

	max=1;
	maxR=1;maxG=1;maxB=1;


	for (i = 0; i < 256; i++) {
	if (doc->dameModo()==8) {
		hdata100[i]=(hdata[i]*100)/max;
	} else {
		hdata100R[i]=(hdataR[i]*100)/maxR;
		hdata100G[i]=(hdataG[i]*100)/maxG;
		hdata100B[i]=(hdataB[i]*100)/maxB;
	}
	}
}



__fastcall Thisto_win::calcular(utilidades* documento)
{
	int i,j,max,maxR,maxG,maxB;
	TRGBTriple pixel;
	int anchura,altura;


	for (i = 0; i < 256; i++) {
		hdata[i]=0;
		hdataR[i]=0;
		hdataG[i]=0;
		hdataB[i]=0;
	}
	doc=documento;
  anchura=doc->dameAnchura();
  altura=doc->dameAltura();

	for (i = 0; i < anchura; i++) {
		for (j=0; j < altura; j++) {
		 if (doc->dameModo()==8) {
			hdata[doc->getPixel(i,j,doc->dameFinal())+1]=hdata[doc->getPixel(i,j,doc->dameFinal())+1]+1;
		 } else {
			pixel=doc->getPixel24(i,j,doc->dameFinal());
			hdataR[pixel.rgbtRed+1]=hdataR[pixel.rgbtRed+1]+1;
			hdataG[pixel.rgbtGreen+1]=hdataG[pixel.rgbtGreen+1]+1;
			hdataB[pixel.rgbtBlue+1]=hdataB[pixel.rgbtBlue+1]+1;
		 }
		}
	}

	max=1;
	maxR=1;maxG=1;maxB=1;
	for (i=0; i < 256; i++) {
	  if (doc->dameModo()==8) {
		 if (hdata[i]>max) {
			max=hdata[i];
		}
	  } else {
		if (hdataR[i]>maxR) {
		   maxR=hdataR[i];
		}
		if (hdataG[i]>maxG) {
		   maxG=hdataG[i];
		}
		if (hdataB[i]>maxB) {
		   maxB=hdataB[i];
		}
	  }
	}

	for (i = 0; i < 256; i++) {
	if (doc->dameModo()==8) {
		hdata100[i]=(hdata[i]*100)/max;
	} else {
		hdata100R[i]=(hdataR[i]*100)/maxR;
		hdata100G[i]=(hdataG[i]*100)/maxG;
		hdata100B[i]=(hdataB[i]*100)/maxB;
	}
	}

}

__fastcall Thisto_win::dibujar()
{
	int i;

	imagen->Canvas->Brush->Color=clWhite;
	imagen->Canvas->Pen->Color=clWhite;
	imagen->Canvas->Rectangle(0,0,256,105);

	imagen->Canvas->Pen->Color=clBlack;
	imagen->Canvas->Pen->Width=1;
	imagen->Canvas->MoveTo(0,102);
	imagen->Canvas->LineTo(256,102);
	imagen->Canvas->Pen->Width=1;
	for (i = 0; i < 52; i++) {
		imagen->Canvas->MoveTo(i*5,102);
		imagen->Canvas->LineTo(i*5,105);
	}

	for (i = 0; i < 256; i++) {
	  if (doc->dameModo()==8){
		imagen->Canvas->MoveTo(i,100);
		imagen->Canvas->LineTo(i,100-hdata100[i]);
	  } else {
		  imagen->Canvas->Pixels[i][100-hdata100R[i]]=clRed;
		  imagen->Canvas->Pixels[i][100-hdata100G[i]]=clGreen;
		  imagen->Canvas->Pixels[i][100-hdata100B[i]]=clBlue;
      }

	}
}
//---------------------------------------------------------------------------
void __fastcall Thisto_win::imagenMouseMove(TObject *Sender, TShiftState Shift, int X,
          int Y)
{
	Label1->Caption="Intensidad: "+IntToStr(X);
	if (doc->dameModo()==8) {
		Label2->Caption="Cantidad: "+IntToStr(hdata100[X])+"% "+IntToStr(hdata[X])+" px";
	} else {
		Label2->Caption="Cantidad: ["+IntToStr(hdataR[X])+"] ["+IntToStr(hdataG[X])+"] ["+IntToStr(hdataB[X])+"]";
	}


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

int __fastcall Thisto_win::dameValor(int posicion){
	return hdata[posicion];
}

int __fastcall Thisto_win::dameValorR(int posicion){
   return hdataR[posicion];
}

int __fastcall Thisto_win::dameValorG(int posicion){
   return hdataG[posicion];
}

int __fastcall Thisto_win::dameValorB(int posicion){
   return hdataB[posicion];
}
