1/*
2    This file is part of darktable,
3    copyright (c) 2019 edgardo hoszowski.
4
5    darktable is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    darktable is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with darktable.  If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include "color_conversion.h"
20#include "rgb_norms.h"
21
22kernel void
23rgbcurve(read_only image2d_t in, write_only image2d_t out, const int width, const int height,
24           read_only image2d_t table_r, read_only image2d_t table_g, read_only image2d_t table_b,
25           constant float *coeffs_r, constant float *coeffs_g, constant float *coeffs_b,
26           const int autoscale, const int preserve_colors,
27           constant dt_colorspaces_iccprofile_info_cl_t *profile_info, read_only image2d_t lut,
28           const int use_work_profile)
29{
30  const int x = get_global_id(0);
31  const int y = get_global_id(1);
32
33  if(x >= width || y >= height) return;
34
35  float4 pixel = read_imagef(in, sampleri, (int2)(x, y));
36
37  if(autoscale == 1) // DT_S_SCALE_MANUAL_RGB
38  {
39    pixel.x = lookup_unbounded(table_r, pixel.x, coeffs_r);
40    pixel.y = lookup_unbounded(table_g, pixel.y, coeffs_g);
41    pixel.z = lookup_unbounded(table_b, pixel.z, coeffs_b);
42  }
43  else if(autoscale == 0) // DT_S_SCALE_AUTOMATIC_RGB
44  {
45    if (preserve_colors == DT_RGB_NORM_NONE)
46    {
47      pixel.x = lookup_unbounded(table_r, pixel.x, coeffs_r);
48      pixel.y = lookup_unbounded(table_r, pixel.y, coeffs_r);
49      pixel.z = lookup_unbounded(table_r, pixel.z, coeffs_r);
50    }
51    else
52    {
53      float ratio = 1.f;
54      const float lum = dt_rgb_norm(pixel, preserve_colors, use_work_profile, profile_info, lut);
55      if(lum > 0.f)
56      {
57        const float curve_lum = lookup_unbounded(table_r, lum, coeffs_r);
58        ratio = curve_lum / lum;
59      }
60      pixel.xyz *= ratio;
61    }
62  }
63
64  write_imagef(out, (int2)(x, y), pixel);
65}
66