~ubuntu-branches/ubuntu/saucy/gimp/saucy-updates

« back to all changes in this revision

Viewing changes to .pc/string-format.patch/app/tools/gimpiscissorstool.c

  • Committer: Package Import Robot
  • Author(s): Micah Gersten
  • Date: 2012-02-22 23:47:53 UTC
  • mfrom: (1.1.25) (0.4.15 sid)
  • Revision ID: package-import@ubuntu.com-20120222234753-2a4wqdkb2bbx4mjb
Tags: 2.6.12-1ubuntu1
* Merge from Debian unstable (LP: #925674). Remaining Changes:
  - debian/patches/02_help-message.patch,
    debian/patches/03_gimp.desktop.in.in.patch:
    + Update some strings for Ubuntu
  - debian/patches/ghost-cursor.patch: fix Wacom tablet cursor events
  - debian/control:
    + Update description
  - debian/rules:
    + Set gettext domain and update translation templates

* Drop poppler patch as it's been applied upstream
  - drop debian/patches/poppler0.18.patch
  - update debian/patches/series
* fix LP: #680521 - Embed page setup dialog functionality in the print dialog
  - add debian/patches/embed-page-setup-dialog.patch
  - update debian/patches/series

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* GIMP - The GNU 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
 
/* This tool is based on a paper from SIGGRAPH '95:
20
 
 *  "Intelligent Scissors for Image Composition", Eric N. Mortensen and
21
 
 *   William A. Barrett, Brigham Young University.
22
 
 *
23
 
 * thanks to Professor D. Forsyth for prompting us to implement this tool. */
24
 
 
25
 
/* Personal note: Dr. Barrett, one of the authors of the paper written above
26
 
 * is not only one of the most brilliant people I have ever met, he is an
27
 
 * incredible professor who genuinely cares about his students and wants them
28
 
 * to learn as much as they can about the topic.
29
 
 *
30
 
 * I didn't even notice I was taking a class from the person who wrote the
31
 
 * paper until halfway through the semester.
32
 
 *                                                   -- Rockwalrus
33
 
 */
34
 
 
35
 
/* The history of this implementation is lonog and varied.  It was
36
 
 * orignally done by Spencer and Peter, and worked fine in the 0.54
37
 
 * (motif only) release of GIMP.  Later revisions (0.99.something
38
 
 * until about 1.1.4) completely changed the algorithm used, until it
39
 
 * bore little resemblance to the one described in the paper above.
40
 
 * The 0.54 version of the algorithm was then forwards ported to 1.1.4
41
 
 * by Austin Donnelly.
42
 
 */
43
 
 
44
 
/* Livewire boundary implementation done by Laramie Leavitt */
45
 
 
46
 
 
47
 
#include "config.h"
48
 
 
49
 
#include <stdlib.h>
50
 
 
51
 
#include <gtk/gtk.h>
52
 
#include <gdk/gdkkeysyms.h>
53
 
 
54
 
#include "libgimpmath/gimpmath.h"
55
 
#include "libgimpwidgets/gimpwidgets.h"
56
 
 
57
 
#include "tools-types.h"
58
 
 
59
 
#include "base/pixel-region.h"
60
 
#include "base/temp-buf.h"
61
 
#include "base/tile-manager.h"
62
 
#include "base/tile.h"
63
 
 
64
 
#include "paint-funcs/paint-funcs.h"
65
 
 
66
 
#include "core/gimpchannel.h"
67
 
#include "core/gimpchannel-select.h"
68
 
#include "core/gimpimage.h"
69
 
#include "core/gimppickable.h"
70
 
#include "core/gimpscanconvert.h"
71
 
#include "core/gimptoolinfo.h"
72
 
 
73
 
#include "widgets/gimphelp-ids.h"
74
 
#include "widgets/gimpwidgets-utils.h"
75
 
 
76
 
#include "display/gimpdisplay.h"
77
 
 
78
 
#include "gimpiscissorsoptions.h"
79
 
#include "gimpiscissorstool.h"
80
 
#include "gimptoolcontrol.h"
81
 
 
82
 
#include "gimp-intl.h"
83
 
 
84
 
 
85
 
/*  defines  */
86
 
#define  MAX_GRADIENT      179.606  /* == sqrt (127^2 + 127^2) */
87
 
#define  GRADIENT_SEARCH   32  /* how far to look when snapping to an edge */
88
 
#define  TARGET_SIZE       25
89
 
#define  POINT_WIDTH       12  /* size (in pixels) of seed handles */
90
 
#define  EXTEND_BY         0.2 /* proportion to expand cost map by */
91
 
#define  FIXED             5   /* additional fixed size to expand cost map */
92
 
#define  MIN_GRADIENT      63  /* gradients < this are directionless */
93
 
 
94
 
#define  COST_WIDTH        2   /* number of bytes for each pixel in cost map  */
95
 
 
96
 
/* weight to give between gradient (_G) and direction (_D) */
97
 
#define  OMEGA_D           0.2
98
 
#define  OMEGA_G           0.8
99
 
 
100
 
/* sentinel to mark seed point in ?cost? map */
101
 
#define  SEED_POINT        9
102
 
 
103
 
/*  Functional defines  */
104
 
#define  PIXEL_COST(x)     ((x) >> 8)
105
 
#define  PIXEL_DIR(x)      ((x) & 0x000000ff)
106
 
 
107
 
 
108
 
struct _ICurve
109
 
{
110
 
  gint       x1, y1;
111
 
  gint       x2, y2;
112
 
  GPtrArray *points;
113
 
};
114
 
 
115
 
 
116
 
/*  local function prototypes  */
117
 
 
118
 
static void   gimp_iscissors_tool_finalize       (GObject               *object);
119
 
 
120
 
static void   gimp_iscissors_tool_control        (GimpTool              *tool,
121
 
                                                  GimpToolAction         action,
122
 
                                                  GimpDisplay           *display);
123
 
static void   gimp_iscissors_tool_button_press   (GimpTool              *tool,
124
 
                                                  GimpCoords            *coords,
125
 
                                                  guint32                time,
126
 
                                                  GdkModifierType        state,
127
 
                                                  GimpDisplay           *display);
128
 
static void   gimp_iscissors_tool_button_release (GimpTool              *tool,
129
 
                                                  GimpCoords            *coords,
130
 
                                                  guint32                time,
131
 
                                                  GdkModifierType        state,
132
 
                                                  GimpButtonReleaseType  release_type,
133
 
                                                  GimpDisplay           *display);
134
 
static void   gimp_iscissors_tool_motion         (GimpTool              *tool,
135
 
                                                  GimpCoords            *coords,
136
 
                                                  guint32                time,
137
 
                                                  GdkModifierType        state,
138
 
                                                  GimpDisplay           *display);
139
 
static void   gimp_iscissors_tool_oper_update    (GimpTool              *tool,
140
 
                                                  GimpCoords            *coords,
141
 
                                                  GdkModifierType        state,
142
 
                                                  gboolean               proximity,
143
 
                                                  GimpDisplay           *display);
144
 
static void   gimp_iscissors_tool_cursor_update  (GimpTool              *tool,
145
 
                                                  GimpCoords            *coords,
146
 
                                                  GdkModifierType        state,
147
 
                                                  GimpDisplay           *display);
148
 
static gboolean gimp_iscissors_tool_key_press    (GimpTool              *tool,
149
 
                                                  GdkEventKey           *kevent,
150
 
                                                  GimpDisplay           *display);
151
 
 
152
 
static void   gimp_iscissors_tool_apply          (GimpIscissorsTool     *iscissors,
153
 
                                                  GimpDisplay           *display);
154
 
static void   gimp_iscissors_tool_reset          (GimpIscissorsTool     *iscissors);
155
 
static void   gimp_iscissors_tool_draw           (GimpDrawTool          *draw_tool);
156
 
 
157
 
 
158
 
static void          iscissors_convert         (GimpIscissorsTool *iscissors,
159
 
                                                GimpDisplay       *display);
160
 
static TileManager * gradient_map_new          (GimpImage         *image);
161
 
 
162
 
static void          find_optimal_path         (TileManager       *gradient_map,
163
 
                                                TempBuf           *dp_buf,
164
 
                                                gint               x1,
165
 
                                                gint               y1,
166
 
                                                gint               x2,
167
 
                                                gint               y2,
168
 
                                                gint               xs,
169
 
                                                gint               ys);
170
 
static void          find_max_gradient         (GimpIscissorsTool *iscissors,
171
 
                                                GimpImage         *image,
172
 
                                                gint              *x,
173
 
                                                gint              *y);
174
 
static void          calculate_curve           (GimpTool          *tool,
175
 
                                                ICurve            *curve);
176
 
static void          iscissors_draw_curve      (GimpDrawTool      *draw_tool,
177
 
                                                ICurve            *curve);
178
 
static void          iscissors_free_icurves    (GQueue            *curves);
179
 
 
180
 
static gint          mouse_over_vertex         (GimpIscissorsTool *iscissors,
181
 
                                                gdouble            x,
182
 
                                                gdouble            y);
183
 
static gboolean      clicked_on_vertex         (GimpIscissorsTool *iscissors,
184
 
                                                gdouble            x,
185
 
                                                gdouble            y);
186
 
static GList       * mouse_over_curve          (GimpIscissorsTool *iscissors,
187
 
                                                gdouble            x,
188
 
                                                gdouble            y);
189
 
static gboolean      clicked_on_curve          (GimpIscissorsTool *iscissors,
190
 
                                                gdouble            x,
191
 
                                                gdouble            y);
192
 
 
193
 
static GPtrArray   * plot_pixels               (GimpIscissorsTool *iscissors,
194
 
                                                TempBuf           *dp_buf,
195
 
                                                gint               x1,
196
 
                                                gint               y1,
197
 
                                                gint               xs,
198
 
                                                gint               ys,
199
 
                                                gint               xe,
200
 
                                                gint               ye);
201
 
 
202
 
 
203
 
G_DEFINE_TYPE (GimpIscissorsTool, gimp_iscissors_tool,
204
 
               GIMP_TYPE_SELECTION_TOOL)
205
 
 
206
 
#define parent_class gimp_iscissors_tool_parent_class
207
 
 
208
 
 
209
 
/*  static variables  */
210
 
 
211
 
/*  where to move on a given link direction  */
212
 
static const gint move[8][2] =
213
 
{
214
 
  {  1,  0 },
215
 
  {  0,  1 },
216
 
  { -1,  1 },
217
 
  {  1,  1 },
218
 
  { -1,  0 },
219
 
  {  0, -1 },
220
 
  {  1, -1 },
221
 
  { -1, -1 },
222
 
};
223
 
 
224
 
/* IE:
225
 
 * '---+---+---`
226
 
 * | 7 | 5 | 6 |
227
 
 * +---+---+---+
228
 
 * | 4 |   | 0 |
229
 
 * +---+---+---+
230
 
 * | 2 | 1 | 3 |
231
 
 * `---+---+---'
232
 
 */
233
 
 
234
 
 
235
 
/*  temporary convolution buffers --  */
236
 
static guchar  maxgrad_conv0[TILE_WIDTH * TILE_HEIGHT * 4] = "";
237
 
static guchar  maxgrad_conv1[TILE_WIDTH * TILE_HEIGHT * 4] = "";
238
 
static guchar  maxgrad_conv2[TILE_WIDTH * TILE_HEIGHT * 4] = "";
239
 
 
240
 
 
241
 
static const gfloat horz_deriv[9] =
242
 
{
243
 
   1,  0, -1,
244
 
   2,  0, -2,
245
 
   1,  0, -1,
246
 
};
247
 
 
248
 
static const gfloat vert_deriv[9] =
249
 
{
250
 
   1,  2,  1,
251
 
   0,  0,  0,
252
 
  -1, -2, -1,
253
 
};
254
 
 
255
 
static const gfloat blur_32[9] =
256
 
{
257
 
   1,  1,  1,
258
 
   1, 24,  1,
259
 
   1,  1,  1,
260
 
};
261
 
 
262
 
static gfloat  distance_weights[GRADIENT_SEARCH * GRADIENT_SEARCH];
263
 
 
264
 
static gint    diagonal_weight[256];
265
 
static gint    direction_value[256][4];
266
 
static Tile   *cur_tile    = NULL;
267
 
 
268
 
 
269
 
void
270
 
gimp_iscissors_tool_register (GimpToolRegisterCallback  callback,
271
 
                              gpointer                  data)
272
 
{
273
 
  (* callback) (GIMP_TYPE_ISCISSORS_TOOL,
274
 
                GIMP_TYPE_ISCISSORS_OPTIONS,
275
 
                gimp_iscissors_options_gui,
276
 
                0,
277
 
                "gimp-iscissors-tool",
278
 
                _("Scissors"),
279
 
                _("Scissors Select Tool: Select shapes using intelligent edge-fitting"),
280
 
                N_("Intelligent _Scissors"),
281
 
                "I",
282
 
                NULL, GIMP_HELP_TOOL_ISCISSORS,
283
 
                GIMP_STOCK_TOOL_ISCISSORS,
284
 
                data);
285
 
}
286
 
 
287
 
static void
288
 
gimp_iscissors_tool_class_init (GimpIscissorsToolClass *klass)
289
 
{
290
 
  GObjectClass      *object_class    = G_OBJECT_CLASS (klass);
291
 
  GimpToolClass     *tool_class      = GIMP_TOOL_CLASS (klass);
292
 
  GimpDrawToolClass *draw_tool_class = GIMP_DRAW_TOOL_CLASS (klass);
293
 
  gint               i;
294
 
 
295
 
  object_class->finalize     = gimp_iscissors_tool_finalize;
296
 
 
297
 
  tool_class->control        = gimp_iscissors_tool_control;
298
 
  tool_class->button_press   = gimp_iscissors_tool_button_press;
299
 
  tool_class->button_release = gimp_iscissors_tool_button_release;
300
 
  tool_class->motion         = gimp_iscissors_tool_motion;
301
 
  tool_class->oper_update    = gimp_iscissors_tool_oper_update;
302
 
  tool_class->cursor_update  = gimp_iscissors_tool_cursor_update;
303
 
  tool_class->key_press      = gimp_iscissors_tool_key_press;
304
 
 
305
 
  draw_tool_class->draw      = gimp_iscissors_tool_draw;
306
 
 
307
 
  for (i = 0; i < 256; i++)
308
 
    {
309
 
      /*  The diagonal weight array  */
310
 
      diagonal_weight[i] = (int) (i * G_SQRT2);
311
 
 
312
 
      /*  The direction value array  */
313
 
      direction_value[i][0] = (127 - abs (127 - i)) * 2;
314
 
      direction_value[i][1] = abs (127 - i) * 2;
315
 
      direction_value[i][2] = abs (191 - i) * 2;
316
 
      direction_value[i][3] = abs (63 - i) * 2;
317
 
    }
318
 
 
319
 
  /*  set the 256th index of the direction_values to the hightest cost  */
320
 
  direction_value[255][0] = 255;
321
 
  direction_value[255][1] = 255;
322
 
  direction_value[255][2] = 255;
323
 
  direction_value[255][3] = 255;
324
 
}
325
 
 
326
 
static void
327
 
gimp_iscissors_tool_init (GimpIscissorsTool *iscissors)
328
 
{
329
 
  GimpTool *tool = GIMP_TOOL (iscissors);
330
 
 
331
 
  iscissors->op           = ISCISSORS_OP_NONE;
332
 
  iscissors->dp_buf       = NULL;
333
 
  iscissors->curves       = g_queue_new ();
334
 
  iscissors->draw         = DRAW_NOTHING;
335
 
  iscissors->state        = NO_ACTION;
336
 
  iscissors->mask         = NULL;
337
 
  iscissors->gradient_map = NULL;
338
 
  iscissors->livewire     = NULL;
339
 
 
340
 
  gimp_tool_control_set_scroll_lock (tool->control, TRUE);
341
 
  gimp_tool_control_set_snap_to     (tool->control, FALSE);
342
 
  gimp_tool_control_set_preserve    (tool->control, FALSE);
343
 
  gimp_tool_control_set_dirty_mask  (tool->control, GIMP_DIRTY_IMAGE_SIZE);
344
 
  gimp_tool_control_set_tool_cursor (tool->control, GIMP_TOOL_CURSOR_ISCISSORS);
345
 
 
346
 
  gimp_iscissors_tool_reset (iscissors);
347
 
}
348
 
 
349
 
static void
350
 
gimp_iscissors_tool_finalize (GObject *object)
351
 
{
352
 
  GimpIscissorsTool *iscissors = GIMP_ISCISSORS_TOOL (object);
353
 
 
354
 
  gimp_iscissors_tool_reset (iscissors);
355
 
 
356
 
  g_queue_free (iscissors->curves);
357
 
 
358
 
  G_OBJECT_CLASS (parent_class)->finalize (object);
359
 
}
360
 
 
361
 
static void
362
 
gimp_iscissors_tool_control (GimpTool       *tool,
363
 
                             GimpToolAction  action,
364
 
                             GimpDisplay    *display)
365
 
{
366
 
  GimpIscissorsTool *iscissors = GIMP_ISCISSORS_TOOL (tool);
367
 
  IscissorsDraw      draw;
368
 
 
369
 
  switch (iscissors->state)
370
 
    {
371
 
    case SEED_PLACEMENT:
372
 
      draw = DRAW_CURVE | DRAW_CURRENT_SEED;
373
 
      break;
374
 
 
375
 
    case SEED_ADJUSTMENT:
376
 
      draw = DRAW_CURVE | DRAW_ACTIVE_CURVE;
377
 
      break;
378
 
 
379
 
    default:
380
 
      draw = DRAW_CURVE;
381
 
      break;
382
 
    }
383
 
 
384
 
  iscissors->draw = draw;
385
 
 
386
 
  GIMP_TOOL_CLASS (parent_class)->control (tool, action, display);
387
 
 
388
 
  switch (action)
389
 
    {
390
 
    case GIMP_TOOL_ACTION_PAUSE:
391
 
    case GIMP_TOOL_ACTION_RESUME:
392
 
      break;
393
 
 
394
 
    case GIMP_TOOL_ACTION_HALT:
395
 
      gimp_iscissors_tool_reset (iscissors);
396
 
      break;
397
 
    }
398
 
}
399
 
 
400
 
static void
401
 
gimp_iscissors_tool_button_press (GimpTool        *tool,
402
 
                                  GimpCoords      *coords,
403
 
                                  guint32          time,
404
 
                                  GdkModifierType  state,
405
 
                                  GimpDisplay     *display)
406
 
{
407
 
  GimpIscissorsTool    *iscissors = GIMP_ISCISSORS_TOOL (tool);
408
 
  GimpIscissorsOptions *options   = GIMP_ISCISSORS_TOOL_GET_OPTIONS (tool);
409
 
 
410
 
  iscissors->x = RINT (coords->x);
411
 
  iscissors->y = RINT (coords->y);
412
 
 
413
 
  /*  If the tool was being used in another image...reset it  */
414
 
 
415
 
  if (gimp_tool_control_is_active (tool->control))
416
 
    {
417
 
      if (display != tool->display)
418
 
        {
419
 
          gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
420
 
          gimp_iscissors_tool_reset (iscissors);
421
 
          gimp_tool_control_activate (tool->control);
422
 
        }
423
 
    }
424
 
  else
425
 
    {
426
 
      gimp_tool_control_activate (tool->control);
427
 
    }
428
 
 
429
 
  tool->display = display;
430
 
 
431
 
  switch (iscissors->state)
432
 
    {
433
 
    case NO_ACTION:
434
 
      iscissors->state = SEED_PLACEMENT;
435
 
      iscissors->draw  = DRAW_CURRENT_SEED;
436
 
 
437
 
      if (! (state & GDK_SHIFT_MASK))
438
 
        find_max_gradient (iscissors,
439
 
                           display->image,
440
 
                           &iscissors->x,
441
 
                           &iscissors->y);
442
 
 
443
 
      iscissors->x = CLAMP (iscissors->x,
444
 
                            0, gimp_image_get_width  (display->image) - 1);
445
 
      iscissors->y = CLAMP (iscissors->y,
446
 
                            0, gimp_image_get_height (display->image) - 1);
447
 
 
448
 
      iscissors->ix = iscissors->x;
449
 
      iscissors->iy = iscissors->y;
450
 
 
451
 
      /*  Initialize the selection core only on starting the tool  */
452
 
      gimp_draw_tool_start (GIMP_DRAW_TOOL (tool), display);
453
 
      break;
454
 
 
455
 
    default:
456
 
      /*  Check if the mouse click occurred on a vertex or the curve itself  */
457
 
      if (clicked_on_vertex (iscissors, coords->x, coords->y))
458
 
        {
459
 
          iscissors->nx    = iscissors->x;
460
 
          iscissors->ny    = iscissors->y;
461
 
          iscissors->state = SEED_ADJUSTMENT;
462
 
 
463
 
          iscissors->draw = DRAW_CURVE | DRAW_ACTIVE_CURVE;
464
 
          gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
465
 
        }
466
 
      /*  If the iscissors is connected, check if the click was inside  */
467
 
      else if (iscissors->connected && iscissors->mask &&
468
 
               gimp_pickable_get_opacity_at (GIMP_PICKABLE (iscissors->mask),
469
 
                                             iscissors->x,
470
 
                                             iscissors->y))
471
 
        {
472
 
          gimp_iscissors_tool_apply (iscissors, display);
473
 
        }
474
 
      else if (! iscissors->connected)
475
 
        {
476
 
          /*  if we're not connected, we're adding a new point  */
477
 
 
478
 
          /*  pause the tool, but undraw nothing  */
479
 
          iscissors->draw = DRAW_NOTHING;
480
 
          gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
481
 
 
482
 
          iscissors->state = SEED_PLACEMENT;
483
 
          iscissors->draw  = DRAW_CURRENT_SEED;
484
 
 
485
 
          if (options->interactive)
486
 
            iscissors->draw |= DRAW_LIVEWIRE;
487
 
 
488
 
          gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
489
 
        }
490
 
      break;
491
 
    }
492
 
}
493
 
 
494
 
 
495
 
static void
496
 
iscissors_convert (GimpIscissorsTool *iscissors,
497
 
                   GimpDisplay       *display)
498
 
{
499
 
  GimpSelectionOptions *options = GIMP_SELECTION_TOOL_GET_OPTIONS (iscissors);
500
 
  GimpScanConvert      *sc;
501
 
  GList                *list;
502
 
  GimpVector2          *points = NULL;
503
 
  guint                 n_total_points = 0;
504
 
 
505
 
  sc = gimp_scan_convert_new ();
506
 
 
507
 
  for (list = g_queue_peek_tail_link (iscissors->curves);
508
 
       list;
509
 
       list = g_list_previous (list))
510
 
    {
511
 
      ICurve *icurve = list->data;
512
 
 
513
 
      n_total_points += icurve->points->len;
514
 
    }
515
 
 
516
 
  points = g_new (GimpVector2, n_total_points);
517
 
  n_total_points = 0;
518
 
 
519
 
  /* go over the curves in reverse order, adding the points we have */
520
 
  for (list = g_queue_peek_tail_link (iscissors->curves);
521
 
       list;
522
 
       list = g_list_previous (list))
523
 
    {
524
 
      ICurve *icurve = list->data;
525
 
      gint    i;
526
 
      guint   n_points;
527
 
 
528
 
      n_points = icurve->points->len;
529
 
 
530
 
      for (i = 0; i < n_points; i++)
531
 
        {
532
 
          guint32  packed = GPOINTER_TO_INT (g_ptr_array_index (icurve->points,
533
 
                                                                i));
534
 
 
535
 
          points[n_total_points+i].x = packed & 0x0000ffff;
536
 
          points[n_total_points+i].y = packed >> 16;
537
 
        }
538
 
 
539
 
      n_total_points += n_points;
540
 
    }
541
 
 
542
 
  gimp_scan_convert_add_polyline (sc, n_total_points, points, TRUE);
543
 
  g_free (points);
544
 
 
545
 
  if (iscissors->mask)
546
 
    g_object_unref (iscissors->mask);
547
 
 
548
 
  iscissors->mask = gimp_channel_new_mask (display->image,
549
 
                                           gimp_image_get_width  (display->image),
550
 
                                           gimp_image_get_height (display->image));
551
 
  gimp_scan_convert_render (sc,
552
 
                            gimp_drawable_get_tiles (GIMP_DRAWABLE (iscissors->mask)),
553
 
                            0, 0, options->antialias);
554
 
  gimp_scan_convert_free (sc);
555
 
}
556
 
 
557
 
static void
558
 
gimp_iscissors_tool_button_release (GimpTool              *tool,
559
 
                                    GimpCoords            *coords,
560
 
                                    guint32                time,
561
 
                                    GdkModifierType        state,
562
 
                                    GimpButtonReleaseType  release_type,
563
 
                                    GimpDisplay           *display)
564
 
{
565
 
  GimpIscissorsTool    *iscissors = GIMP_ISCISSORS_TOOL (tool);
566
 
  GimpIscissorsOptions *options   = GIMP_ISCISSORS_TOOL_GET_OPTIONS (tool);
567
 
 
568
 
  /* Make sure X didn't skip the button release event -- as it's known
569
 
   * to do
570
 
   */
571
 
  if (iscissors->state == WAITING)
572
 
    return;
573
 
 
574
 
  /*  Undraw everything  */
575
 
  switch (iscissors->state)
576
 
    {
577
 
    case SEED_PLACEMENT:
578
 
      iscissors->draw = DRAW_CURVE | DRAW_CURRENT_SEED;
579
 
      if (options->interactive)
580
 
        iscissors->draw |= DRAW_LIVEWIRE;
581
 
      break;
582
 
    case SEED_ADJUSTMENT:
583
 
      iscissors->draw = DRAW_CURVE | DRAW_ACTIVE_CURVE;
584
 
      break;
585
 
    default:
586
 
      break;
587
 
    }
588
 
 
589
 
  gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
590
 
 
591
 
  if (release_type != GIMP_BUTTON_RELEASE_CANCEL)
592
 
    {
593
 
      /*  Progress to the next stage of intelligent selection  */
594
 
      switch (iscissors->state)
595
 
        {
596
 
        case SEED_PLACEMENT:
597
 
          /*  Add a new icurve  */
598
 
          if (!iscissors->first_point)
599
 
            {
600
 
              /*  Determine if we're connecting to the first point  */
601
 
              if (! g_queue_is_empty (iscissors->curves))
602
 
                {
603
 
                  ICurve *curve = g_queue_peek_head (iscissors->curves);
604
 
 
605
 
                  if (gimp_draw_tool_on_handle (GIMP_DRAW_TOOL (tool), display,
606
 
                                                iscissors->x, iscissors->y,
607
 
                                                GIMP_HANDLE_CIRCLE,
608
 
                                                curve->x1, curve->y1,
609
 
                                                POINT_WIDTH, POINT_WIDTH,
610
 
                                                GTK_ANCHOR_CENTER,
611
 
                                                FALSE))
612
 
                    {
613
 
                      iscissors->x = curve->x1;
614
 
                      iscissors->y = curve->y1;
615
 
                      iscissors->connected = TRUE;
616
 
                    }
617
 
                }
618
 
 
619
 
              /*  Create the new curve segment  */
620
 
              if (iscissors->ix != iscissors->x ||
621
 
                  iscissors->iy != iscissors->y)
622
 
                {
623
 
                  ICurve *curve = g_slice_new (ICurve);
624
 
 
625
 
                  curve->x1 = iscissors->ix;
626
 
                  curve->y1 = iscissors->iy;
627
 
                  iscissors->ix = curve->x2 = iscissors->x;
628
 
                  iscissors->iy = curve->y2 = iscissors->y;
629
 
                  curve->points = NULL;
630
 
 
631
 
                  g_queue_push_tail (iscissors->curves, curve);
632
 
 
633
 
                  calculate_curve (tool, curve);
634
 
                }
635
 
            }
636
 
          else /* this was our first point */
637
 
            {
638
 
              iscissors->first_point = FALSE;
639
 
            }
640
 
          break;
641
 
 
642
 
        case SEED_ADJUSTMENT:
643
 
          /*  recalculate both curves  */
644
 
          if (iscissors->curve1)
645
 
            {
646
 
              iscissors->curve1->x1 = iscissors->nx;
647
 
              iscissors->curve1->y1 = iscissors->ny;
648
 
              calculate_curve (tool, iscissors->curve1);
649
 
            }
650
 
          if (iscissors->curve2)
651
 
            {
652
 
              iscissors->curve2->x2 = iscissors->nx;
653
 
              iscissors->curve2->y2 = iscissors->ny;
654
 
              calculate_curve (tool, iscissors->curve2);
655
 
            }
656
 
          break;
657
 
 
658
 
        default:
659
 
          break;
660
 
        }
661
 
    }
662
 
 
663
 
  iscissors->state = WAITING;
664
 
 
665
 
  /*  Draw only the boundary  */
666
 
  iscissors->draw = DRAW_CURVE;
667
 
  gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
668
 
 
669
 
  /*  convert the curves into a region  */
670
 
  if (iscissors->connected)
671
 
    iscissors_convert (iscissors, display);
672
 
}
673
 
 
674
 
static void
675
 
gimp_iscissors_tool_motion (GimpTool        *tool,
676
 
                            GimpCoords      *coords,
677
 
                            guint32          time,
678
 
                            GdkModifierType  state,
679
 
                            GimpDisplay     *display)
680
 
{
681
 
  GimpIscissorsTool    *iscissors = GIMP_ISCISSORS_TOOL (tool);
682
 
  GimpIscissorsOptions *options   = GIMP_ISCISSORS_TOOL_GET_OPTIONS (tool);
683
 
 
684
 
  if (iscissors->state == NO_ACTION)
685
 
    return;
686
 
 
687
 
  if (iscissors->state == SEED_PLACEMENT)
688
 
    {
689
 
      iscissors->draw = DRAW_CURRENT_SEED;
690
 
 
691
 
      if (options->interactive)
692
 
        iscissors->draw |= DRAW_LIVEWIRE;
693
 
    }
694
 
  else if (iscissors->state == SEED_ADJUSTMENT)
695
 
    {
696
 
      iscissors->draw = DRAW_ACTIVE_CURVE;
697
 
    }
698
 
 
699
 
  gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
700
 
 
701
 
  iscissors->x = RINT (coords->x);
702
 
  iscissors->y = RINT (coords->y);
703
 
 
704
 
  switch (iscissors->state)
705
 
    {
706
 
    case SEED_PLACEMENT:
707
 
      /*  Hold the shift key down to disable the auto-edge snap feature  */
708
 
      if (! (state & GDK_SHIFT_MASK))
709
 
        find_max_gradient (iscissors, display->image,
710
 
                           &iscissors->x, &iscissors->y);
711
 
 
712
 
      iscissors->x = CLAMP (iscissors->x,
713
 
                            0, gimp_image_get_width  (display->image) - 1);
714
 
      iscissors->y = CLAMP (iscissors->y,
715
 
                            0, gimp_image_get_height (display->image) - 1);
716
 
 
717
 
      if (iscissors->first_point)
718
 
        {
719
 
          iscissors->ix = iscissors->x;
720
 
          iscissors->iy = iscissors->y;
721
 
        }
722
 
      break;
723
 
 
724
 
    case SEED_ADJUSTMENT:
725
 
      /*  Move the current seed to the location of the cursor  */
726
 
      if (! (state & GDK_SHIFT_MASK))
727
 
        find_max_gradient (iscissors, display->image,
728
 
                           &iscissors->x, &iscissors->y);
729
 
 
730
 
      iscissors->x = CLAMP (iscissors->x,
731
 
                            0, gimp_image_get_width  (display->image) - 1);
732
 
      iscissors->y = CLAMP (iscissors->y,
733
 
                            0, gimp_image_get_height (display->image) - 1);
734
 
 
735
 
      iscissors->nx = iscissors->x;
736
 
      iscissors->ny = iscissors->y;
737
 
      break;
738
 
 
739
 
    default:
740
 
      break;
741
 
    }
742
 
 
743
 
  gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
744
 
}
745
 
 
746
 
static void
747
 
gimp_iscissors_tool_draw (GimpDrawTool *draw_tool)
748
 
{
749
 
  GimpTool          *tool      = GIMP_TOOL (draw_tool);
750
 
  GimpIscissorsTool *iscissors = GIMP_ISCISSORS_TOOL (draw_tool);
751
 
 
752
 
  /*  Draw the crosshairs target if we're placing a seed  */
753
 
  if (iscissors->draw & DRAW_CURRENT_SEED)
754
 
    {
755
 
      gimp_draw_tool_draw_handle (draw_tool,
756
 
                                  GIMP_HANDLE_CROSS,
757
 
                                  iscissors->x, iscissors->y,
758
 
                                  TARGET_SIZE, TARGET_SIZE,
759
 
                                  GTK_ANCHOR_CENTER,
760
 
                                  FALSE);
761
 
 
762
 
      /* Draw a line boundary */
763
 
      if (! iscissors->first_point && ! (iscissors->draw & DRAW_LIVEWIRE))
764
 
        {
765
 
          gimp_draw_tool_draw_line (draw_tool,
766
 
                                    iscissors->ix, iscissors->iy,
767
 
                                    iscissors->x, iscissors->y,
768
 
                                    FALSE);
769
 
        }
770
 
    }
771
 
 
772
 
  /* Draw the livewire boundary */
773
 
  if ((iscissors->draw & DRAW_LIVEWIRE) && ! iscissors->first_point)
774
 
    {
775
 
      /* See if the mouse has moved.  If so, create a new segment... */
776
 
      if (! iscissors->livewire ||
777
 
          (iscissors->livewire &&
778
 
           (iscissors->ix != iscissors->livewire->x1 ||
779
 
            iscissors->x  != iscissors->livewire->x2  ||
780
 
            iscissors->iy != iscissors->livewire->y1 ||
781
 
            iscissors->y  != iscissors->livewire->y2)))
782
 
        {
783
 
          ICurve *curve = g_slice_new (ICurve);
784
 
 
785
 
          curve->x1 = iscissors->ix;
786
 
          curve->y1 = iscissors->iy;
787
 
          curve->x2 = iscissors->x;
788
 
          curve->y2 = iscissors->y;
789
 
          curve->points = NULL;
790
 
 
791
 
          if (iscissors->livewire)
792
 
            {
793
 
              if (iscissors->livewire->points)
794
 
                g_ptr_array_free (iscissors->livewire->points, TRUE);
795
 
 
796
 
              g_slice_free (ICurve, iscissors->livewire);
797
 
 
798
 
              iscissors->livewire = NULL;
799
 
            }
800
 
 
801
 
          iscissors->livewire = curve;
802
 
          calculate_curve (tool, curve);
803
 
        }
804
 
 
805
 
      /*  plot the curve  */
806
 
      iscissors_draw_curve (draw_tool, iscissors->livewire);
807
 
    }
808
 
 
809
 
  if ((iscissors->draw & DRAW_CURVE) && ! iscissors->first_point)
810
 
    {
811
 
      GList *list;
812
 
 
813
 
      /*  Draw a point at the init point coordinates  */
814
 
      if (! iscissors->connected)
815
 
        {
816
 
          gimp_draw_tool_draw_handle (draw_tool,
817
 
                                      GIMP_HANDLE_FILLED_CIRCLE,
818
 
                                      iscissors->ix,
819
 
                                      iscissors->iy,
820
 
                                      POINT_WIDTH,
821
 
                                      POINT_WIDTH,
822
 
                                      GTK_ANCHOR_CENTER,
823
 
                                      FALSE);
824
 
        }
825
 
 
826
 
      /*  Go through the list of icurves, and render each one...  */
827
 
      for (list = g_queue_peek_head_link (iscissors->curves);
828
 
           list;
829
 
           list = g_list_next (list))
830
 
        {
831
 
          ICurve *curve = list->data;
832
 
 
833
 
          if (iscissors->draw & DRAW_ACTIVE_CURVE)
834
 
            {
835
 
              /*  don't draw curve1 at all  */
836
 
              if (curve == iscissors->curve1)
837
 
                continue;
838
 
            }
839
 
 
840
 
          gimp_draw_tool_draw_handle (draw_tool,
841
 
                                      GIMP_HANDLE_FILLED_CIRCLE,
842
 
                                      curve->x1,
843
 
                                      curve->y1,
844
 
                                      POINT_WIDTH,
845
 
                                      POINT_WIDTH,
846
 
                                      GTK_ANCHOR_CENTER,
847
 
                                      FALSE);
848
 
 
849
 
          if (iscissors->draw & DRAW_ACTIVE_CURVE)
850
 
            {
851
 
              /*  draw only the start handle of curve2  */
852
 
              if (curve == iscissors->curve2)
853
 
                continue;
854
 
            }
855
 
 
856
 
          /*  plot the curve  */
857
 
          iscissors_draw_curve (draw_tool, curve);
858
 
        }
859
 
    }
860
 
 
861
 
  if (iscissors->draw & DRAW_ACTIVE_CURVE)
862
 
    {
863
 
      /*  plot both curves, and the control point between them  */
864
 
      if (iscissors->curve1)
865
 
        {
866
 
          gimp_draw_tool_draw_line (draw_tool,
867
 
                                    iscissors->curve1->x2,
868
 
                                    iscissors->curve1->y2,
869
 
                                    iscissors->nx,
870
 
                                    iscissors->ny,
871
 
                                    FALSE);
872
 
        }
873
 
      if (iscissors->curve2)
874
 
        {
875
 
          gimp_draw_tool_draw_line (draw_tool,
876
 
                                    iscissors->curve2->x1,
877
 
                                    iscissors->curve2->y1,
878
 
                                    iscissors->nx,
879
 
                                    iscissors->ny,
880
 
                                    FALSE);
881
 
        }
882
 
 
883
 
      gimp_draw_tool_draw_handle (draw_tool,
884
 
                                  GIMP_HANDLE_FILLED_CIRCLE,
885
 
                                  iscissors->nx,
886
 
                                  iscissors->ny,
887
 
                                  POINT_WIDTH,
888
 
                                  POINT_WIDTH,
889
 
                                  GTK_ANCHOR_CENTER,
890
 
                                  FALSE);
891
 
    }
892
 
}
893
 
 
894
 
 
895
 
static void
896
 
iscissors_draw_curve (GimpDrawTool *draw_tool,
897
 
                      ICurve       *curve)
898
 
{
899
 
  GimpVector2 *points;
900
 
  gpointer    *point;
901
 
  gint         i, len;
902
 
 
903
 
  if (! curve->points)
904
 
    return;
905
 
 
906
 
  len = curve->points->len;
907
 
 
908
 
  points = g_new (GimpVector2, len);
909
 
 
910
 
  for (i = 0, point = curve->points->pdata; i < len; i++, point++)
911
 
    {
912
 
      guint32 coords = GPOINTER_TO_INT (*point);
913
 
 
914
 
      points[i].x = (coords & 0x0000ffff);
915
 
      points[i].y = (coords >> 16);
916
 
    }
917
 
 
918
 
  gimp_draw_tool_draw_lines (draw_tool, points, len, FALSE, FALSE);
919
 
 
920
 
  g_free (points);
921
 
}
922
 
 
923
 
static void
924
 
gimp_iscissors_tool_oper_update (GimpTool        *tool,
925
 
                                 GimpCoords      *coords,
926
 
                                 GdkModifierType  state,
927
 
                                 gboolean         proximity,
928
 
                                 GimpDisplay     *display)
929
 
{
930
 
  GimpIscissorsTool *iscissors = GIMP_ISCISSORS_TOOL (tool);
931
 
 
932
 
  GIMP_TOOL_CLASS (parent_class)->oper_update (tool, coords, state, proximity,
933
 
                                               display);
934
 
  /* parent sets a message in the status bar, but it will be replaced here */
935
 
 
936
 
  if (mouse_over_vertex (iscissors, coords->x, coords->y) > 1)
937
 
    {
938
 
      gchar *status;
939
 
 
940
 
      status = gimp_suggest_modifiers (_("Click-Drag to move this point"),
941
 
                                       GDK_SHIFT_MASK & ~state,
942
 
                                       _("%s: disable auto-snap"), NULL, NULL);
943
 
      gimp_tool_replace_status (tool, display, status);
944
 
      g_free (status);
945
 
      iscissors->op = ISCISSORS_OP_MOVE_POINT;
946
 
    }
947
 
  else if (mouse_over_curve (iscissors, coords->x, coords->y))
948
 
    {
949
 
      ICurve *curve = g_queue_peek_head (iscissors->curves);
950
 
 
951
 
      if (gimp_draw_tool_on_handle (GIMP_DRAW_TOOL (tool), display,
952
 
                                    RINT (coords->x), RINT (coords->y),
953
 
                                    GIMP_HANDLE_CIRCLE,
954
 
                                    curve->x1, curve->y1,
955
 
                                    POINT_WIDTH, POINT_WIDTH,
956
 
                                    GTK_ANCHOR_CENTER,
957
 
                                    FALSE))
958
 
        {
959
 
          gimp_tool_replace_status (tool, display, _("Click to close the"
960
 
                                                     " curve"));
961
 
          iscissors->op = ISCISSORS_OP_CONNECT;
962
 
        }
963
 
      else
964
 
        {
965
 
          gimp_tool_replace_status (tool, display, _("Click to add a point"
966
 
                                                     " on this segment"));
967
 
          iscissors->op = ISCISSORS_OP_ADD_POINT;
968
 
        }
969
 
    }
970
 
  else if (iscissors->connected && iscissors->mask)
971
 
    {
972
 
      if (gimp_pickable_get_opacity_at (GIMP_PICKABLE (iscissors->mask),
973
 
                                        RINT (coords->x),
974
 
                                        RINT (coords->y)))
975
 
        {
976
 
          if (proximity)
977
 
            {
978
 
              gimp_tool_replace_status (tool, display,
979
 
                                        _("Click or press Enter to convert to"
980
 
                                          " a selection"));
981
 
            }
982
 
          iscissors->op = ISCISSORS_OP_SELECT;
983
 
        }
984
 
      else
985
 
        {
986
 
          if (proximity)
987
 
            {
988
 
              gimp_tool_replace_status (tool, display,
989
 
                                        _("Press Enter to convert to a"
990
 
                                          " selection"));
991
 
            }
992
 
          iscissors->op = ISCISSORS_OP_IMPOSSIBLE;
993
 
        }
994
 
    }
995
 
  else
996
 
    {
997
 
      switch (iscissors->state)
998
 
        {
999
 
        case WAITING:
1000
 
          if (proximity)
1001
 
            {
1002
 
              gchar *status;
1003
 
 
1004
 
              status = gimp_suggest_modifiers (_("Click or Click-Drag to add a"
1005
 
                                                 " point"),
1006
 
                                               GDK_SHIFT_MASK & ~state,
1007
 
                                               _("%s: disable auto-snap"),
1008
 
                                               NULL, NULL);
1009
 
              gimp_tool_replace_status (tool, display, status);
1010
 
              g_free (status);
1011
 
            }
1012
 
          iscissors->op = ISCISSORS_OP_ADD_POINT;
1013
 
          break;
1014
 
 
1015
 
        default:
1016
 
          /* if NO_ACTION, keep parent's status bar message (selection tool) */
1017
 
          iscissors->op = ISCISSORS_OP_NONE;
1018
 
          break;
1019
 
        }
1020
 
    }
1021
 
}
1022
 
 
1023
 
static void
1024
 
gimp_iscissors_tool_cursor_update (GimpTool        *tool,
1025
 
                                   GimpCoords      *coords,
1026
 
                                   GdkModifierType  state,
1027
 
                                   GimpDisplay     *display)
1028
 
{
1029
 
  GimpIscissorsTool  *iscissors = GIMP_ISCISSORS_TOOL (tool);
1030
 
  GimpCursorModifier  modifier  = GIMP_CURSOR_MODIFIER_NONE;
1031
 
 
1032
 
  switch (iscissors->op)
1033
 
    {
1034
 
    case ISCISSORS_OP_SELECT:
1035
 
      {
1036
 
        GimpSelectionOptions *options;
1037
 
 
1038
 
        options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
1039
 
 
1040
 
        /* Do not overwrite the modifiers for add, subtract, intersect */
1041
 
        if (options->operation == GIMP_CHANNEL_OP_REPLACE)
1042
 
          {
1043
 
            modifier = GIMP_CURSOR_MODIFIER_SELECT;
1044
 
          }
1045
 
      }
1046
 
      break;
1047
 
 
1048
 
    case ISCISSORS_OP_MOVE_POINT:
1049
 
      modifier = GIMP_CURSOR_MODIFIER_MOVE;
1050
 
      break;
1051
 
 
1052
 
    case ISCISSORS_OP_ADD_POINT:
1053
 
      modifier = GIMP_CURSOR_MODIFIER_PLUS;
1054
 
      break;
1055
 
 
1056
 
    case ISCISSORS_OP_CONNECT:
1057
 
      modifier = GIMP_CURSOR_MODIFIER_JOIN;
1058
 
      break;
1059
 
 
1060
 
    case ISCISSORS_OP_IMPOSSIBLE:
1061
 
      modifier = GIMP_CURSOR_MODIFIER_BAD;
1062
 
      break;
1063
 
 
1064
 
    default:
1065
 
      break;
1066
 
    }
1067
 
 
1068
 
  if (modifier != GIMP_CURSOR_MODIFIER_NONE)
1069
 
    {
1070
 
      gimp_tool_set_cursor (tool, display,
1071
 
                            GIMP_CURSOR_MOUSE,
1072
 
                            GIMP_TOOL_CURSOR_ISCISSORS,
1073
 
                            modifier);
1074
 
    }
1075
 
  else
1076
 
    {
1077
 
      GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords,
1078
 
                                                     state, display);
1079
 
    }
1080
 
}
1081
 
 
1082
 
static gboolean
1083
 
gimp_iscissors_tool_key_press (GimpTool    *tool,
1084
 
                               GdkEventKey *kevent,
1085
 
                               GimpDisplay *display)
1086
 
{
1087
 
  GimpIscissorsTool *iscissors = GIMP_ISCISSORS_TOOL (tool);
1088
 
 
1089
 
  if (display != tool->display)
1090
 
    return FALSE;
1091
 
 
1092
 
  switch (kevent->keyval)
1093
 
    {
1094
 
    case GDK_Return:
1095
 
    case GDK_KP_Enter:
1096
 
    case GDK_ISO_Enter:
1097
 
      if (iscissors->connected && iscissors->mask)
1098
 
        {
1099
 
          gimp_iscissors_tool_apply (iscissors, display);
1100
 
          return TRUE;
1101
 
        }
1102
 
      return FALSE;
1103
 
 
1104
 
    case GDK_Escape:
1105
 
      gimp_tool_control (tool, GIMP_TOOL_ACTION_HALT, display);
1106
 
      return TRUE;
1107
 
 
1108
 
    default:
1109
 
      return FALSE;
1110
 
    }
1111
 
}
1112
 
 
1113
 
static void
1114
 
gimp_iscissors_tool_apply (GimpIscissorsTool *iscissors,
1115
 
                           GimpDisplay       *display)
1116
 
{
1117
 
  GimpTool             *tool    = GIMP_TOOL (iscissors);
1118
 
  GimpSelectionOptions *options = GIMP_SELECTION_TOOL_GET_OPTIONS (tool);
1119
 
 
1120
 
  /*  Undraw the curve  */
1121
 
  gimp_tool_control_halt (tool->control);
1122
 
 
1123
 
  iscissors->draw = DRAW_CURVE;
1124
 
  gimp_draw_tool_stop (GIMP_DRAW_TOOL (tool));
1125
 
 
1126
 
  gimp_channel_select_channel (gimp_image_get_mask (display->image),
1127
 
                               tool->tool_info->blurb,
1128
 
                               iscissors->mask,
1129
 
                               0, 0,
1130
 
                               options->operation,
1131
 
                               options->feather,
1132
 
                               options->feather_radius,
1133
 
                               options->feather_radius);
1134
 
 
1135
 
  gimp_iscissors_tool_reset (iscissors);
1136
 
 
1137
 
  gimp_image_flush (display->image);
1138
 
}
1139
 
 
1140
 
static void
1141
 
gimp_iscissors_tool_reset (GimpIscissorsTool *iscissors)
1142
 
{
1143
 
  /*  Free and reset the curve list  */
1144
 
  iscissors_free_icurves (iscissors->curves);
1145
 
 
1146
 
  /*  free mask  */
1147
 
  if (iscissors->mask)
1148
 
    {
1149
 
      g_object_unref (iscissors->mask);
1150
 
      iscissors->mask = NULL;
1151
 
    }
1152
 
 
1153
 
  /* free the gradient map */
1154
 
  if (iscissors->gradient_map)
1155
 
    {
1156
 
      /* release any tile we were using */
1157
 
      if (cur_tile)
1158
 
        {
1159
 
          tile_release (cur_tile, FALSE);
1160
 
          cur_tile = NULL;
1161
 
        }
1162
 
 
1163
 
      tile_manager_unref (iscissors->gradient_map);
1164
 
      iscissors->gradient_map = NULL;
1165
 
    }
1166
 
 
1167
 
  iscissors->curve1      = NULL;
1168
 
  iscissors->curve2      = NULL;
1169
 
  iscissors->first_point = TRUE;
1170
 
  iscissors->connected   = FALSE;
1171
 
  iscissors->draw        = DRAW_NOTHING;
1172
 
  iscissors->state       = NO_ACTION;
1173
 
 
1174
 
  /*  Reset the dp buffers  */
1175
 
  if (iscissors->dp_buf)
1176
 
    {
1177
 
      temp_buf_free (iscissors->dp_buf);
1178
 
      iscissors->dp_buf = NULL;
1179
 
    }
1180
 
}
1181
 
 
1182
 
 
1183
 
static void
1184
 
iscissors_free_icurves (GQueue *curves)
1185
 
{
1186
 
  while (! g_queue_is_empty (curves))
1187
 
    {
1188
 
      ICurve *curve = g_queue_pop_head (curves);
1189
 
 
1190
 
      if (curve->points)
1191
 
        g_ptr_array_free (curve->points, TRUE);
1192
 
 
1193
 
      g_slice_free (ICurve, curve);
1194
 
    }
1195
 
}
1196
 
 
1197
 
 
1198
 
/* XXX need some scan-conversion routines from somewhere.  maybe. ? */
1199
 
 
1200
 
static gint
1201
 
mouse_over_vertex (GimpIscissorsTool *iscissors,
1202
 
                   gdouble            x,
1203
 
                   gdouble            y)
1204
 
{
1205
 
  GList *list;
1206
 
  gint   curves_found = 0;
1207
 
 
1208
 
  /*  traverse through the list, returning non-zero if the current cursor
1209
 
   *  position is on an existing curve vertex.  Set the curve1 and curve2
1210
 
   *  variables to the two curves containing the vertex in question
1211
 
   */
1212
 
 
1213
 
  iscissors->curve1 = iscissors->curve2 = NULL;
1214
 
 
1215
 
  for (list = g_queue_peek_head_link (iscissors->curves);
1216
 
       list;
1217
 
       list = g_list_next (list))
1218
 
    {
1219
 
      ICurve *curve = list->data;
1220
 
 
1221
 
      if (gimp_draw_tool_on_handle (GIMP_DRAW_TOOL (iscissors),
1222
 
                                    GIMP_TOOL (iscissors)->display,
1223
 
                                    x, y,
1224
 
                                    GIMP_HANDLE_CIRCLE,
1225
 
                                    curve->x1, curve->y1,
1226
 
                                    POINT_WIDTH, POINT_WIDTH,
1227
 
                                    GTK_ANCHOR_CENTER,
1228
 
                                    FALSE))
1229
 
        {
1230
 
          iscissors->curve1 = curve;
1231
 
 
1232
 
          if (curves_found++)
1233
 
            return curves_found;
1234
 
        }
1235
 
      else if (gimp_draw_tool_on_handle (GIMP_DRAW_TOOL (iscissors),
1236
 
                                         GIMP_TOOL (iscissors)->display,
1237
 
                                         x, y,
1238
 
                                         GIMP_HANDLE_CIRCLE,
1239
 
                                         curve->x2, curve->y2,
1240
 
                                         POINT_WIDTH, POINT_WIDTH,
1241
 
                                         GTK_ANCHOR_CENTER,
1242
 
                                         FALSE))
1243
 
        {
1244
 
          iscissors->curve2 = curve;
1245
 
 
1246
 
          if (curves_found++)
1247
 
            return curves_found;
1248
 
        }
1249
 
    }
1250
 
 
1251
 
  return curves_found;
1252
 
}
1253
 
 
1254
 
static gboolean
1255
 
clicked_on_vertex (GimpIscissorsTool *iscissors,
1256
 
                   gdouble            x,
1257
 
                   gdouble            y)
1258
 
{
1259
 
  gint curves_found = 0;
1260
 
 
1261
 
  curves_found = mouse_over_vertex (iscissors, x, y);
1262
 
 
1263
 
  if (curves_found > 1)
1264
 
    {
1265
 
      /*  undraw the curve  */
1266
 
      iscissors->draw = DRAW_CURVE;
1267
 
      gimp_draw_tool_pause (GIMP_DRAW_TOOL (iscissors));
1268
 
 
1269
 
      return TRUE;
1270
 
    }
1271
 
 
1272
 
  /*  if only one curve was found, the curves are unconnected, and
1273
 
   *  the user only wants to move either the first or last point
1274
 
   *  disallow this for now.
1275
 
   */
1276
 
  if (curves_found == 1)
1277
 
    return FALSE;
1278
 
 
1279
 
  return clicked_on_curve (iscissors, x, y);
1280
 
}
1281
 
 
1282
 
 
1283
 
static GList *
1284
 
mouse_over_curve (GimpIscissorsTool *iscissors,
1285
 
                  gdouble            x,
1286
 
                  gdouble            y)
1287
 
{
1288
 
  GList *list;
1289
 
 
1290
 
  /*  traverse through the list, returning the curve segment's list element
1291
 
   *  if the current cursor position is on a curve...
1292
 
   */
1293
 
  for (list = g_queue_peek_head_link (iscissors->curves);
1294
 
       list;
1295
 
       list = g_list_next (list))
1296
 
    {
1297
 
      ICurve   *curve = list->data;
1298
 
      gpointer *pt;
1299
 
      gint      len;
1300
 
 
1301
 
      pt = curve->points->pdata;
1302
 
      len = curve->points->len;
1303
 
 
1304
 
      while (len--)
1305
 
        {
1306
 
          guint32 coords = GPOINTER_TO_INT (*pt);
1307
 
          gint    tx, ty;
1308
 
 
1309
 
          pt++;
1310
 
          tx = coords & 0x0000ffff;
1311
 
          ty = coords >> 16;
1312
 
 
1313
 
          /*  Is the specified point close enough to the curve?  */
1314
 
          if (gimp_draw_tool_in_radius (GIMP_DRAW_TOOL (iscissors),
1315
 
                                        GIMP_TOOL (iscissors)->display,
1316
 
                                        tx, ty,
1317
 
                                        x, y,
1318
 
                                        POINT_WIDTH / 2))
1319
 
            {
1320
 
              return list;
1321
 
            }
1322
 
        }
1323
 
    }
1324
 
 
1325
 
  return NULL;
1326
 
}
1327
 
 
1328
 
static gboolean
1329
 
clicked_on_curve (GimpIscissorsTool *iscissors,
1330
 
                  gdouble            x,
1331
 
                  gdouble            y)
1332
 
{
1333
 
  GList *list = mouse_over_curve (iscissors, x, y);
1334
 
 
1335
 
  /*  traverse through the list, getting back the curve segment's list
1336
 
   *  element if the current cursor position is on a curve...
1337
 
   *  If this occurs, replace the curve with two new curves,
1338
 
   *  separated by a new vertex.
1339
 
   */
1340
 
 
1341
 
  if (list)
1342
 
    {
1343
 
      ICurve *curve = list->data;
1344
 
      ICurve *new_curve;
1345
 
 
1346
 
      /*  undraw the curve  */
1347
 
      iscissors->draw = DRAW_CURVE;
1348
 
      gimp_draw_tool_pause (GIMP_DRAW_TOOL (iscissors));
1349
 
 
1350
 
      /*  Create the new curve  */
1351
 
      new_curve = g_slice_new (ICurve);
1352
 
 
1353
 
      new_curve->x2 = curve->x2;
1354
 
      new_curve->y2 = curve->y2;
1355
 
      new_curve->x1 = curve->x2 = iscissors->x;
1356
 
      new_curve->y1 = curve->y2 = iscissors->y;
1357
 
      new_curve->points = NULL;
1358
 
 
1359
 
      /*  Create the new link and supply the new curve as data  */
1360
 
      g_queue_insert_after (iscissors->curves, list, new_curve);
1361
 
 
1362
 
      iscissors->curve1 = new_curve;
1363
 
      iscissors->curve2 = curve;
1364
 
 
1365
 
      return TRUE;
1366
 
    }
1367
 
 
1368
 
  return FALSE;
1369
 
}
1370
 
 
1371
 
 
1372
 
static void
1373
 
calculate_curve (GimpTool *tool,
1374
 
                 ICurve   *curve)
1375
 
{
1376
 
  GimpIscissorsTool *iscissors;
1377
 
  GimpDisplay       *display;
1378
 
  gint               x, y, dir;
1379
 
  gint               xs, ys, xe, ye;
1380
 
  gint               x1, y1, x2, y2;
1381
 
  gint               width, height;
1382
 
  gint               ewidth, eheight;
1383
 
 
1384
 
  /*  Calculate the lowest cost path from one vertex to the next as specified
1385
 
   *  by the parameter "curve".
1386
 
   *    Here are the steps:
1387
 
   *      1)  Calculate the appropriate working area for this operation
1388
 
   *      2)  Allocate a temp buf for the dynamic programming array
1389
 
   *      3)  Run the dynamic programming algorithm to find the optimal path
1390
 
   *      4)  Translate the optimal path into pixels in the icurve data
1391
 
   *            structure.
1392
 
   */
1393
 
 
1394
 
  iscissors = GIMP_ISCISSORS_TOOL (tool);
1395
 
 
1396
 
  display = tool->display;
1397
 
 
1398
 
  /*  Get the bounding box  */
1399
 
  xs = CLAMP (curve->x1, 0, gimp_image_get_width  (display->image) - 1);
1400
 
  ys = CLAMP (curve->y1, 0, gimp_image_get_height (display->image) - 1);
1401
 
  xe = CLAMP (curve->x2, 0, gimp_image_get_width  (display->image) - 1);
1402
 
  ye = CLAMP (curve->y2, 0, gimp_image_get_height (display->image) - 1);
1403
 
  x1 = MIN (xs, xe);
1404
 
  y1 = MIN (ys, ye);
1405
 
  x2 = MAX (xs, xe) + 1;  /*  +1 because if xe = 199 & xs = 0, x2 - x1, width = 200  */
1406
 
  y2 = MAX (ys, ye) + 1;
1407
 
 
1408
 
  /*  expand the boundaries past the ending points by
1409
 
   *  some percentage of width and height.  This serves the following purpose:
1410
 
   *  It gives the algorithm more area to search so better solutions
1411
 
   *  are found.  This is particularly helpful in finding "bumps" which
1412
 
   *  fall outside the bounding box represented by the start and end
1413
 
   *  coordinates of the "curve".
1414
 
   */
1415
 
  ewidth  = (x2 - x1) * EXTEND_BY + FIXED;
1416
 
  eheight = (y2 - y1) * EXTEND_BY + FIXED;
1417
 
 
1418
 
  if (xe >= xs)
1419
 
    x2 += CLAMP (ewidth, 0, gimp_image_get_width (display->image) - x2);
1420
 
  else
1421
 
    x1 -= CLAMP (ewidth, 0, x1);
1422
 
 
1423
 
  if (ye >= ys)
1424
 
    y2 += CLAMP (eheight, 0, gimp_image_get_height (display->image) - y2);
1425
 
  else
1426
 
    y1 -= CLAMP (eheight, 0, y1);
1427
 
 
1428
 
  /* blow away any previous points list we might have */
1429
 
  if (curve->points)
1430
 
    {
1431
 
      g_ptr_array_free (curve->points, TRUE);
1432
 
      curve->points = NULL;
1433
 
    }
1434
 
 
1435
 
  /*  If the bounding box has width and height...  */
1436
 
  if ((x2 - x1) && (y2 - y1))
1437
 
    {
1438
 
      width = (x2 - x1);
1439
 
      height = (y2 - y1);
1440
 
 
1441
 
      /* Initialise the gradient map tile manager for this image if we
1442
 
       * don't already have one. */
1443
 
      if (!iscissors->gradient_map)
1444
 
          iscissors->gradient_map = gradient_map_new (display->image);
1445
 
 
1446
 
      /*  allocate the dynamic programming array  */
1447
 
      iscissors->dp_buf =
1448
 
        temp_buf_resize (iscissors->dp_buf, 4, x1, y1, width, height);
1449
 
 
1450
 
      /*  find the optimal path of pixels from (x1, y1) to (x2, y2)  */
1451
 
      find_optimal_path (iscissors->gradient_map, iscissors->dp_buf,
1452
 
                         x1, y1, x2, y2, xs, ys);
1453
 
 
1454
 
      /*  get a list of the pixels in the optimal path  */
1455
 
      curve->points = plot_pixels (iscissors, iscissors->dp_buf,
1456
 
                                   x1, y1, xs, ys, xe, ye);
1457
 
    }
1458
 
  /*  If the bounding box has no width  */
1459
 
  else if ((x2 - x1) == 0)
1460
 
    {
1461
 
      /*  plot a vertical line  */
1462
 
      y = ys;
1463
 
      dir = (ys > ye) ? -1 : 1;
1464
 
      curve->points = g_ptr_array_new ();
1465
 
      while (y != ye)
1466
 
        {
1467
 
          g_ptr_array_add (curve->points, GINT_TO_POINTER ((y << 16) + xs));
1468
 
          y += dir;
1469
 
        }
1470
 
    }
1471
 
  /*  If the bounding box has no height  */
1472
 
  else if ((y2 - y1) == 0)
1473
 
    {
1474
 
      /*  plot a horizontal line  */
1475
 
      x = xs;
1476
 
      dir = (xs > xe) ? -1 : 1;
1477
 
      curve->points = g_ptr_array_new ();
1478
 
      while (x != xe)
1479
 
        {
1480
 
          g_ptr_array_add (curve->points, GINT_TO_POINTER ((ys << 16) + x));
1481
 
          x += dir;
1482
 
        }
1483
 
    }
1484
 
}
1485
 
 
1486
 
 
1487
 
/* badly need to get a replacement - this is _way_ too expensive */
1488
 
static gboolean
1489
 
gradient_map_value (TileManager *map,
1490
 
                    gint         x,
1491
 
                    gint         y,
1492
 
                    guint8      *grad,
1493
 
                    guint8      *dir)
1494
 
{
1495
 
  static gint   cur_tilex;
1496
 
  static gint   cur_tiley;
1497
 
  const guint8 *p;
1498
 
 
1499
 
  if (! cur_tile ||
1500
 
      x / TILE_WIDTH != cur_tilex ||
1501
 
      y / TILE_HEIGHT != cur_tiley)
1502
 
    {
1503
 
      if (cur_tile)
1504
 
        tile_release (cur_tile, FALSE);
1505
 
 
1506
 
      cur_tile = tile_manager_get_tile (map, x, y, TRUE, FALSE);
1507
 
 
1508
 
      if (!cur_tile)
1509
 
        return FALSE;
1510
 
 
1511
 
      cur_tilex = x / TILE_WIDTH;
1512
 
      cur_tiley = y / TILE_HEIGHT;
1513
 
    }
1514
 
 
1515
 
  p = tile_data_pointer (cur_tile, x, y);
1516
 
 
1517
 
  *grad = p[0];
1518
 
  *dir  = p[1];
1519
 
 
1520
 
  return TRUE;
1521
 
}
1522
 
 
1523
 
static gint
1524
 
calculate_link (TileManager *gradient_map,
1525
 
                gint         x,
1526
 
                gint         y,
1527
 
                guint32      pixel,
1528
 
                gint         link)
1529
 
{
1530
 
  gint   value = 0;
1531
 
  guint8 grad1, dir1, grad2, dir2;
1532
 
 
1533
 
  if (!gradient_map_value (gradient_map, x, y, &grad1, &dir1))
1534
 
    {
1535
 
      grad1 = 0;
1536
 
      dir1 = 255;
1537
 
    }
1538
 
 
1539
 
  /* Convert the gradient into a cost: large gradients are good, and
1540
 
   * so have low cost. */
1541
 
  grad1 = 255 - grad1;
1542
 
 
1543
 
  /*  calculate the contribution of the gradient magnitude  */
1544
 
  if (link > 1)
1545
 
    value += diagonal_weight[grad1] * OMEGA_G;
1546
 
  else
1547
 
    value += grad1 * OMEGA_G;
1548
 
 
1549
 
  /*  calculate the contribution of the gradient direction  */
1550
 
  x += (gint8)(pixel & 0xff);
1551
 
  y += (gint8)((pixel & 0xff00) >> 8);
1552
 
 
1553
 
  if (!gradient_map_value (gradient_map, x, y, &grad2, &dir2))
1554
 
    {
1555
 
      grad2 = 0;
1556
 
      dir2 = 255;
1557
 
    }
1558
 
 
1559
 
  value +=
1560
 
    (direction_value[dir1][link] + direction_value[dir2][link]) * OMEGA_D;
1561
 
 
1562
 
  return value;
1563
 
}
1564
 
 
1565
 
 
1566
 
static GPtrArray *
1567
 
plot_pixels (GimpIscissorsTool *iscissors,
1568
 
             TempBuf           *dp_buf,
1569
 
             gint               x1,
1570
 
             gint               y1,
1571
 
             gint               xs,
1572
 
             gint               ys,
1573
 
             gint               xe,
1574
 
             gint               ye)
1575
 
{
1576
 
  gint       x, y;
1577
 
  guint32    coords;
1578
 
  gint       link;
1579
 
  gint       width;
1580
 
  guint     *data;
1581
 
  GPtrArray *list;
1582
 
 
1583
 
  width = dp_buf->width;
1584
 
 
1585
 
  /*  Start the data pointer at the correct location  */
1586
 
  data = (guint *) temp_buf_data (dp_buf) + (ye - y1) * width + (xe - x1);
1587
 
 
1588
 
  x = xe;
1589
 
  y = ye;
1590
 
 
1591
 
  list = g_ptr_array_new ();
1592
 
 
1593
 
  while (TRUE)
1594
 
    {
1595
 
      coords = (y << 16) + x;
1596
 
      g_ptr_array_add (list, GINT_TO_POINTER (coords));
1597
 
 
1598
 
      link = PIXEL_DIR (*data);
1599
 
      if (link == SEED_POINT)
1600
 
        return list;
1601
 
 
1602
 
      x += move[link][0];
1603
 
      y += move[link][1];
1604
 
      data += move[link][1] * width + move[link][0];
1605
 
    }
1606
 
 
1607
 
  /*  won't get here  */
1608
 
  return NULL;
1609
 
}
1610
 
 
1611
 
 
1612
 
#define PACK(x, y) ((((y) & 0xff) << 8) | ((x) & 0xff))
1613
 
#define OFFSET(pixel) ((gint8)((pixel) & 0xff) + \
1614
 
  ((gint8)(((pixel) & 0xff00) >> 8)) * dp_buf->width)
1615
 
 
1616
 
 
1617
 
static void
1618
 
find_optimal_path (TileManager *gradient_map,
1619
 
                   TempBuf     *dp_buf,
1620
 
                   gint         x1,
1621
 
                   gint         y1,
1622
 
                   gint         x2,
1623
 
                   gint         y2,
1624
 
                   gint         xs,
1625
 
                   gint         ys)
1626
 
{
1627
 
  gint     i, j, k;
1628
 
  gint     x, y;
1629
 
  gint     link;
1630
 
  gint     linkdir;
1631
 
  gint     dirx, diry;
1632
 
  gint     min_cost;
1633
 
  gint     new_cost;
1634
 
  gint     offset;
1635
 
  gint     cum_cost[8];
1636
 
  gint     link_cost[8];
1637
 
  gint     pixel_cost[8];
1638
 
  guint32  pixel[8];
1639
 
  guint32 *data;
1640
 
  guint32 *d;
1641
 
 
1642
 
  /*  initialize the dynamic programming buffer  */
1643
 
  data = (guint32 *) temp_buf_data_clear (dp_buf);
1644
 
 
1645
 
  /*  what directions are we filling the array in according to?  */
1646
 
  dirx = (xs - x1 == 0) ? 1 : -1;
1647
 
  diry = (ys - y1 == 0) ? 1 : -1;
1648
 
  linkdir = (dirx * diry);
1649
 
 
1650
 
  y = ys;
1651
 
 
1652
 
  for (i = 0; i < dp_buf->height; i++)
1653
 
    {
1654
 
      x = xs;
1655
 
 
1656
 
      d = data + (y-y1) * dp_buf->width + (x-x1);
1657
 
 
1658
 
      for (j = 0; j < dp_buf->width; j++)
1659
 
        {
1660
 
          min_cost = G_MAXINT;
1661
 
 
1662
 
          /* pixel[] array encodes how to get to a neigbour, if possible.
1663
 
           * 0 means no connection (eg edge).
1664
 
           * Rest packed as bottom two bytes: y offset then x offset.
1665
 
           * Initially, we assume we can't get anywhere. */
1666
 
          for (k = 0; k < 8; k++)
1667
 
            pixel[k] = 0;
1668
 
 
1669
 
          /*  Find the valid neighboring pixels  */
1670
 
          /*  the previous pixel  */
1671
 
          if (j)
1672
 
            pixel[((dirx == 1) ? 4 : 0)] = PACK (-dirx, 0);
1673
 
 
1674
 
          /*  the previous row of pixels  */
1675
 
          if (i)
1676
 
            {
1677
 
              pixel[((diry == 1) ? 5 : 1)] = PACK (0, -diry);
1678
 
 
1679
 
              link = (linkdir == 1) ? 3 : 2;
1680
 
              if (j)
1681
 
                pixel[((diry == 1) ? (link + 4) : link)] = PACK(-dirx, -diry);
1682
 
 
1683
 
              link = (linkdir == 1) ? 2 : 3;
1684
 
              if (j != dp_buf->width - 1)
1685
 
                pixel[((diry == 1) ? (link + 4) : link)] = PACK (dirx, -diry);
1686
 
            }
1687
 
 
1688
 
          /*  find the minimum cost of going through each neighbor to reach the
1689
 
           *  seed point...
1690
 
           */
1691
 
          link = -1;
1692
 
          for (k = 0; k < 8; k ++)
1693
 
            if (pixel[k])
1694
 
              {
1695
 
                link_cost[k] = calculate_link (gradient_map,
1696
 
                                               xs + j*dirx, ys + i*diry,
1697
 
                                               pixel [k],
1698
 
                                               ((k > 3) ? k - 4 : k));
1699
 
                offset = OFFSET (pixel [k]);
1700
 
                pixel_cost[k] = PIXEL_COST (d[offset]);
1701
 
                cum_cost[k] = pixel_cost[k] + link_cost[k];
1702
 
                if (cum_cost[k] < min_cost)
1703
 
                  {
1704
 
                    min_cost = cum_cost[k];
1705
 
                    link = k;
1706
 
                  }
1707
 
              }
1708
 
 
1709
 
          /*  If anything can be done...  */
1710
 
          if (link >= 0)
1711
 
            {
1712
 
              /*  set the cumulative cost of this pixel and the new direction  */
1713
 
              *d = (cum_cost[link] << 8) + link;
1714
 
 
1715
 
              /*  possibly change the links from the other pixels to this pixel...
1716
 
               *  these changes occur if a neighboring pixel will receive a lower
1717
 
               *  cumulative cost by going through this pixel.
1718
 
               */
1719
 
              for (k = 0; k < 8; k ++)
1720
 
                if (pixel[k] && k != link)
1721
 
                  {
1722
 
                    /*  if the cumulative cost at the neighbor is greater than
1723
 
                     *  the cost through the link to the current pixel, change the
1724
 
                     *  neighbor's link to point to the current pixel.
1725
 
                     */
1726
 
                    new_cost = link_cost[k] + cum_cost[link];
1727
 
                    if (pixel_cost[k] > new_cost)
1728
 
                    {
1729
 
                      /*  reverse the link direction   /-----------------------\ */
1730
 
                      offset = OFFSET (pixel[k]);
1731
 
                      d[offset] = (new_cost << 8) + ((k > 3) ? k - 4 : k + 4);
1732
 
                    }
1733
 
                  }
1734
 
            }
1735
 
          /*  Set the seed point  */
1736
 
          else if (!i && !j)
1737
 
            *d = SEED_POINT;
1738
 
 
1739
 
          /*  increment the data pointer and the x counter  */
1740
 
          d += dirx;
1741
 
          x += dirx;
1742
 
        }
1743
 
 
1744
 
      /*  increment the y counter  */
1745
 
      y += diry;
1746
 
    }
1747
 
}
1748
 
 
1749
 
 
1750
 
/* Called to fill in a newly referenced tile in the gradient map */
1751
 
static void
1752
 
gradmap_tile_validate (TileManager *tm,
1753
 
                       Tile        *tile,
1754
 
                       GimpImage   *image)
1755
 
{
1756
 
  static gboolean first_gradient = TRUE;
1757
 
 
1758
 
  GimpPickable *pickable;
1759
 
  Tile         *srctile;
1760
 
  PixelRegion   srcPR;
1761
 
  PixelRegion   destPR;
1762
 
  gint          x, y;
1763
 
  gint          dw, dh;
1764
 
  gint          sw, sh;
1765
 
  gint          i, j;
1766
 
  gint          b;
1767
 
  gfloat        gradient;
1768
 
  guint8       *tiledata;
1769
 
  guint8       *gradmap;
1770
 
 
1771
 
  if (first_gradient)
1772
 
    {
1773
 
      gint radius = GRADIENT_SEARCH >> 1;
1774
 
 
1775
 
      /*  compute the distance weights  */
1776
 
      for (i = 0; i < GRADIENT_SEARCH; i++)
1777
 
        for (j = 0; j < GRADIENT_SEARCH; j++)
1778
 
          distance_weights[i * GRADIENT_SEARCH + j] =
1779
 
            1.0 / (1 + sqrt (SQR (i - radius) + SQR (j - radius)));
1780
 
 
1781
 
      first_gradient = FALSE;
1782
 
    }
1783
 
 
1784
 
  tile_manager_get_tile_coordinates (tm, tile, &x, &y);
1785
 
 
1786
 
  dw = tile_ewidth (tile);
1787
 
  dh = tile_eheight (tile);
1788
 
 
1789
 
  pickable = GIMP_PICKABLE (gimp_image_get_projection (image));
1790
 
 
1791
 
  gimp_pickable_flush (pickable);
1792
 
 
1793
 
  /* get corresponding tile in the image */
1794
 
  srctile = tile_manager_get_tile (gimp_pickable_get_tiles (pickable),
1795
 
                                   x, y, TRUE, FALSE);
1796
 
  if (! srctile)
1797
 
    return;
1798
 
 
1799
 
  sw = tile_ewidth (srctile);
1800
 
  sh = tile_eheight (srctile);
1801
 
 
1802
 
  pixel_region_init_data (&srcPR,
1803
 
                          tile_data_pointer (srctile, 0, 0),
1804
 
                          gimp_pickable_get_bytes (pickable),
1805
 
                          gimp_pickable_get_bytes (pickable) *
1806
 
                          MIN (dw, sw),
1807
 
                          0, 0, MIN (dw, sw), MIN (dh, sh));
1808
 
 
1809
 
 
1810
 
  /* XXX tile edges? */
1811
 
 
1812
 
  /*  Blur the source to get rid of noise  */
1813
 
  pixel_region_init_data (&destPR, maxgrad_conv0, 4, TILE_WIDTH * 4,
1814
 
                          0, 0, srcPR.w, srcPR.h);
1815
 
  convolve_region (&srcPR, &destPR, blur_32, 3, 32, GIMP_NORMAL_CONVOL, FALSE);
1816
 
 
1817
 
  /*  Use the blurred region as the new source pixel region  */
1818
 
  pixel_region_init_data (&srcPR, maxgrad_conv0, 4, TILE_WIDTH * 4,
1819
 
                          0, 0, srcPR.w, srcPR.h);
1820
 
 
1821
 
  /*  Get the horizontal derivative  */
1822
 
  pixel_region_init_data (&destPR, maxgrad_conv1, 4, TILE_WIDTH * 4,
1823
 
                          0, 0, srcPR.w, srcPR.h);
1824
 
  convolve_region (&srcPR, &destPR, horz_deriv, 3, 1, GIMP_NEGATIVE_CONVOL,
1825
 
                   FALSE);
1826
 
 
1827
 
  /*  Get the vertical derivative  */
1828
 
  pixel_region_init_data (&destPR, maxgrad_conv2, 4, TILE_WIDTH * 4,
1829
 
                          0, 0, srcPR.w, srcPR.h);
1830
 
  convolve_region (&srcPR, &destPR, vert_deriv, 3, 1, GIMP_NEGATIVE_CONVOL,
1831
 
                   FALSE);
1832
 
 
1833
 
  /* calculate overall gradient */
1834
 
  tiledata = tile_data_pointer (tile, 0, 0);
1835
 
 
1836
 
  for (i = 0; i < srcPR.h; i++)
1837
 
    {
1838
 
      const guint8 *datah = maxgrad_conv1 + srcPR.rowstride * i;
1839
 
      const guint8 *datav = maxgrad_conv2 + srcPR.rowstride * i;
1840
 
 
1841
 
      gradmap = tiledata + tile_ewidth (tile) * COST_WIDTH * i;
1842
 
 
1843
 
      for (j = 0; j < srcPR.w; j++)
1844
 
        {
1845
 
          gint8 hmax = datah[0] - 128;
1846
 
          gint8 vmax = datav[0] - 128;
1847
 
 
1848
 
          for (b = 1; b < srcPR.bytes; b++)
1849
 
            {
1850
 
              if (abs (datah[b] - 128) > abs (hmax))
1851
 
                hmax = datah[b] - 128;
1852
 
 
1853
 
              if (abs (datav[b] - 128) > abs (vmax))
1854
 
                vmax = datav[b] - 128;
1855
 
            }
1856
 
 
1857
 
          if (i == 0 || j == 0 || i == srcPR.h-1 || j == srcPR.w-1)
1858
 
            {
1859
 
              gradmap[j * COST_WIDTH + 0] = 0;
1860
 
              gradmap[j * COST_WIDTH + 1] = 255;
1861
 
              goto contin;
1862
 
            }
1863
 
 
1864
 
          /* 1 byte absolute magnitude first */
1865
 
          gradient = sqrt (SQR (hmax) + SQR (vmax));
1866
 
          gradmap[j * COST_WIDTH] = gradient * 255 / MAX_GRADIENT;
1867
 
 
1868
 
          /* then 1 byte direction */
1869
 
          if (gradient > MIN_GRADIENT)
1870
 
            {
1871
 
              gfloat direction;
1872
 
 
1873
 
              if (!hmax)
1874
 
                direction = (vmax > 0) ? G_PI_2 : -G_PI_2;
1875
 
              else
1876
 
                direction = atan ((gdouble) vmax / (gdouble) hmax);
1877
 
 
1878
 
              /* Scale the direction from between 0 and 254,
1879
 
               *  corresponding to -PI/2, PI/2 255 is reserved for
1880
 
               *  d9irectionless pixels */
1881
 
              gradmap[j * COST_WIDTH + 1] =
1882
 
                (guint8) (254 * (direction + G_PI_2) / G_PI);
1883
 
            }
1884
 
          else
1885
 
            {
1886
 
              gradmap[j * COST_WIDTH + 1] = 255; /* reserved for weak gradient */
1887
 
            }
1888
 
 
1889
 
        contin:
1890
 
          datah += srcPR.bytes;
1891
 
          datav += srcPR.bytes;
1892
 
        }
1893
 
    }
1894
 
 
1895
 
  tile_release (srctile, FALSE);
1896
 
}
1897
 
 
1898
 
static TileManager *
1899
 
gradient_map_new (GimpImage *image)
1900
 
{
1901
 
  TileManager *tm;
1902
 
 
1903
 
  tm = tile_manager_new (gimp_image_get_width  (image),
1904
 
                         gimp_image_get_height (image),
1905
 
                         sizeof (guint8) * COST_WIDTH);
1906
 
 
1907
 
  tile_manager_set_validate_proc (tm,
1908
 
                                  (TileValidateProc) gradmap_tile_validate,
1909
 
                                  image);
1910
 
 
1911
 
  return tm;
1912
 
}
1913
 
 
1914
 
static void
1915
 
find_max_gradient (GimpIscissorsTool *iscissors,
1916
 
                   GimpImage         *image,
1917
 
                   gint              *x,
1918
 
                   gint              *y)
1919
 
{
1920
 
  PixelRegion  srcPR;
1921
 
  gint         radius;
1922
 
  gint         i, j;
1923
 
  gint         endx, endy;
1924
 
  gint         sx, sy, cx, cy;
1925
 
  gint         x1, y1, x2, y2;
1926
 
  gpointer     pr;
1927
 
  gfloat       max_gradient;
1928
 
 
1929
 
  /* Initialise the gradient map tile manager for this image if we
1930
 
   * don't already have one. */
1931
 
  if (! iscissors->gradient_map)
1932
 
    iscissors->gradient_map = gradient_map_new (image);
1933
 
 
1934
 
  radius = GRADIENT_SEARCH >> 1;
1935
 
 
1936
 
  /*  calculate the extent of the search  */
1937
 
  cx = CLAMP (*x, 0, gimp_image_get_width  (image));
1938
 
  cy = CLAMP (*y, 0, gimp_image_get_height (image));
1939
 
  sx = cx - radius;
1940
 
  sy = cy - radius;
1941
 
  x1 = CLAMP (cx - radius, 0, gimp_image_get_width  (image));
1942
 
  y1 = CLAMP (cy - radius, 0, gimp_image_get_height (image));
1943
 
  x2 = CLAMP (cx + radius, 0, gimp_image_get_width  (image));
1944
 
  y2 = CLAMP (cy + radius, 0, gimp_image_get_height (image));
1945
 
  /*  calculate the factor to multiply the distance from the cursor by  */
1946
 
 
1947
 
  max_gradient = 0;
1948
 
  *x = cx;
1949
 
  *y = cy;
1950
 
 
1951
 
  /*  Find the point of max gradient  */
1952
 
  pixel_region_init (&srcPR, iscissors->gradient_map,
1953
 
                     x1, y1, x2 - x1, y2 - y1, FALSE);
1954
 
 
1955
 
  /* this iterates over 1, 2 or 4 tiles only */
1956
 
  for (pr = pixel_regions_register (1, &srcPR);
1957
 
       pr != NULL;
1958
 
       pr = pixel_regions_process (pr))
1959
 
    {
1960
 
      endx = srcPR.x + srcPR.w;
1961
 
      endy = srcPR.y + srcPR.h;
1962
 
 
1963
 
      for (i = srcPR.y; i < endy; i++)
1964
 
        {
1965
 
          const guint8 *gradient = srcPR.data + srcPR.rowstride * (i - srcPR.y);
1966
 
 
1967
 
          for (j = srcPR.x; j < endx; j++)
1968
 
            {
1969
 
              gfloat g = *gradient;
1970
 
 
1971
 
              gradient += COST_WIDTH;
1972
 
 
1973
 
              g *= distance_weights [(i-y1) * GRADIENT_SEARCH + (j-x1)];
1974
 
 
1975
 
              if (g > max_gradient)
1976
 
                {
1977
 
                  max_gradient = g;
1978
 
 
1979
 
                  *x = j;
1980
 
                  *y = i;
1981
 
                }
1982
 
            }
1983
 
        }
1984
 
    }
1985
 
}