heightmap.cpp

Go to the documentation of this file.
00001 /* $Id: heightmap.cpp 22886 2011-09-03 18:56:34Z frosch $ */
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, WL_ERROR);
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, WL_ERROR);
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, WL_ERROR);
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, WL_ERROR);
00140     fclose(fp);
00141     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
00142     return false;
00143   }
00144 
00145   uint width = png_get_image_width(png_ptr, info_ptr);
00146   uint height = png_get_image_height(png_ptr, info_ptr);
00147 
00148   /* Check if image dimensions don't overflow a size_t to avoid memory corruption. */
00149   if ((uint64)width * height >= (size_t)-1) {
00150     ShowErrorMessage(STR_ERROR_PNGMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
00151     fclose(fp);
00152     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
00153     return false;
00154   }
00155 
00156   if (map != NULL) {
00157     *map = MallocT<byte>(width * height);
00158     ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
00159   }
00160 
00161   *x = width;
00162   *y = height;
00163 
00164   fclose(fp);
00165   png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
00166   return true;
00167 }
00168 
00169 #endif /* WITH_PNG */
00170 
00171 
00175 static void ReadHeightmapBMPImageData(byte *map, BmpInfo *info, BmpData *data)
00176 {
00177   uint x, y;
00178   byte gray_palette[256];
00179 
00180   if (data->palette != NULL) {
00181     uint i;
00182     bool all_gray = true;
00183 
00184     if (info->palette_size != 2) {
00185       for (i = 0; i < info->palette_size && (info->palette_size != 16 || all_gray); i++) {
00186         all_gray &= data->palette[i].r == data->palette[i].g && data->palette[i].r == data->palette[i].b;
00187         gray_palette[i] = RGBToGrayscale(data->palette[i].r, data->palette[i].g, data->palette[i].b);
00188       }
00189 
00196       if (info->palette_size == 16 && !all_gray) {
00197         for (i = 0; i < info->palette_size; i++) {
00198           gray_palette[i] = 256 * i / info->palette_size;
00199         }
00200       }
00201     } else {
00206       gray_palette[0] = 0;
00207       gray_palette[1] = 16;
00208     }
00209   }
00210 
00211   /* Read the raw image data and convert in 8-bit grayscale */
00212   for (y = 0; y < info->height; y++) {
00213     byte *pixel = &map[y * info->width];
00214     byte *bitmap = &data->bitmap[y * info->width * (info->bpp == 24 ? 3 : 1)];
00215 
00216     for (x = 0; x < info->width; x++) {
00217       if (info->bpp != 24) {
00218         *pixel++ = gray_palette[*bitmap++];
00219       } else {
00220         *pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2));
00221         bitmap += 3;
00222       }
00223     }
00224   }
00225 }
00226 
00232 static bool ReadHeightmapBMP(char *filename, uint *x, uint *y, byte **map)
00233 {
00234   FILE *f;
00235   BmpInfo info;
00236   BmpData data;
00237   BmpBuffer buffer;
00238 
00239   /* Init BmpData */
00240   memset(&data, 0, sizeof(data));
00241 
00242   f = FioFOpenFile(filename, "rb");
00243   if (f == NULL) {
00244     ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_PNGMAP_FILE_NOT_FOUND, WL_ERROR);
00245     return false;
00246   }
00247 
00248   BmpInitializeBuffer(&buffer, f);
00249 
00250   if (!BmpReadHeader(&buffer, &info, &data)) {
00251     ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
00252     fclose(f);
00253     BmpDestroyData(&data);
00254     return false;
00255   }
00256 
00257   /* Check if image dimensions don't overflow a size_t to avoid memory corruption. */
00258   if ((uint64)info.width * info.height >= (size_t)-1 / (info.bpp == 24 ? 3 : 1)) {
00259     ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_HEIGHTMAP_TOO_LARGE, WL_ERROR);
00260     fclose(f);
00261     BmpDestroyData(&data);
00262     return false;
00263   }
00264 
00265   if (map != NULL) {
00266     if (!BmpReadBitmap(&buffer, &info, &data)) {
00267       ShowErrorMessage(STR_ERROR_BMPMAP, STR_ERROR_BMPMAP_IMAGE_TYPE, WL_ERROR);
00268       fclose(f);
00269       BmpDestroyData(&data);
00270       return false;
00271     }
00272 
00273     *map = MallocT<byte>(info.width * info.height);
00274     ReadHeightmapBMPImageData(*map, &info, &data);
00275   }
00276 
00277   BmpDestroyData(&data);
00278 
00279   *x = info.width;
00280   *y = info.height;
00281 
00282   fclose(f);
00283   return true;
00284 }
00285 
00293 static void GrayscaleToMapHeights(uint img_width, uint img_height, byte *map)
00294 {
00295   /* Defines the detail of the aspect ratio (to avoid doubles) */
00296   const uint num_div = 16384;
00297 
00298   uint width, height;
00299   uint row, col;
00300   uint row_pad = 0, col_pad = 0;
00301   uint img_scale;
00302   uint img_row, img_col;
00303   TileIndex tile;
00304 
00305   /* Get map size and calculate scale and padding values */
00306   switch (_settings_game.game_creation.heightmap_rotation) {
00307     default: NOT_REACHED();
00308     case HM_COUNTER_CLOCKWISE:
00309       width   = MapSizeX();
00310       height  = MapSizeY();
00311       break;
00312     case HM_CLOCKWISE:
00313       width   = MapSizeY();
00314       height  = MapSizeX();
00315       break;
00316   }
00317 
00318   if ((img_width * num_div) / img_height > ((width * num_div) / height)) {
00319     /* Image is wider than map - center vertically */
00320     img_scale = (width * num_div) / img_width;
00321     row_pad = (1 + height - ((img_height * img_scale) / num_div)) / 2;
00322   } else {
00323     /* Image is taller than map - center horizontally */
00324     img_scale = (height * num_div) / img_height;
00325     col_pad = (1 + width - ((img_width * img_scale) / num_div)) / 2;
00326   }
00327 
00328   if (_settings_game.construction.freeform_edges) {
00329     for (uint x = 0; x < MapSizeX(); x++) MakeVoid(TileXY(x, 0));
00330     for (uint y = 0; y < MapSizeY(); y++) MakeVoid(TileXY(0, y));
00331   }
00332 
00333   /* Form the landscape */
00334   for (row = 0; row < height; row++) {
00335     for (col = 0; col < width; col++) {
00336       switch (_settings_game.game_creation.heightmap_rotation) {
00337         default: NOT_REACHED();
00338         case HM_COUNTER_CLOCKWISE: tile = TileXY(col, row); break;
00339         case HM_CLOCKWISE:         tile = TileXY(row, col); break;
00340       }
00341 
00342       /* Check if current tile is within the 1-pixel map edge or padding regions */
00343       if ((!_settings_game.construction.freeform_edges && DistanceFromEdge(tile) <= 1) ||
00344           (row < row_pad) || (row >= (height - row_pad - (_settings_game.construction.freeform_edges ? 0 : 1))) ||
00345           (col < col_pad) || (col >= (width  - col_pad - (_settings_game.construction.freeform_edges ? 0 : 1)))) {
00346         SetTileHeight(tile, 0);
00347       } else {
00348         /* Use nearest neighbor resizing to scale map data.
00349          *  We rotate the map 45 degrees (counter)clockwise */
00350         img_row = (((row - row_pad) * num_div) / img_scale);
00351         switch (_settings_game.game_creation.heightmap_rotation) {
00352           default: NOT_REACHED();
00353           case HM_COUNTER_CLOCKWISE:
00354             img_col = (((width - 1 - col - col_pad) * num_div) / img_scale);
00355             break;
00356           case HM_CLOCKWISE:
00357             img_col = (((col - col_pad) * num_div) / img_scale);
00358             break;
00359         }
00360 
00361         assert(img_row < img_height);
00362         assert(img_col < img_width);
00363 
00364         /* Colour scales from 0 to 255, OpenTTD height scales from 0 to 15 */
00365         SetTileHeight(tile, map[img_row * img_width + img_col] / 16);
00366       }
00367       /* Only clear the tiles within the map area. */
00368       if (TileX(tile) != MapMaxX() && TileY(tile) != MapMaxY() &&
00369           (!_settings_game.construction.freeform_edges || (TileX(tile) != 0 && TileY(tile) != 0))) {
00370         MakeClear(tile, CLEAR_GRASS, 3);
00371       }
00372     }
00373   }
00374 }
00375 
00380 void FixSlopes()
00381 {
00382   uint width, height;
00383   int row, col;
00384   byte current_tile;
00385 
00386   /* Adjust height difference to maximum one horizontal/vertical change. */
00387   width   = MapSizeX();
00388   height  = MapSizeY();
00389 
00390   /* Top and left edge */
00391   for (row = 0; (uint)row < height; row++) {
00392     for (col = 0; (uint)col < width; col++) {
00393       current_tile = MAX_TILE_HEIGHT;
00394       if (col != 0) {
00395         /* Find lowest tile; either the top or left one */
00396         current_tile = TileHeight(TileXY(col - 1, row)); // top edge
00397       }
00398       if (row != 0) {
00399         if (TileHeight(TileXY(col, row - 1)) < current_tile) {
00400           current_tile = TileHeight(TileXY(col, row - 1)); // left edge
00401         }
00402       }
00403 
00404       /* Does the height differ more than one? */
00405       if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
00406         /* Then change the height to be no more than one */
00407         SetTileHeight(TileXY(col, row), current_tile + 1);
00408       }
00409     }
00410   }
00411 
00412   /* Bottom and right edge */
00413   for (row = height - 1; row >= 0; row--) {
00414     for (col = width - 1; col >= 0; col--) {
00415       current_tile = MAX_TILE_HEIGHT;
00416       if ((uint)col != width - 1) {
00417         /* Find lowest tile; either the bottom and right one */
00418         current_tile = TileHeight(TileXY(col + 1, row)); // bottom edge
00419       }
00420 
00421       if ((uint)row != height - 1) {
00422         if (TileHeight(TileXY(col, row + 1)) < current_tile) {
00423           current_tile = TileHeight(TileXY(col, row + 1)); // right edge
00424         }
00425       }
00426 
00427       /* Does the height differ more than one? */
00428       if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) {
00429         /* Then change the height to be no more than one */
00430         SetTileHeight(TileXY(col, row), current_tile + 1);
00431       }
00432     }
00433   }
00434 }
00435 
00439 static bool ReadHeightMap(char *filename, uint *x, uint *y, byte **map)
00440 {
00441   switch (_file_to_saveload.mode) {
00442     default: NOT_REACHED();
00443 #ifdef WITH_PNG
00444     case SL_PNG:
00445       return ReadHeightmapPNG(filename, x, y, map);
00446 #endif /* WITH_PNG */
00447     case SL_BMP:
00448       return ReadHeightmapBMP(filename, x, y, map);
00449   }
00450 }
00451 
00459 bool GetHeightmapDimensions(char *filename, uint *x, uint *y)
00460 {
00461   return ReadHeightMap(filename, x, y, NULL);
00462 }
00463 
00470 void LoadHeightmap(char *filename)
00471 {
00472   uint x, y;
00473   byte *map = NULL;
00474 
00475   if (!ReadHeightMap(filename, &x, &y, &map)) {
00476     free(map);
00477     return;
00478   }
00479 
00480   GrayscaleToMapHeights(x, y, map);
00481   free(map);
00482 
00483   FixSlopes();
00484   MarkWholeScreenDirty();
00485 }
00486 
00491 void FlatEmptyWorld(byte tile_height)
00492 {
00493   int edge_distance = _settings_game.construction.freeform_edges ? 0 : 2;
00494   for (uint row = edge_distance; row < MapSizeY() - edge_distance; row++) {
00495     for (uint col = edge_distance; col < MapSizeX() - edge_distance; col++) {
00496       SetTileHeight(TileXY(col, row), tile_height);
00497     }
00498   }
00499 
00500   FixSlopes();
00501   MarkWholeScreenDirty();
00502 }