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/>.
8 /** @file heightmap.cpp Creating of maps from heightmaps. */
11 #include "heightmap.h"
12 #include "clear_map.h"
15 #include "saveload/saveload.h"
19 #include "fileio_func.h"
21 #include "table/strings.h"
23 #include "safeguards.h"
26 * Maximum number of pixels for one dimension of a heightmap image.
27 * Do not allow images for which the longest side is twice the maximum number of
28 * tiles along the longest side of the (tile) map.
30 static const uint MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS
= 2 * MAX_MAP_SIZE
;
33 * Maximum size in pixels of the heightmap image.
35 static const uint MAX_HEIGHTMAP_SIZE_PIXELS
= 256 << 20; // ~256 million
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.
41 static_assert(MAX_HEIGHTMAP_SIZE_PIXELS
< UINT32_MAX
/ 8);
44 * Check whether the loaded dimension of the heightmap image are considered valid enough
45 * to attempt to load the image. In other words, the width and height are not beyond the
46 * #MAX_HEIGHTMAP_SIDE_LENGTH_IN_PIXELS limit and the total number of pixels does not
47 * exceed #MAX_HEIGHTMAP_SIZE_PIXELS. A width or height less than 1 are disallowed too.
48 * @param width The width of the to be loaded height map.
49 * @param height The height of the to be loaded height map.
50 * @return True iff the dimensions are within the limits.
52 static inline bool IsValidHeightmapDimension(size_t width
, size_t height
)
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
;
60 * Convert RGB colours to Grayscale using 29.9% Red, 58.7% Green, 11.4% Blue
61 * (average luminosity formula, NTSC Colour Space)
63 static inline uint8_t RGBToGrayscale(uint8_t red
, uint8_t green
, uint8_t blue
)
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;
76 * The PNG Heightmap loader.
78 static void ReadHeightmapPNGImageData(std::span
<uint8_t> map
, png_structp png_ptr
, png_infop info_ptr
)
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
);
86 /* Get palette and convert it to grayscale */
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
);
100 * For a non-gray palette of size 16 we assume that
101 * the order of the palette determines the height;
102 * the first entry is the sea (level 0), the second one
105 if (palette_size
== 16 && !all_gray
) {
106 for (i
= 0; i
< palette_size
; i
++) {
107 gray_palette
[i
] = 256 * i
/ palette_size
;
112 row_pointers
= png_get_rows(png_ptr
, info_ptr
);
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
;
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]);
126 *pixel
= row_pointers
[y
][x_offset
];
133 * Reads the heightmap and/or size of the heightmap from a PNG file.
134 * If map == nullptr only the size of the PNG is read, otherwise a map
135 * with grayscale pixels is allocated and assigned to *map.
137 static bool ReadHeightmapPNG(const char *filename
, uint
*x
, uint
*y
, std::vector
<uint8_t> *map
)
139 png_structp png_ptr
= nullptr;
140 png_infop info_ptr
= nullptr;
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
);
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
);
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);
161 png_init_io(png_ptr
, *fp
);
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);
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);
176 uint width
= png_get_image_width(png_ptr
, info_ptr
);
177 uint height
= png_get_image_height(png_ptr
, info_ptr
);
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);
185 if (map
!= nullptr) {
186 map
->resize(static_cast<size_t>(width
) * height
);
187 ReadHeightmapPNGImageData(*map
, png_ptr
, info_ptr
);
193 png_destroy_read_struct(&png_ptr
, &info_ptr
, nullptr);
197 #endif /* WITH_PNG */
201 * The BMP Heightmap loader.
203 static void ReadHeightmapBMPImageData(std::span
<uint8_t> map
, const BmpInfo
&info
, const BmpData
&data
)
205 uint8_t gray_palette
[256];
207 if (!data
.palette
.empty()) {
208 bool all_gray
= true;
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
);
217 * For a non-gray palette of size 16 we assume that
218 * the order of the palette determines the height;
219 * the first entry is the sea (level 0), the second one
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
;
229 * For a palette of size 2 we assume that the order of the palette determines the height;
230 * the first entry is the sea (level 0), the second one is the land (level 1)
233 gray_palette
[1] = 16;
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)];
242 for (uint x
= 0; x
< info
.width
; x
++) {
243 if (info
.bpp
!= 24) {
244 *pixel
++ = gray_palette
[*bitmap
++];
246 *pixel
++ = RGBToGrayscale(*bitmap
, *(bitmap
+ 1), *(bitmap
+ 2));
254 * Reads the heightmap and/or size of the heightmap from a BMP file.
255 * If map == nullptr only the size of the BMP is read, otherwise a map
256 * with grayscale pixels is allocated and assigned to *map.
258 static bool ReadHeightmapBMP(const char *filename
, uint
*x
, uint
*y
, std::vector
<uint8_t> *map
)
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
);
266 RandomAccessFile
file(filename
, HEIGHTMAP_DIR
);
270 if (!BmpReadHeader(file
, info
, data
)) {
271 ShowErrorMessage(STR_ERROR_BMPMAP
, STR_ERROR_BMPMAP_IMAGE_TYPE
, WL_ERROR
);
275 if (!IsValidHeightmapDimension(info
.width
, info
.height
)) {
276 ShowErrorMessage(STR_ERROR_BMPMAP
, STR_ERROR_HEIGHTMAP_TOO_LARGE
, WL_ERROR
);
280 if (map
!= nullptr) {
281 if (!BmpReadBitmap(file
, info
, data
)) {
282 ShowErrorMessage(STR_ERROR_BMPMAP
, STR_ERROR_BMPMAP_IMAGE_TYPE
, WL_ERROR
);
286 map
->resize(static_cast<size_t>(info
.width
) * info
.height
);
287 ReadHeightmapBMPImageData(*map
, info
, data
);
297 * Converts a given grayscale map to something that fits in OTTD map system
298 * and create a map of that data.
299 * @param img_width the with of the image in pixels/tiles
300 * @param img_height the height of the image in pixels/tiles
301 * @param map the input map
303 static void GrayscaleToMapHeights(uint img_width
, uint img_height
, std::span
<const uint8_t> map
)
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
);
312 uint row_pad
= 0, col_pad
= 0;
314 uint img_row
, img_col
;
317 /* Get map size and calculate scale and padding values */
318 switch (_settings_game
.game_creation
.heightmap_rotation
) {
319 default: NOT_REACHED();
320 case HM_COUNTER_CLOCKWISE
:
321 width
= Map::SizeX();
322 height
= Map::SizeY();
325 width
= Map::SizeY();
326 height
= Map::SizeX();
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;
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;
340 if (_settings_game
.construction
.freeform_edges
) {
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
));
345 /* Form the landscape */
346 for (row
= 0; row
< height
; row
++) {
347 for (col
= 0; col
< width
; col
++) {
348 switch (_settings_game
.game_creation
.heightmap_rotation
) {
349 default: NOT_REACHED();
350 case HM_COUNTER_CLOCKWISE
: tile
= TileXY(col
, row
); break;
351 case HM_CLOCKWISE
: tile
= TileXY(row
, col
); break;
354 /* Check if current tile is within the 1-pixel map edge or padding regions */
355 if ((!_settings_game
.construction
.freeform_edges
&& DistanceFromEdge(tile
) <= 1) ||
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);
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
);
363 switch (_settings_game
.game_creation
.heightmap_rotation
) {
364 default: NOT_REACHED();
365 case HM_COUNTER_CLOCKWISE
:
366 img_col
= (((width
- 1 - col
- col_pad
) * num_div
) / img_scale
);
369 img_col
= (((col
- col_pad
) * num_div
) / img_scale
);
373 assert(img_row
< img_height
);
374 assert(img_col
< img_width
);
376 uint heightmap_height
= map
[img_row
* img_width
+ img_col
];
378 if (heightmap_height
> 0) {
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;
385 SetTileHeight(tile
, heightmap_height
);
387 /* Only clear the tiles within the map area. */
388 if (IsInnerTile(tile
)) {
389 MakeClear(tile
, CLEAR_GRASS
, 3);
396 * This function takes care of the fact that land in OpenTTD can never differ
397 * more than 1 in height
403 uint8_t current_tile
;
405 /* Adjust height difference to maximum one horizontal/vertical change. */
406 width
= Map::SizeX();
407 height
= Map::SizeY();
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
;
414 /* Find lowest tile; either the top or left one */
415 current_tile
= TileHeight(TileXY(col
- 1, row
)); // top edge
418 if (TileHeight(TileXY(col
, row
- 1)) < current_tile
) {
419 current_tile
= TileHeight(TileXY(col
, row
- 1)); // left edge
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);
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
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
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);
456 * Reads the heightmap with the correct file reader.
457 * @param dft Type of image file.
458 * @param filename Name of the file to load.
459 * @param[out] x Length of the image.
460 * @param[out] y Height of the image.
461 * @param[in,out] map If not \c nullptr, destination to store the loaded block of image data.
462 * @return Whether loading was successful.
464 static bool ReadHeightMap(DetailedFileType dft
, const char *filename
, uint
*x
, uint
*y
, std::vector
<uint8_t> *map
)
471 case DFT_HEIGHTMAP_PNG
:
472 return ReadHeightmapPNG(filename
, x
, y
, map
);
473 #endif /* WITH_PNG */
475 case DFT_HEIGHTMAP_BMP
:
476 return ReadHeightmapBMP(filename
, x
, y
, map
);
481 * Get the dimensions of a heightmap.
482 * @param dft Type of image file.
483 * @param filename to query
484 * @param x dimension x
485 * @param y dimension y
486 * @return Returns false if loading of the image failed.
488 bool GetHeightmapDimensions(DetailedFileType dft
, const char *filename
, uint
*x
, uint
*y
)
490 return ReadHeightMap(dft
, filename
, x
, y
, nullptr);
494 * Load a heightmap from file and change the map in its current dimensions
495 * to a landscape representing the heightmap.
496 * It converts pixels to height. The brighter, the higher.
497 * @param dft Type of image file.
498 * @param filename of the heightmap file to be imported
500 bool LoadHeightmap(DetailedFileType dft
, const char *filename
)
503 std::vector
<uint8_t> map
;
505 if (!ReadHeightMap(dft
, filename
, &x
, &y
, &map
)) {
509 GrayscaleToMapHeights(x
, y
, map
);
512 MarkWholeScreenDirty();
518 * Make an empty world where all tiles are of height 'tile_height'.
519 * @param tile_height of the desired new empty world
521 void FlatEmptyWorld(uint8_t tile_height
)
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
);
531 MarkWholeScreenDirty();