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;
36 for (i = 0, count = 0; i < S->
nclasses; i++)
52 Data->
x = alloc_matrix(npixels, S->
nbands);
69 for (
int i = 0; i < nbands; i++)
114 for (i = 1; i < S->
nbands; i++)
115 Sp->
R[i] = Sp->
R[i - 1] + S->
nbands;
118 for (i = 1; i < S->
nbands; i++)
127 #define eq(a,b) strcmp(a,b)==0 147 unsigned int version;
149 if (fscanf(fd,
"%u", &version) != 1) {
154 G_warning(
_(
"Invalid signature file version"));
159 while (gettag(fd, tag)) {
160 if (
eq(tag,
"title:"))
161 if (get_title(fd, S) != 0)
163 if (
eq(tag,
"semantic_labels:"))
164 if (get_semantic_labels(fd, S) != 0)
166 if (
eq(tag,
"class:"))
167 if (get_class(fd, S) != 0)
173 static int gettag(FILE * fd,
char *tag)
175 if (fscanf(fd,
"%255s", tag) != 1)
181 static int get_semantic_labels(FILE * fd,
struct SigSet *S)
191 while ((c = (
char)fgetc(fd)) != EOF) {
194 semantic_label[pos] =
'\0';
195 if (strlen(semantic_label) > 0) {
204 semantic_label[pos] =
'\0';
205 if (strlen(semantic_label) > 0) {
211 (n + 1) *
sizeof(
char **));
220 G_warning(
_(
"Invalid signature file: semantic label length limit exceeded"));
223 semantic_label[pos] = c;
228 G_warning(
_(
"Signature file does not contain bands"));
235 static int get_title(FILE * fd,
struct SigSet *S)
240 if (fscanf(fd,
"%1024[^\n]", title) != 1)
248 static int get_class(FILE * fd,
struct SigSet *S)
254 while (gettag(fd, tag)) {
255 if (
eq(tag,
"endclass:"))
257 if (
eq(tag,
"classnum:"))
258 if (get_classnum(fd, C) != 0)
260 if (
eq(tag,
"classtype:"))
261 if (get_classtype(fd, C) != 0)
263 if (
eq(tag,
"classtitle:"))
264 if (get_classtitle(fd, C) != 0)
266 if (
eq(tag,
"subclass:"))
267 if (get_subclass(fd, S, C) != 0)
274 static int get_classnum(FILE * fd,
struct ClassSig *C)
276 if (fscanf(fd,
"%ld", &C->
classnum) != 1)
282 static int get_classtype(FILE * fd,
struct ClassSig *C)
284 if (fscanf(fd,
"%d", &C->
type) != 1)
290 static int get_classtitle(FILE * fd,
struct ClassSig *C)
295 if (fscanf(fd,
"%1024[^\n]", title) != 1)
303 static int get_subclass(FILE * fd,
struct SigSet *S,
struct ClassSig *C)
310 while (gettag(fd, tag)) {
311 if (
eq(tag,
"endsubclass:"))
314 if (get_subclass_pi(fd, Sp) != 0)
316 if (
eq(tag,
"means:"))
317 if (get_subclass_means(fd, Sp, S->
nbands) != 0)
319 if (
eq(tag,
"covar:"))
320 if (get_subclass_covar(fd, Sp, S->
nbands) != 0)
327 static int get_subclass_pi(FILE * fd,
struct SubSig *Sp)
329 if (fscanf(fd,
"%lf", &Sp->
pi) != 1)
335 static int get_subclass_means(FILE * fd,
struct SubSig *Sp,
int nbands)
339 for (i = 0; i < nbands; i++)
340 if (fscanf(fd,
"%lf", &Sp->
means[i]) != 1)
346 static int get_subclass_covar(FILE * fd,
struct SubSig *Sp,
int nbands)
350 for (i = 0; i < nbands; i++)
351 for (j = 0; j < nbands; j++)
352 if (fscanf(fd,
"%lf", &Sp->
R[i][j]) != 1)
405 fprintf(fd,
"semantic_labels: ");
406 for (i = 0; i < S->
nbands; i++) {
416 fprintf(fd,
"class:\n");
417 fprintf(fd,
" classnum: %ld\n", Cp->
classnum);
419 fprintf(fd,
" classtype: %d\n", Cp->
type);
423 fprintf(fd,
" subclass:\n");
424 fprintf(fd,
" pi: %g\n", Sp->
pi);
425 fprintf(fd,
" means:");
426 for (b1 = 0; b1 < S->
nbands; b1++)
427 fprintf(fd,
" %g", Sp->
means[b1]);
429 fprintf(fd,
" covar:\n");
430 for (b1 = 0; b1 < S->
nbands; b1++) {
432 for (b2 = 0; b2 < S->
nbands; b2++)
433 fprintf(fd,
" %g", Sp->
R[b1][b2]);
436 fprintf(fd,
" endsubclass:\n");
438 fprintf(fd,
"endclass:\n");
477 unsigned int total, complete;
478 unsigned int *match1, *match2, mc1, mc2, *new_order;
479 double ***new_means, ****new_vars;
480 char **group_semantic_labels, **mismatches, **new_semantic_labels;
484 G_fatal_error(
"Programming error. Invalid length structs passed to " 485 "I_sort_signatures_by_semantic_label(%d, %d);", S->
nbands,
489 group_semantic_labels = (
char **)
G_malloc(R->
nfiles *
sizeof(
char *));
490 for (
unsigned int j = R->
nfiles; j--;) {
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--;) {
518 for (
unsigned int i = S->
nbands; i--;)
525 for (
unsigned int j = R->
nfiles; j--;) {
526 for (
unsigned int i = S->
nbands; i--;) {
544 mismatches = (
char **)
G_malloc(2 *
sizeof(
char **));
545 mismatches[0] =
NULL;
546 mismatches[1] =
NULL;
548 for (
unsigned int i = 0; i < S->
nbands; i++) {
555 (
char *)
G_realloc(mismatches[0], total *
sizeof(
char *));
557 strcat(mismatches[0],
",");
559 mismatches[0][0] =
'\0';
563 strcat(mismatches[0],
"<semantic label missing>");
569 for (
unsigned int j = 0; j < R->
nfiles; j++) {
571 if (group_semantic_labels[j])
572 total = total + strlen(group_semantic_labels[j]);
576 (
char *)
G_realloc(mismatches[1], total *
sizeof(
char *));
578 strcat(mismatches[1],
",");
580 mismatches[1][0] =
'\0';
581 if (group_semantic_labels[j])
582 strcat(mismatches[1], group_semantic_labels[j]);
584 strcat(mismatches[1],
"<semantic label missing>");
592 for (
unsigned int c = S->
nclasses; c--;) {
594 for (
unsigned int b1 = 0; b1 < S->
nbands; b1++) {
595 new_means[c][s][new_order[b1]] =
597 for (
unsigned int b2 = 0; b2 < S->
nbands; b2++) {
598 new_vars[c][s][new_order[b1]][new_order[b2]] =
607 for (
unsigned int c = S->
nclasses; c--;) {
610 S->
nbands *
sizeof(
double));
611 for (
unsigned int i = S->
nbands; i--;)
613 S->
nbands *
sizeof(
double));
619 for (
unsigned int j = R->
nfiles; j--;)
620 free(group_semantic_labels[j]);
621 free(group_semantic_labels);
625 free(new_semantic_labels);
626 for (
unsigned int c = S->
nclasses; c--;) {
628 free(new_means[c][s]);
629 for (
unsigned int i = S->
nbands; i--;)
630 free(new_vars[c][s][i]);
631 free(new_vars[c][s]);
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
const char * I_GetSigTitle(const struct SigSet *S)
#define SIGNATURE_TYPE_MIXED
void G_strip(char *)
Removes all leading and trailing white space from string.
int I_SetClassTitle(struct ClassSig *C, const char *title)
int I_SetSigTitle(struct SigSet *S, const char *title)
char ** I_SortSigSetBySemanticLabel(struct SigSet *S, const struct Ref *R)
Reorder struct SigSet to match imagery group member order.
struct ClassSig * I_NewClassSig(struct SigSet *S)
struct ClassSig * ClassSig
int I_SigSetNClasses(struct SigSet *S)
struct ClassData * I_AllocClassData(struct SigSet *S, struct ClassSig *C, int npixels)
int I_WriteSigSet(FILE *fd, const struct SigSet *S)
void G_warning(const char *,...) __attribute__((format(printf
struct SubSig * I_NewSubSig(struct SigSet *S, struct ClassSig *C)
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_ReadSigSet(FILE *fd, struct SigSet *S)
Read sigset signatures from file.
const char * I_GetClassTitle(const struct ClassSig *C)
int I_InitSigSet(struct SigSet *S, int nbands)
Initialize struct SigSet before use.
struct ClassData ClassData