Main Page   Compound List   File List   Compound Members   File Members   Related Pages  

geo_tiffp.c

00001 /**********************************************************************
00002  *
00003  *  geo_tiffp.c  Private TIFF interface module for GEOTIFF
00004  *
00005  *    This module implements the interface between the GEOTIFF
00006  *    tag parser and the TIFF i/o module. The current setup
00007  *    relies on the "libtiff" code, but if you use your own
00008  *    TIFF reader software, you may replace the module implementations
00009  *    here with your own calls. No "libtiff" dependencies occur
00010  *    anywhere else in this code.
00011  *
00012  **********************************************************************/
00013  
00014 #include "geotiff.h"    /* public GTIFF interface */
00015 
00016 #include "geo_tiffp.h"  /* Private TIFF interface */
00017 #include "geo_keyp.h"   /* Private GTIFF interface */
00018 
00019 /* tiff size array global */
00020 gsize_t _gtiff_size[] = { 0, 1, 2, 4, 8, 1, 4, 8, 1, 2, 4, 1 };
00021 
00022 static int        _GTIFGetField (tiff_t *tif, pinfo_t tag, int *count, void *value );
00023 static int        _GTIFSetField (tiff_t *tif, pinfo_t tag, int  count, void *value );
00024 static tagtype_t  _GTIFTagType  (tiff_t *tif, pinfo_t tag);
00025 
00026 /*
00027  * Set up default TIFF handlers. 
00028  */
00029 void _GTIFSetDefaultTIFF(TIFFMethod *method)
00030 {
00031         if (!method) return;
00032         
00033         method->get = _GTIFGetField;
00034         method->set = _GTIFSetField;
00035         method->type = _GTIFTagType;
00036 }
00037 
00038 gdata_t _GTIFcalloc(gsize_t size)
00039 {
00040     gdata_t data=(gdata_t)_TIFFmalloc((tsize_t)size);
00041         if (data) _TIFFmemset((tdata_t)data,0,(tsize_t)size);
00042         return data;
00043 }
00044 
00045 gdata_t _GTIFrealloc(gdata_t ptr, gsize_t size)
00046 {
00047     return( _TIFFrealloc((tdata_t)ptr, (tsize_t) size) );
00048 }
00049 
00050 void _GTIFmemcpy(gdata_t out,gdata_t in,gsize_t size)
00051 {
00052         _TIFFmemcpy((tdata_t)out,(tdata_t)in,(tsize_t)size);
00053 }
00054 
00055 void _GTIFFree(gdata_t data)
00056 {
00057         if (data) _TIFFfree((tdata_t)data);
00058 }
00059 
00060 
00061 
00062 /* returns the value of TIFF tag <tag>, or if
00063  * the value is an array, returns an allocated buffer
00064  * containing the values. Allocate a copy of the actual
00065  * buffer, sized up for updating.
00066  */
00067 static int _GTIFGetField (tiff_t *tif, pinfo_t tag, int *count, void *val )
00068 {
00069         int status;
00070         unsigned short scount=0;
00071         char *tmp;
00072         char *value;
00073         gsize_t size = _gtiff_size[_GTIFTagType (tif,tag)];
00074         
00075         if (_GTIFTagType(tif,  tag) == TYPE_ASCII)
00076         {
00077                 status = TIFFGetField((TIFF *)tif,tag,&tmp);
00078                 if (!status) return status;
00079                 scount = strlen(tmp)+1;
00080         }
00081         else status = TIFFGetField((TIFF *)tif,tag,&scount,&tmp);
00082         if (!status) return status;
00083         
00084         *count = scount;
00085 
00086         value = (char *)_GTIFcalloc( (scount+MAX_VALUES)*size);
00087         if (!value) return 0;
00088         
00089         _TIFFmemcpy( value, tmp,  size * scount);
00090         
00091         *(char **)val = value;
00092         return status;
00093 }
00094 
00095 /* 
00096  * Set a GeoTIFF TIFF field.
00097  */
00098 static int _GTIFSetField (tiff_t *tif, pinfo_t tag, int count, void *value )
00099 {
00100         int status;
00101         unsigned short scount = count;
00102 
00103         /* libtiff ASCII uses null-delimiter */
00104         if (_GTIFTagType(tif,  tag) == TYPE_ASCII)
00105                 status = TIFFSetField((TIFF *)tif,tag,value);
00106         else 
00107                 status = TIFFSetField((TIFF *)tif,tag,scount,value);
00108         return status;
00109 }
00110 
00111 
00112 /*
00113  *  This routine is supposed to return the TagType of the <tag>
00114  *  TIFF tag. Unfortunately, "libtiff" does not provide this
00115  *  service by default, so we just have to "know" what type of tags
00116  *  we've got, and how many. We only define the ones Geotiff
00117  *  uses here, and others return UNKNOWN. The "tif" parameter
00118  *  is provided for those TIFF implementations that provide
00119  *  for tag-type queries.
00120  */
00121 static tagtype_t  _GTIFTagType  (tiff_t *tif, pinfo_t tag)
00122 {
00123         tagtype_t ttype;
00124 
00125         (void) tif; /* dummy reference */
00126         
00127         switch (tag)
00128         {
00129                 case GTIFF_ASCIIPARAMS:    ttype=TYPE_ASCII; break;
00130                 case GTIFF_PIXELSCALE:
00131                 case GTIFF_TRANSMATRIX:
00132                 case GTIFF_TIEPOINTS:
00133                 case GTIFF_DOUBLEPARAMS:   ttype=TYPE_DOUBLE; break;
00134                 case GTIFF_GEOKEYDIRECTORY: ttype=TYPE_SHORT; break;
00135                 default: ttype = TYPE_UNKNOWN;
00136         }
00137         
00138         return ttype;
00139 }
00140 

Generated at Sun Mar 4 23:32:44 2001 for libgeotiff by doxygen1.2.3-20001105 written by Dimitri van Heesch, © 1997-2000