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

Commit 34e983c

Browse files
authored
Merge pull request #11 from alicevision/fix/handleNaN
Handle NaN in images when using jetColorMap
2 parents e9798a7 + d1ad152 commit 34e983c

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

src/depthMapEntity/DepthMapEntity.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,8 @@ void DepthMapEntity::loadDepthMap()
328328
}
329329
else
330330
{
331-
float normalizedDepthValue = (depthValue - stats.min[0]) / (stats.max[0] - stats.min[0]);
331+
const float range = stats.max[0] - stats.min[0];
332+
float normalizedDepthValue = range != 0.0f ? (depthValue - stats.min[0]) / range : 1.0f;
332333
Color32f color = getColor32fFromJetColorMapClamp(normalizedDepthValue);
333334
colors.push_back(color);
334335
}

src/imageIOHandler/QtOIIOHandler.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -194,34 +194,20 @@ bool QtOIIOHandler::read(QImage *image)
194194
// perceptually uniform: "inferno", "viridis", "magma", "plasma" -- others: "blue-red", "spectrum", "heat"
195195
const char* colorMapEnv = std::getenv("QTOIIO_COLORMAP");
196196
const std::string colorMapType = colorMapEnv ? colorMapEnv : "plasma";
197+
198+
// detect AliceVision special files that require a jetColorMap based conversion
199+
const bool isDepthMap = d->fileName().contains("depthMap");
200+
const bool isNmodMap = d->fileName().contains("nmodMap");
201+
197202
if(colorMapEnv)
198203
{
199204
qDebug() << "[QtOIIO] compute colormap \"" << colorMapType.c_str() << "\"";
200205
oiio::ImageBufAlgo::color_map(tmpBuf, inBuf, 0, colorMapType);
201206
}
202-
else if(d->fileName().contains("depthMap"))
203-
{
204-
oiio::ImageBufAlgo::PixelStats stats;
205-
oiio::ImageBufAlgo::computePixelStats(stats, inBuf);
206-
207-
#pragma omp parallel for
208-
for(int y = 0; y < inSpec.height; ++y)
209-
{
210-
for(int x = 0; x < inSpec.width; ++x)
211-
{
212-
float depthValue = 0.0f;
213-
inBuf.getpixel(x, y, &depthValue, 1);
214-
float normalizedDepthValue = (depthValue - stats.min[0]) / (stats.max[0] - stats.min[0]);
215-
Color32f color = getColor32fFromJetColorMap(normalizedDepthValue);
216-
tmpBuf.setpixel(x, y, color.m, 3); // set only 3 channels (RGB)
217-
}
218-
}
219-
}
220-
else if(d->fileName().contains("nmodMap"))
207+
else if(isDepthMap || isNmodMap)
221208
{
222209
oiio::ImageBufAlgo::PixelStats stats;
223210
oiio::ImageBufAlgo::computePixelStats(stats, inBuf);
224-
// oiio::ImageBufAlgo::color_map(dst, src, srcchannel, int(knots.size()/3), 3, knots);
225211

226212
#pragma omp parallel for
227213
for(int y = 0; y < inSpec.height; ++y)
@@ -230,8 +216,13 @@ bool QtOIIOHandler::read(QImage *image)
230216
{
231217
float depthValue = 0.0f;
232218
inBuf.getpixel(x, y, &depthValue, 1);
233-
float normalizedDepthValue = (depthValue - stats.min[0]) / (stats.max[0] - stats.min[0]);
234-
Color32f color = getColor32fFromJetColorMapClamp(normalizedDepthValue);
219+
const float range = stats.max[0] - stats.min[0];
220+
const float normalizedDepthValue = range != 0.0f ? (depthValue - stats.min[0]) / range : 1.0f;
221+
Color32f color;
222+
if(isDepthMap)
223+
color = getColor32fFromJetColorMap(normalizedDepthValue);
224+
else if(isNmodMap)
225+
color = getColor32fFromJetColorMapClamp(normalizedDepthValue);
235226
tmpBuf.setpixel(x, y, color.m, 3); // set only 3 channels (RGB)
236227
}
237228
}

src/jetColorMap.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ struct Color32f
4141

4242
inline Color32f getColor32fFromJetColorMap(float value)
4343
{
44+
if(std::isnan(value))
45+
return Color32f(1.0f, 0.0f, 1.0f);
4446
if(value <= 0.0f)
4547
return Color32f(0, 0, 0);
4648
if(value >= 1.0f)
@@ -59,6 +61,8 @@ inline Color32f getColor32fFromJetColorMap(float value)
5961

6062
inline Color32f getColor32fFromJetColorMapClamp(float value)
6163
{
64+
if(std::isnan(value))
65+
return Color32f(1.0f, 0.0f, 1.0f);
6266
if(value < 0.0f)
6367
value = 0.0f;
6468
if(value > 1.0f)

0 commit comments

Comments
 (0)