gimp-transform-region.c 25.9 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
 * 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"

Michael Natterer's avatar
Michael Natterer committed
21
#include <glib-object.h>
Nate Summers's avatar
Nate Summers committed
22 23 24

#include "libgimpmath/gimpmath.h"

Michael Natterer's avatar
Michael Natterer committed
25
#include "core-types.h"
Nate Summers's avatar
Nate Summers committed
26

Michael Natterer's avatar
Michael Natterer committed
27 28 29 30 31
#include "base/pixel-region.h"
#include "base/pixel-surround.h"
#include "base/tile-manager.h"
#include "base/tile.h"

32 33
#include "paint-funcs/paint-funcs.h"

Michael Natterer's avatar
Michael Natterer committed
34 35 36 37 38 39 40
#include "gimp.h"
#include "gimpchannel.h"
#include "gimpcontext.h"
#include "gimpdrawable.h"
#include "gimpdrawable-transform.h"
#include "gimpimage.h"
#include "gimpimage-mask.h"
41
#include "gimpimage-undo.h"
Michael Natterer's avatar
Michael Natterer committed
42
#include "gimplayer.h"
Michael Natterer's avatar
Michael Natterer committed
43
#include "gimplayer-floating-sel.h"
Michael Natterer's avatar
Michael Natterer committed
44

Michael Natterer's avatar
Michael Natterer committed
45
#include "undo.h"
Nate Summers's avatar
Nate Summers committed
46 47 48

#include "libgimp/gimpintl.h"

49

Nate Summers's avatar
Nate Summers committed
50 51 52 53 54 55
#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) \
Michael Natterer's avatar
Michael Natterer committed
56
  gimp_drawable_transform_cubic(dx, (row)[0], (row)[step], (row)[step+step], (row)[step+step+step])
Nate Summers's avatar
Nate Summers committed
57
#define CUBIC_SCALED_ROW(dx, row, step, i) \
Michael Natterer's avatar
Michael Natterer committed
58
  gimp_drawable_transform_cubic(dx, (row)[0] * (row)[i], \
Nate Summers's avatar
Nate Summers committed
59 60 61 62 63 64 65 66
            (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);

67

Michael Natterer's avatar
Michael Natterer committed
68
/*  forward function prototypes  */
Nate Summers's avatar
Nate Summers committed
69

Michael Natterer's avatar
Michael Natterer committed
70 71 72 73 74
static gdouble   gimp_drawable_transform_cubic (gdouble dx,
					        gint    jm1,
					        gint    j,
					        gint    jp1,
					        gint    jp2);
Nate Summers's avatar
Nate Summers committed
75 76


Michael Natterer's avatar
Michael Natterer committed
77
/*  public functions  */
Nate Summers's avatar
Nate Summers committed
78

Nate Summers's avatar
Nate Summers committed
79
TileManager *
Michael Natterer's avatar
Michael Natterer committed
80 81
gimp_drawable_transform_tiles_affine (GimpDrawable           *drawable,
                                      TileManager            *float_tiles,
82
                                      GimpInterpolationType   interpolation_type,
Michael Natterer's avatar
Michael Natterer committed
83 84 85 86 87
                                      gboolean                clip_result,
                                      GimpMatrix3             matrix,
                                      GimpTransformDirection  direction,
                                      GimpProgressFunc        progress_callback,
                                      gpointer                progress_data)
Nate Summers's avatar
Nate Summers committed
88
{
Michael Natterer's avatar
Michael Natterer committed
89
  GimpImage   *gimage;
Nate Summers's avatar
Nate Summers committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  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;

Michael Natterer's avatar
Michael Natterer committed
116 117 118
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (float_tiles != NULL, NULL);

119
  gimage = gimp_item_get_image (GIMP_ITEM (drawable));
Michael Natterer's avatar
Michael Natterer committed
120 121 122

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);

Nate Summers's avatar
Nate Summers committed
123 124 125
  alpha = 0;

  /*  turn interpolation off for simple transformations (e.g. rot90)  */
126
  if (gimp_matrix3_is_simple (matrix))
127
    interpolation_type = GIMP_INTERPOLATION_NONE;
Nate Summers's avatar
Nate Summers committed
128 129 130 131

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

132
  switch (GIMP_IMAGE_TYPE_BASE_TYPE (gimp_drawable_type (drawable)))
Nate Summers's avatar
Nate Summers committed
133
    {
134
    case GIMP_RGB:
Nate Summers's avatar
Nate Summers committed
135 136 137
      bg_col[ALPHA_PIX] = TRANSPARENT_OPACITY;
      alpha = ALPHA_PIX;
      break;
138
    case GIMP_GRAY:
Nate Summers's avatar
Nate Summers committed
139 140 141
      bg_col[ALPHA_G_PIX] = TRANSPARENT_OPACITY;
      alpha = ALPHA_G_PIX;
      break;
142
    case GIMP_INDEXED:
Nate Summers's avatar
Nate Summers committed
143 144
      bg_col[ALPHA_I_PIX] = TRANSPARENT_OPACITY;
      alpha = ALPHA_I_PIX;
145 146
      /*  If the gimage is indexed color, ignore interpolation value  */
      interpolation_type = GIMP_INTERPOLATION_NONE;
Nate Summers's avatar
Nate Summers committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
      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;
    }

Michael Natterer's avatar
Michael Natterer committed
164
  if (direction == GIMP_TRANSFORM_BACKWARD)
Nate Summers's avatar
Nate Summers committed
165 166
    {
      /*  keep the original matrix here, so we dont need to recalculate
Michael Natterer's avatar
Michael Natterer committed
167 168
       *  the inverse later
       */
Nate Summers's avatar
Nate Summers committed
169 170 171 172 173 174 175 176 177 178
      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);
    }

Michael Natterer's avatar
Michael Natterer committed
179 180 181 182
#ifdef __GNUC__
#warning FIXME: path_transform_current_path
#endif
#if 0
Nate Summers's avatar
Nate Summers committed
183
  path_transform_current_path (gimage, matrix, FALSE);
Michael Natterer's avatar
Michael Natterer committed
184
#endif
Nate Summers's avatar
Nate Summers committed
185 186 187 188 189 190

  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  */
Michael Natterer's avatar
Michael Natterer committed
191
  if (alpha == 0 || clip_result)
Nate Summers's avatar
Nate Summers committed
192 193 194 195 196 197 198 199
    {
      tx1 = x1;
      ty1 = y1;
      tx2 = x2;
      ty2 = y2;
    }
  else
    {
200 201 202 203
      gdouble dx1, dy1;
      gdouble dx2, dy2;
      gdouble dx3, dy3;
      gdouble dx4, dy4;
Nate Summers's avatar
Nate Summers committed
204 205 206 207 208 209

      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);

210 211 212 213 214 215 216 217 218 219 220
#define MIN4(a,b,c,d) MIN(MIN(MIN(a,b),c),d)
#define MAX4(a,b,c,d) MAX(MAX(MAX(a,b),c),d)

      tx1 = ROUND (MIN4 (dx1, dx2, dx3, dx4));
      ty1 = ROUND (MIN4 (dy1, dy2, dy3, dy4));

      tx2 = ROUND (MAX4 (dx1, dx2, dx3, dx4));
      ty2 = ROUND (MAX4 (dy1, dy2, dy3, dy4));

#undef MIN2
#undef MAX4
Nate Summers's avatar
Nate Summers committed
221 222 223 224 225 226 227 228 229
    }

  /*  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 */
230
  switch (interpolation_type)
Nate Summers's avatar
Nate Summers committed
231
    {
232 233 234
    case GIMP_INTERPOLATION_NONE:
      /* not actually useful, keeps the code cleaner */
      pixel_surround_init (&surround, float_tiles, 1, 1, bg_col);
235 236
      break;

237 238
    case GIMP_INTERPOLATION_CUBIC:
      pixel_surround_init (&surround, float_tiles, 4, 4, bg_col);
239 240
      break;

241 242
    case GIMP_INTERPOLATION_LINEAR:
      pixel_surround_init (&surround, float_tiles, 2, 2, bg_col);
243
      break;
Nate Summers's avatar
Nate Summers committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    }

  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 */
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
      if (interpolation_type == GIMP_INTERPOLATION_NONE)
        {
          /*  need to transform the pixel's center for INTERPOLATION_NONE,
           *  as we end up at discrete pixel positions and are not aware of
           *  errors in the algorithm below
           */
          tx = xinc * (tx1 + 0.5) + m[0][1] * (y + 0.5) + m[0][2];
          ty = yinc * (tx1 + 0.5) + m[1][1] * (y + 0.5) + m[1][2];
          tw = winc * (tx1 + 0.5) + m[2][1] * (y + 0.5) + m[2][2];
        }
      else
        {
          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];
        }
Nate Summers's avatar
Nate Summers committed
282 283

      d = dest;
284

Nate Summers's avatar
Nate Summers committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
      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  */

305 306
          switch (interpolation_type)
            {
307
            case GIMP_INTERPOLATION_CUBIC:
308 309 310 311 312 313 314
              /*  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);
Nate Summers's avatar
Nate Summers committed
315

316
              /* check if any part of our region overlaps the buffer */
Nate Summers's avatar
Nate Summers committed
317

318
              if ((itx + 2) >= x1 && (itx - 1) < x2 &&
319
                  (ity + 2) >= y1 && (ity - 1) < y2)
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
                {
                  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];
                  a_val = gimp_drawable_transform_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));

                  if (a_val <= 0.0)
                    {
                      a_recip = 0.0;
                      d[alpha] = 0;
                    }
                  else if (a_val > 255.0)
Nate Summers's avatar
Nate Summers committed
351
                    {
352 353 354 355
                      a_recip = 1.0 / a_val;
                      d[alpha] = 255;
                    }
                  else
Nate Summers's avatar
Nate Summers committed
356
                    {
357 358
                      a_recip = 1.0 / a_val;
                      d[alpha] = RINT(a_val);
Nate Summers's avatar
Nate Summers committed
359
                    }
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

                  /*  for colour channels c,
                   *  result = bicubic (c * alpha) / bicubic (alpha)
                   *
                   *  never entered for alpha == 0
                   */
                  for (i = -alpha; i < 0; ++i)
                    {
                      start = &data[alpha];
                      newval =
                        RINT (a_recip *
                              gimp_drawable_transform_cubic
                              (dy,
                               CUBIC_SCALED_ROW (dx, start, bytes, i),
                               CUBIC_SCALED_ROW (dx, start + row, bytes, i),
                               CUBIC_SCALED_ROW (dx, start + row + row, bytes, i),
                               CUBIC_SCALED_ROW (dx, start + row + row + row, bytes, i)));
                      if (newval <= 0)
                        {
                          *d++ = 0;
                        }
                      else if (newval > 255)
                        {
                          *d++ = 255;
                        }
                      else
                        {
                          *d++ = newval;
                        }
                    }

                  /*  alpha already done  */
                  d++;

                  pixel_surround_release (&surround);
                }
              else /* not in source range */
                {
                  /*  increment the destination pointers  */
                  for (b = 0; b < bytes; b++)
                    *d++ = bg_col[b];
Nate Summers's avatar
Nate Summers committed
401
                }
402 403
              break;

404
            case GIMP_INTERPOLATION_LINEAR:
405 406
              itx = floor (ttx);
              ity = floor (tty);
Nate Summers's avatar
Nate Summers committed
407

408 409 410 411
              /*  expand source area to cover interpolation region
               *  (which runs from itx to itx + 1, same in y)
               */
              if ((itx + 1) >= x1 && itx < x2 &&
412
                  (ity + 1) >= y1 && ity < y2)
Nate Summers's avatar
Nate Summers committed
413
                {
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
                  guchar  *data;
                  gint     row;
                  double   dx, dy;
                  guchar  *chan;

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

                  row = pixel_surround_rowstride (&surround);

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

                  /* calculate alpha value of result pixel */
                  chan = &data[alpha];
                  a_val = BILINEAR (chan[0], chan[bytes], chan[row],
                                    chan[row+bytes], dx, dy);
                  if (a_val <= 0.0)
Nate Summers's avatar
Nate Summers committed
433
                    {
434 435 436 437
                      a_recip = 0.0;
                      d[alpha] = 0.0;
                    }
                  else if (a_val >= 255.0)
Nate Summers's avatar
Nate Summers committed
438
                    {
439 440
                      a_recip = 1.0 / a_val;
                      d[alpha] = 255;
Nate Summers's avatar
Nate Summers committed
441
                    }
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 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
                  else
                    {
                      a_recip = 1.0 / a_val;
                      d[alpha] = RINT (a_val);
                    }

                  /*  for colour channels c,
                   *  result = bilinear (c * alpha) / bilinear (alpha)
                   *
                   *  never entered for alpha == 0
                   */
                  for (i = -alpha; i < 0; ++i)
                    {
                      chan = &data[alpha];
                      newval =
                        RINT (a_recip *
                              BILINEAR (chan[0] * chan[i],
                                        chan[bytes] * chan[bytes+i],
                                        chan[row] * chan[row+i],
                                        chan[row+bytes] * chan[row+bytes+i],
                                        dx, dy));
                      if (newval <= 0)
                        {
                          *d++ = 0;
                        }
                      else if (newval > 255)
                        {
                          *d++ = 255;
                        }
                      else
                        {
                          *d++ = newval;
                        }
                    }

                  /*  alpha already done  */
                  d++;

                  pixel_surround_release (&surround);
                }
              else /* not in source range */
                {
                  /*  increment the destination pointers  */
                  for (b = 0; b < bytes; b++)
                    *d++ = bg_col[b];
                }
              break;

490
            case GIMP_INTERPOLATION_NONE:
Nate Summers's avatar
Nate Summers committed
491 492 493 494
              itx = floor (ttx);
              ity = floor (tty);

              if (itx >= x1 && itx < x2 &&
495
                  ity >= y1 && ity < y2)
Nate Summers's avatar
Nate Summers committed
496 497 498 499 500 501 502 503 504 505 506
                {
                  /*  x, y coordinates into source tiles  */
                  sx = itx - x1;
                  sy = ity - y1;

                  REF_TILE (0, sx, sy);

                  for (b = 0; b < bytes; b++)
                    *d++ = src[0][b];

                  tile_release (tile[0], FALSE);
507
                }
Nate Summers's avatar
Nate Summers committed
508 509 510 511 512 513
              else /* not in source range */
                {
                  /*  increment the destination pointers  */
                  for (b = 0; b < bytes; b++)
                    *d++ = bg_col[b];
                }
514 515
              break;
            }
516

Nate Summers's avatar
Nate Summers committed
517 518 519 520 521 522 523 524 525 526 527 528 529
	  /*  increment the transformed coordinates  */
	  tx += xinc;
	  ty += yinc;
	  tw += winc;
	}

      /*  set the pixel region row  */
      pixel_region_set_row (&destPR, 0, (y - ty1), width, dest);
    }

  pixel_surround_clear (&surround);

  g_free (dest);
530

Nate Summers's avatar
Nate Summers committed
531 532 533 534
  return tiles;
}

TileManager *
535 536 537
gimp_drawable_transform_tiles_flip (GimpDrawable        *drawable,
                                    TileManager         *orig,
                                    GimpOrientationType  flip_type)
Nate Summers's avatar
Nate Summers committed
538
{
Michael Natterer's avatar
Michael Natterer committed
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
  TileManager *new;
  PixelRegion  srcPR, destPR;
  gint         orig_width;
  gint         orig_height;
  gint         orig_bpp;
  gint         orig_x, orig_y;
  gint         i;

  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (orig != NULL, NULL);

  orig_width  = tile_manager_width (orig);
  orig_height = tile_manager_height (orig);
  orig_bpp    = tile_manager_bpp (orig);
  tile_manager_get_offsets (orig, &orig_x, &orig_y);

  new = tile_manager_new (orig_width, orig_height, orig_bpp);
  tile_manager_set_offsets (new, orig_x, orig_y);

558
  if (flip_type == GIMP_ORIENTATION_HORIZONTAL)
Michael Natterer's avatar
Michael Natterer committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    {
      for (i = 0; i < orig_width; i++)
        {
          pixel_region_init (&srcPR, orig, i, 0, 1, orig_height, FALSE);
          pixel_region_init (&destPR, new,
                             (orig_width - i - 1), 0, 1, orig_height, TRUE);
          copy_region (&srcPR, &destPR); 
        }
    }
  else
    {
      for (i = 0; i < orig_height; i++)
        {
          pixel_region_init (&srcPR, orig, 0, i, orig_width, 1, FALSE);
          pixel_region_init (&destPR, new,
                             0, (orig_height - i - 1), orig_width, 1, TRUE);
          copy_region (&srcPR, &destPR);
        }
    }

#ifdef __GNUC__
#warning FIXME: path_transform_flip_horz/vert
#endif
#if 0
  /* flip locked paths */
  /* Note that the undo structures etc are setup before we enter this
   * function.
   */
587
  if (flip_type == GIMP_ORIENTATION_HORIZONTAL)
Michael Natterer's avatar
Michael Natterer committed
588 589 590 591 592 593 594 595 596 597
    path_transform_flip_horz (gimage);
  else
    path_transform_flip_vert (gimage);
#endif

  return new;
}

gboolean
gimp_drawable_transform_affine (GimpDrawable           *drawable,
598
                                GimpInterpolationType   interpolation_type,
Michael Natterer's avatar
Michael Natterer committed
599 600 601 602 603 604 605 606 607 608 609
                                gboolean                clip_result,
                                GimpMatrix3             matrix,
                                GimpTransformDirection  direction)
{
  GimpImage   *gimage;
  TileManager *float_tiles;
  gboolean     new_layer;
  gboolean     success = FALSE;

  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);

610
  gimage = gimp_item_get_image (GIMP_ITEM (drawable));
Michael Natterer's avatar
Michael Natterer committed
611 612 613 614

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);

  /* Start a transform undo group */
615 616
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_TRANSFORM,
                               _("Transform"));
Michael Natterer's avatar
Michael Natterer committed
617 618 619 620 621 622 623 624 625 626 627

  /* Cut/Copy from the specified drawable */
  float_tiles = gimp_drawable_transform_cut (drawable, &new_layer);

  if (float_tiles)
    {
      TileManager *new_tiles;

      /* transform the buffer */
      new_tiles = gimp_drawable_transform_tiles_affine (drawable,
                                                        float_tiles,
628
                                                        interpolation_type, 
Michael Natterer's avatar
Michael Natterer committed
629 630 631 632 633 634 635 636 637 638 639 640 641
                                                        FALSE,
                                                        matrix,
                                                        GIMP_TRANSFORM_FORWARD,
                                                        NULL, NULL);

      /* Free the cut/copied buffer */
      tile_manager_destroy (float_tiles);

      if (new_tiles)
        success = gimp_drawable_transform_paste (drawable, new_tiles, new_layer);
    }

  /*  push the undo group end  */
642
  gimp_image_undo_group_end (gimage);
Michael Natterer's avatar
Michael Natterer committed
643 644 645 646 647

  return success;
}

gboolean
648 649
gimp_drawable_transform_flip (GimpDrawable        *drawable,
                              GimpOrientationType  flip_type)
Michael Natterer's avatar
Michael Natterer committed
650 651 652 653 654 655 656 657
{
  GimpImage   *gimage;
  TileManager *float_tiles;
  gboolean     new_layer;
  gboolean     success = FALSE;

  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);

658
  gimage = gimp_item_get_image (GIMP_ITEM (drawable));
Michael Natterer's avatar
Michael Natterer committed
659 660 661 662

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);

  /* Start a transform undo group */
663 664
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_TRANSFORM,
                               _("Flip"));
Michael Natterer's avatar
Michael Natterer committed
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685

  /* Cut/Copy from the specified drawable */
  float_tiles = gimp_drawable_transform_cut (drawable, &new_layer);

  if (float_tiles)
    {
      TileManager *new_tiles;

      /* transform the buffer */
      new_tiles = gimp_drawable_transform_tiles_flip (drawable,
                                                      float_tiles,
                                                      flip_type);

      /* Free the cut/copied buffer */
      tile_manager_destroy (float_tiles);

      if (new_tiles)
        success = gimp_drawable_transform_paste (drawable, new_tiles, new_layer);
    }

  /*  push the undo group end  */
686
  gimp_image_undo_group_end (gimage);
Michael Natterer's avatar
Michael Natterer committed
687 688 689 690 691 692 693 694 695

  return success;
}

TileManager *
gimp_drawable_transform_cut (GimpDrawable *drawable,
                             gboolean     *new_layer)
{
  GimpImage   *gimage;
Nate Summers's avatar
Nate Summers committed
696 697
  TileManager *tiles;

Michael Natterer's avatar
Michael Natterer committed
698 699 700
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
  g_return_val_if_fail (new_layer != NULL, NULL);

701
  gimage = gimp_item_get_image (GIMP_ITEM (drawable));
Michael Natterer's avatar
Michael Natterer committed
702 703 704

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);

Nate Summers's avatar
Nate Summers committed
705
  /*  extract the selected mask if there is a selection  */
706
  if (! gimp_image_mask_is_empty (gimage))
Nate Summers's avatar
Nate Summers committed
707 708
    {
      /* set the keep_indexed flag to FALSE here, since we use
Michael Natterer's avatar
Michael Natterer committed
709 710
       * gimp_layer_new_from_tiles() later which assumes that the tiles
       * are either RGB or GRAY.  Eeek!!!              (Sven)
Nate Summers's avatar
Nate Summers committed
711
       */
712
      tiles = gimp_image_mask_extract (gimage, drawable, TRUE, FALSE, TRUE);
Michael Natterer's avatar
Michael Natterer committed
713

Nate Summers's avatar
Nate Summers committed
714 715 716 717 718 719
      *new_layer = TRUE;
    }
  /*  otherwise, just copy the layer  */
  else
    {
      if (GIMP_IS_LAYER (drawable))
720
	tiles = gimp_image_mask_extract (gimage, drawable, FALSE, TRUE, TRUE);
Nate Summers's avatar
Nate Summers committed
721
      else
722
	tiles = gimp_image_mask_extract (gimage, drawable, FALSE, TRUE, FALSE);
Michael Natterer's avatar
Michael Natterer committed
723

Nate Summers's avatar
Nate Summers committed
724 725 726 727 728 729 730
      *new_layer = FALSE;
    }

  return tiles;
}

gboolean
Michael Natterer's avatar
Michael Natterer committed
731 732 733
gimp_drawable_transform_paste (GimpDrawable *drawable,
                               TileManager  *tiles,
                               gboolean      new_layer)
Nate Summers's avatar
Nate Summers committed
734
{
Michael Natterer's avatar
Michael Natterer committed
735
  GimpImage   *gimage;
Nate Summers's avatar
Nate Summers committed
736 737 738 739
  GimpLayer   *layer   = NULL;
  GimpChannel *channel = NULL;
  GimpLayer   *floating_layer;

Michael Natterer's avatar
Michael Natterer committed
740 741 742
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
  g_return_val_if_fail (tiles != NULL, FALSE);

743
  gimage = gimp_item_get_image (GIMP_ITEM (drawable));
Michael Natterer's avatar
Michael Natterer committed
744 745 746

  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);

Nate Summers's avatar
Nate Summers committed
747 748 749
  if (new_layer)
    {
      layer =
750 751
	gimp_layer_new_from_tiles (tiles,
                                   gimage,
752
                                   gimp_drawable_type_with_alpha (drawable),
Nate Summers's avatar
Nate Summers committed
753
				   _("Transformation"),
754
				   GIMP_OPACITY_OPAQUE, GIMP_NORMAL_MODE);
Nate Summers's avatar
Nate Summers committed
755 756
      if (! layer)
        {
757 758
          g_warning ("%s: gimp_layer_new_frome_tiles() failed",
		     G_GNUC_FUNCTION);
Nate Summers's avatar
Nate Summers committed
759 760 761 762 763 764 765 766
          return FALSE;
        }

      tile_manager_get_offsets (tiles, 
				&(GIMP_DRAWABLE (layer)->offset_x),
				&(GIMP_DRAWABLE (layer)->offset_y));

      /*  Start a group undo  */
767 768
      gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_EDIT_PASTE,
                                   _("Paste Transform"));
Nate Summers's avatar
Nate Summers committed
769 770 771 772

      floating_sel_attach (layer, drawable);

      /*  End the group undo  */
773
      gimp_image_undo_group_end (gimage);
Nate Summers's avatar
Nate Summers committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796

      /*  Free the tiles  */
      tile_manager_destroy (tiles);

      return TRUE;
    }
  else
    {
      if (GIMP_IS_LAYER (drawable))
	layer = GIMP_LAYER (drawable);
      else if (GIMP_IS_CHANNEL (drawable))
	channel = GIMP_CHANNEL (drawable);
      else
	return FALSE;

      if (layer)
	gimp_layer_add_alpha (layer);

      floating_layer = gimp_image_floating_sel (gimage);

      if (floating_layer)
	floating_sel_relax (floating_layer, TRUE);

Michael Natterer's avatar
Michael Natterer committed
797 798 799 800 801
      gimp_image_update (gimage,
                         drawable->offset_x,
                         drawable->offset_y,
                         drawable->width,
                         drawable->height);
Nate Summers's avatar
Nate Summers committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

      /*  Push an undo  */
      if (layer)
	undo_push_layer_mod (gimage, layer);
      else if (channel)
	undo_push_channel_mod (gimage, channel);

      /*  set the current layer's data  */
      drawable->tiles = tiles;

      /*  Fill in the new layer's attributes  */
      drawable->width    = tile_manager_width (tiles);
      drawable->height   = tile_manager_height (tiles);
      drawable->bytes    = tile_manager_bpp (tiles);
      tile_manager_get_offsets (tiles, 
				&drawable->offset_x, &drawable->offset_y);

      if (floating_layer)
	floating_sel_rigor (floating_layer, TRUE);

Michael Natterer's avatar
Michael Natterer committed
822 823 824 825
      gimp_drawable_update (drawable,
			    0, 0,
			    gimp_drawable_width (drawable),
			    gimp_drawable_height (drawable));
Nate Summers's avatar
Nate Summers committed
826 827 828 829 830 831 832 833 834 835 836 837 838

      /*  if we were operating on the floating selection, then it's boundary 
       *  and previews need invalidating
       */
      if (drawable == (GimpDrawable *) floating_layer)
	floating_sel_invalidate (floating_layer);

      return TRUE;
    }
}

/* Note: cubic function no longer clips result */
static gdouble
Michael Natterer's avatar
Michael Natterer committed
839 840 841 842 843
gimp_drawable_transform_cubic (gdouble dx,
                               gint    jm1,
                               gint    j,
                               gint    jp1,
                               gint    jp2)
Nate Summers's avatar
Nate Summers committed
844 845 846 847 848 849 850 851 852 853 854 855
{
  gdouble result;

#if 0
  /* Equivalent to Gimp 1.1.1 and earlier - some ringing */
  result = ((( ( - jm1 + j - jp1 + jp2 ) * dx +
               ( jm1 + jm1 - j - j + jp1 - jp2 ) ) * dx +
               ( - jm1 + jp1 ) ) * dx + j );
  /* Recommended by Mitchell and Netravali - too blurred? */
  result = ((( ( - 7 * jm1 + 21 * j - 21 * jp1 + 7 * jp2 ) * dx +
               ( 15 * jm1 - 36 * j + 27 * jp1 - 6 * jp2 ) ) * dx +
               ( - 9 * jm1 + 9 * jp1 ) ) * dx + (jm1 + 16 * j + jp1) ) / 18.0;
856
#endif
Nate Summers's avatar
Nate Summers committed
857 858 859 860 861 862 863 864

  /* Catmull-Rom - not bad */
  result = ((( ( - jm1 + 3 * j - 3 * jp1 + jp2 ) * dx +
               ( 2 * jm1 - 5 * j + 4 * jp1 - jp2 ) ) * dx +
               ( - jm1 + jp1 ) ) * dx + (j + j) ) / 2.0;

  return result;
}