OpenTTD Source  20241121-master-g67a0fccfad
heightmap.cpp File Reference

Creating of maps from heightmaps. More...

#include "stdafx.h"
#include "heightmap.h"
#include "clear_map.h"
#include "void_map.h"
#include "error.h"
#include "saveload/saveload.h"
#include "bmp.h"
#include "gfx_func.h"
#include "fios.h"
#include "fileio_func.h"
#include "table/strings.h"
#include "safeguards.h"
#include <png.h>

Go to the source code of this file.

Functions

static bool IsValidHeightmapDimension (size_t width, size_t height)
 Check whether the loaded dimension of the heightmap image are considered valid enough to attempt to load the image. More...
 
static uint8_t RGBToGrayscale (uint8_t red, uint8_t green, uint8_t blue)
 Convert RGB colours to Grayscale using 29.9% Red, 58.7% Green, 11.4% Blue (average luminosity formula, NTSC Colour Space)
 
static void ReadHeightmapPNGImageData (std::span< uint8_t > map, png_structp png_ptr, png_infop info_ptr)
 The PNG Heightmap loader. More...
 
static bool ReadHeightmapPNG (const char *filename, uint *x, uint *y, std::vector< uint8_t > *map)
 Reads the heightmap and/or size of the heightmap from a PNG file. More...
 
static void ReadHeightmapBMPImageData (std::span< uint8_t > map, const BmpInfo &info, const BmpData &data)
 The BMP Heightmap loader. More...
 
static bool ReadHeightmapBMP (const char *filename, uint *x, uint *y, std::vector< uint8_t > *map)
 Reads the heightmap and/or size of the heightmap from a BMP file. More...
 
static void GrayscaleToMapHeights (uint img_width, uint img_height, std::span< const uint8_t > map)
 Converts a given grayscale map to something that fits in OTTD map system and create a map of that data. More...
 
void FixSlopes ()
 This function takes care of the fact that land in OpenTTD can never differ more than 1 in height.
 
static bool ReadHeightMap (DetailedFileType dft, const char *filename, uint *x, uint *y, std::vector< uint8_t > *map)
 Reads the heightmap with the correct file reader. More...
 
bool GetHeightmapDimensions (DetailedFileType dft, const char *filename, uint *x, uint *y)
 Get the dimensions of a heightmap. More...
 
bool LoadHeightmap (DetailedFileType dft, const char *filename)
 Load a heightmap from file and change the map in its current dimensions to a landscape representing the heightmap. More...
 
void FlatEmptyWorld (uint8_t tile_height)
 Make an empty world where all tiles are of height 'tile_height'. More...
 

Variables

static const uint MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS = 2 * MAX_MAP_SIZE
 Maximum number of pixels for one dimension of a heightmap image. More...
 
static const uint MAX_HEIGHTMAP_SIZE_PIXELS = 256 << 20
 

Detailed Description

Creating of maps from heightmaps.

Definition in file heightmap.cpp.

Function Documentation

◆ FlatEmptyWorld()

void FlatEmptyWorld ( uint8_t  tile_height)

Make an empty world where all tiles are of height 'tile_height'.

Parameters
tile_heightof the desired new empty world

Definition at line 521 of file heightmap.cpp.

References _settings_game, GameSettings::construction, FixSlopes(), ConstructionSettings::freeform_edges, MarkWholeScreenDirty(), SetTileHeight(), Map::SizeX(), Map::SizeY(), and TileXY().

◆ GetHeightmapDimensions()

bool GetHeightmapDimensions ( DetailedFileType  dft,
const char *  filename,
uint *  x,
uint *  y 
)

Get the dimensions of a heightmap.

Parameters
dftType of image file.
filenameto query
xdimension x
ydimension y
Returns
Returns false if loading of the image failed.

Definition at line 488 of file heightmap.cpp.

◆ GrayscaleToMapHeights()

static void GrayscaleToMapHeights ( uint  img_width,
uint  img_height,
std::span< const uint8_t >  map 
)
static

Converts a given grayscale map to something that fits in OTTD map system and create a map of that data.

Parameters
img_widththe with of the image in pixels/tiles
img_heightthe height of the image in pixels/tiles
mapthe input map

Definition at line 303 of file heightmap.cpp.

References _settings_game, GameSettings::game_creation, GameCreationSettings::heightmap_rotation, and MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS.

◆ IsValidHeightmapDimension()

static bool IsValidHeightmapDimension ( size_t  width,
size_t  height 
)
inlinestatic

Check whether the loaded dimension of the heightmap image are considered valid enough to attempt to load the image.

In other words, the width and height are not beyond the MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS limit and the total number of pixels does not exceed #MAX_HEIGHTMAP_SIZE_PIXELS. A width or height less than 1 are disallowed too.

Parameters
widthThe width of the to be loaded height map.
heightThe height of the to be loaded height map.
Returns
True iff the dimensions are within the limits.

Definition at line 52 of file heightmap.cpp.

◆ LoadHeightmap()

bool LoadHeightmap ( DetailedFileType  dft,
const char *  filename 
)

Load a heightmap from file and change the map in its current dimensions to a landscape representing the heightmap.

It converts pixels to height. The brighter, the higher.

Parameters
dftType of image file.
filenameof the heightmap file to be imported

Definition at line 500 of file heightmap.cpp.

◆ ReadHeightMap()

static bool ReadHeightMap ( DetailedFileType  dft,
const char *  filename,
uint *  x,
uint *  y,
std::vector< uint8_t > *  map 
)
static

Reads the heightmap with the correct file reader.

Parameters
dftType of image file.
filenameName of the file to load.
[out]xLength of the image.
[out]yHeight of the image.
[in,out]mapIf not nullptr, destination to store the loaded block of image data.
Returns
Whether loading was successful.

Definition at line 464 of file heightmap.cpp.

◆ ReadHeightmapBMP()

static bool ReadHeightmapBMP ( const char *  filename,
uint *  x,
uint *  y,
std::vector< uint8_t > *  map 
)
static

Reads the heightmap and/or size of the heightmap from a BMP file.

If map == nullptr only the size of the BMP is read, otherwise a map with grayscale pixels is allocated and assigned to *map.

Definition at line 258 of file heightmap.cpp.

◆ ReadHeightmapBMPImageData()

static void ReadHeightmapBMPImageData ( std::span< uint8_t >  map,
const BmpInfo info,
const BmpData data 
)
static

The BMP Heightmap loader.

For a non-gray palette of size 16 we assume that the order of the palette determines the height; the first entry is the sea (level 0), the second one level 1, etc.

For a palette of size 2 we assume that the order of the palette determines the height; the first entry is the sea (level 0), the second one is the land (level 1)

Definition at line 203 of file heightmap.cpp.

References BmpInfo::palette_size, and RGBToGrayscale().

◆ ReadHeightmapPNG()

static bool ReadHeightmapPNG ( const char *  filename,
uint *  x,
uint *  y,
std::vector< uint8_t > *  map 
)
static

Reads the heightmap and/or size of the heightmap from a PNG file.

If map == nullptr only the size of the PNG is read, otherwise a map with grayscale pixels is allocated and assigned to *map.

Definition at line 137 of file heightmap.cpp.

◆ ReadHeightmapPNGImageData()

static void ReadHeightmapPNGImageData ( std::span< uint8_t >  map,
png_structp  png_ptr,
png_infop  info_ptr 
)
static

The PNG Heightmap loader.

For a non-gray palette of size 16 we assume that the order of the palette determines the height; the first entry is the sea (level 0), the second one level 1, etc.

Definition at line 78 of file heightmap.cpp.

Variable Documentation

◆ MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS

const uint MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS = 2 * MAX_MAP_SIZE
static

Maximum number of pixels for one dimension of a heightmap image.

Do not allow images for which the longest side is twice the maximum number of tiles along the longest side of the (tile) map.

Definition at line 30 of file heightmap.cpp.

Referenced by GrayscaleToMapHeights().