MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / ReadHeightmapBMPImageData

Function ReadHeightmapBMPImageData

src/heightmap.cpp:207–255  ·  view source on GitHub ↗

* The BMP Heightmap loader. */

Source from the content-addressed store, hash-verified

205 * The BMP Heightmap loader.
206 */
207static void ReadHeightmapBMPImageData(std::span<uint8_t> map, const BmpInfo &info, const BmpData &data)
208{
209 uint8_t gray_palette[256];
210
211 if (!data.palette.empty()) {
212 bool all_gray = true;
213
214 if (info.palette_size != 2) {
215 for (uint i = 0; i < info.palette_size && (info.palette_size != 16 || all_gray); i++) {
216 all_gray &= data.palette[i].r == data.palette[i].g && data.palette[i].r == data.palette[i].b;
217 gray_palette[i] = RGBToGreyscale(data.palette[i].r, data.palette[i].g, data.palette[i].b);
218 }
219
220 /**
221 * For a non-gray palette of size 16 we assume that
222 * the order of the palette determines the height;
223 * the first entry is the sea (level 0), the second one
224 * level 1, etc.
225 */
226 if (info.palette_size == 16 && !all_gray) {
227 for (uint i = 0; i < info.palette_size; i++) {
228 gray_palette[i] = 256 * i / info.palette_size;
229 }
230 }
231 } else {
232 /**
233 * For a palette of size 2 we assume that the order of the palette determines the height;
234 * the first entry is the sea (level 0), the second one is the land (level 1)
235 */
236 gray_palette[0] = 0;
237 gray_palette[1] = 16;
238 }
239 }
240
241 /* Read the raw image data and convert in 8-bit greyscale */
242 for (uint y = 0; y < info.height; y++) {
243 uint8_t *pixel = &map[y * static_cast<size_t>(info.width)];
244 const uint8_t *bitmap = &data.bitmap[y * static_cast<size_t>(info.width) * (info.bpp == 24 ? 3 : 1)];
245
246 for (uint x = 0; x < info.width; x++) {
247 if (info.bpp != 24) {
248 *pixel++ = gray_palette[*bitmap++];
249 } else {
250 *pixel++ = RGBToGreyscale(*bitmap, *(bitmap + 1), *(bitmap + 2));
251 bitmap += 3;
252 }
253 }
254 }
255}
256
257/**
258 * Reads the heightmap and/or size of the heightmap from a BMP file.

Callers 1

ReadHeightmapBMPFunction · 0.85

Calls 2

RGBToGreyscaleFunction · 0.85
emptyMethod · 0.45

Tested by

no test coverage detected