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

Last change on this file since 798 was 798, checked in by meunier, 8 years ago
  • Bug fixes in Rosenfeld
File size: 31.6 KB
Line 
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/* -------------------------------------------------------------------------------------------------------- */
52IMAGE_EXPORT(void) display_ui8matrix(uint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
53/* -------------------------------------------------------------------------------------------------------- */
54{
55    long i,j;
56   
57    if(name != NULL) printf(name);
58   
59    for(i=nrl; i<=nrh; i++) {
60        for(j=ncl; j<=nch; j++) {
61            printf(format, m[i][j]);
62        }
63        printf("\n");
64    }
65}
66/* -------------------------------------------------------------------------------------------------------- */
67IMAGE_EXPORT(void) display_si8matrix(sint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
68/* -------------------------------------------------------------------------------------------------------- */
69{
70    long i,j;
71   
72    if(name != NULL) printf(name);
73   
74    for(i=nrl; i<=nrh; i++) {
75        for(j=ncl; j<=nch; j++) {
76            printf(format, m[i][j]);
77        }
78        printf("\n");
79    }
80}
81/* ---------------------------------------------------------------------------------------------------------- */
82IMAGE_EXPORT(void) display_si16matrix(sint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
83/* ---------------------------------------------------------------------------------------------------------- */
84{
85    long i,j;
86   
87    if(name != NULL) printf(name);
88   
89    for(i=nrl; i<=nrh; i++) {
90        for(j=ncl; j<=nch; j++) {
91            printf(format, m[i][j]);
92        }
93        printf("\n");
94    }
95}
96/* ---------------------------------------------------------------------------------------------------------- */
97IMAGE_EXPORT(void) display_ui16matrix(uint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
98/* ---------------------------------------------------------------------------------------------------------- */
99{
100    long i,j;
101   
102    if(name != NULL) printf(name);
103   
104    for(i=nrl; i<=nrh; i++) {
105        for(j=ncl; j<=nch; j++) {
106            printf(format, m[i][j]);
107        }
108        printf("\n");
109    }
110}
111/* ---------------------------------------------------------------------------------------------------------- */
112IMAGE_EXPORT(void) display_si32matrix(sint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
113/* ---------------------------------------------------------------------------------------------------------- */
114{
115    long i,j;
116   
117    if(name != NULL) printf(name);
118   
119    for(i=nrl; i<=nrh; i++) {
120        for(j=ncl; j<=nch; j++) {
121            printf(format, m[i][j]);
122        }
123        printf("\n");
124    }
125}
126/* ---------------------------------------------------------------------------------------------------------- */
127IMAGE_EXPORT(void) display_ui32matrix(uint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
128/* ---------------------------------------------------------------------------------------------------------- */
129{
130    long i,j;
131   
132    if(name != NULL) printf(name);
133   
134    for(i=nrl; i<=nrh; i++) {
135        for(j=ncl; j<=nch; j++) {
136            printf(format, m[i][j]);
137        }
138        printf("\n");
139    }
140}
141/* ---------------------------------------------------------------------------------------------------------- */
142IMAGE_EXPORT(void) display_si64matrix(sint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
143/* ---------------------------------------------------------------------------------------------------------- */
144{
145    long i,j;
146   
147    if(name != NULL) printf(name);
148   
149    for(i=nrl; i<=nrh; i++) {
150        for(j=ncl; j<=nch; j++) {
151            printf(format, m[i][j]);
152        }
153        printf("\n");
154    }
155}
156/* --------------------------------------------------------------------------------------------------------- */
157IMAGE_EXPORT(void) display_i64matrix(uint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
158/* --------------------------------------------------------------------------------------------------------- */
159{
160    long i,j;
161   
162    if(name != NULL) printf(name);
163   
164    for(i=nrl; i<=nrh; i++) {
165        for(j=ncl; j<=nch; j++) {
166            printf(format, m[i][j]);
167        }
168        printf("\n");
169    }
170}
171/* ---------------------------------------------------------------------------------------------------------- */
172IMAGE_EXPORT(void) display_f32matrix(float32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
173/* ---------------------------------------------------------------------------------------------------------- */
174{
175    long i,j;
176   
177    if(name != NULL) printf(name);
178   
179    for(i=nrl; i<=nrh; i++) {
180        for(j=ncl; j<=nch; j++) {
181            printf(format, m[i][j]);
182        }
183        printf("\n");
184    }
185}
186/* ---------------------------------------------------------------------------------------------------------- */
187IMAGE_EXPORT(void) display_f64matrix(float64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
188/* ---------------------------------------------------------------------------------------------------------- */
189{
190    long i,j;
191   
192    if(name != NULL) printf(name);
193   
194    for(i=nrl; i<=nrh; i++) {
195        for(j=ncl; j<=nch; j++) {
196            printf(format, m[i][j]);
197        }
198        printf("\n");
199    }
200}
201/* -------------------------------------------------------------------------------------------------------- */
202IMAGE_EXPORT(void) display_rgb8matrix(rgb8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
203/* -------------------------------------------------------------------------------------------------------- */
204{
205    long i,j;
206   
207    if(name != NULL) printf(name);
208   
209    for(i=nrl; i<=nrh; i++) {
210        for(j=ncl; j<=nch; j++) {
211            printf(format, m[i][j].r, m[i][j].g, m[i][j].b);
212        }
213        printf("\n");
214    }
215}
216/* ---------------------------------------------------------------------------------------------------------- */
217IMAGE_EXPORT(void) display_rgbx8matrix(rgbx8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
218/* ---------------------------------------------------------------------------------------------------------- */
219{
220    long i,j;
221   
222    if(name != NULL) printf(name);
223   
224    for(i=nrl; i<=nrh; i++) {
225        for(j=ncl; j<=nch; j++) {
226            printf(format, m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x);
227        }
228        printf("\n");
229    }
230}
231/*
232 * ----------------------
233 * --- display_matrix ---
234 * ----------------------
235 */
236
237/* ---------------------------------------------------------------------------------------------------------- */
238IMAGE_EXPORT(void) display_si8matrix_T(sint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
239/* ---------------------------------------------------------------------------------------------------------- */
240{
241    long i,j;
242   
243    if(name != NULL) printf(name);
244   
245    for(j=ncl; j<=nch; j++) {
246        for(i=nrl; i<=nrh; i++) {           
247            printf(format, m[i][j]);
248        }
249        printf("\n");
250    }
251}
252/* ---------------------------------------------------------------------------------------------------------- */
253IMAGE_EXPORT(void) display_ui8matrix_T(uint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
254/* ---------------------------------------------------------------------------------------------------------- */
255{
256    long i,j;
257   
258    if(name != NULL) printf(name);
259   
260    for(j=ncl; j<=nch; j++) {
261        for(i=nrl; i<=nrh; i++) {           
262            printf(format, m[i][j]);
263        }
264        printf("\n");
265    }
266}
267/* ------------------------------------------------------------------------------------------------------------ */
268IMAGE_EXPORT(void) display_si16matrix_T(sint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
269/* ------------------------------------------------------------------------------------------------------------ */
270{
271    long i,j;
272   
273    if(name != NULL) printf(name);
274   
275    for(j=ncl; j<=nch; j++) {
276        for(i=nrl; i<=nrh; i++) {           
277            printf(format, m[i][j]);
278        }
279        printf("\n");
280    }
281}
282/* ------------------------------------------------------------------------------------------------------------ */
283IMAGE_EXPORT(void) display_ui16matrix_T(uint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
284/* ------------------------------------------------------------------------------------------------------------ */
285{
286    long i,j;
287   
288    if(name != NULL) printf(name);
289   
290    for(j=ncl; j<=nch; j++) {
291        for(i=nrl; i<=nrh; i++) {           
292            printf(format, m[i][j]);
293        }
294        printf("\n");
295    }
296}
297/* ------------------------------------------------------------------------------------------------------------ */
298IMAGE_EXPORT(void) display_si32matrix_T(sint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
299/* ------------------------------------------------------------------------------------------------------------ */
300{
301    long i,j;
302   
303    if(name != NULL) printf(name);
304   
305    for(j=ncl; j<=nch; j++) {
306        for(i=nrl; i<=nrh; i++) {           
307            printf(format, m[i][j]);
308        }
309        printf("\n");
310    }
311}
312/* ---------------------------------------------------------------------------------------------------------- */
313IMAGE_EXPORT(void) display_ui32matrix_T(uint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
314/* ---------------------------------------------------------------------------------------------------------- */
315{
316    long i,j;
317   
318    if(name != NULL) printf(name);
319   
320    for(j=ncl; j<=nch; j++) {
321        for(i=nrl; i<=nrh; i++) {           
322            printf(format, m[i][j]);
323        }
324        printf("\n");
325    }
326}
327/* ------------------------------------------------------------------------------------------------------------ */
328IMAGE_EXPORT(void) display_si64matrix_T(sint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
329/* ------------------------------------------------------------------------------------------------------------ */
330{
331    long i,j;
332   
333    if(name != NULL) printf(name);
334   
335    for(j=ncl; j<=nch; j++) {
336        for(i=nrl; i<=nrh; i++) {           
337            printf(format, m[i][j]);
338        }
339        printf("\n");
340    }
341}
342/* ------------------------------------------------------------------------------------------------------------ */
343IMAGE_EXPORT(void) display_ui64matrix_T(uint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
344/* ------------------------------------------------------------------------------------------------------------ */
345{
346    long i,j;
347   
348    if(name != NULL) printf(name);
349   
350    for(j=ncl; j<=nch; j++) {
351        for(i=nrl; i<=nrh; i++) {           
352            printf(format, m[i][j]);
353        }
354        printf("\n");
355    }
356}
357/* ------------------------------------------------------------------------------------------------------------ */
358IMAGE_EXPORT(void) display_f32matrix_T(float32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
359/* ------------------------------------------------------------------------------------------------------------ */
360{
361    long i,j;
362   
363    if(name != NULL) printf(name);
364   
365    for(j=ncl; j<=nch; j++) {
366        for(i=nrl; i<=nrh; i++) {           
367            printf(format, m[i][j]);
368        }
369        printf("\n");
370    }
371}
372/* ------------------------------------------------------------------------------------------------------------ */
373IMAGE_EXPORT(void) display_f64matrix_T(float64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
374/* ------------------------------------------------------------------------------------------------------------ */
375{
376    long i,j;
377   
378    if(name != NULL) printf(name);
379   
380    for(j=ncl; j<=nch; j++) {
381        for(i=nrl; i<=nrh; i++) {           
382            printf(format, m[i][j]);
383        }
384        printf("\n");
385    }
386}
387/* ---------------------------------------------------------------------------------------------------------- */
388IMAGE_EXPORT(void) display_rgb8matrix_T(rgb8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
389/* ---------------------------------------------------------------------------------------------------------- */
390{
391    long i,j;
392   
393    if(name != NULL) printf(name);
394   
395    for(j=ncl; j<=nch; j++) {
396        for(i=nrl; i<=nrh; i++) {           
397            printf(format, m[i][j].r, m[i][j].g, m[i][j].b);
398        }
399        printf("\n");
400    }
401}
402/* ------------------------------------------------------------------------------------------------------------ */
403IMAGE_EXPORT(void) display_rgbx8matrix_T(rgbx8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
404/* ------------------------------------------------------------------------------------------------------------ */
405{
406    long i,j;
407   
408    if(name != NULL) printf(name);
409   
410    for(j=ncl; j<=nch; j++) {
411        for(i=nrl; i<=nrh; i++) {           
412            printf(format, m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x);
413        }
414        printf("\n");
415    }
416}
417
418/*
419 * ------------------------
420 * --- display_matrix_T ---
421 * ------------------------
422 */
423
424/* --------------------------------------------------------------------------------------------------------------- */
425IMAGE_EXPORT(void) display_si8matrix_number(sint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
426/* --------------------------------------------------------------------------------------------------------------- */
427{
428    int i,j;
429   
430    if(name != NULL) printf(name);
431   
432    // 1ere ligne
433    printf("%5c", '#');
434    for(j=ncl; j<=nch; j++) {
435        printf(format, j);
436    }
437    printf("\n");
438    for(i=nrl; i<=nrh; i++) {
439        printf("[%3d]", i);
440        for(j=ncl; j<=nch; j++) {
441            printf(format, m[i][j]);
442        }
443        printf("\n");
444    }
445    printf("\n");
446}
447/* --------------------------------------------------------------------------------------------------------------- */
448IMAGE_EXPORT(void) display_ui8matrix_number(uint8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
449/* --------------------------------------------------------------------------------------------------------------- */
450{
451    int i,j;
452   
453    if(name != NULL) printf(name);
454   
455    // 1ere ligne
456    printf("%5c", '#');
457    for(j=ncl; j<=nch; j++) {
458        printf(format, j);
459    }
460    printf("\n");
461    for(i=nrl; i<=nrh; i++) {
462        printf("[%3d]", i);
463        for(j=ncl; j<=nch; j++) {
464            printf(format, m[i][j]);
465        }
466        printf("\n");
467    }
468    printf("\n");
469}
470/* ----------------------------------------------------------------------------------------------------------------- */
471IMAGE_EXPORT(void) display_si16matrix_number(sint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
472/* ----------------------------------------------------------------------------------------------------------------- */
473{
474    int i,j;
475   
476    if(name != NULL) printf(name);
477   
478    // 1ere ligne
479    printf("%5c", '#');
480    for(j=ncl; j<=nch; j++) {
481        printf(format, j);
482    }
483    printf("\n");
484    for(i=nrl; i<=nrh; i++) {
485        printf("[%3d]", i);
486        for(j=ncl; j<=nch; j++) {
487            printf(format, m[i][j]);
488        }
489        printf("\n");
490    }
491    printf("\n");
492}
493/* ----------------------------------------------------------------------------------------------------------------- */
494IMAGE_EXPORT(void) display_ui16matrix_number(uint16 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
495/* ----------------------------------------------------------------------------------------------------------------- */
496{
497    int i,j;
498   
499    if(name != NULL) printf(name);
500   
501    // 1ere ligne
502    printf("%5c", '#');
503    for(j=ncl; j<=nch; j++) {
504        printf(format, j);
505    }
506    printf("\n");
507    for(i=nrl; i<=nrh; i++) {
508        printf("[%3d]", i);
509        for(j=ncl; j<=nch; j++) {
510            printf(format, m[i][j]);
511        }
512        printf("\n");
513    }
514    printf("\n");
515}
516/* ----------------------------------------------------------------------------------------------------------------- */
517IMAGE_EXPORT(void) display_si32matrix_number(sint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
518/* ----------------------------------------------------------------------------------------------------------------- */
519{
520    int i,j;
521   
522    if(name != NULL) printf(name);
523   
524    // 1ere ligne
525    printf("%5c", '#');
526    for(j=ncl; j<=nch; j++) {
527        printf(format, j);
528    }
529    printf("\n");
530    for(i=nrl; i<=nrh; i++) {
531        printf("[%3d]", i);
532        for(j=ncl; j<=nch; j++) {
533            printf(format, m[i][j]);
534        }
535        printf("\n");
536    }
537    printf("\n");
538}
539/* ----------------------------------------------------------------------------------------------------------------- */
540IMAGE_EXPORT(void) display_ui32matrix_number(uint32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
541/* ----------------------------------------------------------------------------------------------------------------- */
542{
543    int i,j;
544   
545    if(name != NULL) printf(name);
546   
547    // 1ere ligne
548    printf("%5c", '#');
549    for(j=ncl; j<=nch; j++) {
550        printf(format, j);
551    }
552    printf("\n");
553    for(i=nrl; i<=nrh; i++) {
554        printf("[%3d]", i);
555        for(j=ncl; j<=nch; j++) {
556            printf(format, m[i][j]);
557        }
558        printf("\n");
559    }
560    printf("\n");
561}
562/* ----------------------------------------------------------------------------------------------------------------- */
563IMAGE_EXPORT(void) display_si64matrix_number(sint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
564/* ----------------------------------------------------------------------------------------------------------------- */
565{
566    int i,j;
567   
568    if(name != NULL) printf(name);
569   
570    // 1ere ligne
571    printf("%5c", '#');
572    for(j=ncl; j<=nch; j++) {
573        printf(format, j);
574    }
575    printf("\n");
576    for(i=nrl; i<=nrh; i++) {
577        printf("[%3d]", i);
578        for(j=ncl; j<=nch; j++) {
579            printf(format, m[i][j]);
580        }
581        printf("\n");
582    }
583    printf("\n");
584}
585/* ----------------------------------------------------------------------------------------------------------------- */
586IMAGE_EXPORT(void) display_ui64matrix_number(uint64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
587/* ----------------------------------------------------------------------------------------------------------------- */
588{
589    int i,j;
590   
591    if(name != NULL) printf(name);
592   
593    // 1ere ligne
594    printf("%5c", '#');
595    for(j=ncl; j<=nch; j++) {
596        printf(format, j);
597    }
598    printf("\n");
599    for(i=nrl; i<=nrh; i++) {
600        printf("[%3d]", i);
601        for(j=ncl; j<=nch; j++) {
602            printf(format, m[i][j]);
603        }
604        printf("\n");
605    }
606    printf("\n");
607}
608/* ----------------------------------------------------------------------------------------------------------------- */
609IMAGE_EXPORT(void) display_f32matrix_number(float32 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
610/* ----------------------------------------------------------------------------------------------------------------- */
611{
612    int i,j;
613   
614    if(name != NULL) printf(name);
615   
616    // 1ere ligne
617    printf("%5c", '#');
618    for(j=ncl; j<=nch; j++) {
619        printf(format, j);
620    }
621    printf("\n");
622    for(i=nrl; i<=nrh; i++) {
623        printf("[%3d]", i);
624        for(j=ncl; j<=nch; j++) {
625            printf(format, m[i][j]);
626        }
627        printf("\n");
628    }
629    printf("\n");
630}
631/* ----------------------------------------------------------------------------------------------------------------- */
632IMAGE_EXPORT(void) display_f64matrix_number(float64 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
633/* ----------------------------------------------------------------------------------------------------------------- */
634{
635    int i,j;
636   
637    if(name != NULL) printf(name);
638   
639    // 1ere ligne
640    printf("%5c", '#');
641    for(j=ncl; j<=nch; j++) {
642        printf(format, j);
643    }
644    printf("\n");
645    for(i=nrl; i<=nrh; i++) {
646        printf("[%3d]", i);
647        for(j=ncl; j<=nch; j++) {
648            printf(format, m[i][j]);
649        }
650        printf("\n");
651    }
652    printf("\n");
653}
654/* --------------------------------------------------------------------------------------------------------------- */
655IMAGE_EXPORT(void) display_rgb8matrix_number(rgb8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
656/* --------------------------------------------------------------------------------------------------------------- */
657{
658    int i,j;
659   
660    if(name != NULL) printf(name);
661   
662    // 1ere ligne
663    printf("%5c", '#');
664    for(j=ncl; j<=nch; j++) {
665        printf(format, j);
666    }
667    printf("\n");
668    for(i=nrl; i<=nrh; i++) {
669        printf("[%3d]", i);
670        for(j=ncl; j<=nch; j++) {
671            printf(format, m[i][j].r, m[i][j].g, m[i][j].b);
672        }
673        printf("\n");
674    }
675    printf("\n");
676}
677/* ----------------------------------------------------------------------------------------------------------------- */
678IMAGE_EXPORT(void) display_rgbx8matrix_number(rgbx8 **m,long nrl,long nrh,long ncl, long nch, char *format, char *name)
679/* ----------------------------------------------------------------------------------------------------------------- */
680{
681    int i,j;
682   
683    if(name != NULL) printf(name);
684   
685    // 1ere ligne
686    printf("%5c", '#');
687    for(j=ncl; j<=nch; j++) {
688        printf(format, j);
689    }
690    printf("\n");
691    for(i=nrl; i<=nrh; i++) {
692        printf("[%3d]", i);
693        for(j=ncl; j<=nch; j++) {
694            printf(format, m[i][j].r, m[i][j].g, m[i][j].b, m[i][j].x);
695        }
696        printf("\n");
697    }
698    printf("\n");
699}
700
701/*
702 * -------------------------------
703 * --- display_matrix_positive ---
704 * -------------------------------
705 */
706
707// ------------------------------------------------------------------------------------------------
708void display_si8matrix_positive(sint8 **m, int i0, int i1, int j0, int j1, int iformat, char *name)
709// ------------------------------------------------------------------------------------------------
710{
711    int i, j;
712   
713    char *format, *str;
714   
715    select_display_positive_parameters(iformat, &format, &str);
716   
717    if(name != NULL) printf(name);
718   
719    for(i=i0; i<=i1; i++) {
720        for(j=j0; j<=j1; j++) {
721            if(m[i][j]) {
722                printf(format, m[i][j]);
723            } else {
724                printf("%s", str);
725            }
726        }
727        printf("\n");
728    }   
729}
730// ------------------------------------------------------------------------------------------------
731void display_ui8matrix_positive(uint8 **m, int i0, int i1, int j0, int j1, int iformat, char *name)
732// ------------------------------------------------------------------------------------------------
733{
734    int i, j;
735   
736    char *format, *str;
737   
738    select_display_positive_parameters(iformat, &format, &str);
739   
740    if(name != NULL) printf(name);
741   
742    for(i=i0; i<=i1; i++) {
743        for(j=j0; j<=j1; j++) {
744            if(m[i][j]) {
745                printf(format, m[i][j]);
746            } else {
747                printf("%s", str);
748            }
749        }
750        printf("\n");
751    }   
752}
753// --------------------------------------------------------------------------------------------------
754void display_si16matrix_positive(sint16 **m, int i0, int i1, int j0, int j1, int iformat, char *name)
755// --------------------------------------------------------------------------------------------------
756{
757    int i, j;
758   
759    char *format, *str;
760   
761    select_display_positive_parameters(iformat, &format, &str);
762   
763    if(name != NULL) printf(name);
764   
765    for(i=i0; i<=i1; i++) {
766        for(j=j0; j<=j1; j++) {
767            if(m[i][j]) {
768                printf(format, m[i][j]);
769            } else {
770                printf("%s", str);
771            }
772        }
773        printf("\n");
774    }   
775}
776// --------------------------------------------------------------------------------------------------
777void display_ui16matrix_positive(uint16 **m, int i0, int i1, int j0, int j1, int iformat, char *name)
778// --------------------------------------------------------------------------------------------------
779{
780    int i, j;
781    char *format, *str;
782   
783    select_display_positive_parameters(iformat, &format, &str);
784   
785    if(name != NULL) printf(name);
786   
787    for(i=i0; i<=i1; i++) {
788        for(j=j0; j<=j1; j++) {
789            if(m[i][j]) {
790                printf(format, m[i][j]);
791            } else {
792                printf("%s", str);
793            }
794        }
795        printf("\n");
796    }   
797}
798// --------------------------------------------------------------------------------------------------
799void display_si32matrix_positive(sint32 **m, int i0, int i1, int j0, int j1, int iformat, char *name)
800// --------------------------------------------------------------------------------------------------
801{
802    int i, j;
803    char *format, *str;
804   
805    select_display_positive_parameters(iformat, &format, &str);
806   
807    if(name != NULL) printf(name);
808   
809    for(i=i0; i<=i1; i++) {
810        for(j=j0; j<=j1; j++) {
811            if(m[i][j]) {
812                printf(format, m[i][j]);
813            } else {
814                printf("%s", str);
815            }
816        }
817        printf("\n");
818    }   
819}
820// --------------------------------------------------------------------------------------------------
821void display_ui32matrix_positive(uint32 **m, int i0, int i1, int j0, int j1, int iformat, char *name)
822// --------------------------------------------------------------------------------------------------
823{
824    int i, j;
825    char *format, *str;
826   
827    select_display_positive_parameters(iformat, &format, &str);
828   
829    if(name != NULL) printf(name);
830   
831    for(i=i0; i<=i1; i++) {
832        for(j=j0; j<=j1; j++) {
833            if(m[i][j]) {
834                printf(format, m[i][j]);
835            } else {
836                printf("%s", str);
837            }
838        }
839        printf("\n");
840    }   
841}
842// --------------------------------------------------------------------------------------------------
843void display_si64matrix_positive(sint64 **m, int i0, int i1, int j0, int j1, int iformat, char *name)
844// --------------------------------------------------------------------------------------------------
845{
846    int i, j;
847    char *format, *str;
848   
849    select_display_positive_parameters(iformat, &format, &str);
850   
851    if(name != NULL) printf(name);
852   
853    for(i=i0; i<=i1; i++) {
854        for(j=j0; j<=j1; j++) {
855            if(m[i][j]) {
856                printf(format, m[i][j]);
857            } else {
858                printf("%s", str);
859            }
860        }
861        printf("\n");
862    }   
863}
864// --------------------------------------------------------------------------------------------------
865void display_ui64matrix_positive(uint64 **m, int i0, int i1, int j0, int j1, int iformat, char *name)
866// --------------------------------------------------------------------------------------------------
867{
868    int i, j;
869    char *format, *str;
870   
871    select_display_positive_parameters(iformat, &format, &str);
872   
873    if(name != NULL) printf(name);
874   
875    for(i=i0; i<=i1; i++) {
876        for(j=j0; j<=j1; j++) {
877            if(m[i][j]) {
878                printf(format, m[i][j]);
879            } else {
880                printf("%s", str);
881            }
882        }
883        printf("\n");
884    }   
885}
886
887
888
889/* --------------------------------------- */
890static char * readitem(int fd, char * buffer)
891/* --------------------------------------- */
892{
893    char * aux;
894    int k;
895    int n;
896   
897    k = 0;
898    aux = buffer;
899    while (1) {
900        n = read(fd, aux, 1);
901        if (n == 0) {
902            break;
903        }
904        switch (k) {
905            case 0:
906                if (*aux == '#') {
907                    k = 1;
908                }
909                if (isalnum(*aux)) {
910                    k = 2;
911                    aux++;
912                }
913                break;
914            case 1:
915                if (*aux == 0xA) {
916                    k = 0;
917                }
918                break;
919            case 2:
920                if (!isalnum(*aux)) {
921                    *aux = 0;
922                    return buffer;
923                }
924                aux++;
925                break;
926        }
927    }
928    *aux = 0;
929    return buffer;
930}
931
932
933/* ------------------------------------------------- */
934static void ReadPGMrow(int fd, int width, uint8 * line)
935/* ------------------------------------------------- */
936{
937    read(fd, &line[0], sizeof(uint8) * width);
938}
939
940
941/* -------------------------------------------------- */
942static void WritePGMrow(uint8 * line, int width, int fd)
943/* -------------------------------------------------- */
944{
945    write(fd, &line[0], sizeof(uint8) * width);
946}
947
948
949/* ----------------------------------------------------------------------------------------------- */
950IMAGE_EXPORT(uint8 **) LoadPGM_ui8matrix(char * filename, int * nrl, int * nrh, int * ncl, int * nch)
951/* ----------------------------------------------------------------------------------------------- */
952{
953    // only for P5 binary type, not for text type
954   
955    int height, width, gris;
956    uint8 ** m;
957    int fd;
958   
959    char buffer[80];
960    int i;
961   
962    // open file
963    fd = open(filename, O_RDONLY);
964    if (fd < 0) {
965        printf("*** Error: Can't open file %s in %s.\n", filename, __func__);
966    }
967   
968    // read PGM header
969    readitem(fd, &buffer[0]);
970    if (strcmp(&buffer[0], "P5") != 0) {
971        printf("*** Error: Invalid file header in file %s\n", filename);
972    }
973   
974    width  = atoi(readitem(fd, &buffer[0]));
975    height = atoi(readitem(fd, &buffer[0]));
976    gris   = atoi(readitem(fd, &buffer[0]));
977   
978    *nrl = 0;
979    *nrh = height - 1;
980    *ncl = 0;
981    *nch = width - 1;
982    m = ui8matrix(*nrl, *nrh, *ncl, *nch);
983   
984    for (i = 0; i < height; i++) {
985        ReadPGMrow(fd, width, m[i]);
986    }
987   
988    close(fd);
989   
990    return m;
991}
992
993
994/* ----------------------------------------------------------------------------------------------- */
995IMAGE_EXPORT(void) SavePGM_ui8matrix(uint8 ** m, int nrl, int nrh, int ncl, int nch, char * filename)
996/* ----------------------------------------------------------------------------------------------- */
997{
998    int nrow = nrh - nrl + 1;
999    int ncol = nch - ncl + 1;
1000   
1001    char buffer[80];
1002   
1003    int fd;
1004    int i;
1005   
1006    fd = open(filename, O_TRUNC | O_CREAT);
1007    if (fd < 0) {
1008        printf("*** Error: Impossible to open file %s in %s\n", filename, __func__);
1009    }
1010   
1011    /* enregistrement de l'image au format rpgm */
1012    snprintf(buffer, 80, "P5\n%d %d\n255\n", ncol, nrow);
1013    write(fd, buffer, strlen(buffer));
1014    for (i = nrl; i <= nrh; i++) {
1015        WritePGMrow(m[i], ncol, fd);
1016    }
1017   
1018    /* fermeture du fichier */
1019    close(fd);
1020}
1021
1022
1023
Note: See TracBrowser for help on using the repository browser.