18 for (
int i = 0; i < nbands; i++)
28 #define SIG struct One_Sig
41 for (n = 0; n < S->
nbands; n++)
46 sprintf(S->
sig[i].
desc,
"Class %d", i + 1);
65 for (i = 0; i < S->
nsigs; i++) {
66 for (n = 0; n < S->
nbands; n++)
72 for (n = 0; n < S->
nbands; n++)
94 while ((i = fgetc(fd)) != EOF)
106 if (fscanf(fd,
"%d", &s->
npoints) != 1)
112 for (i = 0; i < S->
nbands; i++) {
113 if (fscanf(fd,
"%lf", &s->
mean[i]) != 1)
117 for (i = 0; i < S->
nbands; i++) {
118 for (n = 0; n <= i; n++) {
119 if (fscanf(fd,
"%lf", &s->
var[i][n]) != 1)
121 s->
var[n][i] = s->
var[i][n];
124 if (fscanf(fd,
"%f%f%f", &s->
r, &s->
g, &s->
b) == 3 && s->
r >= 0.0 &&
125 s->
r <= 1.0 && s->
g >= 0.0 && s->
g <= 1.0 && s->
b >= 0.0 && s->
b <= 1.0)
156 if (fscanf(fd,
"%d", &ver) != 1) {
161 if (!(ver == 1 || ver == 2)) {
162 G_warning(
_(
"Invalid signature file version"));
167 while ((c = (
char)fgetc(fd)) != EOF)
178 while ((c = (
char)fgetc(fd)) != EOF) {
181 semantic_label[pos] =
'\0';
189 semantic_label[pos] =
'\0';
194 (n + 1) *
sizeof(
char **));
202 G_warning(
_(
"Invalid signature file: semantic label length limit "
206 semantic_label[pos] = c;
212 G_warning(
_(
"Signature file does not contain bands"));
217 if (ver >= 2 && fscanf(fd,
"%d", &S->
have_oclass) != 1) {
218 G_warning(
_(
"Invalid signature file: Original class value presence not "
257 fprintf(fd,
"#%s\n", S->
title);
260 for (k = 0; k < S->
nbands; k++) {
267 for (k = 0; k < S->
nsigs; k++) {
272 fprintf(fd,
"#%s\n", s->
desc);
274 fprintf(fd,
"%d\n", s->
npoints);
277 fprintf(fd,
"%d\n", s->
oclass);
279 for (i = 0; i < S->
nbands; i++)
280 fprintf(fd,
"%g ", s->
mean[i]);
282 for (i = 0; i < S->
nbands; i++) {
283 for (n = 0; n <= i; n++)
284 fprintf(fd,
"%g ", s->
var[i][n]);
288 fprintf(fd,
"%g %g %g\n", s->
r, s->
g, s->
b);
326 unsigned int total, complete;
327 unsigned int *match1, *match2, mc1, mc2, *new_order;
328 double **new_means, ***new_vars;
329 char **group_semantic_labels, **mismatches, **new_semantic_labels;
333 G_fatal_error(
"Programming error. Invalid length structs passed to "
334 "I_sort_signatures_by_semantic_label(%d, %d);",
338 group_semantic_labels = (
char **)
G_malloc(R->
nfiles *
sizeof(
char *));
339 for (
unsigned int j = R->
nfiles; j--;) {
340 group_semantic_labels[j] =
348 new_order = (
unsigned int *)
G_malloc(S->
nbands *
sizeof(
unsigned int));
349 match1 = (
unsigned int *)
G_calloc(S->
nbands,
sizeof(
unsigned int));
350 match2 = (
unsigned int *)
G_calloc(R->
nfiles,
sizeof(
unsigned int));
353 new_semantic_labels = (
char **)
G_malloc(S->
nbands *
sizeof(
char *));
354 new_means = (
double **)
G_malloc(S->
nsigs *
sizeof(
double *));
356 new_vars = (
double ***)
G_malloc(S->
nsigs *
sizeof(
double **));
357 for (
unsigned int c = S->
nsigs; c--;) {
360 for (
unsigned int i = S->
nbands; i--;)
365 for (
unsigned int j = R->
nfiles; j--;) {
366 for (
unsigned int i = S->
nbands; i--;) {
384 mismatches = (
char **)
G_malloc(2 *
sizeof(
char **));
385 mismatches[0] =
NULL;
386 mismatches[1] =
NULL;
388 for (
unsigned int i = 0; i < (
unsigned int)S->
nbands; i++) {
395 (
char *)
G_realloc(mismatches[0], total *
sizeof(
char *));
397 strcat(mismatches[0],
",");
399 mismatches[0][0] =
'\0';
403 strcat(mismatches[0],
"<semantic label missing>");
409 for (
unsigned int j = 0; j < (
unsigned int)R->
nfiles; j++) {
411 if (group_semantic_labels[j])
412 total = total + strlen(group_semantic_labels[j]);
416 (
char *)
G_realloc(mismatches[1], total *
sizeof(
char *));
418 strcat(mismatches[1],
",");
420 mismatches[1][0] =
'\0';
421 if (group_semantic_labels[j])
422 strcat(mismatches[1], group_semantic_labels[j]);
424 strcat(mismatches[1],
"<semantic label missing>");
432 for (
unsigned int c = S->
nsigs; c--;) {
433 for (
unsigned int b1 = 0; b1 < (
unsigned int)S->
nbands; b1++) {
434 new_means[c][new_order[b1]] = S->
sig[c].
mean[b1];
435 for (
unsigned int b2 = 0; b2 <= b1; b2++) {
436 if (new_order[b1] > new_order[b2]) {
437 new_vars[c][new_order[b1]][new_order[b2]] =
441 new_vars[c][new_order[b2]][new_order[b1]] =
450 S->
nbands *
sizeof(
char **));
451 for (
unsigned int c = S->
nsigs; c--;) {
452 memcpy(S->
sig[c].
mean, new_means[c], S->
nbands *
sizeof(
double));
453 for (
unsigned int i = S->
nbands; i--;)
454 memcpy(S->
sig[c].
var[i], new_vars[c][i],
455 S->
nbands *
sizeof(
double));
460 for (
unsigned int j = R->
nfiles; j--;)
461 free(group_semantic_labels[j]);
462 free(group_semantic_labels);
466 free(new_semantic_labels);
467 for (
unsigned int c = S->
nsigs; c--;) {
469 for (
unsigned int i = S->
nbands; i--;)
470 free(new_vars[c][i]);
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void G_strip(char *)
Removes all leading and trailing white space from string.
char * G_store(const char *)
Copy string to allocated memory.
int I_get_to_eol(char *, int, FILE *)
char * Rast_get_semantic_label_or_name(const char *, const char *)
Get a raster map semantic label or fall back to its name.
int I_write_signatures(FILE *fd, struct Signature *S)
Write signatures to file.
int I_read_signatures(FILE *fd, struct Signature *S)
Read signatures from file.
int I_new_signature(struct Signature *S)
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_init_signatures(struct Signature *S, int nbands)
Initialize struct Signature before use.
int I_read_one_signature(FILE *fd, struct Signature *S)
Internal function for I_read_signatures.
int I_free_signatures(struct Signature *S)
Free memory allocated for struct Signature.