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

Divide by zero at multiple locations #3

Open
beldenfox opened this issue Mar 25, 2020 · 0 comments
Open

Divide by zero at multiple locations #3

beldenfox opened this issue Mar 25, 2020 · 0 comments

Comments

@beldenfox
Copy link

I ran into divide-by-zero errors at four points in the code that led to NaN or Inf propagating through the conversion pipeline. I've come up with fixes for these based on inspecting the Objective-C port but I can only debug my changes using a combination of Objective-C and Metal. I'm assuming these fixes will also work for GLSL but can't be 100% sure. These changes only apply to the HSLuv code, I think there are similar problems with the hpluvToLch and lchToHpluv.

vec3 xyzToLuv(vec3 tuple){
    float X = tuple.x;
    float Y = tuple.y;
    // float Z = tuple.z;

    float L = hsluv_yToL(Y);

    float denom = dot(tuple,makeVec3(1,15,3));
    if (denom == 0.0)
        return vec3(0);
    float div = 1./denom;

    return makeVec3(
        1.,
        (52. * (X*div) - 2.57179),
        (117.* (Y*div) - 6.08816)
    ) * L;
}


vec3 luvToXyz(vec3 tuple) {
    float L = tuple.x;
    if (L == 0.0)
        return vec3(0.0);
    float U = tuple.y / (13.0 * L) + 0.19783000664283681;
    float V = tuple.z / (13.0 * L) + 0.468319994938791;

    float Y = hsluv_lToY(L);
    float X = 2.25 * U * Y / V;
    float Z = (3./V - 5.)*Y - (X/3.);

    return makeVec3(X, Y, Z);
}

vec3 hsluvToLch(vec3 tuple) {
    tuple.g = (tuple.b < 0.00001 || tuple.b > 99.99999 ? 0.0 : tuple.g * hsluv_maxChromaForLH(tuple.b, tuple.r) * .01);
    return tuple.bgr;
}

vec3 lchToHsluv(vec3 tuple) {
    tuple.g = (tuple.r < 0.00001 || tuple.r > 99.99999 ? 0.0 : tuple.g / hsluv_maxChromaForLH(tuple.r, tuple.b) * 100.0);
    return tuple.bgr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant