7 static int gettag(FILE *,
char *);
8 static int get_semantic_labels(FILE *,
struct SigSet *);
9 static int get_title(FILE *,
struct SigSet *);
10 static int get_class(FILE *,
struct SigSet *);
11 static int get_classnum(FILE *,
struct ClassSig *);
12 static int get_classtype(FILE *,
struct ClassSig *);
13 static int get_classtitle(FILE *,
struct ClassSig *);
14 static int get_subclass(FILE *,
struct SigSet *,
struct ClassSig *);
15 static int get_subclass_pi(FILE *,
struct SubSig *);
16 static int get_subclass_means(FILE *,
struct SubSig *,
int);
17 static int get_subclass_covar(FILE *,
struct SubSig *,
int);
19 static double **alloc_matrix(
int rows,
int cols)
24 m = (
double **)
G_calloc(rows,
sizeof(
double *));
25 m[0] = (
double *)
G_calloc(rows * cols,
sizeof(
double));
26 for (i = 1; i < rows; i++)
27 m[i] = m[i - 1] + cols;
68 for (
int i = 0; i < nbands; i++)
111 for (i = 1; i < S->
nbands; i++)
112 Sp->
R[i] = Sp->
R[i - 1] + S->
nbands;
115 for (i = 1; i < S->
nbands; i++)
124 #define eq(a, b) strcmp(a, b) == 0
144 unsigned int version;
146 if (fscanf(fd,
"%u", &version) != 1) {
151 G_warning(
_(
"Invalid signature file version"));
156 while (gettag(fd, tag)) {
157 if (
eq(tag,
"title:"))
158 if (get_title(fd, S) != 0)
160 if (
eq(tag,
"semantic_labels:"))
161 if (get_semantic_labels(fd, S) != 0)
163 if (
eq(tag,
"class:"))
164 if (get_class(fd, S) != 0)
170 static int gettag(FILE *fd,
char *tag)
172 if (fscanf(fd,
"%255s", tag) != 1)
178 static int get_semantic_labels(FILE *fd,
struct SigSet *S)
189 while ((c = (
char)fgetc(fd)) != EOF) {
192 semantic_label[pos] =
'\0';
193 if (strlen(semantic_label) > 0) {
202 semantic_label[pos] =
'\0';
203 if (strlen(semantic_label) > 0) {
217 G_warning(
_(
"Invalid signature file: semantic label length limit "
221 semantic_label[pos] = c;
226 G_warning(
_(
"Signature file does not contain bands"));
233 static int get_title(FILE *fd,
struct SigSet *S)
238 if (fscanf(fd,
"%1023[^\n]", title) != 1)
246 static int get_class(FILE *fd,
struct SigSet *S)
252 while (gettag(fd, tag)) {
253 if (
eq(tag,
"endclass:"))
255 if (
eq(tag,
"classnum:"))
256 if (get_classnum(fd, C) != 0)
258 if (
eq(tag,
"classtype:"))
259 if (get_classtype(fd, C) != 0)
261 if (
eq(tag,
"classtitle:"))
262 if (get_classtitle(fd, C) != 0)
264 if (
eq(tag,
"subclass:"))
265 if (get_subclass(fd, S, C) != 0)
272 static int get_classnum(FILE *fd,
struct ClassSig *C)
274 if (fscanf(fd,
"%ld", &C->
classnum) != 1)
280 static int get_classtype(FILE *fd,
struct ClassSig *C)
282 if (fscanf(fd,
"%d", &C->
type) != 1)
288 static int get_classtitle(FILE *fd,
struct ClassSig *C)
293 if (fscanf(fd,
"%1023[^\n]",
title) != 1)
301 static int get_subclass(FILE *fd,
struct SigSet *S,
struct ClassSig *C)
308 while (gettag(fd, tag)) {
309 if (
eq(tag,
"endsubclass:"))
312 if (get_subclass_pi(fd, Sp) != 0)
314 if (
eq(tag,
"means:"))
315 if (get_subclass_means(fd, Sp, S->
nbands) != 0)
317 if (
eq(tag,
"covar:"))
318 if (get_subclass_covar(fd, Sp, S->
nbands) != 0)
325 static int get_subclass_pi(FILE *fd,
struct SubSig *Sp)
327 if (fscanf(fd,
"%lf", &Sp->
pi) != 1)
333 static int get_subclass_means(FILE *fd,
struct SubSig *Sp,
int nbands)
337 for (i = 0; i < nbands; i++)
338 if (fscanf(fd,
"%lf", &Sp->
means[i]) != 1)
344 static int get_subclass_covar(FILE *fd,
struct SubSig *Sp,
int nbands)
348 for (i = 0; i < nbands; i++)
349 for (j = 0; j < nbands; j++)
350 if (fscanf(fd,
"%lf", &Sp->
R[i][j]) != 1)
403 fprintf(fd,
"semantic_labels: ");
404 for (i = 0; i < S->
nbands; i++) {
414 fprintf(fd,
"class:\n");
415 fprintf(fd,
" classnum: %ld\n", Cp->
classnum);
417 fprintf(fd,
" classtype: %d\n", Cp->
type);
421 fprintf(fd,
" subclass:\n");
422 fprintf(fd,
" pi: %g\n", Sp->
pi);
423 fprintf(fd,
" means:");
424 for (b1 = 0; b1 < S->
nbands; b1++)
425 fprintf(fd,
" %g", Sp->
means[b1]);
427 fprintf(fd,
" covar:\n");
428 for (b1 = 0; b1 < S->
nbands; b1++) {
430 for (b2 = 0; b2 < S->
nbands; b2++)
431 fprintf(fd,
" %g", Sp->
R[b1][b2]);
434 fprintf(fd,
" endsubclass:\n");
436 fprintf(fd,
"endclass:\n");
476 unsigned int total, complete;
477 unsigned int *match1, *match2, mc1, mc2, *new_order;
478 double ***new_means, ****new_vars;
479 char **group_semantic_labels, **mismatches, **new_semantic_labels;
482 if (S->
nbands < 1 ||
R->nfiles < 1)
483 G_fatal_error(
"Programming error. Invalid length structs passed to "
484 "I_sort_signatures_by_semantic_label(%d, %d);",
488 group_semantic_labels = (
char **)
G_malloc(
R->nfiles *
sizeof(
char *));
489 for (
unsigned int j =
R->nfiles; j--;) {
490 group_semantic_labels[j] =
495 complete = S->
nbands ==
R->nfiles;
498 new_order = (
unsigned int *)
G_malloc(S->
nbands *
sizeof(
unsigned int));
499 match1 = (
unsigned int *)
G_calloc(S->
nbands,
sizeof(
unsigned int));
500 match2 = (
unsigned int *)
G_calloc(
R->nfiles,
sizeof(
unsigned int));
503 new_semantic_labels = (
char **)
G_malloc(S->
nbands *
sizeof(
char *));
507 for (
unsigned int c = S->
nclasses; c--;) {
514 new_vars[c][s] = (
double **)
G_malloc(S->
nbands *
sizeof(
double *));
515 for (
unsigned int i = S->
nbands; i--;)
522 for (
unsigned int j =
R->nfiles; j--;) {
523 for (
unsigned int i = S->
nbands; i--;) {
541 mismatches = (
char **)
G_malloc(2 *
sizeof(
char **));
542 mismatches[0] =
NULL;
543 mismatches[1] =
NULL;
545 for (
unsigned int i = 0; i < (
unsigned int)S->
nbands; i++) {
552 (
char *)
G_realloc(mismatches[0], total *
sizeof(
char *));
554 strcat(mismatches[0],
",");
556 mismatches[0][0] =
'\0';
560 strcat(mismatches[0],
"<semantic label missing>");
566 for (
unsigned int j = 0; j < (
unsigned int)
R->nfiles; j++) {
568 if (group_semantic_labels[j])
569 total = total + strlen(group_semantic_labels[j]);
573 (
char *)
G_realloc(mismatches[1], total *
sizeof(
char *));
575 strcat(mismatches[1],
",");
577 mismatches[1][0] =
'\0';
578 if (group_semantic_labels[j])
579 strcat(mismatches[1], group_semantic_labels[j]);
581 strcat(mismatches[1],
"<semantic label missing>");
589 for (
unsigned int c = S->
nclasses; c--;) {
591 for (
unsigned int b1 = 0; b1 < (
unsigned int)S->
nbands; b1++) {
592 new_means[c][s][new_order[b1]] =
594 for (
unsigned int b2 = 0; b2 < (
unsigned int)S->
nbands;
596 new_vars[c][s][new_order[b1]][new_order[b2]] =
605 S->
nbands *
sizeof(
char **));
606 for (
unsigned int c = S->
nclasses; c--;) {
609 S->
nbands *
sizeof(
double));
610 for (
unsigned int i = S->
nbands; i--;)
612 S->
nbands *
sizeof(
double));
618 for (
unsigned int j =
R->nfiles; j--;)
619 free(group_semantic_labels[j]);
620 free(group_semantic_labels);
624 free(new_semantic_labels);
625 for (
unsigned int c = S->
nclasses; c--;) {
627 free(new_means[c][s]);
628 for (
unsigned int i = S->
nbands; i--;)
629 free(new_vars[c][s][i]);
630 free(new_vars[c][s]);
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.
char * Rast_get_semantic_label_or_name(const char *, const char *)
Get a raster map semantic label or fall back to its name.
#define SIGNATURE_TYPE_MIXED
int I_SetSigTitle(struct SigSet *S, const char *title)
struct ClassSig * I_NewClassSig(struct SigSet *S)
int I_ReadSigSet(FILE *fd, struct SigSet *S)
Read sigset signatures from file.
const char * I_GetSigTitle(const struct SigSet *S)
int I_SigSetNClasses(struct SigSet *S)
int I_SetClassTitle(struct ClassSig *C, const char *title)
struct ClassData * I_AllocClassData(struct SigSet *S, struct ClassSig *C, int npixels)
int I_InitSigSet(struct SigSet *S, int nbands)
Initialize struct SigSet before use.
struct SubSig * I_NewSubSig(struct SigSet *S, struct ClassSig *C)
const char * I_GetClassTitle(const struct ClassSig *C)
char ** I_SortSigSetBySemanticLabel(struct SigSet *S, const struct Ref *R)
Reorder struct SigSet to match imagery group member order.
int I_WriteSigSet(FILE *fd, const struct SigSet *S)
struct ClassData ClassData
struct ClassSig * ClassSig