-
Notifications
You must be signed in to change notification settings - Fork 11
/
mcweightgraph.c
578 lines (487 loc) · 17.1 KB
/
mcweightgraph.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
Copyright ESIEE (2009)
m.couprie@esiee.fr
This software is an image processing library whose purpose is to be
used primarily for research and teaching.
This software is governed by the CeCILL license under French law and
abiding by the rules of distribution of free software. You can use,
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL
"http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty and the software's author, the holder of the
economic rights, and the successive licensors have only limited
liability.
In this respect, the user's attention is drawn to the risks associated
with loading, using, modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean that it is complicated to manipulate, and that also
therefore means that it is reserved for developers and experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or
data to be ensured and, more generally, to use and operate it in the
same conditions as regards security.
The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
*/
/* authors : J. Cousty - L. Najman and M. Couprie */
/* $Id: mcgraphe.c,v 1.4 2006/02/28 07:49:16 michel Exp $ */
/*
Librairie mcgraphe :
Gestion de graphes de regions
Michel Couprie, aout 1997
Principe du codage :
- a chaque sommet (region) est associe une liste de codes d'aretes
- les regions "atomiques" sont les pixels de l'image originale
- dans une image a N pixels, pour un pixel i, on code par:
i l'arete liant le pixel i a son voisin est i+1,
N+i l'arete liant le pixel i a son voisin sud,
et de plus, pour le 8-voisinage:
2N+i l'arete liant le pixel i a son voisin sud-est,
3N+i l'arete liant le pixel i a son voisin sud-ouest.
- les regions sont obtenues par fusion de regions (voir mcfus).
*/
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <mcsort.h>
#include <mcweightgraph.h>
#define DEBUGGRAPHE
/* ================================================ */
/* ================================================ */
/* fonctions privees */
/* ================================================ */
/* ================================================ */
/* ========================================== */
static void erreur(char * mess)
/* ========================================== */
{
fprintf(stderr, "%s\n", mess);
exit(0);
} /* erreur() */
/*#ifdef DEBUGGRAPHE
/* ====================================================================== */
void printliste(pcell p)
/* ====================================================================== */
{
for (; p != NULL; p = p->next)
printf("%d", p->index);
printf("\n");
} /* printliste() */
void printindex(graphe * g)
/* ====================================================================== */
{
uint32_t i;
pcell p;
for (i = 0; i < g->nbsom; i++)
{
printf("[%d] ", i);
p = *(g->listar+i);
for (; p != NULL; p = p->next){
printf("%d ", p->index);
printf("w=%d :", g->weight[p->index]);
}
printf("\n");
}
printf("\n");
} /* printindex() */
/* ====================================================================== */
void printgraphe(graphe * g)
/* ====================================================================== */
{
uint32_t i;
printf("[#id_vertex]: list of adjacent vertex \n", i);
for (i = 0; i < g->nbsom; i++)
{
printf("[%d]: ", i);
printliste(*(g->listar+i));
}
printf("\n");
printf("[#id_edge]: vertex conforming the edge \n", i);
for (i = 0; i < g->ind; i++)
{
printf("[%d] ", i);
printf("%d ", g->tete[i]);
printf("%d ", g->queue[i]);
printf("\n");
}
} /* printgraphe() */
/*#endif
/* ================================================ */
/* ================================================ */
/* fonctions publiques */
/* ================================================ */
/* ================================================ */
/* ====================================================================== */
graphe * initgraphe(uint32_t nbsom, uint32_t nbmaxar)
/* ====================================================================== */
{
graphe * g;
uint32_t i;
g = (graphe *)calloc(1,sizeof(graphe));
g->cs = -1;
g->rs = -1;
if (g == NULL)
{ fprintf(stderr, "initgraph : malloc failed for g\n");
return(0);
}
g->tasar = (cell *)calloc(1,nbmaxar * sizeof(cell));
if (g->tasar == NULL)
{ fprintf(stderr, "initgraph : malloc failed for g->tasar\n");
return(0);
}
g->listar = (pcell *)calloc(nbsom, sizeof(pcell));
if (g->listar == NULL)
{ fprintf(stderr, "initgraph : calloc failed for g->listar\n");
return(0);
}
g->weight = (double *)calloc(1,nbmaxar * sizeof(double));
g->nbsom = nbsom;
g->nbmaxar = nbmaxar;
g->nbar = 0;
g->ind = 0;
for (i = 0; i < nbmaxar - 1; i++)
(g->tasar+i)->next = g->tasar+i+1;
(g->tasar+i)->next = NULL;
g->librear = g->tasar;
g->tete = (uint32_t*)calloc(g->nbmaxar, sizeof(uint32_t));
if(g->tete == NULL){
fprintf(stderr,"initgrap: calloc failed for g->tete\n");
exit(1);
}
g->queue = (uint32_t*)calloc(g->nbmaxar, sizeof(uint32_t));
if(g->queue == NULL){
fprintf(stderr,"initgrap: calloc failed for g->queue\n");
exit(1);
}
return g;
} /* initgraphe() */
/* ====================================================================== */
graphe * sjinitgraphe(uint32_t nbsom, uint64_t nbmaxar)
/* ====================================================================== */
{
graphe * g;
uint32_t i;
g = (graphe *)calloc(1,sizeof(graphe));
g->cs = -1;
g->rs = -1;
if (g == NULL)
{ fprintf(stderr, "initgraph : malloc failed for g\n");
return(0);
}
g->tasar = (cell *)calloc(1,nbmaxar * sizeof(cell));
if (g->tasar == NULL)
{ fprintf(stderr, "initgraph : malloc failed for g->tasar\n");
return(0);
}
g->listar = (pcell *)calloc(nbsom, sizeof(pcell));
if (g->listar == NULL)
{ fprintf(stderr, "initgraph : calloc failed for g->listar\n");
return(0);
}
g->weight = (double *)calloc(1,nbmaxar * sizeof(double));
g->nbsom = nbsom;
g->nbmaxar = nbmaxar;
g->nbar = 0;
g->ind = 0;
for (i = 0; i < nbmaxar - 1; i++)
(g->tasar+i)->next = g->tasar+i+1;
(g->tasar+i)->next = NULL;
g->librear = g->tasar;
g->tete = (uint32_t*)calloc(g->nbmaxar, sizeof(uint32_t));
if(g->tete == NULL){
fprintf(stderr,"initgrap: calloc failed for g->tete\n");
exit(1);
}
g->queue = (uint32_t*)calloc(g->nbmaxar, sizeof(uint32_t));
if(g->queue == NULL){
fprintf(stderr,"initgrap: calloc failed for g->queue\n");
exit(1);
}
return g;
} /* initgraphe() */
void setSize(graphe *g, int32_t rs , int32_t cs)
{
g->rs = rs;
g->cs = cs;
}
/* ====================================================================== */
void terminegraphe(graphe * g)
/* ====================================================================== */
{
free(g->tasar);
free(g->listar);
free(g->weight);
free(g->tete);
free(g->queue);
free(g);
} /* terminegraphe() */
/* ====================================================================== */
pcell allouecell(graphe * g)
/* ====================================================================== */
{
pcell p;
if (g->librear == NULL) erreur("allouecell : plus de cellules libres");
p = g->librear;
g->librear = g->librear->next;
return p;
} /* allouecell() */
/* ====================================================================== */
void liberecell(graphe * g, pcell p)
/* ====================================================================== */
{
p->next = g->librear;
g->librear = p;
} /* liberecell() */
/* ====================================================================== */
void retiretete(graphe * g, pcell * pliste)
/* ====================================================================== */
{
pcell p;
p = (*pliste)->next;
liberecell(g, *pliste);
*pliste = p;
} /* retiretete() */
/* /\* ====================================================================== *\/ */
/* int32_t estarete(graphe * g, uint32_t som, uint32_t a) */
/* /\* ====================================================================== *\/ */
/* { */
/* pcell liste; */
/* liste = *(g->listar + som); */
/* while (liste != NULL) */
/* if (liste->val == a) return 1; */
/* else liste = liste->next; */
/* return 0; */
/* } /\* estarete() *\/ */
/* /\* ====================================================================== *\/ */
/* void retirearete(graphe * g, uint32_t som, uint32_t a) */
/* /\* ====================================================================== *\/ */
/* { */
/* pcell * pliste; */
/* pliste = g->listar + som; */
/* while ((*pliste != NULL) && (((*pliste)->val) != a)) */
/* pliste = &((*pliste)->next); */
/* if (*pliste != NULL) retiretete(g, pliste); */
/* g->nbar--; */
/* } /\* retirearete() *\/ */
/* /\* ====================================================================== *\/ */
/* void ajoutearete(graphe * g, uint32_t som, uint32_t a) */
/* /\* ====================================================================== *\/ */
/* { */
/* pcell p; */
/* p = allouecell(g); */
/* p->next = *(g->listar + som); */
/* p->val = a; */
/* *(g->listar + som) = p; */
/* g->nbar++; */
/* } /\* ajoutearete() *\/ */
/* ====================================================================== */
void ajoutearete2(graphe * g, uint32_t som, uint32_t a, uint32_t ind)
/* ====================================================================== */
{
pcell p;
p = allouecell(g);
p->next = *(g->listar + som);
// p->val = a;
p->index = ind;
*(g->listar + som) = p;
g->nbar++;
} /* ajoutearete() */
/* ====================================================================== */
void addarete(graphe * g, uint32_t som, uint32_t a, double weight)
/* ====================================================================== */
{
uint32_t i;
i = g->ind;
ajoutearete2(g, som, a,i);
/* indexarete(g, som, a, i); */
ajoutearete2(g, a, som,i);
/* indexarete(g, a, som, i); */
g->tete[i] = a;
g->queue[i] = som;
setweight(g, i, weight);
g->ind= i+1;
} /* addarete() */
/* ====================================================================== */
void setweight(graphe * g, uint32_t index, double value)
/* ====================================================================== */
{
g->weight[index]= value;
} /* setweight */
/* ====================================================================== */
double getweight(graphe * g, uint32_t index)
/* ====================================================================== */
{
return g->weight[index];
} /* setweight */
/* /\* ====================================================================== *\/ */
/* void maille4graphe(graphe * g, uint32_t rs, uint32_t cs) */
/* /\* ====================================================================== *\/ */
/* { */
/* uint32_t i, j; */
/* uint32_t N = rs * cs; */
/* for (j = 0; j < cs; j++) */
/* for (i = 0; i < rs; i++) */
/* { */
/* if (i < rs - 1) */
/* { */
/* ajoutearete(g, j * rs + i, j * rs + i); */
/* ajoutearete(g, j * rs + i + 1, j * rs + i); */
/* } */
/* if (j < cs - 1) */
/* { */
/* ajoutearete(g, j * rs + i, j * rs + i + N); */
/* ajoutearete(g, (j+1) * rs + i, j * rs + i + N); */
/* } */
/* } */
/* } /\* maille4graphe() *\/ */
/* ====================================================================== */
void aretesommets(uint32_t a, uint32_t N, uint32_t rs, uint32_t * s1, uint32_t * s2)
/* ====================================================================== */
{
if (a < N) { *s1 = a; *s2 = a + 1; }
else if (a < 2 * N) { *s1 = a - N; *s2 = a - N + rs; }
else if (a < 3 * N) { *s1 = a - 2*N; *s2 = a - 2*N + rs + 1; }
else { *s1 = a - 3*N; *s2 = a - 3*N + rs - 1; }
} /* aretesommets() */
/* ================================================ */
/* ================================================ */
/* fonctions publiques pour la vision 'liste d'adjacence' */
/* (a chaque sommet est associee la liste des sommets adjacents) */
/* ================================================ */
/* ================================================ */
/* /\* ====================================================================== *\/ */
/* int32_t estsuccesseur(graphe * g, uint32_t som, uint32_t a) */
/* /\* ====================================================================== *\/ */
/* { /\* idem estarete, mais c'est la vision 'liste d'adjacence' *\/ */
/* return estarete(g, som, a); */
/* } /\* estsuccesseur() *\/ */
/* /\* ====================================================================== *\/ */
/* int32_t estsymetrique(graphe * g) */
/* /\* ====================================================================== *\/ */
/* { */
/* uint32_t i, j; */
/* for (i = 0; i < g->nbsom - 1; i++) */
/* for (j = i+1; j < g->nbsom; j++) */
/* { */
/* if (estsuccesseur(g, i, j)) */
/* { */
/* if (!estsuccesseur(g, j, i)) return 0; */
/* } */
/* else */
/* { */
/* if (estsuccesseur(g, j, i)) return 0; */
/* } */
/* } */
/* return 1; */
/* } /\* estsymetrique() *\/ */
/* ====================================================================== */
/* ====================================================================== */
/* FONCTIONS D'ENTREE / SORTIE FICHIER POUR UN GRAPHE */
/* ====================================================================== */
/* ====================================================================== */
/* ====================================================================== */
/*! \fn graphe * ReadGraphe(char * filename)
\param filename (entr�) : nom du fichier graphe.
\return un graphe.
\brief Lit les donn�s d'un graphe dans le fichier filename, retourne un pointeur sur la structure graphe construite.
*/
graphe * ReadGraphe(char * filename,double **Fv)
/* ====================================================================== */
#undef F_NAME
#define F_NAME "ReadGraphe"
{
#define TAILLEBUF 4096
graphe * g;
int i, n, m, t, q, rs = -1, cs = -1;
char buf[TAILLEBUF];
char *ret;
FILE *fd = NULL;
fd = fopen(filename,"rb");
if (!fd)
{
fprintf(stderr, "%s: file not found: %s\n", filename, F_NAME);
return NULL;
}
ret = fgets(buf, TAILLEBUF, fd);
if( sscanf(buf,"#rs %d cs %d\n", &rs, &cs) == 2)
ret = fgets(buf, TAILLEBUF, fd);
sscanf(buf, "%d %d\n", &n, &m);
g = initgraphe(n, 2*m);
setSize(g,rs,cs);
double *F = (double *)calloc(1,n * sizeof(double));
do
{
ret = fgets(buf, TAILLEBUF, fd);
if ((ret != NULL) && (strncmp(buf, "val sommets", 11) == 0))
{
double v;
for (i = 0; i < n; i++)
{
fscanf(fd, "%d %lf\n", &t, &v);
F[i] = v;
}
} /* if ((ret != NULL) && (strncmp(buf, "val sommets", 11) == 0)) */
else if ((ret != NULL) && (strncmp(buf, "arcs values", 11) == 0))
{
double e;
for (i = 0; i < m; i++)
{
fscanf(fd, "%d %d %lf\n", &t, &q, &e);
addarete(g , t, q, e);
}
} /* if ((ret != NULL) && (strncmp(buf, "arcs values", 11) == 0)) */
} while (ret != NULL);
//for(i=0;i<20;i++)printf("BAD FILTER MODE %d %g\n",i,F[i]);
(*Fv)=F;
fclose(fd);
return g;
} /* ReadGraphe() */
/* ====================================================================== */
/*! \fn void SaveGraphe(graphe * g, char *filename)
\param g (entr�) : un graphe.
\param filename (entr�) : nom du fichier �g��er.
\brief sauve le graphe g dans le fichier filename.
*/
void SaveGraphe(graphe * g, char *filename, double *Fv )
/* ====================================================================== */
#undef F_NAME
#define F_NAME "SaveGraphe"
{
int i, j, n = g->nbsom, m = g->ind,a,b;
pcell p;
FILE * fd = NULL;
double v;
fd = fopen(filename,"wb");
if (!fd)
{
fprintf(stderr, "%s: cannot open file: %s\n", F_NAME, filename);
return;
}
if( (g->cs != -1) && (g->rs != -1))
fprintf(fd,"#rs %d cs %d\n",g->rs, g->cs);
fprintf(fd, "%d %d\n", n, m);
fprintf(fd, "val sommets\n");
for (i = 0; i < n; i++)
fprintf(fd, "%d %f\n", i, Fv[i]);
fprintf(fd, "arcs values\n");
for (i = 0; i < m; i++)
{
a = g->tete[i];
b = g->queue[i];
v = getweight (g,i);
fprintf(fd, "%d %d %f\n", a, b, v);
}
fclose(fd);
} /* SaveGraphe() */
/*uint32_t voisin(graphe *g, uint32_t x, uint32_t u)
// returns the vertex of g linked to x by edge u
{
if(g->tete[u] != x)
return g->tete[u];
else return g->queue[u];
}
*/