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

Function ReadHeightmapBMP

src/heightmap.cpp:262–298  ·  view source on GitHub ↗

* Reads the heightmap and/or size of the heightmap from a BMP file. * If map == nullptr only the size of the BMP is read, otherwise a map * with greyscale pixels is allocated and assigned to *map. */

Source from the content-addressed store, hash-verified

260 * with greyscale pixels is allocated and assigned to *map.
261 */
262static bool ReadHeightmapBMP(std::string_view filename, uint *x, uint *y, std::vector<uint8_t> *map)
263{
264 auto f = FioFOpenFile(filename, "rb", HEIGHTMAP_DIR);
265 if (!f.has_value()) {
266 ShowErrorMessage(GetEncodedString(STR_ERROR_BMPMAP), GetEncodedString(STR_ERROR_PNGMAP_FILE_NOT_FOUND), WL_ERROR);
267 return false;
268 }
269
270 RandomAccessFile file(filename, HEIGHTMAP_DIR);
271 BmpInfo info{};
272 BmpData data{};
273
274 if (!BmpReadHeader(file, info, data)) {
275 ShowErrorMessage(GetEncodedString(STR_ERROR_BMPMAP), GetEncodedString(STR_ERROR_BMPMAP_IMAGE_TYPE), WL_ERROR);
276 return false;
277 }
278
279 if (!IsValidHeightmapDimension(info.width, info.height)) {
280 ShowErrorMessage(GetEncodedString(STR_ERROR_BMPMAP), GetEncodedString(STR_ERROR_HEIGHTMAP_TOO_LARGE), WL_ERROR);
281 return false;
282 }
283
284 if (map != nullptr) {
285 if (!BmpReadBitmap(file, info, data)) {
286 ShowErrorMessage(GetEncodedString(STR_ERROR_BMPMAP), GetEncodedString(STR_ERROR_BMPMAP_IMAGE_TYPE), WL_ERROR);
287 return false;
288 }
289
290 map->resize(static_cast<size_t>(info.width) * info.height);
291 ReadHeightmapBMPImageData(*map, info, data);
292 }
293
294 *x = info.width;
295 *y = info.height;
296
297 return true;
298}
299
300/**
301 * Converts a given greyscale map to something that fits in OTTD map system

Callers 1

ReadHeightMapFunction · 0.85

Calls 8

FioFOpenFileFunction · 0.85
ShowErrorMessageFunction · 0.85
BmpReadHeaderFunction · 0.85
BmpReadBitmapFunction · 0.85
resizeMethod · 0.80
GetEncodedStringFunction · 0.70

Tested by

no test coverage detected