Changed last remaining mallocs to new

This commit is contained in:
Trygve 2024-05-07 20:34:40 +02:00
parent 7037874923
commit 9ab5aac40f
3 changed files with 15 additions and 12 deletions

View File

@ -6,7 +6,6 @@
#include <gdal/gdal.h> #include <gdal/gdal.h>
#include "gdal/gdal_priv.h" #include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h> #include <gdal/gdal_frmts.h>
//#include "matplotlibcpp.h"
#include "contour_creator.hh" #include "contour_creator.hh"
@ -31,13 +30,13 @@ HeightMap::HeightMap(const char* filepath)
this->min = band->GetMinimum(); this->min = band->GetMinimum();
this->max = band-> GetMaximum(); this->max = band-> GetMaximum();
//https://gdal.org/api/gdaldataset_cpp.html#_CPPv4N11GDALDataset15GetGeoTransformEPd //https://gdal.org/api/gdaldataset_cpp.html#_CPPv4N11GDALDataset15GetGeoTransformEPd
this->geotransform = (double *) CPLMalloc(sizeof(double)*6); this->geotransform = new double[6];
this->reference_system = *(file->GetSpatialRef()); this->reference_system = *(file->GetSpatialRef());
this->filepath = filepath; this->filepath = filepath;
file->GetGeoTransform(this->geotransform); file->GetGeoTransform(this->geotransform);
this->data = (float *) CPLMalloc(sizeof(float)*width*height); this->data = new float[width*height];
CPLErr error = band->RasterIO( GF_Read, 0, 0, width, height, CPLErr error = band->RasterIO( GF_Read, 0, 0, width, height,
this->data, width, height, GDT_Float32, this->data, width, height, GDT_Float32,
0, 0 ); 0, 0 );
@ -48,9 +47,7 @@ HeightMap::HeightMap(const char* filepath)
float HeightMap::get_pixel(int x, int y) float HeightMap::get_pixel(int x, int y)
{ {
// all the pixels are in an array of floats from left to right, top to bottom // all the pixels are in an array of floats from left to right, top to bottom
int offset = ((this->width * y) + x); return this->data[((this->width * y) + x)];
//std::cout << " offset: " << offset << " " << x << ","<< y;
return *(this->data + offset);
} }
void HeightMap::blur(float standard_deviation) void HeightMap::blur(float standard_deviation)
@ -59,10 +56,10 @@ void HeightMap::blur(float standard_deviation)
int kernel_height = 5; int kernel_height = 5;
int kernel_width = 5; int kernel_width = 5;
int kernel_size = kernel_height*kernel_width; int kernel_size = kernel_height*kernel_width;
float* kernel = (float*) CPLMalloc(sizeof(float)*kernel_size); float* kernel = new float [kernel_size];
for (int i=0; i<kernel_size; i++) for (int i=0; i<kernel_size; i++)
{ {
*(kernel + i) = 1.0; kernel[i] = 1.0;
} }
float* blurred = new float[this->width*this->height]; float* blurred = new float[this->width*this->height];
@ -87,10 +84,12 @@ void HeightMap::blur(float standard_deviation)
} }
blurred_pixel += this->get_pixel(x, y); blurred_pixel += this->get_pixel(x, y);
} }
*(blurred + i) = blurred_pixel/float(kernel_size); blurred[i] = blurred_pixel/float(kernel_size);
} }
} }
free(this->data);
delete[] kernel;
delete[] this->data;
this->data = blurred; this->data = blurred;
blurred = nullptr; blurred = nullptr;
} }
@ -139,7 +138,7 @@ void HeightMap::calculate_steepness()
int kernel_width = 5; int kernel_width = 5;
int kernel_size = kernel_height*kernel_width; int kernel_size = kernel_height*kernel_width;
float* kernel = new float[sizeof(float)*kernel_size]; float* kernel = new float[kernel_size];
float* steepness = new float[this->width*this->height]; float* steepness = new float[this->width*this->height];
#pragma omp parallel #pragma omp parallel
{ {

View File

@ -17,6 +17,11 @@ class HeightMap
float max; //!< Maximum value in image float max; //!< Maximum value in image
OGRSpatialReference reference_system; OGRSpatialReference reference_system;
HeightMap(const char* filepath); HeightMap(const char* filepath);
~HeightMap()
{
delete[] data;
delete[] geotransform;
}
const char* filepath; const char* filepath;
float get_pixel(int x,int y); float get_pixel(int x,int y);
void blur(float standard_deviation); void blur(float standard_deviation);

View File

@ -21,7 +21,6 @@
std::vector<Point> produce_cellmap(HeightMap* heightmap, float z) std::vector<Point> produce_cellmap(HeightMap* heightmap, float z)
{ {
int length = (heightmap->width-1)*(heightmap->height-1); // Defining length of vector int length = (heightmap->width-1)*(heightmap->height-1); // Defining length of vector
uint8_t *cells = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length);
std::vector<Point> points; // Initiating a vector of points std::vector<Point> points; // Initiating a vector of points
for (int i = 0; i<length; i++) { for (int i = 0; i<length; i++) {
int y = i/(heightmap->width-1); int y = i/(heightmap->width-1);