useless.c 8.11 KB
Newer Older
johannes hanika's avatar
johannes hanika committed
1 2
/*
    This file is part of darktable,
3
    copyright (c) 2009--2012 johannes hanika.
johannes hanika's avatar
johannes hanika committed
4 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/>.
*/
#ifdef HAVE_CONFIG_H
19
#include "config.h"
johannes hanika's avatar
johannes hanika committed
20
#endif
21
// our includes go first:
22
#include "bauhaus/bauhaus.h"
23
#include "develop/imageop.h"
johannes hanika's avatar
johannes hanika committed
24
#include "gui/gtk.h"
25
#include "iop/iop_api.h"
26

johannes hanika's avatar
johannes hanika committed
27 28 29 30 31
#include <gtk/gtk.h>
#include <stdlib.h>

// this is the version of the modules parameters,
// and includes version information about compile-time dt
32
DT_MODULE_INTROSPECTION(1, dt_iop_useless_params_t)
johannes hanika's avatar
johannes hanika committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46

// TODO: some build system to support dt-less compilation and translation!

typedef struct dt_iop_useless_params_t
{
  // these are stored in db.
  // make sure everything is in here does not
  // depend on temporary memory (pointers etc)
  // stored in self->params and self->default_params
  // also, since this is stored in db, you should keep changes to this struct
  // to a minimum. if you have to change this struct, it will break
  // users data bases, and you should increment the version
  // of DT_MODULE(VERSION) above!
  int checker_scale;
47
} dt_iop_useless_params_t;
johannes hanika's avatar
johannes hanika committed
48 49 50 51 52

typedef struct dt_iop_useless_gui_data_t
{
  // whatever you need to make your gui happy.
  // stored in self->gui_data
53
  GtkWidget *scale; // this is needed by gui_update
54
} dt_iop_useless_gui_data_t;
johannes hanika's avatar
johannes hanika committed
55 56 57 58 59 60 61 62

typedef struct dt_iop_useless_global_data_t
{
  // this is optionally stored in self->global_data
  // and can be used to alloc globally needed stuff
  // which is needed in gui mode and during processing.

  // we don't need it for this example (as for most dt plugins)
63
} dt_iop_useless_global_data_t;
johannes hanika's avatar
johannes hanika committed
64 65 66 67 68 69 70 71

// this returns a translatable name
const char *name()
{
  // make sure you put all your translatable strings into _() !
  return _("silly example");
}

72
// some additional flags (self explanatory i think):
73
int flags()
74 75 76 77
{
  return IOP_FLAGS_INCLUDE_IN_STYLES | IOP_FLAGS_SUPPORTS_BLENDING;
}

78
// where does it appear in the gui?
79
int groups()
80
{
81
  return IOP_GROUP_BASIC;
82 83
}

johannes hanika's avatar
johannes hanika committed
84
/** modify regions of interest (optional, per pixel ops don't need this) */
85 86 87 88
// void modify_roi_out(struct dt_iop_module_t *self, struct dt_dev_pixelpipe_iop_t *piece, dt_iop_roi_t
// *roi_out, const dt_iop_roi_t *roi_in);
// void modify_roi_in(struct dt_iop_module_t *self, struct dt_dev_pixelpipe_iop_t *piece, const dt_iop_roi_t
// *roi_out, dt_iop_roi_t *roi_in);
johannes hanika's avatar
johannes hanika committed
89 90

/** process, all real work is done here. */
91 92
void process(struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, const void *const i, void *const o,
             const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out)
johannes hanika's avatar
johannes hanika committed
93 94 95 96 97 98
{
  // this is called for preview and full pipe separately, each with its own pixelpipe piece.
  // get our data struct:
  dt_iop_useless_params_t *d = (dt_iop_useless_params_t *)piece->data;
  // the total scale is composed of scale before input to the pipeline (iscale),
  // and the scale of the roi.
99
  const float scale = piece->iscale / roi_in->scale;
100 101
  // how many colors in our buffer?
  const int ch = piece->colors;
102
// iterate over all output pixels (same coordinates as input)
johannes hanika's avatar
johannes hanika committed
103
#ifdef _OPENMP
104
// optional: parallelize it!
105
#pragma omp parallel for default(none) schedule(static) shared(d)
johannes hanika's avatar
johannes hanika committed
106
#endif
107
  for(int j = 0; j < roi_out->height; j++)
johannes hanika's avatar
johannes hanika committed
108
  {
109 110 111 112 113
    float *in = ((float *)i)
                + (size_t)ch * roi_in->width
                  * j; // make sure to address input, output and temp buffers with size_t as we want to also
    float *out = ((float *)o) + (size_t)ch * roi_out->width * j; // correctly handle huge images
    for(int i = 0; i < roi_out->width; i++)
johannes hanika's avatar
johannes hanika committed
114 115 116
    {
      // calculate world space coordinates:
      int wi = (roi_in->x + i) * scale, wj = (roi_in->y + j) * scale;
117 118 119 120
      if((wi / d->checker_scale + wj / d->checker_scale) & 1)
        for(int c = 0; c < 3; c++) out[c] = 0;
      else
        for(int c = 0; c < 3; c++) out[c] = in[c];
121 122
      in += ch;
      out += ch;
johannes hanika's avatar
johannes hanika committed
123 124 125 126
    }
  }
}

127 128
/** optional: if this exists, it will be called to init new defaults if a new image is loaded from film strip
 * mode. */
129 130 131 132 133 134 135
void reload_defaults(dt_iop_module_t *module)
{
  // change default_enabled depending on type of image, or set new default_params even.

  // if this callback exists, it has to write default_params and default_enabled.
}

johannes hanika's avatar
johannes hanika committed
136 137 138 139
/** init, cleanup, commit to pipeline */
void init(dt_iop_module_t *module)
{
  // we don't need global data:
140
  module->data = NULL; // malloc(sizeof(dt_iop_useless_global_data_t));
141 142
  module->params = calloc(1, sizeof(dt_iop_useless_params_t));
  module->default_params = calloc(1, sizeof(dt_iop_useless_params_t));
johannes hanika's avatar
johannes hanika committed
143 144 145
  // our module is disabled by default
  // by default:
  module->default_enabled = 0;
146
  // order has to be changed by editing the dependencies in tools/iop_dependencies.py
Roman Lebedev's avatar
Roman Lebedev committed
147
  module->priority = NAN; // module order created by iop_dependencies.py, do not edit!
johannes hanika's avatar
johannes hanika committed
148 149 150
  module->params_size = sizeof(dt_iop_useless_params_t);
  module->gui_data = NULL;
  // init defaults:
151
  dt_iop_useless_params_t tmp = (dt_iop_useless_params_t){ 50 };
johannes hanika's avatar
johannes hanika committed
152 153 154 155 156

  memcpy(module->params, &tmp, sizeof(dt_iop_useless_params_t));
  memcpy(module->default_params, &tmp, sizeof(dt_iop_useless_params_t));
}

157 158 159 160 161
void init_global(dt_iop_module_so_t *module)
{
  module->data = malloc(sizeof(dt_iop_useless_global_data_t));
}

johannes hanika's avatar
johannes hanika committed
162 163 164 165
void cleanup(dt_iop_module_t *module)
{
  free(module->params);
  module->params = NULL;
166 167 168 169 170
}

void cleanup_global(dt_iop_module_so_t *module)
{
  free(module->data);
johannes hanika's avatar
johannes hanika committed
171 172 173 174
  module->data = NULL;
}

/** put your local callbacks here, be sure to make them static so they won't be visible outside this file! */
175
static void slider_callback(GtkWidget *w, dt_iop_module_t *self)
johannes hanika's avatar
johannes hanika committed
176 177 178 179
{
  // this is important to avoid cycles!
  if(darktable.gui->reset) return;
  dt_iop_useless_params_t *p = (dt_iop_useless_params_t *)self->params;
180
  p->checker_scale = dt_bauhaus_slider_get(w);
johannes hanika's avatar
johannes hanika committed
181
  // let core know of the changes
182
  dt_dev_add_history_item(darktable.develop, self, TRUE);
johannes hanika's avatar
johannes hanika committed
183 184 185
}

/** gui callbacks, these are needed. */
186
void gui_update(dt_iop_module_t *self)
johannes hanika's avatar
johannes hanika committed
187 188 189 190
{
  // let gui slider match current parameters:
  dt_iop_useless_gui_data_t *g = (dt_iop_useless_gui_data_t *)self->gui_data;
  dt_iop_useless_params_t *p = (dt_iop_useless_params_t *)self->params;
191
  dt_bauhaus_slider_set(g->scale, p->checker_scale);
johannes hanika's avatar
johannes hanika committed
192 193
}

194
void gui_init(dt_iop_module_t *self)
johannes hanika's avatar
johannes hanika committed
195 196 197 198
{
  // init the slider (more sophisticated layouts are possible with gtk tables and boxes):
  self->gui_data = malloc(sizeof(dt_iop_useless_gui_data_t));
  dt_iop_useless_gui_data_t *g = (dt_iop_useless_gui_data_t *)self->gui_data;
199 200
  g->scale = dt_bauhaus_slider_new_with_range(self, 1, 100, 1, 50, 0);
  self->widget = g->scale;
201
  g_signal_connect(G_OBJECT(g->scale), "value-changed", G_CALLBACK(slider_callback), self);
johannes hanika's avatar
johannes hanika committed
202 203
}

204
void gui_cleanup(dt_iop_module_t *self)
johannes hanika's avatar
johannes hanika committed
205 206 207 208 209 210 211
{
  // nothing else necessary, gtk will clean up the slider.
  free(self->gui_data);
  self->gui_data = NULL;
}

/** additional, optional callbacks to capture darkroom center events. */
212 213
// void gui_post_expose(dt_iop_module_t *self, cairo_t *cr, int32_t width, int32_t height, int32_t pointerx,
// int32_t pointery);
214
// int mouse_moved(dt_iop_module_t *self, double x, double y, double pressure, int which);
215 216
// int button_pressed(dt_iop_module_t *self, double x, double y, double pressure, int which, int type,
// uint32_t state);
217 218
// int button_released(struct dt_iop_module_t *self, double x, double y, int which, uint32_t state);
// int scrolled(dt_iop_module_t *self, double x, double y, int up, uint32_t state);
johannes hanika's avatar
johannes hanika committed
219

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