gimpimage-crop.c 14 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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 <string.h>

23
#include <glib-object.h>
24 25 26 27 28

#include "core-types.h"

#include "base/pixel-region.h"

29
#include "gimp.h"
30 31 32 33
#include "gimpchannel.h"
#include "gimpdrawable.h"
#include "gimpimage.h"
#include "gimpimage-crop.h"
34
#include "gimpimage-guides.h"
35
#include "gimpimage-mask.h"
36
#include "gimpimage-projection.h"
37
#include "gimpimage-undo.h"
38
#include "gimpimage-undo-push.h"
39
#include "gimplayer.h"
Michael Natterer's avatar
Michael Natterer committed
40
#include "gimplayer-floating-sel.h"
41 42
#include "gimplist.h"

43
#include "gimp-intl.h"
44

45 46 47 48 49 50 51 52 53

typedef enum
{
  AUTO_CROP_NOTHING = 0,
  AUTO_CROP_ALPHA   = 1,
  AUTO_CROP_COLOR   = 2
} AutoCropType;


54
typedef guchar       * (* GetColorFunc)    (GObject     *crop_object,
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
					    gint         ,
					    gint         );
typedef AutoCropType   (* ColorsEqualFunc) (guchar      *,
					    guchar      *,
					    gint         );


/*  local function prototypes  */

static void           gimp_image_crop_adjust_guides (GimpImage    *gimage,
						     gint          x1,
						     gint          y1,
						     gint          x2,
						     gint          y2);

70
static AutoCropType   gimp_image_crop_guess_bgcolor (GObject      *get_color_obj,
71 72 73 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 107 108 109 110 111 112 113 114
						     GetColorFunc  get_color_func,
						     gint          bytes,
						     gboolean      has_alpha,
						     guchar       *color,
						     gint          x1, 
						     gint          x2, 
						     gint          y1, 
						     gint          y2);
static gint           gimp_image_crop_colors_equal  (guchar       *col1,
						     guchar       *col2,
						     gint          bytes);
static gint           gimp_image_crop_colors_alpha  (guchar       *col1,
						     guchar       *col2,
						     gint          bytes);


/*  public functions  */

void
gimp_image_crop (GimpImage *gimage,
		 gint       x1,
		 gint       y1,
		 gint       x2,
		 gint       y2,
		 gboolean   active_layer_only,
		 gboolean   crop_layers)
{
  GimpLayer   *layer;
  GimpLayer   *floating_layer;
  GimpChannel *channel;
  GList       *list;
  gint         width, height;
  gint         lx1, ly1, lx2, ly2;
  gint         off_x, off_y;

  g_return_if_fail (gimage != NULL);
  g_return_if_fail (GIMP_IS_IMAGE (gimage));

  width  = x2 - x1;
  height = y2 - y1;

  /*  Make sure new width and height are non-zero  */
  if (width && height)
    {
115
      gimp_set_busy (gimage->gimp);
116 117 118

      if (active_layer_only)
	{
119
	  gint doff_x, doff_y;
120 121 122 123 124 125 126 127

	  layer = gimp_image_get_active_layer (gimage);

	  gimp_drawable_offsets (GIMP_DRAWABLE (layer), &doff_x, &doff_y);

	  off_x = (doff_x - x1);
	  off_y = (doff_y - y1);

128 129
          gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_LAYER_RESIZE,
                                       _("Resize Layer"));
130 131 132 133

	  if (gimp_layer_is_floating_sel (layer))
	    floating_sel_relax (layer, TRUE);

134
	  gimp_item_resize (GIMP_ITEM (layer), width, height, off_x, off_y);
135 136 137 138

	  if (gimp_layer_is_floating_sel (layer))
	    floating_sel_rigor (layer, TRUE);

139
	  gimp_image_undo_group_end (gimage);
140 141 142 143 144
	}
      else
	{
	  floating_layer = gimp_image_floating_sel (gimage);

145 146
	  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_CROP,
                                       _("Crop Image"));
147 148 149 150 151 152

	  /*  relax the floating layer  */
	  if (floating_layer)
	    floating_sel_relax (floating_layer, TRUE);

	  /*  Push the image size to the stack  */
153
	  gimp_image_undo_push_image_size (gimage, NULL);
154 155 156 157 158 159 160 161 162 163 164 165

	  /*  Set the new width and height  */
	  gimage->width  = width;
	  gimage->height = height;

	  /*  Resize all channels  */
	  for (list = GIMP_LIST (gimage->channels)->list;
	       list;
	       list = g_list_next (list))
	    {
	      channel = (GimpChannel *) list->data;

166
	      gimp_item_resize (GIMP_ITEM (channel), width, height, -x1, -y1);
167 168 169
	    }

	  /*  Don't forget the selection mask!  */
170 171
	  gimp_item_resize (GIMP_ITEM (gimage->selection_mask),
                            width, height, -x1, -y1);
172
	  gimp_image_mask_invalidate (gimage);
173 174 175 176 177 178 179 180 181 182 183 184

	  /*  crop all layers  */
	  list = GIMP_LIST (gimage->layers)->list;

	  while (list)
	    {
	      GList *next;

	      layer = (GimpLayer *) list->data;

	      next = g_list_next (list);

185
	      gimp_layer_translate (layer, -x1, -y1, TRUE);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

	      gimp_drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);

	      if (crop_layers)
		{
		  gimp_drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);

		  lx1 = CLAMP (off_x, 0, gimage->width);
		  ly1 = CLAMP (off_y, 0, gimage->height);
		  lx2 = CLAMP ((gimp_drawable_width (GIMP_DRAWABLE (layer)) + off_x),
			       0, gimage->width);
		  ly2 = CLAMP ((gimp_drawable_height (GIMP_DRAWABLE (layer)) + off_y),
			       0, gimage->height);
		  width = lx2 - lx1;
		  height = ly2 - ly1;

		  if (width && height)
203 204 205
		    gimp_item_resize (GIMP_ITEM (layer), width, height,
                                      -(lx1 - off_x),
                                      -(ly1 - off_y));
206 207 208 209 210 211 212 213
		  else
		    gimp_image_remove_layer (gimage, layer);
		}

	      list = next;
	    }

	  /*  Make sure the projection matches the gimage size  */
214
	  gimp_image_projection_allocate (gimage);
215 216 217 218 219 220 221

	  /*  rigor the floating layer  */
	  if (floating_layer)
	    floating_sel_rigor (floating_layer, TRUE);

	  /* Adjust any guides we might have laying about */
	  gimp_image_crop_adjust_guides (gimage, x1, y1, x2, y2);
222 223 224

	  gimp_image_undo_group_end (gimage);
  	}
225

226 227 228 229 230
      gimp_image_update (gimage,
                         0, 0,
                         gimage->width,
                         gimage->height);

231 232
      gimp_viewable_size_changed (GIMP_VIEWABLE (gimage));

233 234
      gimp_image_mask_changed (gimage);

235
      gimp_unset_busy (gimage->gimp);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    }
}

gboolean
gimp_image_crop_auto_shrink (GimpImage *gimage,
			     gint       x1,
			     gint       y1,
			     gint       x2,
			     gint       y2,
			     gboolean   active_drawable_only,
			     gint      *shrunk_x1,
			     gint      *shrunk_y1,
			     gint      *shrunk_x2,
			     gint      *shrunk_y2)
{
  GimpDrawable    *active_drawable = NULL;
  GetColorFunc     get_color_func;
  ColorsEqualFunc  colors_equal_func;
254
  GObject         *get_color_obj;
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  guchar           bgcolor[MAX_CHANNELS] = { 0, 0, 0, 0 };
  gboolean         has_alpha;
  PixelRegion      PR;
  guchar          *buffer = NULL;
  gint             width, height;
  gint             bytes;
  gint             x, y, abort;
  gboolean         retval = FALSE;
  
  g_return_val_if_fail (gimage != NULL, FALSE);
  g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  g_return_val_if_fail (shrunk_x1 != NULL, FALSE);
  g_return_val_if_fail (shrunk_y1 != NULL, FALSE);
  g_return_val_if_fail (shrunk_x2 != NULL, FALSE);
  g_return_val_if_fail (shrunk_y2 != NULL, FALSE);

271
  gimp_set_busy (gimage->gimp);
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
 
  /* You should always keep in mind that crop->tx2 and crop->ty2 are the NOT the
     coordinates of the bottomright corner of the area to be cropped. They point 
     at the pixel located one to the right and one to the bottom.  
   */

  if (active_drawable_only)
    {
      active_drawable = gimp_image_active_drawable (gimage);

      if (! active_drawable)
	goto FINISH;

      bytes          = gimp_drawable_bytes (GIMP_DRAWABLE (active_drawable));
      has_alpha      = gimp_drawable_has_alpha (GIMP_DRAWABLE (active_drawable));
287
      get_color_obj  = G_OBJECT (active_drawable);
288 289 290 291 292
      get_color_func = (GetColorFunc) gimp_drawable_get_color_at;
    }
  else
    {
      has_alpha      = TRUE; 
293
      bytes          = gimp_image_projection_bytes (gimage);
294
      get_color_obj  = G_OBJECT (gimage);
295
      get_color_func = (GetColorFunc) gimp_image_projection_get_color_at;
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
   }

  switch (gimp_image_crop_guess_bgcolor (get_color_obj, get_color_func,
					 bytes, has_alpha, bgcolor,
					 x1, x2-1, y1, y2-1))
    {
    case AUTO_CROP_ALPHA:
      colors_equal_func = (ColorsEqualFunc) gimp_image_crop_colors_alpha;
      break;
    case AUTO_CROP_COLOR:
      colors_equal_func = (ColorsEqualFunc) gimp_image_crop_colors_equal;
      break;
    default:
      goto FINISH;
      break;
    }

  width  = x2 - x1;
  height = y2 - y1;

  if (active_drawable_only)
    pixel_region_init (&PR, gimp_drawable_data (active_drawable), 
		       x1, y1, width, height, FALSE);
  else
320
    pixel_region_init (&PR, gimp_image_projection (gimage), 
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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
		       x1, y1, width, height, FALSE);

  /* The following could be optimized further by processing
   * the smaller side first instead of defaulting to width    --Sven
   */

  buffer = g_malloc ((width > height ? width : height) * bytes);

  /* Check how many of the top lines are uniform/transparent. */
  abort = FALSE;
  for (y = y1; y < y2 && !abort; y++)
    {
      pixel_region_get_row (&PR, x1, y, width, buffer, 1);
      for (x = 0; x < width && !abort; x++)
	abort = !(colors_equal_func) (bgcolor, buffer + x * bytes, bytes);
    }
  if (y == y2 && !abort)
    goto FINISH;
  y1 = y - 1;

  /* Check how many of the bottom lines are uniform/transparent. */
  abort = FALSE;
  for (y = y2; y > y1 && !abort; y--)
    {
      pixel_region_get_row (&PR, x1, y-1 , width, buffer, 1);
      for (x = 0; x < width && !abort; x++)
	abort = !(colors_equal_func) (bgcolor, buffer + x * bytes, bytes);
    }
  y2 = y + 1;

  /* compute a new height for the next operations */
  height = y2 - y1;

  /* Check how many of the left lines are uniform/transparent. */
  abort = FALSE;
  for (x = x1; x < x2 && !abort; x++)
    {
      pixel_region_get_col (&PR, x, y1, height, buffer, 1);
      for (y = 0; y < height && !abort; y++)
	abort = !(colors_equal_func) (bgcolor, buffer + y * bytes, bytes);
    }
  x1 = x - 1;
 
  /* Check how many of the right lines are uniform/transparent. */
  abort = FALSE;
  for (x = x2; x > x1 && !abort; x--)
    {
      pixel_region_get_col (&PR, x-1, y1, height, buffer, 1);
      for (y = 0; y < height && !abort; y++)
	abort = !(colors_equal_func) (bgcolor, buffer + y * bytes, bytes);
    }
  x2 = x + 1;

  *shrunk_x1 = x1;
  *shrunk_y1 = y1;
  *shrunk_x2 = x2;
  *shrunk_y2 = y2;

  retval = TRUE;

 FINISH:
  g_free (buffer);
383
  gimp_unset_busy (gimage->gimp);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

  return retval;
}


/*  private functions  */

static void
gimp_image_crop_adjust_guides (GimpImage *gimage,
			       gint       x1,
			       gint       y1,
			       gint       x2,
			       gint       y2)

{
399
  GList *list;
400

401 402
  list = gimage->guides;
  while (list)
403
    {
404 405
      GimpGuide *guide        = list->data;
      gboolean   remove_guide = FALSE;
406

407
      list = g_list_next (list);
408 409 410

      switch (guide->orientation)
	{
411
	case GIMP_ORIENTATION_HORIZONTAL:
412 413 414 415
	  if ((guide->position < y1) || (guide->position > y2))
	    remove_guide = TRUE;
	  break;

416
	case GIMP_ORIENTATION_VERTICAL:
417 418 419 420 421 422 423 424 425 426
	  if ((guide->position < x1) || (guide->position > x2))
	    remove_guide = TRUE;
	  break;

	default:
	  break;
	}

      if (remove_guide)
	{
427
          gimp_image_remove_guide (gimage, guide, TRUE);
428 429 430
	}
      else
	{
431 432 433 434 435 436 437 438 439 440 441 442 443
          switch (guide->orientation)
            {
            case GIMP_ORIENTATION_HORIZONTAL:
              gimp_image_move_guide (gimage, guide, guide->position - y1, TRUE);
              break;

            case GIMP_ORIENTATION_VERTICAL:
              gimp_image_move_guide (gimage, guide, guide->position - x1, TRUE);
              break;

            default:
              break;
            }
444 445 446 447 448
	}
    }
}

static AutoCropType
449
gimp_image_crop_guess_bgcolor (GObject      *get_color_obj,
450 451 452 453 454 455 456 457 458 459 460 461 462
			       GetColorFunc  get_color_func,
			       gint          bytes,
			       gboolean      has_alpha,
			       guchar       *color,
			       gint          x1, 
			       gint          x2, 
			       gint          y1, 
			       gint          y2) 
{
  guchar *tl = NULL;
  guchar *tr = NULL;
  guchar *bl = NULL;
  guchar *br = NULL;
463
  gint    i, alpha;
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 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

  for (i = 0; i < bytes; i++)
    color[i] = 0; 

  /* First check if there's transparency to crop. If not, guess the 
   * background-color to see if at least 2 corners are equal.
   */

  if (!(tl = (*get_color_func) (get_color_obj, x1, y1))) 
    goto ERROR;
  if (!(tr = (*get_color_func) (get_color_obj, x1, y2))) 
    goto ERROR;
  if (!(bl = (*get_color_func) (get_color_obj, x2, y1))) 
    goto ERROR;  
  if (!(br = (*get_color_func) (get_color_obj, x2, y2))) 
    goto ERROR;

  if (has_alpha)
    {
      alpha = bytes - 1;
      if ((tl[alpha] == 0 && tr[alpha] == 0) ||
	  (tl[alpha] == 0 && bl[alpha] == 0) ||
	  (tr[alpha] == 0 && br[alpha] == 0) ||
	  (bl[alpha] == 0 && br[alpha] == 0))
	{
	  g_free (tl);
	  g_free (tr);
	  g_free (bl);
	  g_free (br);
	  return AUTO_CROP_ALPHA;
	}
    }
  
  if (gimp_image_crop_colors_equal (tl, tr, bytes) ||
      gimp_image_crop_colors_equal (tl, bl, bytes))
    {
      memcpy (color, tl, bytes);
    }
  else if (gimp_image_crop_colors_equal (br, bl, bytes) ||
	   gimp_image_crop_colors_equal (br, tr, bytes))
    {
      memcpy (color, br, bytes);
    }
  else
    {
      goto ERROR;
    }

  g_free (tl); 
  g_free (tr); 
  g_free (bl); 
  g_free (br);
  return AUTO_CROP_COLOR;

 ERROR:
  g_free (tl); 
  g_free (tr); 
  g_free (bl); 
  g_free (br);
  return AUTO_CROP_NOTHING;
}

static int 
gimp_image_crop_colors_equal (guchar *col1, 
			      guchar *col2, 
			      gint    bytes) 
{
  gboolean equal = TRUE;
  gint     b;
  
  for (b = 0; b < bytes; b++)
    {
      if (col1[b] != col2[b])
	{
	  equal = FALSE;
	  break;
	}
    }

  return equal;
}

static gboolean
gimp_image_crop_colors_alpha (guchar *dummy, 
			      guchar *col, 
			      gint    bytes) 
{
  if (col[bytes-1] == 0)
    return TRUE;
  else
    return FALSE;
}