diff --git a/src/tiff_max.cpp b/src/tiff_max.cpp new file mode 100644 index 0000000..4c41e37 --- /dev/null +++ b/src/tiff_max.cpp @@ -0,0 +1,65 @@ +#include "gdal_priv.h" +#include "cpl_conv.h" // for CPLMalloc() +#include +#include + +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; +}