1 | /* --------------- */ |
---|
2 | /* --- nrio2.c --- */ |
---|
3 | /* --------------- */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Copyright (c) 2000-2014, Lionel Lacassagne, All rights reserved |
---|
7 | * Univ Paris Sud XI, CNRS |
---|
8 | * |
---|
9 | * Distributed under the Boost Software License, Version 1.0 |
---|
10 | * see accompanying file LICENSE.txt or copy it at |
---|
11 | * http://www.boost.org/LICENSE_1_0.txt |
---|
12 | */ |
---|
13 | |
---|
14 | #include <stdio.h> |
---|
15 | #include <stdlib.h> |
---|
16 | #include <string.h> |
---|
17 | |
---|
18 | #include "nrc_os_config.h" |
---|
19 | |
---|
20 | #if TARGET_OS == LINUX |
---|
21 | #include <sys/types.h> |
---|
22 | #include <sys/stat.h> |
---|
23 | #include <fcntl.h> |
---|
24 | #include <unistd.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | |
---|
28 | #include "mypredef.h" |
---|
29 | #include "nrtype.h" |
---|
30 | #include "nrdef.h" |
---|
31 | #include "nrmacro.h" |
---|
32 | #include "nrkernel.h" |
---|
33 | |
---|
34 | #include "nralloc1.h" |
---|
35 | #include "nralloc2.h" |
---|
36 | #include "nrio0.h" |
---|
37 | #include "nrio1.h" |
---|
38 | #include "nrio2.h" |
---|
39 | |
---|
40 | #define isalnum(x) (((x) >= 0x30 && (x) <= 0x39) \ |
---|
41 | || ((x) >= 0x41 && (x) <= 0x5A) \ |
---|
42 | || ((x) >= 0x61 && (x) <= 0x7A)) |
---|
43 | |
---|
44 | |
---|
45 | /* |
---|
46 | * ---------------------- |
---|
47 | * --- display_matrix --- |
---|
48 | * ---------------------- |
---|
49 | */ |
---|
50 | |
---|
51 | |
---|
52 | #undef display_type_matrix |
---|
53 | #define display_type_matrix(t) \ |
---|
54 | void short_name(t,display_,matrix)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name) \ |
---|
55 | { \ |
---|
56 | if (name != NULL) { \ |
---|
57 | printf("%s\n", name); \ |
---|
58 | } \ |
---|
59 | \ |
---|
60 | for (int32_t i = nrl; i <= nrh; i++) { \ |
---|
61 | for (int32_t j = ncl; j <= nch; j++) { \ |
---|
62 | printf(format, m[i][j]); \ |
---|
63 | } \ |
---|
64 | printf("\n"); \ |
---|
65 | } \ |
---|
66 | } |
---|
67 | |
---|
68 | display_type_matrix(int8_t); |
---|
69 | display_type_matrix(uint8_t); |
---|
70 | display_type_matrix(int16_t); |
---|
71 | display_type_matrix(uint16_t); |
---|
72 | display_type_matrix(int32_t); |
---|
73 | display_type_matrix(uint32_t); |
---|
74 | display_type_matrix(int64_t); |
---|
75 | display_type_matrix(uint64_t); |
---|
76 | display_type_matrix(float); |
---|
77 | display_type_matrix(double); |
---|
78 | |
---|
79 | |
---|
80 | /* -------------------------------------------------------------------------------------------------------- */ |
---|
81 | void display_rgb8matrix(rgb8 ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name) |
---|
82 | /* -------------------------------------------------------------------------------------------------------- */ |
---|
83 | { |
---|
84 | if (name != NULL) { |
---|
85 | printf(name); |
---|
86 | } |
---|
87 | |
---|
88 | for (int32_t i = nrl; i <= nrh; i++) { |
---|
89 | for (int32_t j = ncl; j <= nch; j++) { |
---|
90 | printf(format, m[i][j].r, m[i][j].g, m[i][j].b); |
---|
91 | } |
---|
92 | printf("\n"); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | /* ---------------------------------------------------------------------------------------------------------- */ |
---|
97 | void display_rgbx8matrix(rgbx8 ** m,int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name) |
---|
98 | /* ---------------------------------------------------------------------------------------------------------- */ |
---|
99 | { |
---|
100 | if (name != NULL) { |
---|
101 | printf(name); |
---|
102 | } |
---|
103 | |
---|
104 | for (int32_t i = nrl; i <= nrh; i++) { |
---|
105 | for (int32_t j = ncl; j <= nch; j++) { |
---|
106 | printf(format, m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x); |
---|
107 | } |
---|
108 | printf("\n"); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | /* |
---|
114 | * ------------------------ |
---|
115 | * --- display_matrix_T --- |
---|
116 | * ------------------------ |
---|
117 | */ |
---|
118 | |
---|
119 | #undef display_type_matrix_T |
---|
120 | #define display_type_matrix_T(t) \ |
---|
121 | void short_name(t,display_,matrix_T)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name) \ |
---|
122 | { \ |
---|
123 | if (name != NULL) { \ |
---|
124 | printf("%s\n", name); \ |
---|
125 | } \ |
---|
126 | \ |
---|
127 | for (int32_t j = ncl; j <= nch; j++) { \ |
---|
128 | for (int32_t i = nrl; i <= nrh; i++) { \ |
---|
129 | printf(format, m[i][j]); \ |
---|
130 | } \ |
---|
131 | printf("\n"); \ |
---|
132 | } \ |
---|
133 | } |
---|
134 | |
---|
135 | display_type_matrix_T(int8_t); |
---|
136 | display_type_matrix_T(uint8_t); |
---|
137 | display_type_matrix_T(int16_t); |
---|
138 | display_type_matrix_T(uint16_t); |
---|
139 | display_type_matrix_T(int32_t); |
---|
140 | display_type_matrix_T(uint32_t); |
---|
141 | display_type_matrix_T(int64_t); |
---|
142 | display_type_matrix_T(uint64_t); |
---|
143 | display_type_matrix_T(float); |
---|
144 | display_type_matrix_T(double); |
---|
145 | |
---|
146 | |
---|
147 | /* -------------------------------------------------------------------------------------------------------- */ |
---|
148 | void display_rgb8matrix_T(rgb8 ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name) |
---|
149 | /* -------------------------------------------------------------------------------------------------------- */ |
---|
150 | { |
---|
151 | if (name != NULL) { |
---|
152 | printf(name); |
---|
153 | } |
---|
154 | |
---|
155 | for (int32_t j = ncl; j <= nch; j++) { |
---|
156 | for (int32_t i = nrl; i <= nrh; i++) { |
---|
157 | printf(format, m[i][j].r, m[i][j].g, m[i][j].b); |
---|
158 | } |
---|
159 | printf("\n"); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /* ---------------------------------------------------------------------------------------------------------- */ |
---|
164 | void display_rgbx8matrix_T(rgbx8 ** m,int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name) |
---|
165 | /* ---------------------------------------------------------------------------------------------------------- */ |
---|
166 | { |
---|
167 | if (name != NULL) { |
---|
168 | printf(name); |
---|
169 | } |
---|
170 | |
---|
171 | for (int32_t j = ncl; j <= nch; j++) { |
---|
172 | for (int32_t i = nrl; i <= nrh; i++) { |
---|
173 | printf(format, m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x); |
---|
174 | } |
---|
175 | printf("\n"); |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | /* |
---|
182 | * ----------------------------- |
---|
183 | * --- display_matrix_number --- |
---|
184 | * ----------------------------- |
---|
185 | */ |
---|
186 | |
---|
187 | #undef display_type_matrix_number |
---|
188 | #define display_type_matrix_number(t) \ |
---|
189 | void short_name(t,display_,matrix_number)(t ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name) \ |
---|
190 | { \ |
---|
191 | if (name != NULL) { \ |
---|
192 | printf("%s\n", name); \ |
---|
193 | } \ |
---|
194 | printf("%5c", '#'); \ |
---|
195 | for (int32_t j = ncl; j <= nch; j++) { \ |
---|
196 | printf(format, j); \ |
---|
197 | } \ |
---|
198 | printf("\n"); \ |
---|
199 | for (int32_t i = nrl; i <= nrh; i++) { \ |
---|
200 | printf("[%3d]", i); \ |
---|
201 | for (int32_t j = ncl; j <= nch; j++) { \ |
---|
202 | printf(format, m[i][j]); \ |
---|
203 | } \ |
---|
204 | printf("\n"); \ |
---|
205 | } \ |
---|
206 | printf("\n"); \ |
---|
207 | } |
---|
208 | |
---|
209 | display_type_matrix_number(int8_t); |
---|
210 | display_type_matrix_number(uint8_t); |
---|
211 | display_type_matrix_number(int16_t); |
---|
212 | display_type_matrix_number(uint16_t); |
---|
213 | display_type_matrix_number(int32_t); |
---|
214 | display_type_matrix_number(uint32_t); |
---|
215 | display_type_matrix_number(int64_t); |
---|
216 | display_type_matrix_number(uint64_t); |
---|
217 | display_type_matrix_number(float); |
---|
218 | display_type_matrix_number(double); |
---|
219 | |
---|
220 | |
---|
221 | /* --------------------------------------------------------------------------------------------------------------- */ |
---|
222 | void display_rgb8matrix_number(rgb8 ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name) |
---|
223 | /* --------------------------------------------------------------------------------------------------------------- */ |
---|
224 | { |
---|
225 | if (name != NULL) { |
---|
226 | printf(name); |
---|
227 | } |
---|
228 | // 1ere ligne |
---|
229 | printf("%5c", '#'); |
---|
230 | for (int32_t j = ncl; j <= nch; j++) { |
---|
231 | printf(format, j); |
---|
232 | } |
---|
233 | printf("\n"); |
---|
234 | for (int32_t i = nrl; i <= nrh; i++) { |
---|
235 | printf("[%3d]", i); |
---|
236 | for (int32_t j = ncl; j <= nch; j++) { |
---|
237 | printf(format, m[i][j].r, m[i][j].g, m[i][j].b); |
---|
238 | } |
---|
239 | printf("\n"); |
---|
240 | } |
---|
241 | printf("\n"); |
---|
242 | } |
---|
243 | |
---|
244 | /* ----------------------------------------------------------------------------------------------------------------- */ |
---|
245 | void display_rgbx8matrix_number(rgbx8 **m,int32_t nrl,int32_t nrh,int32_t ncl, int32_t nch, char *format, char *name) |
---|
246 | /* ----------------------------------------------------------------------------------------------------------------- */ |
---|
247 | { |
---|
248 | if (name != NULL) { |
---|
249 | printf(name); |
---|
250 | } |
---|
251 | // 1ere ligne |
---|
252 | printf("%5c", '#'); |
---|
253 | for (int32_t j = ncl; j <= nch; j++) { |
---|
254 | printf(format, j); |
---|
255 | } |
---|
256 | printf("\n"); |
---|
257 | for (int32_t i = nrl; i <= nrh; i++) { |
---|
258 | printf("[%3d]", i); |
---|
259 | for (int32_t j = ncl; j <= nch; j++) { |
---|
260 | printf(format, m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x); |
---|
261 | } |
---|
262 | printf("\n"); |
---|
263 | } |
---|
264 | printf("\n"); |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | |
---|
269 | /* |
---|
270 | * ------------------------------- |
---|
271 | * --- display_matrix_positive --- |
---|
272 | * ------------------------------- |
---|
273 | */ |
---|
274 | |
---|
275 | #if TARGET_OS != GIETVM |
---|
276 | |
---|
277 | #undef display_type_matrix_positive |
---|
278 | #define display_type_matrix_positive(t) \ |
---|
279 | void short_name(t,display_,matrix_positive)(t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, int32_t iformat, char * name) \ |
---|
280 | { \ |
---|
281 | char * format; \ |
---|
282 | char * str; \ |
---|
283 | select_display_positive_parameters(iformat, &format, &str); \ |
---|
284 | if (name != NULL) { \ |
---|
285 | printf("%s\n", name); \ |
---|
286 | } \ |
---|
287 | for (int32_t i = i0; i <= i1; i++) { \ |
---|
288 | for (int32_t j = j0; j <= j1; j++) { \ |
---|
289 | if (m[i][j] != 0) { \ |
---|
290 | printf(format, m[i][j]); \ |
---|
291 | } \ |
---|
292 | else { \ |
---|
293 | printf("%s", str); \ |
---|
294 | } \ |
---|
295 | } \ |
---|
296 | printf("\n"); \ |
---|
297 | } \ |
---|
298 | printf("\n"); \ |
---|
299 | } |
---|
300 | |
---|
301 | #else |
---|
302 | |
---|
303 | #undef display_type_matrix_positive |
---|
304 | #define display_type_matrix_positive(t) \ |
---|
305 | void short_name(t,display_,matrix_positive)(t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, int32_t iformat, char * name) \ |
---|
306 | { \ |
---|
307 | char * format; \ |
---|
308 | char * str; \ |
---|
309 | select_display_positive_parameters(iformat, &format, &str); \ |
---|
310 | if (name != NULL) { \ |
---|
311 | printf("%s\n", name); \ |
---|
312 | } \ |
---|
313 | for (int32_t i = i0; i <= i1; i++) { \ |
---|
314 | for (int32_t j = j0; j <= j1; j++) { \ |
---|
315 | if (m[i][j] != 0) { \ |
---|
316 | int32_t a = m[i][j]; \ |
---|
317 | int32_t len = 0; \ |
---|
318 | if (a == 0) { \ |
---|
319 | len = 1; \ |
---|
320 | } \ |
---|
321 | else { \ |
---|
322 | while (a != 0) { \ |
---|
323 | a = a / 10; \ |
---|
324 | len++; \ |
---|
325 | } \ |
---|
326 | } \ |
---|
327 | for (int32_t k = len; k < iformat; k++) { \ |
---|
328 | printf(" "); \ |
---|
329 | } \ |
---|
330 | printf(format, m[i][j]); \ |
---|
331 | } \ |
---|
332 | else { \ |
---|
333 | printf("%s", str); \ |
---|
334 | } \ |
---|
335 | } \ |
---|
336 | printf("\n"); \ |
---|
337 | } \ |
---|
338 | printf("\n"); \ |
---|
339 | } |
---|
340 | |
---|
341 | #endif |
---|
342 | |
---|
343 | |
---|
344 | display_type_matrix_positive(int8_t); |
---|
345 | display_type_matrix_positive(uint8_t); |
---|
346 | display_type_matrix_positive(int16_t); |
---|
347 | display_type_matrix_positive(uint16_t); |
---|
348 | display_type_matrix_positive(int32_t); |
---|
349 | display_type_matrix_positive(uint32_t); |
---|
350 | display_type_matrix_positive(int64_t); |
---|
351 | display_type_matrix_positive(uint64_t); |
---|
352 | display_type_matrix_positive(float); |
---|
353 | display_type_matrix_positive(double); |
---|
354 | |
---|
355 | |
---|
356 | /* --------------------------------------- */ |
---|
357 | static char * readitem(int fd, char * buffer) |
---|
358 | /* --------------------------------------- */ |
---|
359 | { |
---|
360 | char * aux; |
---|
361 | int k; |
---|
362 | int n; |
---|
363 | |
---|
364 | k = 0; |
---|
365 | aux = buffer; |
---|
366 | while (1) { |
---|
367 | n = read(fd, aux, 1); |
---|
368 | if (n == 0) { |
---|
369 | break; |
---|
370 | } |
---|
371 | switch (k) { |
---|
372 | case 0: |
---|
373 | if (*aux == '#') { |
---|
374 | k = 1; |
---|
375 | } |
---|
376 | if (isalnum(*aux)) { |
---|
377 | k = 2; |
---|
378 | aux++; |
---|
379 | } |
---|
380 | break; |
---|
381 | case 1: |
---|
382 | if (*aux == 0xA) { |
---|
383 | k = 0; |
---|
384 | } |
---|
385 | break; |
---|
386 | case 2: |
---|
387 | if (!isalnum(*aux)) { |
---|
388 | *aux = 0; |
---|
389 | return buffer; |
---|
390 | } |
---|
391 | aux++; |
---|
392 | break; |
---|
393 | } |
---|
394 | } |
---|
395 | *aux = 0; |
---|
396 | return buffer; |
---|
397 | } |
---|
398 | |
---|
399 | |
---|
400 | /* ------------------------------------------------- */ |
---|
401 | static void ReadPGMrow(int32_t fd, int32_t width, uint8 * line) |
---|
402 | /* ------------------------------------------------- */ |
---|
403 | { |
---|
404 | read(fd, &line[0], sizeof(uint8_t) * width); |
---|
405 | } |
---|
406 | |
---|
407 | |
---|
408 | /* -------------------------------------------------- */ |
---|
409 | static void WritePGMrow(uint8_t * line, int32_t width, int32_t fd) |
---|
410 | /* -------------------------------------------------- */ |
---|
411 | { |
---|
412 | write(fd, &line[0], sizeof(uint8_t) * width); |
---|
413 | } |
---|
414 | |
---|
415 | |
---|
416 | /* ----------------------------------------------------------------------------------------------- */ |
---|
417 | uint8_t ** LoadPGM_ui8matrix(char * filename, int * nrl, int * nrh, int * ncl, int * nch) |
---|
418 | /* ----------------------------------------------------------------------------------------------- */ |
---|
419 | { |
---|
420 | // only for P5 binary type, not for text type |
---|
421 | |
---|
422 | int32_t height, width, gris; |
---|
423 | uint8_t ** m; |
---|
424 | int32_t fd; |
---|
425 | |
---|
426 | char buffer[80]; |
---|
427 | (void) gris; |
---|
428 | |
---|
429 | // open file |
---|
430 | fd = open(filename, O_RDONLY); |
---|
431 | if (fd < 0) { |
---|
432 | printf("\n*** Error: Can't open file %s in %s.\n", filename, __func__); |
---|
433 | exit(1); |
---|
434 | } |
---|
435 | |
---|
436 | // read PGM header |
---|
437 | readitem(fd, &buffer[0]); |
---|
438 | if (strcmp(&buffer[0], "P5") != 0) { |
---|
439 | printf("*** Error: Invalid file header in file %s\n", filename); |
---|
440 | } |
---|
441 | |
---|
442 | width = atoi(readitem(fd, &buffer[0])); |
---|
443 | height = atoi(readitem(fd, &buffer[0])); |
---|
444 | gris = atoi(readitem(fd, &buffer[0])); |
---|
445 | |
---|
446 | *nrl = 0; |
---|
447 | *nrh = height - 1; |
---|
448 | *ncl = 0; |
---|
449 | *nch = width - 1; |
---|
450 | m = ui8matrix(*nrl, *nrh, *ncl, *nch); |
---|
451 | |
---|
452 | for (int32_t i = 0; i < height; i++) { |
---|
453 | ReadPGMrow(fd, width, m[i]); |
---|
454 | } |
---|
455 | |
---|
456 | close(fd); |
---|
457 | |
---|
458 | return m; |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | /* ----------------------------------------------------------------------------------------------- */ |
---|
463 | void SavePGM_ui8matrix(uint8 ** m, int nrl, int nrh, int ncl, int nch, char * filename) |
---|
464 | /* ----------------------------------------------------------------------------------------------- */ |
---|
465 | { |
---|
466 | int32_t nrow = nrh - nrl + 1; |
---|
467 | int32_t ncol = nch - ncl + 1; |
---|
468 | |
---|
469 | char buffer[80]; |
---|
470 | |
---|
471 | int32_t fd; |
---|
472 | |
---|
473 | //fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRWXU); |
---|
474 | fd = open(filename, O_TRUNC | O_CREAT); |
---|
475 | if (fd < 0) { |
---|
476 | printf("\n*** Error: Impossible to open file %s in %s\n", filename, __func__); |
---|
477 | return; |
---|
478 | } |
---|
479 | |
---|
480 | /* enregistrement de l'image au format rpgm */ |
---|
481 | snprintf(buffer, 80, "P5\n%d %d\n255\n", ncol, nrow); |
---|
482 | write(fd, buffer, strlen(buffer)); |
---|
483 | for (int32_t i = nrl; i <= nrh; i++) { |
---|
484 | WritePGMrow(m[i], ncol, fd); |
---|
485 | } |
---|
486 | |
---|
487 | /* fermeture du fichier */ |
---|
488 | close(fd); |
---|
489 | } |
---|
490 | |
---|
491 | |
---|
492 | |
---|
493 | // Local Variables: |
---|
494 | // tab-width: 4 |
---|
495 | // c-basic-offset: 4 |
---|
496 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
497 | // indent-tabs-mode: nil |
---|
498 | // End: |
---|
499 | |
---|
500 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
501 | |
---|