GDAL保存GTiff文件时默认是以BIP的方式保存的,以BSQ方式保存的例程如下:
int main(int argc, char* argv[])
{
char* InputFileName="c:\\Cup95eff.tif";
char* OutputFileName="c:\\TestOut.tif";
GDALAllRegister(); //利用GDAL读取图片,先要进行注册
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8","NO"); //设置支持中文
//准备读取图片
GDALDataset *ReadDataSet=(GDALDataset*)GDALOpen(InputFileName,GA_ReadOnly);
int width=ReadDataSet->GetRasterXSize();
int height=ReadDataSet->GetRasterYSize();
int count=ReadDataSet->GetRasterCount();
float *pDataIn=new float[width*height*count];
if(ReadDataSet->RasterIO(GF_Read,0,0,width,height,pDataIn,width,height,GDT_Float32,count,NULL,0,0,0)==CE_Failure )return -2;
//准备保存图片
const char *pszFormat ="GTiff";
GDALDriver *poDriver=GetGDALDriverManager()->GetDriverByName(pszFormat);
if( poDriver == NULL )
return -1;
char **papszOptions = NULL;
papszOptions = CSLSetNameValue( papszOptions, "INTERLEAVE", "BAND" );
GDALDataset *WriteDataSet = poDriver->Create(OutputFileName, width,height,count,GDT_Float32,papszOptions);
if(WriteDataSet->RasterIO(GF_Write,0,0,width,height,pDataIn,width,height,GDT_Float32,count,NULL,0,0,0)==CE_Failure )return -3;
//保存图像的相关信息
//GetSetGDALDatasetMessage(ReadDataSet,WriteDataSet);
delete ReadDataSet;ReadDataSet=NULL;
delete WriteDataSet;WriteDataSet=NULL;
delete[] pDataIn;pDataIn=NULL;
return 0;
}
关键是设置属性的这一句:
papszOptions = CSLSetNameValue( papszOptions, "INTERLEAVE", "BAND" );