"Fossies" - the Fresh Open Source Software Archive

Member "libgeotiff-1.6.0/geo_set.c" (15 Jun 2019, 6615 Bytes) of package /linux/privat/libgeotiff-1.6.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "geo_set.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.5.1_vs_1.6.0.

    1 /**********************************************************************
    2  *
    3  *  geo_set.c  -- Public routines for GEOTIFF GeoKey access.
    4  *
    5  *    Written By: Niles D. Ritter.
    6  *
    7  *  copyright (c) 1995   Niles D. Ritter
    8  *
    9  *  Permission granted to use this software, so long as this copyright
   10  *  notice accompanies any products derived therefrom.
   11  *
   12  **********************************************************************/
   13 
   14 #include "geotiff.h"   /* public interface        */
   15 #include "geo_tiffp.h" /* external TIFF interface */
   16 #include "geo_keyp.h"  /* private interface       */
   17 
   18 #include <assert.h>
   19 
   20 /**
   21 This function writes a geokey_t value to a GeoTIFF file.
   22 
   23 @param gtif The geotiff information handle from GTIFNew().
   24 
   25 @param keyID The geokey_t name (such as ProjectedCSTypeGeoKey).
   26 This must come from the list of legal geokey_t values
   27 (an enumeration) listed below.
   28 
   29 @param type Type of the key.
   30 
   31 @param count Indicates how many values
   32 to read.  At this time all keys except for strings have only one value,
   33 so <b>index</b> should be zero, and <b>count</b> should be one.<p>
   34 
   35 The <b>keyID</b> indicates the key name to be written to the
   36 file and should from the geokey_t enumeration
   37 (eg. <tt>ProjectedCSTypeGeoKey</tt>).  The full list of possible geokey_t
   38 values can be found in geokeys.inc, or in the online documentation for
   39 GTIFKeyGet().<p>
   40 
   41 The <b>type</b> should be one of TYPE_SHORT, TYPE_ASCII, or TYPE_DOUBLE and
   42 will indicate the type of value being passed at the end of the argument
   43 list (the key value).  The <b>count</b> should be one except for strings
   44 when it should be the length of the string (or zero to for this to be
   45 computed internally).  As a special case a <b>count</b> of -1 can be
   46 used to request an existing key be deleted, in which no value is passed.<p>
   47 
   48 The actual value is passed at the end of the argument list, and should be
   49 a short, a double, or a char * value.  Note that short and double values
   50 are passed by value rather than as pointers when count is 1, but as pointers
   51 if count is larger than 1.<p>
   52 
   53 Note that key values aren't actually flushed to the file until
   54 GTIFWriteKeys() is called.  Till then
   55 the new values are just kept with the GTIF structure.<p>
   56 
   57 <b>Example:</b><p>
   58 
   59 <pre>
   60     GTIFKeySet(gtif, GTRasterTypeGeoKey, TYPE_SHORT, 1,
   61                RasterPixelIsArea);
   62     GTIFKeySet(gtif, GTCitationGeoKey, TYPE_ASCII, 0,
   63                "UTM 11 North / NAD27" );
   64 </pre>
   65 
   66  */
   67 
   68 int GTIFKeySet(GTIF *gtif, geokey_t keyID, tagtype_t type, int count,...)
   69 {
   70     va_list ap;
   71     int nIndex = gtif->gt_keyindex[ keyID ];
   72     GeoKey *key;
   73     char *data = NULL;
   74     char *val = NULL;
   75     pinfo_t sval;
   76     double dval;
   77 
   78     va_start(ap, count);
   79     /* pass singleton keys by value */
   80     if (count>1 && type!=TYPE_ASCII)
   81     {
   82         val = va_arg(ap, char*);
   83     }
   84     else if( count == -1 )
   85     {
   86         /* delete the indicated tag */
   87         va_end(ap);
   88 
   89         if( nIndex < 1 )
   90             return 0;
   91 
   92         if (gtif->gt_keys[nIndex].gk_type == TYPE_ASCII)
   93         {
   94             _GTIFFree (gtif->gt_keys[nIndex].gk_data);
   95         }
   96 
   97         while( nIndex < gtif->gt_num_keys )
   98         {
   99             _GTIFmemcpy( gtif->gt_keys + nIndex,
  100                          gtif->gt_keys + nIndex + 1,
  101                          sizeof(GeoKey) );
  102             gtif->gt_keyindex[gtif->gt_keys[nIndex].gk_key] = nIndex;
  103             nIndex++;
  104         }
  105 
  106         gtif->gt_num_keys--;
  107         gtif->gt_nshorts -= sizeof(KeyEntry)/sizeof(pinfo_t);
  108         gtif->gt_keyindex[keyID] = 0;
  109         gtif->gt_flags |= FLAG_FILE_MODIFIED;
  110 
  111         return 1;
  112     }
  113     else switch (type)
  114     {
  115       case TYPE_SHORT:
  116         /* cppcheck-suppress unreadVariable */
  117         sval=(pinfo_t) va_arg(ap, int);
  118         val=(char *)&sval;
  119         break;
  120       case TYPE_DOUBLE:
  121         /* cppcheck-suppress unreadVariable */
  122         dval=va_arg(ap, dblparam_t);
  123         val=(char *)&dval;
  124         break;
  125       case TYPE_ASCII:
  126         val=va_arg(ap, char*);
  127         count = (int)strlen(val) + 1; /* force = string length */
  128         break;
  129       default:
  130         assert( FALSE );
  131         break;
  132     }
  133     va_end(ap);
  134 
  135     /* We assume here that there are no multi-valued SHORTS ! */
  136     if (nIndex)
  137     {
  138         /* Key already exists */
  139         key = gtif->gt_keys+nIndex;
  140         if (type!=key->gk_type || count > key->gk_count)
  141         {
  142             /* need to reset data pointer */
  143             key->gk_type = type;
  144             key->gk_count = count;
  145             key->gk_size = _gtiff_size[ type ];
  146 
  147             if( type == TYPE_DOUBLE )
  148             {
  149                 key->gk_data = (char *)(gtif->gt_double + gtif->gt_ndoubles);
  150                 gtif->gt_ndoubles += count;
  151             }
  152         }
  153     }
  154     else
  155     {
  156         /* We need to create the key */
  157         if (gtif->gt_num_keys == MAX_KEYS) return 0;
  158         key = gtif->gt_keys + ++gtif->gt_num_keys;
  159         nIndex = gtif->gt_num_keys;
  160         gtif->gt_keyindex[ keyID ] = nIndex;
  161         key->gk_key = keyID;
  162         key->gk_type = type;
  163         key->gk_count = count;
  164         key->gk_size = _gtiff_size[ type ];
  165         if ((geokey_t)gtif->gt_keymin > keyID)  gtif->gt_keymin=keyID;
  166         if ((geokey_t)gtif->gt_keymax < keyID)  gtif->gt_keymax=keyID;
  167         gtif->gt_nshorts += sizeof(KeyEntry)/sizeof(pinfo_t);
  168         if( type == TYPE_DOUBLE )
  169         {
  170             key->gk_data = (char *)(gtif->gt_double + gtif->gt_ndoubles);
  171             gtif->gt_ndoubles += count;
  172         }
  173     }
  174 
  175     switch (type)
  176     {
  177         case TYPE_SHORT:
  178             if (count > 1) return 0;
  179             data = (char *)&key->gk_data; /* store value *in* data */
  180             break;
  181         case TYPE_DOUBLE:
  182             data = key->gk_data;
  183             break;
  184         case TYPE_ASCII:
  185             /* throw away existing data and allocate room for new data */
  186             if (key->gk_data != 0)
  187             {
  188                 _GTIFFree(key->gk_data);
  189             }
  190             key->gk_data = (char *)_GTIFcalloc(count);
  191             key->gk_count = count;
  192             data = key->gk_data;
  193             break;
  194         default:
  195             return 0;
  196     }
  197 
  198     _GTIFmemcpy(data, val, count*key->gk_size);
  199 
  200     gtif->gt_flags |= FLAG_FILE_MODIFIED;
  201     return 1;
  202 }
  203 
  204 /* Set the version numbers of the GeoTIFF directory */
  205 int  GTIFSetVersionNumbers(GTIF* gtif,
  206                            unsigned short version,
  207                            unsigned short key_revision,
  208                            unsigned short minor_revision)
  209 {
  210     gtif->gt_version = version;
  211     gtif->gt_rev_major = key_revision;
  212     gtif->gt_rev_minor = minor_revision;
  213     return 1;
  214 }