~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/paint/gimpdodgeburn.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <glib-object.h>
 
22
 
 
23
#include "libgimpmath/gimpmath.h"
 
24
 
 
25
#include "paint-types.h"
 
26
 
 
27
#include "base/gimplut.h"
 
28
#include "base/pixel-region.h"
 
29
#include "base/temp-buf.h"
 
30
 
 
31
#include "paint-funcs/paint-funcs.h"
 
32
 
 
33
#include "core/gimp.h"
 
34
#include "core/gimpdrawable.h"
 
35
#include "core/gimpimage.h"
 
36
 
 
37
#include "gimpdodgeburn.h"
 
38
#include "gimpdodgeburnoptions.h"
 
39
 
 
40
#include "gimp-intl.h"
 
41
 
 
42
 
 
43
static void   gimp_dodge_burn_class_init (GimpDodgeBurnClass *klass);
 
44
static void   gimp_dodge_burn_init       (GimpDodgeBurn      *dodgeburn);
 
45
 
 
46
static void   gimp_dodge_burn_finalize   (GObject            *object);
 
47
 
 
48
static void   gimp_dodge_burn_paint      (GimpPaintCore      *paint_core,
 
49
                                          GimpDrawable       *drawable,
 
50
                                          GimpPaintOptions   *paint_options,
 
51
                                          GimpPaintState      paint_state,
 
52
                                          guint32             time);
 
53
static void   gimp_dodge_burn_motion     (GimpPaintCore      *paint_core,
 
54
                                          GimpDrawable       *drawable,
 
55
                                          GimpPaintOptions   *paint_options);
 
56
 
 
57
static void   gimp_dodge_burn_make_luts  (GimpDodgeBurn      *dodgeburn,
 
58
                                          gdouble             db_exposure,
 
59
                                          GimpDodgeBurnType   type,
 
60
                                          GimpTransferMode    mode,
 
61
                                          GimpDrawable       *drawable);
 
62
 
 
63
static gfloat gimp_dodge_burn_highlights_lut_func (gpointer   user_data,
 
64
                                                   gint       nchannels,
 
65
                                                   gint       channel,
 
66
                                                   gfloat     value);
 
67
static gfloat gimp_dodge_burn_midtones_lut_func   (gpointer   user_data,
 
68
                                                   gint       nchannels,
 
69
                                                   gint       channel,
 
70
                                                   gfloat     value);
 
71
static gfloat gimp_dodge_burn_shadows_lut_func    (gpointer   user_data,
 
72
                                                   gint       nchannels,
 
73
                                                   gint       channel,
 
74
                                                   gfloat     value);
 
75
 
 
76
 
 
77
static GimpBrushCoreClass *parent_class = NULL;
 
78
 
 
79
 
 
80
void
 
81
gimp_dodge_burn_register (Gimp                      *gimp,
 
82
                          GimpPaintRegisterCallback  callback)
 
83
{
 
84
  (* callback) (gimp,
 
85
                GIMP_TYPE_DODGE_BURN,
 
86
                GIMP_TYPE_DODGE_BURN_OPTIONS,
 
87
                _("Dodge/Burn"));
 
88
}
 
89
 
 
90
GType
 
91
gimp_dodge_burn_get_type (void)
 
92
{
 
93
  static GType type = 0;
 
94
 
 
95
  if (! type)
 
96
    {
 
97
      static const GTypeInfo info =
 
98
      {
 
99
        sizeof (GimpDodgeBurnClass),
 
100
        (GBaseInitFunc) NULL,
 
101
        (GBaseFinalizeFunc) NULL,
 
102
        (GClassInitFunc) gimp_dodge_burn_class_init,
 
103
        NULL,           /* class_finalize */
 
104
        NULL,           /* class_data     */
 
105
        sizeof (GimpDodgeBurn),
 
106
        0,              /* n_preallocs    */
 
107
        (GInstanceInitFunc) gimp_dodge_burn_init,
 
108
      };
 
109
 
 
110
      type = g_type_register_static (GIMP_TYPE_BRUSH_CORE,
 
111
                                     "GimpDodgeBurn",
 
112
                                     &info, 0);
 
113
    }
 
114
 
 
115
  return type;
 
116
}
 
117
 
 
118
static void
 
119
gimp_dodge_burn_class_init (GimpDodgeBurnClass *klass)
 
120
{
 
121
  GObjectClass       *object_class     = G_OBJECT_CLASS (klass);
 
122
  GimpPaintCoreClass *paint_core_class = GIMP_PAINT_CORE_CLASS (klass);
 
123
  GimpBrushCoreClass *brush_core_class = GIMP_BRUSH_CORE_CLASS (klass);
 
124
 
 
125
  parent_class = g_type_class_peek_parent (klass);
 
126
 
 
127
  object_class->finalize  = gimp_dodge_burn_finalize;
 
128
 
 
129
  paint_core_class->paint = gimp_dodge_burn_paint;
 
130
 
 
131
  brush_core_class->handles_changing_brush = TRUE;
 
132
}
 
133
 
 
134
static void
 
135
gimp_dodge_burn_init (GimpDodgeBurn *dodgeburn)
 
136
{
 
137
}
 
138
 
 
139
static void
 
140
gimp_dodge_burn_finalize (GObject *object)
 
141
{
 
142
  GimpDodgeBurn *dodgeburn = GIMP_DODGE_BURN (object);
 
143
 
 
144
  if (dodgeburn->lut)
 
145
    {
 
146
      gimp_lut_free (dodgeburn->lut);
 
147
      dodgeburn->lut = NULL;
 
148
    }
 
149
 
 
150
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
151
}
 
152
 
 
153
static void
 
154
gimp_dodge_burn_paint (GimpPaintCore    *paint_core,
 
155
                       GimpDrawable     *drawable,
 
156
                       GimpPaintOptions *paint_options,
 
157
                       GimpPaintState    paint_state,
 
158
                       guint32           time)
 
159
{
 
160
  GimpDodgeBurn        *dodgeburn = GIMP_DODGE_BURN (paint_core);
 
161
  GimpDodgeBurnOptions *options   = GIMP_DODGE_BURN_OPTIONS (paint_options);
 
162
 
 
163
  switch (paint_state)
 
164
    {
 
165
    case GIMP_PAINT_STATE_INIT:
 
166
      dodgeburn->lut = gimp_lut_new ();
 
167
 
 
168
      gimp_dodge_burn_make_luts (dodgeburn,
 
169
                                 options->exposure,
 
170
                                 options->type,
 
171
                                 options->mode,
 
172
                                 drawable);
 
173
      break;
 
174
 
 
175
    case GIMP_PAINT_STATE_MOTION:
 
176
      gimp_dodge_burn_motion (paint_core, drawable, paint_options);
 
177
      break;
 
178
 
 
179
    case GIMP_PAINT_STATE_FINISH:
 
180
      if (dodgeburn->lut)
 
181
        {
 
182
          gimp_lut_free (dodgeburn->lut);
 
183
          dodgeburn->lut = NULL;
 
184
        }
 
185
      break;
 
186
    }
 
187
}
 
188
 
 
189
static void
 
190
gimp_dodge_burn_motion (GimpPaintCore    *paint_core,
 
191
                        GimpDrawable     *drawable,
 
192
                        GimpPaintOptions *paint_options)
 
193
{
 
194
  GimpDodgeBurn        *dodgeburn        = GIMP_DODGE_BURN (paint_core);
 
195
  GimpContext          *context          = GIMP_CONTEXT (paint_options);
 
196
  GimpPressureOptions  *pressure_options = paint_options->pressure_options;
 
197
  GimpImage            *gimage;
 
198
  TempBuf              *area;
 
199
  TempBuf              *orig;
 
200
  PixelRegion           srcPR, destPR, tempPR;
 
201
  guchar               *temp_data;
 
202
  gdouble               opacity;
 
203
 
 
204
  gimage = gimp_item_get_image (GIMP_ITEM (drawable));
 
205
 
 
206
  if (gimp_drawable_is_indexed (drawable))
 
207
    return;
 
208
 
 
209
  opacity = gimp_paint_options_get_fade (paint_options, gimage,
 
210
                                         paint_core->pixel_dist);
 
211
  if (opacity == 0.0)
 
212
    return;
 
213
 
 
214
  area = gimp_paint_core_get_paint_area (paint_core, drawable, paint_options);
 
215
  if (! area)
 
216
    return;
 
217
 
 
218
  /* Constant painting --get a copy of the orig drawable (with no
 
219
   * paint from this stroke yet)
 
220
   */
 
221
  {
 
222
    GimpItem *item = GIMP_ITEM (drawable);
 
223
    gint      x1, y1, x2, y2;
 
224
 
 
225
    x1 = CLAMP (area->x, 0, gimp_item_width  (item));
 
226
    y1 = CLAMP (area->y, 0, gimp_item_height (item));
 
227
    x2 = CLAMP (area->x + area->width,  0, gimp_item_width  (item));
 
228
    y2 = CLAMP (area->y + area->height, 0, gimp_item_height (item));
 
229
 
 
230
    if (!(x2 - x1) || !(y2 - y1))
 
231
      return;
 
232
 
 
233
    /*  get the original untouched image  */
 
234
    orig = gimp_paint_core_get_orig_image (paint_core, drawable, x1, y1, x2, y2);
 
235
 
 
236
    srcPR.bytes     = orig->bytes;
 
237
    srcPR.x         = 0;
 
238
    srcPR.y         = 0;
 
239
    srcPR.w         = x2 - x1;
 
240
    srcPR.h         = y2 - y1;
 
241
    srcPR.rowstride = srcPR.bytes * orig->width;
 
242
    srcPR.data      = temp_buf_data (orig);
 
243
  }
 
244
 
 
245
  /* tempPR will hold the dodgeburned region */
 
246
  tempPR.bytes     = srcPR.bytes;
 
247
  tempPR.x         = srcPR.x;
 
248
  tempPR.y         = srcPR.y;
 
249
  tempPR.w         = srcPR.w;
 
250
  tempPR.h         = srcPR.h;
 
251
  tempPR.rowstride = tempPR.bytes * tempPR.w;
 
252
  tempPR.data      = g_malloc (tempPR.h * tempPR.rowstride);
 
253
 
 
254
  temp_data        = tempPR.data;
 
255
 
 
256
  /*  DodgeBurn the region  */
 
257
  gimp_lut_process (dodgeburn->lut, &srcPR, &tempPR);
 
258
 
 
259
  /* The dest is the paint area we got above (= canvas_buf) */
 
260
  destPR.bytes     = area->bytes;
 
261
  destPR.x         = 0;
 
262
  destPR.y         = 0;
 
263
  destPR.w         = area->width;
 
264
  destPR.h         = area->height;
 
265
  destPR.rowstride = area->width * destPR.bytes;
 
266
  destPR.data      = temp_buf_data (area);
 
267
 
 
268
  /* Now add an alpha to the dodgeburned region
 
269
   * and put this in area = canvas_buf
 
270
   */
 
271
  if (! gimp_drawable_has_alpha (drawable))
 
272
    add_alpha_region (&tempPR, &destPR);
 
273
  else
 
274
    copy_region (&tempPR, &destPR);
 
275
 
 
276
  if (pressure_options->opacity)
 
277
    opacity *= PRESSURE_SCALE * paint_core->cur_coords.pressure;
 
278
 
 
279
  /* Replace the newly dodgedburned area (canvas_buf) to the gimage */
 
280
  gimp_brush_core_replace_canvas (GIMP_BRUSH_CORE (paint_core), drawable,
 
281
                                  MIN (opacity, GIMP_OPACITY_OPAQUE),
 
282
                                  gimp_context_get_opacity (context),
 
283
                                  gimp_paint_options_get_brush_mode (paint_options),
 
284
                                  GIMP_PAINT_CONSTANT);
 
285
 
 
286
  g_free (temp_data);
 
287
}
 
288
 
 
289
static void
 
290
gimp_dodge_burn_make_luts (GimpDodgeBurn     *dodgeburn,
 
291
                           gdouble            db_exposure,
 
292
                           GimpDodgeBurnType  type,
 
293
                           GimpTransferMode   mode,
 
294
                           GimpDrawable      *drawable)
 
295
{
 
296
  GimpLutFunc   lut_func;
 
297
  gint          nchannels = gimp_drawable_bytes (drawable);
 
298
  static gfloat exposure;
 
299
 
 
300
  exposure = db_exposure / 100.0;
 
301
 
 
302
  /* make the exposure negative if burn for luts*/
 
303
  if (type == GIMP_BURN)
 
304
    exposure = -exposure;
 
305
 
 
306
  switch (mode)
 
307
    {
 
308
    case GIMP_HIGHLIGHTS:
 
309
      lut_func = gimp_dodge_burn_highlights_lut_func;
 
310
      break;
 
311
    case GIMP_MIDTONES:
 
312
      lut_func = gimp_dodge_burn_midtones_lut_func;
 
313
      break;
 
314
    case GIMP_SHADOWS:
 
315
      lut_func = gimp_dodge_burn_shadows_lut_func;
 
316
      break;
 
317
    default:
 
318
      lut_func = NULL;
 
319
      break;
 
320
    }
 
321
 
 
322
  gimp_lut_setup_exact (dodgeburn->lut,
 
323
                        lut_func, (gpointer) &exposure,
 
324
                        nchannels);
 
325
}
 
326
 
 
327
static gfloat
 
328
gimp_dodge_burn_highlights_lut_func (gpointer  user_data,
 
329
                                     gint      nchannels,
 
330
                                     gint      channel,
 
331
                                     gfloat    value)
 
332
{
 
333
  gfloat *exposure_ptr = (gfloat *) user_data;
 
334
  gfloat  exposure     = *exposure_ptr;
 
335
  gfloat  factor       = 1.0 + exposure * (.333333);
 
336
 
 
337
  if ((nchannels == 2 && channel == 1) ||
 
338
      (nchannels == 4 && channel == 3))
 
339
    return value;
 
340
 
 
341
  return factor * value;
 
342
}
 
343
 
 
344
static gfloat
 
345
gimp_dodge_burn_midtones_lut_func (gpointer  user_data,
 
346
                                   gint      nchannels,
 
347
                                   gint      channel,
 
348
                                   gfloat    value)
 
349
{
 
350
  gfloat *exposure_ptr = (gfloat *) user_data;
 
351
  gfloat  exposure     = *exposure_ptr;
 
352
  gfloat  factor;
 
353
 
 
354
  if ((nchannels == 2 && channel == 1) ||
 
355
      (nchannels == 4 && channel == 3))
 
356
    return value;
 
357
 
 
358
  if (exposure < 0)
 
359
    factor = 1.0 - exposure * (.333333);
 
360
  else
 
361
    factor = 1 / (1.0 + exposure);
 
362
 
 
363
  return pow (value, factor);
 
364
}
 
365
 
 
366
static gfloat
 
367
gimp_dodge_burn_shadows_lut_func (gpointer  user_data,
 
368
                                  gint      nchannels,
 
369
                                  gint      channel,
 
370
                                  gfloat    value)
 
371
{
 
372
  gfloat *exposure_ptr = (gfloat *) user_data;
 
373
  gfloat  exposure     = *exposure_ptr;
 
374
  gfloat  new_value;
 
375
  gfloat  factor;
 
376
 
 
377
  if ((nchannels == 2 && channel == 1) ||
 
378
      (nchannels == 4 && channel == 3))
 
379
    return value;
 
380
 
 
381
  if (exposure >= 0)
 
382
    {
 
383
      factor = 0.333333 * exposure;
 
384
      new_value =  factor + value - factor * value;
 
385
    }
 
386
  else /* exposure < 0 */
 
387
    {
 
388
      factor = -0.333333 * exposure;
 
389
      if (value < factor)
 
390
        new_value = 0;
 
391
      else /*factor <= value <=1*/
 
392
        new_value = (value - factor)/(1 - factor);
 
393
    }
 
394
 
 
395
  return new_value;
 
396
}