# heightmap.cpp

Go to the documentation of this file.

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