Skip to content

Commit

Permalink
Increase LUT_ELEM to 512
Browse files Browse the repository at this point in the history
Subtly improved precision for lookup_gamut function due to larger table.
Also, as we use LUT_ELEM to be as 2^n we can redefine lookup_gamut
- removing the restriction of hue input angle
- improved performance
  • Loading branch information
jenshannoschwalm committed Sep 7, 2024
1 parent 51b4ca3 commit 843beed
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
16 changes: 5 additions & 11 deletions data/kernels/colorspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -965,24 +965,18 @@ static inline float soft_clip(const float x, const float soft_threshold, const f

static inline float lookup_gamut(global const float *gamut_lut, const float x)
{
// WARNING : x should be between [-pi ; pi ], which is the default output of atan2 anyway

// Linearly interpolate the value of the gamut LUT at the hue angle in radians.
// convert in LUT coordinate
const float x_test = (LUT_ELEM - 1) * (x + M_PI_F) / (2.f * M_PI_F);

// find the 2 closest integer coordinates (next/previous)
float x_prev = floor(x_test);
float x_next = ceil(x_test);
const float x_prev = floor(x_test);
const float x_next = ceil(x_test);

// get the 2 closest LUT elements at integer coordinates
// cycle on the hue ring if out of bounds
int xi = (int)x_prev;
if(xi < 0) xi = LUT_ELEM - 1;
else if(xi > LUT_ELEM - 1) xi = 0;

int xii = (int)x_next;
if(xii < 0) xii = LUT_ELEM - 1;
else if(xii > LUT_ELEM - 1) xii = 0;
const int xi = ((int)x_prev) & (LUT_ELEM - 1);
const int xii = ((int)x_next) & (LUT_ELEM - 1);

// fetch the corresponding y values
const float y_prev = gamut_lut[xi];
Expand Down
2 changes: 1 addition & 1 deletion data/kernels/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ constant sampler_t samplerA = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_NONE
#define M_PI_F 3.14159265358979323846 // should be defined by the OpenCL compiler acc. to standard
#endif

#define LUT_ELEM 360 // gamut LUT number of elements:
#define LUT_ELEM 512 // gamut LUT number of elements:

#define RED 0
#define GREEN 1
Expand Down
17 changes: 5 additions & 12 deletions src/common/darktable_ucs_22_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,14 @@ static inline float lookup_gamut(const float gamut_lut[LUT_ELEM], const float hu
// convert hue in LUT index coordinate
const float x_test = (float)LUT_ELEM * (hue + M_PI_F) / (2.f * M_PI_F);

// find the 2 closest integer coordinates (next/previous)
float x_prev = floorf(x_test);
float x_next = ceilf(x_test);
const float x_prev = floor(x_test);
const float x_next = ceil(x_test);

// get the 2 closest LUT elements at integer coordinates
// cycle on the hue ring if out of bounds
int xi = (int)x_prev;
if(xi < 0) xi = LUT_ELEM - 1;
else if(xi > LUT_ELEM - 1) xi = 0;

int xii = (int)x_next;
if(xii < 0) xii = LUT_ELEM - 1;
else if(xii > LUT_ELEM - 1) xii = 0;

// fetch the corresponding y values
const int xi = (int)x_prev & (LUT_ELEM - 1);
const int xii = (int)x_next & (LUT_ELEM - 1);
// fetch the corresponding y values
const float y_prev = gamut_lut[xi];

// return y_prev if we are on the same integer LUT element or do linear interpolation
Expand Down
2 changes: 1 addition & 1 deletion src/common/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <stdint.h>
#include "common/sse.h" // also includes darktable.h

#define LUT_ELEM 360 // gamut LUT number of elements:
#define LUT_ELEM 512 // gamut LUT number of elements:

#define NORM_MIN 1.52587890625e-05f // norm can't be < to 2^(-16)

Expand Down

0 comments on commit 843beed

Please sign in to comment.