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代码分析之异常日志设计 错误异常是面向对象开发中的记录提示程序执行问题的一种重要机制,在程序执行发生问题的条件下,异常会在中断程序执行,同时会沿着代码的执行路径一步一步的向上抛出异常,最 ...
随机推荐
- 113. 路径总和 II
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 ...
- os.path.dirname(__file__)使用
os.path.dirname(__file__)使用 该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py test11.py import os #该文件所在位置 ...
- UVALive 6907 Body Building
题目链接:https://vjudge.net/problem/UVALive-6907 题意: 给出一张图,判断这张图中有多少个哑铃,哑铃判断的条件是,对于一个连通图:如果找到一条边连接这两个点的个 ...
- [USACO06FEC]Milk Patterns --- 后缀数组
[USACO06FEC]Milk Patterns 题目描述: Farmer John has noticed that the quality of milk given by his cows v ...
- 埃及分数 a* 搜索 知识点mark
题意 在古埃及,人们使用单位分数的和(即1/a,a是自然数)表示一切有理 数. 例如,2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为在加数中不允许有相同的. 对于一个分数a/b,表示方法 ...
- Java基础学习——多线程之创建任务
这次来盘点一下Java中用线程执行任务的写法. 1.扩展Thread 最基本的实现方法是在创建一个继承Thread的新类,在其中覆盖run()方法执行任务. public class MyThread ...
- ASP.NET中在一般处理程序中使用session的简单介绍
这篇文章介绍了ASP.NET中在一般处理程序中使用session,有需要的朋友可以参考一下 <%@ WebHandler Language="C#" Class=" ...
- 用Java实现HTTP Multipart的服务端和客户端
今天简单介绍一下如何用Java支持HTTP Multipart的request和response. 整个项目的代码可以在https://github.com/mcai4gl2/multi下载. 在这个 ...
- EntityFramework中几种操作小结
目前项目中使用到的EntityFramework中几种操作小结,先标记下.没有详细介绍,后续有空的话再补充一些并完善一下. 列中加入RowVersion时间戳 public class Product ...
- Tasker文件夹说明
################导入类#################路径 类型 后缀名 /tasker/profiles/ 配置 .prf.xml /tasker/projects/ 项目 .pr ...