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代码分析之异常日志设计 错误异常是面向对象开发中的记录提示程序执行问题的一种重要机制,在程序执行发生问题的条件下,异常会在中断程序执行,同时会沿着代码的执行路径一步一步的向上抛出异常,最 ...
随机推荐
- python笔记五:IO与文件
1.python IO: Python提供了两个内置函数从标准输入读入一行文本,默认的标准输入是键盘: 1)raw_input([prompt]) 函数从标准输入读取一个行,并返回一个字符串 2 ...
- JAVA解析xml的四种方式比较
1)DOM解析 DOM是html和xml的应用程序接口(API),以层次结构(类似于树型)来组织节点和信息片段,映射XML文档的结构,允许获取 和操作文档的任意部分,是W3C的官方标准 [优点] ①允 ...
- 【BZOJ 3729】3729: Gty的游戏 (Splay维护dfs序+博弈)
未经博主同意不得转载 3729: Gty的游戏 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 448 Solved: 150 Description ...
- [USACO06FEC]Milk Patterns --- 后缀数组
[USACO06FEC]Milk Patterns 题目描述: Farmer John has noticed that the quality of milk given by his cows v ...
- Ubuntu 12.04下Hadoop 2.2.0 集群搭建(原创)
现在大家可以跟我一起来实现Ubuntu 12.04下Hadoop 2.2.0 集群搭建,在这里我使用了两台服务器,一台作为master即namenode主机,另一台作为slave即datanode主机 ...
- hdu 3038 并查集
题意:给出多个区间的和,判断数据矛盾的区间有几个,比方说[1,5] = 10 ,[6.10] = 10, [1, 10] = 30,这明显第三个与前面两个矛盾. 链接:点我 水题了,val代表到根的 ...
- 53.FIB词链
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Description Fibonacci词定义如下:FIB1 = b FIB2 = a 当k ...
- ASP.Net如何用Cookies保存对象
在ASP.Net中,有时候考虑到较多的使用Session来保存对象,会增加服务器的负载,所以我们会选择用Cookies来保存对象的状态,而Cookies只能保存字符串,这时,我们可以考虑用序列化操作来 ...
- 《python学习手册》第34章 异常对象
基于字符串的异常 python在2.6之前可以使用字符串来定义异常,并且是通过对象标识符来匹配的(即通过is 而不是==) myexc = "My excetion string" ...
- PAT甲级1107. Social Clusters
PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...