gimpimage-resize.c 4.95 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 42 43 44 45 46 47
gimp_image_resize (GimpImage        *gimage, 
		   gint              new_width, 
		   gint              new_height,
		   gint              offset_x, 
		   gint              offset_y,
                   GimpProgressFunc  progress_func,
                   gpointer          progress_data)
48
{
49 50
  GimpLayer *floating_layer;
  GList     *list;
51 52
  gint       progress_max;
  gint       progress_current = 1;
53

54 55
  g_return_if_fail (GIMP_IS_IMAGE (gimage));
  g_return_if_fail (new_width > 0 && new_height > 0);
56

57
  gimp_set_busy (gimage->gimp);
58

59 60 61 62 63
  progress_max = (gimage->channels->num_children +
                  gimage->layers->num_children   +
                  gimage->vectors->num_children  +
                  1 /* selection */);

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

67 68
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_RESIZE,
                               _("Resize Image"));
69 70 71 72 73 74

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

  /*  Push the image size to the stack  */
75
  gimp_image_undo_push_image_size (gimage, NULL);
76 77

  /*  Set the new width and height  */
78
  gimage->width  = new_width;
79 80 81
  gimage->height = new_height;

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

88
      gimp_item_resize (item, new_width, new_height, offset_x, offset_y);
89 90 91

      if (progress_func)
        (* progress_func) (0, progress_max, progress_current++, progress_data);
92 93
    }

94
  /*  Resize all vectors  */
95 96
  for (list = GIMP_LIST (gimage->vectors)->list;
       list;
97 98 99 100 101
       list = g_list_next (list))
    {
      GimpItem *item = list->data;

      gimp_item_resize (item, new_width, new_height, offset_x, offset_y);
102 103 104

      if (progress_func)
        (* progress_func) (0, progress_max, progress_current++, progress_data);
105 106
    }

107 108 109 110 111
  /*  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);

112 113 114
  if (progress_func)
    (* progress_func) (0, progress_max, progress_current++, progress_data);

115 116 117 118 119 120 121 122
  /*  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);
123 124 125

      if (progress_func)
        (* progress_func) (0, progress_max, progress_current++, progress_data);
126 127 128
    }

  /*  Reposition or remove all guides  */
129 130
  list = gimage->guides;
  while (list)
131
    {
132 133
      GimpGuide *guide        = list->data;
      gboolean   remove_guide = FALSE;
134
      gint       new_position = guide->position;
135

136
      list = g_list_next (list);
137 138 139

      switch (guide->orientation)
	{
140
	case GIMP_ORIENTATION_HORIZONTAL:
141
          new_position += offset_y;
142 143
	  if (new_position < 0 || new_position > new_height)
            remove_guide = TRUE;
144
	  break;
145

146
	case GIMP_ORIENTATION_VERTICAL:
147
          new_position += offset_x;
148 149
	  if (new_position < 0 || new_position > new_width)
            remove_guide = TRUE;
150
	  break;
151

152
	default:
153
          break;
154
	}
155 156 157

      if (remove_guide)
        gimp_image_remove_guide (gimage, guide, TRUE);
158
      else if (new_position != guide->position)
159
        gimp_image_move_guide (gimage, guide, new_position, TRUE);
160 161 162
    }

  /*  Make sure the projection matches the gimage size  */
163
  gimp_image_projection_allocate (gimage);
164 165 166 167 168

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

169
  gimp_image_undo_group_end (gimage);
170

171
  gimp_viewable_size_changed (GIMP_VIEWABLE (gimage));
172

173 174
  gimp_image_mask_changed (gimage);

175
  gimp_unset_busy (gimage->gimp);
176
}