1 | /* ------------------ */ |
---|
2 | /* --- mca_test.c --- */ |
---|
3 | /* ------------------ */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Copyright (c) 2016 Lionel Lacassagne, LIP6, UPMC, CNRS |
---|
7 | * Init : 2016/03/03 |
---|
8 | */ |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <stdlib.h> |
---|
12 | #include <string.h> |
---|
13 | #include <math.h> |
---|
14 | #include <user_barrier.h> |
---|
15 | |
---|
16 | #ifdef CLI |
---|
17 | #include "nrc_os_config.h" |
---|
18 | #include "nrc.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | #include "util.h" |
---|
24 | #include "ecc_common.h" |
---|
25 | #include "ecc_features.h" |
---|
26 | #include "palette.h" |
---|
27 | #include "bmpNR.h" |
---|
28 | #include "mca_matrix_dist.h" |
---|
29 | #include "mca_rosenfeld.h" |
---|
30 | |
---|
31 | |
---|
32 | /* -- local -- */ |
---|
33 | #include "mca.h" |
---|
34 | #include "mca_test.h" |
---|
35 | |
---|
36 | #define DEFAULT_NTHREADS 4 |
---|
37 | #define MAX_THREADS 256 |
---|
38 | |
---|
39 | pthread_t thread_table[MAX_THREADS]; |
---|
40 | |
---|
41 | giet_barrier_t main_barrier; |
---|
42 | |
---|
43 | int num_threads = DEFAULT_NTHREADS; |
---|
44 | |
---|
45 | // -------------------------------------------------------------------------- |
---|
46 | void init_forme_boulon1(uint8 *** X0, int * i0, int * i1, int * j0, int * j1) |
---|
47 | // -------------------------------------------------------------------------- |
---|
48 | { |
---|
49 | uint8 ** X; |
---|
50 | int i = 0; |
---|
51 | int h = 28; |
---|
52 | int w = 30; |
---|
53 | |
---|
54 | X = ui8matrix(0, h - 1, 0, w - 1); |
---|
55 | zero_ui8matrix(X, 0, h - 1, 0, w - 1); |
---|
56 | |
---|
57 | *X0 = X; |
---|
58 | *i0 = 0; |
---|
59 | *i1 = h - 1; |
---|
60 | *j0 = 0; |
---|
61 | *j1 = w - 1; |
---|
62 | |
---|
63 | // 0000000001111111111122222222223 |
---|
64 | // 0123456789012345678901234567890 |
---|
65 | set_ui8vector_str(X[i++], 0, w - 1, " 111 "); // 00 |
---|
66 | set_ui8vector_str(X[i++], 0, w - 1, " 11111 "); // 01 |
---|
67 | set_ui8vector_str(X[i++], 0, w - 1, " 1111111 "); // 02 |
---|
68 | set_ui8vector_str(X[i++], 0, w - 1, " 11111111 "); // 03 |
---|
69 | set_ui8vector_str(X[i++], 0, w - 1, " 1111111111"); // 04 |
---|
70 | set_ui8vector_str(X[i++], 0, w - 1, " 11111111111"); // 05 |
---|
71 | set_ui8vector_str(X[i++], 0, w - 1, " 1111111111111"); // 06 |
---|
72 | set_ui8vector_str(X[i++], 0, w - 1, " 11111111111111 "); // 07 |
---|
73 | set_ui8vector_str(X[i++], 0, w - 1, " 11111111111111 "); // 08 |
---|
74 | set_ui8vector_str(X[i++], 0, w - 1, " 11111111111111 "); // 09 |
---|
75 | set_ui8vector_str(X[i++], 0, w - 1, " 11 11111111111111 "); // 10 |
---|
76 | set_ui8vector_str(X[i++], 0, w - 1, " 111 11111111111111 "); // 11 |
---|
77 | set_ui8vector_str(X[i++], 0, w - 1, " 11111111111111111111 "); // 12 |
---|
78 | set_ui8vector_str(X[i++], 0, w - 1, " 11111111111111111111 "); // 13 |
---|
79 | set_ui8vector_str(X[i++], 0, w - 1, "1111111111111111111 "); // 14 |
---|
80 | set_ui8vector_str(X[i++], 0, w - 1, " 11111111111111111 "); // 15 |
---|
81 | set_ui8vector_str(X[i++], 0, w - 1, " 1111111111111111 "); // 16 |
---|
82 | set_ui8vector_str(X[i++], 0, w - 1, " 111111111111111 "); // 17 |
---|
83 | set_ui8vector_str(X[i++], 0, w - 1, " 111111111111 "); // 18 |
---|
84 | set_ui8vector_str(X[i++], 0, w - 1, " 1111111111 "); // 29 |
---|
85 | set_ui8vector_str(X[i++], 0, w - 1, " 1111111111 "); // 20 |
---|
86 | set_ui8vector_str(X[i++], 0, w - 1, " 111111111 "); // 21 |
---|
87 | set_ui8vector_str(X[i++], 0, w - 1, " 111111111 "); // 22 |
---|
88 | set_ui8vector_str(X[i++], 0, w - 1, " 11111111 "); // 23 |
---|
89 | set_ui8vector_str(X[i++], 0, w - 1, " 1111111 "); // 24 |
---|
90 | set_ui8vector_str(X[i++], 0, w - 1, " 11111 "); // 25 |
---|
91 | set_ui8vector_str(X[i++], 0, w - 1, " 111 "); // 26 |
---|
92 | set_ui8vector_str(X[i++], 0, w - 1, " "); // 27 |
---|
93 | |
---|
94 | //printf("[init_forme_boulon1]: h = %d i = %d\n", h, i); |
---|
95 | if (i != h) { |
---|
96 | MCA_Error("init_forme_boulon1 i != h"); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | //display_ui8matrix_positive(X, 0, h-1, 0, w-1, 4, "forme_boulon1"); printf(""); |
---|
101 | //write_ui8matrix_positive( X, 0, h-1, 0, w-1, 4, "forme_boulon1.txt"); |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | // ----------------- |
---|
106 | void mca_test1(void) |
---|
107 | // ----------------- |
---|
108 | { |
---|
109 | int i0, i1, j0, j1; |
---|
110 | int height, width; |
---|
111 | |
---|
112 | uint8 ** X0; |
---|
113 | uint8 ** X; |
---|
114 | uint32 ** E; |
---|
115 | MCA * mca; |
---|
116 | |
---|
117 | barrier_init(&main_barrier, num_threads); |
---|
118 | |
---|
119 | // -- Allocation -- |
---|
120 | init_forme_boulon1(&X0, &i0, &i1, &j0, &j1); |
---|
121 | |
---|
122 | height = i1 - i0 + 1; |
---|
123 | width = j1 - j0 + 1; |
---|
124 | |
---|
125 | // @QM Ã quoi sert X ?? |
---|
126 | X = ui8matrix (i0, i1, j0, j1); |
---|
127 | E = ui32matrix(i0, i1, j0, j1); |
---|
128 | |
---|
129 | zero_ui32matrix(E, i0, i1, j0, j1); |
---|
130 | zero_ui8matrix (X, i0, i1, j0, j1); |
---|
131 | |
---|
132 | mca = MCA_pConstructor_Empty(); |
---|
133 | |
---|
134 | // -- set param |
---|
135 | MCA_Set_Size(mca, width, height); |
---|
136 | MCA_Set_ImageX(mca, X0); |
---|
137 | MCA_Set_ImageL(mca, E); |
---|
138 | MCA_Set_NP(mca, num_threads); |
---|
139 | |
---|
140 | // -- MCA init |
---|
141 | MCA_Initialize(mca); |
---|
142 | MCA_Display_Parameters(mca); |
---|
143 | |
---|
144 | display_ui8matrix_positive(mca->X, i0, i1, j0, j1, 5, "X0"); |
---|
145 | for (int i = 1; i < num_threads; i++) { |
---|
146 | giet_pthread_create(&thread_table[i], NULL, MCA_Label_Rosenfeld, (void *) mca->mcas[i]); |
---|
147 | } |
---|
148 | MCA_Label_Rosenfeld(mca->mcas[0]); |
---|
149 | for (int i = 1; i < num_threads; i++) { |
---|
150 | giet_pthread_join(thread_table[i], NULL); |
---|
151 | } |
---|
152 | display_ui32matrix_positive(mca->E, i0, i1, j0, j1, 5, "Efinal"); |
---|
153 | |
---|
154 | |
---|
155 | // -- free -- |
---|
156 | printf("Finalize\n"); |
---|
157 | MCA_Finalize(mca); |
---|
158 | |
---|
159 | printf("Free_matrix\n"); |
---|
160 | free_ui8matrix (X0, i0, i1, j0, j1); |
---|
161 | free_ui8matrix (X, i0, i1, j0, j1); |
---|
162 | free_ui32matrix(E, i0, i1, j0, j1); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | // ----------------- |
---|
168 | void mca_test2(void) |
---|
169 | // ----------------- |
---|
170 | { |
---|
171 | int i0, i1, j0, j1; |
---|
172 | int height, width; |
---|
173 | |
---|
174 | uint8 ** X0; |
---|
175 | uint8 ** X; |
---|
176 | uint8 ** E8; |
---|
177 | uint32 ** E; |
---|
178 | MCA * mca; |
---|
179 | |
---|
180 | char * pathSrc = "/misc/"; |
---|
181 | char * pathDst = ""; |
---|
182 | char * filename = "boulons.pgm"; |
---|
183 | |
---|
184 | //RegionStats * Stats = NULL; |
---|
185 | RGBQuad palette[256]; |
---|
186 | char complete_filename[1024]; |
---|
187 | |
---|
188 | barrier_init(&main_barrier, num_threads); |
---|
189 | |
---|
190 | Palette_18ColorsBW(palette); |
---|
191 | generate_path_filename(pathSrc, filename, complete_filename, 1024); |
---|
192 | |
---|
193 | printf("Loading file %s... ", filename); |
---|
194 | X0 = LoadPGM_ui8matrix(complete_filename, &i0, &i1, &j0, &j1); |
---|
195 | printf("done.\n"); |
---|
196 | |
---|
197 | printf("Allocating memory... "); |
---|
198 | height = i1 - i0 + 1; |
---|
199 | width = j1 - j0 + 1; |
---|
200 | |
---|
201 | X = ui8matrix (i0, i1, j0, j1); |
---|
202 | E8 = ui8matrix (i0, i1, j0, j1); |
---|
203 | E = ui32matrix(i0, i1, j0, j1); |
---|
204 | |
---|
205 | zero_ui32matrix(E, i0, i1, j0, j1); |
---|
206 | zero_ui8matrix(E8, i0, i1, j0, j1); |
---|
207 | zero_ui8matrix(X, i0, i1, j0, j1); |
---|
208 | |
---|
209 | // pre-traitements |
---|
210 | binarisation_ui8matrix(X0, i0, i1, j0, j1, 20, 1, X); // pour le traitement |
---|
211 | binarisation_ui8matrix(X0, i0, i1, j0, j1, 20, 255, X0); // pour la verif visuelle |
---|
212 | printf("done.\n"); |
---|
213 | |
---|
214 | generate_path_filename(pathDst, "verif.pgm", complete_filename, 1024); |
---|
215 | printf("Saving file %s for verification... ", complete_filename); |
---|
216 | SavePGM_ui8matrix(X0, i0, i1, j0, j1, complete_filename); |
---|
217 | printf("done.\n"); |
---|
218 | |
---|
219 | |
---|
220 | printf("Allocating and initializing MCA... \n"); |
---|
221 | mca = MCA_pConstructor_Empty(); |
---|
222 | |
---|
223 | // -- set param |
---|
224 | MCA_Set_Size(mca, width, height); |
---|
225 | MCA_Set_ImageX(mca, X); |
---|
226 | MCA_Set_ImageL(mca, E); |
---|
227 | MCA_Set_NP(mca, num_threads); |
---|
228 | |
---|
229 | // -- MCA init |
---|
230 | MCA_Initialize(mca); |
---|
231 | MCA_Display_Parameters(mca); |
---|
232 | printf("End of MCA allocation and initialization.\n"); |
---|
233 | |
---|
234 | //display_ui8matrix_positive(mca->X, i0, i1, j0, j1, 5, "X0"); |
---|
235 | for (int i = 1; i < num_threads; i++) { |
---|
236 | giet_pthread_create(&thread_table[i], NULL, MCA_Label_Rosenfeld, (void *) mca->mcas[i]); |
---|
237 | } |
---|
238 | MCA_Label_Rosenfeld(mca->mcas[0]); |
---|
239 | for (int i = 1; i < num_threads; i++) { |
---|
240 | giet_pthread_join(thread_table[i], NULL); |
---|
241 | } |
---|
242 | //display_ui32matrix_positive(mca->E, i0, i1, j0, j1, 5, "Efinal"); |
---|
243 | mod_ui32matrix_ui8matrix(mca->E, i0, i1, j0, j1, E8); |
---|
244 | generate_path_filename(pathDst, "verif_final.bmp", complete_filename, 1024); |
---|
245 | printf("Saving file %s for verification... ", complete_filename); |
---|
246 | SaveBMP2_ui8matrix(E8, width, height, palette, complete_filename); |
---|
247 | printf("done.\n"); |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | // -- free -- |
---|
252 | MCA_Finalize(mca); |
---|
253 | printf("Deallocating memory..."); |
---|
254 | free_ui8matrix (X0, i0, i1, j0, j1); |
---|
255 | free_ui8matrix (X, i0, i1, j0, j1); |
---|
256 | free_ui32matrix(E, i0, i1, j0, j1); |
---|
257 | printf("done.\n"); |
---|
258 | |
---|
259 | //free_RegionStatsVector(Stats, 0, nemax); |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | |
---|
264 | |
---|
265 | // -------------------------------------- |
---|
266 | int main_test_mca() |
---|
267 | // -------------------------------------- |
---|
268 | { |
---|
269 | printf("===================\n"); |
---|
270 | printf("== main_test_mca ==\n"); |
---|
271 | printf("===================\n"); |
---|
272 | |
---|
273 | mca_test2(); |
---|
274 | |
---|
275 | return 0; |
---|
276 | } |
---|
277 | |
---|