tonemap.cc 9.98 KB
Newer Older
1 2
/*
    This file is part of darktable,
3
    copyright (c) 2009--2010 Thierry Leconte.
4
    copyright (c) 2012 Henrik Andersson.
5 6 7 8 9 10 11 12 13 14 15 16 17 18

    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/>.
*/
19 20 21 22 23

//
// A tonemapping module using Durand's process :
// <http://graphics.lcs.mit.edu/~fredo/PUBLI/Siggraph2002/>
//
24 25
// Use andrew adams et al.'s permutohedral lattice, for fast bilateral filtering
// See Permutohedral.h
26 27
//

28 29
#define __STDC_FORMAT_MACROS

30
extern "C" {
31

32
#ifdef HAVE_CONFIG_H
33
#include "config.h"
34 35
#endif
#include <assert.h>
36 37
#include <math.h>
#include <stdlib.h>
38
#include <string.h>
39

40
#include "bauhaus/bauhaus.h"
41
#include "control/control.h"
42 43
#include "develop/develop.h"
#include "develop/imageop.h"
44
#include "gui/accelerators.h"
45
#include "gui/gtk.h"
46
#include "iop/iop_api.h"
47 48
#include <gtk/gtk.h>
#include <inttypes.h>
49
}
50

51 52
#include "iop/Permutohedral.h"

53 54
extern "C" {
DT_MODULE_INTROSPECTION(1, dt_iop_tonemapping_params_t)
55

56 57 58 59
typedef struct dt_iop_tonemapping_params_t
{
  float contrast, Fsize;
} dt_iop_tonemapping_params_t;
60

61 62 63 64
typedef struct dt_iop_tonemapping_gui_data_t
{
  GtkWidget *contrast, *Fsize;
} dt_iop_tonemapping_gui_data_t;
65

66 67 68 69
typedef struct dt_iop_tonemapping_data_t
{
  float contrast, Fsize;
} dt_iop_tonemapping_data_t;
70

71 72 73 74
const char *name()
{
  return _("tone mapping");
}
75 76


77 78 79 80
int groups()
{
  return IOP_GROUP_TONE;
}
81

82 83 84 85
int flags()
{
  return IOP_FLAGS_SUPPORTS_BLENDING;
}
86

87 88 89 90 91
void init_key_accels(dt_iop_module_so_t *self)
{
  dt_accel_register_slider_iop(self, FALSE, NC_("accel", "contrast compression"));
  dt_accel_register_slider_iop(self, FALSE, NC_("accel", "spatial extent"));
}
92

93 94 95
void connect_key_accels(dt_iop_module_t *self)
{
  dt_iop_tonemapping_gui_data_t *g = (dt_iop_tonemapping_gui_data_t *)self->gui_data;
96

97 98 99
  dt_accel_connect_slider_iop(self, "contrast compression", GTK_WIDGET(g->contrast));
  dt_accel_connect_slider_iop(self, "spatial extent", GTK_WIDGET(g->Fsize));
}
100

101 102
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)
103 104 105
{
  dt_iop_tonemapping_data_t *data = (dt_iop_tonemapping_data_t *)piece->data;
  const int ch = piece->colors;
106

107 108 109
  int width, height;
  float inv_sigma_s;
  const float inv_sigma_r = 1.0 / 0.4;
110

111 112 113 114 115
  width = roi_in->width;
  height = roi_in->height;
  const size_t size = (size_t)width * height;
  const float iw = piece->buf_in.width * roi_out->scale;
  const float ih = piece->buf_in.height * roi_out->scale;
116

117 118 119
  inv_sigma_s = (data->Fsize / 100.0) * fminf(iw, ih);
  if(inv_sigma_s < 3.0) inv_sigma_s = 3.0;
  inv_sigma_s = 1.0 / inv_sigma_s;
120

121
  PermutohedralLattice<3, 2> lattice(size, omp_get_max_threads());
122

123 124
// Build I=log(L)
// and splat into the lattice
125
#ifdef _OPENMP
126
#pragma omp parallel for shared(lattice)
127
#endif
128 129 130 131 132 133
  for(int j = 0; j < height; j++)
  {
    size_t index = (size_t)j * width;
    const int thread = omp_get_thread_num();
    const float *in = (const float *)ivoid + (size_t)j * width * ch;
    for(int i = 0; i < width; i++, index++, in += ch)
134
    {
135 136 137 138 139 140
      float L = 0.2126 * in[0] + 0.7152 * in[1] + 0.0722 * in[2];
      if(L <= 0.0) L = 1e-6;
      L = logf(L);
      float pos[3] = { i * inv_sigma_s, j * inv_sigma_s, L * inv_sigma_r };
      float val[2] = { L, 1.0 };
      lattice.splat(pos, val, index, thread);
141
    }
142
  }
143

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  lattice.merge_splat_threads();

  // blur the lattice
  lattice.blur();

  //
  // Durand process :
  // r=R/(input intensity), g=G/input intensity, B=B/input intensity
  // log(base)=Bilateral(log(input intensity))
  // log(detail)=log(input intensity)-log(base)
  // log (output intensity)=log(base)*compressionfactor+log(detail)
  // R output = r*exp(log(output intensity)), etc.
  //
  // Simplyfing :
  // R output = R/(input intensity)*exp(log(output intensity))
  //          = R*exp(log(output intensity)-log(input intensity))
  //          = R*exp(log(base)*compressionfactor+log(input intensity)-log(base)-log(input intensity))
  //          = R*exp(log(base)*(compressionfactor-1))
  //
  // Plus :
  //  Before compressing the base intensity , we remove average base intensity in order to not have
  //  variable average intensity when varying compression factor.
  //  after compression we substract 2.0 to have an average intensity at middle tone.
  //

  const float contr = 1. / data->contrast;
170
#ifdef _OPENMP
171
#pragma omp parallel for
172
#endif
173 174 175 176 177 178
  for(int j = 0; j < height; j++)
  {
    size_t index = (size_t)j * width;
    const float *in = (const float *)ivoid + (size_t)j * width * ch;
    float *out = (float *)ovoid + (size_t)j * width * ch;
    for(int i = 0; i < width; i++, index++, in += ch, out += ch)
179
    {
180 181 182 183 184 185 186 187 188 189 190 191 192
      float val[2];
      lattice.slice(val, index);
      float L = 0.2126 * in[0] + 0.7152 * in[1] + 0.0722 * in[2];
      if(L <= 0.0) L = 1e-6;
      L = logf(L);
      const float B = val[0] / val[1];
      const float detail = L - B;
      const float Ln = expf(B * (contr - 1.0f) + detail - 1.0f);

      out[0] = in[0] * Ln;
      out[1] = in[1] * Ln;
      out[2] = in[2] * Ln;
      out[3] = in[3];
193
    }
194
  }
195 196
  // also process the clipping point, as good as we can without knowing
  // the local environment (i.e. assuming detail == 0)
197
  float *pmax = piece->pipe->dsc.processed_maximum;
198 199 200 201 202 203
  float L = 0.2126 * pmax[0] + 0.7152 * pmax[1] + 0.0722 * pmax[2];
  if(L <= 0.0) L = 1e-6;
  L = logf(L);
  const float Ln = expf(L * (contr - 1.0f) - 1.0f);
  for(int k = 0; k < 3; k++) pmax[k] *= Ln;
}
204 205 206 207


// GUI
//
208 209 210 211 212 213 214 215
static void contrast_callback(GtkWidget *slider, gpointer user_data)
{
  dt_iop_module_t *self = (dt_iop_module_t *)user_data;
  if(self->dt->gui->reset) return;
  dt_iop_tonemapping_params_t *p = (dt_iop_tonemapping_params_t *)self->params;
  p->contrast = dt_bauhaus_slider_get(slider);
  dt_dev_add_history_item(darktable.develop, self, TRUE);
}
216

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

226 227 228 229 230 231 232 233
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)
{
  dt_iop_tonemapping_params_t *p = (dt_iop_tonemapping_params_t *)p1;
  dt_iop_tonemapping_data_t *d = (dt_iop_tonemapping_data_t *)piece->data;
  d->contrast = p->contrast;
  d->Fsize = p->Fsize;
}
234

235 236 237 238 239
void init_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
{
  piece->data = malloc(sizeof(dt_iop_tonemapping_data_t));
  self->commit_params(self, self->default_params, pipe, piece);
}
240

241 242 243 244 245
void cleanup_pipe(struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
{
  free(piece->data);
  piece->data = NULL;
}
246

247 248 249 250 251 252 253 254
void gui_update(struct dt_iop_module_t *self)
{
  dt_iop_module_t *module = (dt_iop_module_t *)self;
  dt_iop_tonemapping_gui_data_t *g = (dt_iop_tonemapping_gui_data_t *)self->gui_data;
  dt_iop_tonemapping_params_t *p = (dt_iop_tonemapping_params_t *)module->params;
  dt_bauhaus_slider_set(g->contrast, p->contrast);
  dt_bauhaus_slider_set(g->Fsize, p->Fsize);
}
255

256 257 258 259 260 261
void reload_defaults(dt_iop_module_t *module)
{
  dt_iop_tonemapping_params_t tmp = (dt_iop_tonemapping_params_t){ 2.5, 30 };
  memcpy(module->params, &tmp, sizeof(dt_iop_tonemapping_params_t));
  memcpy(module->default_params, &tmp, sizeof(dt_iop_tonemapping_params_t));
}
262

263 264 265 266 267 268
void init(dt_iop_module_t *module)
{
  // module->data = malloc(sizeof(dt_iop_tonemapping_data_t));
  module->params = (dt_iop_params_t *)malloc(sizeof(dt_iop_tonemapping_params_t));
  module->default_params = (dt_iop_params_t *)malloc(sizeof(dt_iop_tonemapping_params_t));
  module->default_enabled = 0;
Heiko Bauke's avatar
Heiko Bauke committed
269
  module->priority = 147; // module order created by iop_dependencies.py, do not edit!
270 271 272
  module->params_size = sizeof(dt_iop_tonemapping_params_t);
  module->gui_data = NULL;
}
273

274 275 276 277 278
void cleanup(dt_iop_module_t *module)
{
  free(module->params);
  module->params = NULL;
}
279

280 281 282 283 284
void gui_init(struct dt_iop_module_t *self)
{
  self->gui_data = malloc(sizeof(dt_iop_tonemapping_gui_data_t));
  dt_iop_tonemapping_gui_data_t *g = (dt_iop_tonemapping_gui_data_t *)self->gui_data;
  dt_iop_tonemapping_params_t *p = (dt_iop_tonemapping_params_t *)self->params;
285

286
  self->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, DT_BAUHAUS_SPACE);
287 288 289

  /* contrast */
  g->contrast = dt_bauhaus_slider_new_with_range(self, 1.0, 5.0000, 0.1, p->contrast, 3);
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
  gtk_box_pack_start(GTK_BOX(self->widget), GTK_WIDGET(g->contrast), TRUE, TRUE, 0);
  dt_bauhaus_widget_set_label(g->contrast, NULL, _("contrast compression"));
  g_signal_connect(G_OBJECT(g->contrast), "value-changed", G_CALLBACK(contrast_callback), self);

  /* spatial extent */
  g->Fsize = dt_bauhaus_slider_new_with_range(self, 0.0, 100.0, 1.0, p->Fsize, 1);
  dt_bauhaus_slider_set_format(g->Fsize, "%.0f%%");
  dt_bauhaus_widget_set_label(g->Fsize, NULL, _("spatial extent"));
  gtk_box_pack_start(GTK_BOX(self->widget), GTK_WIDGET(g->Fsize), TRUE, TRUE, 0);
  g_signal_connect(G_OBJECT(g->Fsize), "value-changed", G_CALLBACK(Fsize_callback), self);
}

void gui_cleanup(struct dt_iop_module_t *self)
{
  free(self->gui_data);
  self->gui_data = NULL;
}
308
}
309

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