OpenTTD Source  20241108-master-g80f628063a
heightmap.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "stdafx.h"
11 #include "heightmap.h"
12 #include "clear_map.h"
13 #include "void_map.h"
14 #include "error.h"
15 #include "saveload/saveload.h"
16 #include "bmp.h"
17 #include "gfx_func.h"
18 #include "fios.h"
19 #include "fileio_func.h"
20 
21 #include "table/strings.h"
22 
23 #include "safeguards.h"
24 
31 
32 /*
33  * Maximum size in pixels of the heightmap image.
34  */
35 static const uint MAX_HEIGHTMAP_SIZE_PIXELS = 256 << 20; // ~256 million
36 /*
37  * When loading a PNG or BMP the 24 bpp variant requires at least 4 bytes per pixel
38  * of memory to load the data. Make sure the "reasonable" limit is well within the
39  * maximum amount of memory allocatable on 32 bit platforms.
40  */
41 static_assert(MAX_HEIGHTMAP_SIZE_PIXELS < UINT32_MAX / 8);
42 
52 static inline bool IsValidHeightmapDimension(size_t width, size_t height)
53 {
54  return (uint64_t)width * height <= MAX_HEIGHTMAP_SIZE_PIXELS &&
55  width > 0 && width <= MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS &&
56  height > 0 && height <= MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS;
57 }
58 
63 static inline uint8_t RGBToGrayscale(uint8_t red, uint8_t green, uint8_t blue)
64 {
65  /* To avoid doubles and stuff, multiply it with a total of 65536 (16bits), then
66  * divide by it to normalize the value to a byte again. */
67  return ((red * 19595) + (green * 38470) + (blue * 7471)) / 65536;
68 }
69 
70 
71 #ifdef WITH_PNG
72 
73 #include <png.h>
74 
78 static void ReadHeightmapPNGImageData(std::span<uint8_t> map, png_structp png_ptr, png_infop info_ptr)
79 {
80  uint x, y;
81  uint8_t gray_palette[256];
82  png_bytep *row_pointers = nullptr;
83  bool has_palette = png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE;
84  uint channels = png_get_channels(png_ptr, info_ptr);
85 
86  /* Get palette and convert it to grayscale */
87  if (has_palette) {
88  int i;
89  int palette_size;
90  png_color *palette;
91  bool all_gray = true;
92 
93  png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size);
94  for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) {
95  all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue;
96  gray_palette[i] = RGBToGrayscale(palette[i].red, palette[i].green, palette[i].blue);
97  }
98 
105  if (palette_size == 16 && !all_gray) {
106  for (i = 0; i < palette_size; i++) {
107  gray_palette[i] = 256 * i / palette_size;
108  }
109  }
110  }
111 
112  row_pointers = png_get_rows(png_ptr, info_ptr);
113 
114  /* Read the raw image data and convert in 8-bit grayscale */
115  for (x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
116  for (y = 0; y < png_get_image_height(png_ptr, info_ptr); y++) {
117  uint8_t *pixel = &map[y * png_get_image_width(png_ptr, info_ptr) + x];
118  uint x_offset = x * channels;
119 
120  if (has_palette) {
121  *pixel = gray_palette[row_pointers[y][x_offset]];
122  } else if (channels == 3) {
123  *pixel = RGBToGrayscale(row_pointers[y][x_offset + 0],
124  row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]);
125  } else {
126  *pixel = row_pointers[y][x_offset];
127  }
128  }
129  }
130 }
131 
137 static bool ReadHeightmapPNG(const char *filename, uint *x, uint *y, std::vector<uint8_t> *map)
138 {
139  png_structp png_ptr = nullptr;
140  png_infop info_ptr = nullptr;
141 
142  auto fp = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
143  if (!fp.has_value()) {
144  ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR);
145  return false;
146  }
147 
148  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
149  if (png_ptr == nullptr) {
150  ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_MISC, WL_ERROR);
151  return false;
152  }
153 
154  info_ptr = png_create_info_struct(png_ptr);
155  if (info_ptr == nullptr || setjmp(png_jmpbuf(png_ptr))) {
156  ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_MISC, WL_ERROR);
157  png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
158  return false;
159  }
160 
161  png_init_io(png_ptr, *fp);
162 
163  /* Allocate memory and read image, without alpha or 16-bit samples
164  * (result is either 8-bit indexed/grayscale or 24-bit RGB) */
165  png_set_packing(png_ptr);
166  png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_STRIP_16, nullptr);
167 
168  /* Maps of wrong colour-depth are not used.
169  * (this should have been taken care of by stripping alpha and 16-bit samples on load) */
170  if ((png_get_channels(png_ptr, info_ptr) != 1) && (png_get_channels(png_ptr, info_ptr) != 3) && (png_get_bit_depth(png_ptr, info_ptr) != 8)) {
171  ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_IMAGE_TYPE, WL_ERROR);
172  png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
173  return false;
174  }
175 
176  uint width = png_get_image_width(png_ptr, info_ptr);
177  uint height = png_get_image_height(png_ptr, info_ptr);
178 
179  if (!IsValidHeightmapDimension(width, height)) {
180  ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
181  png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
182  return false;
183  }
184 
185  if (map != nullptr) {
186  map->resize(static_cast<size_t>(width) * height);
187  ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
188  }
189 
190  *x = width;
191  *y = height;
192 
193  png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
194  return true;
195 }
196 
197 #endif /* WITH_PNG */
198 
199 
203 static void ReadHeightmapBMPImageData(std::span<uint8_t> map, const BmpInfo &info, const BmpData &data)
204 {
205  uint8_t gray_palette[256];
206 
207  if (!data.palette.empty()) {
208  bool all_gray = true;
209 
210  if (info.palette_size != 2) {
211  for (uint i = 0; i < info.palette_size && (info.palette_size != 16 || all_gray); i++) {
212  all_gray &= data.palette[i].r == data.palette[i].g && data.palette[i].r == data.palette[i].b;
213  gray_palette[i] = RGBToGrayscale(data.palette[i].r, data.palette[i].g, data.palette[i].b);
214  }
215 
222  if (info.palette_size == 16 && !all_gray) {
223  for (uint i = 0; i < info.palette_size; i++) {
224  gray_palette[i] = 256 * i / info.palette_size;
225  }
226  }
227  } else {
232  gray_palette[0] = 0;
233  gray_palette[1] = 16;
234  }
235  }
236 
237  /* Read the raw image data and convert in 8-bit grayscale */
238  for (uint y = 0; y < info.height; y++) {
239  uint8_t *pixel = &map[y * static_cast<size_t>(info.width)];
240  const uint8_t *bitmap = &data.bitmap[y * static_cast<size_t>(info.width) * (info.bpp == 24 ? 3 : 1)];
241 
242  for (uint x = 0; x < info.width; x++) {
243  if (info.bpp != 24) {
244  *pixel++ = gray_palette[*bitmap++];
245  } else {
246  *pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2));
247  bitmap += 3;
248  }
249  }
250  }
251 }
252 
258 static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, std::vector<uint8_t> *map)
259 {
260  auto f = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
261  if (!f.has_value()) {
262  ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR);
263  return false;
264  }
265 
266  RandomAccessFile file(filename, HEIGHTMAP_DIR);
267  BmpInfo info{};
268  BmpData data{};
269 
270  if (!BmpReadHeader(file, info, data)) {
271  ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
272  return false;
273  }
274 
275  if (!IsValidHeightmapDimension(info.width, info.height)) {
276  ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
277  return false;
278  }
279 
280  if (map != nullptr) {
281  if (!BmpReadBitmap(file, info, data)) {
282  ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
283  return false;
284  }
285 
286  map->resize(static_cast<size_t>(info.width) * info.height);
287  ReadHeightmapBMPImageData(*map, info, data);
288  }
289 
290  *x = info.width;
291  *y = info.height;
292 
293  return true;
294 }
295 
303 static void GrayscaleToMapHeights(uint img_width, uint img_height, std::span<const uint8_t> map)
304 {
305  /* Defines the detail of the aspect ratio (to avoid doubles) */
306  const uint num_div = 16384;
307  /* Ensure multiplication with num_div does not cause overflows. */
308  static_assert(num_div <= std::numeric_limits<uint>::max() / MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS);
309 
310  uint width, height;
311  uint row, col;
312  uint row_pad = 0, col_pad = 0;
313  uint img_scale;
314  uint img_row, img_col;
315  TileIndex tile;
316 
317  /* Get map size and calculate scale and padding values */
319  default: NOT_REACHED();
321  width = Map::SizeX();
322  height = Map::SizeY();
323  break;
324  case HM_CLOCKWISE:
325  width = Map::SizeY();
326  height = Map::SizeX();
327  break;
328  }
329 
330  if ((img_width * num_div) / img_height > ((width * num_div) / height)) {
331  /* Image is wider than map - center vertically */
332  img_scale = (width * num_div) / img_width;
333  row_pad = (1 + height - ((img_height * img_scale) / num_div)) / 2;
334  } else {
335  /* Image is taller than map - center horizontally */
336  img_scale = (height * num_div) / img_height;
337  col_pad = (1 + width - ((img_width * img_scale) / num_div)) / 2;
338  }
339 
341  for (uint x = 0; x < Map::SizeX(); x++) MakeVoid(TileXY(x, 0));
342  for (uint y = 0; y < Map::SizeY(); y++) MakeVoid(TileXY(0, y));
343  }
344 
345  /* Form the landscape */
346  for (row = 0; row < height; row++) {
347  for (col = 0; col < width; col++) {
349  default: NOT_REACHED();
350  case HM_COUNTER_CLOCKWISE: tile = TileXY(col, row); break;
351  case HM_CLOCKWISE: tile = TileXY(row, col); break;
352  }
353 
354  /* Check if current tile is within the 1-pixel map edge or padding regions */
356  (row < row_pad) || (row >= (height - row_pad - (_settings_game.construction.freeform_edges ? 0 : 1))) ||
357  (col < col_pad) || (col >= (width - col_pad - (_settings_game.construction.freeform_edges ? 0 : 1)))) {
358  SetTileHeight(tile, 0);
359  } else {
360  /* Use nearest neighbour resizing to scale map data.
361  * We rotate the map 45 degrees (counter)clockwise */
362  img_row = (((row - row_pad) * num_div) / img_scale);
364  default: NOT_REACHED();
366  img_col = (((width - 1 - col - col_pad) * num_div) / img_scale);
367  break;
368  case HM_CLOCKWISE:
369  img_col = (((col - col_pad) * num_div) / img_scale);
370  break;
371  }
372 
373  assert(img_row < img_height);
374  assert(img_col < img_width);
375 
376  uint heightmap_height = map[img_row * img_width + img_col];
377 
378  if (heightmap_height > 0) {
379  /* 0 is sea level.
380  * Other grey scales are scaled evenly to the available height levels > 0.
381  * (The coastline is independent from the number of height levels) */
382  heightmap_height = 1 + (heightmap_height - 1) * _settings_game.game_creation.heightmap_height / 255;
383  }
384 
385  SetTileHeight(tile, heightmap_height);
386  }
387  /* Only clear the tiles within the map area. */
388  if (IsInnerTile(tile)) {
389  MakeClear(tile, CLEAR_GRASS, 3);
390  }
391  }
392  }
393 }
394 
399 void FixSlopes()
400 {
401  uint width, height;
402  int row, col;
403  uint8_t current_tile;
404 
405  /* Adjust height difference to maximum one horizontal/vertical change. */
406  width = Map::SizeX();
407  height = Map::SizeY();
408 
409  /* Top and left edge */
410  for (row = 0; (uint)row < height; row++) {
411  for (col = 0; (uint)col < width; col++) {
412  current_tile = MAX_TILE_HEIGHT;
413  if (col != 0) {
414  /* Find lowest tile; either the top or left one */
415  current_tile = TileHeight(TileXY(col - 1, row)); // top edge
416  }
417  if (row != 0) {
418  if (TileHeight(TileXY(col, row - 1)) < current_tile) {
419  current_tile = TileHeight(TileXY(col, row - 1)); // left edge
420  }
421  }
422 
423  /* Does the height differ more than one? */
424  if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
425  /* Then change the height to be no more than one */
426  SetTileHeight(TileXY(col, row), current_tile + 1);
427  }
428  }
429  }
430 
431  /* Bottom and right edge */
432  for (row = height - 1; row >= 0; row--) {
433  for (col = width - 1; col >= 0; col--) {
434  current_tile = MAX_TILE_HEIGHT;
435  if ((uint)col != width - 1) {
436  /* Find lowest tile; either the bottom and right one */
437  current_tile = TileHeight(TileXY(col + 1, row)); // bottom edge
438  }
439 
440  if ((uint)row != height - 1) {
441  if (TileHeight(TileXY(col, row + 1)) < current_tile) {
442  current_tile = TileHeight(TileXY(col, row + 1)); // right edge
443  }
444  }
445 
446  /* Does the height differ more than one? */
447  if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
448  /* Then change the height to be no more than one */
449  SetTileHeight(TileXY(col, row), current_tile + 1);
450  }
451  }
452  }
453 }
454 
464 static bool ReadHeightMap(DetailedFileType dft, const char *filename, uint *x, uint *y, std::vector<uint8_t> *map)
465 {
466  switch (dft) {
467  default:
468  NOT_REACHED();
469 
470 #ifdef WITH_PNG
471  case DFT_HEIGHTMAP_PNG:
472  return ReadHeightmapPNG(filename, x, y, map);
473 #endif /* WITH_PNG */
474 
475  case DFT_HEIGHTMAP_BMP:
476  return ReadHeightmapBMP(filename, x, y, map);
477  }
478 }
479 
488 bool GetHeightmapDimensions(DetailedFileType dft, const char *filename, uint *x, uint *y)
489 {
490  return ReadHeightMap(dft, filename, x, y, nullptr);
491 }
492 
500 bool LoadHeightmap(DetailedFileType dft, const char *filename)
501 {
502  uint x, y;
503  std::vector<uint8_t> map;
504 
505  if (!ReadHeightMap(dft, filename, &x, &y, &map)) {
506  return false;
507  }
508 
509  GrayscaleToMapHeights(x, y, map);
510 
511  FixSlopes();
513 
514  return true;
515 }
516 
521 void FlatEmptyWorld(uint8_t tile_height)
522 {
523  int edge_distance = _settings_game.construction.freeform_edges ? 0 : 2;
524  for (uint row = edge_distance; row < Map::SizeY() - edge_distance; row++) {
525  for (uint col = edge_distance; col < Map::SizeX() - edge_distance; col++) {
526  SetTileHeight(TileXY(col, row), tile_height);
527  }
528  }
529 
530  FixSlopes();
532 }
Read and write support for bmps.
A file from which bytes, words and double words are read in (potentially) a random order.
Map accessors for 'clear' tiles.
@ CLEAR_GRASS
0-3
Definition: clear_map.h:20
void MakeClear(Tile t, ClearGround g, uint density)
Make a clear tile.
Definition: clear_map.h:259
Functions related to errors.
void ShowErrorMessage(StringID summary_msg, int x, int y, CommandCost cc)
Display an error message in a window.
Definition: error_gui.cpp:367
@ WL_ERROR
Errors (eg. saving/loading failed)
Definition: error.h:26
std::optional< FileHandle > FioFOpenFile(const std::string &filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
Definition: fileio.cpp:242
Functions for Standard In/Out file operations.
DetailedFileType
Kinds of files in each AbstractFileType.
Definition: fileio_type.h:29
@ DFT_HEIGHTMAP_BMP
BMP file.
Definition: fileio_type.h:35
@ DFT_HEIGHTMAP_PNG
PNG file.
Definition: fileio_type.h:36
@ HEIGHTMAP_DIR
Subdirectory of scenario for heightmaps.
Definition: fileio_type.h:120
Declarations for savegames operations.
Functions related to the gfx engine.
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1529
static const uint MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS
Maximum number of pixels for one dimension of a heightmap image.
Definition: heightmap.cpp:30
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 dat...
Definition: heightmap.cpp:303
bool LoadHeightmap(DetailedFileType dft, const char *filename)
Load a heightmap from file and change the map in its current dimensions to a landscape representing t...
Definition: heightmap.cpp:500
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...
Definition: heightmap.cpp:63
bool GetHeightmapDimensions(DetailedFileType dft, const char *filename, uint *x, uint *y)
Get the dimensions of a heightmap.
Definition: heightmap.cpp:488
void FlatEmptyWorld(uint8_t tile_height)
Make an empty world where all tiles are of height 'tile_height'.
Definition: heightmap.cpp:521
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.
Definition: heightmap.cpp:137
static void ReadHeightmapPNGImageData(std::span< uint8_t > map, png_structp png_ptr, png_infop info_ptr)
The PNG Heightmap loader.
Definition: heightmap.cpp:78
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 l...
Definition: heightmap.cpp:52
void FixSlopes()
This function takes care of the fact that land in OpenTTD can never differ more than 1 in height.
Definition: heightmap.cpp:399
static void ReadHeightmapBMPImageData(std::span< uint8_t > map, const BmpInfo &info, const BmpData &data)
The BMP Heightmap loader.
Definition: heightmap.cpp:203
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.
Definition: heightmap.cpp:464
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.
Definition: heightmap.cpp:258
Functions related to creating heightmaps from files.
@ HM_CLOCKWISE
Rotate the map clockwise 45 degrees.
Definition: heightmap.h:21
@ HM_COUNTER_CLOCKWISE
Rotate the map counter clockwise 45 degrees.
Definition: heightmap.h:20
uint DistanceFromEdge(TileIndex tile)
Param the minimum distance to an edge.
Definition: map.cpp:206
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:373
static const uint MAX_MAP_SIZE
Maximal map size = 4096.
Definition: map_type.h:40
A number of safeguards to prevent using unsafe methods.
Functions/types related to saving and loading games.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:57
Definition of base types and functions in a cross-platform compatible way.
Definition: bmp.h:26
Definition: bmp.h:16
uint32_t height
bitmap height
Definition: bmp.h:19
uint32_t width
bitmap width
Definition: bmp.h:18
uint32_t palette_size
number of colours in palette
Definition: bmp.h:23
uint16_t bpp
bits per pixel
Definition: bmp.h:21
bool freeform_edges
allow terraforming the tiles at the map edges
uint8_t heightmap_rotation
rotation director for the heightmap
uint8_t heightmap_height
highest mountain for heightmap (towards what it scales)
ConstructionSettings construction
construction of things in-game
GameCreationSettings game_creation
settings used during the creation of a game (map)
static uint SizeY()
Get the size of the map along the Y.
Definition: map_func.h:279
static debug_inline uint SizeX()
Get the size of the map along the X.
Definition: map_func.h:270
bool IsInnerTile(Tile tile)
Check if a tile is within the map (not a border)
Definition: tile_map.h:109
void SetTileHeight(Tile tile, uint height)
Sets the height of a tile.
Definition: tile_map.h:57
static debug_inline uint TileHeight(Tile tile)
Returns the height of a tile.
Definition: tile_map.h:29
static const uint MAX_TILE_HEIGHT
Maximum allowed tile height.
Definition: tile_type.h:24
Map accessors for void tiles.
void MakeVoid(Tile t)
Make a nice void tile ;)
Definition: void_map.h:19