-
Notifications
You must be signed in to change notification settings - Fork 2
/
geo_tiffp.c
145 lines (119 loc) · 3.87 KB
/
geo_tiffp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**********************************************************************
*
* geo_tiffp.c Private TIFF interface module for GEOTIFF
*
* This module implements the interface between the GEOTIFF
* tag parser and the TIFF i/o module. The current setup
* relies on the "libtiff" code, but if you use your own
* TIFF reader software, you may replace the module implementations
* here with your own calls. No "libtiff" dependencies occur
* anywhere else in this code.
*
* copyright (c) 1995 Niles D. Ritter
*
* Permission granted to use this software, so long as this copyright
* notice accompanies any products derived therefrom.
*
**********************************************************************/
#include "geotiff.h" /* public GTIFF interface */
#include "geo_tiffp.h" /* Private TIFF interface */
#include "geo_keyp.h" /* Private GTIFF interface */
/* tiff size array global */
gsize_t _gtiff_size[] = { 0, 1, 2, 4, 8, 1, 4, 8, 1, 2, 4, 1 };
static int _GTIFGetField (tiff_t *tif, pinfo_t tag, int *count, void *value );
static int _GTIFSetField (tiff_t *tif, pinfo_t tag, int count, void *value );
static tagtype_t _GTIFTagType (tiff_t *tif, pinfo_t tag);
/*
* Set up default TIFF handlers.
*/
void _GTIFSetDefaultTIFF(TIFFMethod *method)
{
if (!method) return;
method->get = _GTIFGetField;
method->set = _GTIFSetField;
method->type = _GTIFTagType;
}
gdata_t _GTIFcalloc(gsize_t size)
{
gdata_t data=(gdata_t)_TIFFmalloc((tsize_t)size);
if (data) _TIFFmemset((tdata_t)data,0,(tsize_t)size);
return data;
}
gdata_t _GTIFrealloc(gdata_t ptr, gsize_t size)
{
return( _TIFFrealloc((tdata_t)ptr, (tsize_t) size) );
}
void _GTIFmemcpy(gdata_t out,gdata_t in,gsize_t size)
{
_TIFFmemcpy((tdata_t)out,(tdata_t)in,(tsize_t)size);
}
void _GTIFFree(gdata_t data)
{
if (data) _TIFFfree((tdata_t)data);
}
/* returns the value of TIFF tag <tag>, or if
* the value is an array, returns an allocated buffer
* containing the values. Allocate a copy of the actual
* buffer, sized up for updating.
*/
static int _GTIFGetField (tiff_t *tif, pinfo_t tag, int *count, void *val )
{
int status;
unsigned short scount=0;
char *tmp;
char *value;
gsize_t size = _gtiff_size[_GTIFTagType (tif,tag)];
if (_GTIFTagType(tif, tag) == TYPE_ASCII)
{
status = TIFFGetField((TIFF *)tif,tag,&tmp);
if (!status) return status;
scount = (unsigned short) (strlen(tmp)+1);
}
else status = TIFFGetField((TIFF *)tif,tag,&scount,&tmp);
if (!status) return status;
*count = scount;
value = (char *)_GTIFcalloc( (scount+MAX_VALUES)*size);
if (!value) return 0;
_TIFFmemcpy( value, tmp, size * scount);
*(char **)val = value;
return status;
}
/*
* Set a GeoTIFF TIFF field.
*/
static int _GTIFSetField (tiff_t *tif, pinfo_t tag, int count, void *value )
{
int status;
unsigned short scount = (unsigned short) count;
/* libtiff ASCII uses null-delimiter */
if (_GTIFTagType(tif, tag) == TYPE_ASCII)
status = TIFFSetField((TIFF *)tif,tag,value);
else
status = TIFFSetField((TIFF *)tif,tag,scount,value);
return status;
}
/*
* This routine is supposed to return the TagType of the <tag>
* TIFF tag. Unfortunately, "libtiff" does not provide this
* service by default, so we just have to "know" what type of tags
* we've got, and how many. We only define the ones Geotiff
* uses here, and others return UNKNOWN. The "tif" parameter
* is provided for those TIFF implementations that provide
* for tag-type queries.
*/
static tagtype_t _GTIFTagType (tiff_t *tif, pinfo_t tag)
{
tagtype_t ttype;
(void) tif; /* dummy reference */
switch (tag)
{
case GTIFF_ASCIIPARAMS: ttype=TYPE_ASCII; break;
case GTIFF_PIXELSCALE:
case GTIFF_TRANSMATRIX:
case GTIFF_TIEPOINTS:
case GTIFF_DOUBLEPARAMS: ttype=TYPE_DOUBLE; break;
case GTIFF_GEOKEYDIRECTORY: ttype=TYPE_SHORT; break;
default: ttype = TYPE_UNKNOWN;
}
return ttype;
}