Skip to content
This repository has been archived by the owner on Jan 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #11 from alicevision/fix/handleNaN
Browse files Browse the repository at this point in the history
Handle NaN in images when using jetColorMap
  • Loading branch information
fabiencastan authored Jul 12, 2019
2 parents e9798a7 + d1ad152 commit 34e983c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/depthMapEntity/DepthMapEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ void DepthMapEntity::loadDepthMap()
}
else
{
float normalizedDepthValue = (depthValue - stats.min[0]) / (stats.max[0] - stats.min[0]);
const float range = stats.max[0] - stats.min[0];
float normalizedDepthValue = range != 0.0f ? (depthValue - stats.min[0]) / range : 1.0f;
Color32f color = getColor32fFromJetColorMapClamp(normalizedDepthValue);
colors.push_back(color);
}
Expand Down
35 changes: 13 additions & 22 deletions src/imageIOHandler/QtOIIOHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,34 +194,20 @@ bool QtOIIOHandler::read(QImage *image)
// perceptually uniform: "inferno", "viridis", "magma", "plasma" -- others: "blue-red", "spectrum", "heat"
const char* colorMapEnv = std::getenv("QTOIIO_COLORMAP");
const std::string colorMapType = colorMapEnv ? colorMapEnv : "plasma";

// detect AliceVision special files that require a jetColorMap based conversion
const bool isDepthMap = d->fileName().contains("depthMap");
const bool isNmodMap = d->fileName().contains("nmodMap");

if(colorMapEnv)
{
qDebug() << "[QtOIIO] compute colormap \"" << colorMapType.c_str() << "\"";
oiio::ImageBufAlgo::color_map(tmpBuf, inBuf, 0, colorMapType);
}
else if(d->fileName().contains("depthMap"))
{
oiio::ImageBufAlgo::PixelStats stats;
oiio::ImageBufAlgo::computePixelStats(stats, inBuf);

#pragma omp parallel for
for(int y = 0; y < inSpec.height; ++y)
{
for(int x = 0; x < inSpec.width; ++x)
{
float depthValue = 0.0f;
inBuf.getpixel(x, y, &depthValue, 1);
float normalizedDepthValue = (depthValue - stats.min[0]) / (stats.max[0] - stats.min[0]);
Color32f color = getColor32fFromJetColorMap(normalizedDepthValue);
tmpBuf.setpixel(x, y, color.m, 3); // set only 3 channels (RGB)
}
}
}
else if(d->fileName().contains("nmodMap"))
else if(isDepthMap || isNmodMap)
{
oiio::ImageBufAlgo::PixelStats stats;
oiio::ImageBufAlgo::computePixelStats(stats, inBuf);
// oiio::ImageBufAlgo::color_map(dst, src, srcchannel, int(knots.size()/3), 3, knots);

#pragma omp parallel for
for(int y = 0; y < inSpec.height; ++y)
Expand All @@ -230,8 +216,13 @@ bool QtOIIOHandler::read(QImage *image)
{
float depthValue = 0.0f;
inBuf.getpixel(x, y, &depthValue, 1);
float normalizedDepthValue = (depthValue - stats.min[0]) / (stats.max[0] - stats.min[0]);
Color32f color = getColor32fFromJetColorMapClamp(normalizedDepthValue);
const float range = stats.max[0] - stats.min[0];
const float normalizedDepthValue = range != 0.0f ? (depthValue - stats.min[0]) / range : 1.0f;
Color32f color;
if(isDepthMap)
color = getColor32fFromJetColorMap(normalizedDepthValue);
else if(isNmodMap)
color = getColor32fFromJetColorMapClamp(normalizedDepthValue);
tmpBuf.setpixel(x, y, color.m, 3); // set only 3 channels (RGB)
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/jetColorMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ struct Color32f

inline Color32f getColor32fFromJetColorMap(float value)
{
if(std::isnan(value))
return Color32f(1.0f, 0.0f, 1.0f);
if(value <= 0.0f)
return Color32f(0, 0, 0);
if(value >= 1.0f)
Expand All @@ -59,6 +61,8 @@ inline Color32f getColor32fFromJetColorMap(float value)

inline Color32f getColor32fFromJetColorMapClamp(float value)
{
if(std::isnan(value))
return Color32f(1.0f, 0.0f, 1.0f);
if(value < 0.0f)
value = 0.0f;
if(value > 1.0f)
Expand Down

0 comments on commit 34e983c

Please sign in to comment.