#include "type_image.h"

void Cercle(type_image& Image, float x, float y, float rr, float r, float g, float b)
{
	for (int i = 0; i < Image.Width; i++)
		for (int j = 0; j < Image.Height; j++)
			if ( (i-x)*(i-x)+(j-y)*(j-y) < rr*rr)
			{
				Image(i,j,ROUGE) = r;
				Image(i,j,VERT) = g;
				Image(i,j,BLEU) = b;
			}
}

void Rectangle(type_image& Image, float x, float y, float xx, float yy, float r, float g, float b)
{
	for (int i = 0; i < Image.Width; i++)
		for (int j = 0; j < Image.Height; j++)
			if ( (i > x) && (i < xx) && (j > y) && (j < yy))
			{
				Image(i,j,ROUGE) = r;
				Image(i,j,VERT) = g;
				Image(i,j,BLEU) = b;
			}
}

type_image::type_image(char *)
{
	Height =299;
	Width = 500;

	PtrImage = new float[Width*Height*3];

	for (int i = 0; i < Width*Height*3; i++)
		PtrImage[i] = 1;

	Rectangle(*this, 45, 45, 450, 100, 1, 0, 0);
	Cercle(*this, 250, 250, 50, 0, 0, 1);
	Cercle(*this, 0, 0, 100, 1, 0, 1);
	Cercle(*this, 500, 150, 100, 0, 1, 1);
}

type_image* PtrImg;

#include <GL/glut.h>

void afficher(type_image& Img)
{
	int argc = 0;
	char *argv[2] = { "imageprocess" , NULL };

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(Img.Width, Img.Height);
	glutInitWindowPosition( (glutGet(GLUT_SCREEN_WIDTH) - Img.Width)/ 2.0, (glutGet(GLUT_SCREEN_HEIGHT) - Img.Height)/2.0);
	glutCreateWindow("Martin is hot!");
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, Img.Width, -Img.Height, 0.0);	
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glutIdleFunc(NULL);
	glutDisplayFunc(display);

	PtrImg = &Img;
	glutMainLoop();
}

void display(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
#if 0
	glBegin(GL_POINTS);
	for (int i = 0; i < PtrImg->Width; i++)
		for (int j = 0; j < PtrImg->Height; j++)
		{
			glColor3f((*PtrImg)(i, j, ROUGE), (*PtrImg)(i, j, VERT), (*PtrImg)(i, j, BLEU));
			glVertex2i(i, -j);
		}
	glEnd();

#else

	glBegin(GL_QUADS);
	for (int i = 0; i < PtrImg->Width; i++)
		for (int j = 0; j < PtrImg->Height; j++)
		{
			glColor3f((*PtrImg)(i, j, ROUGE), (*PtrImg)(i, j, VERT), (*PtrImg)(i, j, BLEU));
			glVertex2i(i, -j);
			glVertex2i(i+1, -j);
			glVertex2i(i+1, -j-1);
			glVertex2i(i, -j-1);
		}
	glEnd();
#endif
	glutSwapBuffers();
}
