heightmap.cpp

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

Generated on Mon Feb 16 23:12:07 2009 for openttd by  doxygen 1.5.6