Hotfix: Conditional order comparator dropdown list was broken.
[openttd-joker.git] / src / heightmap.cpp
blob2ead9b85a73e2390d72927e51ec1fa6c1c64f453
1 /* $Id: heightmap.cpp 26057 2013-11-23 13:12:19Z rubidium $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file heightmap.cpp Creating of maps from heightmaps. */
12 #include "stdafx.h"
13 #include "heightmap.h"
14 #include "clear_map.h"
15 #include "void_map.h"
16 #include "error.h"
17 #include "saveload/saveload.h"
18 #include "bmp.h"
19 #include "gfx_func.h"
20 #include "fios.h"
21 #include "fileio_func.h"
23 #include "table/strings.h"
25 #include "safeguards.h"
27 /**
28 * Convert RGB colours to Grayscale using 29.9% Red, 58.7% Green, 11.4% Blue
29 * (average luminosity formula, NTSC Colour Space)
31 static inline byte RGBToGrayscale(byte red, byte green, byte blue)
33 /* To avoid doubles and stuff, multiply it with a total of 65536 (16bits), then
34 * divide by it to normalize the value to a byte again. */
35 return ((red * 19595) + (green * 38470) + (blue * 7471)) / 65536;
39 #ifdef WITH_PNG
41 #include <png.h>
43 /**
44 * The PNG Heightmap loader.
46 static void ReadHeightmapPNGImageData(byte *map, png_structp png_ptr, png_infop info_ptr)
48 uint x, y;
49 byte gray_palette[256];
50 png_bytep *row_pointers = NULL;
51 bool has_palette = png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE;
52 uint channels = png_get_channels(png_ptr, info_ptr);
54 /* Get palette and convert it to grayscale */
55 if (has_palette) {
56 int i;
57 int palette_size;
58 png_color *palette;
59 bool all_gray = true;
61 png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size);
62 for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) {
63 all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue;
64 gray_palette[i] = RGBToGrayscale(palette[i].red, palette[i].green, palette[i].blue);
67 /**
68 * For a non-gray palette of size 16 we assume that
69 * the order of the palette determines the height;
70 * the first entry is the sea (level 0), the second one
71 * level 1, etc.
73 if (palette_size == 16 && !all_gray) {
74 for (i = 0; i < palette_size; i++) {
75 gray_palette[i] = 256 * i / palette_size;
80 row_pointers = png_get_rows(png_ptr, info_ptr);
82 /* Read the raw image data and convert in 8-bit grayscale */
83 for (x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
84 for (y = 0; y < png_get_image_height(png_ptr, info_ptr); y++) {
85 byte *pixel = &map[y * png_get_image_width(png_ptr, info_ptr) + x];
86 uint x_offset = x * channels;
88 if (has_palette) {
89 *pixel = gray_palette[row_pointers[y][x_offset]];
90 } else if (channels == 3) {
91 *pixel = RGBToGrayscale(row_pointers[y][x_offset + 0],
92 row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]);
93 } else {
94 *pixel = row_pointers[y][x_offset];
101 * Reads the heightmap and/or size of the heightmap from a PNG file.
102 * If map == NULL only the size of the PNG is read, otherwise a map
103 * with grayscale pixels is allocated and assigned to *map.
105 static bool ReadHeightmapPNG(const char *filename, uint *x, uint *y, byte **map)
107 FILE *fp;
108 png_structp png_ptr = NULL;
109 png_infop info_ptr = NULL;
111 fp = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
112 if (fp == NULL) {
113 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR);
114 return false;
117 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
118 if (png_ptr == NULL) {
119 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_MISC, WL_ERROR);
120 fclose(fp);
121 return false;
124 info_ptr = png_create_info_struct(png_ptr);
125 if (info_ptr == NULL || setjmp(png_jmpbuf(png_ptr))) {
126 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_MISC, WL_ERROR);
127 fclose(fp);
128 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
129 return false;
132 png_init_io(png_ptr, fp);
134 /* Allocate memory and read image, without alpha or 16-bit samples
135 * (result is either 8-bit indexed/grayscale or 24-bit RGB) */
136 png_set_packing(png_ptr);
137 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_STRIP_16, NULL);
139 /* Maps of wrong colour-depth are not used.
140 * (this should have been taken care of by stripping alpha and 16-bit samples on load) */
141 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)) {
142 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_IMAGE_TYPE, WL_ERROR);
143 fclose(fp);
144 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
145 return false;
148 uint width = png_get_image_width(png_ptr, info_ptr);
149 uint height = png_get_image_height(png_ptr, info_ptr);
151 /* Check if image dimensions don't overflow a size_t to avoid memory corruption. */
152 if ((uint64)width * height >= (size_t)-1) {
153 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
154 fclose(fp);
155 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
156 return false;
159 if (map != NULL) {
160 *map = MallocT<byte>(width * height);
161 ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
164 *x = width;
165 *y = height;
167 fclose(fp);
168 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
169 return true;
172 #endif /* WITH_PNG */
176 * The BMP Heightmap loader.
178 static void ReadHeightmapBMPImageData(byte *map, BmpInfo *info, BmpData *data)
180 uint x, y;
181 byte gray_palette[256];
183 if (data->palette != NULL) {
184 uint i;
185 bool all_gray = true;
187 if (info->palette_size != 2) {
188 for (i = 0; i < info->palette_size && (info->palette_size != 16 || all_gray); i++) {
189 all_gray &= data->palette[i].r == data->palette[i].g && data->palette[i].r == data->palette[i].b;
190 gray_palette[i] = RGBToGrayscale(data->palette[i].r, data->palette[i].g, data->palette[i].b);
194 * For a non-gray palette of size 16 we assume that
195 * the order of the palette determines the height;
196 * the first entry is the sea (level 0), the second one
197 * level 1, etc.
199 if (info->palette_size == 16 && !all_gray) {
200 for (i = 0; i < info->palette_size; i++) {
201 gray_palette[i] = 256 * i / info->palette_size;
204 } else {
206 * For a palette of size 2 we assume that the order of the palette determines the height;
207 * the first entry is the sea (level 0), the second one is the land (level 1)
209 gray_palette[0] = 0;
210 gray_palette[1] = 16;
214 /* Read the raw image data and convert in 8-bit grayscale */
215 for (y = 0; y < info->height; y++) {
216 byte *pixel = &map[y * info->width];
217 byte *bitmap = &data->bitmap[y * info->width * (info->bpp == 24 ? 3 : 1)];
219 for (x = 0; x < info->width; x++) {
220 if (info->bpp != 24) {
221 *pixel++ = gray_palette[*bitmap++];
222 } else {
223 *pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2));
224 bitmap += 3;
231 * Reads the heightmap and/or size of the heightmap from a BMP file.
232 * If map == NULL only the size of the BMP is read, otherwise a map
233 * with grayscale pixels is allocated and assigned to *map.
235 static bool ReadHeightmapBMP(const char *filename, uint *x, uint *y, byte **map)
237 FILE *f;
238 BmpInfo info;
239 BmpData data;
240 BmpBuffer buffer;
242 /* Init BmpData */
243 memset(&data, 0, sizeof(data));
245 f = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
246 if (f == NULL) {
247 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR);
248 return false;
251 BmpInitializeBuffer(&buffer, f);
253 if (!BmpReadHeader(&buffer, &info, &data)) {
254 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
255 fclose(f);
256 BmpDestroyData(&data);
257 return false;
260 /* Check if image dimensions don't overflow a size_t to avoid memory corruption. */
261 if ((uint64)info.width * info.height >= (size_t)-1 / (info.bpp == 24 ? 3 : 1)) {
262 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
263 fclose(f);
264 BmpDestroyData(&data);
265 return false;
268 if (map != NULL) {
269 if (!BmpReadBitmap(&buffer, &info, &data)) {
270 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
271 fclose(f);
272 BmpDestroyData(&data);
273 return false;
276 *map = MallocT<byte>(info.width * info.height);
277 ReadHeightmapBMPImageData(*map, &info, &data);
280 BmpDestroyData(&data);
282 *x = info.width;
283 *y = info.height;
285 fclose(f);
286 return true;
290 * Converts a given grayscale map to something that fits in OTTD map system
291 * and create a map of that data.
292 * @param img_width the with of the image in pixels/tiles
293 * @param img_height the height of the image in pixels/tiles
294 * @param map the input map
296 static void GrayscaleToMapHeights(uint img_width, uint img_height, byte *map)
298 /* Defines the detail of the aspect ratio (to avoid doubles) */
299 const uint num_div = 16384;
301 uint width, height;
302 uint row, col;
303 uint row_pad = 0, col_pad = 0;
304 uint img_scale;
305 uint img_row, img_col;
306 TileIndex tile;
308 /* Get map size and calculate scale and padding values */
309 switch (_settings_game.game_creation.heightmap_rotation) {
310 default: NOT_REACHED();
311 case HM_COUNTER_CLOCKWISE:
312 width = MapSizeX();
313 height = MapSizeY();
314 break;
315 case HM_CLOCKWISE:
316 width = MapSizeY();
317 height = MapSizeX();
318 break;
321 if ((img_width * num_div) / img_height > ((width * num_div) / height)) {
322 /* Image is wider than map - center vertically */
323 img_scale = (width * num_div) / img_width;
324 row_pad = (1 + height - ((img_height * img_scale) / num_div)) / 2;
325 } else {
326 /* Image is taller than map - center horizontally */
327 img_scale = (height * num_div) / img_height;
328 col_pad = (1 + width - ((img_width * img_scale) / num_div)) / 2;
331 if (_settings_game.construction.freeform_edges) {
332 for (uint x = 0; x < MapSizeX(); x++) MakeVoid(TileXY(x, 0));
333 for (uint y = 0; y < MapSizeY(); y++) MakeVoid(TileXY(0, y));
336 /* Form the landscape */
337 for (row = 0; row < height; row++) {
338 for (col = 0; col < width; col++) {
339 switch (_settings_game.game_creation.heightmap_rotation) {
340 default: NOT_REACHED();
341 case HM_COUNTER_CLOCKWISE: tile = TileXY(col, row); break;
342 case HM_CLOCKWISE: tile = TileXY(row, col); break;
345 /* Check if current tile is within the 1-pixel map edge or padding regions */
346 if ((!_settings_game.construction.freeform_edges && DistanceFromEdge(tile) <= 1) ||
347 (row < row_pad) || (row >= (height - row_pad - (_settings_game.construction.freeform_edges ? 0 : 1))) ||
348 (col < col_pad) || (col >= (width - col_pad - (_settings_game.construction.freeform_edges ? 0 : 1)))) {
349 SetTileHeight(tile, 0);
350 } else {
351 /* Use nearest neighbour resizing to scale map data.
352 * We rotate the map 45 degrees (counter)clockwise */
353 img_row = (((row - row_pad) * num_div) / img_scale);
354 switch (_settings_game.game_creation.heightmap_rotation) {
355 default: NOT_REACHED();
356 case HM_COUNTER_CLOCKWISE:
357 img_col = (((width - 1 - col - col_pad) * num_div) / img_scale);
358 break;
359 case HM_CLOCKWISE:
360 img_col = (((col - col_pad) * num_div) / img_scale);
361 break;
364 assert(img_row < img_height);
365 assert(img_col < img_width);
367 uint heightmap_height = map[img_row * img_width + img_col];
369 if (heightmap_height > 0) {
370 /* 0 is sea level.
371 * Other grey scales are scaled evenly to the available height levels > 0.
372 * (The coastline is independent from the number of height levels) */
373 heightmap_height = 1 + (heightmap_height - 1) * _settings_game.construction.max_heightlevel / 255;
376 SetTileHeight(tile, heightmap_height);
378 /* Only clear the tiles within the map area. */
379 if (IsInnerTile(tile)) {
380 MakeClear(tile, CLEAR_GRASS, 3);
387 * This function takes care of the fact that land in OpenTTD can never differ
388 * more than 1 in height
390 void FixSlopes()
392 uint width, height;
393 int row, col;
394 byte current_tile;
396 /* Adjust height difference to maximum one horizontal/vertical change. */
397 width = MapSizeX();
398 height = MapSizeY();
400 /* Top and left edge */
401 for (row = 0; (uint)row < height; row++) {
402 for (col = 0; (uint)col < width; col++) {
403 current_tile = MAX_TILE_HEIGHT;
404 if (col != 0) {
405 /* Find lowest tile; either the top or left one */
406 current_tile = TileHeight(TileXY(col - 1, row)); // top edge
408 if (row != 0) {
409 if (TileHeight(TileXY(col, row - 1)) < current_tile) {
410 current_tile = TileHeight(TileXY(col, row - 1)); // left edge
414 /* Does the height differ more than one? */
415 if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
416 /* Then change the height to be no more than one */
417 SetTileHeight(TileXY(col, row), current_tile + 1);
422 /* Bottom and right edge */
423 for (row = height - 1; row >= 0; row--) {
424 for (col = width - 1; col >= 0; col--) {
425 current_tile = MAX_TILE_HEIGHT;
426 if ((uint)col != width - 1) {
427 /* Find lowest tile; either the bottom and right one */
428 current_tile = TileHeight(TileXY(col + 1, row)); // bottom edge
431 if ((uint)row != height - 1) {
432 if (TileHeight(TileXY(col, row + 1)) < current_tile) {
433 current_tile = TileHeight(TileXY(col, row + 1)); // right edge
437 /* Does the height differ more than one? */
438 if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
439 /* Then change the height to be no more than one */
440 SetTileHeight(TileXY(col, row), current_tile + 1);
447 * Reads the heightmap with the correct file reader.
448 * @param dft Type of image file.
449 * @param filename Name of the file to load.
450 * @param [out] x Length of the image.
451 * @param [out] y Height of the image.
452 * @param [inout] map If not \c NULL, destination to store the loaded block of image data.
453 * @return Whether loading was successful.
455 static bool ReadHeightMap(DetailedFileType dft, const char *filename, uint *x, uint *y, byte **map)
457 switch (dft) {
458 default:
459 NOT_REACHED();
461 #ifdef WITH_PNG
462 case DFT_HEIGHTMAP_PNG:
463 return ReadHeightmapPNG(filename, x, y, map);
464 #endif /* WITH_PNG */
466 case DFT_HEIGHTMAP_BMP:
467 return ReadHeightmapBMP(filename, x, y, map);
472 * Get the dimensions of a heightmap.
473 * @param dft Type of image file.
474 * @param filename to query
475 * @param x dimension x
476 * @param y dimension y
477 * @return Returns false if loading of the image failed.
479 bool GetHeightmapDimensions(DetailedFileType dft, const char *filename, uint *x, uint *y)
481 return ReadHeightMap(dft, filename, x, y, NULL);
485 * Load a heightmap from file and change the map in his current dimensions
486 * to a landscape representing the heightmap.
487 * It converts pixels to height. The brighter, the higher.
488 * @param dft Type of image file.
489 * @param filename of the heightmap file to be imported
491 void LoadHeightmap(DetailedFileType dft, const char *filename)
493 uint x, y;
494 byte *map = NULL;
496 if (!ReadHeightMap(dft, filename, &x, &y, &map)) {
497 free(map);
498 return;
501 GrayscaleToMapHeights(x, y, map);
502 free(map);
504 FixSlopes();
505 MarkWholeScreenDirty();
509 * Make an empty world where all tiles are of height 'tile_height'.
510 * @param tile_height of the desired new empty world
512 void FlatEmptyWorld(byte tile_height)
514 int edge_distance = _settings_game.construction.freeform_edges ? 0 : 2;
515 for (uint row = edge_distance; row < MapSizeY() - edge_distance; row++) {
516 for (uint col = edge_distance; col < MapSizeX() - edge_distance; col++) {
517 SetTileHeight(TileXY(col, row), tile_height);
521 FixSlopes();
522 MarkWholeScreenDirty();