# heightmap.cpp

Go to the documentation of this file.

    00001 /* $Id: heightmap.cpp 18756 2010-01-08 03:17:12Z glx $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "heightmap.h"
    00014 #include "clear_map.h"
    00015 #include "void_map.h"
    00016 #include "gui.h"
    00017 #include "saveload/saveload.h"
    00018 #include "bmp.h"
    00019 #include "gfx_func.h"
    00020 #include "fios.h"
    00021 #include "fileio_func.h"
    00022
    00023 #include "table/strings.h"
    00024
    00030 static inline byte RGBToGrayscale(byte red, byte green, byte blue)
    00031 {
    00032   /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
    00033    *  divide by it to normalize the value to a byte again. */
    00034   return ((red * 19595) + (green * 38470) + (blue * 7471)) / 65536;
    00035 }
    00036
    00037
    00038 #ifdef WITH_PNG
    00039
    00040 #include <png.h>
    00041
    00045 static void ReadHeightmapPNGImageData(byte *map, png_structp png_ptr, png_infop info_ptr)
    00046 {
    00047   uint x, y;
    00048   byte gray_palette[256];
    00049   png_bytep *row_pointers = NULL;
    00050
    00051   /* Get palette and convert it to grayscale */
    00052   if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE) {
    00053     int i;
    00054     int palette_size;
    00055     png_color *palette;
    00056     bool all_gray = true;
    00057
    00058     png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size);
    00059     for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) {
    00060       all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue;
    00061       gray_palette[i] = RGBToGrayscale(palette[i].red, palette[i].green, palette[i].blue);
    00062     }
    00063
    00070     if (palette_size == 16 && !all_gray) {
    00071       for (i = 0; i < palette_size; i++) {
    00072         gray_palette[i] = 256 * i / palette_size;
    00073       }
    00074     }
    00075   }
    00076
    00077   row_pointers = png_get_rows(png_ptr, info_ptr);
    00078
    00079   /* Read the raw image data and convert in 8-bit grayscale */
    00080   for (x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
    00081     for (y = 0; y < png_get_image_height(png_ptr, info_ptr); y++) {
    00082       byte *pixel = &map[y * png_get_image_width(png_ptr, info_ptr) + x];
    00083       uint x_offset = x * png_get_channels(png_ptr, info_ptr);
    00084
    00085       if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE) {
    00086         *pixel = gray_palette[row_pointers[y][x_offset]];
    00087       } else if (png_get_channels(png_ptr, info_ptr) == 3) {
    00088         *pixel = RGBToGrayscale(row_pointers[y][x_offset + 0],
    00089             row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]);
    00090       } else {
    00091         *pixel = row_pointers[y][x_offset];
    00092       }
    00093     }
    00094   }
    00095 }
    00096
    00102 static bool ReadHeightmapPNG(char *filename, uint *x, uint *y, byte **map)
    00103 {
    00104   FILE *fp;
    00105   png_structp png_ptr = NULL;
    00106   png_infop info_ptr  = NULL;
    00107
    00108   fp = FioFOpenFile(filename, "rb");
    00109   if (fp == NULL) {
    00110     ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, 0, 0);
    00111     return false;
    00112   }
    00113
    00114   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    00115   if (png_ptr == NULL) {
    00116     ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_MISC, 0, 0);
    00117     fclose(fp);
    00118     return false;
    00119   }
    00120
    00121   info_ptr = png_create_info_struct(png_ptr);
    00122   if (info_ptr == NULL || setjmp(png_jmpbuf(png_ptr))) {
    00123     ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_MISC, 0, 0);
    00124     fclose(fp);
    00125     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    00126     return false;
    00127   }
    00128
    00129   png_init_io(png_ptr, fp);
    00130
    00131   /* Allocate memory and read image, without alpha or 16-bit samples
    00132    * (result is either 8-bit indexed/grayscale or 24-bit RGB) */
    00133   png_set_packing(png_ptr);
    00134   png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_STRIP_16, NULL);
    00135
    00136   /* Maps of wrong colour-depth are not used.
    00137    * (this should have been taken care of by stripping alpha and 16-bit samples on load) */
    00138   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)) {
    00139     ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_PNGMAP_IMAGE_TYPE, 0, 0);
    00140     fclose(fp);
    00141     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    00142     return false;
    00143   }
    00144
    00145   if (map != NULL) {
    00146     *map = MallocT<byte>(png_get_image_width(png_ptr, info_ptr) * png_get_image_height(png_ptr, info_ptr));
    00147     ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
    00148   }
    00149
    00150   *x = png_get_image_width(png_ptr, info_ptr);
    00151   *y = png_get_image_height(png_ptr, info_ptr);
    00152
    00153   fclose(fp);
    00154   png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    00155   return true;
    00156 }
    00157
    00158 #endif /* WITH_PNG */
    00159
    00160
    00164 static void ReadHeightmapBMPImageData(byte *map, BmpInfo *info, BmpData *data)
    00165 {
    00166   uint x, y;
    00167   byte gray_palette[256];
    00168
    00169   if (data->palette != NULL) {
    00170     uint i;
    00171     bool all_gray = true;
    00172
    00173     if (info->palette_size != 2) {
    00174       for (i = 0; i < info->palette_size && (info->palette_size != 16 || all_gray); i++) {
    00175         all_gray &= data->palette[i].r == data->palette[i].g && data->palette[i].r == data->palette[i].b;
    00176         gray_palette[i] = RGBToGrayscale(data->palette[i].r, data->palette[i].g, data->palette[i].b);
    00177       }
    00178
    00185       if (info->palette_size == 16 && !all_gray) {
    00186         for (i = 0; i < info->palette_size; i++) {
    00187           gray_palette[i] = 256 * i / info->palette_size;
    00188         }
    00189       }
    00190     } else {
    00195       gray_palette[0] = 0;
    00196       gray_palette[1] = 16;
    00197     }
    00198   }
    00199
    00200   /* Read the raw image data and convert in 8-bit grayscale */
    00201   for (y = 0; y < info->height; y++) {
    00202     byte *pixel = &map[y * info->width];
    00203     byte *bitmap = &data->bitmap[y * info->width * (info->bpp == 24 ? 3 : 1)];
    00204
    00205     for (x = 0; x < info->width; x++) {
    00206       if (info->bpp != 24) {
    00207         *pixel++ = gray_palette[*bitmap++];
    00208       } else {
    00209         *pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2));
    00210         bitmap += 3;
    00211       }
    00212     }
    00213   }
    00214 }
    00215
    00221 static bool ReadHeightmapBMP(char *filename, uint *x, uint *y, byte **map)
    00222 {
    00223   FILE *f;
    00224   BmpInfo info;
    00225   BmpData data;
    00226   BmpBuffer buffer;
    00227
    00228   /* Init BmpData */
    00229   memset(&data, 0, sizeof(data));
    00230
    00231   f = FioFOpenFile(filename, "rb");
    00232   if (f == NULL) {
    00233     ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, 0, 0);
    00234     return false;
    00235   }
    00236
    00237   BmpInitializeBuffer(&buffer, f);
    00238
    00239   if (!BmpReadHeader(&buffer, &info, &data)) {
    00240     ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, 0, 0);
    00241     fclose(f);
    00242     BmpDestroyData(&data);
    00243     return false;
    00244   }
    00245
    00246   if (map != NULL) {
    00247     if (!BmpReadBitmap(&buffer, &info, &data)) {
    00248       ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, 0, 0);
    00249       fclose(f);
    00250       BmpDestroyData(&data);
    00251       return false;
    00252     }
    00253
    00254     *map = MallocT<byte>(info.width * info.height);
    00255     ReadHeightmapBMPImageData(*map, &info, &data);
    00256   }
    00257
    00258   BmpDestroyData(&data);
    00259
    00260   *x = info.width;
    00261   *y = info.height;
    00262
    00263   fclose(f);
    00264   return true;
    00265 }
    00266
    00274 static void GrayscaleToMapHeights(uint img_width, uint img_height, byte *map)
    00275 {
    00276   /* Defines the detail of the aspect ratio (to avoid doubles) */
    00277   const uint num_div = 16384;
    00278
    00279   uint width, height;
    00280   uint row, col;
    00281   uint row_pad = 0, col_pad = 0;
    00282   uint img_scale;
    00283   uint img_row, img_col;
    00284   TileIndex tile;
    00285
    00286   /* Get map size and calculate scale and padding values */
    00287   switch (_settings_game.game_creation.heightmap_rotation) {
    00288     default: NOT_REACHED();
    00289     case HM_COUNTER_CLOCKWISE:
    00290       width   = MapSizeX();
    00291       height  = MapSizeY();
    00292       break;
    00293     case HM_CLOCKWISE:
    00294       width   = MapSizeY();
    00295       height  = MapSizeX();
    00296       break;
    00297   }
    00298
    00299   if ((img_width * num_div) / img_height > ((width * num_div) / height)) {
    00300     /* Image is wider than map - center vertically */
    00301     img_scale = (width * num_div) / img_width;
    00302     row_pad = (1 + height - ((img_height * img_scale) / num_div)) / 2;
    00303   } else {
    00304     /* Image is taller than map - center horizontally */
    00305     img_scale = (height * num_div) / img_height;
    00306     col_pad = (1 + width - ((img_width * img_scale) / num_div)) / 2;
    00307   }
    00308
    00309   if (_settings_game.construction.freeform_edges) {
    00310     for (uint x = 0; x < MapSizeX(); x++) MakeVoid(TileXY(x, 0));
    00311     for (uint y = 0; y < MapSizeY(); y++) MakeVoid(TileXY(0, y));
    00312   }
    00313
    00314   /* Form the landscape */
    00315   for (row = 0; row < height; row++) {
    00316     for (col = 0; col < width; col++) {
    00317       switch (_settings_game.game_creation.heightmap_rotation) {
    00318         default: NOT_REACHED();
    00319         case HM_COUNTER_CLOCKWISE: tile = TileXY(col, row); break;
    00320         case HM_CLOCKWISE:         tile = TileXY(row, col); break;
    00321       }
    00322
    00323       /* Check if current tile is within the 1-pixel map edge or padding regions */
    00324       if ((!_settings_game.construction.freeform_edges && DistanceFromEdge(tile) <= 1) ||
    00325           (row < row_pad) || (row >= (height - row_pad - (_settings_game.construction.freeform_edges ? 0 : 1))) ||
    00326           (col < col_pad) || (col >= (width  - col_pad - (_settings_game.construction.freeform_edges ? 0 : 1)))) {
    00327         SetTileHeight(tile, 0);
    00328       } else {
    00329         /* Use nearest neighbor resizing to scale map data.
    00330          *  We rotate the map 45 degrees (counter)clockwise */
    00331         img_row = (((row - row_pad) * num_div) / img_scale);
    00332         switch (_settings_game.game_creation.heightmap_rotation) {
    00333           default: NOT_REACHED();
    00334           case HM_COUNTER_CLOCKWISE:
    00335             img_col = (((width - 1 - col - col_pad) * num_div) / img_scale);
    00336             break;
    00337           case HM_CLOCKWISE:
    00338             img_col = (((col - col_pad) * num_div) / img_scale);
    00339             break;
    00340         }
    00341
    00342         assert(img_row < img_height);
    00343         assert(img_col < img_width);
    00344
    00345         /* Colour scales from 0 to 255, OpenTTD height scales from 0 to 15 */
    00346         SetTileHeight(tile, map[img_row * img_width + img_col] / 16);
    00347       }
    00348       /* Only clear the tiles within the map area. */
    00349       if (TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY() &&
    00350           (!_settings_game.construction.freeform_edges || (TileX(tile) != 0 && TileY(tile) != 0))) {
    00351         MakeClear(tile, CLEAR_GRASS, 3);
    00352       }
    00353     }
    00354   }
    00355 }
    00356
    00361 void FixSlopes()
    00362 {
    00363   uint width, height;
    00364   int row, col;
    00365   byte current_tile;
    00366
    00367   /* Adjust height difference to maximum one horizontal/vertical change. */
    00368   width   = MapSizeX();
    00369   height  = MapSizeY();
    00370
    00371   /* Top and left edge */
    00372   for (row = 0; (uint)row < height; row++) {
    00373     for (col = 0; (uint)col < width; col++) {
    00374       current_tile = MAX_TILE_HEIGHT;
    00375       if (col != 0) {
    00376         /* Find lowest tile; either the top or left one */
    00377         current_tile = TileHeight(TileXY(col - 1, row)); // top edge
    00378       }
    00379       if (row != 0) {
    00380         if (TileHeight(TileXY(col, row - 1)) < current_tile) {
    00381           current_tile = TileHeight(TileXY(col, row - 1)); // left edge
    00382         }
    00383       }
    00384
    00385       /* Does the height differ more than one? */
    00386       if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
    00387         /* Then change the height to be no more than one */
    00388         SetTileHeight(TileXY(col, row), current_tile + 1);
    00389       }
    00390     }
    00391   }
    00392
    00393   /* Bottom and right edge */
    00394   for (row = height - 1; row >= 0; row--) {
    00395     for (col = width - 1; col >= 0; col--) {
    00396       current_tile = MAX_TILE_HEIGHT;
    00397       if ((uint)col != width - 1) {
    00398         /* Find lowest tile; either the bottom and right one */
    00399         current_tile = TileHeight(TileXY(col + 1, row)); // bottom edge
    00400       }
    00401
    00402       if ((uint)row != height - 1) {
    00403         if (TileHeight(TileXY(col, row + 1)) < current_tile) {
    00404           current_tile = TileHeight(TileXY(col, row + 1)); // right edge
    00405         }
    00406       }
    00407
    00408       /* Does the height differ more than one? */
    00409       if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
    00410         /* Then change the height to be no more than one */
    00411         SetTileHeight(TileXY(col, row), current_tile + 1);
    00412       }
    00413     }
    00414   }
    00415 }
    00416
    00420 static bool ReadHeightMap(char *filename, uint *x, uint *y, byte **map)
    00421 {
    00422   switch (_file_to_saveload.mode) {
    00423     default: NOT_REACHED();
    00424 #ifdef WITH_PNG
    00425     case SL_PNG:
    00426       return ReadHeightmapPNG(filename, x, y, map);
    00427 #endif /* WITH_PNG */
    00428     case SL_BMP:
    00429       return ReadHeightmapBMP(filename, x, y, map);
    00430   }
    00431 }
    00432
    00433 bool GetHeightmapDimensions(char *filename, uint *x, uint *y)
    00434 {
    00435   return ReadHeightMap(filename, x, y, NULL);
    00436 }
    00437
    00438 void LoadHeightmap(char *filename)
    00439 {
    00440   uint x, y;
    00441   byte *map = NULL;
    00442
    00443   if (!ReadHeightMap(filename, &x, &y, &map)) {
    00444     free(map);
    00445     return;
    00446   }
    00447
    00448   GrayscaleToMapHeights(x, y, map);
    00449   free(map);
    00450
    00451   FixSlopes();
    00452   MarkWholeScreenDirty();
    00453 }
    00454
    00455 void FlatEmptyWorld(byte tile_height)
    00456 {
    00457   int edge_distance = _settings_game.construction.freeform_edges ? 0 : 2;
    00458   for (uint row = edge_distance; row < MapSizeY() - edge_distance; row++) {
    00459     for (uint col = edge_distance; col < MapSizeX() - edge_distance; col++) {
    00460       SetTileHeight(TileXY(col, row), tile_height);
    00461     }
    00462   }
    00463
    00464   FixSlopes();
    00465   MarkWholeScreenDirty();
    00466 }