summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ovision/ui/liveView.cc
blob: 6aef4e15dd4ecd5dd5d4f0a6b038b3553260740e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <GL/glut.h>    // Header File For The GLUT Library
#include <GL/gl.h>      // Header File For The OpenGL32 Library
#include <GL/glu.h>     // Header File For The GLu32 Library
#include <iostream>  

#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <vector>
#include <string>

#include "adjust.hh"
#include "live.hh"

using namespace std;

#define IMG_WIDTH 	360
#define IMG_HEIGHT 	296
#define WIDTH  718
#define HEIGHT 592
int window;
Live *live;
int width=538; int height=395;
static const unsigned nbImg = 4;
std::vector<std::string> *fileList;
Live::ImageInput ii;
unsigned iFileList;
unsigned nnNbColor;
Image::PixelFormat pf;
int colorToFind;

/// Chargement d'une texture a partir de donnees RGB
unsigned int 
loadImage(int width, int height, unsigned char *data, unsigned int lastTex) 
{
    GLuint tex;
    if (glIsTexture(lastTex)) glDeleteTextures(1, &lastTex);
    // Mode de chargement des donnees
    glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    // Creer une id pour stocker l'image
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    // Stocke l'image
    //glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
    // Config des parametres
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    // Retourne l'id de l'image
    return tex;
}

/// Initialisation des fonctions OpenGL
void 
initGL(int width, int height)	       
{
    // Initialisation du mode d'affichage
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);	
    // Mode de chargement des donnees
    glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    glShadeModel(GL_FLAT);	
    glEnable(GL_TEXTURE_2D);
    // Remise a zero de la matrice de projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();		
    // Plan de projection en 2D
    glOrtho(0.0, WIDTH, 0.0, HEIGHT, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
}



/// Redimenssionement d'une fenetre
void
reSizeGLScene(int w, int h)
{
    // Evite div par 0
    if (height==0) h=1;
    height = h;
    width = w;
    // Initialise le point de vue  
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    // Remise a zero et mode 2D
    glLoadIdentity();
    glOrtho(0.0, WIDTH, 0.0, HEIGHT, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
}


/// Affiche une image
/// @param texId numero de la texture a afficher
void
drawImage(GLuint texId) 
{
    int wMin, wMax;
    int hMin, hMax;
    // Selection de l'image
    glBindTexture(GL_TEXTURE_2D, live->tex[texId]);
    // Calcul des coordonnees de l'image
    wMin = BORDER+(texId%3)*(352+BORDER);
    wMax = 352+BORDER+(texId%3)*(352+BORDER);
    hMin = HEIGHT-288-BORDER-(int)(texId/3)*(288+BORDER);
    hMax = HEIGHT-BORDER-(int)(texId/3)*(288+BORDER);
    // Dessine un carre contenant l'image	
    glBegin(GL_QUADS);
    	glNormal3f( 0.0f, 0.0f, 1.0f);
	glColor3f(1.0f,1.0f,1.0f);
	glTexCoord2f(0.0f, 0.0f); glVertex3f(wMax, hMax, -1.0f);
	glTexCoord2f(1.0f, 0.0f); glVertex3f(wMin, hMax, -1.0f);
	glTexCoord2f(1.0f, 1.0f); glVertex3f(wMin, hMin, -1.0f);
	glTexCoord2f(0.0f, 1.0f); glVertex3f(wMax, hMin, -1.0f);
    glEnd();
}


/// Dessine les images a l'ecran
void 
drawGLScene()
{
    // Efface le buffer
    glClear(GL_COLOR_BUFFER_BIT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    // Affiche toutes les images
    for (unsigned i=0; i<nbImg; i++)
	drawImage(i);
    glFlush();
    // Affiche le buffer suivant
    glutSwapBuffers();
}


/// Interruptions du clavier
void 
keyPressed(unsigned char key, int x, int y) 
{
    // Touche Echap
    if (key == 27) 
      {
	glutDestroyWindow(window); 
	exit (0);                  
      }
    else if (key == ' ')
      {
	if (iFileList < (fileList->size ()-1))
	  {
	    ++iFileList;
	    live->updateImg ((*fileList)[iFileList].c_str (), pf);
	  }
      }
    else if (key == 8)
      {
	if (iFileList > 0)
	  {
	    --iFileList;
	    live->updateImg ((*fileList)[iFileList].c_str (), pf);
	  }
      }
    else if (key == 'b')
      {
	if (colorToFind > -1)
	  {
	    --colorToFind;
	    live->setColorToFind (colorToFind);
	    live->updateImg ((*fileList)[iFileList].c_str ());
	    live->updateImg ((*fileList)[iFileList].c_str ());
	    std::cout << "Couleur s�lectionn�e " << colorToFind << std::endl;
	  }
      }
    else if (key == 'n')
      {
	if (live->oconfig->nnNbColor > colorToFind)
	  {
	    ++colorToFind;
	    live->setColorToFind (colorToFind);
	    live->updateImg ((*fileList)[iFileList].c_str ());
	    std::cout << "Couleur s�lectionn�e " << colorToFind << std::endl;
	  }
      }
    else if (key == 'r')
	live->reloadConfig ("rc/vision.conf");
    else if (key == 'm')
	std::cout << "Nombre de couleurs du prochain r�seau de neurones : " << ++nnNbColor << std::endl;
    else if (key == 'l')
	std::cout << "Nombre de couleurs du prochain r�seau de neurones : " << --nnNbColor << std::endl;
    else if (key == 13)
      {
	live->segm->setMode (pf);
	live->segm->buildNN (nnNbColor, Segm::generate);
	live->segm->trainNN (live->img);
	live->updateImg ((*fileList)[iFileList].c_str ());
      }
    drawGLScene ();
}

/// Gestion souris
void
mouseFunc(int button, int state, int x, int y)
{
}

/// Usage
void
usage ()
{
    std::cerr << "Usage : ./liveView {file, cam, socket} format_image [liste d'images]" << std::endl;
    exit (1);
}


/// Analyse la ligne de commande
void
parseCommandLine (int argc, char **argv, Live::ImageInput &ii, std::vector<std::string> &list)
{
    if (argc < 3)
	usage ();
    std::string str (argv[1]);
    if (str == "file") ii = Live::file;
    else if (str == "cam") ii = Live::cam;
    else if (str == "socket") ii = Live::socket;
    else usage ();
    if ((argc < 4) && (ii == Live::file))
	usage ();
    std::string str2 (argv[2]);
    if (str2 == "rgb") pf = Image::rgb;
    else if (str2 == "yuv") pf = Image::yuv;
    else usage ();
    while (--argc > 2)
      {
	std::string *str = new std::string (argv[argc]);
	list.push_back (*str);
	std::cout << *str << std::endl;
      }
}

/// Fonction principale
int
main(int argc, char **argv) 
{
    fileList = new std::vector<std::string>;
    parseCommandLine (argc, argv, ii, *fileList);
    // Initialisation de l'OpenGL    
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);  
    glutInitWindowSize(width, height);  
    glutInitWindowPosition(0, 0);  
    window = glutCreateWindow("liveView - APBteam");  
    glutDisplayFunc(&drawGLScene);  
    glutReshapeFunc(&reSizeGLScene);
    glutKeyboardFunc(&keyPressed);
    glutMouseFunc(&mouseFunc);
    initGL(WIDTH, HEIGHT);

    iFileList = 0;
    nnNbColor = 6;
    colorToFind = -1;

    live = new Live (nbImg, ii);
    live->init ((*fileList)[0].c_str (), pf);

    // Boucle principale  
    glutMainLoop(); 

    delete live;
    delete fileList;
    return 1;
}