HomeWildfire Games

Fix loading grayscale heightmaps for RM maps.

Description

Fix loading grayscale heightmaps for RM maps.

rP2113 introduced code to read Heightmap images into RM terrain data. However, the original diff contained a bug where it read Out-of-bounds array data for grayscale images. This bug was hidden by another issue until D1816 / rP22892, which changed the code and made the OOB read actually relevant. The effect was twofold:

  • The height chosen was not the max of the 3 color channels, but the max of the 3 neighboring pixel (thus slightly lowering the quality of generated maps)
  • The height for the bottom-right coordinates were random memory values, thus garbage.

This random height ended up resulting in non-deterministic map generation, which was reported on Ngorongoro.

The cause of this silent failure is that the transformation to BGRA is not applied for grayscale images, as the alpha transformation is processed first and exits. This is not per-se buggy, so it is not changed here.
The fixed behaviour is specialized for the common grayscale case, and retains the max-of-3-color-channel intended behaviour otherwise.

Fixes #6261.

Reported by: Feldfeld

Differential Revision: https://code.wildfiregames.com/D4203

Event Timeline

The commit can be reverted after fixing the bug in the tex.cpp. Reported the original bug in #5022.

The commit can be reverted after fixing the bug in the tex.cpp. Reported the original bug in #5022.

I would argue against it, as the transformation is less efficient. For that matter, I'm not entirely sure we actually need to transform much here.

I would argue against it, as the transformation is less efficient. For that matter, I'm not entirely sure we actually need to transform much here.

I don't insist but the code is still broken, it doesn't support RGB/grayscale in 16bits, palette images. So the commit will be reverted with a fixed transformation or it needs to support all possible cases without transformation at all.