gimpimage-resize.c 3.91 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 "gimplayer.h"
Michael Natterer's avatar
Michael Natterer committed
34
#include "gimplayer-floating-sel.h"
35
#include "gimplist.h"
36

37
#include "undo.h"
38

39 40
#include "libgimp/gimpintl.h"

41

42
void
43
gimp_image_resize (GimpImage *gimage, 
44 45 46 47
		   gint       new_width, 
		   gint       new_height,
		   gint       offset_x, 
		   gint       offset_y)
48
{
Michael Natterer's avatar
Michael Natterer committed
49 50 51
  GimpChannel *channel;
  GimpLayer   *layer;
  GimpLayer   *floating_layer;
52
  GList       *list;
Michael Natterer's avatar
Michael Natterer committed
53
  GList       *guide_list;
54

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

58
  gimp_set_busy (gimage->gimp);
59 60 61 62

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

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

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

  /*  Push the image size to the stack  */
Michael Natterer's avatar
Michael Natterer committed
71
  undo_push_image_size (gimage);
72 73

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

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

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

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

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

      switch (guide->orientation)
	{
98
	case GIMP_ORIENTATION_HORIZONTAL:
Michael Natterer's avatar
Michael Natterer committed
99
	  undo_push_image_guide (gimage, guide);
100 101 102 103
	  guide->position += offset_y;
	  if (guide->position < 0 || guide->position > new_height)
	    gimp_image_delete_guide (gimage, guide);
	  break;
104

105
	case GIMP_ORIENTATION_VERTICAL:
Michael Natterer's avatar
Michael Natterer committed
106
	  undo_push_image_guide (gimage, guide);
107 108 109 110
	  guide->position += offset_x;
	  if (guide->position < 0 || guide->position > new_width)
	    gimp_image_delete_guide (gimage, guide);
	  break;
111

112
	default:
113
	  g_error ("Unknown guide orientation\n");
114
	}
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
      layer = (GimpLayer *) list->data;
128

129
      gimp_layer_translate (layer, offset_x, offset_y);
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
}