gimpimage-resize.c 3.97 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 39
#include "libgimp/gimpintl.h"

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
{
Michael Natterer's avatar
Michael Natterer committed
48 49 50
  GimpChannel *channel;
  GimpLayer   *layer;
  GimpLayer   *floating_layer;
51
  GList       *list;
Michael Natterer's avatar
Michael Natterer committed
52
  GList       *guide_list;
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

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

62 63
  gimp_image_undo_group_start (gimage, GIMP_UNDO_GROUP_IMAGE_RESIZE,
                               _("Resize Image"));
64 65 66 67 68 69

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

  /*  Push the image size to the stack  */
70
  gimp_image_undo_push_image_size (gimage, NULL);
71 72

  /*  Set the new width and height  */
73
  gimage->width  = new_width;
74 75 76
  gimage->height = new_height;

  /*  Resize all channels  */
77 78 79
  for (list = GIMP_LIST (gimage->channels)->list; 
       list; 
       list = g_list_next (list))
80
    {
Michael Natterer's avatar
Michael Natterer committed
81
      channel = (GimpChannel *) list->data;
82

Michael Natterer's avatar
Michael Natterer committed
83
      gimp_channel_resize (channel, new_width, new_height, offset_x, offset_y);
84 85 86 87 88 89
    }

  /*  Reposition or remove any guides  */
  guide_list = gimage->guides;
  while (guide_list)
    {
Michael Natterer's avatar
Michael Natterer committed
90
      GimpGuide *guide;
91

Michael Natterer's avatar
Michael Natterer committed
92
      guide = (GimpGuide *) guide_list->data;
Sven Neumann's avatar
Sven Neumann committed
93
      guide_list = g_list_next (guide_list);
94 95 96

      switch (guide->orientation)
	{
97
	case GIMP_ORIENTATION_HORIZONTAL:
98
	  gimp_image_undo_push_image_guide (gimage, NULL, guide);
99 100 101 102
	  guide->position += offset_y;
	  if (guide->position < 0 || guide->position > new_height)
	    gimp_image_delete_guide (gimage, guide);
	  break;
103

104
	case GIMP_ORIENTATION_VERTICAL:
105
	  gimp_image_undo_push_image_guide (gimage, NULL, guide);
106 107 108 109
	  guide->position += offset_x;
	  if (guide->position < 0 || guide->position > new_width)
	    gimp_image_delete_guide (gimage, guide);
	  break;
110

111
	default:
112
	  g_error ("Unknown guide orientation\n");
113
	}
114 115 116
    }

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

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

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

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

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

138
  gimp_image_undo_group_end (gimage);
139

140
  gimp_viewable_size_changed (GIMP_VIEWABLE (gimage));
141

142 143
  gimp_image_mask_changed (gimage);

144
  gimp_unset_busy (gimage->gimp);
145
}