GDALOpen 代码分析
先来一句话,看了这么多GDAL的源代码,并不喜欢其C风格的烙印太重,还是更喜欢boost风格的简洁的现代C++风格。不过为了更好地应用GDAL,更深的定制它,还是需要将源代码看到底。因为GDAL毕竟是一个很好的图像处理的解决方案。复用它,可以省掉很多人年的工作。
GDALOpen函数代码:注释值得一看
/************************************************************************/
/* GDALOpen() */
/************************************************************************/ /**
* \brief Open a raster file as a GDALDataset.
*
* This function will try to open the passed file, or virtual dataset
* name by invoking the Open method of each registered GDALDriver in turn.
* The first successful open will result in a returned dataset. If all
* drivers fail then NULL is returned and an error is issued.
*
* Several recommandations :
* <ul>
* <li>If you open a dataset object with GA_Update access, it is not recommanded
* to open a new dataset on the same underlying file.</li>
* <li>The returned dataset should only be accessed by one thread at a time. If you
* want to use it from different threads, you must add all necessary code (mutexes, etc.)
* to avoid concurrent use of the object. (Some drivers, such as GeoTIFF, maintain internal
* state variables that are updated each time a new block is read, thus preventing concurrent
* use.) </li>
* </ul>
*
* \sa GDALOpenShared()
*
* @param pszFilename the name of the file to access. In the case of
* exotic drivers this may not refer to a physical file, but instead contain
* information for the driver on how to access a dataset. It should be in UTF8
* encoding.
*
* @param eAccess the desired access, either GA_Update or GA_ReadOnly. Many
* drivers support only read only access.
*
* @return A GDALDatasetH handle or NULL on failure. For C++ applications
* this handle can be cast to a GDALDataset *.
*/ GDALDatasetH CPL_STDCALL
GDALOpen( const char * pszFilename, GDALAccess eAccess ) {
return GDALOpenInternal(pszFilename, eAccess, NULL);
}
注释提示了几个地方:
1. 会依次调用每个已经注册的driver的open函数,第一个成功的会返回Dataset。这个依次应该是按照注册顺序,先注册的driver先被调用。
2. Dataset对象不是线程安全的,使用者自己注意维护多线程环境下的安全性。
3. 返回NULL代表打开失败
GDALDatasetH实际上是个void* , 又是C的玩法。逃过了编译器类型检查。
/** Opaque type used for the C bindings of the C++ GDALDataset class */
typedef void *GDALDatasetH;
实际上就是CDALDataset* 指针。
真正实现代码在下面的函数里面:
/* The drivers listed in papszAllowedDrivers can be in any order */
/* Only the order of registration will be taken into account */
GDALDatasetH GDALOpenInternal( const char * pszFilename, GDALAccess eAccess,
const char* const * papszAllowedDrivers)
{
VALIDATE_POINTER1( pszFilename, "GDALOpen", NULL ); int iDriver;
GDALDriverManager *poDM = GetGDALDriverManager();
GDALOpenInfo oOpenInfo( pszFilename, eAccess );
CPLLocaleC oLocaleForcer; CPLErrorReset();
CPLAssert( NULL != poDM ); for( iDriver = 0; iDriver < poDM->GetDriverCount(); iDriver++ )
{
GDALDriver *poDriver = poDM->GetDriver( iDriver );
GDALDataset *poDS; if (papszAllowedDrivers != NULL &&
CSLFindString((char**)papszAllowedDrivers, GDALGetDriverShortName(poDriver)) == -1)
continue; if ( poDriver->pfnOpen == NULL )
continue; poDS = poDriver->pfnOpen( &oOpenInfo );
if( poDS != NULL )
{
if( strlen(poDS->GetDescription()) == 0 )
poDS->SetDescription( oOpenInfo.pszFilename ); if( poDS->poDriver == NULL )
poDS->poDriver = poDriver; if( CPLGetPID() != GDALGetResponsiblePIDForCurrentThread() )
CPLDebug( "GDAL", "GDALOpen(%s, this=%p) succeeds as %s (pid=%d, responsiblePID=%d).",
pszFilename, poDS, poDriver->GetDescription(),
(int)CPLGetPID(), (int)GDALGetResponsiblePIDForCurrentThread() );
else
CPLDebug( "GDAL", "GDALOpen(%s, this=%p) succeeds as %s.",
pszFilename, poDS, poDriver->GetDescription() ); return (GDALDatasetH) poDS;
} if( CPLGetLastErrorNo() != 0 )
return NULL;
} if( oOpenInfo.bStatOK )
CPLError( CE_Failure, CPLE_OpenFailed,
"`%s' not recognised as a supported file format.\n",
pszFilename );
else
CPLError( CE_Failure, CPLE_OpenFailed,
"`%s' does not exist in the file system,\n"
"and is not recognised as a supported dataset name.\n",
pszFilename ); return NULL;
}
这段就和注释说的一样,遍历driver,依次尝试打开文件。在我的GeoTiff driver中,open方法会调用geotiff.cpp文件的Open方法:
/************************************************************************/
/* Open() */
/************************************************************************/ GDALDataset *GTiffDataset::Open( GDALOpenInfo * poOpenInfo ) {
TIFF *hTIFF;
int bAllowRGBAInterface = TRUE;
const char *pszFilename = poOpenInfo->pszFilename; /* -------------------------------------------------------------------- */
/* Check if it looks like a TIFF file. */
/* -------------------------------------------------------------------- */
if (!Identify(poOpenInfo))
return NULL; if( EQUALN(pszFilename,"GTIFF_RAW:", strlen("GTIFF_RAW:")) )
{
bAllowRGBAInterface = FALSE;
pszFilename += strlen("GTIFF_RAW:");
} /* -------------------------------------------------------------------- */
/* We have a special hook for handling opening a specific */
/* directory of a TIFF file. */
/* -------------------------------------------------------------------- */
if( EQUALN(pszFilename,"GTIFF_DIR:",strlen("GTIFF_DIR:")) )
return OpenDir( poOpenInfo ); if (!GTiffOneTimeInit())
return NULL; /* -------------------------------------------------------------------- */
/* Try opening the dataset. */
/* -------------------------------------------------------------------- */
if( poOpenInfo->eAccess == GA_ReadOnly )
hTIFF = VSI_TIFFOpen( pszFilename, "r" );
else
hTIFF = VSI_TIFFOpen( pszFilename, "r+" ); if( hTIFF == NULL )
return( NULL ); /* -------------------------------------------------------------------- */
/* Create a corresponding GDALDataset. */
/* -------------------------------------------------------------------- */
GTiffDataset *poDS; poDS = new GTiffDataset();
poDS->SetDescription( pszFilename );
poDS->osFilename = pszFilename;
poDS->poActiveDS = poDS; if( poDS->OpenOffset( hTIFF, &(poDS->poActiveDS),
TIFFCurrentDirOffset(hTIFF), TRUE,
poOpenInfo->eAccess,
bAllowRGBAInterface, TRUE,
poOpenInfo->papszSiblingFiles) != CE_None )
{
delete poDS;
return NULL;
} /* -------------------------------------------------------------------- */
/* Initialize any PAM information. */
/* -------------------------------------------------------------------- */
poDS->TryLoadXML();
poDS->ApplyPamInfo(); int i;
for(i=1;i<=poDS->nBands;i++)
{
GTiffRasterBand* poBand = (GTiffRasterBand*) poDS->GetRasterBand(i); /* Load scale, offset and unittype from PAM if available */
if (!poBand->bHaveOffsetScale)
{
poBand->dfScale = poBand->GDALPamRasterBand::GetScale(&poBand->bHaveOffsetScale);
poBand->dfOffset = poBand->GDALPamRasterBand::GetOffset();
}
if (poBand->osUnitType.size() == 0)
{
const char* pszUnitType = poBand->GDALPamRasterBand::GetUnitType();
if (pszUnitType)
poBand->osUnitType = pszUnitType;
}
} poDS->bMetadataChanged = FALSE;
poDS->bGeoTIFFInfoChanged = FALSE; /* -------------------------------------------------------------------- */
/* Check for external overviews. */
/* -------------------------------------------------------------------- */
poDS->oOvManager.Initialize( poDS, pszFilename ); return poDS;
}
这里主要看poDs->OpenOffset函数,它负责读取tiff文件的基本信息,以便后面快速读取。因为这么大的文件,显然不可能一次读进内存来。
/************************************************************************/
/* OpenOffset() */
/* */
/* Initialize the GTiffDataset based on a passed in file */
/* handle, and directory offset to utilize. This is called for */
/* full res, and overview pages. */
/************************************************************************/ CPLErr GTiffDataset::OpenOffset( TIFF *hTIFFIn,
GTiffDataset **ppoActiveDSRef,
toff_t nDirOffsetIn,
int bBaseIn, GDALAccess eAccess,
int bAllowRGBAInterface,
int bReadGeoTransform,
char** papszSiblingFiles )
该函数的定义在geotiff.cpp文件中,非常长,所以这里就不列代码了。
如果要完整的将GeoTiff整个加载过程分析透,需要更多的篇幅。我以后会不断的修改已经有的文章,使得其更准确,并增加新的文章,描述更多的细节。
欢迎讨论。
GDALOpen 代码分析的更多相关文章
- Android代码分析工具lint学习
1 lint简介 1.1 概述 lint是随Android SDK自带的一个静态代码分析工具.它用来对Android工程的源文件进行检查,找出在正确性.安全.性能.可使用性.可访问性及国际化等方面可能 ...
- pmd静态代码分析
在正式进入测试之前,进行一定的静态代码分析及code review对代码质量及系统提高是有帮助的,以上为数据证明 Pmd 它是一个基于静态规则集的Java源码分析器,它可以识别出潜在的如下问题:– 可 ...
- [Asp.net 5] DependencyInjection项目代码分析-目录
微软DI文章系列如下所示: [Asp.net 5] DependencyInjection项目代码分析 [Asp.net 5] DependencyInjection项目代码分析2-Autofac [ ...
- [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(5)(IEnumerable<>补充)
Asp.net 5的依赖注入注入系列可以参考链接: [Asp.net 5] DependencyInjection项目代码分析-目录 我们在之前讲微软的实现时,对于OpenIEnumerableSer ...
- 完整全面的Java资源库(包括构建、操作、代码分析、编译器、数据库、社区等等)
构建 这里搜集了用来构建应用程序的工具. Apache Maven:Maven使用声明进行构建并进行依赖管理,偏向于使用约定而不是配置进行构建.Maven优于Apache Ant.后者采用了一种过程化 ...
- STM32启动代码分析 IAR 比较好
stm32启动代码分析 (2012-06-12 09:43:31) 转载▼ 最近开始使用ST的stm32w108芯片(也是一款zigbee芯片).开始看他的启动代码看的晕晕呼呼呼的. 还好在c ...
- 常用 Java 静态代码分析工具的分析与比较
常用 Java 静态代码分析工具的分析与比较 简介: 本文首先介绍了静态代码分析的基 本概念及主要技术,随后分别介绍了现有 4 种主流 Java 静态代码分析工具 (Checkstyle,FindBu ...
- SonarQube-5.6.3 代码分析平台搭建使用
python代码分析 官网主页: http://docs.sonarqube.org/display/PLUG/Python+Plugin Windows下安装使用: 快速使用: 1.下载jdk ht ...
- angular代码分析之异常日志设计
angular代码分析之异常日志设计 错误异常是面向对象开发中的记录提示程序执行问题的一种重要机制,在程序执行发生问题的条件下,异常会在中断程序执行,同时会沿着代码的执行路径一步一步的向上抛出异常,最 ...
随机推荐
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 xor (根号分治)
xor There is a tree with nn nodes. For each node, there is an integer value a_iai, (1 \le a_i \le ...
- 【JAVAWEB学习笔记】22_ajax:异步校验用户名和站内查询
Js原生Ajax和Jquery的Ajax 学习目标 案例1-异步校验用户名是否存在 案例2-站内查询 一.Ajax概述 1.什么是同步,什么是异步 同步现象:客户端发送请求到服务器端,当服务器返回响应 ...
- java8新特性——接口中的静态方法与默认方法
以前我们知道,接口中的方法必须时抽象方法,而从 java8 开始接口中也可以有方法的实现了,叫做默认方法. 一 .默认方法(default修饰) 在 java8 中,因为存在函数式接口,一个接口中只能 ...
- Varnish与Squid的对比
Varnish与Squid的对比 说到Varnish,就不能不提Squid.Squid是一个高性能的代理缓存服务器,它和Varnish相比较有诸多的异同点,下面进行分析. 下面是Varnish与Squ ...
- SpringBoot 部署 docker 打包镜像
SpringBoot 部署 docker 打包镜像 环境: 1.代码编写工具:IDEA 2.打包:maven 3.docker 4.linux 7.JDK1.8 8.Xshell 9.Xftp 第一步 ...
- [转]Java 对象锁-synchronized()与线程的状态与生命周期
线程的状态与生命周期 Java 对象锁-synchronized() ? 1 2 3 4 synchronized(someObject){ //对象锁 } 对象锁的使用说明: 1.对象锁的返 ...
- 使用辗转相除法求两个数的最大公因数(python实现)
数学背景: 整除的定义: 任给两个整数a,b,其中b≠0,如果存在一个整数q使得等式 a = bq 成立,我们就说是b整除 ...
- 基于CDH,部署Apache Kylin读写分离
一. 部署读写分离的契机 目前公司整体项目稳定运行在CDH5.6版本上,与其搭配的Hbase1.0.0无法正确运行Kylin,原因是Kylin只满足Hbase1.1.x+版本.解决方案如下 1. 升级 ...
- HDU 4678 Mine (2013多校8 1003题 博弈)
Mine Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submis ...
- 【PHP内存泄漏案例】PHP对象递归引用造成内存泄漏
[案例一] 作者:老王 如果PHP对象存在递归引用,就会出现内存泄漏.这个Bug在PHP里已经存在很久很久了,先让我们来重现这个Bug,代码如下: <?php class Foo { funct ...