| 1 |
/* |
| 2 |
* xtiff.c |
| 3 |
* |
| 4 |
* Extended TIFF Directory GEO Tag Support. |
| 5 |
* |
| 6 |
* You may use this file as a template to add your own |
| 7 |
* extended tags to the library. Only the parts of the code |
| 8 |
* marked with "XXX" require modification. |
| 9 |
* |
| 10 |
* Author: Niles D. Ritter |
| 11 |
* |
| 12 |
* Revisions: |
| 13 |
* 18 Sep 1995 -- Deprecated Integraph Matrix tag with new one. |
| 14 |
* Backward compatible support provided. --NDR. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "xtiffio.h" |
| 18 |
#include <stdio.h> |
| 19 |
#include "cpl_serv.h" |
| 20 |
|
| 21 |
/* Tiff info structure. |
| 22 |
* |
| 23 |
* Entry format: |
| 24 |
* { TAGNUMBER, ReadCount, WriteCount, DataType, FIELDNUM, |
| 25 |
* OkToChange, PassDirCountOnSet, AsciiName } |
| 26 |
* |
| 27 |
* For ReadCount, WriteCount, -1 = unknown. |
| 28 |
*/ |
| 29 |
|
| 30 |
static const TIFFFieldInfo xtiffFieldInfo[] = { |
| 31 |
|
| 32 |
/* XXX Insert Your tags here */ |
| 33 |
{ TIFFTAG_GEOPIXELSCALE, -1,-1, TIFF_DOUBLE, FIELD_CUSTOM, |
| 34 |
TRUE, TRUE, "GeoPixelScale" }, |
| 35 |
{ TIFFTAG_INTERGRAPH_MATRIX,-1,-1, TIFF_DOUBLE, FIELD_CUSTOM, |
| 36 |
TRUE, TRUE, "Intergraph TransformationMatrix" }, |
| 37 |
{ TIFFTAG_GEOTRANSMATRIX, -1,-1, TIFF_DOUBLE, FIELD_CUSTOM, |
| 38 |
TRUE, TRUE, "GeoTransformationMatrix" }, |
| 39 |
{ TIFFTAG_GEOTIEPOINTS, -1,-1, TIFF_DOUBLE, FIELD_CUSTOM, |
| 40 |
TRUE, TRUE, "GeoTiePoints" }, |
| 41 |
{ TIFFTAG_GEOKEYDIRECTORY,-1,-1, TIFF_SHORT, FIELD_CUSTOM, |
| 42 |
TRUE, TRUE, "GeoKeyDirectory" }, |
| 43 |
{ TIFFTAG_GEODOUBLEPARAMS, -1,-1, TIFF_DOUBLE, FIELD_CUSTOM, |
| 44 |
TRUE, TRUE, "GeoDoubleParams" }, |
| 45 |
{ TIFFTAG_GEOASCIIPARAMS, -1,-1, TIFF_ASCII, FIELD_CUSTOM, |
| 46 |
TRUE, FALSE, "GeoASCIIParams" }, |
| 47 |
#ifdef JPL_TAG_SUPPORT |
| 48 |
{ TIFFTAG_JPL_CARTO_IFD, 1, 1, TIFF_LONG, FIELD_CUSTOM, |
| 49 |
TRUE, TRUE, "JPL Carto IFD offset" }, /** Don't use this! **/ |
| 50 |
#endif |
| 51 |
}; |
| 52 |
|
| 53 |
#define N(a) (sizeof (a) / sizeof (a[0])) |
| 54 |
static void _XTIFFLocalDefaultDirectory(TIFF *tif) |
| 55 |
{ |
| 56 |
/* Install the extended Tag field info */ |
| 57 |
TIFFMergeFieldInfo(tif, xtiffFieldInfo, N(xtiffFieldInfo)); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/********************************************************************** |
| 62 |
* Nothing below this line should need to be changed. |
| 63 |
**********************************************************************/ |
| 64 |
|
| 65 |
static TIFFExtendProc _ParentExtender; |
| 66 |
|
| 67 |
/* |
| 68 |
* This is the callback procedure, and is |
| 69 |
* called by the DefaultDirectory method |
| 70 |
* every time a new TIFF directory is opened. |
| 71 |
*/ |
| 72 |
|
| 73 |
static void |
| 74 |
_XTIFFDefaultDirectory(TIFF *tif) |
| 75 |
{ |
| 76 |
/* set up our own defaults */ |
| 77 |
_XTIFFLocalDefaultDirectory(tif); |
| 78 |
|
| 79 |
/* Since an XTIFF client module may have overridden |
| 80 |
* the default directory method, we call it now to |
| 81 |
* allow it to set up the rest of its own methods. |
| 82 |
*/ |
| 83 |
|
| 84 |
if (_ParentExtender) |
| 85 |
(*_ParentExtender)(tif); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
Registers an extension with libtiff for adding GeoTIFF tags. |
| 91 |
After this one-time initialization, any TIFF open function may be called in |
| 92 |
the usual manner to create a TIFF file that compatible with libgeotiff. |
| 93 |
The XTIFF open functions are simply for convenience: they call this |
| 94 |
and then pass their parameters on to the appropriate TIFF open function. |
| 95 |
|
| 96 |
<p>This function may be called any number of times safely, since it will |
| 97 |
only register the extension the first time it is called. |
| 98 |
**/ |
| 99 |
|
| 100 |
void XTIFFInitialize(void) |
| 101 |
{ |
| 102 |
static int first_time=1; |
| 103 |
|
| 104 |
if (! first_time) return; /* Been there. Done that. */ |
| 105 |
first_time = 0; |
| 106 |
|
| 107 |
/* Grab the inherited method and install */ |
| 108 |
_ParentExtender = TIFFSetTagExtender(_XTIFFDefaultDirectory); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* GeoTIFF compatible TIFF file open function. |
| 114 |
* |
| 115 |
* @param name The filename of a TIFF file to open. |
| 116 |
* @param mode The open mode ("r", "w" or "a"). |
| 117 |
* |
| 118 |
* @return a TIFF * for the file, or NULL if the open failed. |
| 119 |
* |
| 120 |
This function is used to open GeoTIFF files instead of TIFFOpen() from |
| 121 |
libtiff. Internally it calls TIFFOpen(), but sets up some extra hooks |
| 122 |
so that GeoTIFF tags can be extracted from the file. If XTIFFOpen() isn't |
| 123 |
used, GTIFNew() won't work properly. Files opened |
| 124 |
with XTIFFOpen() should be closed with XTIFFClose(). |
| 125 |
|
| 126 |
The name of the file to be opened should be passed as <b>name</b>, and an |
| 127 |
opening mode ("r", "w" or "a") acceptable to TIFFOpen() should be passed as the |
| 128 |
<b>mode</b>.<p> |
| 129 |
|
| 130 |
If XTIFFOpen() fails it will return NULL. Otherwise, normal TIFFOpen() |
| 131 |
error reporting steps will have already taken place.<p> |
| 132 |
*/ |
| 133 |
|
| 134 |
TIFF* |
| 135 |
XTIFFOpen(const char* name, const char* mode) |
| 136 |
{ |
| 137 |
TIFF *tif; |
| 138 |
|
| 139 |
/* Set up the callback */ |
| 140 |
XTIFFInitialize(); |
| 141 |
|
| 142 |
/* Open the file; the callback will set everything up |
| 143 |
*/ |
| 144 |
tif = TIFFOpen(name, mode); |
| 145 |
if (!tif) return tif; |
| 146 |
|
| 147 |
return tif; |
| 148 |
} |
| 149 |
|
| 150 |
TIFF* |
| 151 |
XTIFFFdOpen(int fd, const char* name, const char* mode) |
| 152 |
{ |
| 153 |
TIFF *tif; |
| 154 |
|
| 155 |
/* Set up the callback */ |
| 156 |
XTIFFInitialize(); |
| 157 |
|
| 158 |
/* Open the file; the callback will set everything up |
| 159 |
*/ |
| 160 |
tif = TIFFFdOpen(fd, name, mode); |
| 161 |
if (!tif) return tif; |
| 162 |
|
| 163 |
return tif; |
| 164 |
} |
| 165 |
|
| 166 |
TIFF* |
| 167 |
XTIFFClientOpen(const char* name, const char* mode, thandle_t thehandle, |
| 168 |
TIFFReadWriteProc RWProc, TIFFReadWriteProc RWProc2, |
| 169 |
TIFFSeekProc SProc, TIFFCloseProc CProc, |
| 170 |
TIFFSizeProc SzProc, |
| 171 |
TIFFMapFileProc MFProvc, TIFFUnmapFileProc UMFProc ) |
| 172 |
{ |
| 173 |
TIFF *tif; |
| 174 |
|
| 175 |
/* Set up the callback */ |
| 176 |
XTIFFInitialize(); |
| 177 |
|
| 178 |
/* Open the file; the callback will set everything up |
| 179 |
*/ |
| 180 |
tif = TIFFClientOpen(name, mode, thehandle, |
| 181 |
RWProc, RWProc2, |
| 182 |
SProc, CProc, |
| 183 |
SzProc, |
| 184 |
MFProvc, UMFProc); |
| 185 |
|
| 186 |
if (!tif) return tif; |
| 187 |
|
| 188 |
return tif; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Close a file opened with XTIFFOpen(). |
| 193 |
* |
| 194 |
* @param tif The file handle returned by XTIFFOpen(). |
| 195 |
* |
| 196 |
* If a GTIF structure was created with GTIFNew() |
| 197 |
* for this file, it should be freed with GTIFFree() |
| 198 |
* <i>before</i> calling XTIFFClose(). |
| 199 |
*/ |
| 200 |
|
| 201 |
void |
| 202 |
XTIFFClose(TIFF *tif) |
| 203 |
{ |
| 204 |
TIFFClose(tif); |
| 205 |
} |