Skip to content

Commit

Permalink
opengl::Texture now can render 16bit images
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Nov 25, 2024
1 parent 40f443b commit 07783e5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
4 changes: 3 additions & 1 deletion doc/source/doxygen-docs/changelog.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
5 changes: 5 additions & 0 deletions libs/opengl/include/mrpt/opengl/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 40 additions & 3 deletions libs/opengl/src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#include <mrpt/opengl/Texture.h>
#include <mrpt/opengl/opengl_api.h>

// Universal include for all versions of OpenCV
#include <mrpt/3rdparty/do_opencv_includes.h>

#include <iostream>
#include <mutex>
#include <set>
Expand Down Expand Up @@ -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<double>(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<int>(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);

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 07783e5

Please sign in to comment.