Skip to content

Commit

Permalink
Fix CE OpenCL scharr filter
Browse files Browse the repository at this point in the history
1. Fixed a bug in the saturation gradient scharr filter that resulted in the filter being non-effective because of
   generating wrong data.
2. Some perf improvement for mask visualized output
  • Loading branch information
jenshannoschwalm authored and TurboGit committed Sep 7, 2024
1 parent 2c8ad5e commit 269d5a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
40 changes: 24 additions & 16 deletions data/kernels/colorequal.cl
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ static inline float scharr_gradient(__read_only image2d_t in,
const int x,
const int y)
{
const float gx = 47.0f / 255.0f * (read_imagef(in, samplerA, (int2)(y-1, x-1)).x - read_imagef(in, samplerA, (int2)(y-1, x+1)).x
+ read_imagef(in, samplerA, (int2)(y+1, x-1)).x - read_imagef(in, samplerA, (int2)(y+1, x+1)).x)
+ 162.0f / 255.0f * (read_imagef(in, samplerA, (int2)(y, x-1)).x - read_imagef(in, samplerA, (int2)(y, x+1)).x);
const float gy = 47.0f / 255.0f * (read_imagef(in, samplerA, (int2)(y-1, x-1)).x - read_imagef(in, samplerA, (int2)(y+1, x-1)).x
+ read_imagef(in, samplerA, (int2)(y-1, x+1)).x - read_imagef(in, samplerA, (int2)(y+1, x+1)).x)
+ 162.0f / 255.0f * (read_imagef(in, samplerA, (int2)(y-1, x)).x - read_imagef(in, samplerA, (int2)(y+1, x)).x);
const float gx = 47.0f / 255.0f * (read_imagef(in, samplerA, (int2)(x-1, y-1)).x - read_imagef(in, samplerA, (int2)(x+1, y-1)).x
+ read_imagef(in, samplerA, (int2)(x-1, y+1)).x - read_imagef(in, samplerA, (int2)(x+1, y+1)).x)
+ 162.0f / 255.0f * (read_imagef(in, samplerA, (int2)(x-1, y)).x - read_imagef(in, samplerA, (int2)(x+1, y)).x);
const float gy = 47.0f / 255.0f * (read_imagef(in, samplerA, (int2)(x-1, x-1)).x - read_imagef(in, samplerA, (int2)(x-1, y+1)).x
+ read_imagef(in, samplerA, (int2)(x+1, y-1)).x - read_imagef(in, samplerA, (int2)(x+1, y+1)).x)
+ 162.0f / 255.0f * (read_imagef(in, samplerA, (int2)(x, y-1)).x - read_imagef(in, samplerA, (int2)(x, y+1)).x);

return dt_fast_hypot(gx, gy);
}
Expand Down Expand Up @@ -430,11 +430,15 @@ __kernel void write_visual(__write_only image2d_t out,
fmax(0.0f, neg ? val : val - corr),
0.0f };

const float gv = 1.0f - read_imagef(scharr, samplerA, (int2)(col, row)).x;
if(mode == BRIGHTNESS && gv > 0.2f)

if(mode == BRIGHTNESS)
{
pout.x = pout.z = 0.0f;
pout.y = gv;
const float gv = 1.0f - read_imagef(scharr, samplerA, (int2)(col, row)).x;
if(gv > 0.2f)
{
pout.x = pout.z = 0.0f;
pout.y = gv;
}
}
write_imagef(out, (int2)(col, row), pout);
}
Expand All @@ -452,13 +456,17 @@ __kernel void draw_weight(__write_only image2d_t out,
if(col >= width || y > 0) return;

const float eps = 0.5f / (float)height;
const float weight = _get_satweight((float)col / (float)width
- (mode == SATURATION_GRAD ? sat_shift : bright_shift), weights);
if(weight > eps && weight < 1.0f - eps)
const float shift = (mode == SATURATION_GRAD) ? sat_shift : bright_shift;
const float4 mark = { 0.0f, 1.0f, 0.0f, 0.0f};
for(int i = 0; i < 16; i++)
{
const int row = (int)((1.0f - weight) * (float)(height-1));
const float4 mark = { 0.0f, 1.0f, 0.0f, 0.0f};
write_imagef(out, (int2)(col/16, row), mark);
const float pos = (float)(16*col +i) / (float)(16*width);
const float weight = _get_satweight(pos - shift, weights);
if(weight > eps && weight < 1.0f - eps)
{
const int row = (int)((1.0f - weight) * (float)(height-1));
write_imagef(out, (int2)(col, row), mark);
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/iop/colorequal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ void process(struct dt_iop_module_t *self,
pix_out[2] = MAX(0.0f, neg ? val : val - corr);

const float gv = 1.0f - tmp[k];
if(mode == BRIGHTNESS && d->use_filter && gv > 0.2f)
if(mode == BRIGHTNESS && gv > 0.2f)
{
pix_out[0] = pix_out[2] = 0.0f;
pix_out[1] = gv;
Expand Down Expand Up @@ -1656,7 +1656,7 @@ int process_cl(struct dt_iop_module_t *self,
CLARG(owidth), CLARG(oheight));
if(err != CL_SUCCESS) goto error;
}
else if(guiding)
else
{
piece->pipe->mask_display = DT_DEV_PIXELPIPE_DISPLAY_PASSTHRU;
const int mode = mask_mode - 1;
Expand All @@ -1669,9 +1669,8 @@ int process_cl(struct dt_iop_module_t *self,

if(mode == BRIGHTNESS_GRAD || mode == SATURATION_GRAD)
{
const int colums = owidth*16;
err = dt_opencl_enqueue_kernel_2d_args(devid, gd->ce_draw_weight, owidth * 16, 1,
CLARG(dev_out), CLARG(weight), CLARG(bright_shift), CLARG(sat_shift), CLARG(mode), CLARG(colums), CLARG(oheight));
err = dt_opencl_enqueue_kernel_2d_args(devid, gd->ce_draw_weight, owidth, 1,
CLARG(dev_out), CLARG(weight), CLARG(bright_shift), CLARG(sat_shift), CLARG(mode), CLARG(owidth), CLARG(oheight));
}
}

Expand Down

0 comments on commit 269d5a3

Please sign in to comment.