gimpimage-resize.c 3.96 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
      GimpChannel *channel = list->data;
79

Michael Natterer's avatar
Michael Natterer committed
80
      gimp_channel_resize (channel, new_width, new_height, offset_x, offset_y);
81 82 83
    }

  /*  Reposition or remove any guides  */
84 85
  list = gimage->guides;
  while (list)
86
    {
87 88
      GimpGuide *guide        = list->data;
      gboolean   remove_guide = FALSE;
Michael Natterer's avatar
Michael Natterer committed
89
      gint       new_position = 0;
90

91
      list = g_list_next (list);
92 93 94

      switch (guide->orientation)
	{
95
	case GIMP_ORIENTATION_HORIZONTAL:
96
          new_position = guide->position + offset_y;
97 98
	  if (new_position < 0 || new_position > new_height)
            remove_guide = TRUE;
99
	  break;
100

101
	case GIMP_ORIENTATION_VERTICAL:
102
          new_position = guide->position + offset_x;
103 104
	  if (new_position < 0 || new_position > new_width)
            remove_guide = TRUE;
105
	  break;
106

107
	default:
108
	  g_error ("Unknown guide orientation\n");
109
	}
110 111 112 113 114

      if (remove_guide)
        gimp_image_remove_guide (gimage, guide, TRUE);
      else
        gimp_image_move_guide (gimage, guide, new_position, TRUE);
115 116 117
    }

  /*  Don't forget the selection mask!  */
Michael Natterer's avatar
Michael Natterer committed
118 119
  gimp_channel_resize (gimage->selection_mask,
		       new_width, new_height, offset_x, offset_y);
120
  gimp_image_mask_invalidate (gimage);
121 122

  /*  Reposition all layers  */
123 124 125
  for (list = GIMP_LIST (gimage->layers)->list; 
       list; 
       list = g_list_next (list))
126
    {
127
      GimpLayer *layer = list->data;
128

129
      gimp_layer_translate (layer, offset_x, offset_y, TRUE);
130 131 132
    }

  /*  Make sure the projection matches the gimage size  */
133
  gimp_image_projection_allocate (gimage);
134 135 136 137 138

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

139
  gimp_image_undo_group_end (gimage);
140

141
  gimp_viewable_size_changed (GIMP_VIEWABLE (gimage));
142

143 144
  gimp_image_mask_changed (gimage);

145
  gimp_unset_busy (gimage->gimp);
146
}