gimpimage-resize.c 4.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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.
 */
18

19 20
#include "config.h"

21
#include <glib-object.h>
Sven Neumann's avatar
Sven Neumann committed
22

Michael Natterer's avatar
Michael Natterer committed
23 24
#include "core-types.h"

25
#include "gimp.h"
26
#include "gimpimage.h"
27
#include "gimpimage-guides.h"
28
#include "gimpimage-mask.h"
29
#include "gimpimage-projection.h"
30
#include "gimpimage-resize.h"
31
#include "gimpimage-undo.h"
32
#include "gimpimage-undo-push.h"
33
#include "gimplayer.h"
Michael Natterer's avatar
Michael Natterer committed
34
#include "gimplayer-floating-sel.h"
35
#include "gimplist.h"
36

37
#include "gimp-intl.h"
38

39

40
void
41
gimp_image_resize (GimpImage *gimage, 
42 43 44 45
		   gint       new_width, 
		   gint       new_height,
		   gint       offset_x, 
		   gint       offset_y)
46
{
47 48
  GimpLayer *floating_layer;
  GList     *list;
49

50 51
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
  g_return_if_fail (new_width > 0 && new_height > 0);
52

53
  gimp_set_busy (gimage->gimp);
54 55 56 57

  /*  Get the floating layer if one exists  */
  floating_layer = gimp_image_floating_sel (gimage);

58 59
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_RESIZE,
                               _("Resize Image"));
60 61 62 63 64 65

  /*  Relax the floating selection  */
  if (floating_layer)
    floating_sel_relax (floating_layer, TRUE);

  /*  Push the image size to the stack  */
66
  gimp_image_undo_push_image_size (gimage, NULL);
67 68

  /*  Set the new width and height  */
69
  gimage->width  = new_width;
70 71 72
  gimage->height = new_height;

  /*  Resize all channels  */
73 74
  for (list = GIMP_LIST (gimage->channels)->list;
       list;
75
       list = g_list_next (list))
76
    {
77
      GimpItem *item = list->data;
78

79
      gimp_item_resize (item, new_width, new_height, offset_x, offset_y);
80 81
    }

82
  /*  Resize all vectors  */
83 84
  for (list = GIMP_LIST (gimage->vectors)->list;
       list;
85 86 87 88 89 90 91
       list = g_list_next (list))
    {
      GimpItem *item = list->data;

      gimp_item_resize (item, new_width, new_height, offset_x, offset_y);
    }

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  /*  Don't forget the selection mask!  */
  gimp_item_resize (GIMP_ITEM (gimage->selection_mask),
                    new_width, new_height, offset_x, offset_y);
  gimp_image_mask_invalidate (gimage);

  /*  Reposition all layers  */
  for (list = GIMP_LIST (gimage->layers)->list;
       list;
       list = g_list_next (list))
    {
      GimpItem *item = list->data;

      gimp_item_translate (item, offset_x, offset_y, TRUE);
    }

  /*  Reposition or remove all guides  */
108 109
  list = gimage->guides;
  while (list)
110
    {
111 112
      GimpGuide *guide        = list->data;
      gboolean   remove_guide = FALSE;
113
      gint       new_position = guide->position;
114

115
      list = g_list_next (list);
116 117 118

      switch (guide->orientation)
	{
119
	case GIMP_ORIENTATION_HORIZONTAL:
120
          new_position += offset_y;
121 122
	  if (new_position < 0 || new_position > new_height)
            remove_guide = TRUE;
123
	  break;
124

125
	case GIMP_ORIENTATION_VERTICAL:
126
          new_position += offset_x;
127 128
	  if (new_position < 0 || new_position > new_width)
            remove_guide = TRUE;
129
	  break;
130

131
	default:
132
          break;
133
	}
134 135 136

      if (remove_guide)
        gimp_image_remove_guide (gimage, guide, TRUE);
137
      else if (new_position != guide->position)
138
        gimp_image_move_guide (gimage, guide, new_position, TRUE);
139 140 141
    }

  /*  Make sure the projection matches the gimage size  */
142
  gimp_image_projection_allocate (gimage);
143 144 145 146 147

  /*  Rigor the floating selection  */
  if (floating_layer)
    floating_sel_rigor (floating_layer, TRUE);

148
  gimp_image_undo_group_end (gimage);
149

150
  gimp_viewable_size_changed (GIMP_VIEWABLE (gimage));
151

152 153
  gimp_image_mask_changed (gimage);

154
  gimp_unset_busy (gimage->gimp);
155
}