diff --git a/Utilities/GDAL/frmts/jdem/jdemdataset.cpp b/Utilities/GDAL/frmts/jdem/jdemdataset.cpp deleted file mode 100644 index e2da216be4fc23dc718beaa2737a77d96512d9dc..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/jdem/jdemdataset.cpp +++ /dev/null @@ -1,346 +0,0 @@ -/****************************************************************************** - * $Id: jdemdataset.cpp,v 1.9 2005/05/05 15:54:48 fwarmerdam Exp $ - * - * Project: JDEM Reader - * Purpose: All code for Japanese DEM Reader - * Author: Frank Warmerdam, warmerdam@pobox.com - * - ****************************************************************************** - * Copyright (c) 2000, Frank Warmerdam <warmerdam@pobox.com> - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - ****************************************************************************** - * - * $Log: jdemdataset.cpp,v $ - * Revision 1.9 2005/05/05 15:54:48 fwarmerdam - * PAM Enabled - * - * Revision 1.8 2003/07/08 21:21:56 warmerda - * avoid warnings - * - * Revision 1.7 2002/09/04 06:50:37 warmerda - * avoid static driver pointers - * - * Revision 1.6 2002/06/12 21:12:25 warmerda - * update to metadata based driver info - * - * Revision 1.5 2001/11/11 23:51:00 warmerda - * added required class keyword to friend declarations - * - * Revision 1.4 2001/07/18 04:51:57 warmerda - * added CPL_CVSID - * - * Revision 1.3 2001/06/21 19:59:51 warmerda - * added help link - * - * Revision 1.2 2000/11/28 02:28:54 warmerda - * Added error checks, GetGeoTransform and GetProjection - * - * Revision 1.1 2000/11/27 19:03:26 warmerda - * New - * - */ - -#include "gdal_pam.h" - -CPL_CVSID("$Id: jdemdataset.cpp,v 1.9 2005/05/05 15:54:48 fwarmerdam Exp $"); - -CPL_C_START -void GDALRegister_JDEM(void); -CPL_C_END - -/************************************************************************/ -/* JDEMGetField() */ -/************************************************************************/ - -static int JDEMGetField( char *pszField, int nWidth ) - -{ - char szWork[32]; - - CPLAssert( nWidth < (int) sizeof(szWork) ); - - strncpy( szWork, pszField, nWidth ); - szWork[nWidth] = '\0'; - - return atoi(szWork); -} - -/************************************************************************/ -/* JDEMGetAngle() */ -/************************************************************************/ - -static double JDEMGetAngle( char *pszField ) - -{ - int nAngle = JDEMGetField( pszField, 7 ); - int nDegree, nMin, nSec; - - // Note, this isn't very general purpose, but it would appear - // from the field widths that angles are never negative. Nice - // to be a country in the "first quadrant". - - nDegree = nAngle / 10000; - nMin = (nAngle / 100) % 100; - nSec = nAngle % 100; - - return nDegree + nMin / 60.0 + nSec / 3600.0; -} - -/************************************************************************/ -/* ==================================================================== */ -/* JDEMDataset */ -/* ==================================================================== */ -/************************************************************************/ - -class JDEMRasterBand; - -class JDEMDataset : public GDALPamDataset -{ - friend class JDEMRasterBand; - - FILE *fp; - GByte abyHeader[1012]; - - public: - ~JDEMDataset(); - - static GDALDataset *Open( GDALOpenInfo * ); - - CPLErr GetGeoTransform( double * padfTransform ); - const char *GetProjectionRef(); -}; - -/************************************************************************/ -/* ==================================================================== */ -/* JDEMRasterBand */ -/* ==================================================================== */ -/************************************************************************/ - -class JDEMRasterBand : public GDALPamRasterBand -{ - friend class JDEMDataset; - - public: - - JDEMRasterBand( JDEMDataset *, int ); - - virtual CPLErr IReadBlock( int, int, void * ); -}; - - -/************************************************************************/ -/* JDEMRasterBand() */ -/************************************************************************/ - -JDEMRasterBand::JDEMRasterBand( JDEMDataset *poDS, int nBand ) - -{ - this->poDS = poDS; - this->nBand = nBand; - - eDataType = GDT_Float32; - - nBlockXSize = poDS->GetRasterXSize(); - nBlockYSize = 1; -} - -/************************************************************************/ -/* IReadBlock() */ -/************************************************************************/ - -CPLErr JDEMRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff, - void * pImage ) - -{ - JDEMDataset *poGDS = (JDEMDataset *) poDS; - char *pszRecord; - int nRecordSize = nBlockXSize*5 + 9 + 2; - int i; - - VSIFSeek( poGDS->fp, 1011 + nRecordSize*nBlockYOff, SEEK_SET ); - - pszRecord = (char *) CPLMalloc(nRecordSize); - VSIFRead( pszRecord, 1, nRecordSize, poGDS->fp ); - - if( !EQUALN((char *) poGDS->abyHeader,pszRecord,6) ) - { - CPLFree( pszRecord ); - - CPLError( CE_Failure, CPLE_AppDefined, - "JDEM Scanline corrupt. Perhaps file was not transferred\n" - "in binary mode?" ); - return CE_Failure; - } - - if( JDEMGetField( pszRecord + 6, 3 ) != nBlockYOff + 1 ) - { - CPLFree( pszRecord ); - - CPLError( CE_Failure, CPLE_AppDefined, - "JDEM scanline out of order, JDEM driver does not\n" - "currently support partial datasets." ); - return CE_Failure; - } - - for( i = 0; i < nBlockXSize; i++ ) - ((float *) pImage)[i] = (float) - (JDEMGetField( pszRecord + 9 + 5 * i, 5) * 0.1); - - return CE_None; -} - -/************************************************************************/ -/* ==================================================================== */ -/* JDEMDataset */ -/* ==================================================================== */ -/************************************************************************/ - -/************************************************************************/ -/* ~JDEMDataset() */ -/************************************************************************/ - -JDEMDataset::~JDEMDataset() - -{ - FlushCache(); - if( fp != NULL ) - VSIFClose( fp ); -} - -/************************************************************************/ -/* GetGeoTransform() */ -/************************************************************************/ - -CPLErr JDEMDataset::GetGeoTransform( double * padfTransform ) - -{ - double dfLLLat, dfLLLong, dfURLat, dfURLong; - - dfLLLat = JDEMGetAngle( (char *) abyHeader + 29 ); - dfLLLong = JDEMGetAngle( (char *) abyHeader + 36 ); - dfURLat = JDEMGetAngle( (char *) abyHeader + 43 ); - dfURLong = JDEMGetAngle( (char *) abyHeader + 50 ); - - padfTransform[0] = dfLLLong; - padfTransform[3] = dfURLat; - padfTransform[1] = (dfURLong - dfLLLong) / GetRasterXSize(); - padfTransform[2] = 0.0; - - padfTransform[4] = 0.0; - padfTransform[5] = -1 * (dfURLat - dfLLLat) / GetRasterYSize(); - - - return CE_None; -} - -/************************************************************************/ -/* GetProjectionRef() */ -/************************************************************************/ - -const char *JDEMDataset::GetProjectionRef() - -{ - return( "GEOGCS[\"Tokyo\",DATUM[\"Tokyo\",SPHEROID[\"Bessel 1841\",6377397.155,299.1528128,AUTHORITY[\"EPSG\",7004]],TOWGS84[-148,507,685,0,0,0,0],AUTHORITY[\"EPSG\",6301]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",8901]],UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",9108]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST],AUTHORITY[\"EPSG\",4301]]" ); -} - -/************************************************************************/ -/* Open() */ -/************************************************************************/ - -GDALDataset *JDEMDataset::Open( GDALOpenInfo * poOpenInfo ) - -{ -/* -------------------------------------------------------------------- */ -/* Before trying JDEMOpen() we first verify that there is at */ -/* least one "\n#keyword" type signature in the first chunk of */ -/* the file. */ -/* -------------------------------------------------------------------- */ - if( poOpenInfo->fp == NULL || poOpenInfo->nHeaderBytes < 50 ) - return NULL; - - /* check if century values seem reasonable */ - if( (!EQUALN((char *)poOpenInfo->pabyHeader+11,"19",2) - && !EQUALN((char *)poOpenInfo->pabyHeader+11,"20",2)) - || (!EQUALN((char *)poOpenInfo->pabyHeader+15,"19",2) - && !EQUALN((char *)poOpenInfo->pabyHeader+15,"20",2)) - || (!EQUALN((char *)poOpenInfo->pabyHeader+19,"19",2) - && !EQUALN((char *)poOpenInfo->pabyHeader+19,"20",2)) ) - { - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* Create a corresponding GDALDataset. */ -/* -------------------------------------------------------------------- */ - JDEMDataset *poDS; - - poDS = new JDEMDataset(); - - poDS->fp = poOpenInfo->fp; - poOpenInfo->fp = NULL; - -/* -------------------------------------------------------------------- */ -/* Read the header. */ -/* -------------------------------------------------------------------- */ - VSIFSeek( poDS->fp, 0, SEEK_SET ); - VSIFRead( poDS->abyHeader, 1, 1012, poDS->fp ); - - poDS->nRasterXSize = JDEMGetField( (char *) poDS->abyHeader + 23, 3 ); - poDS->nRasterYSize = JDEMGetField( (char *) poDS->abyHeader + 26, 3 ); - -/* -------------------------------------------------------------------- */ -/* Create band information objects. */ -/* -------------------------------------------------------------------- */ - poDS->SetBand( 1, new JDEMRasterBand( poDS, 1 )); - -/* -------------------------------------------------------------------- */ -/* Initialize any PAM information. */ -/* -------------------------------------------------------------------- */ - poDS->SetDescription( poOpenInfo->pszFilename ); - poDS->TryLoadXML(); - - return( poDS ); -} - -/************************************************************************/ -/* GDALRegister_JDEM() */ -/************************************************************************/ - -void GDALRegister_JDEM() - -{ - GDALDriver *poDriver; - - if( GDALGetDriverByName( "JDEM" ) == NULL ) - { - poDriver = new GDALDriver(); - - poDriver->SetDescription( "JDEM" ); - poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, - "Japanese DEM (.mem)" ); - poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, - "frmt_various.html#JDEM" ); - poDriver->SetMetadataItem( GDAL_DMD_EXTENSION, "mem" ); - - poDriver->pfnOpen = JDEMDataset::Open; - - GetGDALDriverManager()->RegisterDriver( poDriver ); - } -} diff --git a/Utilities/GDAL/frmts/jdem/makefile.vc b/Utilities/GDAL/frmts/jdem/makefile.vc deleted file mode 100644 index fc7eeeeb01a4aaffd4fb9fc2715d38ef8e1b5573..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/jdem/makefile.vc +++ /dev/null @@ -1,15 +0,0 @@ - -OBJ = jdemdataset.obj - -EXTRAFLAGS = -I..\iso8211 - -GDAL_ROOT = ..\.. - -!INCLUDE $(GDAL_ROOT)\nmake.opt - -default: $(OBJ) - copy *.obj ..\o - -clean: - -del *.obj - diff --git a/Utilities/GDAL/frmts/pgchip/INSTALL b/Utilities/GDAL/frmts/pgchip/INSTALL deleted file mode 100644 index 36da77dedb55f674388d44333a4dc8048ebea0e6..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/pgchip/INSTALL +++ /dev/null @@ -1,37 +0,0 @@ -REQUIREMENTS : - -* Make sure you have a full PostgreSQL/Postgis clean source installation -* You need to have Proj4 installed and configured in Pstgis to get the driver work - - -INSTALL NOTES : - -1* Go to frmts directory under GDAL source tree - -2* Unpack pgchip archive in the frmts directory - -3* Edit GNUMakefile to set your Postgis include path - -4* Add registration entry point declaration : - - Open gdal/gcore/gdal_frmts.h - - Add "void CPL_DLL GDALRegister_PGCHIP(void);" between the CPL_C_START and CPL_C_END tags - -5* Add a call to the registration function in frmts/gdalallregister.c - In the GDALAllRegister() function add the followinf lines : - #ifdef FRMT_pgchip - GDALRegister_PGCHIP(); - #endif - -6* Add the format short name to the GDAL_FORMATS macro in GDALmake.opt.in (and to GDALmake.opt) : - - Locate the variable GDAL_FORMATS and add "pgchip" (lowercase) to the list of formats - -7* Add a format specific item to the EXTRAFLAGS macro in frmts/makefile.vc - -8* Recompile your GDAL library : - - make clean - -./configure - - make - - make install - -9* Test your pgchip installation : - execute gdalinfo --formats and search for pgchip \ No newline at end of file diff --git a/Utilities/GDAL/frmts/pgchip/README b/Utilities/GDAL/frmts/pgchip/README deleted file mode 100644 index 5fab1f71a0d0fa25524f5f200891c1d1271d1079..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/pgchip/README +++ /dev/null @@ -1,29 +0,0 @@ -Important Drivers Restrictions : - - * PGCHIP driver is currently under development which means it has NOT been fully tested and no stable release is downloadable. - - * The driver only supports GDT_Byte and GDT_UInt16 datatypes and deals with 1 or 4 bands (GREY_SCALE, PALETTE and RGBA) - - * The column name for the chip is not yet changeable and is "raster" by default - - * In order to specify the database you want to connect to, you have to give a connection string. The differents connection parameters (host,port,dbname) must be delimited with a "#" character. The name of the Postgis layer should be given at the end of the string after a "%layer=" argument. Example : - - $ gdalinfo PG:host=192.168.1.1#dbname=mydb%layer=myRasterTable - - -How can I test the driver : - - * You can choose to build your own application using the GDAL API or use the utility programs. - * Some examples with gdal_translate : - - Import BMP raster : - gdal_translate -of pgchip /DATA/myRaster.bmp PG:host=192.168.1.1#dbname=mydb#port=5432%layer=myRaster - - Then export to PNG : - gdal_translate -of png -ot UInt16 PG:host=192.168.1.1#dbname=mydb#port=5432%layer=myRaster /DATA/myRaster.png - - -Author information and bug report : - - website : http://simon.benjamin.free.fr/pgchip/ - email : noumayoss@gmail.com diff --git a/Utilities/GDAL/frmts/pgchip/makefile.vc b/Utilities/GDAL/frmts/pgchip/makefile.vc deleted file mode 100644 index 87a08aea0d8ed488820c2b30ac83aa48182dc9df..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/pgchip/makefile.vc +++ /dev/null @@ -1,15 +0,0 @@ - -OBJ = pgChipdataset.obj - -EXTRAFLAGS = -I..\iso8211 - -GDAL_ROOT = ..\.. - -!INCLUDE $(GDAL_ROOT)\nmake.opt - -default: $(OBJ) - copy *.obj ..\o - -clean: - -del *.obj - diff --git a/Utilities/GDAL/frmts/pgchip/pgchip.h b/Utilities/GDAL/frmts/pgchip/pgchip.h deleted file mode 100644 index 4e428fbd5c80bcee72878e18b80d5c5fb0779f88..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/pgchip/pgchip.h +++ /dev/null @@ -1,123 +0,0 @@ -/****************************************************************************** - * - * File : pgchip.h - * Project: PGCHIP Driver - * Purpose: Main header file for POSTGIS CHIP/GDAL Driver - * Author: Benjamin Simon, noumayoss@gmail.com - * - ****************************************************************************** - * Copyright (c) 2005, Benjamin Simon, noumayoss@gmail.com - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - ****************************************************************************** - * - * Revision 1.1 2005/08/29 bsimon - * New - * - */ - -#include "gdal_priv.h" -#include "libpq-fe.h" -#include "liblwgeom.h" - -// External functions (what's again the reason for using explicit hex form ?) -extern void deparse_hex_string(unsigned char *strOut,char *strIn,int length); -extern void parse_hex_string(unsigned char *strOut,char *strIn,int length); - -/* color types */ -#define PGCHIP_COLOR_TYPE_GRAY 0 -#define PGCHIP_COLOR_TYPE_PALETTE 1 -#define PGCHIP_COLOR_TYPE_RGB_ALPHA 4 - -//pg_chip color struct -typedef struct pgchip_color_nohex_struct -{ - unsigned char red; - unsigned char green; - unsigned char blue; - unsigned char alpha; -} pgchip_color; - - -/************************************************************************/ -/* ==================================================================== */ -/* PGCHIPDataset */ -/* ==================================================================== */ -/************************************************************************/ - -class PGCHIPRasterBand; - -class PGCHIPDataset : public GDALDataset{ - - friend class PGCHIPRasterBand; - - PGconn *hPGConn; - char *pszConnectionString; - char *pszDBName; - char *pszName; - char *pszProjection; - int bHavePostGIS; - - CHIP *PGCHIP; - int SRID; - int nBitDepth; - - int nColorType; /* PGHIP_COLOR_TYPE_* */ - GDALColorTable *poColorTable; - int bHaveNoData; - double dfNoDataValue; - - double adfGeoTransform[6]; - int bGeoTransformValid; - - public: - - PGCHIPDataset(); - ~PGCHIPDataset(); - - static GDALDataset *Open( GDALOpenInfo * ); - - void printChipInfo(); - - CPLErr GetGeoTransform( double * padfTransform ); - virtual CPLErr SetGeoTransform( double * ); - - CPLErr SetProjection( const char *); - const char *GetProjectionRef(); -}; - -/************************************************************************/ -/* ==================================================================== */ -/* PGCHIPRasterBand */ -/* ==================================================================== */ -/************************************************************************/ - -class PGCHIPRasterBand : public GDALRasterBand{ - - friend class PGCHIPDataset; - - public: - - PGCHIPRasterBand( PGCHIPDataset *, int ); - - virtual CPLErr IReadBlock( int, int, void * ); - virtual GDALColorInterp GetColorInterpretation(); - virtual GDALColorTable *GetColorTable(); - -}; diff --git a/Utilities/GDAL/frmts/pgchip/pgchipdataset.cpp b/Utilities/GDAL/frmts/pgchip/pgchipdataset.cpp deleted file mode 100644 index 5def63f11b440e2add6575aae0c286b06b930cc7..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/pgchip/pgchipdataset.cpp +++ /dev/null @@ -1,1074 +0,0 @@ -/****************************************************************************** - * - * File : pgchipdataset.cpp - * Project: PGCHIP Driver - * Purpose: GDALDataset code for POSTGIS CHIP/GDAL Driver - * Author: Benjamin Simon, noumayoss@gmail.com - * - ****************************************************************************** - * Copyright (c) 2005, Benjamin Simon, noumayoss@gmail.com - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - ****************************************************************************** - * - * Revision 1.1 2005/08/29 bsimon - * New - * - */ - -#include "pgchip.h" - - -CPL_C_START -void GDALRegister_PGCHIP(void); -CPL_C_END - - -/************************************************************************/ -/* ==================================================================== */ -/* PGCHIPDataset */ -/* ==================================================================== */ -/************************************************************************/ - - -/************************************************************************/ -/* PGCHIPDataset() */ -/************************************************************************/ - -PGCHIPDataset::PGCHIPDataset(){ - - hPGConn = NULL; - pszConnectionString = NULL; - pszDBName = NULL; - pszName = NULL; - bHavePostGIS = FALSE; - PGCHIP = NULL; - - bGeoTransformValid = FALSE; - adfGeoTransform[0] = 0.0; - adfGeoTransform[1] = 1.0; - adfGeoTransform[2] = 0.0; - adfGeoTransform[3] = 0.0; - adfGeoTransform[4] = 0.0; - adfGeoTransform[5] = 1.0; - - SRID = -1; - pszProjection = CPLStrdup(""); - - bHaveNoData = FALSE; - dfNoDataValue = -1; -} - -/************************************************************************/ -/* ~PGCHIPDataset() */ -/************************************************************************/ - -PGCHIPDataset::~PGCHIPDataset(){ - - CPLFree(pszProjection); - CPLFree(pszConnectionString); - CPLFree(pszDBName); - CPLFree(pszName); - - - if(PGCHIP->data) - CPLFree(PGCHIP->data); - - if(PGCHIP) - CPLFree(PGCHIP); - -} - -/************************************************************************/ -/* GetGeoTransform() */ -/************************************************************************/ - -CPLErr PGCHIPDataset::GetGeoTransform( double * padfTransform ){ - - memcpy( padfTransform, adfGeoTransform, sizeof(adfGeoTransform[0]) * 6 ); - - if( bGeoTransformValid ) - return CE_None; - else - return CE_Failure; -} - - - -/************************************************************************/ -/* SetGeoTransform() */ -/************************************************************************/ - -CPLErr PGCHIPDataset::SetGeoTransform( double * padfTransform ){ - - CPLErr eErr = CE_None; - - memcpy( adfGeoTransform, padfTransform, sizeof(double) * 6 ); - - if ( pszConnectionString && bGeoTransformValid ) - { - - /* NOT YET AVAILABLE */ - - } - - return eErr; -} - - -/************************************************************************/ -/* GetProjectionRef() */ -/************************************************************************/ - -const char *PGCHIPDataset::GetProjectionRef(){ - - char szCommand[1024]; - PGconn *hPGConn; - PGresult *hResult; - int SRID = -1; - - hPGConn = this->hPGConn; - - SRID = this->PGCHIP->SRID; - -/* -------------------------------------------------------------------- */ -/* Reading proj */ -/* -------------------------------------------------------------------- */ - - sprintf( szCommand,"SELECT srtext FROM spatial_ref_sys where SRID=%d",SRID); - - hResult = PQexec(hPGConn,szCommand); - - if(SRID == -1) { - return ""; - } - else if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK - && PQntuples(hResult) > 0 ){ - - pszProjection = CPLStrdup(PQgetvalue(hResult,0,0)); - - return( pszProjection ); - } - - if( hResult ) - PQclear( hResult ); - - return NULL; -} - - -/************************************************************************/ -/* SetProjection() */ -/************************************************************************/ - -CPLErr PGCHIPDataset::SetProjection( const char * pszNewProjection ){ - - char szCommand[1024]; - PGconn *hPGConn; - PGresult *hResult; - - hPGConn = this->hPGConn; - - - if( !EQUALN(pszNewProjection,"GEOGCS",6) - && !EQUALN(pszNewProjection,"PROJCS",6) - && !EQUALN(pszProjection,"+",1) - && !EQUAL(pszNewProjection,"") ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "Only OGC WKT Projections supported for writing to Postgis.\n" - "%s not supported.", - pszNewProjection ); - - return CE_Failure; - } - - CPLFree( pszProjection ); - -/* -------------------------------------------------------------------- */ -/* Reading SRID */ -/* -------------------------------------------------------------------- */ - - this->SRID = -1; - - if( pszNewProjection[0]=='+') - sprintf( szCommand,"SELECT SRID FROM spatial_ref_sys where proj4text=%s",pszNewProjection); - else - sprintf( szCommand,"SELECT SRID FROM spatial_ref_sys where srtext=%s",pszNewProjection); - - - hResult = PQexec(hPGConn,szCommand); - - - if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK - && PQntuples(hResult) > 0 ){ - - this->SRID = atoi(PQgetvalue(hResult,0,0)); - - pszProjection = CPLStrdup( pszNewProjection ); - - PQclear( hResult ); - - return CE_None; - } - - // Try to find SRID via EPSG number - if (this->SRID == -1 && strcmp(pszNewProjection,"")!=0){ - - char *buf; - char epsg[16]; - memset(epsg,0,16); - char *workingproj = (char *)pszNewProjection; - - while( (buf = strstr(workingproj,"EPSG")) != 0){ - workingproj = buf+4; - } - - int iChar = 0; - workingproj = workingproj + 3; - - while(workingproj[iChar] != '"'){ - epsg[iChar] = workingproj[iChar]; - iChar++; - } - - if(epsg[0] != 0){ - this->SRID = atoi(epsg); - pszProjection = CPLStrdup(pszNewProjection); - } - - return CE_None; - } - else{ - - CPLError( CE_Failure, CPLE_AppDefined, - "Projection %s not found in spatial_ref_sys table.\n", - pszNewProjection ); - - this->SRID = -1; - pszProjection = CPLStrdup(""); - - if( hResult ) - PQclear( hResult ); - - return CE_Failure; - } -} - - - -/************************************************************************/ -/* Open() */ -/************************************************************************/ - -GDALDataset *PGCHIPDataset::Open( GDALOpenInfo * poOpenInfo ){ - - char szCommand[1024]; - PGresult *hResult = NULL; - PGCHIPDataset *poDS = NULL; - char *chipStringHex; - - unsigned char *chipdata; - char *layerName; - int t; - - - /* Chek Postgis connection string */ - if( poOpenInfo->pszFilename == NULL) - return NULL; - -/* -------------------------------------------------------------------- */ -/* Create a corresponding GDALDataset. */ -/* -------------------------------------------------------------------- */ - - poDS = new PGCHIPDataset(); - poDS->pszConnectionString = CPLStrdup(poOpenInfo->pszFilename); - layerName = CPLStrdup(poOpenInfo->pszFilename); - -/* -------------------------------------------------------------------- */ -/* Verify postgresql prefix. */ -/* -------------------------------------------------------------------- */ - if( !EQUALN(poDS->pszConnectionString,"PG:",3) ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "%s does not conform to PostgreSQL naming convention," - " PG:*\n" ); - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* Try to establish connection. */ -/* -------------------------------------------------------------------- */ - int i=0; - while(poDS->pszConnectionString[i] != '\0'){ - - if(poDS->pszConnectionString[i] == '#') - poDS->pszConnectionString[i] = ' '; - if(poDS->pszConnectionString[i] == '%') - poDS->pszConnectionString[i] = '\0'; - i++; - } - - - poDS->hPGConn = PQconnectdb( poDS->pszConnectionString + 3 ); - - if( poDS->hPGConn == NULL || PQstatus(poDS->hPGConn) == CONNECTION_BAD ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "PGconnectcb failed.\n%s", - PQerrorMessage(poDS->hPGConn) ); - PQfinish(poDS->hPGConn); - poDS->hPGConn = NULL; - return NULL; - } - - -/* -------------------------------------------------------------------- */ -/* Try to establish the database name from the connection */ -/* string passed. */ -/* -------------------------------------------------------------------- */ - if( strstr(poDS->pszConnectionString, "dbname=") != NULL ) - { - int i; - - poDS->pszDBName = CPLStrdup( strstr(poDS->pszConnectionString, "dbname=") + 7 ); - - for( i = 0; poDS->pszDBName[i] != '\0'; i++ ) - { - if( poDS->pszDBName[i] == ' ' ) - { - poDS->pszDBName[i] = '\0'; - break; - } - } - } - else if( getenv( "USER" ) != NULL ) - poDS->pszDBName = CPLStrdup( getenv("USER") ); - else - poDS->pszDBName = CPLStrdup( "unknown_dbname" ); - - -/* -------------------------------------------------------------------- */ -/* Test to see if this database instance has support for the */ -/* PostGIS Geometry type. If so, disable sequential scanning */ -/* so we will get the value of the gist indexes. */ -/* -------------------------------------------------------------------- */ - - - hResult = PQexec(poDS->hPGConn, - "SELECT oid FROM pg_type WHERE typname = 'geometry'" ); - - - if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK - && PQntuples(hResult) > 0 ) - { - poDS->bHavePostGIS = TRUE; - } - - if( hResult ) - PQclear( hResult ); - - if(!poDS->bHavePostGIS){ - CPLError( CE_Failure, CPLE_AppDefined, - "Can't find geometry type, is Postgis correctly installed ?\n"); - return NULL; - } - - -/* -------------------------------------------------------------------- */ -/* try opening the layer */ -/* -------------------------------------------------------------------- */ - - if( strstr(layerName, "layer=") != NULL ) - { - poDS->pszName = CPLStrdup( strstr(layerName, "layer=") + 6 ); - } - else - poDS->pszName = CPLStrdup("unknown_layer"); - - -/* -------------------------------------------------------------------- */ -/* Read the chip header */ -/* -------------------------------------------------------------------- */ - - - hResult = PQexec(poDS->hPGConn, "BEGIN"); - - if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) - { - PQclear( hResult ); - sprintf( szCommand, - "SELECT raster FROM %s", - poDS->pszName); - - hResult = PQexec(poDS->hPGConn,szCommand); - } - - - if( !hResult || PQresultStatus(hResult) != PGRES_TUPLES_OK ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "%s", PQerrorMessage(poDS->hPGConn) ); - return NULL; - } - - chipStringHex = PQgetvalue(hResult, 0, 0); - int stringlen = strlen((char *)chipStringHex); - - // Allocating memory for chip - chipdata = (unsigned char *) CPLMalloc(stringlen/2); - - for (t=0;t<stringlen/2;t++){ - chipdata[t] = parse_hex( &chipStringHex[t*2]) ; - } - - // Chip assigment - poDS->PGCHIP = (CHIP *)chipdata; - - if( hResult ) - PQclear( hResult ); - - hResult = PQexec(poDS->hPGConn, "COMMIT"); - PQclear( hResult ); - - -/* -------------------------------------------------------------------- */ -/* Set some information from the file that is of interest. */ -/* -------------------------------------------------------------------- */ - - poDS->nRasterXSize = poDS->PGCHIP->width; - poDS->nRasterYSize = poDS->PGCHIP->height; - poDS->nBands = (int)poDS->PGCHIP->future[0]; - poDS->nBitDepth = (int)poDS->PGCHIP->future[1]; - poDS->nColorType = (int)poDS->PGCHIP->future[2]; - - -/* -------------------------------------------------------------------- */ -/* Create band information objects. */ -/* -------------------------------------------------------------------- */ - for( int iBand = 0; iBand < poDS->nBands; iBand++ ) - poDS->SetBand( iBand+1, new PGCHIPRasterBand( poDS, iBand+1 ) ); - - -/* -------------------------------------------------------------------- */ -/* Is there a palette? Note: we should also read back and */ -/* apply transparency values if available. */ -/* -------------------------------------------------------------------- */ - if( poDS->nColorType == PGCHIP_COLOR_TYPE_PALETTE ) - { - unsigned char *pPalette; - int nColorCount = 0; - int sizePalette = 0; - int offsetColor = -1; - GDALColorEntry oEntry; - - nColorCount = (int)poDS->PGCHIP->compression; - pPalette = (unsigned char *)chipdata + sizeof(CHIP); - sizePalette = nColorCount * sizeof(pgchip_color); - - poDS->poColorTable = new GDALColorTable(); - - for( int iColor = 0; iColor < nColorCount; iColor++ ) - { - oEntry.c1 = pPalette[offsetColor++]; - oEntry.c2 = pPalette[offsetColor++]; - oEntry.c3 = pPalette[offsetColor++]; - oEntry.c4 = pPalette[offsetColor++]; - - poDS->poColorTable->SetColorEntry( iColor, &oEntry ); - } - } - - return( poDS ); -} - - -/************************************************************************/ -/* PGCHIPCreateCopy() */ -/************************************************************************/ -static GDALDataset * PGCHIPCreateCopy( const char * pszFilename, GDALDataset *poSrcDS, - int bStrict, char ** papszOptions, - GDALProgressFunc pfnProgress, void * pProgressData ){ - - - PGconn *hPGConn; - char *pszConnectionString; - char *pszDBName; - char *pszName; - int bHavePostGIS; - char *szCommand; - PGresult *hResult; - char *layerName; - char *pszProjection; - int SRID; - GDALColorTable *poCT= NULL; - - int nXSize = poSrcDS->GetRasterXSize(); - int nYSize = poSrcDS->GetRasterYSize(); - int nBands = poSrcDS->GetRasterCount(); - - -/* -------------------------------------------------------------------- */ -/* Some some rudimentary checks */ -/* -------------------------------------------------------------------- */ - - /* check number of bands */ - if( nBands != 1 && nBands != 4) - { - CPLError( CE_Failure, CPLE_NotSupported, - "Under development : PGCHIP driver doesn't support %d bands. Must be 1 or 4\n", nBands ); - - return NULL; - } - - - if( poSrcDS->GetRasterBand(1)->GetRasterDataType() != GDT_Byte - && poSrcDS->GetRasterBand(1)->GetRasterDataType() != GDT_UInt16) - { - CPLError( CE_Failure, CPLE_NotSupported, - "Under development : PGCHIP driver doesn't support data type %s. " - "Only eight bit (Byte) and sixteen bit (UInt16) bands supported.\n", - GDALGetDataTypeName( - poSrcDS->GetRasterBand(1)->GetRasterDataType()) ); - - return NULL; - } - - /* Check Postgis connection string */ - if( pszFilename == NULL){ - CPLError( CE_Failure, CPLE_NotSupported, - "Connection string is NULL.\n"); - return NULL; - } - - -/* -------------------------------------------------------------------- */ -/* Setup some parameters. */ -/* -------------------------------------------------------------------- */ - - int nBitDepth; - GDALDataType eType; - int storageChunk; - int nColorType=0; - - - if( nBands == 1 && poSrcDS->GetRasterBand(1)->GetColorTable() == NULL ){ - nColorType = PGCHIP_COLOR_TYPE_GRAY; - } - else if( nBands == 1 ){ - nColorType = PGCHIP_COLOR_TYPE_PALETTE; - } - else if( nBands == 4 ){ - nColorType = PGCHIP_COLOR_TYPE_RGB_ALPHA; - } - - if( poSrcDS->GetRasterBand(1)->GetRasterDataType() != GDT_UInt16 ) - { - eType = GDT_Byte; - nBitDepth = 8; - } - else - { - eType = GDT_UInt16; - nBitDepth = 16; - } - - storageChunk = nBitDepth/8; - - printf("nBands = %d, nBitDepth = %d\n",nBands,nBitDepth); - -/* -------------------------------------------------------------------- */ -/* Verify postgresql prefix. */ -/* -------------------------------------------------------------------- */ - - if( !EQUALN(pszFilename,"PG:",3) ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "%s does not conform to PostgreSQL naming convention," - " PG:*\n" ); - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* Try to establish connection. */ -/* -------------------------------------------------------------------- */ - - pszConnectionString = CPLStrdup(pszFilename); - layerName = CPLStrdup(pszFilename); - - int i=0; - while(pszConnectionString[i] != '\0'){ - - if(pszConnectionString[i] == '#') - pszConnectionString[i] = ' '; - - i++; - } - - hPGConn = PQconnectdb( pszConnectionString + 3 ); - - if( hPGConn == NULL || PQstatus(hPGConn) == CONNECTION_BAD ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "PGconnectcb failed.\n%s", - PQerrorMessage(hPGConn) ); - PQfinish(hPGConn); - hPGConn = NULL; - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* Try to establish the database name from the connection */ -/* string passed. */ -/* -------------------------------------------------------------------- */ - - if( strstr(pszFilename, "dbname=") != NULL ) - { - int i; - - pszDBName = CPLStrdup( strstr(pszFilename, "dbname=") + 7 ); - - for( i = 0; pszDBName[i] != '\0'; i++ ) - { - if( pszDBName[i] == ' ' ) - { - pszDBName[i] = '\0'; - break; - } - } - } - else if( getenv( "USER" ) != NULL ) - pszDBName = CPLStrdup( getenv("USER") ); - else - pszDBName = CPLStrdup( "unknown_dbname" ); - - -/* -------------------------------------------------------------------- */ -/* Test to see if this database instance has support for the */ -/* PostGIS Geometry type. If so, disable sequential scanning */ -/* so we will get the value of the gist indexes. */ -/* -------------------------------------------------------------------- */ - - hResult = PQexec(hPGConn, "BEGIN"); - - if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) - { - PQclear( hResult ); - - hResult = PQexec(hPGConn, - "SELECT oid FROM pg_type WHERE typname = 'geometry'" ); - } - - if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK - && PQntuples(hResult) > 0 ) - { - bHavePostGIS = TRUE; - } - else { - CPLError( CE_Failure, CPLE_AppDefined, - "You don't seem to have Postgis installed. Check your settings.\n"); - return NULL; - } - - if( hResult ) - PQclear( hResult ); - - - hResult = PQexec(hPGConn, "COMMIT"); - PQclear( hResult ); - - -/* -------------------------------------------------------------------- */ -/* try opening Postgis Raster Layer */ -/* -------------------------------------------------------------------- */ - - if( strstr(layerName, "layer=") != NULL ) - { - pszName = CPLStrdup( strstr(layerName, "layer=") + 6 ); - } - else - pszName = CPLStrdup("unknown_layer"); - - CPLFree(layerName); - - - // First allocation is small - szCommand = (char *)CPLMalloc(1024); - - hResult = PQexec(hPGConn, "BEGIN"); - - if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) - { - int bTableExists = FALSE; - - PQclear( hResult ); - sprintf( szCommand, - "select b.attname from pg_class a,pg_attribute b where a.oid=b.attrelid and a.relname='%s' and b.attname='raster';", - pszName); - - hResult = PQexec(hPGConn,szCommand); - - if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK - && PQntuples(hResult) > 0 ){ - bTableExists = TRUE; - } - - if(!bTableExists){ - PQclear( hResult ); - sprintf( szCommand, - "CREATE TABLE %s(raster chip)", - pszName); - - hResult = PQexec(hPGConn,szCommand); - } - } - - if( hResult && (PQresultStatus(hResult) == PGRES_COMMAND_OK || PQresultStatus(hResult) == PGRES_TUPLES_OK)){ - PQclear( hResult ); - } - else { - CPLError( CE_Failure, CPLE_AppDefined, - "%s", PQerrorMessage(hPGConn) ); - CPLFree(szCommand); - return NULL; - } - - hResult = PQexec(hPGConn, "COMMIT"); - PQclear( hResult ); - - -/* -------------------------------------------------------------------- */ -/* Projection, finding SRID */ -/* -------------------------------------------------------------------- */ - - pszProjection = (char *)poSrcDS->GetProjectionRef(); - SRID = -1; - - if( !EQUALN(pszProjection,"GEOGCS",6) - && !EQUALN(pszProjection,"PROJCS",6) - && !EQUALN(pszProjection,"+",6) - && !EQUAL(pszProjection,"") ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "Only OGC WKT Projections supported for writing to Postgis.\n" - "%s not supported.", - pszProjection ); - } - - - if( pszProjection[0]=='+') - sprintf( szCommand,"SELECT SRID FROM spatial_ref_sys where proj4text=%s",pszProjection); - else - sprintf( szCommand,"SELECT SRID FROM spatial_ref_sys where srtext=%s",pszProjection); - - hResult = PQexec(hPGConn,szCommand); - - if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK - && PQntuples(hResult) > 0 ){ - - SRID = atoi(PQgetvalue(hResult,0,0)); - - } - - // Try to find SRID via EPSG number - if (SRID == -1 && strcmp(pszProjection,"") != 0){ - - char *buf; - char epsg[16]; - memset(epsg,0,16); - char *workingproj = CPLStrdup( pszProjection ); - - while( (buf = strstr(workingproj,"EPSG")) != 0){ - workingproj = buf+4; - } - - int iChar = 0; - workingproj = workingproj + 3; - - - while(workingproj[iChar] != '"'){ - epsg[iChar] = workingproj[iChar]; - iChar++; - } - - if(epsg[0] != 0){ - SRID = atoi(epsg); - } - } - else{ - CPLError( CE_Failure, CPLE_AppDefined, - "Projection %s not found in spatial_ref_sys table. SRID will be set to -1.\n", - pszProjection ); - - SRID = -1; - } - - if( hResult ) - PQclear( hResult ); - - -/* -------------------------------------------------------------------- */ -/* Write palette if there is one. Technically, I think it is */ -/* possible to write 16bit palettes for PNG, but we will omit */ -/* this for now. */ -/* -------------------------------------------------------------------- */ - - unsigned char *pPalette = NULL; - int bHaveNoData = FALSE; - double dfNoDataValue = -1; - int nbColors = 0,bFoundTrans = FALSE; - int sizePalette = 0; - - if( nColorType == PGCHIP_COLOR_TYPE_PALETTE ) - { - - GDALColorEntry sEntry; - int iColor; - int offsetColor = -1; - - poCT = poSrcDS->GetRasterBand(1)->GetColorTable(); - nbColors = poCT->GetColorEntryCount(); - - sizePalette += sizeof(pgchip_color) * poCT->GetColorEntryCount(); - - pPalette = (unsigned char *) CPLMalloc(sizePalette); - - - for( iColor = 0; iColor < poCT->GetColorEntryCount(); iColor++ ) - { - poCT->GetColorEntryAsRGB( iColor, &sEntry ); - if( sEntry.c4 != 255 ) - bFoundTrans = TRUE; - - pPalette[offsetColor++] = (unsigned char) sEntry.c1; - pPalette[offsetColor++] = (unsigned char) sEntry.c2; - pPalette[offsetColor++] = (unsigned char) sEntry.c3; - - - if( bHaveNoData && iColor == (int) dfNoDataValue ){ - pPalette[offsetColor++] = 0; - } - else{ - pPalette[offsetColor++] = (unsigned char) sEntry.c4; - } - } - } - - -/* -------------------------------------------------------------------- */ -/* Initialize CHIP Structure */ -/* -------------------------------------------------------------------- */ - - CHIP PGCHIP; - - memset(&PGCHIP,0,sizeof(PGCHIP)); - - PGCHIP.factor = 1.0; - PGCHIP.endian_hint = 1; - PGCHIP.compression = nbColors; // To cope with palette extra information : <header><palette><data> - PGCHIP.height = nYSize; - PGCHIP.width = nXSize; - PGCHIP.SRID = SRID; - PGCHIP.future[0] = nBands; //nBands is stored in future variable - PGCHIP.future[1] = nBitDepth; //nBitDepth is stored in future variable - PGCHIP.future[2] = nColorType; //nBitDepth is stored in future variable - PGCHIP.future[3] = nbColors; // Useless as we store nbColors in the "compression" integer - PGCHIP.data = NULL; // Serialized Form - - // PGCHIP.size changes if there is a palette. - // Is calculated by Postgis when inserting anyway - PGCHIP.size = sizeof(CHIP) + (nYSize * nXSize * storageChunk * nBands) + sizePalette; - - switch(storageChunk*nBands){ - case 1 : - PGCHIP.datatype = 8; - break; - case 2 : - PGCHIP.datatype = 6; - break; - case 4 : - // Postgis sets data_size to 4 by default anyway - PGCHIP.datatype = 0; - break; - default : - CPLError( CE_Failure, CPLE_AppDefined,"Under development : ERROR STORAGE CHUNK SIZE NOT SUPPORTED\n"); - break; - } - - -/* -------------------------------------------------------------------- */ -/* Loop over image */ -/* -------------------------------------------------------------------- */ - - CPLErr eErr; - int lineSize = nXSize * storageChunk * nBands; - - // allocating data buffer - GByte *data = (GByte *) CPLMalloc( nYSize * lineSize); - - for( int iLine = 0; iLine < nYSize; iLine++ ){ - for( int iBand = 0; iBand < nBands; iBand++ ){ - - GDALRasterBand * poBand = poSrcDS->GetRasterBand( iBand+1 ); - - eErr = poBand->RasterIO( GF_Read, 0, iLine, nXSize, 1, - data + (iBand*storageChunk) + iLine * lineSize, - nXSize, 1, eType, - nBands * storageChunk, - lineSize ); - } - } - - -/* -------------------------------------------------------------------- */ -/* Write Header, Palette and Data */ -/* -------------------------------------------------------------------- */ - - char *result; - int j=0; - - // Calculating result length (*2 -> Hex form, +1 -> end string) - int size_result = (PGCHIP.size * 2) + 1; - - // memory allocation - result = (char *) CPLMalloc( size_result * sizeof(char)); - - // Assign chip - GByte *header = (GByte *)&PGCHIP; - - // Copy header into result string - for(j=0;j<(int)sizeof(PGCHIP);j++){ - deparse_hex( ((unsigned char *) header)[j], (unsigned char *)&result[j*2]); - } - - // Copy Palette into result string if required - int offsetPalette = (int)sizeof(PGCHIP) * 2; - if(nColorType == PGCHIP_COLOR_TYPE_PALETTE && sizePalette>0){ - for(j=0;j<sizePalette;j++){ - deparse_hex( ((unsigned char *) pPalette)[j], (unsigned char *)&result[offsetPalette + (j*2)]); - } - } - - // Copy data into result string - int offsetData = offsetPalette + sizePalette * 2; - for(j=0;j<(nYSize * lineSize);j++){ - deparse_hex( ((unsigned char *) data)[j], (unsigned char *)&result[offsetData + (j*2)]); - } - - - // end string - result[offsetData + j*2] = '\0'; - - -/* -------------------------------------------------------------------- */ -/* Inserting Chip */ -/* -------------------------------------------------------------------- */ - - // Second allocation to cope with data size - CPLFree(szCommand); - szCommand = (char *)CPLMalloc(PGCHIP.size*2 + 256); - - hResult = PQexec(hPGConn, "BEGIN"); - - if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) - { - - PQclear( hResult ); - sprintf( szCommand, - "INSERT INTO %s(raster) values('%s')", - pszName,result); - - - hResult = PQexec(hPGConn,szCommand); - - } - - if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ){ - PQclear( hResult ); - } - else { - CPLError( CE_Failure, CPLE_AppDefined, - "%s", PQerrorMessage(hPGConn) ); - CPLFree(szCommand); - return NULL; - } - - hResult = PQexec(hPGConn, "COMMIT"); - PQclear( hResult ); - - CPLFree( szCommand ); - CPLFree( pPalette ); - CPLFree( data ); - CPLFree( result ); - - return (GDALDataset *)GDALOpen(pszFilename,GA_Update); -} - - -/************************************************************************/ -/* Display CHIP information */ -/************************************************************************/ -void PGCHIPDataset::printChipInfo(){ - - if(this->PGCHIP != NULL){ - printf("\n---< CHIP INFO >----\n"); - printf("CHIP.datatype = %d\n",this->PGCHIP->datatype); - printf("CHIP.compression = %d\n",this->PGCHIP->compression); - printf("CHIP.size = %d\n",this->PGCHIP->size); - printf("CHIP.factor = %f\n",this->PGCHIP->factor); - printf("CHIP.width = %d\n",this->PGCHIP->width); - printf("CHIP.height = %d\n",this->PGCHIP->height); - printf("CHIP.nBands = %d\n",(int)this->PGCHIP->future[0]); - printf("CHIP.nBitDepth = %d\n",(int)this->PGCHIP->future[1]); - printf("--------------------\n"); - } -} - - -/************************************************************************/ -/* GDALRegister_PGCHIP() */ -/************************************************************************/ -void GDALRegister_PGCHIP(){ - - GDALDriver *poDriver; - - if( GDALGetDriverByName( "PGCHIP" ) == NULL ) - { - poDriver = new GDALDriver(); - - poDriver->SetDescription( "PGCHIP" ); - poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, - "Postgis CHIP raster" ); - - poDriver->SetMetadataItem( GDAL_DMD_CREATIONDATATYPES, - "Byte UInt16" ); - - poDriver->pfnOpen = PGCHIPDataset::Open; - poDriver->pfnCreateCopy = PGCHIPCreateCopy; - - GetGDALDriverManager()->RegisterDriver( poDriver ); - } -} - - - - - - - diff --git a/Utilities/GDAL/frmts/pgchip/pgchiprasterband.cpp b/Utilities/GDAL/frmts/pgchip/pgchiprasterband.cpp deleted file mode 100644 index b33626214de091d3fb92c161debe7b40bb828fd4..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/pgchip/pgchiprasterband.cpp +++ /dev/null @@ -1,218 +0,0 @@ -/****************************************************************************** - * - * File : pgchiprasterband.cpp - * Project: PGCHIP Driver - * Purpose: GDALRasterBand code for POSTGIS CHIP/GDAL Driver - * Author: Benjamin Simon, noumayoss@gmail.com - * - ****************************************************************************** - * Copyright (c) 2005, Benjamin Simon, noumayoss@gmail.com - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - ****************************************************************************** - * - * Revision 1.1 2005/08/29 bsimon - * New - * - */ - -/************************************************************************/ -/* ==================================================================== */ -/* PGCHIPRasterBand */ -/* ==================================================================== */ -/************************************************************************/ - -#include "pgchip.h" - - -/************************************************************************/ -/* PGCHIPRasterBand() */ -/************************************************************************/ - -PGCHIPRasterBand::PGCHIPRasterBand( PGCHIPDataset *poDS, int nBand ){ - - this->poDS = poDS; - this->nBand = nBand; - - if( poDS->nBitDepth == 16 ) - eDataType = GDT_UInt16; - else - eDataType = GDT_Byte; - - nBlockXSize = poDS->GetRasterXSize(); - nBlockYSize = 1; - -} - -/************************************************************************/ -/* IReadBlock() */ -/************************************************************************/ - -CPLErr PGCHIPRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff, - void * pImage ){ - - char szCommand[1024]; - PGCHIPDataset *poGDS = (PGCHIPDataset *) poDS; - PGconn *hPGConn; - PGresult *hResult; - - int chipDataSize; - int bandSize; - int nPixelSize; - int nPixelOffset; - int nXSize; - int i; - - hPGConn = poGDS->hPGConn; - - // Must start on the very left - CPLAssert( nBlockXOff == 0 ); - - - if( poGDS->nBitDepth == 16 ) - nPixelSize = 2; - else - nPixelSize = 1; - - nPixelOffset = poGDS->nBands * nPixelSize; - nXSize = GetXSize(); - bandSize = nPixelSize * nXSize; - int sizePalette = 0; - - if(poGDS->PGCHIP->future[2] == PGCHIP_COLOR_TYPE_PALETTE){ - sizePalette = (int)poGDS->PGCHIP->compression * sizeof(pgchip_color); - } - - // Determine size of whole Image Data - chipDataSize = poGDS->PGCHIP->size - sizeof(CHIP) - sizePalette; - - -/* -------------------------------------------------------------------- */ -/* Reading Chip (first pas only) */ -/* -------------------------------------------------------------------- */ - - if(poGDS->PGCHIP->data == NULL){ - - hResult = PQexec(hPGConn, "BEGIN"); - - if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ) - { - - PQclear( hResult ); - sprintf( szCommand,"SELECT raster FROM %s",poGDS->pszName); - - hResult = PQexec(hPGConn,szCommand); - - char *chipData = PQgetvalue(hResult,0,0); - poGDS->PGCHIP->data = (char *) CPLMalloc(chipDataSize); - - char *data = chipData + (sizePalette + sizeof(CHIP))*2; - - // Reading whole data - for(i=0 ; i<chipDataSize ; i++){ - ((unsigned char *)poGDS->PGCHIP->data)[i] = parse_hex( &data[i*2]) ; - } - - PQclear( hResult ); - - } - else { - CPLError( CE_Failure, CPLE_AppDefined, "%s", PQerrorMessage(hPGConn) ); - PQclear( hResult ); - } - - hResult = PQexec(hPGConn, "COMMIT"); - PQclear( hResult ); - - } - -/* -------------------------------------------------------------------- */ -/* Extracting band from pointer */ -/* -------------------------------------------------------------------- */ - - if(poGDS->PGCHIP->data){ - - - if( nPixelSize == 1 ){ - - char *bufferData = ((char *)poGDS->PGCHIP->data) + (nBlockYOff * poGDS->nBands * bandSize) + ((nBand-1) * nPixelSize); - - for(i = 0; i < nXSize; i++ ){ - ((char *) pImage)[i] = bufferData[i*nPixelOffset]; - } - } - else { - - GUInt16 *bufferData = (GUInt16 *)(((char *)poGDS->PGCHIP->data) + (nBlockYOff * poGDS->nBands * bandSize) + ((nBand-1) * nPixelSize)); - - for(i = 0; i < nXSize; i++ ){ - - ((GUInt16 *) pImage)[i] = bufferData[i*poGDS->nBands]; - - } - } - - } - - return CE_None; -} - - - -/************************************************************************/ -/* GetColorInterpretation() */ -/************************************************************************/ -GDALColorInterp PGCHIPRasterBand::GetColorInterpretation(){ - - PGCHIPDataset *poGDS = (PGCHIPDataset *) poDS; - - if( poGDS->nColorType == PGCHIP_COLOR_TYPE_GRAY ){ - return GCI_GrayIndex; - } - else if( poGDS->nColorType == PGCHIP_COLOR_TYPE_PALETTE ){ - return GCI_PaletteIndex; - } - else if(poGDS->nColorType == PGCHIP_COLOR_TYPE_RGB_ALPHA){ - if( nBand == 1 ) - return GCI_RedBand; - else if( nBand == 2 ) - return GCI_GreenBand; - else if( nBand == 3 ) - return GCI_BlueBand; - else - return GCI_AlphaBand; - } - - return GCI_GrayIndex; -} - - -/************************************************************************/ -/* GetColorTable() */ -/************************************************************************/ - -GDALColorTable *PGCHIPRasterBand::GetColorTable(){ - - PGCHIPDataset *poGDS = (PGCHIPDataset *) poDS; - - if( nBand == 1 ) - return poGDS->poColorTable; - else - return NULL; -} diff --git a/Utilities/GDAL/frmts/pgchip/pgchiputilities.cpp b/Utilities/GDAL/frmts/pgchip/pgchiputilities.cpp deleted file mode 100644 index 9921497c0806d8a4ae5cb95d6ea5dd9d9bcd4b6b..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/pgchip/pgchiputilities.cpp +++ /dev/null @@ -1,290 +0,0 @@ -/****************************************************************************** - * - * File : pgchiputilities.cpp - * Project: PGCHIP Driver - * Purpose: Utility functions for POSTGIS CHIP/GDAL Driver - * Author: Benjamin Simon, noumayoss@gmail.com - * - ****************************************************************************** - * Copyright (c) 2005, Benjamin Simon, noumayoss@gmail.com - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - ****************************************************************************** - * - * Revision 1.1 2005/08/29 bsimon - * New - * - */ - -#include "pgchip.h" - -/************************************************************************/ -/* ==================================================================== */ -/* Utility Hex Functions */ -/* ==================================================================== */ -/************************************************************************/ - -void deparse_hex(unsigned char str, unsigned char *result){ - - int input_high; - int input_low; - - input_high = (str>>4); - input_low = (str & 0x0F); - - switch (input_high) - { - case 0: - result[0] = '0'; - break; - case 1: - result[0] = '1'; - break; - case 2: - result[0] = '2'; - break; - case 3: - result[0] = '3'; - break; - case 4: - result[0] = '4'; - break; - case 5: - result[0] = '5'; - break; - case 6: - result[0] = '6'; - break; - case 7: - result[0] = '7'; - break; - case 8: - result[0] = '8'; - break; - case 9: - result[0] = '9'; - break; - case 10: - result[0] = 'A'; - break; - case 11: - result[0] = 'B'; - break; - case 12: - result[0] = 'C'; - break; - case 13: - result[0] = 'D'; - break; - case 14: - result[0] = 'E'; - break; - case 15: - result[0] = 'F'; - break; - } - - switch (input_low) - { - case 0: - result[1] = '0'; - break; - case 1: - result[1] = '1'; - break; - case 2: - result[1] = '2'; - break; - case 3: - result[1] = '3'; - break; - case 4: - result[1] = '4'; - break; - case 5: - result[1] = '5'; - break; - case 6: - result[1] = '6'; - break; - case 7: - result[1] = '7'; - break; - case 8: - result[1] = '8'; - break; - case 9: - result[1] = '9'; - break; - case 10: - result[1] = 'A'; - break; - case 11: - result[1] = 'B'; - break; - case 12: - result[1] = 'C'; - break; - case 13: - result[1] = 'D'; - break; - case 14: - result[1] = 'E'; - break; - case 15: - result[1] = 'F'; - break; - } -} - -//given a string with at least 2 chars in it, convert them to -// a byte value. No error checking done! -unsigned char parse_hex(char *str){ - - //do this a little brute force to make it faster - - unsigned char result_high = 0; - unsigned char result_low = 0; - - switch (str[0]) - { - case '0' : - result_high = 0; - break; - case '1' : - result_high = 1; - break; - case '2' : - result_high = 2; - break; - case '3' : - result_high = 3; - break; - case '4' : - result_high = 4; - break; - case '5' : - result_high = 5; - break; - case '6' : - result_high = 6; - break; - case '7' : - result_high = 7; - break; - case '8' : - result_high = 8; - break; - case '9' : - result_high = 9; - break; - case 'A' : - result_high = 10; - break; - case 'B' : - result_high = 11; - break; - case 'C' : - result_high = 12; - break; - case 'D' : - result_high = 13; - break; - case 'E' : - result_high = 14; - break; - case 'F' : - result_high = 15; - break; - } - switch (str[1]) - { - case '0' : - result_low = 0; - break; - case '1' : - result_low = 1; - break; - case '2' : - result_low = 2; - break; - case '3' : - result_low = 3; - break; - case '4' : - result_low = 4; - break; - case '5' : - result_low = 5; - break; - case '6' : - result_low = 6; - break; - case '7' : - result_low = 7; - break; - case '8' : - result_low = 8; - break; - case '9' : - result_low = 9; - break; - case 'A' : - result_low = 10; - break; - case 'B' : - result_low = 11; - break; - case 'C' : - result_low = 12; - break; - case 'D' : - result_low = 13; - break; - case 'E' : - result_low = 14; - break; - case 'F' : - result_low = 15; - break; - } - return (unsigned char) ((result_high<<4) + result_low); -} - -/* Parse an hex string */ -void parse_hex_string(unsigned char *strOut,char *strIn,int length){ - - int i; - for(i=0;i<length;i++){ - //printf("Before = %c\n",strIn[i]); - strOut[i] = parse_hex(&strIn[i]); - //printf("After = %c\n",strOut[i]); - } - -} - -/* Deparse an hex string */ -void deparse_hex_string(unsigned char *strOut,char *strIn,int length){ - - int i; - - for(i=0;i<length;i++) - deparse_hex(strIn[i],&strOut[i]); - -} diff --git a/Utilities/GDAL/frmts/pgchip/todo b/Utilities/GDAL/frmts/pgchip/todo deleted file mode 100644 index e9a65ef2234584edf5d954d97eb3a91b62e095e6..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/pgchip/todo +++ /dev/null @@ -1,9 +0,0 @@ -TODO List : - -* Test Driver compatibility with various raster formats -* Modify the connection string to cope with the name of the raster column -* Improve the number of color interpretation options -* Manage geoTrasnform -* Deal with more datatypes -* Improve SRID conversion -* Test makefile.vc (Visual C) diff --git a/Utilities/GDAL/frmts/xpm/makefile.vc b/Utilities/GDAL/frmts/xpm/makefile.vc deleted file mode 100644 index 23b53de7c15efd93930543adc4a29f083bf63fcf..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/xpm/makefile.vc +++ /dev/null @@ -1,15 +0,0 @@ - -OBJ = xpmdataset.obj - -GDAL_ROOT = ..\.. - -EXTRAFLAGS = -I..\mem - -!INCLUDE $(GDAL_ROOT)\nmake.opt - -default: $(OBJ) - copy *.obj ..\o - -clean: - -del *.obj - diff --git a/Utilities/GDAL/frmts/xpm/xpmdataset.cpp b/Utilities/GDAL/frmts/xpm/xpmdataset.cpp deleted file mode 100644 index 02424a0079f979bfc91defff45ddd92673d453cd..0000000000000000000000000000000000000000 --- a/Utilities/GDAL/frmts/xpm/xpmdataset.cpp +++ /dev/null @@ -1,671 +0,0 @@ -/****************************************************************************** - * $Id: xpmdataset.cpp,v 1.13 2006/03/27 17:58:12 fwarmerdam Exp $ - * - * Project: XPM Driver - * Purpose: Implement GDAL XPM Support - * Author: Frank Warmerdam, warmerdam@pobox.com - * - ****************************************************************************** - * Copyright (c) 2002, Frank Warmerdam - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - ****************************************************************************** - * - * $Log: xpmdataset.cpp,v $ - * Revision 1.13 2006/03/27 17:58:12 fwarmerdam - * Fixed last change. - * - * Revision 1.12 2006/03/27 17:57:31 fwarmerdam - * Added check for static keyword. Ivan encountered "XPM" in a raw image. - * - * Revision 1.11 2005/05/05 15:54:49 fwarmerdam - * PAM Enabled - * - * Revision 1.10 2004/11/11 17:07:19 fwarmerdam - * Avoid warnings about using char as a subscript. - * - * Revision 1.9 2003/07/08 21:15:55 warmerda - * avoid warnings - * - * Revision 1.8 2003/04/10 10:24:16 dron - * More leaks fixed. - * - * Revision 1.7 2003/04/09 21:32:02 dron - * Memory leak fixed. - * - * Revision 1.6 2002/11/23 18:54:17 warmerda - * added CREATIONDATATYPES metadata for drivers - * - * Revision 1.5 2002/09/04 06:50:37 warmerda - * avoid static driver pointers - * - * Revision 1.4 2002/06/12 21:12:25 warmerda - * update to metadata based driver info - * - * Revision 1.3 2002/04/16 17:46:47 warmerda - * changed path to memdataset.h - * - * Revision 1.2 2002/04/16 16:12:53 warmerda - * Fixed help topic. - * - * Revision 1.1 2002/04/12 20:13:01 warmerda - * New - * - */ - -#include "gdal_pam.h" -#include "cpl_string.h" -#include "memdataset.h" -#include "gdal_frmts.h" - - -CPL_CVSID("$Id: xpmdataset.cpp,v 1.13 2006/03/27 17:58:12 fwarmerdam Exp $"); - -static unsigned char *ParseXPM( const char *pszInput, - int *pnXSize, int *pnYSize, - GDALColorTable **ppoRetTable ); - - -/************************************************************************/ -/* ==================================================================== */ -/* XPMDataset */ -/* ==================================================================== */ -/************************************************************************/ - -class XPMDataset : public GDALPamDataset -{ - public: - XPMDataset(); - ~XPMDataset(); - - static GDALDataset *Open( GDALOpenInfo * ); -}; - -/************************************************************************/ -/* XPMDataset() */ -/************************************************************************/ - -XPMDataset::XPMDataset() - -{ -} - -/************************************************************************/ -/* ~XPMDataset() */ -/************************************************************************/ - -XPMDataset::~XPMDataset() - -{ - FlushCache(); -} - -/************************************************************************/ -/* Open() */ -/************************************************************************/ - -GDALDataset *XPMDataset::Open( GDALOpenInfo * poOpenInfo ) - -{ -/* -------------------------------------------------------------------- */ -/* First we check to see if the file has the expected header */ -/* bytes. For now we expect the XPM file to start with a line */ -/* containing the letters XPM, and to have "static" in the */ -/* header. */ -/* -------------------------------------------------------------------- */ - if( poOpenInfo->nHeaderBytes < 32 - || strstr((const char *) poOpenInfo->pabyHeader,"XPM") == NULL - || strstr((const char *) poOpenInfo->pabyHeader,"static") == NULL ) - return NULL; - - if( poOpenInfo->eAccess == GA_Update ) - { - CPLError( CE_Failure, CPLE_NotSupported, - "The XPM driver does not support update access to existing" - " files." ); - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* Read the whole file into a memory strings. */ -/* -------------------------------------------------------------------- */ - unsigned int nFileSize; - char *pszFileContents; - - VSIFSeek( poOpenInfo->fp, 0, SEEK_END ); - nFileSize = VSIFTell( poOpenInfo->fp ); - - pszFileContents = (char *) VSIMalloc(nFileSize+1); - if( pszFileContents == NULL ) - { - CPLError( CE_Failure, CPLE_OutOfMemory, - "Insufficient memory for loading XPM file %s into memory.", - poOpenInfo->pszFilename ); - return NULL; - } - - VSIFSeek( poOpenInfo->fp, 0, SEEK_SET ); - - if( VSIFRead( pszFileContents, 1, nFileSize, poOpenInfo->fp ) != nFileSize) - { - CPLFree( pszFileContents ); - CPLError( CE_Failure, CPLE_FileIO, - "Failed to read all %d bytes from file %s.", - nFileSize, poOpenInfo->pszFilename ); - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* Convert into a binary image. */ -/* -------------------------------------------------------------------- */ - GByte *pabyImage; - int nXSize, nYSize; - GDALColorTable *poCT = NULL; - - CPLErrorReset(); - - pabyImage = ParseXPM( pszFileContents, &nXSize, &nYSize, &poCT ); - CPLFree( pszFileContents ); - - if( pabyImage == NULL ) - { - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* Create a corresponding GDALDataset. */ -/* -------------------------------------------------------------------- */ - XPMDataset *poDS; - - poDS = new XPMDataset(); - -/* -------------------------------------------------------------------- */ -/* Capture some information from the file that is of interest. */ -/* -------------------------------------------------------------------- */ - poDS->nRasterXSize = nXSize; - poDS->nRasterYSize = nYSize; - -/* -------------------------------------------------------------------- */ -/* Create band information objects. */ -/* -------------------------------------------------------------------- */ - MEMRasterBand *poBand; - - poBand = new MEMRasterBand( poDS, 1, pabyImage, GDT_Byte, 1, nXSize, - TRUE ); - poBand->SetColorTable( poCT ); - poDS->SetBand( 1, poBand ); - - delete poCT; - -/* -------------------------------------------------------------------- */ -/* Initialize any PAM information. */ -/* -------------------------------------------------------------------- */ - poDS->SetDescription( poOpenInfo->pszFilename ); - poDS->TryLoadXML(); - - return poDS; -} - -/************************************************************************/ -/* XPMCreateCopy() */ -/************************************************************************/ - -static GDALDataset * -XPMCreateCopy( const char * pszFilename, GDALDataset *poSrcDS, - int bStrict, char ** papszOptions, - GDALProgressFunc pfnProgress, void * pProgressData ) - -{ - int nBands = poSrcDS->GetRasterCount(); - int nXSize = poSrcDS->GetRasterXSize(); - int nYSize = poSrcDS->GetRasterYSize(); - GDALColorTable *poCT; - -/* -------------------------------------------------------------------- */ -/* Some some rudimentary checks */ -/* -------------------------------------------------------------------- */ - if( nBands != 1 ) - { - CPLError( CE_Failure, CPLE_NotSupported, - "XPM driver only supports one band images.\n" ); - - return NULL; - } - - if( poSrcDS->GetRasterBand(1)->GetRasterDataType() != GDT_Byte - && bStrict ) - { - CPLError( CE_Failure, CPLE_NotSupported, - "XPM driver doesn't support data type %s. " - "Only eight bit bands supported.\n", - GDALGetDataTypeName( - poSrcDS->GetRasterBand(1)->GetRasterDataType()) ); - - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* If there is no colortable on the source image, create a */ -/* greyscale one with 64 levels of grey. */ -/* -------------------------------------------------------------------- */ - GDALRasterBand *poBand = poSrcDS->GetRasterBand(1); - int i; - GDALColorTable oGreyTable; - - poCT = poBand->GetColorTable(); - if( poCT == NULL ) - { - poCT = &oGreyTable; - - for( i = 0; i < 256; i++ ) - { - GDALColorEntry sColor; - - sColor.c1 = (short) i; - sColor.c2 = (short) i; - sColor.c3 = (short) i; - sColor.c4 = 255; - - poCT->SetColorEntry( i, &sColor ); - } - } - -/* -------------------------------------------------------------------- */ -/* Build list of active colors, and the mapping from pixels to */ -/* our active colormap. */ -/* -------------------------------------------------------------------- */ - const char *pszColorCodes = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-+=[]|:;,.<>?/"; - - int anPixelMapping[256]; - GDALColorEntry asPixelColor[256]; - int nActiveColors = MIN(poCT->GetColorEntryCount(),256); - - // Setup initial colortable and pixel value mapping. - memset( anPixelMapping+0, 0, sizeof(int) * 256 ); - for( i = 0; i < nActiveColors; i++ ) - { - poCT->GetColorEntryAsRGB( i, asPixelColor + i ); - anPixelMapping[i] = i; - } - -/* ==================================================================== */ -/* Iterate merging colors until we are under our limit (about 85). */ -/* ==================================================================== */ - while( nActiveColors > (int) strlen(pszColorCodes) ) - { - int nClosestDistance = 768; - int iClose1 = -1, iClose2 = -1; - int iColor1, iColor2; - - // Find the closest pair of colors. - for( iColor1 = 0; iColor1 < nActiveColors; iColor1++ ) - { - for( iColor2 = iColor1+1; iColor2 < nActiveColors; iColor2++ ) - { - int nDistance; - - if( asPixelColor[iColor1].c4 < 128 - && asPixelColor[iColor2].c4 < 128 ) - nDistance = 0; - else - nDistance = - ABS(asPixelColor[iColor1].c1-asPixelColor[iColor2].c1) - + ABS(asPixelColor[iColor1].c2-asPixelColor[iColor2].c2) - + ABS(asPixelColor[iColor1].c3-asPixelColor[iColor2].c3); - - if( nDistance < nClosestDistance ) - { - nClosestDistance = nDistance; - iClose1 = iColor1; - iClose2 = iColor2; - } - } - - if( nClosestDistance < 8 ) - break; - } - - // This should never happen! - if( iClose1 == -1 ) - break; - - // Merge two selected colors - shift icolor2 into icolor1 and - // move the last active color into icolor2's slot. - for( i = 0; i < 256; i++ ) - { - if( anPixelMapping[i] == iClose2 ) - anPixelMapping[i] = iClose1; - else if( anPixelMapping[i] == nActiveColors-1 ) - anPixelMapping[i] = iClose2; - } - - asPixelColor[iClose2] = asPixelColor[nActiveColors-1]; - nActiveColors--; - } - -/* ==================================================================== */ -/* Write the output image. */ -/* ==================================================================== */ - FILE *fpPBM; - - fpPBM = VSIFOpen( pszFilename, "wt+" ); - if( fpPBM == NULL ) - { - CPLError( CE_Failure, CPLE_OpenFailed, - "Unable to create file `%s'.", - pszFilename ); - - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* Write the header lines. */ -/* -------------------------------------------------------------------- */ - fprintf( fpPBM, "/* XPM */\n" ); - fprintf( fpPBM, "static char *%s[] = {\n", - CPLGetBasename( pszFilename ) ); - fprintf( fpPBM, "/* width height num_colors chars_per_pixel */\n" ); - fprintf( fpPBM, "\" %3d %3d %3d 1\",\n", - nXSize, nYSize, nActiveColors ); - fprintf( fpPBM, "/* colors */\n" ); - -/* -------------------------------------------------------------------- */ -/* Write the color table. */ -/* -------------------------------------------------------------------- */ - for( i = 0; i < nActiveColors; i++ ) - { - if( asPixelColor[i].c4 < 128 ) - fprintf( fpPBM, "\"%c c None\",\n", pszColorCodes[i] ); - else - fprintf( fpPBM, - "\"%c c #%02x%02x%02x\",\n", - pszColorCodes[i], - asPixelColor[i].c1, - asPixelColor[i].c2, - asPixelColor[i].c3 ); - } - -/* -------------------------------------------------------------------- */ -/* Dump image. */ -/* -------------------------------------------------------------------- */ - int iLine; - GByte *pabyScanline; - - pabyScanline = (GByte *) CPLMalloc( nXSize ); - for( iLine = 0; iLine < nYSize; iLine++ ) - { - poBand->RasterIO( GF_Read, 0, iLine, nXSize, 1, - (void *) pabyScanline, nXSize, 1, GDT_Byte, 0, 0 ); - - fputc( '"', fpPBM ); - for( int iPixel = 0; iPixel < nXSize; iPixel++ ) - fputc( pszColorCodes[anPixelMapping[pabyScanline[iPixel]]], - fpPBM); - fprintf( fpPBM, "\",\n" ); - } - - CPLFree( pabyScanline ); - -/* -------------------------------------------------------------------- */ -/* cleanup */ -/* -------------------------------------------------------------------- */ - fprintf( fpPBM, "};\n" ); - VSIFClose( fpPBM ); - -/* -------------------------------------------------------------------- */ -/* Re-open dataset, and copy any auxilary pam information. */ -/* -------------------------------------------------------------------- */ - GDALPamDataset *poDS = (GDALPamDataset *) - GDALOpen( pszFilename, GA_ReadOnly ); - - if( poDS ) - poDS->CloneInfo( poSrcDS, GCIF_PAM_DEFAULT ); - - return poDS; -} - -/************************************************************************/ -/* GDALRegister_XPM() */ -/************************************************************************/ - -void GDALRegister_XPM() - -{ - GDALDriver *poDriver; - - if( GDALGetDriverByName( "XPM" ) == NULL ) - { - poDriver = new GDALDriver(); - - poDriver->SetDescription( "XPM" ); - poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, - "X11 PixMap Format" ); - poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, - "frmt_various.html#XPM" ); - poDriver->SetMetadataItem( GDAL_DMD_EXTENSION, "xpm" ); - poDriver->SetMetadataItem( GDAL_DMD_MIMETYPE, "image/x-xpixmap" ); - poDriver->SetMetadataItem( GDAL_DMD_CREATIONDATATYPES, - "Byte" ); - - poDriver->pfnOpen = XPMDataset::Open; - poDriver->pfnCreateCopy = XPMCreateCopy; - - GetGDALDriverManager()->RegisterDriver( poDriver ); - } -} - -/************************************************************************/ -/* ParseXPM() */ -/************************************************************************/ - -static unsigned char * -ParseXPM( const char *pszInput, int *pnXSize, int *pnYSize, - GDALColorTable **ppoRetTable ) - -{ -/* ==================================================================== */ -/* Parse input into an array of strings from within the first C */ -/* initializer (list os comma separated strings in braces). */ -/* ==================================================================== */ - char **papszXPMList = NULL; - const char *pszNext = pszInput; - int i; - - // Skip till after open brace. - while( *pszNext != '\0' && *pszNext != '{' ) - pszNext++; - - if( *pszNext == '\0' ) - return NULL; - - pszNext++; - - // Read lines till close brace. - - while( *pszNext != '\0' && *pszNext != '}' ) - { - // skip whole comment. - if( EQUALN(pszNext,"/*",2) ) - { - pszNext += 2; - while( *pszNext != '\0' && !EQUALN(pszNext,"*/",2) ) - pszNext++; - } - - // reading string constants - else if( *pszNext == '"' ) - { - char *pszLine; - - pszNext++; - i = 0; - - while( pszNext[i] != '\0' && pszNext[i] != '"' ) - i++; - - pszLine = (char *) CPLMalloc(i+1); - strncpy( pszLine, pszNext, i ); - pszLine[i] = '\0'; - - papszXPMList = CSLAddString( papszXPMList, pszLine ); - CPLFree( pszLine ); - pszNext = pszNext + i + 1; - } - - // just ignore everything else (whitespace, commas, newlines, etc). - else - pszNext++; - } - - if( CSLCount(papszXPMList) < 3 || *pszNext != '}' ) - { - CSLDestroy( papszXPMList ); - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* Get the image information. */ -/* -------------------------------------------------------------------- */ - int nColorCount, nCharsPerPixel; - - if( sscanf( papszXPMList[0], "%d %d %d %d", - pnXSize, pnYSize, &nColorCount, &nCharsPerPixel ) != 4 ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "Image definition (%s) not well formed.", - papszXPMList[0] ); - CSLDestroy( papszXPMList ); - return NULL; - } - - if( nCharsPerPixel != 1 ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "Only one character per pixel XPM images supported by GDAL at this time." ); - CSLDestroy( papszXPMList ); - return NULL; - } - -/* -------------------------------------------------------------------- */ -/* Parse out colors. */ -/* -------------------------------------------------------------------- */ - int iColor; - int anCharLookup[256]; - GDALColorTable oCTable; - - for( i = 0; i < 256; i++ ) - anCharLookup[i] = -1; - - for( iColor = 0; iColor < nColorCount; iColor++ ) - { - char **papszTokens = CSLTokenizeString( papszXPMList[iColor+1]+1 ); - GDALColorEntry sColor; - int nRed, nGreen, nBlue; - - if( CSLCount(papszTokens) != 2 || !EQUAL(papszTokens[0],"c") ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "Ill formed color definition (%s) in XPM header.", - papszXPMList[iColor+1] ); - CSLDestroy( papszXPMList ); - CSLDestroy( papszTokens ); - return NULL; - } - - anCharLookup[(int)papszXPMList[iColor+1][0]] = iColor; - - if( EQUAL(papszTokens[1],"None") ) - { - sColor.c1 = 0; - sColor.c2 = 0; - sColor.c3 = 0; - sColor.c4 = 0; - } - else if( sscanf( papszTokens[1], "#%02x%02x%02x", - &nRed, &nGreen, &nBlue ) != 3 ) - { - CPLError( CE_Failure, CPLE_AppDefined, - "Ill formed color definition (%s) in XPM header.", - papszXPMList[iColor+1] ); - CSLDestroy( papszXPMList ); - CSLDestroy( papszTokens ); - return NULL; - } - else - { - sColor.c1 = (short) nRed; - sColor.c2 = (short) nGreen; - sColor.c3 = (short) nBlue; - sColor.c4 = 255; - } - - oCTable.SetColorEntry( iColor, &sColor ); - - CSLDestroy( papszTokens ); - } - -/* -------------------------------------------------------------------- */ -/* Prepare image buffer. */ -/* -------------------------------------------------------------------- */ - GByte *pabyImage; - - pabyImage = (GByte *) VSIMalloc(*pnXSize * *pnYSize); - if( pabyImage == NULL ) - { - CPLError( CE_Failure, CPLE_OutOfMemory, - "Insufficient memory for %dx%d XPM image buffer.", - *pnXSize, *pnYSize ); - CSLDestroy( papszXPMList ); - return NULL; - } - - memset( pabyImage, 0, *pnXSize * *pnYSize ); - -/* -------------------------------------------------------------------- */ -/* Parse image. */ -/* -------------------------------------------------------------------- */ - for( int iLine = 0; iLine < *pnYSize; iLine++ ) - { - const char *pszInLine = papszXPMList[iLine + nColorCount + 1]; - - if( pszInLine == NULL ) - { - CPLFree( pabyImage ); - CSLDestroy( papszXPMList ); - CPLError( CE_Failure, CPLE_AppDefined, - "Insufficient imagery lines in XPM image." ); - return NULL; - } - - for( int iPixel = 0; - pszInLine[iPixel] != '\0' && iPixel < *pnXSize; - iPixel++ ) - { - int nPixelValue = anCharLookup[(int)pszInLine[iPixel]]; - if( nPixelValue != -1 ) - pabyImage[iLine * *pnXSize + iPixel] = (GByte) nPixelValue; - } - } - - CSLDestroy( papszXPMList ); - - *ppoRetTable = oCTable.Clone(); - - return pabyImage; -}