//把影像读入到内存空间
//把影像读到内存中
/**
* @brief ReadImageToBuff.
* 把数据从内存中输出到文件中
* @created: 2012/03/25
* @author: Zhengwen.Fu
* @param inputFileName 目标文件路径
* @param pImageBuf 内存块指针
* @exception 无
* @return 0 成功
* @return -1 内存分配失败
* @return -4 读取数据失败
* @note
* @remarks
* @history: 1.Created by Zhengwen.Fu on 3 25th,2012.
*/
int ReadImageToBuff(const char* inputFileName,float *&pImageBuf) throw()
{
GDALAllRegister(); //利用GDAL读取图片,先要进行注册
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO"); //设置支持中文路径
//准备读取图片
GDALDataset *ReadDataSet=(GDALDataset*)GDALOpen(inputFileName,GA_ReadOnly);
int width=ReadDataSet->GetRasterXSize();
int height=ReadDataSet->GetRasterYSize();
int bandCount=ReadDataSet->GetRasterCount();
pImageBuf = NULL;
pImageBuf=new(std::nothrow) float[width*height*bandCount];
if(NULL==pImageBuf)
{
delete ReadDataSet;ReadDataSet=NULL;
return -1;
}
if(ReadDataSet->RasterIO(GF_Read,0,0,width,height,pImageBuf,width,height,GDT_Float32,bandCount,NULL,0,0,0)==CE_Failure )
{
delete ReadDataSet;ReadDataSet=NULL;
delete[] pImageBuf;pImageBuf=NULL;
return -4;
}
delete ReadDataSet;ReadDataSet=NULL;
return 0;
}
//用法:
float *pImg=NULL;
ReadImageToBuff("C:\\Test.tif",pImg);
//...
if(pImg!=NULL)
{
delete[] pImg;
pImg = NULL;
{
使用GDAL读取影像
最新推荐文章于 2025-02-12 20:28:41 发布