gimptransformtool.c 55.3 KB
Newer Older
Nate Summers's avatar
Nate Summers committed
1
/* The GIMP -- an image manipulation program
Nate Summers's avatar
Nate Summers committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * Copyright (C) 1995-2001 Spencer Kimball, Peter Mattis, and others
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <stdlib.h>

#include <gtk/gtk.h>

#include "libgimpmath/gimpmath.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "apptypes.h"

30
31
#include "paint-funcs/paint-funcs.h"

32
#include "gui/info-dialog.h"
Nate Summers's avatar
Nate Summers committed
33
#include "cursorutil.h"
34
35
36
#include "gdisplay.h"
#include "gimpui.h"

Nate Summers's avatar
Nate Summers committed
37
38
39
40
#include "drawable.h"
#include "floating_sel.h"
#include "gimage_mask.h"
#include "gimpchannel.h"
41
#include "gimpcontext.h"
Nate Summers's avatar
Nate Summers committed
42
43
#include "gimpimage.h"
#include "gimplayer.h"
Nate Summers's avatar
Nate Summers committed
44
#include "gimpmarshal.h"
Nate Summers's avatar
Nate Summers committed
45
46
47
#include "gimprc.h"
#include "path_transform.h"
#include "pixel_region.h"
48
#include "pixel_surround.h"
Nate Summers's avatar
Nate Summers committed
49
50
51
52
#include "undo.h"
#include "tile_manager.h"
#include "tile.h"

Nate Summers's avatar
Nate Summers committed
53
#include "tools/gimptool.h"
54
#include "tools/gimptoolinfo.h"
Nate Summers's avatar
Nate Summers committed
55
#include "tools/tool_manager.h"
56
#include "tools/tool_options.h"
Nate Summers's avatar
Nate Summers committed
57
58
#include "tools/gimpdrawtool.h"
#include "tools/gimptransformtool.h"
59
60
61
62
63

#include "tools/gimpperspectivetool.h"
#include "tools/gimprotatetool.h"
#include "tools/gimpscaletool.h"
#include "tools/gimpsheartool.h"
64
#include "tools/gimpfliptool.h"
Nate Summers's avatar
Nate Summers committed
65
66
67

#include "libgimp/gimpintl.h"

68

Nate Summers's avatar
Nate Summers committed
69
70
71
72
enum
{
  TRANSFORM,
  LAST_SIGNAL
Nate Summers's avatar
Nate Summers committed
73
};
Nate Summers's avatar
Nate Summers committed
74

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

typedef struct _TransformOptions TransformOptions;

struct _TransformOptions
{
  ToolOptions  tool_options;

  gboolean     smoothing;
  gboolean     smoothing_d;
  GtkWidget   *smoothing_w;

  gint	       direction;
  gint         direction_d;
  GtkWidget   *direction_w[2];  /* 2 radio buttons */

  gboolean     show_grid;
  gboolean     show_grid_d;
  GtkWidget   *show_grid_w;

  gint	       grid_size;
  gint         grid_size_d;
  GtkObject   *grid_size_w;

  gboolean     clip;
  gboolean     clip_d;
  GtkWidget   *clip_w;

  gboolean     showpath;
  gboolean     showpath_d;
  GtkWidget   *showpath_w;
};

Nate Summers's avatar
Nate Summers committed
107
108
109
110
111
112
#define BILINEAR(jk,j1k,jk1,j1k1,dx,dy) \
                ((1-dy) * (jk + dx * (j1k - jk)) + \
		    dy  * (jk1 + dx * (j1k1 - jk1)))

/* access interleaved pixels */
#define CUBIC_ROW(dx, row, step) \
Nate Summers's avatar
Nate Summers committed
113
  gimp_transform_tool_cubic(dx, (row)[0], (row)[step], (row)[step+step], (row)[step+step+step])
Nate Summers's avatar
Nate Summers committed
114
#define CUBIC_SCALED_ROW(dx, row, step, i) \
Nate Summers's avatar
Nate Summers committed
115
  gimp_transform_tool_cubic(dx, (row)[0] * (row)[i], \
Nate Summers's avatar
Nate Summers committed
116
117
118
119
120
121
122
123
            (row)[step] * (row)[step + i], \
            (row)[step+step]* (row)[step+step + i], \
            (row)[step+step+step] * (row)[step+step+step + i])

#define REF_TILE(i,x,y) \
     tile[i] = tile_manager_get_tile (float_tiles, x, y, TRUE, FALSE); \
     src[i] = tile_data_pointer (tile[i], (x) % TILE_WIDTH, (y) % TILE_HEIGHT);

124

Nate Summers's avatar
Nate Summers committed
125
/*  forward function declarations  */
Nate Summers's avatar
Nate Summers committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
static void      gimp_transform_tool_bounds    (GimpTransformTool      *tool,
                                                GDisplay               *gdisp);
static void      gimp_transform_tool_recalc    (GimpTransformTool      *tool,
					        GDisplay               *gdisp);
static void      gimp_transform_tool_doit      (GimpTransformTool      *tool,
					        GDisplay               *gdisp);
static gdouble   gimp_transform_tool_cubic     (gdouble                 dx,
					        gint                    jm1,
					        gint                    j,
					        gint                    jp1,
					        gint                    jp2);
static void    gimp_transform_tool_setup_grid  (GimpTransformTool      *tool);
static void    gimp_transform_tool_grid_recalc (GimpTransformTool      *gimp_transform_tool);
static void    gimp_transform_tool_init        (GimpTransformTool      *tool);
static void    gimp_transform_tool_class_init  (GimpTransformToolClass *tool);
Nate Summers's avatar
Nate Summers committed
141

142
143
144
static void    gimp_transform_tool_destroy     (GtkObject              *object);

static void   gimp_transform_tool_button_press (GimpTool               *tool,
Nate Summers's avatar
Nate Summers committed
145
146
147
                                                GdkEventButton         *bevent,
			                        GDisplay               *gdisp);
			          
148
static void gimp_transform_tool_button_release (GimpTool               *tool,
Nate Summers's avatar
Nate Summers committed
149
150
151
			                        GdkEventButton         *bevent,
			                        GDisplay               *gdisp);
			                        
152
static void    gimp_transform_tool_motion      (GimpTool               *tool,
Nate Summers's avatar
Nate Summers committed
153
154
155
		                                GdkEventMotion         *mevent,
		                                GDisplay               *gdisp);
		                                
156
static void  gimp_transform_tool_cursor_update (GimpTool               *tool,
Nate Summers's avatar
Nate Summers committed
157
158
159
	                 		        GdkEventMotion         *mevent,
			                        GDisplay               *gdisp);
			                        
160
static void    gimp_transform_tool_control     (GimpTool               *tool,
Nate Summers's avatar
Nate Summers committed
161
162
			                        ToolAction              action,
			                        GDisplay               *gdisp);
163
164
165
166

static void    gimp_transform_tool_draw        (GimpDrawTool           *draw_tool);

static TransformOptions * transform_options_new   (void);
167
static void               transform_options_reset (ToolOptions *tool_options);
168
169


Nate Summers's avatar
Nate Summers committed
170
/*  variables  */
Nate Summers's avatar
Nate Summers committed
171
172
173
174
static TranInfo           old_trans_info;
InfoDialog               *transform_info        = NULL;
static gboolean           transform_info_inited = FALSE;
static GimpDrawToolClass *parent_class          = NULL;
175
static TransformOptions  *transform_options     = NULL;
Nate Summers's avatar
Nate Summers committed
176
static guint gimp_transform_tool_signals[LAST_SIGNAL] = { 0 };
Nate Summers's avatar
Nate Summers committed
177

178

Nate Summers's avatar
Nate Summers committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
GtkType
gimp_transform_tool_get_type (void)
{
  static GtkType tool_type = 0;

  if (! tool_type)
    {
      GtkTypeInfo tool_info =
      {
        "GimpTransformTool",
        sizeof (GimpTransformTool),
        sizeof (GimpTransformToolClass),
        (GtkClassInitFunc) gimp_transform_tool_class_init,
        (GtkObjectInitFunc) gimp_transform_tool_init,
        /* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        NULL /* (GtkClassInitFunc) gimp_tool_class_init, */
      };

      tool_type = gtk_type_unique (GIMP_TYPE_DRAW_TOOL, &tool_info);
    }

  return tool_type;
}

static void
gimp_transform_tool_class_init (GimpTransformToolClass *klass)
{
Nate Summers's avatar
Nate Summers committed
207
208
209
  GtkObjectClass    *object_class;
  GimpToolClass     *tool_class;
  GimpDrawToolClass *draw_class;
Nate Summers's avatar
Nate Summers committed
210
211
212

  object_class = (GtkObjectClass *) klass;
  tool_class   = (GimpToolClass *) klass;
Nate Summers's avatar
Nate Summers committed
213
  draw_class   = (GimpDrawToolClass *) klass;
Nate Summers's avatar
Nate Summers committed
214
215
216
217
218

  parent_class = gtk_type_class (GIMP_TYPE_DRAW_TOOL);

  gimp_transform_tool_signals[TRANSFORM] =
    gtk_signal_new ("transform",
Nate Summers's avatar
Nate Summers committed
219
    		    GTK_RUN_LAST,
Nate Summers's avatar
Nate Summers committed
220
221
222
    		    object_class->type,
    		    GTK_SIGNAL_OFFSET (GimpTransformToolClass,
    		    		       transform),
Nate Summers's avatar
Nate Summers committed
223
    		    gimp_marshal_POINTER__POINTER_INT,
Nate Summers's avatar
Nate Summers committed
224
225
226
227
228
229
230
    		    GTK_TYPE_POINTER, 2,
    		    GTK_TYPE_POINTER,
    		    GTK_TYPE_INT);

  gtk_object_class_add_signals (object_class, gimp_transform_tool_signals,
				LAST_SIGNAL);

Nate Summers's avatar
Nate Summers committed
231
  object_class->destroy      = gimp_transform_tool_destroy;
Nate Summers's avatar
Nate Summers committed
232
233
234
235
236
237

  tool_class->button_press   = gimp_transform_tool_button_press;
  tool_class->button_release = gimp_transform_tool_button_release;
  tool_class->motion         = gimp_transform_tool_motion;
  tool_class->cursor_update  = gimp_transform_tool_cursor_update;
  tool_class->control        = gimp_transform_tool_control;
Nate Summers's avatar
Nate Summers committed
238
239

  draw_class->draw           = gimp_transform_tool_draw;
Nate Summers's avatar
Nate Summers committed
240
241
242
}

static void
Nate Summers's avatar
Nate Summers committed
243
gimp_transform_tool_init (GimpTransformTool *tr_tool)
Nate Summers's avatar
Nate Summers committed
244
{
245
246
  GimpTool *tool = GIMP_TOOL (tr_tool);
  gint      i;
Nate Summers's avatar
Nate Summers committed
247
248
249
250
251
252
253
254
255
256
257

  tr_tool->function = TRANSFORM_CREATING;
  tr_tool->original = NULL;

  tr_tool->bpressed = FALSE;

  for (i = 0; i < TRAN_INFO_SIZE; i++)
    tr_tool->trans_info[i] = 0;

  tr_tool->grid_coords = tr_tool->tgrid_coords = NULL;

258
259
260
  /* FIXME */
  tr_tool->interactive = TRUE;

Nate Summers's avatar
Nate Summers committed
261
262
  tool->scroll_lock = TRUE;   /*  Disallow scrolling  */
  tool->preserve    = FALSE;  /*  Don't preserve on drawable change  */
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

  if (! transform_options)
    {
      transform_options = transform_options_new ();

      tool_manager_register_tool_options (GIMP_TYPE_ROTATE_TOOL,
					  (ToolOptions *) transform_options);
      tool_manager_register_tool_options (GIMP_TYPE_SCALE_TOOL,
					  (ToolOptions *) transform_options);
      tool_manager_register_tool_options (GIMP_TYPE_SHEAR_TOOL,
					  (ToolOptions *) transform_options);
      tool_manager_register_tool_options (GIMP_TYPE_PERSPECTIVE_TOOL,
					  (ToolOptions *) transform_options);

      /*  press all default buttons  */
278
      transform_options_reset ((ToolOptions *) transform_options);
279
    }
Nate Summers's avatar
Nate Summers committed
280
281
}

Nate Summers's avatar
Nate Summers committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
TileManager *
gimp_transform_tool_transform (GimpTransformTool   *tool,
                               GDisplay            *gdisp,
			       TransformState       state)
{
  TileManager *retval;

  g_return_val_if_fail (tool, NULL);
  g_return_val_if_fail (GIMP_IS_TRANSFORM_TOOL (tool), NULL);

  gtk_signal_emit (GTK_OBJECT (tool), gimp_transform_tool_signals[TRANSFORM],
                   gdisp, state, &retval);

  return retval;
}

Nate Summers's avatar
Nate Summers committed
298
299
300
301
static void
transform_ok_callback (GtkWidget *widget,
		       gpointer   data)
{
Nate Summers's avatar
Nate Summers committed
302
  GimpTool *tool;
Nate Summers's avatar
Nate Summers committed
303

Nate Summers's avatar
Nate Summers committed
304
305
  tool = GIMP_TOOL(data);
  gimp_transform_tool_doit (GIMP_TRANSFORM_TOOL(tool), tool->gdisp);
Nate Summers's avatar
Nate Summers committed
306
307
308
309
310
311
312
313
314
315
}

static void
transform_reset_callback (GtkWidget *widget,
			  gpointer   data)
{
  GimpTransformTool   *tool;
  GimpDrawTool        *dr_tool;
  gint                 i;

316
317
  tool    = GIMP_TRANSFORM_TOOL (data);
  dr_tool = GIMP_DRAW_TOOL (data);
Nate Summers's avatar
Nate Summers committed
318
319

  /*  stop the current tool drawing process  */
Nate Summers's avatar
Nate Summers committed
320
  gimp_draw_tool_pause (dr_tool);
Nate Summers's avatar
Nate Summers committed
321
322
323
324
325
326

  /*  Restore the previous transformation info  */
  for (i = 0; i < TRAN_INFO_SIZE; i++)
    tool->trans_info [i] = old_trans_info [i];

  /*  recalculate the tool's transformation matrix  */
Nate Summers's avatar
Nate Summers committed
327
  gimp_transform_tool_recalc (tool, GIMP_TOOL(tool)->gdisp);
Nate Summers's avatar
Nate Summers committed
328
329

  /*  resume drawing the current tool  */
Nate Summers's avatar
Nate Summers committed
330
  gimp_draw_tool_resume (dr_tool);
Nate Summers's avatar
Nate Summers committed
331
332
}

333
334
static void
gimp_transform_tool_destroy (GtkObject *object)
Nate Summers's avatar
Nate Summers committed
335
{
336
337
  GimpTransformTool *tr_tool;
  GimpTool          *tool;
Nate Summers's avatar
Nate Summers committed
338

339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
  tr_tool = GIMP_TRANSFORM_TOOL (object);
  tool    = GIMP_TOOL           (tr_tool);

  /*  Free up the original selection if it exists  */
  if (tr_tool->original)
    tile_manager_destroy (tr_tool->original);

  /*  If there is an information dialog, free it up  */
  if (transform_info)
    info_dialog_free (transform_info);

  transform_info        = NULL;
  transform_info_inited = FALSE;

  /*  Free the grid line endpoint arrays if they exist */
  if (tr_tool->grid_coords != NULL)
    g_free (tr_tool->grid_coords);

  if (tr_tool->tgrid_coords != NULL)
    g_free (tr_tool->tgrid_coords);

  if (GTK_OBJECT_CLASS (parent_class)->destroy)
    GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

static void
Nate Summers's avatar
Nate Summers committed
365
gimp_transform_tool_button_press (GimpTool           *tool,
Nate Summers's avatar
Nate Summers committed
366
367
368
                                  GdkEventButton     *bevent,
			          GDisplay           *gdisp)
{
Nate Summers's avatar
Nate Summers committed
369
370
371
372
373
374
375
  GimpTransformTool  *gt_tool;
  GimpDrawable       *drawable;
  gint                dist;
  gint                closest_dist;
  gint                x, y;
  gint                i;
  gint                off_x, off_y;
Nate Summers's avatar
Nate Summers committed
376

377
  gt_tool = GIMP_TRANSFORM_TOOL (tool);
Nate Summers's avatar
Nate Summers committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

  gt_tool->bpressed = TRUE; /* ALT */

  drawable = gimp_image_active_drawable (gdisp->gimage);

  if (gt_tool->function == TRANSFORM_CREATING && tool->state == ACTIVE)
    {
      /*  Save the current transformation info  */
      for (i = 0; i < TRAN_INFO_SIZE; i++)
	old_trans_info [i] = gt_tool->trans_info [i];
    }

  /*  if we have already displayed the bounding box and handles,
   *  check to make sure that the display which currently owns the
   *  tool is the one which just received the button pressed event
   */
  if ((gdisp == tool->gdisp) && gt_tool->interactive)
    {
      /*  start drawing the bounding box and handles...  */
397
398
      gimp_draw_tool_start (GIMP_DRAW_TOOL (gt_tool), gdisp->canvas->window);

Nate Summers's avatar
Nate Summers committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
      x = bevent->x;
      y = bevent->y;

      closest_dist = SQR (x - gt_tool->sx1) + SQR (y - gt_tool->sy1);
      gt_tool->function = TRANSFORM_HANDLE_1;

      dist = SQR (x - gt_tool->sx2) + SQR (y - gt_tool->sy2);
      if (dist < closest_dist)
	{
	  closest_dist = dist;
	  gt_tool->function = TRANSFORM_HANDLE_2;
	}

      dist = SQR (x - gt_tool->sx3) + SQR (y - gt_tool->sy3);
      if (dist < closest_dist)
	{
	  closest_dist = dist;
	  gt_tool->function = TRANSFORM_HANDLE_3;
	}

      dist = SQR (x - gt_tool->sx4) + SQR (y - gt_tool->sy4);
      if (dist < closest_dist)
	{
	  closest_dist = dist;
	  gt_tool->function = TRANSFORM_HANDLE_4;
	}

426
427
      if ((SQR (x - gt_tool->scx) +
	   SQR (y - gt_tool->scy)) <= 100)
Nate Summers's avatar
Nate Summers committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
	{
	  gt_tool->function = TRANSFORM_HANDLE_CENTER;
	}

      /*  Save the current pointer position  */
      gdisplay_untransform_coords (gdisp, bevent->x, bevent->y,
				   &gt_tool->startx,
				   &gt_tool->starty, TRUE, 0);
      gt_tool->lastx = gt_tool->startx;
      gt_tool->lasty = gt_tool->starty;

      gdk_pointer_grab (gdisp->canvas->window, FALSE,
			GDK_POINTER_MOTION_HINT_MASK |
			GDK_BUTTON1_MOTION_MASK |
			GDK_BUTTON_RELEASE_MASK,
			NULL, NULL, bevent->time);

      tool->state = ACTIVE;
      return;
    }


  /*  Initialisation stuff: if the cursor is clicked inside the current
   *  selection, show the bounding box and handles...
   */
  gdisplay_untransform_coords (gdisp, bevent->x, bevent->y, &x, &y,
			       FALSE, FALSE);

  gimp_drawable_offsets (drawable, &off_x, &off_y);
  if (x >= off_x && y >= off_y &&
      x < (off_x + gimp_drawable_width (drawable)) &&
      y < (off_y + gimp_drawable_height (drawable)))
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
    {
      if (gimage_mask_is_empty (gdisp->gimage) ||
	  gimage_mask_value (gdisp->gimage, x, y))
	{
	  if (GIMP_IS_LAYER (drawable) &&
	      gimp_layer_get_mask (GIMP_LAYER (drawable)))
	    {
	      g_message (_("Transformations do not work on\n"
			   "layers that contain layer masks."));
	      tool->state = INACTIVE;
	      return;
	    }

	  /*  If the tool is already active, clear the current state
	   *  and reset
	   */
	  if (tool->state == ACTIVE)
	    gimp_transform_tool_reset (gt_tool, gdisp);

	  /*  Set the pointer to the active display  */
	  tool->gdisp    = gdisp;
	  tool->drawable = drawable;
	  tool->state    = ACTIVE;

	  /*  Grab the pointer if we're in non-interactive mode  */
	  if (!gt_tool->interactive)
	    gdk_pointer_grab (gdisp->canvas->window, FALSE,
			      (GDK_POINTER_MOTION_HINT_MASK |
			       GDK_BUTTON1_MOTION_MASK |
			       GDK_BUTTON_RELEASE_MASK),
			      NULL, NULL, bevent->time);

	  /*  Find the transform bounds for some tools (like scale,
	   *  perspective) that actually need the bounds for
	   *  initializing
	   */
	  gimp_transform_tool_bounds (gt_tool, gdisp);

	  /*  Calculate the grid line endpoints  */
	  if (gimp_transform_tool_show_grid ())
	    gimp_transform_tool_setup_grid (gt_tool);

	  /*  Initialize the transform tool */
	  gimp_transform_tool_transform (gt_tool, gdisp, TRANSFORM_INIT);

	  if (transform_info && !transform_info_inited)
	    {
	      GtkType tool_type;

	      tool_type =
		gimp_context_get_tool (gimp_context_get_user ())->tool_type;

	      gimp_dialog_create_action_area
		(GTK_DIALOG (transform_info->shell),

		 /* FIXME: this does not belong here */
		 (tool_type == GIMP_TYPE_ROTATE_TOOL)      ? _("Rotate")    :
		 (tool_type == GIMP_TYPE_SCALE_TOOL)       ? _("Scale")     :
		 (tool_type == GIMP_TYPE_SHEAR_TOOL)       ? _("Shear")     :
		 (tool_type == GIMP_TYPE_PERSPECTIVE_TOOL) ? _("Transform") :
		 "EEK",
		 transform_ok_callback,
		 tool, NULL, NULL, TRUE, FALSE,

		 _("Reset"), transform_reset_callback,
		 tool, NULL, NULL, FALSE, FALSE,

		 NULL);

	      transform_info_inited = TRUE;
	    }

	  /*  Recalculate the transform tool  */
	  gimp_transform_tool_recalc (gt_tool, gdisp);

	  /*  recall this function to find which handle we're dragging  */
	  if (gt_tool->interactive)
	    gimp_transform_tool_button_press (tool, bevent, gdisp);
	}
    }
Nate Summers's avatar
Nate Summers committed
540
541
}

542
static void
Nate Summers's avatar
Nate Summers committed
543
544
545
gimp_transform_tool_button_release (GimpTool         *tool,
			            GdkEventButton   *bevent,
			            GDisplay         *gdisp)
Nate Summers's avatar
Nate Summers committed
546
{
Nate Summers's avatar
Nate Summers committed
547
548
  GimpTransformTool *gt_tool;
  gint               i;
Nate Summers's avatar
Nate Summers committed
549

550
  gt_tool = GIMP_TRANSFORM_TOOL (tool);
Nate Summers's avatar
Nate Summers committed
551
552
553
554

  gt_tool->bpressed = FALSE; /* ALT */

  /*  if we are creating, there is nothing to be done...exit  */
555
  if (gt_tool->function == TRANSFORM_CREATING && gt_tool->interactive)
Nate Summers's avatar
Nate Summers committed
556
557
558
559
560
561
562
563
564
565
    return;

  /*  release of the pointer grab  */
  gdk_pointer_ungrab (bevent->time);
  gdk_flush ();

  /*  if the 3rd button isn't pressed, transform the selected mask  */
  if (! (bevent->state & GDK_BUTTON3_MASK))
    {
      /* Shift-clicking is another way to approve the transform  */
566
      if ((bevent->state & GDK_SHIFT_MASK) || GIMP_IS_FLIP_TOOL (tool))
Nate Summers's avatar
Nate Summers committed
567
	{
Nate Summers's avatar
Nate Summers committed
568
	  gimp_transform_tool_doit (gt_tool, gdisp);
Nate Summers's avatar
Nate Summers committed
569
570
571
572
573
574
575
576
577
578
579
	}
      else
	{
	  /*  Only update the paths preview */
	  path_transform_current_path (gdisp->gimage,
				       gt_tool->transform, TRUE);
	}
    }
  else
    {
      /*  stop the current tool drawing process  */
580
      gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
Nate Summers's avatar
Nate Summers committed
581
582
583
584
585
586
587
588
589

      /*  Restore the previous transformation info  */
      for (i = 0; i < TRAN_INFO_SIZE; i++)
	gt_tool->trans_info [i] = old_trans_info [i];

      /*  recalculate the tool's transformation matrix  */
      gimp_transform_tool_recalc (gt_tool, gdisp);

      /*  resume drawing the current tool  */
590
      gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
Nate Summers's avatar
Nate Summers committed
591
592
593
594
595
596
597
598
599
600
601

      /* Update the paths preview */
      path_transform_current_path (gdisp->gimage,
				   gt_tool->transform, TRUE);
    }

  /*  if this tool is non-interactive, make it inactive after use  */
  if (!gt_tool->interactive)
    tool->state = INACTIVE;
}

602
static void
Nate Summers's avatar
Nate Summers committed
603
604
605
606
607
608
609
610
611
612
613
614
gimp_transform_tool_doit (GimpTransformTool  *gt_tool,
		          GDisplay           *gdisp)
{
  GimpTool      *tool;
  TileManager   *new_tiles;
  TransformUndo *tu;
  PathUndo      *pundo;
  gboolean       new_layer;
  gint           i, x, y;

  gimp_add_busy_cursors ();

615
  tool = GIMP_TOOL (gt_tool);
Nate Summers's avatar
Nate Summers committed
616
617

  /* undraw the tool before we muck around with the transform matrix */
618
  gimp_draw_tool_pause (GIMP_DRAW_TOOL (gt_tool));
Nate Summers's avatar
Nate Summers committed
619
620
621
622
623
624
625
626
627

  /*  We're going to dirty this image, but we want to keep the tool
   *  around
   */
  tool->preserve = TRUE;

  /*  Start a transform undo group  */
  undo_push_group_start (gdisp->gimage, TRANSFORM_CORE_UNDO);

628
629
630
  /* With the old UI, if original is NULL, then this is the
   * first transformation. In the new UI, it is always so, right?
   */
Nate Summers's avatar
Nate Summers committed
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
  g_assert (gt_tool->original == NULL);

  /* If we're in interactive mode, we need to copy the current
   *  selection to the transform tool's private selection pointer, so
   *  that the original source can be repeatedly modified.
   */
  tool->drawable = gimp_image_active_drawable (gdisp->gimage);

  gt_tool->original = gimp_transform_tool_cut (gdisp->gimage,
					       tool->drawable,
					       &new_layer);

  pundo = path_transform_start_undo (gdisp->gimage);

  /*  Send the request for the transformation to the tool...
   */
Nate Summers's avatar
Nate Summers committed
647
  new_tiles = gimp_transform_tool_transform (gt_tool, gdisp,
648
					     TRANSFORM_FINISH);
Nate Summers's avatar
Nate Summers committed
649

Nate Summers's avatar
Nate Summers committed
650
  gimp_transform_tool_transform (gt_tool, gdisp, TRANSFORM_INIT);
Nate Summers's avatar
Nate Summers committed
651
652
653
654
655
656
657
658
659
660

  gimp_transform_tool_recalc (gt_tool, gdisp);

  if (new_tiles)
    {
      /*  paste the new transformed image to the gimage...also implement
       *  undo...
       */
      /*  FIXME: we should check if the drawable is still valid  */
      gimp_transform_tool_paste (gdisp->gimage, tool->drawable,
661
				 new_tiles, new_layer);
Nate Summers's avatar
Nate Summers committed
662
663

      /*  create and initialize the transform_undo structure  */
664
665
666
      tu = g_new0 (TransformUndo, 1);
      tu->tool_ID   = tool->ID;
      tu->tool_type = GTK_OBJECT (tool)->klass->type;
Nate Summers's avatar
Nate Summers committed
667

Nate Summers's avatar
Nate Summers committed
668
669
      for (i = 0; i < TRAN_INFO_SIZE; i++)
	tu->trans_info[i] = old_trans_info[i];
670
671

      tu->original  = NULL;
Nate Summers's avatar
Nate Summers committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
      tu->path_undo = pundo;

      /*  Make a note of the new current drawable (since we may have
       *  a floating selection, etc now.
       */
      tool->drawable = gimp_image_active_drawable (gdisp->gimage);

      undo_push_transform (gdisp->gimage, (void *) tu);
    }

  /*  push the undo group end  */
  undo_push_group_end (gdisp->gimage);

  /*  We're done dirtying the image, and would like to be restarted
   *  if the image gets dirty while the tool exists
   */
  tool->preserve = FALSE;

  /*  Flush the gdisplays  */
  if (gdisp->disp_xoffset || gdisp->disp_yoffset)
    {
      gdk_window_get_size (gdisp->canvas->window, &x, &y);
      if (gdisp->disp_yoffset)
	{
	  gdisplay_expose_area (gdisp, 0, 0, gdisp->disp_width,
				gdisp->disp_yoffset);
	  gdisplay_expose_area (gdisp, 0, gdisp->disp_yoffset + y,
				gdisp->disp_width, gdisp->disp_height);
	}
      if (gdisp->disp_xoffset)
	{
	  gdisplay_expose_area (gdisp, 0, 0, gdisp->disp_xoffset,
				gdisp->disp_height);
	  gdisplay_expose_area (gdisp, gdisp->disp_xoffset + x, 0,
				gdisp->disp_width, gdisp->disp_height);
	}
    }

  gimp_remove_busy_cursors (NULL);

  gdisplays_flush ();

714
  gimp_transform_tool_reset (gt_tool, gdisp);
Nate Summers's avatar
Nate Summers committed
715
716

  /*  if this tool is non-interactive, make it inactive after use  */
Nate Summers's avatar
Nate Summers committed
717
  if (!gt_tool->interactive)
Nate Summers's avatar
Nate Summers committed
718
719
720
    tool->state = INACTIVE;
}

721
static void
Nate Summers's avatar
Nate Summers committed
722
723
724
gimp_transform_tool_motion (GimpTool          *tool,
		            GdkEventMotion    *mevent,
		            GDisplay          *gdisp)
Nate Summers's avatar
Nate Summers committed
725
{
Nate Summers's avatar
Nate Summers committed
726
  GimpTransformTool *tr_tool;
Nate Summers's avatar
Nate Summers committed
727

728
  tr_tool = GIMP_TRANSFORM_TOOL (tool);
Nate Summers's avatar
Nate Summers committed
729

730
  if (! tr_tool->bpressed)
Nate Summers's avatar
Nate Summers committed
731
732
733
734
735
    return;

  /*  if we are creating or this tool is non-interactive, there is
   *  nothing to be done so exit.
   */
Nate Summers's avatar
Nate Summers committed
736
737
  if (tr_tool->function == TRANSFORM_CREATING ||
      !tr_tool->interactive)
Nate Summers's avatar
Nate Summers committed
738
739
740
    return;

  /*  stop the current tool drawing process  */
741
  gimp_draw_tool_pause (GIMP_DRAW_TOOL (tool));
Nate Summers's avatar
Nate Summers committed
742
743

  gdisplay_untransform_coords (gdisp, mevent->x, mevent->y,
Nate Summers's avatar
Nate Summers committed
744
745
746
			       &tr_tool->curx,
			       &tr_tool->cury, TRUE, 0);
  tr_tool->state = mevent->state;
Nate Summers's avatar
Nate Summers committed
747
748

  /*  recalculate the tool's transformation matrix  */
Nate Summers's avatar
Nate Summers committed
749
  gimp_transform_tool_transform (tr_tool, gdisp, TRANSFORM_MOTION);
Nate Summers's avatar
Nate Summers committed
750

Nate Summers's avatar
Nate Summers committed
751
752
  tr_tool->lastx = tr_tool->curx;
  tr_tool->lasty = tr_tool->cury;
Nate Summers's avatar
Nate Summers committed
753
754

  /*  resume drawing the current tool  */
755
  gimp_draw_tool_resume (GIMP_DRAW_TOOL (tool));
Nate Summers's avatar
Nate Summers committed
756
757
}

758
static void
Nate Summers's avatar
Nate Summers committed
759
gimp_transform_tool_cursor_update (GimpTool           *tool,
Nate Summers's avatar
Nate Summers committed
760
761
			           GdkEventMotion     *mevent,
			           GDisplay           *gdisp)
Nate Summers's avatar
Nate Summers committed
762
{
Nate Summers's avatar
Nate Summers committed
763
764
765
766
  GimpTransformTool  *tr_tool;
  GimpDrawable       *drawable;
  GdkCursorType       ctype = GDK_TOP_LEFT_ARROW;
  gint                x, y;
Nate Summers's avatar
Nate Summers committed
767

768
  tr_tool = GIMP_TRANSFORM_TOOL (tool);
Nate Summers's avatar
Nate Summers committed
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798

  gdisplay_untransform_coords (gdisp, mevent->x, mevent->y, &x, &y,
			       FALSE, FALSE);

  if ((drawable = gimp_image_active_drawable (gdisp->gimage)))
    {
      if (GIMP_IS_LAYER (drawable) &&
	  gimp_layer_get_mask (GIMP_LAYER (drawable)))
	{
	  ctype = GIMP_BAD_CURSOR;
	}
      else if (x >= drawable->offset_x &&
	       y >= drawable->offset_y &&
	       x < (drawable->offset_x + drawable->width) &&
	       y < (drawable->offset_y + drawable->height))
	{
	  if (gimage_mask_is_empty (gdisp->gimage) ||
	      gimage_mask_value (gdisp->gimage, x, y))
	    {
	      ctype = GIMP_MOUSE_CURSOR;
	    }
	}
    }

  gdisplay_install_tool_cursor (gdisp,
				ctype,
				tool->tool_cursor,
				GIMP_CURSOR_MODIFIER_NONE);
}

799
800
801
802
static void
gimp_transform_tool_control (GimpTool   *tool,
			     ToolAction  action,
			     GDisplay   *gdisp)
Nate Summers's avatar
Nate Summers committed
803
{
804
805
  GimpDrawTool      *dr_tool;
  GimpTransformTool *tr_tool;
Nate Summers's avatar
Nate Summers committed
806

807
808
  dr_tool = GIMP_DRAW_TOOL (tool);
  tr_tool = GIMP_TRANSFORM_TOOL (tool);
Nate Summers's avatar
Nate Summers committed
809
810
811
812
813
814
815

  switch (action)
    {
    case PAUSE:
      break;

    case RESUME:
Nate Summers's avatar
Nate Summers committed
816
      gimp_transform_tool_recalc (tr_tool, gdisp);
Nate Summers's avatar
Nate Summers committed
817
818
819
      break;

    case HALT:
Nate Summers's avatar
Nate Summers committed
820
      gimp_transform_tool_reset (tr_tool, gdisp);
Nate Summers's avatar
Nate Summers committed
821
822
823
824
825
826
      break;

    default:
      break;
    }

827
828
  if (GIMP_TOOL_CLASS (parent_class)->control)
    GIMP_TOOL_CLASS (parent_class)->control (tool, action, gdisp);
Nate Summers's avatar
Nate Summers committed
829
830
}

831
static void
Nate Summers's avatar
Nate Summers committed
832
gimp_transform_tool_draw (GimpDrawTool *dr_tool)
Nate Summers's avatar
Nate Summers committed
833
{
Nate Summers's avatar
Nate Summers committed
834
835
836
837
838
839
840
841
  GDisplay           *gdisp;
  GimpTransformTool  *tr_tool;
  GimpTool           *tool;
  gint                x1, y1, x2, y2, x3, y3, x4, y4;
  gint                srw, srh;
  gint                i, k, gci;
  gint                xa, ya, xb, yb;

842
843
  tr_tool = GIMP_TRANSFORM_TOOL (dr_tool);
  tool    = GIMP_TOOL (dr_tool);
Nate Summers's avatar
Nate Summers committed
844

845
  gdisp   = tool->gdisp;
Nate Summers's avatar
Nate Summers committed
846
847
848
849
850
851
852
853
854
855
856
857
858
859

  gdisplay_transform_coords (gdisp, tr_tool->tx1, tr_tool->ty1,
			     &tr_tool->sx1, &tr_tool->sy1, FALSE);
  gdisplay_transform_coords (gdisp, tr_tool->tx2, tr_tool->ty2,
			     &tr_tool->sx2, &tr_tool->sy2, FALSE);
  gdisplay_transform_coords (gdisp, tr_tool->tx3, tr_tool->ty3,
			     &tr_tool->sx3, &tr_tool->sy3, FALSE);
  gdisplay_transform_coords (gdisp, tr_tool->tx4, tr_tool->ty4,
			     &tr_tool->sx4, &tr_tool->sy4, FALSE);

  x1 = tr_tool->sx1;  y1 = tr_tool->sy1;
  x2 = tr_tool->sx2;  y2 = tr_tool->sy2;
  x3 = tr_tool->sx3;  y3 = tr_tool->sy3;
  x4 = tr_tool->sx4;  y4 = tr_tool->sy4;
Nate Summers's avatar
Nate Summers committed
860
861
862
863
864
865

  /*  find the handles' width and height  */
  srw = 10;
  srh = 10;

  /*  draw the bounding box  */
Nate Summers's avatar
Nate Summers committed
866
  gdk_draw_line (dr_tool->win, dr_tool->gc,
Nate Summers's avatar
Nate Summers committed
867
		 x1, y1, x2, y2);
Nate Summers's avatar
Nate Summers committed
868
  gdk_draw_line (dr_tool->win, dr_tool->gc,
Nate Summers's avatar
Nate Summers committed
869
		 x2, y2, x4, y4);
Nate Summers's avatar
Nate Summers committed
870
  gdk_draw_line (dr_tool->win, dr_tool->gc,
Nate Summers's avatar
Nate Summers committed
871
		 x3, y3, x4, y4);
Nate Summers's avatar
Nate Summers committed
872
  gdk_draw_line (dr_tool->win, dr_tool->gc,
Nate Summers's avatar
Nate Summers committed
873
874
875
876
		 x3, y3, x1, y1);

  /*  Draw the grid */

Nate Summers's avatar
Nate Summers committed
877
  if ((tr_tool->grid_coords != NULL) &&
Nate Summers's avatar
Nate Summers committed
878
879
      (tr_tool->tgrid_coords != NULL) /* FIXME!!! this doesn't belong here &&
      ((tool->type != PERSPECTIVE)  ||
Nate Summers's avatar
Nate Summers committed
880
       ((tr_tool->transform[0][0] >=0.0) &&
Nate Summers's avatar
Nate Summers committed
881
	(tr_tool->transform[1][1] >=0.0)) */ ) 
Nate Summers's avatar
Nate Summers committed
882
883
    {
      gci = 0;
Nate Summers's avatar
Nate Summers committed
884
      k = tr_tool->ngx + tr_tool->ngy;
885

Nate Summers's avatar
Nate Summers committed
886
887
      for (i = 0; i < k; i++)
	{
Nate Summers's avatar
Nate Summers committed
888
889
	  gdisplay_transform_coords (gdisp, tr_tool->tgrid_coords[gci],
				     tr_tool->tgrid_coords[gci+1],
Nate Summers's avatar
Nate Summers committed
890
				     &xa, &ya, FALSE);
Nate Summers's avatar
Nate Summers committed
891
892
	  gdisplay_transform_coords (gdisp, tr_tool->tgrid_coords[gci+2],
				     tr_tool->tgrid_coords[gci+3],
Nate Summers's avatar
Nate Summers committed
893
894
				     &xb, &yb, FALSE);

Nate Summers's avatar
Nate Summers committed
895
	  gdk_draw_line (dr_tool->win, dr_tool->gc,
Nate Summers's avatar
Nate Summers committed
896
897
898
899
900
901
			 xa, ya, xb, yb);
	  gci += 4;
	}
    }

  /*  draw the tool handles  */
Nate Summers's avatar
Nate Summers committed
902
  gdk_draw_rectangle (dr_tool->win, dr_tool->gc, 0,
Nate Summers's avatar
Nate Summers committed
903
		      x1 - (srw >> 1), y1 - (srh >> 1), srw, srh);
Nate Summers's avatar
Nate Summers committed
904
  gdk_draw_rectangle (dr_tool->win, dr_tool->gc, 0,
Nate Summers's avatar
Nate Summers committed
905
		      x2 - (srw >> 1), y2 - (srh >> 1), srw, srh);
Nate Summers's avatar
Nate Summers committed
906
  gdk_draw_rectangle (dr_tool->win, dr_tool->gc, 0,
Nate Summers's avatar
Nate Summers committed
907
		      x3 - (srw >> 1), y3 - (srh >> 1), srw, srh);
Nate Summers's avatar
Nate Summers committed
908
  gdk_draw_rectangle (dr_tool->win, dr_tool->gc, 0,
Nate Summers's avatar
Nate Summers committed
909
910
911
		      x4 - (srw >> 1), y4 - (srh >> 1), srw, srh);

  /*  draw the center  */
912
913
914
  gdisplay_transform_coords (gdisp,
			     tr_tool->tcx, tr_tool->tcy,
			     &tr_tool->scx, &tr_tool->scy, FALSE);
Nate Summers's avatar
Nate Summers committed
915

916
917
918
919
920
921
  gdk_draw_arc (dr_tool->win,
		dr_tool->gc,
		1,
		tr_tool->scx - (srw >> 1),
		tr_tool->scy - (srh >> 1),
		srw, srh, 0, 23040);
Nate Summers's avatar
Nate Summers committed
922

923
  if (gimp_transform_tool_showpath ())
Nate Summers's avatar
Nate Summers committed
924
925
926
    {
      GimpMatrix3 tmp_matrix;

927
      if (gimp_transform_tool_direction () == TRANSFORM_CORRECTIVE)
Nate Summers's avatar
Nate Summers committed
928
	{
Nate Summers's avatar
Nate Summers committed
929
	  gimp_matrix3_invert (tr_tool->transform, tmp_matrix);
Nate Summers's avatar
Nate Summers committed
930
931
932
	}
      else
	{
Nate Summers's avatar
Nate Summers committed
933
	  gimp_matrix3_duplicate (tr_tool->transform, tmp_matrix);
Nate Summers's avatar
Nate Summers committed
934
935
	}

Nate Summers's avatar
Nate Summers committed
936
      path_transform_draw_current (gdisp, dr_tool, tmp_matrix);
Nate Summers's avatar
Nate Summers committed
937
938
939
940
    }
}

void
Nate Summers's avatar
Nate Summers committed
941
gimp_transform_tool_transform_bounding_box (GimpTransformTool *tr_tool)
Nate Summers's avatar
Nate Summers committed
942
{
Nate Summers's avatar
Nate Summers committed
943
944
945
946
  GimpTool  *tool;
  gint       i, k;
  gint       gci;

947
  tool = GIMP_TOOL (tr_tool);
Nate Summers's avatar
Nate Summers committed
948
949
950
951
952
953
954
955
956
957
958
959
960

  gimp_matrix3_transform_point (tr_tool->transform,
				tr_tool->x1, tr_tool->y1,
				&tr_tool->tx1, &tr_tool->ty1);
  gimp_matrix3_transform_point (tr_tool->transform,
				tr_tool->x2, tr_tool->y1,
				&tr_tool->tx2, &tr_tool->ty2);
  gimp_matrix3_transform_point (tr_tool->transform,
				tr_tool->x1, tr_tool->y2,
				&tr_tool->tx3, &tr_tool->ty3);
  gimp_matrix3_transform_point (tr_tool->transform,
				tr_tool->x2, tr_tool->y2,
				&tr_tool->tx4, &tr_tool->ty4);
Nate Summers's avatar
Nate Summers committed
961

962
963
964
  gimp_matrix3_transform_point (tr_tool->transform,
				tr_tool->cx, tr_tool->cy,
				&tr_tool->tcx, &tr_tool->tcy);
Nate Summers's avatar
Nate Summers committed
965

Nate Summers's avatar
Nate Summers committed
966
967
  if (tr_tool->grid_coords != NULL &&
      tr_tool->tgrid_coords != NULL)
Nate Summers's avatar
Nate Summers committed
968
969
    {
      gci = 0;
Nate Summers's avatar
Nate Summers committed
970
      k  = (tr_tool->ngx + tr_tool->ngy) * 2;
971

Nate Summers's avatar
Nate Summers committed
972
973
      for (i = 0; i < k; i++)
	{
Nate Summers's avatar
Nate Summers committed
974
975
976
977
978
	  gimp_matrix3_transform_point (tr_tool->transform,
					tr_tool->grid_coords[gci],
					tr_tool->grid_coords[gci+1],
					&(tr_tool->tgrid_coords[gci]),
					&(tr_tool->tgrid_coords[gci+1]));
Nate Summers's avatar
Nate Summers committed
979
980
981
982
983
984
	  gci += 2;
	}
    }
}

void
985
986
gimp_transform_tool_reset (GimpTransformTool *tr_tool,
		           GDisplay          *gdisp)
Nate Summers's avatar
Nate Summers committed
987
{
Nate Summers's avatar
Nate Summers committed
988
  GimpTool *tool;
Nate Summers's avatar
Nate Summers committed
989

990
  tool = GIMP_TOOL (tr_tool);
Nate Summers's avatar
Nate Summers committed
991

Nate Summers's avatar
Nate Summers committed
992
  if (tr_tool->original)
993
994
995
996
    {
      tile_manager_destroy (tr_tool->original);
      tr_tool->original = NULL;
    }
Nate Summers's avatar
Nate Summers committed
997
998

  /*  inactivate the tool  */
Nate Summers's avatar
Nate Summers committed
999
  tr_tool->function = TRANSFORM_CREATING;
1000
  gimp_draw_tool_stop (GIMP_DRAW_TOOL (tr_tool));
Nate Summers's avatar
Nate Summers committed
1001
1002
1003
1004
1005
1006
1007
1008
  info_dialog_popdown (transform_info);

  tool->state    = INACTIVE;
  tool->gdisp    = NULL;
  tool->drawable = NULL;
}

static void
1009
1010
gimp_transform_tool_bounds (GimpTransformTool *tr_tool,
		            GDisplay          *gdisp)
Nate Summers's avatar
Nate Summers committed
1011
1012
1013
1014
1015
{
  TileManager   *tiles;
  GimpDrawable  *drawable;
  gint           offset_x, offset_y;

1016
1017
  tiles    = tr_tool->original;
  drawable = gimp_image_active_drawable (gdisp->gimage);
Nate Summers's avatar
Nate Summers committed
1018
1019
1020
1021
1022

  /*  find the boundaries  */
  if (tiles)
    {
      tile_manager_get_offsets (tiles,
Nate Summers's avatar
Nate Summers committed
1023
1024
1025
1026
				&tr_tool->x1, &tr_tool->y1);
				
      tr_tool->x2 = tr_tool->x1 + tile_manager_width (tiles);
      tr_tool->y2 = tr_tool->y1 + tile_manager_height (tiles);
Nate Summers's avatar
Nate Summers committed
1027
1028
1029
1030
1031
    }
  else
    {
      gimp_drawable_offsets (drawable, &offset_x, &offset_y);
      gimp_drawable_mask_bounds (drawable,
Nate Summers's avatar
Nate Summers committed
1032
1033
1034
1035
1036
1037
				 &tr_tool->x1, &tr_tool->y1,
				 &tr_tool->x2, &tr_tool->y2);
      tr_tool->x1 += offset_x;
      tr_tool->y1 += offset_y;
      tr_tool->x2 += offset_x;
      tr_tool->y2 += offset_y;
Nate Summers's avatar
Nate Summers committed
1038
    }
Nate Summers's avatar
Nate Summers committed
1039
1040
1041

  tr_tool->cx = (tr_tool->x1 + tr_tool->x2) / 2;
  tr_tool->cy = (tr_tool->y1 + tr_tool->y2) / 2;
Nate Summers's avatar
Nate Summers committed
1042
1043

  /*  changing the bounds invalidates any grid we may have  */
Nate Summers's avatar
Nate Summers committed
1044
  gimp_transform_tool_grid_recalc (tr_tool);
Nate Summers's avatar
Nate Summers committed
1045
1046
1047
1048
1049
}

void
gimp_transform_tool_grid_density_changed (void)
{
Nate Summers's avatar
Nate Summers committed
1050
1051
  GimpTransformTool *tr_tool;
  GimpDrawTool      *dr_tool;
Nate Summers's avatar
Nate Summers committed
1052

1053
1054
  tr_tool = GIMP_TRANSFORM_TOOL ( /* EEEEEEEK!!! */ active_tool);
  dr_tool = GIMP_DRAW_TOOL (tr_tool);
Nate Summers's avatar
Nate Summers committed
1055

Nate Summers's avatar
Nate Summers committed
1056
  if (tr_tool->function == TRANSFORM_CREATING)
Nate Summers's avatar
Nate Summers committed
1057
1058
    return;

Nate Summers's avatar
Nate Summers committed
1059
  gimp_draw_tool_pause (dr_tool);
1060

Nate Summers's avatar
Nate Summers committed
1061
1062
  gimp_transform_tool_grid_recalc (tr_tool);
  gimp_transform_tool_transform_bounding_box (tr_tool);
1063

Nate Summers's avatar
Nate Summers committed
1064
  gimp_draw_tool_resume (dr_tool);
Nate Summers's avatar
Nate Summers committed
1065
1066
1067
}

void
Nate Summers's avatar
Nate Summers committed
1068
gimp_transform_tool_showpath_changed (gint type /* a truly undescriptive name */)
Nate Summers's avatar
Nate Summers committed
1069
{
Nate Summers's avatar
Nate Summers committed
1070
  GimpTransformTool *tr_tool;
Nate Summers's avatar
Nate Summers committed
1071

1072
  tr_tool = GIMP_TRANSFORM_TOOL ( /* EEEEEEEK!!! */ active_tool);
Nate Summers's avatar
Nate Summers committed
1073

Nate Summers's avatar
Nate Summers committed
1074
  if (tr_tool->function == TRANSFORM_CREATING)
Nate Summers's avatar
Nate Summers committed
1075
1076
1077
    return;

  if (type)
1078
    gimp_draw_tool_pause (GIMP_DRAW_TOOL (tr_tool));
Nate Summers's avatar
Nate Summers committed
1079
  else
1080
    gimp_draw_tool_resume (GIMP_DRAW_TOOL (tr_tool));
Nate Summers's avatar
Nate Summers committed
1081
1082
1083
}

static void
Nate Summers's avatar
Nate Summers committed
1084
gimp_transform_tool_grid_recalc (GimpTransformTool *tr_tool)
Nate Summers's avatar
Nate Summers committed
1085
{
Nate Summers's avatar
Nate Summers committed
1086
  if (tr_tool->grid_coords != NULL)
Nate Summers's avatar
Nate Summers committed
1087
    {
Nate Summers's avatar
Nate Summers committed
1088
1089
      g_free (tr_tool->grid_coords);
      tr_tool->grid_coords = NULL;
Nate Summers's avatar
Nate Summers committed
1090
    }
1091

Nate Summers's avatar
Nate Summers committed
1092
  if (tr_tool->tgrid_coords != NULL)
Nate Summers's avatar
Nate Summers committed
1093
    {
Nate Summers's avatar
Nate Summers committed
1094
1095
      g_free (tr_tool->tgrid_coords);
      tr_tool->tgrid_coords = NULL;
Nate Summers's avatar
Nate Summers committed
1096
    }
1097

1098
  if (gimp_transform_tool_show_grid ())
1099
    gimp_transform_tool_setup_grid (GIMP_TRANSFORM_TOOL ( /* EEEEEEK!!! */ active_tool));
Nate Summers's avatar
Nate Summers committed
1100
1101
1102
}

static void
Nate Summers's avatar
Nate Summers committed
1103
gimp_transform_tool_setup_grid (GimpTransformTool *tr_tool)
Nate Summers's avatar
Nate Summers committed
1104
{
1105
1106
1107
  GimpTool *tool;
  gint      i, gci;
  gdouble  *coords;
Nate Summers's avatar
Nate Summers committed
1108

1109
  tool = GIMP_TOOL (tr_tool);
Nate Summers's avatar
Nate Summers committed
1110

Nate Summers's avatar
Nate Summers committed
1111
  /*  We use the gimp_transform_tool_grid_size function only here, even
Nate Summers's avatar
Nate Summers committed
1112
1113
1114
   *  if the user changes the grid size in the middle of an
   *  operation, nothing happens.
   */
1115
  tr_tool->ngx = (tr_tool->x2 - tr_tool->x1) / gimp_transform_tool_grid_size ();
Nate Summers's avatar
Nate Summers committed
1116
1117
  if (tr_tool->ngx > 0)
    tr_tool->ngx--;
Nate Summers's avatar
Nate Summers committed
1118

1119
  tr_tool->ngy = (tr_tool->y2 - tr_tool->y1) / gimp_transform_tool_grid_size ();
Nate Summers's avatar
Nate Summers committed
1120
1121
  if (tr_tool->ngy > 0)
    tr_tool->ngy--;
Nate Summers's avatar
Nate Summers committed
1122

Nate Summers's avatar
Nate Summers committed
1123
  tr_tool->grid_coords = coords =
1124
    g_new (gdouble, (tr_tool->ngx + tr_tool->ngy) * 4);
Nate Summers's avatar
Nate Summers committed
1125

Nate Summers's avatar
Nate Summers committed
1126
  tr_tool->tgrid_coords =
1127
    g_new (gdouble, (tr_tool->ngx + tr_tool->ngy) * 4);
Nate Summers's avatar
Nate Summers committed
1128
1129

  gci = 0;
1130

Nate Summers's avatar
Nate Summers committed
1131
  for (i = 1; i <= tr_tool->ngx; i++)
Nate Summers's avatar
Nate Summers committed
1132
    {
Nate Summers's avatar
Nate Summers committed
1133
      coords[gci] = tr_tool->x1 +
1134
	((gdouble) i) / (tr_tool->ngx + 1) *
Nate Summers's avatar
Nate Summers committed
1135
1136
	(tr_tool->x2 - tr_tool->x1);
      coords[gci+1] = tr_tool->y1;
Nate Summers's avatar
Nate Summers committed
1137
      coords[gci+2] = coords[gci];
Nate Summers's avatar
Nate Summers committed
1138
      coords[gci+3] = tr_tool->y2;
Nate Summers's avatar
Nate Summers committed
1139
1140
      gci += 4;
    }
1141

Nate Summers's avatar
Nate Summers committed
1142
  for (i = 1; i <= tr_tool->ngy; i++)
Nate Summers's avatar
Nate Summers committed
1143
    {
Nate Summers's avatar
Nate Summers committed
1144
1145
      coords[gci] = tr_tool->x1;
      coords[gci+1] = tr_tool->y1 +
1146
	((gdouble) i) / (tr_tool->ngy + 1) *
Nate Summers's avatar
Nate Summers committed
1147
1148
	(tr_tool->y2 - tr_tool->y1);
      coords[gci+2] = tr_tool->x2;
Nate Summers's avatar
Nate Summers committed
1149
1150
1151
1152
1153
1154
      coords[gci+3] = coords[gci+1];
      gci += 4;
    }
}

static void
1155
1156
gimp_transform_tool_recalc (GimpTransformTool *tr_tool,
		            GDisplay          *gdisp)
Nate Summers's avatar
Nate Summers committed
1157
{
Nate Summers's avatar
Nate Summers committed
1158
  gimp_transform_tool_bounds (tr_tool, gdisp);
Nate Summers's avatar
Nate Summers committed
1159

1160
  gimp_transform_tool_transform (tr_tool, gdisp, TRANSFORM_RECALC);
Nate Summers's avatar
Nate Summers committed
1161
1162
1163
1164
}

/*  Actually carry out a transformation  */
TileManager *
1165
gimp_transform_tool_do (GimpImage        *gimage,
Nate Summers's avatar
Nate Summers committed
1166
1167
1168
1169
1170
1171
                        GimpDrawable     *drawable,
                        TileManager      *float_tiles,
                        gboolean          interpolation,
                        GimpMatrix3       matrix,
                        GimpProgressFunc  progress_callback,
                        gpointer          progress_data)
Nate Summers's avatar
Nate Summers committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
{
  PixelRegion  destPR;
  TileManager *tiles;
  GimpMatrix3  m;
  GimpMatrix3  im;
  gint         itx, ity;
  gint         tx1, ty1, tx2, ty2;
  gint         width, height;
  gint         alpha;
  gint         bytes, b;
  gint         x, y;
  gint         sx, sy;
  gint         x1, y1, x2, y2;
  gdouble      xinc, yinc, winc;
  gdouble      tx, ty, tw;
  gdouble      ttx = 0.0, tty = 0.0;
  guchar      *dest;
  guchar      *d;
  guchar      *src[16];
  Tile        *tile[16];
  guchar       bg_col[MAX_CHANNELS];
  gint         i;
  gdouble      a_val, a_recip;
  gint         newval;

  PixelSurround surround;

  alpha = 0;

  /*  turn interpolation off for simple transformations (e.g. rot90)  */
  if (gimp_matrix3_is_simple (matrix) ||
      interpolation_type == NEAREST_NEIGHBOR_INTERPOLATION)
    interpolation = FALSE;

  /*  Get the background color  */
  gimp_image_get_background (gimage, drawable, bg_col);

  switch (gimp_drawable_type (drawable))
    {
    case RGB_GIMAGE: case RGBA_GIMAGE:
      bg_col[ALPHA_PIX] = TRANSPARENT_OPACITY;
      alpha = ALPHA_PIX;
      break;
    case GRAY_GIMAGE: case GRAYA_GIMAGE:
      bg_col[ALPHA_G_PIX] = TRANSPARENT_OPACITY;
      alpha = ALPHA_G_PIX;
      break;
    case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
      bg_col[ALPHA_I_PIX] = TRANSPARENT_OPACITY;
      alpha = ALPHA_I_PIX;
      /*  If the gimage is indexed color, ignore smoothing value  */
      interpolation = FALSE;
      break;
    default:
      g_assert_not_reached ();
      break;
    }

  /*  enable rotating un-floated non-layers  */
  if (tile_manager_bpp (float_tiles) == 1)
    {
      bg_col[0] = OPAQUE_OPACITY;

      /*  setting alpha = 0 will cause the channel's value to be treated
       *  as alpha and the color channel loops never to be entered
       */
      alpha = 0;
    }

1241
  if (gimp_transform_tool_direction () == TRANSFORM_CORRECTIVE)
Nate Summers's avatar
Nate Summers committed
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
    {
      /*  keep the original matrix here, so we dont need to recalculate
	  the inverse later  */
      gimp_matrix3_duplicate (matrix, m);
      gimp_matrix3_invert (matrix, im);
      matrix = im;
    }
  else
    {
      /*  Find the inverse of the transformation matrix  */
      gimp_matrix3_invert (matrix, m);
    }

  path_transform_current_path (gimage, matrix, FALSE);

  tile_manager_get_offsets (float_tiles, &x1, &y1);
  x2 = x1 + tile_manager_width (float_tiles);
  y2 = y1 + tile_manager_height (float_tiles);

  /*  Find the bounding coordinates  */
1262
  if (alpha == 0 || (active_tool && gimp_transform_tool_clip ()))
Nate Summers's avatar
Nate Summers committed
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
    {
      tx1 = x1;
      ty1 = y1;
      tx2 = x2;
      ty2 = y2;
    }
  else
    {
      gdouble dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4;

      gimp_matrix3_transform_point (matrix, x1, y1, &dx1, &dy1);
      gimp_matrix3_transform_point (matrix, x2, y1, &dx2, &dy2);
      gimp_matrix3_transform_point (matrix, x1, y2, &dx3, &dy3);
      gimp_matrix3_transform_point (matrix, x2, y2, &dx4, &dy4);

      tx1 = MIN (dx1, dx2);
      tx1 = MIN (tx1, dx3);
      tx1 = MIN (tx1, dx4);
      ty1 = MIN (dy1, dy2);
      ty1 = MIN (ty1, dy3);
      ty1 = MIN (ty1, dy4);
      tx2 = MAX (dx1, dx2);
      tx2 = MAX (tx2, dx3);
      tx2 = MAX (tx2, dx4);
      ty2 = MAX (dy1, dy2);
      ty2 = MAX (ty2, dy3);
      ty2 = MAX (ty2, dy4);
    }

  /*  Get the new temporary buffer for the transformed result  */
  tiles = tile_manager_new ((tx2 - tx1), (ty2 - ty1),
			    tile_manager_bpp (float_tiles));
  pixel_region_init (&destPR, tiles, 0, 0, (tx2 - tx1), (ty2 - ty1), TRUE);
  tile_manager_set_offsets (tiles, tx1, ty1);

  /* initialise the pixel_surround accessor */
  if (interpolation)
    {
      if (interpolation_type == CUBIC_INTERPOLATION)
	{
	  pixel_surround_init (&surround, float_tiles, 4, 4, bg_col);
	}
      else
	{
	  pixel_surround_init (&surround, float_tiles, 2, 2, bg_col);
	}
    }
  else
    {
      /* not actually useful, keeps the code cleaner */
      pixel_surround_init (&surround, float_tiles, 1, 1, bg_col);
    }

  width  = tile_manager_width (tiles);
  height = tile_manager_height (tiles);
  bytes  = tile_manager_bpp (tiles);

  dest = g_new (guchar, width * bytes);

  xinc = m[0][0];
  yinc = m[1][0];
  winc = m[2][0];

  /* these loops could be rearranged, depending on which bit of code
   * you'd most like to write more than once.
   */

  for (y = ty1; y < ty2; y++)
    {
      if (progress_callback && !(y & 0xf))
	(* progress_callback) (ty1, ty2, y, progress_data);

      /* set up inverse transform steps */
      tx = xinc * tx1 + m[0][1] * y + m[0][2];
      ty = yinc * tx1 + m[1][1] * y + m[1][2];
      tw = winc * tx1 + m[2][1] * y + m[2][2];

      d = dest;
      for (x = tx1; x < tx2; x++)
	{
	  /*  normalize homogeneous coords  */
	  if (tw == 0.0)
	    {
	      g_warning ("homogeneous coordinate = 0...\n");
	    }
	  else if (tw != 1.0)
	    {
	      ttx = tx / tw;
	      tty = ty / tw;
	    }
	  else
	    {
	      ttx = tx;
	      tty = ty;
	    }

          /*  Set the destination pixels  */

          if (interpolation)
       	    {
              if (interpolation_type == CUBIC_INTERPOLATION)
       	        {
                  /*  ttx & tty are the subpixel coordinates of the point in
		   *  the original selection's floating buffer.
		   *  We need the four integer pixel coords around them:
		   *  itx to itx + 3, ity to ity + 3
                   */
                  itx = floor (ttx);
                  ity = floor (tty);

		  /* check if any part of our region overlaps the buffer */

                  if ((itx + 2) >= x1 && (itx - 1) < x2 &&
                      (ity + 2) >= y1 && (ity - 1) < y2 )
                    {
                      guchar  *data;
                      gint     row;
                      gdouble  dx, dy;
                      guchar  *start;

		      /* lock the pixel surround */
                      data = pixel_surround_lock (&surround,
						  itx - 1 - x1, ity - 1 - y1);

                      row = pixel_surround_rowstride (&surround);

                      /* the fractional error */
                      dx = ttx - itx;
                      dy = tty - ity;

		      /* calculate alpha of result */
		      start = &data[alpha];
1395
1396
1397
1398
1399
1400
		      a_val = gimp_transform_tool_cubic
			(dy,
			 CUBIC_ROW (dx, start, bytes),
			 CUBIC_ROW (dx, start + row, bytes),
			 CUBIC_ROW (dx, start + row + row, bytes),
			 CUBIC_ROW (dx, start + row + row + row, bytes