#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <cv.h>
#include <highgui.h>

using namespace std;
#define TAM_X 242
#define TAM_Y 430
#define MITAD_X 121


typedef struct{
	int val0, val1, val2;
}color;

int main(){

	FILE * fOrigen;
	char buffer[200];
	color col;
	vector <color> pixeles;

	fopen_s(&fOrigen,"Pixeles.txt","r");
	if(fOrigen==NULL){
		printf("Error en el fichero Pixeles.txt\n");
		exit(0);
	}

	int tam_X=242;
	int tam_Y=430;

	
	IplImage * img = cvCreateImage(cvSize(tam_X,tam_Y),8,1);
	IplImage * img2 = cvCreateImage(cvSize(tam_X,tam_Y),8,1);
	IplImage * img3 = cvCreateImage(cvSize(tam_X,tam_Y),8,1);
	cvSetZero(img);
	cvSetZero(img2);
	cvSetZero(img3);
	
		
	int key;
	
	uchar *data    =  (uchar *)img->imageData;
	uchar *data2    =  (uchar *)img2->imageData;
	uchar *data3    =  (uchar *)img3->imageData;
	int step	= img->widthStep;
	int width	= img->width;
	int height	= img->height;

	fgets(buffer,200,fOrigen);

	int dist0_MAX=0;
	int dist1_MAX=0;
	int dist0_MIN=99999999999999;
	int dist1_MIN=99999999999999;
	int dist0;
	int dist1;

	int n_2_lt_0=0;
	int n_2_lt_1=0;
	int n=0;

	//Para separar las ventanas en positivos y negativos para los histogramas
	int v0_pos[MITAD_X];
	int v0_neg[MITAD_X];
	int v1_pos[MITAD_X];
	int v1_neg[MITAD_X];
	int v0_v1_pos[MITAD_X];
	int v0_v1_neg[MITAD_X];

	/*int v0_rel_v2_pos[MITAD_X];
	int v1_rel_v2_pos[MITAD_X];*/


	for(int i=0; i<MITAD_X; i++){
		v0_pos[i]=0;
		v0_neg[i]=0;
		v1_pos[i]=0;
		v1_neg[i]=0;
		v0_v1_pos[i]=0;
		v0_v1_neg[i]=0;
	}

	while( fgets(buffer,200,fOrigen) != NULL ){
		n++;
		sscanf_s(buffer,"%d %d %d ",&col.val0, &col.val1, &col.val2);
		dist0=col.val2-col.val0;
		dist1=col.val2-col.val1;

		if(dist0>dist0_MAX)
			dist0_MAX=dist0;
		if(dist1>dist1_MAX)
			dist1_MAX=dist1;
		if(dist0<dist0_MIN)
			dist0_MIN=dist0;
		if(dist1<dist1_MIN)
			dist1_MIN=dist1;

		if(dist0<0){
			v0_neg[abs(dist0)]++;
			n_2_lt_0++;
		}
		else {
			v0_pos[dist0]++;
		}

		if(dist1<0){
			v1_neg[abs(dist1)]++;
			n_2_lt_1++;
		}
		else {
			v1_pos[dist1]++;
		}

		int dist_v0_v1=col.val0-col.val1;
		if(dist_v0_v1<0)
			v0_v1_neg[abs(dist_v0_v1)]++;
		else{
			v0_v1_pos[abs(dist_v0_v1)]++;
		}

		pixeles.push_back(col);
	}

	int mitad=width/2;
	for(int i=0; i<mitad;i+=2){
		for(int j=0; j < (v0_neg[121-i]/7); j++){
			data[((tam_Y-1)-j)*step+i] = 255;
			data[((tam_Y-1)-j)*step+i+1] = 255;
		}
		for(int j=0; j < (v0_pos[i]/7); j++){
			data[((tam_Y-1)-j)*step+i+mitad] = 128;
			data[((tam_Y-1)-j)*step+i+mitad+1] = 128;

		}

		for(int j=0; j < (v1_neg[121-i]/15); j++){
			data2[(height-j)*step+i] = 255;
			data2[(height-j)*step+i+1] = 255;
		}
		for(int j=0; j < (v1_pos[i]/15); j++){
			data2[(height-j)*step+i+mitad] = 128;
			data2[(height-j)*step+i+mitad+1] = 128;

		}

		for(int j=0; j < (v0_v1_neg[MITAD_X-i]/15); j++){
			data3[(height-j)*step+i] = 255;
			data3[(height-j)*step+i+1] = 255;
		}
		for(int j=0; j < (v0_v1_pos[i]/15); j++){
			data3[(height-j)*step+i+mitad] = 128;
			data3[(height-j)*step+i+mitad+1] = 128;

		}


	}



	cvNamedWindow( "Rojo_Azul", CV_WINDOW_AUTOSIZE );
	cvNamedWindow( "Rojo_Verde", CV_WINDOW_AUTOSIZE );
	cvNamedWindow( "Azul_Verde", CV_WINDOW_AUTOSIZE );

	while(1){
		
		
		cvShowImage("Rojo_Verde",img2);
		cvShowImage("Rojo_Azul",img);
		cvShowImage("Azul_Verde",img3);

		key=(cvWaitKey(10) & 255);

		if(key==27) break;
	}

	return 0;
}