source: soft/giet_vm/applications/rosenfeld/nrc2/src/nrio2.c @ 826

Last change on this file since 826 was 826, checked in by meunier, 7 years ago
  • Mise à jour NR2 et Rosenfeld
File size: 16.4 KB
RevLine 
[772]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>
[798]16#include <string.h>
[772]17
18#include "nrc_os_config.h"
[798]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
[772]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
[798]40#define isalnum(x) (((x) >= 0x30 && (x) <= 0x39) \
41                 || ((x) >= 0x41 && (x) <= 0x5A) \
42                 || ((x) >= 0x61 && (x) <= 0x7A))
43
44
[772]45/*
46 * ----------------------
47 * --- display_matrix ---
48 * ----------------------
49 */
50
[822]51
52#undef display_type_matrix
53#define display_type_matrix(t) \
54void 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    }                                          \
[772]66}
[822]67
68display_type_matrix(int8_t);
69display_type_matrix(uint8_t);
70display_type_matrix(int16_t);
71display_type_matrix(uint16_t);
72display_type_matrix(int32_t);
73display_type_matrix(uint32_t);
74display_type_matrix(int64_t);
75display_type_matrix(uint64_t);
76display_type_matrix(float);
77display_type_matrix(double);
78
79
[772]80/* -------------------------------------------------------------------------------------------------------- */
[822]81void display_rgb8matrix(rgb8 ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name)
[772]82/* -------------------------------------------------------------------------------------------------------- */
83{
[822]84    if (name != NULL) {
85        printf(name);
[772]86    }
87   
[822]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);
[772]91        }
92        printf("\n");
93    }
94}
[822]95
[772]96/* ---------------------------------------------------------------------------------------------------------- */
[822]97void display_rgbx8matrix(rgbx8 ** m,int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name)
[772]98/* ---------------------------------------------------------------------------------------------------------- */
99{
[822]100    if (name != NULL) {
101        printf(name);
[772]102    }
103   
[822]104    for (int32_t i = nrl; i <= nrh; i++) {
105        for (int32_t j = ncl; j <= nch; j++) {
[772]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}
[822]111
112
[772]113/*
[822]114 * ------------------------
115 * --- display_matrix_T ---
116 * ------------------------
[772]117 */
118
[822]119#undef display_type_matrix_T
120#define display_type_matrix_T(t) \
121void 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    }                                          \
[772]133}
[822]134
135display_type_matrix_T(int8_t);
136display_type_matrix_T(uint8_t);
137display_type_matrix_T(int16_t);
138display_type_matrix_T(uint16_t);
139display_type_matrix_T(int32_t);
140display_type_matrix_T(uint32_t);
141display_type_matrix_T(int64_t);
142display_type_matrix_T(uint64_t);
143display_type_matrix_T(float);
144display_type_matrix_T(double);
145
146
147/* -------------------------------------------------------------------------------------------------------- */
148void display_rgb8matrix_T(rgb8 ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name)
149/* -------------------------------------------------------------------------------------------------------- */
[772]150{
[822]151    if (name != NULL) {
152        printf(name);
[772]153    }
154   
[822]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);
[772]158        }
159        printf("\n");
160    }
161}
[822]162
[772]163/* ---------------------------------------------------------------------------------------------------------- */
[822]164void display_rgbx8matrix_T(rgbx8 ** m,int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name)
[772]165/* ---------------------------------------------------------------------------------------------------------- */
166{
[822]167    if (name != NULL) {
168        printf(name);
[772]169    }
170   
[822]171    for (int32_t j = ncl; j <= nch; j++) {
172        for (int32_t i = nrl; i <= nrh; i++) {
[772]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
[822]179
180
[772]181/*
[822]182 * -----------------------------
183 * --- display_matrix_number ---
184 * -----------------------------
[772]185 */
186
[822]187#undef display_type_matrix_number
188#define display_type_matrix_number(t) \
189void 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");                              \
[772]207}
[822]208
209display_type_matrix_number(int8_t);
210display_type_matrix_number(uint8_t);
211display_type_matrix_number(int16_t);
212display_type_matrix_number(uint16_t);
213display_type_matrix_number(int32_t);
214display_type_matrix_number(uint32_t);
215display_type_matrix_number(int64_t);
216display_type_matrix_number(uint64_t);
217display_type_matrix_number(float);
218display_type_matrix_number(double);
219
220
[772]221/* --------------------------------------------------------------------------------------------------------------- */
[822]222void display_rgb8matrix_number(rgb8 ** m, int32_t nrl, int32_t nrh, int32_t ncl, int32_t nch, char * format, char * name)
[772]223/* --------------------------------------------------------------------------------------------------------------- */
224{
[822]225    if (name != NULL) {
226        printf(name);
[772]227    }
228    // 1ere ligne
229    printf("%5c", '#');
[822]230    for (int32_t j = ncl; j <= nch; j++) {
[772]231        printf(format, j);
232    }
233    printf("\n");
[822]234    for (int32_t i = nrl; i <= nrh; i++) {
[772]235        printf("[%3d]", i);
[822]236        for (int32_t j = ncl; j <= nch; j++) {
237            printf(format, m[i][j].r, m[i][j].g, m[i][j].b);
[772]238        }
239        printf("\n");
240    }
241    printf("\n");
242}
[822]243
[772]244/* ----------------------------------------------------------------------------------------------------------------- */
[822]245void display_rgbx8matrix_number(rgbx8 **m,int32_t nrl,int32_t nrh,int32_t ncl, int32_t nch, char *format, char *name)
[772]246/* ----------------------------------------------------------------------------------------------------------------- */
247{
[822]248    if (name != NULL) {
249        printf(name);
[772]250    }
251    // 1ere ligne
252    printf("%5c", '#');
[822]253    for (int32_t j = ncl; j <= nch; j++) {
[772]254        printf(format, j);
255    }
256    printf("\n");
[822]257    for (int32_t i = nrl; i <= nrh; i++) {
[772]258        printf("[%3d]", i);
[822]259        for (int32_t j = ncl; j <= nch; j++) {
[772]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
[822]267
268
[772]269/*
270 * -------------------------------
271 * --- display_matrix_positive ---
272 * -------------------------------
273 */
274
[822]275#if TARGET_OS != GIETVM
276
277#undef display_type_matrix_positive
278#define display_type_matrix_positive(t) \
279void 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");                                               \
[772]299}
[805]300
[822]301#else
[805]302
[822]303#undef display_type_matrix_positive
304#define display_type_matrix_positive(t) \
305void 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");                                               \
[772]339}
[805]340
[822]341#endif
[798]342
343
[822]344display_type_matrix_positive(int8_t);
345display_type_matrix_positive(uint8_t);
346display_type_matrix_positive(int16_t);
347display_type_matrix_positive(uint16_t);
348display_type_matrix_positive(int32_t);
349display_type_matrix_positive(uint32_t);
350display_type_matrix_positive(int64_t);
351display_type_matrix_positive(uint64_t);
352display_type_matrix_positive(float);
353display_type_matrix_positive(double);
[798]354
[822]355
[798]356/* --------------------------------------- */
357static 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/* ------------------------------------------------- */
[822]401static void ReadPGMrow(int32_t fd, int32_t width, uint8 * line)
[798]402/* ------------------------------------------------- */
403{
[822]404    read(fd, &line[0], sizeof(uint8_t) * width);
[798]405}
406
407
408/* -------------------------------------------------- */
[822]409static void WritePGMrow(uint8_t * line, int32_t width, int32_t fd)
[798]410/* -------------------------------------------------- */
411{
[822]412    write(fd, &line[0], sizeof(uint8_t) * width);
[798]413}
414
415
416/* ----------------------------------------------------------------------------------------------- */
[822]417uint8_t ** LoadPGM_ui8matrix(char * filename, int * nrl, int * nrh, int * ncl, int * nch)
[798]418/* ----------------------------------------------------------------------------------------------- */
419{
420    // only for P5 binary type, not for text type
421   
[822]422    int32_t height, width, gris;
423    uint8_t ** m;
424    int32_t fd;
[798]425   
426    char buffer[80];
[821]427    (void) gris;
[798]428   
429    // open file
430    fd = open(filename, O_RDONLY);
431    if (fd < 0) {
[821]432        printf("\n*** Error: Can't open file %s in %s.\n", filename, __func__);
433        exit(1);
[798]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   
[822]452    for (int32_t i = 0; i < height; i++) {
[798]453        ReadPGMrow(fd, width, m[i]);
454    }
455   
456    close(fd);
457   
458    return m;
459}
460
461
462/* ----------------------------------------------------------------------------------------------- */
[822]463void SavePGM_ui8matrix(uint8 ** m, int nrl, int nrh, int ncl, int nch, char * filename)
[798]464/* ----------------------------------------------------------------------------------------------- */
465{
[822]466    int32_t nrow = nrh - nrl + 1;
467    int32_t ncol = nch - ncl + 1;
[798]468   
469    char buffer[80];
470   
[822]471    int32_t fd;
[826]472
473    //fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, S_IRWXU);
[798]474    fd = open(filename, O_TRUNC | O_CREAT);
475    if (fd < 0) {
[821]476        printf("\n*** Error: Impossible to open file %s in %s\n", filename, __func__);
477        return;
[798]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));
[822]483    for (int32_t i = nrl; i <= nrh; i++) {
[798]484        WritePGMrow(m[i], ncol, fd);
485    }
486   
487    /* fermeture du fichier */
488    close(fd);
489}
490
491
492
[822]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
Note: See TracBrowser for help on using the repository browser.