GRASS Programmer's Manual  6.4.2(2012)
group.c
Go to the documentation of this file.
00001 
00002 /****************************************************************************
00003  *
00004  * MODULE:       imagery library
00005  * AUTHOR(S):    Original author(s) name(s) unknown - written by CERL
00006  * PURPOSE:      Image processing library
00007  * COPYRIGHT:    (C) 1999, 2005 by the GRASS Development Team
00008  *
00009  *               This program is free software under the GNU General Public
00010  *               License (>=v2). Read the file COPYING that comes with GRASS
00011  *               for details.
00012  *
00013  *****************************************************************************/
00014 
00015 /**********************************************************
00016 * I_get_group (group);
00017 * I_put_group (group);
00018 *
00019 * I_get_group_ref (group, &Ref);
00020 * I_put_group_ref (group, &Ref);
00021 * I_get_subgroup_ref_file (group, subgroup, &Ref);
00022 * I_put_subgroup_ref_file (group, subgroup, &Ref);
00023 * I_add_file_to_group_ref (name, mapset, &Ref)
00024 * I_transfer_group_ref_file (&Src_ref, n, &Dst_ref)
00025 * I_init_group_ref (&Ref);
00026 * I_free_group_ref (&Ref);
00027 **********************************************************/
00028 
00029 #include <string.h>
00030 #include <stdlib.h>
00031 #include <grass/imagery.h>
00032 
00033 static int get_ref(const char *, const char *, struct Ref *);
00034 static int set_color(const char *, const char *, const char *, struct Ref *);
00035 static int put_ref(const char *, const char *, const struct Ref *);
00036 
00037 /* get current group name from file GROUPFILE in current mapset */
00038 int I_get_group(char *group)
00039 {
00040     FILE *fd;
00041     int stat;
00042 
00043     *group = 0;
00044     G_suppress_warnings(1);
00045     fd = G_fopen_old("", GROUPFILE, G_mapset());
00046     G_suppress_warnings(0);
00047     if (fd == NULL)
00048         return 0;
00049     stat = (fscanf(fd, "%s", group) == 1);
00050     fclose(fd);
00051     return stat;
00052 }
00053 
00054 /* write group name to file GROUPFILE in current mapset */
00055 int I_put_group(const char *group)
00056 {
00057     FILE *fd;
00058 
00059     fd = G_fopen_new("", GROUPFILE);
00060     if (fd == NULL)
00061         return 0;
00062     fprintf(fd, "%s\n", group);
00063     fclose(fd);
00064     return 1;
00065 }
00066 
00067 /* get current subgroup for group in current mapset */
00068 int I_get_subgroup(const char *group, char *subgroup)
00069 {
00070     FILE *fd;
00071     int stat;
00072 
00073     *subgroup = 0;
00074     if (!I_find_group(group))
00075         return 0;
00076     G_suppress_warnings(1);
00077     fd = I_fopen_group_file_old(group, SUBGROUPFILE);
00078     G_suppress_warnings(0);
00079     if (fd == NULL)
00080         return 0;
00081     stat = (fscanf(fd, "%s", subgroup) == 1);
00082     fclose(fd);
00083     return stat;
00084 }
00085 
00086 /* write current subgroup to group in current mapset */
00087 int I_put_subgroup(const char *group, const char *subgroup)
00088 {
00089     FILE *fd;
00090 
00091     if (!I_find_group(group))
00092         return 0;
00093     fd = I_fopen_group_file_new(group, SUBGROUPFILE);
00094     if (fd == NULL)
00095         return 0;
00096     fprintf(fd, "%s\n", subgroup);
00097     fclose(fd);
00098     return 1;
00099 }
00100 
00101 
00114 int I_get_group_ref(const char *group, struct Ref *ref)
00115 {
00116     return get_ref(group, "", ref);
00117 }
00118 
00119 
00134 int I_get_subgroup_ref(const char *group,
00135                        const char *subgroup, struct Ref *ref)
00136 {
00137     return get_ref(group, subgroup, ref);
00138 }
00139 
00140 static int get_ref(const char *group, const char *subgroup, struct Ref *ref)
00141 {
00142     int n;
00143     char buf[1024];
00144     char name[INAME_LEN], mapset[INAME_LEN];
00145     char color[20];
00146     FILE *fd;
00147 
00148     I_init_group_ref(ref);
00149 
00150     G_suppress_warnings(1);
00151     if (*subgroup == 0)
00152         fd = I_fopen_group_ref_old(group);
00153     else
00154         fd = I_fopen_subgroup_ref_old(group, subgroup);
00155     G_suppress_warnings(0);
00156     if (!fd)
00157         return 0;
00158 
00159     while (G_getl2(buf, sizeof buf, fd)) {
00160         n = sscanf(buf, "%255s %255s %15s", name, mapset, color);  /* better use INAME_LEN */
00161         if (n == 2 || n == 3) {
00162             I_add_file_to_group_ref(name, mapset, ref);
00163             if (n == 3)
00164                 set_color(name, mapset, color, ref);
00165         }
00166     }
00167     /* make sure we have a color assignment */
00168     I_init_ref_color_nums(ref);
00169 
00170     fclose(fd);
00171     return 1;
00172 }
00173 
00174 static int set_color(const char *name, const char *mapset, const char *color,
00175                      struct Ref *ref)
00176 {
00177     int n;
00178 
00179     for (n = 0; n < ref->nfiles; n++) {
00180         if (strcmp(ref->file[n].name, name) == 0
00181             && strcmp(ref->file[n].mapset, mapset) == 0)
00182             break;
00183     }
00184 
00185     if (n < ref->nfiles)
00186         while (*color) {
00187             switch (*color) {
00188             case 'r':
00189             case 'R':
00190                 if (ref->red.n < 0)
00191                     ref->red.n = n;
00192                 break;
00193             case 'g':
00194             case 'G':
00195                 if (ref->grn.n < 0)
00196                     ref->grn.n = n;
00197                 break;
00198             case 'b':
00199             case 'B':
00200                 if (ref->blu.n < 0)
00201                     ref->blu.n = n;
00202                 break;
00203             }
00204             color++;
00205         }
00206 
00207     return 0;
00208 }
00209 
00210 int I_init_ref_color_nums(struct Ref *ref)
00211 {
00212     ref->red.table = NULL;
00213     ref->grn.table = NULL;
00214     ref->blu.table = NULL;
00215 
00216     ref->red.index = NULL;
00217     ref->grn.index = NULL;
00218     ref->blu.index = NULL;
00219 
00220     if (ref->nfiles <= 0 || ref->red.n >= 0 || ref->blu.n >= 0 ||
00221         ref->blu.n >= 0)
00222         return 1;
00223     switch (ref->nfiles) {
00224     case 1:
00225         ref->red.n = 0;
00226         ref->grn.n = 0;
00227         ref->blu.n = 0;
00228         break;
00229     case 2:
00230         ref->blu.n = 0;
00231         ref->grn.n = 1;
00232         break;
00233     case 3:
00234         ref->blu.n = 0;
00235         ref->grn.n = 1;
00236         ref->red.n = 2;
00237         break;
00238     case 4:
00239         ref->blu.n = 0;
00240         ref->grn.n = 1;
00241         ref->red.n = 3;
00242         break;
00243     default:
00244         ref->blu.n = 1;
00245         ref->grn.n = 2;
00246         ref->red.n = 4;
00247         break;
00248     }
00249 
00250     return 0;
00251 }
00252 
00253 
00268 int I_put_group_ref(const char *group, const struct Ref *ref)
00269 {
00270     return put_ref(group, "", ref);
00271 }
00272 
00273 
00290 int I_put_subgroup_ref(const char *group, const char *subgroup,
00291                        const struct Ref *ref)
00292 {
00293     return put_ref(group, subgroup, ref);
00294 }
00295 
00296 static int put_ref(const char *group, const char *subgroup,
00297                    const struct Ref *ref)
00298 {
00299     int n;
00300     FILE *fd;
00301 
00302     if (*subgroup == 0)
00303         fd = I_fopen_group_ref_new(group);
00304     else
00305         fd = I_fopen_subgroup_ref_new(group, subgroup);
00306     if (!fd)
00307         return 0;
00308 
00309     for (n = 0; n < ref->nfiles; n++) {
00310         fprintf(fd, "%s %s", ref->file[n].name, ref->file[n].mapset);
00311         if (n == ref->red.n || n == ref->grn.n || n == ref->blu.n) {
00312             fprintf(fd, " ");
00313             if (n == ref->red.n)
00314                 fprintf(fd, "r");
00315             if (n == ref->grn.n)
00316                 fprintf(fd, "g");
00317             if (n == ref->blu.n)
00318                 fprintf(fd, "b");
00319         }
00320         fprintf(fd, "\n");
00321     }
00322     fclose(fd);
00323     return 1;
00324 }
00325 
00326 
00346 int I_add_file_to_group_ref(const char *name, const char *mapset,
00347                             struct Ref *ref)
00348 {
00349     int n;
00350 
00351     for (n = 0; n < ref->nfiles; n++) {
00352         if (strcmp(ref->file[n].name, name) == 0
00353             && strcmp(ref->file[n].mapset, mapset) == 0)
00354             return n;
00355     }
00356 
00357     if ((n = ref->nfiles++))
00358         ref->file =
00359             (struct Ref_Files *)G_realloc(ref->file,
00360                                           ref->nfiles *
00361                                           sizeof(struct Ref_Files));
00362     else
00363         ref->file =
00364             (struct Ref_Files *)G_malloc(ref->nfiles *
00365                                          sizeof(struct Ref_Files));
00366     strcpy(ref->file[n].name, name);
00367     strcpy(ref->file[n].mapset, mapset);
00368     return n;
00369 }
00370 
00371 
00398 int I_transfer_group_ref_file(const struct Ref *ref2, int n, struct Ref *ref1)
00399 {
00400     int k;
00401 
00402     /* insert old name into new ref */
00403     k = I_add_file_to_group_ref(ref2->file[n].name, ref2->file[n].mapset,
00404                                 ref1);
00405 
00406     /* preserve color assignment */
00407     if (n == ref2->red.n)
00408         ref1->red.n = k;
00409     if (n == ref2->grn.n)
00410         ref1->grn.n = k;
00411     if (n == ref2->blu.n)
00412         ref1->blu.n = k;
00413 
00414     return 0;
00415 }
00416 
00417 
00418 
00433 int I_init_group_ref(struct Ref *ref)
00434 {
00435     ref->nfiles = 0;
00436     ref->red.n = ref->grn.n = ref->blu.n = -1;
00437     ref->red.table = ref->grn.table = ref->blu.table = NULL;
00438 
00439     return 0;
00440 }
00441 
00442 
00452 int I_free_group_ref(struct Ref *ref)
00453 {
00454     if (ref->nfiles > 0)
00455         free(ref->file);
00456     ref->nfiles = 0;
00457 
00458     return 0;
00459 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines