vibrance.c 8.3 KB
Newer Older
1 2
/*
    This file is part of darktable,
3
    copyright (c) 2011-2012 Henrik Andersson.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

    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 <assert.h>
22 23
#include <math.h>
#include <stdlib.h>
24
#include <string.h>
25

26
#include "bauhaus/bauhaus.h"
27 28
#include "common/opencl.h"
#include "control/control.h"
29 30 31 32
#include "develop/develop.h"
#include "develop/imageop.h"
#include "gui/accelerators.h"
#include "gui/gtk.h"
33
#include "iop/iop_api.h"
34 35 36 37
#include <gtk/gtk.h>
#include <inttypes.h>

// NaN-safe clip: NaN compares false and will result in 0.0
38
#define CLIP(x) (((x) >= 0.0) ? ((x) <= 1.0 ? (x) : 1.0) : 0.0)
39
DT_MODULE_INTROSPECTION(2, dt_iop_vibrance_params_t)
40 41 42 43

typedef struct dt_iop_vibrance_params_t
{
  float amount;
44
} dt_iop_vibrance_params_t;
45 46 47

typedef struct dt_iop_vibrance_gui_data_t
{
48
  GtkWidget *amount_scale;
49
} dt_iop_vibrance_gui_data_t;
50 51 52 53

typedef struct dt_iop_vibrance_data_t
{
  float amount;
54
} dt_iop_vibrance_data_t;
55

56 57 58
typedef struct dt_iop_vibrance_global_data_t
{
  int kernel_vibrance;
59
} dt_iop_vibrance_global_data_t;
60

61 62 63 64 65 66 67
const char *name()
{
  return _("vibrance");
}

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

71
int groups()
72 73 74 75
{
  return IOP_GROUP_COLOR;
}

76
#if 0 // BAUHAUS doesn't support keyaccels yet...
77 78 79 80 81 82 83 84 85 86 87 88
void init_key_accels(dt_iop_module_so_t *self)
{
  dt_accel_register_slider_iop(self, FALSE, NC_("accel", "vibrance"));
}

void connect_key_accels(dt_iop_module_t *self)
{
  dt_iop_vibrance_gui_data_t *g = (dt_iop_vibrance_gui_data_t*)self->gui_data;

  dt_accel_connect_slider_iop(self, "vibrance",
                              GTK_WIDGET(g->amount_scale));
}
89
#endif
90

91 92
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)
93 94
{
  dt_iop_vibrance_data_t *d = (dt_iop_vibrance_data_t *)piece->data;
95
  const float *in = (float *)ivoid;
96 97 98
  float *out = (float *)ovoid;
  const int ch = piece->colors;

99
  const float amount = (d->amount * 0.01);
100

101
#ifdef _OPENMP
102
#pragma omp parallel for default(none) shared(in, out) schedule(static)
103
#endif
104
  for(int k = 0; k < roi_out->height; k++)
105
  {
106 107
    size_t offs = (size_t)k * roi_out->width * ch;
    for(int l = 0; l < (roi_out->width * ch); l += ch)
108 109
    {
      /* saturation weight 0 - 1 */
110 111
      float sw = sqrt((in[offs + l + 1] * in[offs + l + 1]) + (in[offs + l + 2] * in[offs + l + 2])) / 256.0;
      float ls = 1.0 - ((amount * sw) * .25);
112
      float ss = 1.0 + (amount * sw);
113 114 115 116 117 118 119 120
      out[offs + l + 0] = in[offs + l + 0] * ls;
      out[offs + l + 1] = in[offs + l + 1] * ss;
      out[offs + l + 2] = in[offs + l + 2] * ss;
      out[offs + l + 3] = in[offs + l + 3];
    }
  }
}

121 122

#ifdef HAVE_OPENCL
123
int process_cl(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out,
124
               const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
125 126 127 128 129 130 131 132 133
{
  dt_iop_vibrance_data_t *data = (dt_iop_vibrance_data_t *)piece->data;
  dt_iop_vibrance_global_data_t *gd = (dt_iop_vibrance_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;

134
  const float amount = data->amount * 0.01f;
135

136
  size_t sizes[] = { ROUNDUPWD(width), ROUNDUPHT(height), 1 };
137 138 139 140 141

  dt_opencl_set_kernel_arg(devid, gd->kernel_vibrance, 0, sizeof(cl_mem), (void *)&dev_in);
  dt_opencl_set_kernel_arg(devid, gd->kernel_vibrance, 1, sizeof(cl_mem), (void *)&dev_out);
  dt_opencl_set_kernel_arg(devid, gd->kernel_vibrance, 2, sizeof(int), (void *)&width);
  dt_opencl_set_kernel_arg(devid, gd->kernel_vibrance, 3, sizeof(int), (void *)&height);
142
  dt_opencl_set_kernel_arg(devid, gd->kernel_vibrance, 4, sizeof(float), (void *)&amount);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  err = dt_opencl_enqueue_kernel_2d(devid, gd->kernel_vibrance, sizes);
  if(err != CL_SUCCESS) goto error;

  return TRUE;

error:
  dt_print(DT_DEBUG_OPENCL, "[opencl_vibrance] couldn't enqueue kernel! %d\n", err);
  return FALSE;
}
#endif



void init_global(dt_iop_module_so_t *module)
{
  const int program = 8; // extended.cl, from programs.conf
159 160
  dt_iop_vibrance_global_data_t *gd
      = (dt_iop_vibrance_global_data_t *)malloc(sizeof(dt_iop_vibrance_global_data_t));
161 162 163 164 165 166 167 168 169 170 171 172 173 174
  module->data = gd;
  gd->kernel_vibrance = dt_opencl_create_kernel(program, "vibrance");
}

void cleanup_global(dt_iop_module_so_t *module)
{
  dt_iop_vibrance_global_data_t *gd = (dt_iop_vibrance_global_data_t *)module->data;
  dt_opencl_free_kernel(gd->kernel_vibrance);
  free(module->data);
  module->data = NULL;
}



175
static void amount_callback(GtkWidget *slider, gpointer user_data)
176 177 178 179
{
  dt_iop_module_t *self = (dt_iop_module_t *)user_data;
  if(self->dt->gui->reset) return;
  dt_iop_vibrance_params_t *p = (dt_iop_vibrance_params_t *)self->params;
180
  p->amount = dt_bauhaus_slider_get(slider);
181 182 183
  dt_dev_add_history_item(darktable.develop, self, TRUE);
}

184 185
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)
186 187 188 189 190 191
{
  dt_iop_vibrance_params_t *p = (dt_iop_vibrance_params_t *)p1;
  dt_iop_vibrance_data_t *d = (dt_iop_vibrance_data_t *)piece->data;
  d->amount = p->amount;
}

192
void init_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
193
{
194
  piece->data = calloc(1, sizeof(dt_iop_vibrance_data_t));
195 196 197
  self->commit_params(self, self->default_params, pipe, piece);
}

198
void cleanup_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
199 200
{
  free(piece->data);
201
  piece->data = NULL;
202 203 204 205 206 207 208
}

void gui_update(struct dt_iop_module_t *self)
{
  dt_iop_module_t *module = (dt_iop_module_t *)self;
  dt_iop_vibrance_gui_data_t *g = (dt_iop_vibrance_gui_data_t *)self->gui_data;
  dt_iop_vibrance_params_t *p = (dt_iop_vibrance_params_t *)module->params;
209
  dt_bauhaus_slider_set(g->amount_scale, p->amount);
210 211 212 213
}

void init(dt_iop_module_t *module)
{
214 215
  module->params = calloc(1, sizeof(dt_iop_vibrance_params_t));
  module->default_params = calloc(1, sizeof(dt_iop_vibrance_params_t));
216
  module->default_enabled = 0;
Heiko Bauke's avatar
Heiko Bauke committed
217
  module->priority = 426; // module order created by iop_dependencies.py, do not edit!
218 219
  module->params_size = sizeof(dt_iop_vibrance_params_t);
  module->gui_data = NULL;
220
  dt_iop_vibrance_params_t tmp = (dt_iop_vibrance_params_t){ 25 };
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  memcpy(module->params, &tmp, sizeof(dt_iop_vibrance_params_t));
  memcpy(module->default_params, &tmp, sizeof(dt_iop_vibrance_params_t));
}

void cleanup(dt_iop_module_t *module)
{
  free(module->params);
  module->params = NULL;
}

void gui_init(struct dt_iop_module_t *self)
{
  self->gui_data = malloc(sizeof(dt_iop_vibrance_gui_data_t));
  dt_iop_vibrance_gui_data_t *g = (dt_iop_vibrance_gui_data_t *)self->gui_data;
  dt_iop_vibrance_params_t *p = (dt_iop_vibrance_params_t *)self->params;

237
  self->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, DT_BAUHAUS_SPACE);
238
  ;
239

240
  /* vibrance */
241 242
  g->amount_scale = dt_bauhaus_slider_new_with_range(self, 0.0, 100.0, 1, p->amount, 0);
  dt_bauhaus_slider_set_format(g->amount_scale, "%.0f%%");
243
  dt_bauhaus_widget_set_label(g->amount_scale, NULL, _("vibrance"));
244
  gtk_box_pack_start(GTK_BOX(self->widget), GTK_WIDGET(g->amount_scale), TRUE, TRUE, 0);
245
  gtk_widget_set_tooltip_text(g->amount_scale, _("the amount of vibrance"));
246
  g_signal_connect(G_OBJECT(g->amount_scale), "value-changed", G_CALLBACK(amount_callback), self);
247 248 249 250 251 252 253
}

void gui_cleanup(struct dt_iop_module_t *self)
{
  free(self->gui_data);
  self->gui_data = NULL;
}
Richard Wonka's avatar
Richard Wonka committed
254
// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.sh
255
// vim: shiftwidth=2 expandtab tabstop=2 cindent
Tobias Ellinghaus's avatar
Tobias Ellinghaus committed
256
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;