2004.10.21 03:32 "[Tiff] geotiff problem", by Zheng Hu
Hi,
I want to read data from geotiff files,but the result is wrong...I think my codes maybe have some problems.....
#include "xtiffio.h" /* for TIFF */
#include "geotiffio.h" /* for GeoTIFF */
enum {VERSION=0,MAJOR,MINOR};
void main()
{
TIFF *tif=(TIFF*)0; /* TIFF-level descriptor */
GTIF *gtif=(GTIF*)0; /* GeoKey-level descriptor */
int versions[3];
int cit_length;
geocode_t model; /* all key-codes are of this type */
char *citation;
int size,type,stripMax,stripCount,tempbyte;
tsize_t stripSize;
unsigned long imageOffset, result;
uint32 width,length;
uint16 pixel, fillorder;
char *buffer;
unsigned long bufferSize, count;
uint16 photo;
/* Open TIFF descriptor to read GeoTIFF tags */
tif=XTIFFOpen("haha.tif","r");
if (!tif) goto failure;
/* Open GTIF Key parser; keys will be read at this time. */
gtif = GTIFNew(tif);
if (!gtif) goto failure;
/* Get the GeoTIFF directory info */
GTIFDirectoryInfo(gtif,versions,0);
if (versions[MAJOR] > 1)
{
printf("this file is too new for me\n"); goto failure;
}
if (!GTIFKeyGet(gtif, GTModelTypeGeoKey, &model, 0, 1))
{
printf("Yikes! no Model Type\n"); goto failure;
}
/* ASCII keys are variable-length; compute size */
cit_length = GTIFKeyInfo(gtif,GTCitationGeoKey,&size,&type);
if (cit_length > 0)
{
citation = malloc(size*cit_length);
if (!citation) goto failure;
GTIFKeyGet(gtif, GTCitationGeoKey, citation, 0, cit_length);
printf("Citation:%s\n",citation);
}
/* Get some TIFF info on this image */
TIFFGetField(tif,TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &length);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &pixel);
stripSize = TIFFStripSize (tif);
stripMax = TIFFNumberOfStrips (tif);
printf("%d\n%d\n%d\n",width,length,pixel);
imageOffset = 0;
// Check that it is of a type that we support
//if((TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bps) == 0) || (bps != 1)){
// fprintf(stderr, "Either undefined or unsupported number of bits per sample\n");
// exit(42);
//}
//if((TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &spp) == 0) || (spp != 1)){
//fprintf(stderr, "Either undefined or unsupported number of samples per pixel\n");
//exit(42);
//}
bufferSize = TIFFNumberOfStrips (tif) * stripSize;
if((buffer = (char *) malloc(bufferSize)) == NULL){
fprintf(stderr, "Could not allocate enough memory for the uncompressed image\n");
exit(42);
}
for (stripCount = 0; stripCount < stripMax; stripCount++){
if((result = TIFFReadEncodedStrip (tif, stripCount,
buffer + imageOffset,
stripSize)) == -1){
fprintf(stderr, "Read error on input strip number %d\n", stripCount);
exit(42);
}
imageOffset += result;
}
if(TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photo) == 0){
fprintf(stderr, "Image has an undefined photometric interpretation\n");
exit(42);
}
//if(photo != PHOTOMETRIC_MINISWHITE){
// Flip bits
//printf("Fixing the photometric interpretation\n");
//for(count = 0; count < bufferSize; count++)
//buffer[count] = ~buffer[count];
//}
// Deal with fillorder
//if(TIFFGetField(tif, TIFFTAG_FILLORDER, &fillorder) == 0){
//fprintf(stderr, "Image has an undefined fillorder\n");
//exit(42);
//}
//if(fillorder != FILLORDER_MSB2LSB){
// We need to swap bits -- ABCDEFGH becomes HGFEDCBA
//printf("Fixing the fillorder\n");
// for(count = 0; count < bufferSize; count++){
// if(buffer[count] & 32768) tempbyte += 1;
// if(buffer[count] & 16384) tempbyte += 2;
// if(buffer[count] & 8192) tempbyte += 4;
// if(buffer[count] & 4096) tempbyte += 8;
// if(buffer[count] & 2048) tempbyte += 16;
// if(buffer[count] & 1024) tempbyte += 32;
// if(buffer[count] & 512) tempbyte += 64;
// if(buffer[count] & 256) tempbyte += 128;
// if(buffer[count] & 128) tempbyte += 256;
// if(buffer[count] & 64) tempbyte += 512;
// if(buffer[count] & 32) tempbyte += 1024;
// if(buffer[count] & 16) tempbyte += 2048;
// if(buffer[count] & 8) tempbyte += 4096;
// if(buffer[count] & 4) tempbyte += 8192;
// if(buffer[count] & 2) tempbyte += 16384;
// if(buffer[count] & 1) tempbyte += 32768;
// buffer[count] = tempbyte;
// }
//}
// Do whatever it is we do with the buffer -- we dump it in hex
//if(TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width) == 0){
//fprintf(stderr, "Image does not define its width\n");
//exit(42);
//}
for(count = 0; count < 50; count++){
printf("%d", (unsigned long) buffer[count]);
if((count + 1) % (width / 8) == 0) printf("\n");
else printf(" ");
}
/* get rid of the key parser */
GTIFFree(gtif);
/* close the TIFF file descriptor */
XTIFFClose(tif);
exit (0);
failure:
exit (-1);
}