基于GDAL库读取tiff文件的C++代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>

#include "gdal_priv.h"
#include "gdal.h"

BYTE * imread(const char *filename, int &nbands, int &lWidth, int &lHeight)
{
	GDALAllRegister();
	GDALDataset *pDataSet = (GDALDataset *)GDALOpen(filename, GA_ReadOnly);
	if (!pDataSet)
	{
		printf("open tif file failed!\n");
		return NULL;
	}
	nbands = pDataSet->GetRasterCount();
	lWidth = pDataSet->GetRasterXSize();
	lHeight = pDataSet->GetRasterYSize();
	BYTE* pData = new BYTE[lWidth*lHeight*nbands];//{x,y}(0),{x,y}(1),...,{x,y}(n)
	pDataSet->RasterIO(GF_Read, 0, 0, lWidth, lHeight, pData, lWidth, lHeight, GDT_Byte, nbands, 0, 0, 0, 0);

	return pData;
}
发布了134 篇原创文章 · 获赞 116 · 访问量 65万+

猜你喜欢

转载自blog.csdn.net/shixin_0125/article/details/103927750
今日推荐