GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
sig.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <grass/imagery.h>
3 
4 int I_init_signatures(struct Signature *S, int nbands)
5 {
6  S->nbands = nbands;
7  S->nsigs = 0;
8  S->sig = NULL;
9  S->title[0] = 0;
10 
11  return 0;
12 }
13 
14 #define SIG struct One_Sig
15 
16 int I_new_signature(struct Signature *S)
17 {
18  int n;
19  int i;
20 
21  i = S->nsigs++;
22  S->sig = (SIG *) G_realloc(S->sig, S->nsigs * sizeof(SIG));
23 
24  S->sig[i].mean = (double *)G_calloc(S->nbands, sizeof(double));
25  S->sig[i].var = (double **)G_calloc(S->nbands, sizeof(double *));
26 
27  for (n = 0; n < S->nbands; n++)
28  S->sig[i].var[n] = (double *)G_calloc(S->nbands, sizeof(double));
29 
30  S->sig[i].status = 0;
31  S->sig[i].have_color = 0;
32  sprintf(S->sig[i].desc, "Class %d", i + 1);;
33  return S->nsigs;
34 }
35 
37 {
38  int n;
39  int i;
40 
41  for (i = 0; i < S->nsigs; i++) {
42  for (n = 0; n < S->nbands; n++)
43  free(S->sig[i].var[n]);
44  free(S->sig[i].var);
45  free(S->sig[i].mean);
46  }
47  I_init_signatures(S, 0);
48 
49  return 0;
50 }
51 
52 int I_read_one_signature(FILE * fd, struct Signature *S)
53 {
54  int n;
55  int i;
56  struct One_Sig *s;
57 
58  while ((i = fgetc(fd)) != EOF)
59  if (i == '#')
60  break;
61  if (i != '#')
62  return 0;
63 
64  i = I_new_signature(S);
65  s = &S->sig[i - 1];
66 
67  I_get_to_eol(s->desc, sizeof(s->desc), fd);
68  G_strip(s->desc);
69 
70  if (fscanf(fd, "%d", &s->npoints) != 1)
71  return -1;
72 
73  for (i = 0; i < S->nbands; i++) {
74  if (fscanf(fd, "%lf", &s->mean[i]) != 1)
75  return -1;
76  }
77 
78  for (i = 0; i < S->nbands; i++) {
79  for (n = 0; n <= i; n++) {
80  if (fscanf(fd, "%lf", &s->var[i][n]) != 1)
81  return -1;
82  s->var[n][i] = s->var[i][n]; /* added 28 aug 91 */
83  }
84  }
85  if (fscanf(fd, "%f%f%f", &s->r, &s->g, &s->b) == 3 &&
86  s->r >= 0.0 && s->r <= 1.0 &&
87  s->g >= 0.0 && s->g <= 1.0 && s->b >= 0.0 && s->b <= 1.0)
88  s->have_color = 1;
89 
90  s->status = 1;
91  return 1;
92 }
93 
94 int I_read_signatures(FILE * fd, struct Signature *S)
95 {
96  int n;
97 
98  S->title[0] = 0;
99  while ((n = fgetc(fd)) != EOF)
100  if (n == '#')
101  break;
102  if (n != '#')
103  return -1;
104  I_get_to_eol(S->title, sizeof(S->title), fd);
105  G_strip(S->title);
106 
107  while ((n = I_read_one_signature(fd, S)) == 1) ;
108 
109  if (n < 0)
110  return -1;
111  if (S->nsigs == 0)
112  return -1;
113  return 1;
114 }
115 
116 int I_write_signatures(FILE * fd, struct Signature *S)
117 {
118  int k;
119  int n;
120  int i;
121  struct One_Sig *s;
122 
123  fprintf(fd, "#%s\n", S->title);
124  for (k = 0; k < S->nsigs; k++) {
125  s = &S->sig[k];
126  if (s->status != 1)
127  continue;
128  fprintf(fd, "#%s\n", s->desc);
129  fprintf(fd, "%d\n", s->npoints);
130  for (i = 0; i < S->nbands; i++)
131  fprintf(fd, "%g ", s->mean[i]);
132  fprintf(fd, "\n");
133  for (i = 0; i < S->nbands; i++) {
134  for (n = 0; n <= i; n++)
135  fprintf(fd, "%g ", s->var[i][n]);
136  fprintf(fd, "\n");
137  }
138  if (s->have_color)
139  fprintf(fd, "%g %g %g\n", s->r, s->g, s->b);
140  }
141  return 1;
142 }
int I_new_signature(struct Signature *S)
Definition: sig.c:16
double ** var
Definition: imagery.h:57
int npoints
Definition: imagery.h:55
char desc[100]
Definition: imagery.h:54
void G_strip(char *)
Removes all leading and trailing white space from string.
Definition: strings.c:300
int I_read_signatures(FILE *fd, struct Signature *S)
Definition: sig.c:94
void free(void *)
#define NULL
Definition: ccmath.h:32
#define SIG
Definition: sig.c:14
int have_color
Definition: imagery.h:60
char title[100]
Definition: imagery.h:67
#define G_calloc(m, n)
Definition: defs/gis.h:113
float g
Definition: imagery.h:59
float b
Definition: imagery.h:59
int I_init_signatures(struct Signature *S, int nbands)
Definition: sig.c:4
int nbands
Definition: imagery.h:65
int I_write_signatures(FILE *fd, struct Signature *S)
Definition: sig.c:116
int nsigs
Definition: imagery.h:66
int I_free_signatures(struct Signature *S)
Definition: sig.c:36
int status
Definition: imagery.h:58
#define G_realloc(p, n)
Definition: defs/gis.h:114
struct One_Sig * sig
Definition: imagery.h:68
double * mean
Definition: imagery.h:56
int I_get_to_eol(char *, int, FILE *)
Definition: eol.c:12
int I_read_one_signature(FILE *fd, struct Signature *S)
Definition: sig.c:52
float r
Definition: imagery.h:59