11 #include <QDomDocument>
12 #include <QDomElement>
15 #include "TiffImporter.h"
19 #include "IException.h"
21 #include "ProjectionFactory.h"
24 #include "PvlToPvlTranslationManager.h"
25 #include "TProjection.h"
37 if ((
m_image = XTIFFOpen(inputName.
expanded().toLatin1().data(),
"r")) == NULL) {
39 QString(
"Could not open TIFF image [") + inputName.
expanded().toLatin1().data() +
"]", _FILEINFO_);
45 TIFFGetField(
m_image, TIFFTAG_IMAGELENGTH, &height);
49 TIFFGetField(
m_image, TIFFTAG_IMAGEWIDTH, &width);
57 if ((
m_raster = (uint32 *) malloc(
sizeof(uint32) * imagesize)) == NULL) {
59 "Could not allocate enough memory", _FILEINFO_);
65 "Could not read image", _FILEINFO_);
71 "Image has an undefined photometric interpretation", _FILEINFO_);
116 geocode_t rasterType;
117 geocode_t coordSysType;
119 if ((GTIFKeyGet(
m_geotiff, GTModelTypeGeoKey, &modelType, 0, 1) == 1) &&
120 ((modelType == 1) || (modelType == 2))) {
122 if ((GTIFKeyGet(
m_geotiff, GTRasterTypeGeoKey, &rasterType, 0, 1) == 1) &&
123 (rasterType == 1 || rasterType == 2)) {
128 if (GTIFKeyGet(
m_geotiff, ProjectedCSTypeGeoKey, &coordSysType, 0, 1) == 1) {
131 FileName transFile((QString)
"$ISISROOT/appdata/translations/" +
137 geoTiffCodeTranslater.
Auto(outPvl);
145 if (GTIFKeyGet(
m_geotiff, GeographicTypeGeoKey, &geoCode, 0, 1) == 1) {
146 std::cout <<
"GeographicTypeGeoKey = " << geoCode << std::endl;
149 std::cout <<
"no GeographicTypeGeoKey" << std::endl;
152 if (GTIFKeyGet(
m_geotiff, GeogAngularUnitsGeoKey, &geoCode, 0, 1) == 1) {
153 std::cout <<
"GeogAngularUnitsGeoKey = " << geoCode << std::endl;
156 std::cout <<
"no GeogAngularUnitsGeoKey" << std::endl;
159 if (GTIFKeyGet(
m_geotiff, GeogEllipsoidGeoKey, &geoCode, 0, 1) == 1) {
160 std::cout <<
"GeogEllipsoidGeoKey = " << geoCode << std::endl;
163 std::cout <<
"no GeogEllipsoidGeoKey" << std::endl;
166 if (GTIFKeyGet(
m_geotiff, GeogSemiMajorAxisGeoKey, &geoCode, 0, 1) == 1) {
167 std::cout <<
"GeogSemiMajorAxisGeoKey = " << geoCode << std::endl;
170 std::cout <<
"no GeogSemiMajorAxisGeoKey" << std::endl;
173 if (GTIFKeyGet(
m_geotiff, GeogSemiMinorAxisGeoKey, &geoCode, 0, 1) == 1) {
174 std::cout <<
"GeogSemiMinorAxisGeoKey = " << geoCode << std::endl;
177 std::cout <<
"no GeogSemiMinorAxisGeoKey" << std::endl;
180 if (GTIFKeyGet(
m_geotiff, GeogInvFlatteningGeoKey, &geoCode, 0, 1) == 1) {
181 std::cout <<
"GeogInvFlatteningGeoKey = " << geoCode << std::endl;
184 std::cout <<
"no GeogInvFlatteningGeoKey" << std::endl;
187 if (GTIFKeyGet(
m_geotiff, ProjCoordTransGeoKey, &geoCode, 0, 1) == 1) {
188 std::cout <<
"ProjCoordTransGeoKey = " << geoCode << std::endl;
191 std::cout <<
"no ProjCoordTransGeoKey" << std::endl;
194 if (GTIFKeyGet(
m_geotiff, ProjLinearUnitsGeoKey, &geoCode, 0, 1) == 1) {
195 std::cout <<
"ProjLinearUnitsGeoKey = " << geoCode << std::endl;
198 std::cout <<
"no ProjLinearUnitsGeoKey" << std::endl;
201 if (GTIFKeyGet(
m_geotiff, ProjStdParallel1GeoKey, &geoCode, 0, 1) == 1) {
202 std::cout <<
"ProjStdParallel1GeoKey = " << geoCode << std::endl;
205 std::cout <<
"no ProjStdParallel1GeoKey" << std::endl;
208 if (GTIFKeyGet(
m_geotiff, ProjStdParallel2GeoKey, &geoCode, 0, 1) == 1) {
209 std::cout <<
"ProjStdParallel2GeoKey = " << geoCode << std::endl;
212 std::cout <<
"no ProjStdParallel2GeoKey" << std::endl;
215 if (GTIFKeyGet(
m_geotiff, ProjNatOriginLongGeoKey, &geoCode, 0, 1) == 1) {
216 std::cout <<
"ProjNatOriginLongGeoKey = " << geoCode << std::endl;
219 std::cout <<
"no ProjNatOriginLongGeoKey" << std::endl;
222 if (GTIFKeyGet(
m_geotiff, ProjNatOriginLatGeoKey, &geoCode, 0, 1) == 1) {
223 std::cout <<
"ProjNatOriginLatGeoKey = " << geoCode << std::endl;
226 std::cout <<
"no ProjNatOriginLatGeoKey" << std::endl;
229 if (GTIFKeyGet(
m_geotiff, ProjFalseEastingGeoKey, &geoCode, 0, 1) == 1) {
230 std::cout <<
"ProjFalseEastingGeoKey = " << geoCode << std::endl;
233 std::cout <<
"no ProjFalseEastingGeoKey" << std::endl;
236 if (GTIFKeyGet(
m_geotiff, ProjFalseNorthingGeoKey, &geoCode, 0, 1) == 1) {
237 std::cout <<
"ProjFalseNorthingGeoKey = " << geoCode << std::endl;
240 std::cout <<
"no ProjFalseNorthingGeoKey" << std::endl;
243 if (GTIFKeyGet(
m_geotiff, ProjFalseOriginLongGeoKey, &geoCode, 0, 1) == 1) {
244 std::cout <<
"ProjFalseOriginLongGeoKey = " << geoCode << std::endl;
247 std::cout <<
"no ProjFalseOriginLongGeoKey" << std::endl;
250 if (GTIFKeyGet(
m_geotiff, ProjFalseOriginLatGeoKey, &geoCode, 0, 1) == 1) {
251 std::cout <<
"ProjFalseOriginLatGeoKey = " << geoCode << std::endl;
254 std::cout <<
"no ProjFalseOriginLatGeoKey" << std::endl;
257 if (GTIFKeyGet(
m_geotiff, ProjFalseOriginEastingGeoKey, &geoCode, 0, 1) == 1) {
258 std::cout <<
"ProjFalseOriginEastingGeoKey = " << geoCode << std::endl;
261 std::cout <<
"no ProjFalseOriginEastingGeoKey" << std::endl;
264 if (GTIFKeyGet(
m_geotiff, ProjFalseOriginNorthingGeoKey, &geoCode, 0, 1) == 1) {
265 std::cout <<
"ProjFalseOriginNorthingGeoKey = " << geoCode << std::endl;
268 std::cout <<
"no ProjFalseOriginNorthingGeoKey" << std::endl;
271 if (GTIFKeyGet(
m_geotiff, ProjCenterLongGeoKey, &geoCode, 0, 1) == 1) {
272 std::cout <<
"ProjCenterLongGeoKey = " << geoCode << std::endl;
275 std::cout <<
"no ProjCenterLongGeoKey" << std::endl;
278 if (GTIFKeyGet(
m_geotiff, ProjCenterLatGeoKey, &geoCode, 0, 1) == 1) {
279 std::cout <<
"ProjCenterLatGeoKey = " << geoCode << std::endl;
282 std::cout <<
"no ProjCenterLatGeoKey" << std::endl;
285 if (GTIFKeyGet(
m_geotiff, ProjCenterEastingGeoKey, &geoCode, 0, 1) == 1) {
286 std::cout <<
"ProjCenterEastingGeoKey = " << geoCode << std::endl;
289 std::cout <<
"no ProjCenterEastingGeoKey" << std::endl;
292 if (GTIFKeyGet(
m_geotiff, ProjCenterNorthingGeoKey, &geoCode, 0, 1) == 1) {
293 std::cout <<
"ProjCenterNorthingGeoKey = " << geoCode << std::endl;
296 std::cout <<
"no ProjCenterNorthingGeoKey" << std::endl;
299 if (GTIFKeyGet(
m_geotiff, ProjScaleAtNatOriginGeoKey, &geoCode, 0, 1) == 1) {
300 std::cout <<
"ProjScaleAtNatOriginGeoKey = " << geoCode << std::endl;
303 std::cout <<
"no ProjScaleAtNatOriginGeoKey" << std::endl;
306 if (GTIFKeyGet(
m_geotiff, ProjAzimuthAngleGeoKey, &geoCode, 0, 1) == 1) {
307 std::cout <<
"ProjAzimuthAngleGeoKey = " << geoCode << std::endl;
310 std::cout <<
"no ProjAzimuthAngleGeoKey" << std::endl;
313 if (GTIFKeyGet(
m_geotiff, ProjStraightVertPoleLongGeoKey, &geoCode, 0, 1) == 1) {
314 std::cout <<
"ProjStraightVertPoleLongGeoKey = " << geoCode << std::endl;
317 std::cout <<
"no ProjStraightVertPoleLongGeoKey" << std::endl;
320 if (GTIFKeyGet(
m_geotiff, VerticalUnitsGeoKey, &geoCode, 0, 1) == 1) {
321 std::cout <<
"VerticalUnitsGeoKey = " << geoCode << std::endl;
324 std::cout <<
"no VerticalUnitsGeoKey" << std::endl;
349 double pixelResolution = mapGroup[
"PixelResolution"];
351 double localRadius = proj->
LocalRadius(trueScaleLat);
353 double scale = (2.0 *
Isis::PI * localRadius) / (360.0 * pixelResolution);
377 char *gdalMetadataBuf;
378 short int gdalMetadataCount = 0;
380 if (TIFFGetField(
m_image, 42112, &gdalMetadataCount, &gdalMetadataBuf) == 1) {
382 QString gdalMetadataQstring(gdalMetadataBuf);
384 QDomDocument gdalDoc(
"GDALMetaData");
385 if (gdalDoc.setContent(gdalMetadataQstring)) {
386 QDomElement gdalRoot = gdalDoc.documentElement();
387 if (gdalRoot.tagName() ==
"GDALMetadata") {
389 QDomNode gdalNode = gdalRoot.firstChild();
390 while (!gdalNode.isNull()) {
391 QDomElement gdalElement = gdalNode.toElement();
392 if (!gdalElement.isNull() ) {
393 if (gdalElement.tagName() ==
"Item") {
394 if (gdalElement.attribute(
"name",
"") ==
"WEST_LONGITUDE") {
395 QString westLon = gdalElement.text();
398 else if (gdalElement.attribute(
"name",
"") ==
"EAST_LONGITUDE") {
399 QString eastLon = gdalElement.text();
402 else if (gdalElement.attribute(
"name",
"") ==
"SOUTH_LATITUDE") {
403 QString southLat = gdalElement.text();
406 else if (gdalElement.attribute(
"name",
"") ==
"NORTH_LATITUDE") {
407 QString northLat = gdalElement.text();
413 gdalNode = gdalNode.nextSibling();
435 double *tiePoints = NULL;
436 short int tieCount = 0;
437 if (TIFFGetField(
m_image, TIFFTAG_GEOTIEPOINTS, &tieCount, &tiePoints) == 1) {
441 if (tiePoints[0] == 0.0 && tiePoints[1] == 0.0) {
444 x = (double)map[
"FalseEasting"] + tiePoints[3];
450 y = (double)map[
"FalseNorthing"] + tiePoints[4];
458 QString msg =
"The upper left X and Y can not be calculated. Unsupported tiepoint "
459 "type in Tiff file (i.e., not ( 0.0, 0.0))";
481 double *scales = NULL;
482 short int scaleCount = 0;
483 if (TIFFGetField(
m_image, TIFFTAG_GEOPIXELSCALE, &scaleCount, &scales) == 1) {
487 if ((scaleCount == 3) && (scales[0] > 0.0 && scales[1] > 0.0) && (scales[0] == scales[1])) {
491 QString msg =
"The pixel resolution could not be retrieved from the TIFF file. Unsupported "
492 "PixelScale tag values.";
523 m_photo == PHOTOMETRIC_MINISWHITE ||
524 m_photo == PHOTOMETRIC_MINISBLACK;
600 return TIFFGetR(pixel);
612 return TIFFGetG(pixel);
624 return TIFFGetB(pixel);
636 return TIFFGetA(pixel);