/** * The PNG Heightmap loader. */ static void ReadHeightmapPNGImageData(byte *map, png_structp png_ptr, png_infop info_ptr) { uint x, y; byte gray_palette[256]; png_bytep *row_pointers = nullptr; bool has_palette = png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE; uint channels = png_get_channels(png_ptr, info_ptr); /* Get palette and convert it to grayscale */ if (has_palette) { int i; int palette_size; png_color *palette; bool all_gray = true; png_get_PLTE(png_ptr, info_ptr, &palette, &palette_size); for (i = 0; i < palette_size && (palette_size != 16 || all_gray); i++) { all_gray &= palette[i].red == palette[i].green && palette[i].red == palette[i].blue; gray_palette[i] = RGBToGrayscale(palette[i].red, palette[i].green, palette[i].blue); } /** * For a non-gray palette of size 16 we assume that * the order of the palette determines the height; * the first entry is the sea (level 0), the second one * level 1, etc. */ if (palette_size == 16 && !all_gray) { for (i = 0; i < palette_size; i++) { gray_palette[i] = 256 * i / palette_size; } } } row_pointers = png_get_rows(png_ptr, info_ptr); /* Read the raw image data and convert in 8-bit grayscale */ for (x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) { for (y = 0; y < png_get_image_height(png_ptr, info_ptr); y++) { byte *pixel = &map[y * png_get_image_width(png_ptr, info_ptr) + x]; uint x_offset = x * channels; if (has_palette) { *pixel = gray_palette[row_pointers[y][x_offset]]; } else if (channels == 3) { *pixel = RGBToGrayscale(row_pointers[y][x_offset + 0], row_pointers[y][x_offset + 1], row_pointers[y][x_offset + 2]); } else { *pixel = row_pointers[y][x_offset]; } } } }
/** * The BMP Heightmap loader. */ static void ReadHeightmapBMPImageData(byte *map, BmpInfo *info, BmpData *data) { uint x, y; byte gray_palette[256]; if (data->palette != NULL) { uint i; bool all_gray = true; if (info->palette_size != 2) { for (i = 0; i < info->palette_size && (info->palette_size != 16 || all_gray); i++) { all_gray &= data->palette[i].r == data->palette[i].g && data->palette[i].r == data->palette[i].b; gray_palette[i] = RGBToGrayscale(data->palette[i].r, data->palette[i].g, data->palette[i].b); } /** * For a non-gray palette of size 16 we assume that * the order of the palette determines the height; * the first entry is the sea (level 0), the second one * level 1, etc. */ if (info->palette_size == 16 && !all_gray) { for (i = 0; i < info->palette_size; i++) { gray_palette[i] = 256 * i / info->palette_size; } } } else { /** * For a palette of size 2 we assume that the order of the palette determines the height; * the first entry is the sea (level 0), the second one is the land (level 1) */ gray_palette[0] = 0; gray_palette[1] = 16; } } /* Read the raw image data and convert in 8-bit grayscale */ for (y = 0; y < info->height; y++) { byte *pixel = &map[y * info->width]; byte *bitmap = &data->bitmap[y * info->width * (info->bpp == 24 ? 3 : 1)]; for (x = 0; x < info->width; x++) { if (info->bpp != 24) { *pixel++ = gray_palette[*bitmap++]; } else { *pixel++ = RGBToGrayscale(*bitmap, *(bitmap + 1), *(bitmap + 2)); bitmap += 3; } } } }
uint8_t PixelToGrayscale(const pixel_t pixel) { return RGBToGrayscale(PixelToRGB(pixel)); }