[postgis-commits] svn - r2703 - in branches/gSoC2007_raster/pgraster: . win32/build

postgis-commits at postgis.refractions.net postgis-commits at postgis.refractions.net
Sat Aug 25 14:09:27 PDT 2007


Author: xingkth
Date: 2007-08-25 14:09:26 -0700 (Sat, 25 Aug 2007)
New Revision: 2703

Modified:
   branches/gSoC2007_raster/pgraster/geotiff2pgraster.c
   branches/gSoC2007_raster/pgraster/pgraster2geotiff.c
   branches/gSoC2007_raster/pgraster/win32/build/geotiff2pgraster.exe
   branches/gSoC2007_raster/pgraster/win32/build/pgraster2geotiff.exe
   branches/gSoC2007_raster/pgraster/wkb.c
Log:
xing-test for none-utf8 single band images(float8).

Modified: branches/gSoC2007_raster/pgraster/geotiff2pgraster.c
===================================================================
--- branches/gSoC2007_raster/pgraster/geotiff2pgraster.c	2007-08-24 18:53:53 UTC (rev 2702)
+++ branches/gSoC2007_raster/pgraster/geotiff2pgraster.c	2007-08-25 21:09:26 UTC (rev 2703)
@@ -55,7 +55,6 @@
 //GeoTIFF & TIFF library headers
 #include "geotiff.h"
 #include "xtiffio.h"
-//#include "tiffiop.h"
 #include "geo_normalize.h"
 #include "geovalues.h"
 #include "tiffio.h"
@@ -81,11 +80,14 @@
 #include <sys/param.h>       
 #endif
 
+#ifndef TIFF_SWAB
+#define	TIFF_SWAB		0x0080	/* byte swap file information */
+#endif
+
 #ifdef USE_ICONV
 	char *utf8(const char *fromcode, char *inputbuf);
 #endif  
 
-
 /* Debug Control Flag */
 //#define DEBUG 1
 
@@ -122,10 +124,6 @@
 int nTIFFBitsPerPixel = 0;
 int nRealBytePerPixel = 0;
 int nRealBitsPerPixel = 0;
-uint32 stripMax, stripCount;
-tsize_t stripSize;
-uint32 bufferSize;
-unsigned long imageOffset;
 double dblNoDataValue = -1.0f;
 uint16 bps       = 1;
 uint16 spp       = 1;
@@ -946,7 +944,8 @@
 
 	//Read Image Metdata Data 
 	{
-		uint16 tif_magic;
+		
+		uint16 tif_magic = 0;
 		FILE* ftifftemp = fopen(geotiff_file, "r");
 		// Judge TIFF Endian??		
 		if(ftifftemp == NULL)
@@ -957,13 +956,19 @@
 		}
 		fseek(ftifftemp,0,SEEK_SET);
 		fread(&tif_magic,sizeof(tif_magic),1, ftifftemp);
+		fclose(ftifftemp);
+		ftifftemp = NULL;
+	
 		if( tif_magic == TIFF_BIGENDIAN)
 			tiff_bigendian = 1;
 		else 
 			tiff_bigendian = 0;
-		fclose(ftifftemp);
-		ftifftemp = NULL;
+	
 
+#ifdef DEBUG
+		fprintf(stderr, "TIFF Big Endian: %d, Big Endian: %d\n", tiff_bigendian, big_endian);
+#endif
+
 		if(tiff_bigendian != big_endian)
 			badIndians = 1;
 		else
@@ -983,6 +988,11 @@
 		//Calculate nTIFFBytePerPixel (Not so simple) 
 		nTIFFBytePerPixel = bps*spp/8;
 		nTIFFBitsPerPixel = bps*spp;
+
+		if((mode==PHOTOMETRIC_RGB) && (spp==4))
+			hasAlpha = 1;
+		else
+			hasAlpha = 0;
 		
 		if(bps == 1 && (mode == PHOTOMETRIC_MINISBLACK || mode == PHOTOMETRIC_MINISWHITE)) 
 		{
@@ -1274,6 +1284,9 @@
 		metadata.rasterRowCount = rows;
 		metadata.rasterColumnCount = cols;
 		metadata.rasterCellDepth = nRealBitsPerPixel;
+#ifdef DEBUG
+		fprintf(stderr, "nRealBitsPerPixel: %d\n",nRealBitsPerPixel);
+#endif
 		metadata.blockSizeBands = 1;
 		metadata.blockCompression = CT_NONE;
 		metadata.SRID = SRID;
@@ -1596,6 +1609,7 @@
 					nBufferHeight = blockRowSize;
 				
 				datalength = nBufferWidth * nBufferHeight * nRealBitsPerPixel;
+
 				if(datalength % 8 != 0)
 					datalength = (datalength / 8) +1;
 				else
@@ -1807,48 +1821,113 @@
 {
 	
 	int result = 0;
+	uint32 bufferSize;
+	
 
-	if((mode==PHOTOMETRIC_RGB) && (spp==4))
-		hasAlpha = 1;
-	else
-		hasAlpha = 0;
+	if(!TIFFIsTiled(tif))
+	{
+		// Process in Strips.
 
-	// Read in the possibly multiple strips
-  	stripSize = TIFFStripSize (tif);
-  	stripMax = TIFFNumberOfStrips (tif);
-  	imageOffset = 0;
+		unsigned long imageOffset;
+		uint32 stripMax, stripCount;
+		tsize_t stripSize;
 
-	bufferSize = stripMax * stripSize;
+  		stripSize = TIFFStripSize (tif);
+  		stripMax = TIFFNumberOfStrips (tif);
+  		imageOffset = 0;
 
-	if((pDataBuffer = (byte*) malloc(bufferSize)) == NULL)
-	{
-    	if(outmode != MODE_QUIET)
-			fprintf(stderr, "Could not allocate enough memory for the uncompressed TIFF image.\n");
-     	return 0;
-  	}
+		bufferSize = stripMax * stripSize;
 
-	if( mode == PHOTOMETRIC_CIELAB )
-		_initCIELabConversion(tif);
+		if((pDataBuffer = (byte*) malloc(bufferSize)) == NULL)
+		{
+    		if(outmode != MODE_QUIET)
+				fprintf(stderr, "Could not allocate enough memory for the uncompressed TIFF image.\n");
+     		return 0;
+  		}
 
+		if( mode == PHOTOMETRIC_CIELAB )
+			_initCIELabConversion(tif);
 
-	TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+		TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+		for (stripCount = 0; stripCount < stripMax; stripCount++)
+		{
+    		if((result = TIFFReadEncodedStrip (tif, stripCount,
+					      pDataBuffer + imageOffset,
+					      stripSize)) == -1)
+			{
+      			if(outmode != MODE_QUIET)
+					fprintf(stderr, "**Error** read TIFF data in strip of number %d\n", (unsigned int)stripCount);
+      			return 0;
+    		}
 
-	for (stripCount = 0; stripCount < stripMax; stripCount++)
+    		imageOffset += result;
+  		}
+
+		return 1;
+	}
+	else
 	{
-    	if((result = TIFFReadEncodedStrip (tif, stripCount,
-				      pDataBuffer + imageOffset,
-				      stripSize)) == -1)
+		// Process in Tiles
+		uint32  width = cols, height = rows;		/* image width & height */
+    	uint32  tile_width, tile_height;
+		byte 	*raster = NULL;
+		uint32  row, col;
+		uint32 	rows_read, cols_read;
+		
+		TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width);
+        TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height);
+
+		bufferSize = width * height * nTIFFBytePerPixel;
+
+		if((pDataBuffer = (byte*) malloc(bufferSize)) == NULL)
 		{
-      		if(outmode != MODE_QUIET)
-				fprintf(stderr, "**Error** read TIFF data in strip of number %d\n", (unsigned int)stripCount);
-      		return 0;
+    		if(outmode != MODE_QUIET)
+				fprintf(stderr, "Could not allocate enough memory for the uncompressed TIFF image.\n");
+     		return 0;
+  		}		
+
+		raster = _TIFFmalloc(tile_width * tile_height * nTIFFBytePerPixel);
+    	if (raster == 0) 
+		{
+        	if(outmode != MODE_QUIET)
+				fprintf(stderr, "Could not allocate enough memory for the TILE buffer.\n");
+     		return 0;
     	}
 
-    	imageOffset += result;
-  	}
+		for (row = 0; row < height; row += tile_height)
+		{
+			
+			if((row+tile_height) < height)
+				rows_read = tile_height;
+			else
+				rows_read = height - row;
 
-	return 1;
-	
+			for (col = 0; col < width; col += tile_width)
+			{
+				uint32 i;
+				// Read 
+				TIFFReadTile(tif, raster, col, row, 0, 0);
+				
+				if((col+tile_width) < width)
+					cols_read = tile_width;
+				else
+					cols_read = width - col;
+
+				// Put Data Into Data Buffer.
+				for( i = 0 ; i <rows_read; i++)
+				{
+					memcpy(pDataBuffer+((row+i)*cols+col)*nTIFFBytePerPixel, 
+								raster+i*tile_width*nTIFFBytePerPixel, cols_read * nTIFFBytePerPixel);
+				}
+			}
+		}
+
+		// TIFF Free memory
+		_TIFFfree( raster );
+
+		return 1;
+	}
+
 }
 
 static TIFFDisplay display_sRGB = {
@@ -1912,7 +1991,7 @@
 				((uint16*)buffer)[0] = ((uint16*)(&(pDataBuffer[nIndex])))[0]; // Red
 				((uint16*)buffer)[1] = ((uint16*)(&(pDataBuffer[nIndex])))[1]; // Green
 				((uint16*)buffer)[2] = ((uint16*)(&(pDataBuffer[nIndex])))[2]; // Blue
-				if( ! tiff_bigendian)
+				if(tiff_bigendian)
 				{
 					// inverse byte order
 					((uint16*)buffer)[0] = _ntohs(((uint16*)buffer)[0]);
@@ -1926,7 +2005,7 @@
 				((uint16*)buffer)[1] = ((uint16*)(&(pDataBuffer[nIndex])))[1]; // Green
 				((uint16*)buffer)[2] = ((uint16*)(&(pDataBuffer[nIndex])))[2]; // Blue
 				((uint16*)buffer)[3] = ((uint16*)(&(pDataBuffer[nIndex])))[3]; // Alpha
-				if( ! tiff_bigendian)
+				if(tiff_bigendian)
 				{
 					// inverse byte order
 					((uint16*)buffer)[0] = _ntohs(((uint16*)buffer)[0]);
@@ -2050,7 +2129,7 @@
 						{
 							buffer = (byte*)malloc(sizeof(_uint16));
 							*((_uint16*)buffer) = ((_uint16*)pDataBuffer)[nIndex+band]; // TIFF By Default BSQ.
-							if(!tiff_bigendian)
+							if(tiff_bigendian)
 							{
 								*((_uint16*)buffer) = _ntohs(*((_uint16*)buffer));
 							}
@@ -2059,7 +2138,7 @@
 						{
 							buffer = (byte*)malloc(sizeof(_uint32));
 							*((_uint32*)buffer) = ((_uint32*)pDataBuffer)[nIndex+band]; // TIFF By Default BSQ.
-							if(!tiff_bigendian)
+							if(tiff_bigendian)
 							{
 								*((_uint32*)buffer) = _ntohl(*((_uint32*)buffer));
 							}
@@ -2068,7 +2147,7 @@
 						{
 							buffer = (byte*)malloc(sizeof(_int64));
 							*((_int64*)buffer) = ((_int64*)pDataBuffer)[nIndex+band]; // TIFF By Default BSQ.
-							if(!tiff_bigendian)
+							if(tiff_bigendian)
 							{
 								*((_int64*)buffer) = _ntohll(*((_int64*)buffer));
 							}
@@ -2180,6 +2259,7 @@
 			// Directly return data
 			int nIndexInByte = (nTIFFBitsPerPixel* (cols * row + col )) / 8;
 			int nOffsetInBits = (nTIFFBitsPerPixel* (cols * row + col )) % 8;
+
 			if(bps < 8)
 			{
 				byte c = ((byte*)pDataBuffer)[nIndexInByte];
@@ -2235,7 +2315,7 @@
 				buffer = (byte*)malloc(2*sizeof(char));
 				c= ((int16*)pDataBuffer)[nIndex];
 				memcpy(buffer,(byte*)&c,2);
-				if(!tiff_bigendian)
+				if(tiff_bigendian)
 					*((int16*)buffer) = _ntohs(*((int16*)buffer));
 			}
 			else if(bps == 16) // unsigned short
@@ -2243,9 +2323,9 @@
 				uint16 c;
 				*bitsreturned = 2*8;
 				buffer = (byte*)malloc(2*sizeof(char));
-				c= ((uint16*)pDataBuffer)[nIndex];
+				c= ((uint16*)(pDataBuffer+nIndex))[band];
 				memcpy(buffer,(byte*)&c,2);
-				if(!tiff_bigendian)
+				if(tiff_bigendian)
 					*((uint16*)buffer) = _ntohs(*((uint16*)buffer));
 			}
 			else if(bps == 32 && format == SAMPLEFORMAT_INT) // long
@@ -2253,30 +2333,31 @@
 				int32 c;
 				*bitsreturned = 4*8;
 				buffer = (byte*)malloc(4*sizeof(char));
-				c= ((int32*)pDataBuffer)[nIndex];
+				c= ((int32*)(pDataBuffer+nIndex))[band];
 				memcpy(buffer,(byte*)&c,4);
-				if(!tiff_bigendian)
+				if(tiff_bigendian)
 					*((int32*)buffer) = _ntohl(*((int32*)buffer));
 			}
 			else if(bps == 32 && format == SAMPLEFORMAT_IEEEFP) // IEEE FP
 			{
-				uint32 c;
+				float c;
 				*bitsreturned = 4*8;
-				buffer = (byte*)malloc(4*sizeof(char));
-				c= ((uint32*)pDataBuffer)[nIndex];
+				buffer = (byte*)malloc(sizeof(float));
+				c= ((float*)(pDataBuffer+nIndex))[band];
 				memcpy(buffer,(byte*)&c,4);
-				//XXX: I don't know how to swap bytes order of IEEE FP.
-				if(!tiff_bigendian)
-					*((int32*)buffer) = _ntohl(*((int32*)buffer));
+
+				if(tiff_bigendian)
+					*((float*)buffer) = _ntohf(*((float*)buffer));
+				
 			}
 			else if(bps == 32) // unsigned long
 			{
 				uint32 c;
 				*bitsreturned = 4*8;
 				buffer = (byte*)malloc(4*sizeof(char));
-				c= ((uint32*)pDataBuffer)[nIndex];
+				c= ((uint32*)(pDataBuffer+nIndex))[band];
 				memcpy(buffer,(byte*)&c,4);
-				if(!tiff_bigendian)
+				if(tiff_bigendian)
 					*((uint32*)buffer) = _ntohl(*((uint32*)buffer));
 			}
 			else 
@@ -2394,6 +2475,7 @@
 {
 	// Some metadata need to be updated after the data loading.
 	char query[QUERY_BUF_LENGTH];
+
 	memset(query, '\0' , QUERY_BUF_LENGTH);
 	if(outmode == MODE_VERBOSE)
 		fprintf(stdout, "Start updating metadata second time.\n");
@@ -2415,6 +2497,25 @@
 		exit_nicely(conn, 1);
 	}
 
+	memset(query, '\0' , QUERY_BUF_LENGTH);
+	sprintf(query,"UPDATE \"%s\".\"pgraster_metadata\" SET rastercelldepth=%d WHERE rasterObjectID=%Ld", schema, (int)metadata.rasterCellDepth, metadata.rasterObjectID);
+
+	res = PQexecParams(conn,
+                       query,
+                       0,       
+                       NULL,    
+                       NULL,
+                       NULL,    
+                       NULL,    
+                       0);
+
+	if ( ! res || PQresultStatus(res) != PGRES_COMMAND_OK )
+	{
+		if(outmode != MODE_QUIET)
+			fprintf(stderr, "**Error** when updating spatial metadata at server side: %s\n.",PQerrorMessage(conn));
+		exit_nicely(conn, 1);
+	}
+
 	if(outmode == MODE_VERBOSE)
 		fprintf(stdout, "Finish updateing metadata second time.\n");
 }

Modified: branches/gSoC2007_raster/pgraster/pgraster2geotiff.c
===================================================================
--- branches/gSoC2007_raster/pgraster/pgraster2geotiff.c	2007-08-24 18:53:53 UTC (rev 2702)
+++ branches/gSoC2007_raster/pgraster/pgraster2geotiff.c	2007-08-25 21:09:26 UTC (rev 2703)
@@ -75,6 +75,10 @@
 #include <sys/param.h>       
 #endif
 
+#ifndef TIFF_SWAB
+#define	TIFF_SWAB		0x0080	/* byte swap file information */
+#endif
+
 #define TIFFOpen XTIFFOpen
 #define TIFFClose XTIFFClose
 #define	streq(a,b)	(strcmp(a,b) == 0)
@@ -85,7 +89,7 @@
 #endif  
 
 /* Debug Control Flag */
-// #define DEBUG 1
+//#define DEBUG 1
 
 /* Buffer length for SQL statement */
 #define QUERY_BUF_LENGTH 1024
@@ -1090,7 +1094,7 @@
 		georeferenced = atoi(PQgetvalue(res,0,21));
 
 #ifdef DEBUG
-		printf("SRID: %d\n", SRID);
+		printf("???SRID: %d\n", SRID);
 #endif 
 
 		PQclear(res);
@@ -1255,9 +1259,10 @@
 void _CreateGeoTIFF()
 {
 	//Set BigEndian as default output byte order
-	uint16 tif_magic;
+	
 	FILE* ftifftemp = fopen(geotiff_file, "w");
-			
+	uint16 tif_magic = TIFF_LITTLEENDIAN;
+
 	if(ftifftemp == NULL)
 	{
 		if(outmode != MODE_QUIET)
@@ -1265,13 +1270,10 @@
 		exit_nicely(conn, 1);
 	}
 	fseek(ftifftemp,0,SEEK_SET);
-	if(big_endian)
-		tif_magic = TIFF_BIGENDIAN;
-	else
-		tif_magic = TIFF_LITTLEENDIAN;
 	fwrite(&tif_magic,sizeof(tif_magic),1, ftifftemp);
 	fclose(ftifftemp);
 	ftifftemp = NULL;
+
 	
 	tif=XTIFFOpen(geotiff_file,"w");
 	if (!tif) 
@@ -1280,7 +1282,7 @@
 			fprintf(stderr, "**Error** when creating TIFF file.\n");
 		exit_nicely(conn,1);
 	}
-	
+
 	gtif = GTIFNew(tif);
 	if (!gtif)
 	{
@@ -1578,6 +1580,10 @@
 	nTIFFBytePerPixel = (bps/8) * spp;
 	_uint32 stripsize = cols * rowsperstrip * bps * spp;
 	
+#ifdef DEBUG
+	fprintf(stderr, "nTIFFBytePerPixel: = %d, bps: %d, spp: %d\n", nTIFFBytePerPixel,bps,spp);
+#endif
+
 	if(stripsize % 8 == 0 )
 		stripsize =stripsize / 8;
 	else
@@ -1657,7 +1663,7 @@
 			rowsReturned = (_uint32)PQntuples(res);
 
 #ifdef DEBUG
-			printf("rowsReturned: %d\n", rowsReturned);
+			printf("rowsReturned: %d, nTIFFBytePerPixel: %d \n", rowsReturned, nTIFFBytePerPixel);
 #endif	
 			
 			for(k = 0; k < rowsReturned; k++)
@@ -1671,7 +1677,59 @@
 				for(n=0; n < realBlockRowSize; n++)
 				{
 					//fill data buffer
-					memcpy(buffer+(cols*n+(realBlockColumnNumber-1)*blocksizeColumns)*nTIFFBytePerPixel, pQData+(realBlockColumnSize*n)*nTIFFBytePerPixel,nTIFFBytePerPixel*realBlockColumnSize);	
+					byte *pSrcData = pQData+(realBlockColumnSize*n)*nTIFFBytePerPixel;
+					
+					if(big_endian) // by default,the data is save in little endian
+					{
+						byte *pTransformed = malloc(nTIFFBytePerPixel*realBlockColumnSize);
+						byte *pCur = pTransformed;
+							
+						for(m = 0; m < realBlockColumnSize; m++)
+						{
+							if(datatype == DT_16BIT_S || datatype == DT_16BIT_U)
+							{
+								*((_uint16*)pCur) = _ntohs(*((_uint16*)pSrcData));
+								pCur += 2;
+								pSrcData += 2;
+							}
+							else if(datatype == DT_32BIT_S || datatype == DT_32BIT_U)
+							{
+								*((_uint32*)pCur) = _ntohl(*((_uint32*)pSrcData));
+								pCur += 4;
+								pSrcData += 4;
+							}
+							/*
+							else if(datatype == DT_64BIT_S || datatype == DT_64BIT_U)
+							{
+								*((_uint64*)pCur) = _ntohl(*((_uint64*)pSrcData));
+								pCur += 8;
+								pSrcData += 8;
+							}
+							*/
+							else if(datatype == DT_32BIT_REAL )
+							{
+								*((float*)pCur) = _ntohf(*((float*)pSrcData));
+								pCur += 4;
+								pSrcData += 4;
+							}
+							else if(datatype == DT_64BIT_REAL )
+							{
+								*((double*)pCur) = _ntohd(*((double*)pSrcData));
+								pCur += 8;
+								pSrcData += 8;
+							}
+							else
+								continue;
+						}
+
+						memcpy(buffer+(cols*n+(realBlockColumnNumber-1)*blocksizeColumns)*nTIFFBytePerPixel, pTransformed,nTIFFBytePerPixel*realBlockColumnSize);
+						free(pTransformed);
+
+					}
+					else
+					{
+						memcpy(buffer+(cols*n+(realBlockColumnNumber-1)*blocksizeColumns)*nTIFFBytePerPixel, pSrcData,nTIFFBytePerPixel*realBlockColumnSize);	
+					}
 				}
 			}
 

Modified: branches/gSoC2007_raster/pgraster/win32/build/geotiff2pgraster.exe
===================================================================
(Binary files differ)

Modified: branches/gSoC2007_raster/pgraster/win32/build/pgraster2geotiff.exe
===================================================================
(Binary files differ)

Modified: branches/gSoC2007_raster/pgraster/wkb.c
===================================================================
--- branches/gSoC2007_raster/pgraster/wkb.c	2007-08-24 18:53:53 UTC (rev 2702)
+++ branches/gSoC2007_raster/pgraster/wkb.c	2007-08-25 21:09:26 UTC (rev 2703)
@@ -155,7 +155,7 @@
 	if( pMbr == NULL)
 		return (WKBGeometry*)NULL;
 	
-	wksPtArray = (WKSPoint*)generic_alloc(sizeof(WKSPoint) * 5);
+	wksPtArray = (WKSPoint*)generic_alloc(sizeof(WKSPoint) * 4);
 	
 	wksPtArray[0].X = pMbr->minX;
 	wksPtArray[0].Y = pMbr->minY;
@@ -168,11 +168,8 @@
 	
 	wksPtArray[3].X = pMbr->minX;
 	wksPtArray[3].Y = pMbr->maxY;
-
-	wksPtArray[4].X = pMbr->minX;
-	wksPtArray[4].Y = pMbr->minY;
 	
-	pGeom = G_CreatePolygon_Simple(5, wksPtArray);
+	pGeom = G_CreatePolygon_Simple(4, wksPtArray);
 	
 	generic_free((void*)wksPtArray);
 	



More information about the postgis-commits mailing list