Add a method in NewGRFInspectWindow to resolve FeatureIndex
[openttd/fttd.git] / src / heightmap.cpp
blobd80c61987681cbb8529171dd5860c98a0d35677b
1 /* $Id$ */
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 "map/ground.h"
14 #include "heightmap.h"
15 #include "error.h"
16 #include "saveload/saveload.h"
17 #include "bmp.h"
18 #include "gfx_func.h"
19 #include "fios.h"
20 #include "fileio_func.h"
22 #include "table/strings.h"
24 /**
25 * Convert RGB colours to Grayscale using 29.9% Red, 58.7% Green, 11.4% Blue
26 * (average luminosity formula, NTSC Colour Space)
28 static inline byte RGBToGrayscale(byte red, byte green, byte blue)
30 /* To avoid doubles and stuff, multiply it with a total of 65536 (16bits), then
31 * divide by it to normalize the value to a byte again. */
32 return ((red * 19595) + (green * 38470) + (blue * 7471)) / 65536;
36 #ifdef WITH_PNG
38 #include <png.h>
40 /**
41 * The PNG Heightmap loader.
43 static void ReadHeightmapPNGImageData(byte *map, png_structp png_ptr, png_infop info_ptr)
45 uint x, y;
46 byte gray_palette[256];
47 png_bytep *row_pointers = NULL;
49 /* Get palette and convert it to grayscale */
50 if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE) {
51 int i;
52 int palette_size;
53 png_color *palette;
54 bool all_gray = true;
56 png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size);
57 for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) {
58 all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue;
59 gray_palette[i] = RGBToGrayscale(palette[i].red, palette[i].green, palette[i].blue);
62 /**
63 * For a non-gray palette of size 16 we assume that
64 * the order of the palette determines the height;
65 * the first entry is the sea (level 0), the second one
66 * level 1, etc.
68 if (palette_size == 16 && !all_gray) {
69 for (i = 0; i < palette_size; i++) {
70 gray_palette[i] = 256 * i / palette_size;
75 row_pointers = png_get_rows(png_ptr, info_ptr);
77 /* Read the raw image data and convert in 8-bit grayscale */
78 for (x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
79 for (y = 0; y < png_get_image_height(png_ptr, info_ptr); y++) {
80 byte *pixel = &map[y * png_get_image_width(png_ptr, info_ptr) + x];
81 uint x_offset = x * png_get_channels(png_ptr, info_ptr);
83 if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE) {
84 *pixel = gray_palette[row_pointers[y][x_offset]];
85 } else if (png_get_channels(png_ptr, info_ptr) == 3) {
86 *pixel = RGBToGrayscale(row_pointers[y][x_offset + 0],
87 row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]);
88 } else {
89 *pixel = row_pointers[y][x_offset];
95 /**
96 * Reads the heightmap and/or size of the heightmap from a PNG file.
97 * If map == NULL only the size of the PNG is read, otherwise a map
98 * with grayscale pixels is allocated and assigned to *map.
100 static bool ReadHeightmapPNG(char *filename, uint *x, uint *y, byte **map)
102 FILE *fp;
103 png_structp png_ptr = NULL;
104 png_infop info_ptr = NULL;
106 fp = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
107 if (fp == NULL) {
108 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR);
109 return false;
112 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
113 if (png_ptr == NULL) {
114 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_MISC, WL_ERROR);
115 fclose(fp);
116 return false;
119 info_ptr = png_create_info_struct(png_ptr);
120 if (info_ptr == NULL || setjmp(png_jmpbuf(png_ptr))) {
121 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_MISC, WL_ERROR);
122 fclose(fp);
123 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
124 return false;
127 png_init_io(png_ptr, fp);
129 /* Allocate memory and read image, without alpha or 16-bit samples
130 * (result is either 8-bit indexed/grayscale or 24-bit RGB) */
131 png_set_packing(png_ptr);
132 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_STRIP_16, NULL);
134 /* Maps of wrong colour-depth are not used.
135 * (this should have been taken care of by stripping alpha and 16-bit samples on load) */
136 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)) {
137 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_IMAGE_TYPE, WL_ERROR);
138 fclose(fp);
139 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
140 return false;
143 uint width = png_get_image_width(png_ptr, info_ptr);
144 uint height = png_get_image_height(png_ptr, info_ptr);
146 /* Check if image dimensions don't overflow a size_t to avoid memory corruption. */
147 if ((uint64)width * height >= (size_t)-1) {
148 ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
149 fclose(fp);
150 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
151 return false;
154 if (map != NULL) {
155 *map = MallocT<byte>(width * height);
156 ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
159 *x = width;
160 *y = height;
162 fclose(fp);
163 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
164 return true;
167 #endif /* WITH_PNG */
171 * The BMP Heightmap loader.
173 static void ReadHeightmapBMPImageData(byte *map, BmpInfo *info, BmpData *data)
175 uint x, y;
176 byte gray_palette[256];
178 if (data->palette != NULL) {
179 uint i;
180 bool all_gray = true;
182 if (info->palette_size != 2) {
183 for (i = 0; i < info->palette_size && (info->palette_size != 16 || all_gray); i++) {
184 all_gray &= data->palette[i].r == data->palette[i].g && data->palette[i].r == data->palette[i].b;
185 gray_palette[i] = RGBToGrayscale(data->palette[i].r, data->palette[i].g, data->palette[i].b);
189 * For a non-gray palette of size 16 we assume that
190 * the order of the palette determines the height;
191 * the first entry is the sea (level 0), the second one
192 * level 1, etc.
194 if (info->palette_size == 16 && !all_gray) {
195 for (i = 0; i < info->palette_size; i++) {
196 gray_palette[i] = 256 * i / info->palette_size;
199 } else {
201 * For a palette of size 2 we assume that the order of the palette determines the height;
202 * the first entry is the sea (level 0), the second one is the land (level 1)
204 gray_palette[0] = 0;
205 gray_palette[1] = 16;
209 /* Read the raw image data and convert in 8-bit grayscale */
210 for (y = 0; y < info->height; y++) {
211 byte *pixel = &map[y * info->width];
212 byte *bitmap = &data->bitmap[y * info->width * (info->bpp == 24 ? 3 : 1)];
214 for (x = 0; x < info->width; x++) {
215 if (info->bpp != 24) {
216 *pixel++ = gray_palette[*bitmap++];
217 } else {
218 *pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2));
219 bitmap += 3;
226 * Reads the heightmap and/or size of the heightmap from a BMP file.
227 * If map == NULL only the size of the BMP is read, otherwise a map
228 * with grayscale pixels is allocated and assigned to *map.
230 static bool ReadHeightmapBMP(char *filename, uint *x, uint *y, byte **map)
232 FILE *f;
233 BmpInfo info;
234 BmpData data;
235 BmpBuffer buffer;
237 /* Init BmpData */
238 memset(&data, 0, sizeof(data));
240 f = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
241 if (f == NULL) {
242 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR);
243 return false;
246 BmpInitializeBuffer(&buffer, f);
248 if (!BmpReadHeader(&buffer, &info, &data)) {
249 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
250 fclose(f);
251 BmpDestroyData(&data);
252 return false;
255 /* Check if image dimensions don't overflow a size_t to avoid memory corruption. */
256 if ((uint64)info.width * info.height >= (size_t)-1 / (info.bpp == 24 ? 3 : 1)) {
257 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
258 fclose(f);
259 BmpDestroyData(&data);
260 return false;
263 if (map != NULL) {
264 if (!BmpReadBitmap(&buffer, &info, &data)) {
265 ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
266 fclose(f);
267 BmpDestroyData(&data);
268 return false;
271 *map = MallocT<byte>(info.width * info.height);
272 ReadHeightmapBMPImageData(*map, &info, &data);
275 BmpDestroyData(&data);
277 *x = info.width;
278 *y = info.height;
280 fclose(f);
281 return true;
285 * Converts a given grayscale map to something that fits in OTTD map system
286 * and create a map of that data.
287 * @param img_width the with of the image in pixels/tiles
288 * @param img_height the height of the image in pixels/tiles
289 * @param map the input map
291 static void GrayscaleToMapHeights(uint img_width, uint img_height, byte *map)
293 /* Defines the detail of the aspect ratio (to avoid doubles) */
294 const uint num_div = 16384;
296 uint width, height;
297 uint row, col;
298 uint row_pad = 0, col_pad = 0;
299 uint img_scale;
300 uint img_row, img_col;
301 TileIndex tile;
303 /* Get map size and calculate scale and padding values */
304 switch (_settings_game.game_creation.heightmap_rotation) {
305 default: NOT_REACHED();
306 case HM_COUNTER_CLOCKWISE:
307 width = MapSizeX();
308 height = MapSizeY();
309 break;
310 case HM_CLOCKWISE:
311 width = MapSizeY();
312 height = MapSizeX();
313 break;
316 if ((img_width * num_div) / img_height > ((width * num_div) / height)) {
317 /* Image is wider than map - center vertically */
318 img_scale = (width * num_div) / img_width;
319 row_pad = (1 + height - ((img_height * img_scale) / num_div)) / 2;
320 } else {
321 /* Image is taller than map - center horizontally */
322 img_scale = (height * num_div) / img_height;
323 col_pad = (1 + width - ((img_width * img_scale) / num_div)) / 2;
326 if (_settings_game.construction.freeform_edges) {
327 for (uint x = 0; x < MapSizeX(); x++) MakeVoid(TileXY(x, 0));
328 for (uint y = 0; y < MapSizeY(); y++) MakeVoid(TileXY(0, y));
331 /* Form the landscape */
332 for (row = 0; row < height; row++) {
333 for (col = 0; col < width; col++) {
334 switch (_settings_game.game_creation.heightmap_rotation) {
335 default: NOT_REACHED();
336 case HM_COUNTER_CLOCKWISE: tile = TileXY(col, row); break;
337 case HM_CLOCKWISE: tile = TileXY(row, col); break;
340 /* Check if current tile is within the 1-pixel map edge or padding regions */
341 if ((!_settings_game.construction.freeform_edges && DistanceFromEdge(tile) <= 1) ||
342 (row < row_pad) || (row >= (height - row_pad - (_settings_game.construction.freeform_edges ? 0 : 1))) ||
343 (col < col_pad) || (col >= (width - col_pad - (_settings_game.construction.freeform_edges ? 0 : 1)))) {
344 SetTileHeight(tile, 0);
345 } else {
346 /* Use nearest neighbour resizing to scale map data.
347 * We rotate the map 45 degrees (counter)clockwise */
348 img_row = (((row - row_pad) * num_div) / img_scale);
349 switch (_settings_game.game_creation.heightmap_rotation) {
350 default: NOT_REACHED();
351 case HM_COUNTER_CLOCKWISE:
352 img_col = (((width - 1 - col - col_pad) * num_div) / img_scale);
353 break;
354 case HM_CLOCKWISE:
355 img_col = (((col - col_pad) * num_div) / img_scale);
356 break;
359 assert(img_row < img_height);
360 assert(img_col < img_width);
362 /* Colour scales from 0 to 255, OpenTTD height scales from 0 to 15 */
363 SetTileHeight(tile, map[img_row * img_width + img_col] / 16);
365 /* Only clear the tiles within the map area. */
366 if (IsInnerTile(tile)) {
367 MakeClear(tile, GROUND_GRASS, 3);
374 * This function takes care of the fact that land in OpenTTD can never differ
375 * more than 1 in height
377 void FixSlopes()
379 uint width, height;
380 int row, col;
381 byte current_tile;
383 /* Adjust height difference to maximum one horizontal/vertical change. */
384 width = MapSizeX();
385 height = MapSizeY();
387 /* Top and left edge */
388 for (row = 0; (uint)row < height; row++) {
389 for (col = 0; (uint)col < width; col++) {
390 current_tile = MAX_TILE_HEIGHT;
391 if (col != 0) {
392 /* Find lowest tile; either the top or left one */
393 current_tile = TileHeight(TileXY(col - 1, row)); // top edge
395 if (row != 0) {
396 if (TileHeight(TileXY(col, row - 1)) < current_tile) {
397 current_tile = TileHeight(TileXY(col, row - 1)); // left edge
401 /* Does the height differ more than one? */
402 if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
403 /* Then change the height to be no more than one */
404 SetTileHeight(TileXY(col, row), current_tile + 1);
409 /* Bottom and right edge */
410 for (row = height - 1; row >= 0; row--) {
411 for (col = width - 1; col >= 0; col--) {
412 current_tile = MAX_TILE_HEIGHT;
413 if ((uint)col != width - 1) {
414 /* Find lowest tile; either the bottom and right one */
415 current_tile = TileHeight(TileXY(col + 1, row)); // bottom edge
418 if ((uint)row != height - 1) {
419 if (TileHeight(TileXY(col, row + 1)) < current_tile) {
420 current_tile = TileHeight(TileXY(col, row + 1)); // right edge
424 /* Does the height differ more than one? */
425 if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
426 /* Then change the height to be no more than one */
427 SetTileHeight(TileXY(col, row), current_tile + 1);
434 * Reads the heightmap with the correct file reader
436 static bool ReadHeightMap(char *filename, uint *x, uint *y, byte **map)
438 switch (_file_to_saveload.mode) {
439 default: NOT_REACHED();
440 #ifdef WITH_PNG
441 case SL_PNG:
442 return ReadHeightmapPNG(filename, x, y, map);
443 #endif /* WITH_PNG */
444 case SL_BMP:
445 return ReadHeightmapBMP(filename, x, y, map);
450 * Get the dimensions of a heightmap.
451 * @param filename to query
452 * @param x dimension x
453 * @param y dimension y
454 * @return Returns false if loading of the image failed.
456 bool GetHeightmapDimensions(char *filename, uint *x, uint *y)
458 return ReadHeightMap(filename, x, y, NULL);
462 * Load a heightmap from file and change the map in his current dimensions
463 * to a landscape representing the heightmap.
464 * It converts pixels to height. The brighter, the higher.
465 * @param filename of the heightmap file to be imported
467 void LoadHeightmap(char *filename)
469 uint x, y;
470 byte *map = NULL;
472 if (!ReadHeightMap(filename, &x, &y, &map)) {
473 free(map);
474 return;
477 GrayscaleToMapHeights(x, y, map);
478 free(map);
480 FixSlopes();
481 MarkWholeScreenDirty();
485 * Make an empty world where all tiles are of height 'tile_height'.
486 * @param tile_height of the desired new empty world
488 void FlatEmptyWorld(byte tile_height)
490 int edge_distance = _settings_game.construction.freeform_edges ? 0 : 2;
491 for (uint row = edge_distance; row < MapSizeY() - edge_distance; row++) {
492 for (uint col = edge_distance; col < MapSizeX() - edge_distance; col++) {
493 SetTileHeight(TileXY(col, row), tile_height);
497 FixSlopes();
498 MarkWholeScreenDirty();