Skip to content

Commit

Permalink
XYZ elevation: add terrarium elevation encoding support
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Jan 8, 2025
1 parent bb525fc commit 0f90160
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/osgEarth/XYZ
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/

#ifndef OSGEARTH_XYZ_H
#define OSGEARTH_XYZ_H
#pragma once

#include <osgEarth/Common>
#include <osgEarth/ImageLayer>
Expand Down Expand Up @@ -220,5 +218,3 @@ namespace osgEarth

OSGEARTH_SPECIALIZE_CONFIG(osgEarth::XYZImageLayer::Options);
OSGEARTH_SPECIALIZE_CONFIG(osgEarth::XYZElevationLayer::Options);

#endif // OSGEARTH_XYZ_H
27 changes: 27 additions & 0 deletions src/osgEarth/XYZ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ XYZElevationLayerOptions::fromConfig(const Config& conf)
conf.get("format", _format);
conf.get("invert_y", _invertY);
conf.get("elevation_encoding", _elevationEncoding);
conf.get("interpretation", elevationEncoding()); // compat with QGIS
}

Config
Expand Down Expand Up @@ -337,6 +338,32 @@ XYZElevationLayer::createHeightFieldImplementation(const TileKey& key, ProgressC

return GeoHeightField(hf, key.getExtent());
}

else if (options().elevationEncoding() == "terrarium")
{
// Allocate the heightfield.
osg::HeightField* hf = new osg::HeightField();
hf->allocate(image->s(), image->t());

ImageUtils::PixelReader reader(image);
osg::Vec4f pixel;

for (int c = 0; c < image->s(); c++)
{
for (int r = 0; r < image->t(); r++)
{
reader(pixel, c, r);
pixel.r() *= 255.0;
pixel.g() *= 255.0;
pixel.b() *= 255.0;
float h = (pixel.r() * 256.0f + pixel.g() + pixel.b() / 256.0f) - 32768.0f;
hf->setHeight(c, r, h);
}
}

return GeoHeightField(hf, key.getExtent());
}

else
{
ImageToHeightFieldConverter conv;
Expand Down

0 comments on commit 0f90160

Please sign in to comment.