truelight@4300: /* $Id$ */ truelight@4300: truelight@4300: #include "stdafx.h" truelight@4300: #include "openttd.h" truelight@4300: #include "variables.h" truelight@4300: #include "functions.h" truelight@4300: #include "heightmap.h" truelight@4300: #include "clear_map.h" truelight@4300: #include "table/strings.h" truelight@4300: #include "void_map.h" truelight@4300: #include "debug.h" truelight@4300: #include "gfx.h" truelight@4300: #include "gui.h" truelight@4300: #include "saveload.h" truelight@4300: #include "bmp.h" truelight@4300: truelight@4300: /** truelight@4300: * Convert RGB colors to Grayscale using 29.9% Red, 58.7% Green, 11.4% Blue truelight@4300: * (average luminosity formula) -- Dalestan truelight@4300: * This in fact is the NTSC Color Space -- TrueLight truelight@4300: */ truelight@4300: static inline byte RGBToGrayscale(byte red, byte green, byte blue) truelight@4300: { truelight@4300: /* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then truelight@4300: * divide by it to normalize the value to a byte again. */ truelight@4300: return ((red * 19595) + (green * 38470) + (blue * 7471)) / 65536; truelight@4300: } truelight@4300: truelight@4300: truelight@4300: #ifdef WITH_PNG truelight@4300: truelight@4300: #include "png.h" truelight@4300: truelight@4300: /** truelight@4300: * The PNG Heightmap loader. truelight@4300: */ truelight@4300: static void ReadHeightmapPNGImageData(byte *map, png_structp png_ptr, png_infop info_ptr) truelight@4300: { truelight@4300: uint x, y; truelight@4300: byte gray_palette[256]; truelight@4300: png_bytep *row_pointers = NULL; truelight@4300: truelight@4300: /* Get palette and convert it to grayscale */ truelight@4300: if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { truelight@4300: int i; truelight@4300: int palette_size; truelight@4300: png_color *palette; truelight@4300: bool all_gray = true; truelight@4300: truelight@4300: png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size); truelight@4300: for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) { truelight@4300: all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue; truelight@4300: gray_palette[i] = RGBToGrayscale(palette[i].red, palette[i].green, palette[i].blue); truelight@4300: } truelight@4300: truelight@4300: /** truelight@4300: * For a non-gray palette of size 16 we assume that truelight@4300: * the order of the palette determines the height; truelight@4300: * the first entry is the sea (level 0), the second one truelight@4300: * level 1, etc. truelight@4300: */ truelight@4300: if (palette_size == 16 && !all_gray) { truelight@4300: for (i = 0; i < palette_size; i++) { truelight@4300: gray_palette[i] = 256 * i / palette_size; truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: row_pointers = png_get_rows(png_ptr, info_ptr); truelight@4300: truelight@4300: /* Read the raw image data and convert in 8-bit grayscale */ truelight@4300: for (x = 0; x < info_ptr->width; x++) { truelight@4300: for (y = 0; y < info_ptr->height; y++) { truelight@4300: byte *pixel = &map[y * info_ptr->width + x]; truelight@4300: uint x_offset = x * info_ptr->channels; truelight@4300: truelight@4300: if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) { truelight@4300: *pixel = gray_palette[row_pointers[y][x_offset]]; truelight@4300: } else if (info_ptr->channels == 3) { truelight@4300: *pixel = RGBToGrayscale(row_pointers[y][x_offset + 0], truelight@4300: row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]); truelight@4300: } else { truelight@4300: *pixel = row_pointers[y][x_offset]; truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /** truelight@4300: * Reads the heightmap and/or size of the heightmap from a PNG file. truelight@4300: * If map == NULL only the size of the PNG is read, otherwise a map truelight@4300: * with grayscale pixels is allocated and assigned to *map. truelight@4300: */ truelight@4300: static bool ReadHeightmapPNG(char *filename, uint *x, uint *y, byte **map) truelight@4300: { truelight@4300: FILE *fp; truelight@4300: png_structp png_ptr = NULL; truelight@4300: png_infop info_ptr = NULL; truelight@4300: truelight@4300: fp = fopen(filename, "rb"); truelight@4300: if (fp == NULL) { truelight@4300: ShowErrorMessage(STR_PNGMAP_ERR_FILE_NOT_FOUND, STR_PNGMAP_ERROR, 0, 0); truelight@4300: return false; truelight@4300: } truelight@4300: truelight@4300: png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); truelight@4300: if (png_ptr == NULL) { truelight@4300: ShowErrorMessage(STR_PNGMAP_ERR_MISC, STR_PNGMAP_ERROR, 0, 0); truelight@4300: fclose(fp); truelight@4300: return false; truelight@4300: } truelight@4300: truelight@4300: info_ptr = png_create_info_struct(png_ptr); truelight@4300: if (info_ptr == NULL || setjmp(png_jmpbuf(png_ptr))) { truelight@4300: ShowErrorMessage(STR_PNGMAP_ERR_MISC, STR_PNGMAP_ERROR, 0, 0); truelight@4300: fclose(fp); truelight@4300: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); truelight@4300: return false; truelight@4300: } truelight@4300: truelight@4300: png_init_io(png_ptr, fp); truelight@4300: truelight@4300: /* Allocate memory and read image, without alpha or 16-bit samples rubidium@4549: * (result is either 8-bit indexed/grayscale or 24-bit RGB) */ truelight@4300: png_set_packing(png_ptr); truelight@4300: png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING | PNG_TRANSFORM_STRIP_ALPHA | PNG_TRANSFORM_STRIP_16, NULL); truelight@4300: truelight@4300: /* Maps of wrong color-depth are not used. truelight@4300: * (this should have been taken care of by stripping alpha and 16-bit samples on load) */ truelight@4300: if ((info_ptr->channels != 1) && (info_ptr->channels != 3) && (info_ptr->bit_depth != 8)) { truelight@4300: ShowErrorMessage(STR_PNGMAP_ERR_IMAGE_TYPE, STR_PNGMAP_ERROR, 0, 0); truelight@4300: fclose(fp); truelight@4300: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); truelight@4300: return false; truelight@4300: } truelight@4300: truelight@4300: if (map != NULL) { truelight@4300: *map = malloc(info_ptr->width * info_ptr->height * sizeof(byte)); truelight@4300: truelight@4300: if (*map == NULL) { truelight@4300: ShowErrorMessage(STR_PNGMAP_ERR_MISC, STR_PNGMAP_ERROR, 0, 0); truelight@4300: fclose(fp); truelight@4300: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); truelight@4300: return false; truelight@4300: } truelight@4300: truelight@4300: ReadHeightmapPNGImageData(*map, png_ptr, info_ptr); truelight@4300: } truelight@4300: truelight@4300: *x = info_ptr->width; truelight@4300: *y = info_ptr->height; truelight@4300: truelight@4300: fclose(fp); truelight@4300: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); truelight@4300: return true; truelight@4300: } truelight@4300: truelight@4300: #endif /* WITH_PNG */ truelight@4300: truelight@4300: truelight@4300: /** truelight@4300: * The BMP Heightmap loader. truelight@4300: */ truelight@4300: static void ReadHeightmapBMPImageData(byte *map, BmpInfo *info, BmpData *data) truelight@4300: { truelight@4300: uint x, y; truelight@4300: byte gray_palette[256]; truelight@4300: truelight@4300: if (data->palette != NULL) { truelight@4300: uint i; truelight@4300: bool all_gray = true; truelight@4300: truelight@4300: if (info->palette_size != 2) { truelight@4300: for (i = 0; i < info->palette_size && (info->palette_size != 16 || all_gray); i++) { truelight@4300: all_gray &= data->palette[i].r == data->palette[i].g && data->palette[i].r == data->palette[i].b; truelight@4300: gray_palette[i] = RGBToGrayscale(data->palette[i].r, data->palette[i].g, data->palette[i].b); truelight@4300: } truelight@4300: truelight@4300: /** truelight@4300: * For a non-gray palette of size 16 we assume that truelight@4300: * the order of the palette determines the height; truelight@4300: * the first entry is the sea (level 0), the second one truelight@4300: * level 1, etc. truelight@4300: */ truelight@4300: if (info->palette_size == 16 && !all_gray) { truelight@4300: for (i = 0; i < info->palette_size; i++) { truelight@4300: gray_palette[i] = 256 * i / info->palette_size; truelight@4300: } truelight@4300: } truelight@4300: } else { truelight@4300: /** truelight@4300: * For a palette of size 2 we assume that the order of the palette determines the height; truelight@4300: * the first entry is the sea (level 0), the second one is the land (level 1) truelight@4300: */ truelight@4300: gray_palette[0] = 0; truelight@4300: gray_palette[1] = 16; truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /* Read the raw image data and convert in 8-bit grayscale */ truelight@4300: for (y = 0; y < info->height; y++) { truelight@4300: byte *pixel = &map[y * info->width]; truelight@4300: byte *bitmap = &data->bitmap[y * info->width * (info->bpp == 24 ? 3 : 1)]; truelight@4300: truelight@4300: for (x = 0; x < info->width; x++) { truelight@4300: if (info->bpp != 24) { truelight@4300: *pixel++ = gray_palette[*bitmap++]; truelight@4300: } else { truelight@4300: *pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2)); truelight@4300: bitmap += 3; truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /** truelight@4300: * Reads the heightmap and/or size of the heightmap from a BMP file. truelight@4300: * If map == NULL only the size of the BMP is read, otherwise a map truelight@4300: * with grayscale pixels is allocated and assigned to *map. truelight@4300: */ truelight@4300: static bool ReadHeightmapBMP(char *filename, uint *x, uint *y, byte **map) truelight@4300: { truelight@4300: FILE *f; truelight@4300: BmpInfo info; truelight@4300: BmpData data; truelight@4300: BmpBuffer buffer; truelight@4300: truelight@4300: f = fopen(filename, "rb"); truelight@4300: if (f == NULL) { truelight@4300: ShowErrorMessage(STR_PNGMAP_ERR_FILE_NOT_FOUND, STR_BMPMAP_ERROR, 0, 0); truelight@4300: return false; truelight@4300: } truelight@4300: truelight@4300: BmpInitializeBuffer(&buffer, f); truelight@4300: truelight@4300: if (!BmpReadHeader(&buffer, &info, &data)) { truelight@4300: ShowErrorMessage(STR_BMPMAP_ERR_IMAGE_TYPE, STR_BMPMAP_ERROR, 0, 0); truelight@4300: fclose(f); truelight@4300: BmpDestroyData(&data); truelight@4300: return false; truelight@4300: } truelight@4300: truelight@4300: if (map != NULL) { truelight@4300: if (!BmpReadBitmap(&buffer, &info, &data)) { truelight@4300: ShowErrorMessage(STR_BMPMAP_ERR_IMAGE_TYPE, STR_BMPMAP_ERROR, 0, 0); truelight@4300: fclose(f); truelight@4300: BmpDestroyData(&data); truelight@4300: return false; truelight@4300: } truelight@4300: truelight@4300: *map = malloc(info.width * info.height * sizeof(byte)); truelight@4300: if (*map == NULL) { truelight@4300: ShowErrorMessage(STR_PNGMAP_ERR_MISC, STR_BMPMAP_ERROR, 0, 0); truelight@4300: fclose(f); truelight@4300: BmpDestroyData(&data); truelight@4300: return false; truelight@4300: } truelight@4300: truelight@4300: ReadHeightmapBMPImageData(*map, &info, &data); truelight@4300: truelight@4300: } truelight@4300: truelight@4300: BmpDestroyData(&data); truelight@4300: truelight@4300: *x = info.width; truelight@4300: *y = info.height; truelight@4300: truelight@4300: fclose(f); truelight@4300: return true; truelight@4300: } truelight@4300: truelight@4300: static void GrayscaleToMapHeights(uint img_width, uint img_height, byte *map) truelight@4300: { truelight@4300: /* Defines the detail of the aspect ratio (to avoid doubles) */ truelight@4300: const uint num_div = 16384; truelight@4300: truelight@4300: uint width, height; truelight@4300: uint row, col; truelight@4300: uint row_pad = 0, col_pad = 0; truelight@4300: uint img_scale; truelight@4300: uint img_row, img_col; truelight@4300: TileIndex tile; truelight@4300: truelight@4300: /* Get map size and calculate scale and padding values */ truelight@4300: switch (_patches.heightmap_rotation) { truelight@4300: case HM_COUNTER_CLOCKWISE: truelight@4300: width = MapSizeX(); truelight@4300: height = MapSizeY(); truelight@4300: break; truelight@4300: case HM_CLOCKWISE: truelight@4300: width = MapSizeY(); truelight@4300: height = MapSizeX(); truelight@4300: break; truelight@4300: default: truelight@4300: NOT_REACHED(); truelight@4300: /* Avoids compiler warnings */ truelight@4300: return; truelight@4300: } truelight@4300: truelight@4300: if ((img_width * num_div) / img_height > ((width * num_div) / height)) { truelight@4300: /* Image is wider than map - center vertically */ truelight@4300: img_scale = (width * num_div) / img_width; truelight@4300: row_pad = (height - ((img_height * img_scale) / num_div)) / 2; truelight@4300: } else { truelight@4300: /* Image is taller than map - center horizontally */ truelight@4300: img_scale = (height * num_div) / img_height; truelight@4300: col_pad = (width - ((img_width * img_scale) / num_div)) / 2; truelight@4300: } truelight@4300: truelight@4300: /* Form the landscape */ truelight@4300: for (row = 0; row < height - 1; row++) { truelight@4300: for (col = 0; col < width - 1; col++) { truelight@4300: switch (_patches.heightmap_rotation) { truelight@4300: case HM_COUNTER_CLOCKWISE: tile = TileXY(col, row); break; truelight@4300: case HM_CLOCKWISE: tile = TileXY(row, col); break; truelight@4300: default: NOT_REACHED(); return; truelight@4300: } truelight@4300: truelight@4300: /* Check if current tile is within the 1-pixel map edge or padding regions */ truelight@4300: if ((DistanceFromEdge(tile) <= 1) || rubidium@5558: (row < row_pad) || (row >= (img_height + row_pad)) || rubidium@5558: (col < col_pad) || (col >= (img_width + col_pad))) { truelight@4300: SetTileHeight(tile, 0); truelight@4300: } else { truelight@4300: /* Use nearest neighbor resizing to scale map data. truelight@4300: * We rotate the map 45 degrees (counter)clockwise */ truelight@4300: img_row = (((row - row_pad) * num_div) / img_scale); truelight@4300: switch (_patches.heightmap_rotation) { truelight@4300: case HM_COUNTER_CLOCKWISE: truelight@4300: img_col = (((width - 1 - col - col_pad) * num_div) / img_scale); truelight@4300: break; truelight@4300: case HM_CLOCKWISE: truelight@4300: img_col = (((col - col_pad) * num_div) / img_scale); truelight@4300: break; truelight@4300: default: truelight@4300: NOT_REACHED(); truelight@4300: /* Avoids compiler warnings */ truelight@4300: return; truelight@4300: } truelight@4300: truelight@4300: assert(img_row < img_height); truelight@4300: assert(img_col < img_width); truelight@4300: truelight@4300: /* Color scales from 0 to 255, OpenTTD height scales from 0 to 15 */ truelight@4300: SetTileHeight(tile, map[img_row * img_width + img_col] / 16); truelight@4300: } truelight@4300: MakeClear(tile, CLEAR_GRASS, 3); truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /** truelight@4300: * This function takes care of the fact that land in OpenTTD can never differ truelight@4300: * more than 1 in height truelight@4300: */ truelight@4300: static void FixSlopes(void) truelight@4300: { truelight@4300: uint width, height; truelight@4300: uint row, col; truelight@4300: byte current_tile; truelight@4300: truelight@4300: /* Adjust height difference to maximum one horizontal/vertical change. */ truelight@4300: width = MapSizeX(); truelight@4300: height = MapSizeY(); truelight@4300: truelight@4300: /* Top and left edge */ truelight@4300: for (row = 1; row < height - 2; row++) { truelight@4300: for (col = 1; col < width - 2; col++) { truelight@4300: /* Find lowest tile; either the top or left one */ truelight@4300: current_tile = TileHeight(TileXY(col - 1, row)); // top edge truelight@4300: if (TileHeight(TileXY(col, row - 1)) < current_tile) { truelight@4300: current_tile = TileHeight(TileXY(col, row - 1)); // left edge truelight@4300: } truelight@4300: truelight@4300: /* Does the height differ more than one? */ truelight@4300: if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) { truelight@4300: /* Then change the height to be no more than one */ truelight@4300: SetTileHeight(TileXY(col, row), current_tile + 1); truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /* Bottom and right edge */ truelight@4300: for (row = height - 2; row > 0; row--) { truelight@4300: for (col = width - 2; col > 0; col--) { truelight@4300: /* Find lowest tile; either the bottom and right one */ truelight@4300: current_tile = TileHeight(TileXY(col + 1, row)); // bottom edge truelight@4300: if (TileHeight(TileXY(col, row + 1)) < current_tile) { truelight@4300: current_tile = TileHeight(TileXY(col, row + 1)); // right edge truelight@4300: } truelight@4300: truelight@4300: /* Does the height differ more than one? */ truelight@4300: if (TileHeight(TileXY(col, row)) >= (uint)current_tile + 2) { truelight@4300: /* Then change the height to be no more than one */ truelight@4300: SetTileHeight(TileXY(col, row), current_tile + 1); truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: /** truelight@4300: * Reads the heightmap with the correct file reader truelight@4300: */ truelight@4300: static bool ReadHeightMap(char *filename, uint *x, uint *y, byte **map) truelight@4300: { truelight@4300: switch (_file_to_saveload.mode) { truelight@4300: #ifdef WITH_PNG truelight@4300: case SL_PNG: truelight@4300: return ReadHeightmapPNG(filename, x, y, map); truelight@4300: #endif /* WITH_PNG */ truelight@4300: case SL_BMP: truelight@4300: return ReadHeightmapBMP(filename, x, y, map); truelight@4300: truelight@4300: default: truelight@4300: NOT_REACHED(); truelight@4300: /* Avoids compiler warnings */ truelight@4300: return false; truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: bool GetHeightmapDimensions(char *filename, uint *x, uint *y) truelight@4300: { truelight@4300: return ReadHeightMap(filename, x, y, NULL); truelight@4300: } truelight@4300: truelight@4300: void LoadHeightmap(char *filename) truelight@4300: { truelight@4300: uint x, y; truelight@4300: byte *map = NULL; truelight@4300: truelight@4300: if (!ReadHeightMap(filename, &x, &y, &map)) { truelight@4300: free(map); truelight@4300: return; truelight@4300: } truelight@4300: truelight@4300: GrayscaleToMapHeights(x, y, map); truelight@4300: free(map); truelight@4300: truelight@4300: FixSlopes(); truelight@4300: MarkWholeScreenDirty(); truelight@4300: } truelight@4300: truelight@4300: void FlatEmptyWorld(byte tile_height) truelight@4300: { truelight@4300: uint width, height; truelight@4300: uint row, col; truelight@4300: truelight@4300: width = MapSizeX(); truelight@4300: height = MapSizeY(); truelight@4300: truelight@4300: for (row = 2; row < height - 2; row++) { truelight@4300: for (col = 2; col < width - 2; col++) { truelight@4300: SetTileHeight(TileXY(col, row), tile_height); truelight@4300: } truelight@4300: } truelight@4300: truelight@4300: FixSlopes(); truelight@4300: MarkWholeScreenDirty(); truelight@4300: }