18 for (
int i = 0; i < nbands; i++)
27 #define SIG struct One_Sig 40 for (n = 0; n < S->
nbands; n++)
45 sprintf(S->
sig[i].
desc,
"Class %d", i + 1);
64 for (i = 0; i < S->
nsigs; i++) {
65 for (n = 0; n < S->
nbands; n++)
71 for (n = 0; n < S->
nbands; n++)
90 while ((i = fgetc(fd)) != EOF)
102 if (fscanf(fd,
"%d", &s->
npoints) != 1)
105 for (i = 0; i < S->
nbands; i++) {
106 if (fscanf(fd,
"%lf", &s->
mean[i]) != 1)
110 for (i = 0; i < S->
nbands; i++) {
111 for (n = 0; n <= i; n++) {
112 if (fscanf(fd,
"%lf", &s->
var[i][n]) != 1)
114 s->
var[n][i] = s->
var[i][n];
117 if (fscanf(fd,
"%f%f%f", &s->
r, &s->
g, &s->
b) == 3 &&
118 s->
r >= 0.0 && s->
r <= 1.0 &&
119 s->
g >= 0.0 && s->
g <= 1.0 && s->
b >= 0.0 && s->
b <= 1.0)
150 if (fscanf(fd,
"%d", &ver) != 1) {
156 G_warning(
_(
"Invalid signature file version"));
161 while ((c = (
char)fgetc(fd)) != EOF)
171 while ((c = (
char)fgetc(fd)) != EOF) {
174 semantic_label[pos] =
'\0';
182 semantic_label[pos] =
'\0';
194 G_warning(
_(
"Invalid signature file: semantic label length limit exceeded"));
197 semantic_label[pos] = c;
203 G_warning(
_(
"Signature file does not contain bands"));
240 fprintf(fd,
"#%s\n", S->
title);
243 for (k = 0; k < S->
nbands; k++) {
248 for (k = 0; k < S->
nsigs; k++) {
253 fprintf(fd,
"#%s\n", s->
desc);
255 fprintf(fd,
"%d\n", s->
npoints);
257 for (i = 0; i < S->
nbands; i++)
258 fprintf(fd,
"%g ", s->
mean[i]);
260 for (i = 0; i < S->
nbands; i++) {
261 for (n = 0; n <= i; n++)
262 fprintf(fd,
"%g ", s->
var[i][n]);
266 fprintf(fd,
"%g %g %g\n", s->
r, s->
g, s->
b);
301 unsigned int total, complete;
302 unsigned int *match1, *match2, mc1, mc2, *new_order;
303 double **new_means, ***new_vars;
304 char **group_semantic_labels, **mismatches, **new_semantic_labels;
308 G_fatal_error(
"Programming error. Invalid length structs passed to " 309 "I_sort_signatures_by_semantic_label(%d, %d);", S->
nbands, R->
nfiles);
312 group_semantic_labels = (
char **)
G_malloc(R->
nfiles *
sizeof(
char *));
313 for (
unsigned int j = R->
nfiles; j--;) {
321 new_order = (
unsigned int *)
G_malloc(S->
nbands *
sizeof(
unsigned int));
322 match1 = (
unsigned int *)
G_calloc(S->
nbands,
sizeof(
unsigned int));
323 match2 = (
unsigned int *)
G_calloc(R->
nfiles,
sizeof(
unsigned int));
326 new_semantic_labels = (
char **)
G_malloc(S->
nbands *
sizeof(
char *));
327 new_means = (
double **)
G_malloc(S->
nsigs *
sizeof(
double *));
329 new_vars = (
double ***)
G_malloc(S->
nsigs *
sizeof(
double **));
330 for (
unsigned int c = S->
nsigs; c--;) {
333 for (
unsigned int i = S->
nbands; i--;)
338 for (
unsigned int j = R->
nfiles; j--;) {
339 for (
unsigned int i = S->
nbands; i--;) {
357 mismatches = (
char **)
G_malloc(2 *
sizeof(
char **));
358 mismatches[0] =
NULL;
359 mismatches[1] =
NULL;
361 for (
unsigned int i = 0; i < S->
nbands; i++) {
367 mismatches[0] = (
char *)
G_realloc(mismatches[0], total *
sizeof(
char *));
369 strcat(mismatches[0],
",");
371 mismatches[0][0] =
'\0';
375 strcat(mismatches[0],
"<semantic label missing>");
381 for (
unsigned int j = 0; j < R->
nfiles; j++) {
383 if (group_semantic_labels[j])
384 total = total + strlen(group_semantic_labels[j]);
387 mismatches[1] = (
char *)
G_realloc(mismatches[1], total *
sizeof(
char *));
389 strcat(mismatches[1],
",");
391 mismatches[1][0] =
'\0';
392 if (group_semantic_labels[j])
393 strcat(mismatches[1], group_semantic_labels[j]);
395 strcat(mismatches[1],
"<semantic label missing>");
403 for (
unsigned int c = S->
nsigs; c--;) {
404 for (
unsigned int b1 = 0; b1 < S->
nbands; b1++) {
405 new_means[c][new_order[b1]] = S->
sig[c].
mean[b1];
406 for (
unsigned int b2 = 0; b2 <= b1; b2++) {
407 if (new_order[b1] > new_order[b2]) {
408 new_vars[c][new_order[b1]][new_order[b2]] = S->
sig[c].
var[b1][b2];
411 new_vars[c][new_order[b2]][new_order[b1]] = S->
sig[c].
var[b1][b2];
419 for (
unsigned int c = S->
nsigs; c--;) {
420 memcpy(S->
sig[c].
mean, new_means[c], S->
nbands *
sizeof(
double));
421 for (
unsigned int i = S->
nbands; i--;)
422 memcpy(S->
sig[c].
var[i], new_vars[c][i], S->
nbands *
sizeof(
double));
427 for (
unsigned int j = R->
nfiles; j--;)
428 free(group_semantic_labels[j]);
429 free(group_semantic_labels);
433 free(new_semantic_labels);
434 for (
unsigned int c = S->
nsigs; c--;) {
436 for (
unsigned int i = S->
nbands; i--;)
437 free(new_vars[c][i]);
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int I_new_signature(struct Signature *S)
void G_strip(char *)
Removes all leading and trailing white space from string.
int I_read_signatures(FILE *fd, struct Signature *S)
Read signatures from file.
int I_init_signatures(struct Signature *S, int nbands)
Initialize struct Signature before use.
int I_write_signatures(FILE *fd, struct Signature *S)
Write signatures to file.
char ** I_sort_signatures_by_semantic_label(struct Signature *S, const struct Ref *R)
Reorder struct Signature to match imagery group member order.
int I_free_signatures(struct Signature *S)
Free memory allocated for struct Signature.
void G_warning(const char *,...) __attribute__((format(printf
char * Rast_get_semantic_label_or_name(const char *, const char *)
Get a raster map semantic label or fall back to its name.
char * G_store(const char *)
Copy string to allocated memory.
int I_get_to_eol(char *, int, FILE *)
int I_read_one_signature(FILE *fd, struct Signature *S)