contour-creator/HeightMap.cpp

39 lines
1.1 KiB
C++
Raw Normal View History

2024-04-06 11:18:28 +00:00
#include <gdal/gdal.h>
#include "gdal/gdal_priv.h"
#include <iostream>
#include <stdfloat>
#include "HeightMap.hh"
HeightMap::HeightMap(const char* filepath)
{
// Open the file with some gdal nonsense:
const GDALAccess eAccess = GA_ReadOnly;
GDALDatasetUniquePtr file;
GDALAllRegister();
file = GDALDatasetUniquePtr(GDALDataset::FromHandle(GDALOpen( filepath, eAccess )));
if( !file )
{
std::cout << "Could not open tiff file!"; // handle error
}
// The heigthmap only has one band
auto band = file->GetBands()[0];
// write the attrributes
this->x_size = band->GetXSize();
this->y_size = band->GetYSize();
this->data = (float *) CPLMalloc(sizeof(float)*x_size*y_size);
band->RasterIO( GF_Read, 0, 0, x_size, y_size,
this->data, x_size, y_size, GDT_Float32,
0, 0 );
band->FlushCache();
}
float HeightMap::get_pixel(int x, int y)
{
// all the pixels are in an array of floats from left to right, top to bottom
int offset = ((this->x_size * y) + x);
return *(this->data + offset);
}