Return-Path: <tiff-bounces@remotesensing.org>
Received: from inav003.isp.belgacom.be (inav003.isp.belgacom.be [195.238.3.252])
        by mailst034.isp.belgacom.be (8.12.11/8.12.11/Skynet-MAILSTORE-2.14) with ESMTP id i9L3Xnmu023563
        for <joris.at.lebbeke@skynet.be>; Thu, 21 Oct 2004 05:33:49 +0200
        (envelope-from <tiff-bounces@remotesensing.org>)
Received: from inas005.isp.belgacom.be (localhost [127.0.0.1])
        by inav003.isp.belgacom.be (8.12.11/8.12.11/Skynet-IN-AV-3.00) with ESMTP id i9L3Xm7S029549
        for <joris.at.lebbeke@skynet.be>; Thu, 21 Oct 2004 05:33:48 +0200
        (envelope-from <tiff-bounces@remotesensing.org>)
Received: from inmx013.isp.belgacom.be (inmx013.isp.belgacom.be [195.238.2.216])
        by inas005.isp.belgacom.be (8.12.11/8.12.11/Skynet-IN-AS-2.03) with ESMTP id i9L3XZXq031472
        for <joris.at.lebbeke@skynet.be>; Thu, 21 Oct 2004 05:33:40 +0200
        (envelope-from <tiff-bounces@remotesensing.org>)
Received: from xserve.flids.com (adsl-065-083-169-202.sip.mco.bellsouth.net [65.83.169.202])
        by inmx013.isp.belgacom.be (8.12.11/8.12.11/Skynet-IN-2.32) with ESMTP id i9L3XJWQ002180
        for <joris.at.lebbeke@skynet.be>; Thu, 21 Oct 2004 05:33:21 +0200
        (envelope-from <tiff-bounces@remotesensing.org>)
Received: from adsl-065-083-169-202.sip.mco.bellsouth.net (localhost [127.0.0.1])
	by xserve.flids.com (Postfix) with ESMTP
	id D0EDC3D9AED; Wed, 20 Oct 2004 23:32:53 -0400 (EDT)
X-Original-To: tiff@remotesensing.org
Delivered-To: tiff@remotesensing.org
Received: from mproxy.gmail.com (rproxy.gmail.com [64.233.170.207])
	by xserve.flids.com (Postfix) with ESMTP id 967AD3D9ACA
	for <tiff@remotesensing.org>; Wed, 20 Oct 2004 23:32:51 -0400 (EDT)
Received: by mproxy.gmail.com with SMTP id v30so199254rnb
	for <tiff@remotesensing.org>; Wed, 20 Oct 2004 20:32:49 -0700 (PDT)
DomainKey-Signature: a=rsa-sha1; c=nofws; s=beta; d=gmail.com;
	h=received:message-id:date:from:reply-to:to:subject:mime-version:content-type:content-transfer-encoding;
	b=c4FFJ/LH7FaRCcCeLqfRx0AnkXDHtTbXHEzmZ89EHzSsurLIoEy4zdCPrg5S2rUET1ctd5jrVn8fvGoSPM6Lx3kf5S4c/gHJ6u70YK8w52D+G8+UImwrO/utbDyul8iC17YSmbdkPmVTDplGcwv5srQcgnyY8sTEWZIf2G78LQI
Received: by 10.38.206.72 with SMTP id d72mr1947648rng;
	Wed, 20 Oct 2004 20:32:48 -0700 (PDT)
Received: by 10.38.8.19 with HTTP; Wed, 20 Oct 2004 20:32:48 -0700 (PDT)
Message-ID: <be8970bb04102020323ec8e9df@mail.gmail.com>
Date: Wed, 20 Oct 2004 23:32:48 -0400
From: zheng hu <liamezh@gmail.com>
To: tiff@remotesensing.org
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Subject: [Tiff] geotiff problem
X-BeenThere: tiff@remotesensing.org
X-Mailman-Version: 2.1.2
Precedence: list
Reply-To: zheng hu <liamezh@gmail.com>
List-Id: TIFF and libtiff discussion  <tiff.remotesensing.org>
List-Unsubscribe: <http://xserve.flids.com/mailman/listinfo/tiff>,
	<mailto:tiff-request@remotesensing.org?subject=unsubscribe>
List-Archive: <http://xserve.flids.com/pipermail/tiff>
List-Post: <mailto:tiff@remotesensing.org>
List-Help: <mailto:tiff-request@remotesensing.org?subject=help>
List-Subscribe: <http://xserve.flids.com/mailman/listinfo/tiff>,
	<mailto:tiff-request@remotesensing.org?subject=subscribe>
Sender: tiff-bounces@remotesensing.org
Errors-To: tiff-bounces@remotesensing.org
X-UIDL: 8585e69ca052a6d8250ac8e68042500e

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);
	}
_______________________________________________
Tiff mailing list
Tiff@remotesensing.org
http://xserve.flids.com/mailman/listinfo/tiff
