添加gdal cpp benchmark

This commit is contained in:
along 2025-07-15 10:54:46 +08:00
parent c683cbde4d
commit 51dfd717f2

65
src/tiff_max.cpp Normal file
View File

@ -0,0 +1,65 @@
#include "gdal_priv.h"
#include "cpl_conv.h" // for CPLMalloc()
#include <iostream>
#include <ctime>
int main()
{
const char *filename = "imgs/ortho.tif";
// 初始化 GDAL
GDALAllRegister();
clock_t start = clock();
// 打开数据集
GDALDataset *poDataset = (GDALDataset *)GDALOpen(filename, GA_ReadOnly);
if (poDataset == nullptr)
{
std::cerr << "Failed to open file: " << filename << std::endl;
return 1;
}
// 获取第一个波段
GDALRasterBand *poBand = poDataset->GetRasterBand(1);
if (!poBand)
{
std::cerr << "Failed to get raster band." << std::endl;
GDALClose(poDataset);
return 1;
}
int nX = poBand->GetXSize();
int nY = poBand->GetYSize();
// 分配缓冲区
uint8_t *buffer = (uint8_t *)CPLMalloc(sizeof(uint8_t) * nX * nY);
// 读取整个波段数据到内存中
CPLErr err = poBand->RasterIO(GF_Read, 0, 0, nX, nY, buffer, nX, nY, GDT_Byte, 0, 0);
if (err != CE_None)
{
std::cerr << "RasterIO failed." << std::endl;
CPLFree(buffer);
GDALClose(poDataset);
return 1;
}
// 查找最大值
uint8_t maxVal = 0;
for (int i = 0; i < nX * nY; i++)
{
if (buffer[i] > maxVal)
maxVal = buffer[i];
}
clock_t end = clock();
std::cout << "GDAL Max: " << (int)maxVal << std::endl;
std::cout << "Time taken: " << ((double)(end - start)) / CLOCKS_PER_SEC << " seconds" << std::endl;
CPLFree(buffer);
GDALClose(poDataset);
return 0;
}