diff --git a/doc/source/doxygen-docs/changelog.md b/doc/source/doxygen-docs/changelog.md index 49053a2910..17538cb25c 100644 --- a/doc/source/doxygen-docs/changelog.md +++ b/doc/source/doxygen-docs/changelog.md @@ -1,7 +1,9 @@ \page changelog Change Log # Version 2.14.6: UNRELEASED -(None yet) +- Changes in libraries: + - \ref mrpt_opengl_grp: + - mrpt::opengl::Texture now supports rendering 16-bit images. # Version 2.14.5: Released Nov 4th, 2024 - Upgraded nanoflann to v1.6.2 (fixes a potential bug). diff --git a/libs/opengl/include/mrpt/opengl/Texture.h b/libs/opengl/include/mrpt/opengl/Texture.h index d521f6e29f..fec04e8dd4 100644 --- a/libs/opengl/include/mrpt/opengl/Texture.h +++ b/libs/opengl/include/mrpt/opengl/Texture.h @@ -71,6 +71,11 @@ class Texture /** How to repeat texture coordinate "T" */ Wrapping wrappingModeT = Wrapping::Repeat; + + /** If enabled, 16-bit images will be automatically bright-adjusted so they are visible + * as 8-bit image textures. Set to false to disable adjustment. + */ + bool autoScale16to8bitConversion = true; }; /** This is how an 2D texture image is loaded into this object, and a diff --git a/libs/opengl/src/Texture.cpp b/libs/opengl/src/Texture.cpp index 7fe4bb0fd1..98cc050121 100644 --- a/libs/opengl/src/Texture.cpp +++ b/libs/opengl/src/Texture.cpp @@ -14,6 +14,9 @@ #include #include +// Universal include for all versions of OpenCV +#include + #include #include #include @@ -244,11 +247,45 @@ void Texture::internalAssignImage_2D( in_rgb->forceLoad(); // just in case they are lazy-load imgs if (in_alpha) in_alpha->forceLoad(); - ASSERT_(in_rgb->getPixelDepth() == mrpt::img::PixelDepth::D8U); + mrpt::img::CImage rgb; + + switch (in_rgb->getPixelDepth()) + { + // Ideal case, nothing to do: + case mrpt::img::PixelDepth::D8U: + rgb = mrpt::img::CImage(*in_rgb, mrpt::img::SHALLOW_COPY); + break; + + case mrpt::img::PixelDepth::D16U: + { +#if MRPT_HAS_OPENCV + double ratio; + if (o.autoScale16to8bitConversion) + { + // enhance brigthness: + + cv::Scalar meanColor = cv::mean(in_rgb->asCvMatRef()); + const double avrVal = meanColor.val[0]; + ratio = std::max(1.0, 2 * avrVal); + } + else + { + ratio = 65536.0; + } + + rgb.resize(in_rgb->getWidth(), in_rgb->getHeight(), mrpt::img::CH_RGB); + cv::convertScaleAbs(in_rgb->asCvMatRef(), rgb.asCvMatRef(), 255.0 / ratio); +#endif + } + break; + + default: + THROW_EXCEPTION_FMT( + "Unhandled pixel depth: PixelDepth=#%i", static_cast(in_rgb->getPixelDepth())); + }; // Shallow copy of the images, for the case we need to downsample them // below: - mrpt::img::CImage rgb(*in_rgb, mrpt::img::SHALLOW_COPY); mrpt::img::CImage alpha; if (in_alpha) alpha = mrpt::img::CImage(*in_alpha, mrpt::img::SHALLOW_COPY); @@ -299,7 +336,7 @@ void Texture::internalAssignImage_2D( glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, lmbdWrapMap(o.wrappingModeT)); CHECK_OPENGL_ERROR_IN_DEBUG(); - // Assure that the images do not overpass the maximum dimensions allowed + // Ensure that the images do not overpass the maximum dimensions allowed // by OpenGL: // ------------------------------------------------------------------------------------ GLint texSize;