Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Math] Fix NaN from powf() domain error. #2067

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/OpenColorIO/ops/gamma/GammaOpCPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#include "SSE.h"


// powf() throws a domain error if the base is negative and the exponent non-integer.
// This is a safer call to powf() that avoids a negative base.
#define POW_NON_NEG(B, E) \
std::pow((B) < 0.0f ? 0.0f : (B), (E))

namespace OCIO_NAMESPACE
{

Expand Down Expand Up @@ -540,10 +545,10 @@ void GammaMoncurveOpCPUFwd::apply(const void * inImg, void * outImg, long numPix
{
const float pixel[4] = { in[0], in[1], in[2], in[3] };

const float data[4] = { std::pow(pixel[0] * red[0] + red[1], red[2]),
std::pow(pixel[1] * grn[0] + grn[1], grn[2]),
std::pow(pixel[2] * blu[0] + blu[1], blu[2]),
std::pow(pixel[3] * alp[0] + alp[1], alp[2]) };
const float data[4] = { POW_NON_NEG(pixel[0] * red[0] + red[1], red[2]),
POW_NON_NEG(pixel[1] * grn[0] + grn[1], grn[2]),
POW_NON_NEG(pixel[2] * blu[0] + blu[1], blu[2]),
POW_NON_NEG(pixel[3] * alp[0] + alp[1], alp[2]) };

out[0] = pixel[0]<=red[3] ? pixel[0] * red[4] : data[0];
out[1] = pixel[1]<=grn[3] ? pixel[1] * grn[4] : data[1];
Expand Down Expand Up @@ -629,10 +634,10 @@ void GammaMoncurveOpCPURev::apply(const void * inImg, void * outImg, long numPix
{
const float pixel[4] = { in[0], in[1], in[2], in[3] };

const float data[4] = { std::pow(pixel[0], red[0]) * red[1] - red[2],
std::pow(pixel[1], grn[0]) * grn[1] - grn[2],
std::pow(pixel[2], blu[0]) * blu[1] - blu[2],
std::pow(pixel[3], alp[0]) * alp[1] - alp[2] };
const float data[4] = { POW_NON_NEG(pixel[0], red[0]) * red[1] - red[2],
POW_NON_NEG(pixel[1], grn[0]) * grn[1] - grn[2],
POW_NON_NEG(pixel[2], blu[0]) * blu[1] - blu[2],
POW_NON_NEG(pixel[3], alp[0]) * alp[1] - alp[2] };

out[0] = pixel[0]<=red[3] ? pixel[0] * red[4] : data[0];
out[1] = pixel[1]<=grn[3] ? pixel[1] * grn[4] : data[1];
Expand Down
Loading