colisa.c 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
  This file is part of darktable,
  copyright (c) 2013 ulrich pegelow.

  darktable is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  darktable is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with darktable.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "bauhaus/bauhaus.h"
23 24 25
#include "common/debug.h"
#include "common/opencl.h"
#include "control/control.h"
26 27
#include "develop/develop.h"
#include "develop/imageop.h"
28
#include "develop/imageop_math.h"
29 30 31 32
#include "develop/tiling.h"
#include "gui/accelerators.h"
#include "gui/gtk.h"
#include "gui/presets.h"
33
#include "iop/iop_api.h"
34 35 36 37 38
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

39 40 41
#include <gtk/gtk.h>
#include <inttypes.h>

42
DT_MODULE_INTROSPECTION(1, dt_iop_colisa_params_t)
43 44 45 46

typedef struct dt_iop_colisa_params_t
{
  float contrast;
47
  float brightness;
48
  float saturation;
49
} dt_iop_colisa_params_t;
50 51 52 53

typedef struct dt_iop_colisa_gui_data_t
{
  GtkWidget *contrast;
54
  GtkWidget *brightness;
55
  GtkWidget *saturation;
56
} dt_iop_colisa_gui_data_t;
57 58 59 60

typedef struct dt_iop_colisa_data_t
{
  float contrast;
61
  float brightness;
62
  float saturation;
63 64 65 66 67
  float ctable[0x10000];      // precomputed look-up table for contrast curve
  float cunbounded_coeffs[3]; // approximation for extrapolation of contrast curve
  float ltable[0x10000];      // precomputed look-up table for brightness curve
  float lunbounded_coeffs[3]; // approximation for extrapolation of brightness curve
} dt_iop_colisa_data_t;
68 69 70 71

typedef struct dt_iop_colisa_global_data_t
{
  int kernel_colisa;
72
} dt_iop_colisa_global_data_t;
73 74 75 76


const char *name()
{
77
  return _("contrast brightness saturation");
78 79 80 81 82 83 84
}

int flags()
{
  return IOP_FLAGS_INCLUDE_IN_STYLES | IOP_FLAGS_SUPPORTS_BLENDING | IOP_FLAGS_ALLOW_TILING;
}

85
int groups()
86 87 88 89 90 91 92 93
{
  return IOP_GROUP_BASIC;
}


void init_key_accels(dt_iop_module_so_t *self)
{
  dt_accel_register_slider_iop(self, FALSE, NC_("accel", "contrast"));
94
  dt_accel_register_slider_iop(self, FALSE, NC_("accel", "brightness"));
95 96 97 98 99
  dt_accel_register_slider_iop(self, FALSE, NC_("accel", "saturation"));
}

void connect_key_accels(dt_iop_module_t *self)
{
100
  dt_iop_colisa_gui_data_t *g = (dt_iop_colisa_gui_data_t *)self->gui_data;
101 102

  dt_accel_connect_slider_iop(self, "contrast", GTK_WIDGET(g->contrast));
103
  dt_accel_connect_slider_iop(self, "brightness", GTK_WIDGET(g->brightness));
104 105 106 107 108
  dt_accel_connect_slider_iop(self, "saturation", GTK_WIDGET(g->saturation));
}


#ifdef HAVE_OPENCL
109
int process_cl(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out,
110
               const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
{
  dt_iop_colisa_data_t *d = (dt_iop_colisa_data_t *)piece->data;
  dt_iop_colisa_global_data_t *gd = (dt_iop_colisa_global_data_t *)self->data;

  cl_int err = -999;
  const int devid = piece->pipe->devid;

  const int width = roi_in->width;
  const int height = roi_in->height;

  const float saturation = d->saturation;

  cl_mem dev_cm = NULL;
  cl_mem dev_ccoeffs = NULL;
  cl_mem dev_lm = NULL;
  cl_mem dev_lcoeffs = NULL;

  dev_cm = dt_opencl_copy_host_to_device(devid, d->ctable, 256, 256, sizeof(float));
  if(dev_cm == NULL) goto error;
130
  dev_ccoeffs = dt_opencl_copy_host_to_device_constant(devid, sizeof(float) * 3, d->cunbounded_coeffs);
131 132 133
  if(dev_ccoeffs == NULL) goto error;
  dev_lm = dt_opencl_copy_host_to_device(devid, d->ltable, 256, 256, sizeof(float));
  if(dev_lm == NULL) goto error;
134
  dev_lcoeffs = dt_opencl_copy_host_to_device_constant(devid, sizeof(float) * 3, d->lunbounded_coeffs);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  if(dev_lcoeffs == NULL) goto error;

  size_t sizes[3];
  sizes[0] = ROUNDUPWD(width);
  sizes[1] = ROUNDUPWD(height);
  sizes[2] = 1;
  dt_opencl_set_kernel_arg(devid, gd->kernel_colisa, 0, sizeof(cl_mem), (void *)&dev_in);
  dt_opencl_set_kernel_arg(devid, gd->kernel_colisa, 1, sizeof(cl_mem), (void *)&dev_out);
  dt_opencl_set_kernel_arg(devid, gd->kernel_colisa, 2, sizeof(int), (void *)&width);
  dt_opencl_set_kernel_arg(devid, gd->kernel_colisa, 3, sizeof(int), (void *)&height);
  dt_opencl_set_kernel_arg(devid, gd->kernel_colisa, 4, sizeof(float), (void *)&saturation);
  dt_opencl_set_kernel_arg(devid, gd->kernel_colisa, 5, sizeof(cl_mem), (void *)&dev_cm);
  dt_opencl_set_kernel_arg(devid, gd->kernel_colisa, 6, sizeof(cl_mem), (void *)&dev_ccoeffs);
  dt_opencl_set_kernel_arg(devid, gd->kernel_colisa, 7, sizeof(cl_mem), (void *)&dev_lm);
  dt_opencl_set_kernel_arg(devid, gd->kernel_colisa, 8, sizeof(cl_mem), (void *)&dev_lcoeffs);

  err = dt_opencl_enqueue_kernel_2d(devid, gd->kernel_colisa, sizes);
  if(err != CL_SUCCESS) goto error;

154 155 156 157
  dt_opencl_release_mem_object(dev_lcoeffs);
  dt_opencl_release_mem_object(dev_lm);
  dt_opencl_release_mem_object(dev_ccoeffs);
  dt_opencl_release_mem_object(dev_cm);
158 159 160
  return TRUE;

error:
161 162 163 164
  dt_opencl_release_mem_object(dev_lcoeffs);
  dt_opencl_release_mem_object(dev_lm);
  dt_opencl_release_mem_object(dev_ccoeffs);
  dt_opencl_release_mem_object(dev_cm);
165 166 167 168 169
  dt_print(DT_DEBUG_OPENCL, "[opencl_colisa] couldn't enqueue kernel! %d\n", err);
  return FALSE;
}
#endif

170 171
void process(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, const void *const ivoid,
             void *const ovoid, const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
172 173
{
  dt_iop_colisa_data_t *data = (dt_iop_colisa_data_t *)piece->data;
174
  float *in = (float *)ivoid;
175 176 177 178 179 180 181
  float *out = (float *)ovoid;

  const int width = roi_in->width;
  const int height = roi_in->height;
  const int ch = piece->colors;

#ifdef _OPENMP
182
#pragma omp parallel for default(none) shared(in, out, data) schedule(static)
183
#endif
184
  for(size_t k = 0; k < (size_t)width * height; k++)
185
  {
186 187 188 189 190 191 192 193
    float L = (in[k * ch + 0] < 100.0f)
                  ? data->ctable[CLAMP((int)(in[k * ch + 0] / 100.0f * 0x10000ul), 0, 0xffff)]
                  : dt_iop_eval_exp(data->cunbounded_coeffs, in[k * ch + 0] / 100.0f);
    out[k * ch + 0] = (L < 100.0f) ? data->ltable[CLAMP((int)(L / 100.0f * 0x10000ul), 0, 0xffff)]
                                   : dt_iop_eval_exp(data->lunbounded_coeffs, L / 100.0f);
    out[k * ch + 1] = in[k * ch + 1] * data->saturation;
    out[k * ch + 2] = in[k * ch + 2] * data->saturation;
    out[k * ch + 3] = in[k * ch + 3];
194 195 196 197
  }
}


198
static void contrast_callback(GtkWidget *slider, gpointer user_data)
199 200 201 202 203 204 205 206
{
  dt_iop_module_t *self = (dt_iop_module_t *)user_data;
  if(self->dt->gui->reset) return;
  dt_iop_colisa_params_t *p = (dt_iop_colisa_params_t *)self->params;
  p->contrast = dt_bauhaus_slider_get(slider);
  dt_dev_add_history_item(darktable.develop, self, TRUE);
}

207
static void brightness_callback(GtkWidget *slider, gpointer user_data)
208 209 210 211
{
  dt_iop_module_t *self = (dt_iop_module_t *)user_data;
  if(self->dt->gui->reset) return;
  dt_iop_colisa_params_t *p = (dt_iop_colisa_params_t *)self->params;
212
  p->brightness = dt_bauhaus_slider_get(slider);
213 214 215
  dt_dev_add_history_item(darktable.develop, self, TRUE);
}

216
static void saturation_callback(GtkWidget *slider, gpointer user_data)
217 218 219 220 221 222 223 224 225
{
  dt_iop_module_t *self = (dt_iop_module_t *)user_data;
  if(self->dt->gui->reset) return;
  dt_iop_colisa_params_t *p = (dt_iop_colisa_params_t *)self->params;
  p->saturation = dt_bauhaus_slider_get(slider);
  dt_dev_add_history_item(darktable.develop, self, TRUE);
}


226 227
void commit_params(struct dt_iop_module_t *self, dt_iop_params_t *p1, dt_dev_pixelpipe_t *pipe,
                   dt_dev_pixelpipe_iop_t *piece)
228 229 230 231
{
  dt_iop_colisa_params_t *p = (dt_iop_colisa_params_t *)p1;
  dt_iop_colisa_data_t *d = (dt_iop_colisa_data_t *)piece->data;

232 233 234
  d->contrast = p->contrast + 1.0f; // rescale from [-1;+1] to [0;+2] (zero meaning no contrast -> gray plane)
  d->brightness = p->brightness * 2.0f; // rescale from [-1;+1] to [-2;+2]
  d->saturation = p->saturation + 1.0f; // rescale from [-1;+1] to [0;+2] (zero meaning no saturation -> b&w)
235 236 237 238

  // generate precomputed contrast curve
  if(d->contrast <= 1.0f)
  {
239
// linear curve for d->contrast below 1
240
#ifdef _OPENMP
241
#pragma omp parallel for default(none) shared(d) schedule(static)
242
#endif
243
    for(int k = 0; k < 0x10000; k++) d->ctable[k] = d->contrast * (100.0f * k / 0x10000 - 50.0f) + 50.0f;
244 245 246 247 248
  }
  else
  {
    // sigmoidal curve for d->contrast above 1
    const float boost = 20.0f;
249
    const float contrastm1sq = boost * (d->contrast - 1.0f) * (d->contrast - 1.0f);
250 251
    const float contrastscale = sqrt(1.0f + contrastm1sq);
#ifdef _OPENMP
252
#pragma omp parallel for default(none) shared(d) schedule(static)
253
#endif
254
    for(int k = 0; k < 0x10000; k++)
255
    {
256
      float kx2m1 = 2.0f * (float)k / 0x10000 - 1.0f;
257 258 259 260 261
      d->ctable[k] = 50.0f * (contrastscale * kx2m1 / sqrtf(1.0f + contrastm1sq * kx2m1 * kx2m1) + 1.0f);
    }
  }

  // now the extrapolation stuff for the contrast curve:
262 263 264 265 266
  const float xc[4] = { 0.7f, 0.8f, 0.9f, 1.0f };
  const float yc[4] = { d->ctable[CLAMP((int)(xc[0] * 0x10000ul), 0, 0xffff)],
                        d->ctable[CLAMP((int)(xc[1] * 0x10000ul), 0, 0xffff)],
                        d->ctable[CLAMP((int)(xc[2] * 0x10000ul), 0, 0xffff)],
                        d->ctable[CLAMP((int)(xc[3] * 0x10000ul), 0, 0xffff)] };
267 268 269
  dt_iop_estimate_exp(xc, yc, 4, d->cunbounded_coeffs);


270
  // generate precomputed brightness curve
271
  const float gamma = (d->brightness >= 0.0f) ? 1.0f / (1.0f + d->brightness) : (1.0f - d->brightness);
272 273

#ifdef _OPENMP
274
#pragma omp parallel for default(none) shared(d) schedule(static)
275
#endif
276
  for(int k = 0; k < 0x10000; k++)
277
  {
278
    d->ltable[k] = 100.0f * powf((float)k / 0x10000, gamma);
279 280
  }

281
  // now the extrapolation stuff for the brightness curve:
282 283 284 285 286
  const float xl[4] = { 0.7f, 0.8f, 0.9f, 1.0f };
  const float yl[4] = { d->ltable[CLAMP((int)(xl[0] * 0x10000ul), 0, 0xffff)],
                        d->ltable[CLAMP((int)(xl[1] * 0x10000ul), 0, 0xffff)],
                        d->ltable[CLAMP((int)(xl[2] * 0x10000ul), 0, 0xffff)],
                        d->ltable[CLAMP((int)(xl[3] * 0x10000ul), 0, 0xffff)] };
287 288 289
  dt_iop_estimate_exp(xl, yl, 4, d->lunbounded_coeffs);
}

290
void init_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
291
{
292
  dt_iop_colisa_data_t *d = (dt_iop_colisa_data_t *)calloc(1, sizeof(dt_iop_colisa_data_t));
293
  piece->data = (void *)d;
294
  self->commit_params(self, self->default_params, pipe, piece);
295
  for(int k = 0; k < 0x10000; k++) d->ctable[k] = d->ltable[k] = 100.0f * k / 0x10000; // identity
296 297
}

298
void cleanup_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
299 300
{
  free(piece->data);
301
  piece->data = NULL;
302 303
}

304
void gui_update(struct dt_iop_module_t *self)
305 306 307 308 309
{
  dt_iop_module_t *module = (dt_iop_module_t *)self;
  dt_iop_colisa_gui_data_t *g = (dt_iop_colisa_gui_data_t *)self->gui_data;
  dt_iop_colisa_params_t *p = (dt_iop_colisa_params_t *)module->params;
  dt_bauhaus_slider_set(g->contrast, p->contrast);
310
  dt_bauhaus_slider_set(g->brightness, p->brightness);
311 312 313
  dt_bauhaus_slider_set(g->saturation, p->saturation);
}

314
void init(dt_iop_module_t *module)
315
{
316 317
  module->params = calloc(1, sizeof(dt_iop_colisa_params_t));
  module->default_params = calloc(1, sizeof(dt_iop_colisa_params_t));
318
  module->default_enabled = 0;
Heiko Bauke's avatar
Heiko Bauke committed
319
  module->priority = 647; // module order created by iop_dependencies.py, do not edit!
320 321
  module->params_size = sizeof(dt_iop_colisa_params_t);
  module->gui_data = NULL;
322
  dt_iop_colisa_params_t tmp = (dt_iop_colisa_params_t){ 0, 0, 0 };
323 324 325 326
  memcpy(module->params, &tmp, sizeof(dt_iop_colisa_params_t));
  memcpy(module->default_params, &tmp, sizeof(dt_iop_colisa_params_t));
}

327
void init_global(dt_iop_module_so_t *module)
328 329
{
  const int program = 2; // basic.cl, from programs.conf
330 331
  dt_iop_colisa_global_data_t *gd
      = (dt_iop_colisa_global_data_t *)malloc(sizeof(dt_iop_colisa_global_data_t));
332 333 334 335 336
  module->data = gd;
  gd->kernel_colisa = dt_opencl_create_kernel(program, "colisa");
}


337
void cleanup(dt_iop_module_t *module)
338 339 340 341 342
{
  free(module->params);
  module->params = NULL;
}

343
void cleanup_global(dt_iop_module_so_t *module)
344 345 346 347 348 349 350 351
{
  dt_iop_colisa_global_data_t *gd = (dt_iop_colisa_global_data_t *)module->data;
  dt_opencl_free_kernel(gd->kernel_colisa);
  free(module->data);
  module->data = NULL;
}


352
void gui_init(struct dt_iop_module_t *self)
353 354 355 356 357
{
  self->gui_data = malloc(sizeof(dt_iop_colisa_gui_data_t));
  dt_iop_colisa_gui_data_t *g = (dt_iop_colisa_gui_data_t *)self->gui_data;
  dt_iop_colisa_params_t *p = (dt_iop_colisa_params_t *)self->params;

358
  self->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, DT_BAUHAUS_SPACE);
359

360 361 362
  g->contrast = dt_bauhaus_slider_new_with_range(self, -1.0, 1.0, 0.01, p->contrast, 2);
  g->brightness = dt_bauhaus_slider_new_with_range(self, -1.0, 1.0, 0.01, p->brightness, 2);
  g->saturation = dt_bauhaus_slider_new_with_range(self, -1.0, 1.0, 0.01, p->saturation, 2);
363

364 365 366
  dt_bauhaus_widget_set_label(g->contrast, NULL, _("contrast"));
  dt_bauhaus_widget_set_label(g->brightness, NULL, _("brightness"));
  dt_bauhaus_widget_set_label(g->saturation, NULL, _("saturation"));
367 368

  gtk_box_pack_start(GTK_BOX(self->widget), g->contrast, TRUE, TRUE, 0);
369
  gtk_box_pack_start(GTK_BOX(self->widget), g->brightness, TRUE, TRUE, 0);
370 371
  gtk_box_pack_start(GTK_BOX(self->widget), g->saturation, TRUE, TRUE, 0);

372 373 374
  gtk_widget_set_tooltip_text(g->contrast, _("contrast adjustment"));
  gtk_widget_set_tooltip_text(g->brightness, _("brightness adjustment"));
  gtk_widget_set_tooltip_text(g->saturation, _("color saturation adjustment"));
375

376 377 378
  g_signal_connect(G_OBJECT(g->contrast), "value-changed", G_CALLBACK(contrast_callback), self);
  g_signal_connect(G_OBJECT(g->brightness), "value-changed", G_CALLBACK(brightness_callback), self);
  g_signal_connect(G_OBJECT(g->saturation), "value-changed", G_CALLBACK(saturation_callback), self);
379 380
}

381
void gui_cleanup(struct dt_iop_module_t *self)
382 383 384 385 386 387 388
{
  free(self->gui_data);
  self->gui_data = NULL;
}

// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
// vim: shiftwidth=2 expandtab tabstop=2 cindent
Tobias Ellinghaus's avatar
Tobias Ellinghaus committed
389
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;