现在来仔细分析如何注册一个驱动的代码,看下面代码:
#ifdef FRMT_vrt GDALRegister_VRT(); #endif
EXTRAFLAGS = -DFRMT_nitf -DFRMT_gtiff -DFRMT_jpeg
现在看注册函数内部都怎么实现的。迅速看了一下void GDALRegister_GTiff() 函数的代码,主要是创建一个GDALDriver对象,设置属性,然后调用GetGDALDriverManager()->RegisterDriver(poDriver) 将其添加进去。所以,现在可以回答register是什么的问题。就是为每一个driver,创建一个driver对象,添加到singleton对象GDALDriverManager中去。
注意GDALDriverManger的类声明在gdal_priv.h文件中,里面用了双指针保存所有的Driver的指针以方便象数组一样访问,同时用了<key, value> 结构的map也保存了Driver指针,便于用字符串查找。
class CPL_DLL GDALDriverManager : public GDALMajorObject { int nDrivers; GDALDriver **papoDrivers; char *pszHome; typedef std::map<CPLString, GDALDriver *> NameDriverMap; NameDriverMap m_NameDriverMap;
GDAL的函数注释都写在cpp文件中,这点很好。我一直都想这么干,这样维护代码的时候都方便啊,不用文件跳来跳去。
int GDALDriverManager::RegisterDriver( GDALDriver * poDriver ) 函数代码实现不复杂,就是存放到容器中。
大的算法就这么简单,现在来细看driver里面存放了什么值,代码:
poDriver->SetDescription( "GTiff" ); poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, "GeoTIFF" ); poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, "frmt_gtiff.html" ); poDriver->SetMetadataItem( GDAL_DMD_MIMETYPE, "image/tiff" ); poDriver->SetMetadataItem( GDAL_DMD_EXTENSION, "tif" ); poDriver->SetMetadataItem( GDAL_DMD_CREATIONDATATYPES, "Byte UInt16 Int16 UInt32 Int32 Float32 " "Float64 CInt16 CInt32 CFloat32 CFloat64" ); poDriver->SetMetadataItem( GDAL_DMD_CREATIONOPTIONLIST, szCreateOptions ); poDriver->SetMetadataItem( GDAL_DCAP_VIRTUALIO, "YES" ); poDriver->pfnOpen = GTiffDataset::Open; poDriver->pfnCreate = GTiffDataset::Create; poDriver->pfnCreateCopy = GTiffDataset::CreateCopy; poDriver->pfnUnloadDriver = GDALDeregister_GTiff; poDriver->pfnIdentify = GTiffDataset::Identify;
然后就是设置一些函数指针,有Open, Create, CreateCopy等等。
<CreationOptionList> <Option name="COMPRESS" type="string-select"> <Value>NONE</Value> <Value>LZW</Value> <Value>PACKBITS</Value> <Value>JPEG</Value> <Value>CCITTRLE</Value> <Value>CCITTFAX3</Value> <Value>CCITTFAX4</Value> <Value>DEFLATE</Value> </Option> <Option name="PREDICTOR" type="int" description="Predictor Type" /> <Option name="JPEG_QUALITY" type="int" description="JPEG quality 1-100" default="75" /> <Option name="ZLEVEL" type="int" description="DEFLATE compression level 1-9" default="6" /> <Option name="NBITS" type="int" description="BITS for sub-byte files (1-7), sub-uint16 (9-15), sub-uint32 (17-31)" /> <Option name="INTERLEAVE" type="string-select" default="PIXEL"> <Value>BAND</Value> <Value>PIXEL</Value> </Option> <Option name="TILED" type="boolean" description="Switch to tiled format" /> <Option name="TFW" type="boolean" description="Write out world file" /> <Option name="RPB" type="boolean" description="Write out .RPB (RPC) file" /> <Option name="BLOCKXSIZE" type="int" description="Tile Width" /> <Option name="BLOCKYSIZE" type="int" description="Tile/Strip Height" /> <Option name="PHOTOMETRIC" type="string-select"> <Value>MINISBLACK</Value> <Value>MINISWHITE</Value> <Value>PALETTE</Value> <Value>RGB</Value> <Value>CMYK</Value> <Value>YCBCR</Value> <Value>CIELAB</Value> <Value>ICCLAB</Value> <Value>ITULAB</Value> </Option> <Option name="SPARSE_OK" type="boolean" description="Can newly created files have missing blocks?" default="FALSE" /> <Option name="ALPHA" type="boolean" description="Mark first extrasample as being alpha" /> <Option name="PROFILE" type="string-select" default="GDALGeoTIFF"> <Value>GDALGeoTIFF</Value> <Value>GeoTIFF</Value> <Value>BASELINE</Value> </Option> <Option name="PIXELTYPE" type="string-select"> <Value>DEFAULT</Value> <Value>SIGNEDBYTE</Value> </Option> <Option name="BIGTIFF" type="string-select" description="Force creation of BigTIFF file"> <Value>YES</Value> <Value>NO</Value> <Value>IF_NEEDED</Value> <Value>IF_SAFER</Value> </Option> <Option name="ENDIANNESS" type="string-select" default="NATIVE" description="Force endianness of created file. For DEBUG purpose mostly"> <Value>NATIVE</Value> <Value>INVERTED</Value> <Value>LITTLE</Value> <Value>BIG</Value> </Option> <Option name="COPY_SRC_OVERVIEWS" type="boolean" default="NO" description="Force copy of overviews of source dataset (CreateCopy())" /> </CreationOptionList>