Fix: [OSX] Don't show a crash/assertion message box for a GUI-less video driver.
[openttd-github.git] / src / heightmap.cpp
blobfab93c9802d5d64b4d1155f726c6012a793c8cbf
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 */
8 /** @file heightmap.cpp Creating of maps from heightmaps. */
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"
21 #include "table/strings.h"
23 #include "safeguards.h"
25 /**
26 * Convert RGB colours to Grayscale using 29.9% Red, 58.7% Green, 11.4% Blue
27 * (average luminosity formula, NTSC Colour Space)
29 static inline byte RGBToGrayscale(byte red, byte green, byte blue)
31 /* To avoid doubles and stuff, multiply it with a total of 65536 (16bits), then
32 * divide by it to normalize the value to a byte again. */
33 return ((red * 19595) + (green * 38470) + (blue * 7471)) / 65536;
37 #ifdef WITH_PNG
39 #include <png.h>
41 /**
42 * The PNG Heightmap loader.
44 static void ReadHeightmapPNGImageData(byte *map, png_structp png_ptr, png_infop info_ptr)
46 uint x, y;
47 byte gray_palette[256];
48 png_bytep *row_pointers = nullptr;
49 bool has_palette = png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE;
50 uint channels = png_get_channels(png_ptr, info_ptr);
52 /* Get palette and convert it to grayscale */
53 if (has_palette) {
54 int i;
55 int palette_size;
56 png_color *palette;
57 bool all_gray = true;
59 png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size);
60 for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) {
61 all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue;
62 gray_palette[i] = RGBToGrayscale(palette[i].red, palette[i].green, palette[i].blue);
65 /**
66 * For a non-gray palette of size 16 we assume that
67 * the order of the palette determines the height;
68 * the first entry is the sea (level 0), the second one
69 * level 1, etc.
71 if (palette_size == 16 && !all_gray) {
72 for (i = 0; i < palette_size; i++) {
73 gray_palette[i] = 256 * i / palette_size;
78 row_pointers = png_get_rows(png_ptr, info_ptr);
80 /* Read the raw image data and convert in 8-bit grayscale */
81 for (x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
82 for (y = 0; y < png_get_image_height(png_ptr, info_ptr); y++) {
83 byte *pixel = &map[y * png_get_image_width(png_ptr, info_ptr) + x];
84 uint x_offset = x * channels;
86 if (has_palette) {
87 *pixel = gray_palette[row_pointers[y][x_offset]];
88 } else if (channels == 3) {
89 *pixel = RGBToGrayscale(row_pointers[y][x_offset + 0],
90 row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]);
91 } else {
92 *pixel = row_pointers[y][x_offset];
98 /**
99 * Reads the heightmap and/or size of the heightmap from a PNG file.
100 * If map == nullptr only the size of the PNG is read, otherwise a map
101 * with grayscale pixels is allocated and assigned to *map.
103 static bool ReadHeightmapPNG(const char *filename, uint *x, uint *y, byte **map)
105 FILE *fp;
106 png_structp png_ptr = nullptr;
107 png_infop info_ptr = nullptr;
109 fp = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
110 if (fp == nullptr) {
111 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR);
112 return false;
115 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
116 if (png_ptr == nullptr) {
117 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_MISC, WL_ERROR);
118 fclose(fp);
119 return false;
122 info_ptr = png_create_info_struct(png_ptr);
123 if (info_ptr == nullptr || setjmp(png_jmpbuf(png_ptr))) {
124 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_MISC, WL_ERROR);
125 fclose(fp);
126 png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
127 return false;
130 png_init_io(png_ptr, fp);
132 /* Allocate memory and read image, without alpha or 16-bit samples
133 * (result is either 8-bit indexed/grayscale or 24-bit RGB) */
134 png_set_packing(png_ptr);
135 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_STRIP_16, nullptr);
137 /* Maps of wrong colour-depth are not used.
138 * (this should have been taken care of by stripping alpha and 16-bit samples on load) */
139 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)) {
140 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_IMAGE_TYPE, WL_ERROR);
141 fclose(fp);
142 png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
143 return false;
146 uint width = png_get_image_width(png_ptr, info_ptr);
147 uint height = png_get_image_height(png_ptr, info_ptr);
149 /* Check if image dimensions don't overflow a size_t to avoid memory corruption. */
150 if ((uint64)width * height >= (size_t)-1) {
151 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
152 fclose(fp);
153 png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
154 return false;
157 if (map != nullptr) {
158 *map = MallocT<byte>(width * height);
159 ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
162 *x = width;
163 *y = height;
165 fclose(fp);
166 png_destroy_read_struct(&png_ptr, &info_ptr, nullptr);
167 return true;
170 #endif /* WITH_PNG */
174 * The BMP Heightmap loader.
176 static void ReadHeightmapBMPImageData(byte *map, BmpInfo *info, BmpData *data)
178 uint x, y;
179 byte gray_palette[256];
181 if (data->palette != nullptr) {
182 uint i;
183 bool all_gray = true;
185 if (info->palette_size != 2) {
186 for (i = 0; i < info->palette_size && (info->palette_size != 16 || all_gray); i++) {
187 all_gray &= data->palette[i].r == data->palette[i].g && data->palette[i].r == data->palette[i].b;
188 gray_palette[i] = RGBToGrayscale(data->palette[i].r, data->palette[i].g, data->palette[i].b);
192 * For a non-gray palette of size 16 we assume that
193 * the order of the palette determines the height;
194 * the first entry is the sea (level 0), the second one
195 * level 1, etc.
197 if (info->palette_size == 16 && !all_gray) {
198 for (i = 0; i < info->palette_size; i++) {
199 gray_palette[i] = 256 * i / info->palette_size;
202 } else {
204 * For a palette of size 2 we assume that the order of the palette determines the height;
205 * the first entry is the sea (level 0), the second one is the land (level 1)
207 gray_palette[0] = 0;
208 gray_palette[1] = 16;
212 /* Read the raw image data and convert in 8-bit grayscale */
213 for (y = 0; y < info->height; y++) {
214 byte *pixel = &map[y * info->width];
215 byte *bitmap = &data->bitmap[y * info->width * (info->bpp == 24 ? 3 : 1)];
217 for (x = 0; x < info->width; x++) {
218 if (info->bpp != 24) {
219 *pixel++ = gray_palette[*bitmap++];
220 } else {
221 *pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2));
222 bitmap += 3;
229 * Reads the heightmap and/or size of the heightmap from a BMP file.
230 * If map == nullptr only the size of the BMP is read, otherwise a map
231 * with grayscale pixels is allocated and assigned to *map.
233 static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, byte **map)
235 FILE *f;
236 BmpInfo info;
237 BmpData data;
238 BmpBuffer buffer;
240 /* Init BmpData */
241 memset(&data, 0, sizeof(data));
243 f = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
244 if (f == nullptr) {
245 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR);
246 return false;
249 BmpInitializeBuffer(&buffer, f);
251 if (!BmpReadHeader(&buffer, &info, &data)) {
252 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
253 fclose(f);
254 BmpDestroyData(&data);
255 return false;
258 /* Check if image dimensions don't overflow a size_t to avoid memory corruption. */
259 if ((uint64)info.width * info.height >= (size_t)-1 / (info.bpp == 24 ? 3 : 1)) {
260 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
261 fclose(f);
262 BmpDestroyData(&data);
263 return false;
266 if (map != nullptr) {
267 if (!BmpReadBitmap(&buffer, &info, &data)) {
268 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
269 fclose(f);
270 BmpDestroyData(&data);
271 return false;
274 *map = MallocT<byte>(info.width * info.height);
275 ReadHeightmapBMPImageData(*map, &info, &data);
278 BmpDestroyData(&data);
280 *x = info.width;
281 *y = info.height;
283 fclose(f);
284 return true;
288 * Converts a given grayscale map to something that fits in OTTD map system
289 * and create a map of that data.
290 * @param img_width the with of the image in pixels/tiles
291 * @param img_height the height of the image in pixels/tiles
292 * @param map the input map
294 static void GrayscaleToMapHeights(uint img_width, uint img_height, byte *map)
296 /* Defines the detail of the aspect ratio (to avoid doubles) */
297 const uint num_div = 16384;
299 uint width, height;
300 uint row, col;
301 uint row_pad = 0, col_pad = 0;
302 uint img_scale;
303 uint img_row, img_col;
304 TileIndex tile;
306 /* Get map size and calculate scale and padding values */
307 switch (_settings_game.game_creation.heightmap_rotation) {
308 default: NOT_REACHED();
309 case HM_COUNTER_CLOCKWISE:
310 width = MapSizeX();
311 height = MapSizeY();
312 break;
313 case HM_CLOCKWISE:
314 width = MapSizeY();
315 height = MapSizeX();
316 break;
319 if ((img_width * num_div) / img_height > ((width * num_div) / height)) {
320 /* Image is wider than map - center vertically */
321 img_scale = (width * num_div) / img_width;
322 row_pad = (1 + height - ((img_height * img_scale) / num_div)) / 2;
323 } else {
324 /* Image is taller than map - center horizontally */
325 img_scale = (height * num_div) / img_height;
326 col_pad = (1 + width - ((img_width * img_scale) / num_div)) / 2;
329 if (_settings_game.construction.freeform_edges) {
330 for (uint x = 0; x < MapSizeX(); x++) MakeVoid(TileXY(x, 0));
331 for (uint y = 0; y < MapSizeY(); y++) MakeVoid(TileXY(0, y));
334 /* Form the landscape */
335 for (row = 0; row < height; row++) {
336 for (col = 0; col < width; col++) {
337 switch (_settings_game.game_creation.heightmap_rotation) {
338 default: NOT_REACHED();
339 case HM_COUNTER_CLOCKWISE: tile = TileXY(col, row); break;
340 case HM_CLOCKWISE: tile = TileXY(row, col); break;
343 /* Check if current tile is within the 1-pixel map edge or padding regions */
344 if ((!_settings_game.construction.freeform_edges && DistanceFromEdge(tile) <= 1) ||
345 (row < row_pad) || (row >= (height - row_pad - (_settings_game.construction.freeform_edges ? 0 : 1))) ||
346 (col < col_pad) || (col >= (width - col_pad - (_settings_game.construction.freeform_edges ? 0 : 1)))) {
347 SetTileHeight(tile, 0);
348 } else {
349 /* Use nearest neighbour resizing to scale map data.
350 * We rotate the map 45 degrees (counter)clockwise */
351 img_row = (((row - row_pad) * num_div) / img_scale);
352 switch (_settings_game.game_creation.heightmap_rotation) {
353 default: NOT_REACHED();
354 case HM_COUNTER_CLOCKWISE:
355 img_col = (((width - 1 - col - col_pad) * num_div) / img_scale);
356 break;
357 case HM_CLOCKWISE:
358 img_col = (((col - col_pad) * num_div) / img_scale);
359 break;
362 assert(img_row < img_height);
363 assert(img_col < img_width);
365 uint heightmap_height = map[img_row * img_width + img_col];
367 if (heightmap_height > 0) {
368 /* 0 is sea level.
369 * Other grey scales are scaled evenly to the available height levels > 0.
370 * (The coastline is independent from the number of height levels) */
371 heightmap_height = 1 + (heightmap_height - 1) * _settings_game.construction.max_heightlevel / 255;
374 SetTileHeight(tile, heightmap_height);
376 /* Only clear the tiles within the map area. */
377 if (IsInnerTile(tile)) {
378 MakeClear(tile, CLEAR_GRASS, 3);
385 * This function takes care of the fact that land in OpenTTD can never differ
386 * more than 1 in height
388 void FixSlopes()
390 uint width, height;
391 int row, col;
392 byte current_tile;
394 /* Adjust height difference to maximum one horizontal/vertical change. */
395 width = MapSizeX();
396 height = MapSizeY();
398 /* Top and left edge */
399 for (row = 0; (uint)row < height; row++) {
400 for (col = 0; (uint)col < width; col++) {
401 current_tile = MAX_TILE_HEIGHT;
402 if (col != 0) {
403 /* Find lowest tile; either the top or left one */
404 current_tile = TileHeight(TileXY(col - 1, row)); // top edge
406 if (row != 0) {
407 if (TileHeight(TileXY(col, row - 1)) < current_tile) {
408 current_tile = TileHeight(TileXY(col, row - 1)); // left edge
412 /* Does the height differ more than one? */
413 if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
414 /* Then change the height to be no more than one */
415 SetTileHeight(TileXY(col, row), current_tile + 1);
420 /* Bottom and right edge */
421 for (row = height - 1; row >= 0; row--) {
422 for (col = width - 1; col >= 0; col--) {
423 current_tile = MAX_TILE_HEIGHT;
424 if ((uint)col != width - 1) {
425 /* Find lowest tile; either the bottom and right one */
426 current_tile = TileHeight(TileXY(col + 1, row)); // bottom edge
429 if ((uint)row != height - 1) {
430 if (TileHeight(TileXY(col, row + 1)) < current_tile) {
431 current_tile = TileHeight(TileXY(col, row + 1)); // right edge
435 /* Does the height differ more than one? */
436 if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
437 /* Then change the height to be no more than one */
438 SetTileHeight(TileXY(col, row), current_tile + 1);
445 * Reads the heightmap with the correct file reader.
446 * @param dft Type of image file.
447 * @param filename Name of the file to load.
448 * @param[out] x Length of the image.
449 * @param[out] y Height of the image.
450 * @param[in,out] map If not \c nullptr, destination to store the loaded block of image data.
451 * @return Whether loading was successful.
453 static bool ReadHeightMap(DetailedFileType dft, const char *filename, uint *x, uint *y, byte **map)
455 switch (dft) {
456 default:
457 NOT_REACHED();
459 #ifdef WITH_PNG
460 case DFT_HEIGHTMAP_PNG:
461 return ReadHeightmapPNG(filename, x, y, map);
462 #endif /* WITH_PNG */
464 case DFT_HEIGHTMAP_BMP:
465 return ReadHeightmapBMP(filename, x, y, map);
470 * Get the dimensions of a heightmap.
471 * @param dft Type of image file.
472 * @param filename to query
473 * @param x dimension x
474 * @param y dimension y
475 * @return Returns false if loading of the image failed.
477 bool GetHeightmapDimensions(DetailedFileType dft, const char *filename, uint *x, uint *y)
479 return ReadHeightMap(dft, filename, x, y, nullptr);
483 * Load a heightmap from file and change the map in his current dimensions
484 * to a landscape representing the heightmap.
485 * It converts pixels to height. The brighter, the higher.
486 * @param dft Type of image file.
487 * @param filename of the heightmap file to be imported
489 void LoadHeightmap(DetailedFileType dft, const char *filename)
491 uint x, y;
492 byte *map = nullptr;
494 if (!ReadHeightMap(dft, filename, &x, &y, &map)) {
495 free(map);
496 return;
499 GrayscaleToMapHeights(x, y, map);
500 free(map);
502 FixSlopes();
503 MarkWholeScreenDirty();
507 * Make an empty world where all tiles are of height 'tile_height'.
508 * @param tile_height of the desired new empty world
510 void FlatEmptyWorld(byte tile_height)
512 int edge_distance = _settings_game.construction.freeform_edges ? 0 : 2;
513 for (uint row = edge_distance; row < MapSizeY() - edge_distance; row++) {
514 for (uint col = edge_distance; col < MapSizeX() - edge_distance; col++) {
515 SetTileHeight(TileXY(col, row), tile_height);
519 FixSlopes();
520 MarkWholeScreenDirty();