gimpimage-resize.c 4.22 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 "gimpchannel.h"
27
#include "gimpimage.h"
28
#include "gimpimage-guides.h"
29
#include "gimpimage-mask.h"
30
#include "gimpimage-projection.h"
31
#include "gimpimage-resize.h"
32
#include "gimpimage-undo.h"
33
#include "gimpimage-undo-push.h"
34
#include "gimplayer.h"
Michael Natterer's avatar
Michael Natterer committed
35
#include "gimplayer-floating-sel.h"
36
#include "gimplist.h"
37

38
#include "gimp-intl.h"
39

40

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

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

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

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

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

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

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

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

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

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

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

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

93
  /*  Reposition or remove any guides  */
94 95
  list = gimage->guides;
  while (list)
96
    {
97 98
      GimpGuide *guide        = list->data;
      gboolean   remove_guide = FALSE;
Michael Natterer's avatar
Michael Natterer committed
99
      gint       new_position = 0;
100

101
      list = g_list_next (list);
102 103 104

      switch (guide->orientation)
	{
105
	case GIMP_ORIENTATION_HORIZONTAL:
106
          new_position = guide->position + offset_y;
107 108
	  if (new_position < 0 || new_position > new_height)
            remove_guide = TRUE;
109
	  break;
110

111
	case GIMP_ORIENTATION_VERTICAL:
112
          new_position = guide->position + offset_x;
113 114
	  if (new_position < 0 || new_position > new_width)
            remove_guide = TRUE;
115
	  break;
116

117
	default:
118
	  g_error ("Unknown guide orientation\n");
119
	}
120 121 122 123 124

      if (remove_guide)
        gimp_image_remove_guide (gimage, guide, TRUE);
      else
        gimp_image_move_guide (gimage, guide, new_position, TRUE);
125 126 127
    }

  /*  Don't forget the selection mask!  */
128 129
  gimp_item_resize (GIMP_ITEM (gimage->selection_mask),
                    new_width, new_height, offset_x, offset_y);
130
  gimp_image_mask_invalidate (gimage);
131 132

  /*  Reposition all layers  */
133 134 135
  for (list = GIMP_LIST (gimage->layers)->list; 
       list; 
       list = g_list_next (list))
136
    {
137
      GimpLayer *layer = list->data;
138

139
      gimp_item_translate (GIMP_ITEM (layer), offset_x, offset_y, TRUE);
140 141 142
    }

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

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

149
  gimp_image_undo_group_end (gimage);
150

151
  gimp_viewable_size_changed (GIMP_VIEWABLE (gimage));
152

153 154
  gimp_image_mask_changed (gimage);

155
  gimp_unset_busy (gimage->gimp);
156
}