summaryrefslogtreecommitdiff
path: root/2003/i/buzz/src/vision/image.cc
blob: f868587709c83c20fc75e079ecba56fc60fb5f5a (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// image.cc - Classe Image
// buzz - Programme du robot Efrei Robotique I1-I2 2003
// Copyright (C) 2003 Nicolas Schodet
#include "image.h"
#include "rgbyuv.h"

// A definir si on code en YUV.
//#define USE_YUV

extern "C" {
#include <ppm.h>
};

// Attention, l'un des param�tres est evalu� deux fois.
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))

// Constructeur.
Image::Image (const char *filename, Thresholds *thresholds,
	      SizeThresholds *sizeThresholds)
{
    pixel **img;
    pixel *p;
    pixval maxval;
    int x, y, w, h;
#ifdef USE_YUV
    int r, g, b, Y, U, V;
#endif // USE_YUV
    FILE *fp;
    unsigned char *pi;
    // Open the file.
    fp = fopen (filename, "r");
    if (!fp) throw "Image::Image: fopen failled";
    // Read it.
    img = ppm_readppm (fp, &w, &h, &maxval);
    // Close it.
    fclose (fp);
    if (!img) throw "Image::Image: ppm_readppm failled";
    // Allocate memory...
    m_image = new unsigned char [w*h*3];
    m_width = w;
    m_height = h;
    // Extrait les info RGB ou YUV.
    pi = m_image;
    for (y = 0; y < h; y++)
      {
	p = img[y];
	for (x = 0; x < w; x++)
	  {
#ifdef USE_YUV
	    r = PPM_GETR (*p) * 255 / maxval;
	    g = PPM_GETG (*p) * 255 / maxval;
	    b = PPM_GETB (*p) * 255 / maxval;
	    RgbYuv::rgbToYuv (r, g, b, Y, U, V);
	    *pi++ = (unsigned char) Y;
	    *pi++ = (unsigned char) U;
	    *pi++ = (unsigned char) V;
#else // ! USE_YUV
	    *pi++ = (unsigned char) (PPM_GETR (*p) * 255 / maxval);
	    *pi++ = (unsigned char) (PPM_GETG (*p) * 255 / maxval);
	    *pi++ = (unsigned char) (PPM_GETB (*p) * 255 / maxval);
#endif // ! USE_YUV
	    p++;
	  }
      }
    // Alloue de la memoire pour les zones.
    m_zones = new unsigned char [m_width * m_height];
    // Initalisation
    m_groups = 0;
    m_thresholds = thresholds;
    m_sizeThresholds = sizeThresholds;
}

// Destructeur
Image::~Image (void)
{
    ImageGroup *g, *g2;
    if (m_image) delete [] m_image;
    if (m_zones) delete [] m_zones;
    for (g = m_groups; g; g = g2)
      {
	g2 = g->next;
	delete g;
      }
}

// Filtre l'image pour trouver les zones.
void Image::filter (void)
{
    int i;
    unsigned char *pz, *pi;
    unsigned char r, g, b; // ou y, u, v, c'est le m�me traitement.
    // Filtre.
    pz = m_zones;
    pi = m_image;
    for (i = 0; i < m_width * m_height; i++)
      {
	r = *pi++;
	g = *pi++;
	b = *pi++;
	*pz++ = m_thresholds->findZone (r, g, b);
      }
}

// Group les groupes de pixels.
void Image::group (void)
{
    // c: group courant, n: group suivant (chercheur).
    ImageGroup *c, **n, *n2;
    // Supprime tous les groupes.
    for (c = m_groups; c; c = n2)
      {
	n2 = c->next;
	delete c;
      }
    // Fait des groupes de pixels.
    groupLine ();
    for (c = m_groups; c; c = c->next)
      {
	n = &c->next;
	// Recherche des groups plus bas dans l'image qui sont colle au groupe
	// courant.
	while (*n && (*n)->y <= c->y + c->h)
	  {
	    // Si ils sont de la m�me zone et qu'ils se touchent (avec au
	    // moins 5 pixels), on les rassemble en 1 groupe.
	    if (c->zone == (*n)->zone
		&& (*n)->x < c->x + c->w - 5
		&& c->x < (*n)->x + (*n)->w - 5)
	      {
		// Dechainage de n.
		n2 = (*n);
		*n = n2->next;
		// Mise a jour de c.
		c->h++;
		c->w = max (c->x + c->w, n2->x + n2->w) - min (c->x, n2->x);
		c->x = min (c->x, n2->x);
		// Supression de n.
		delete n2;
	      }
	    else
		n = &(*n)->next;
	  }
      }
}

// Fait des packets de pixels sur les lignes.
void Image::groupLine (void)
{
    unsigned char zone;
    int n, y, x, sx;
    ImageGroup **p;
    unsigned char *pz;
    pz = m_zones;
    p = &m_groups;
    for (y = 0; y < m_height; y++)
      {
	// Pour chaque ligne.
	n = 0;
	zone = 0;
	for (x = 0; x < m_width; x++)
	  {
	    if (n == 0)
	      {
		// Nouvelle zone.
		sx = x;
		zone = *pz;
		n = 1;
	      }
	    else
	      {
		if (zone == *pz)
		  {
		    // M�me zone.
		    n++;
		  }
		else
		  {
		    // Zone differente.
		    if (zone && n > 5)
		      {
			*p = new ImageGroup;
			(*p)->x = sx;
			(*p)->y = y;
			(*p)->w = n;
			(*p)->h = 1;
			(*p)->zone = zone;
			(*p)->type = 0;
			p = &(*p)->next;
		      }
		    n = 0;
		    zone = *pz;
		  }
	      }
	    pz++;
	  }
	// Dernier groupe, si assez grand.
	if (zone && n > 5)
	  {
	    *p = new ImageGroup;
	    (*p)->x = sx;
	    (*p)->y = y;
	    (*p)->w = n;
	    (*p)->h = 1;
	    (*p)->zone = zone;
	    (*p)->type = 0;
	    p = &(*p)->next;
	  }
      }
    *p = 0;
}

// Affiche les groupes qui on �t� trouv�s.
void
Image::dumpGroups (void)
{
    ImageGroup *g;
    printf ("Groups\n");
    for (g = m_groups; g; g = g->next)
      {
	printf ("x: %d, y: %d, w: %d, h: %d, z: %d, t: %d\n", g->x, g->y,
		g->w, g->h, g->zone, g->type);
      }
}

// Filtre les packets de pixels.
void
Image::groupFilter (void)
{
    ImageGroup **g, *g2;
    g = &m_groups;
    while (*g)
      {
	g2 = *g;
	// Trouve le type de palet.
	g2->type = m_sizeThresholds->findType (g2->w, g2->h);
	// Vire les groupes qu'on ne voix pas en entier ou d'une taille bizare.
	if (g2->type == 0
	    || g2->x <= 0 || g2->x + g2->w >= m_width
	    || g2->y <= 0 || g2->y + g2->h >= m_height)
	  {
	    *g = g2->next;
	    delete g2;
	  }
	else
	  {
	    g = &g2->next;
	  }
      }
}

// Enregistre les infos trouv�es.
void
Image::dump (const char *filename)
{
    pixel *row;
    pixel *pr;
    int x, y;
    FILE *fp;
    unsigned char *p;
    int r, g, b;
    ImageGroup *pg;
    // Open the file.
    fp = fopen (filename, "w");
    if (!fp) throw "Image::dump: fopen failled";
    // Allocate memory...
    row = ppm_allocrow (m_width);
    // Sauve l'image.
#ifdef USE_YUV
    ppm_writeppminit (fp, m_width, m_height * 4, 255, 0);
#else // ! USE_YUV
    ppm_writeppminit (fp, m_width, m_height * 3, 255, 0);
#endif // ! USE_YUV
    // image.
    p = m_image;
    for (y = 0; y < m_height; y++)
      {
	pr = row;
	for (x = 0; x < m_width; x++)
	  {
	    r = *p++;
	    g = *p++;
	    b = *p++;
	    PPM_ASSIGN (*pr, r, g, b);
	    pr++;
	  }
	ppm_writeppmrow (fp, row, m_width, 255, 0);
      }
#ifdef USE_YUV
    // Image en couleurs RGB
    p = m_image;
    for (y = 0; y < m_height; y++)
      {
	pr = row;
	for (x = 0; x < m_width; x++)
	  {
	    // Lit les YUV.
	    r = *p++;
	    g = *p++;
	    b = *p++;
	    // Convertit.
	    RgbYuv::yuvToRgb (r, g, b, r, g, b);
	    PPM_ASSIGN (*pr, r, g, b);
	    pr++;
	  }
	ppm_writeppmrow (fp, row, m_width, 255, 0);
      }
#endif // USE_YUV
    // Sauve les zones.
    p = m_zones;
    for (y = 0; y < m_height; y++)
      {
	pr = row;
	for (x = 0; x < m_width; x++)
	  {
	    r = *p == 1 ? 255 : 0;
	    g = *p == 2 ? 255 : 0;
	    b = 0;
	    p++;
	    PPM_ASSIGN (*pr, r, g, b);
	    pr++;
	  }
	ppm_writeppmrow (fp, row, m_width, 255, 0);
      }
    // Sauve les groupes.
    for (y = 0; y < m_height; y++)
      {
	pr = row;
	for (x = 0; x < m_width; x++)
	  {
	    r = g = b = 0;
	    for (pg = m_groups; pg; pg = pg->next)
	      {
		if (x >= pg->x && x < pg->x + pg->w
		    && y >= pg->y && y < pg->y + pg->h)
		  {
		    if (pg->zone == 1)
			r = 255;
		    else if (pg->zone == 2)
			g = 255;
		    else
			b = 255;
		  }
	      }
	    PPM_ASSIGN (*pr, r, g, b);
	    pr++;
	  }
	ppm_writeppmrow (fp, row, m_width, 255, 0);
      }
    // Free memory.
    ppm_freerow (row);
    // Close it.
    fclose (fp);
}