kopaka1822 / ImageViewer

HDR, PFM, DDS, KTX, EXR, PNG, JPG, BMP image viewer and manipulator
MIT License
331 stars 37 forks source link

Invalid Formula (I0-I1)^2 #15

Closed Jojendersie closed 4 years ago

Jojendersie commented 4 years ago

(abs(I0-I1))^2 looks different and more correct

With (I0-I1)^2, the negative areas become 0

kopaka1822 commented 4 years ago

This is bad... Apparently this happens because of the HLSL pow function which is defined as:

X Y Result
"< 0" any NAN
"> 0" == 0 1
"== 0" > 0 0
"== 0" < 0 inf
"> 0" < 0 1/pow(X,-Y)
"== 0" == 0 Depends on the particular graphics processor 0, or 1, or NAN

The GLSL pow function had undefined behaviour for X < 0 or X=0 and Y≤0.

The question would be how to handle the undefined behaviour inside the image viewer. My proposal would be:

X Y Result
"< 0" even pow(-X, Y)
"< 0" odd -pow(-X, Y)
"< 0" other NaN
"> 0" == 0 1
"== 0" > 0 0
"== 0" < 0 inf
"> 0" < 0 1/pow(X,-Y)
"== 0" == 0 NAN

What are your thoughts?