gimpimage-resize.c 3.71 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 "gimplayer.h"
33
#include "gimplist.h"
34 35

#include "floating_sel.h"
36
#include "undo.h"
37

38

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

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

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

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

Sven Neumann's avatar
Sven Neumann committed
60
  undo_push_group_start (gimage, IMAGE_RESIZE_UNDO);
61 62 63 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  */
  undo_push_gimage_mod (gimage);

  /*  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
    {
Michael Natterer's avatar
Michael Natterer committed
78
      channel = (GimpChannel *) 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 84 85 86
    }

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

Michael Natterer's avatar
Michael Natterer committed
89
      guide = (GimpGuide *) guide_list->data;
Sven Neumann's avatar
Sven Neumann committed
90
      guide_list = g_list_next (guide_list);
91 92 93

      switch (guide->orientation)
	{
94
	case ORIENTATION_HORIZONTAL:
Sven Neumann's avatar
Sven Neumann committed
95
	  undo_push_guide (gimage, guide);
96 97 98 99
	  guide->position += offset_y;
	  if (guide->position < 0 || guide->position > new_height)
	    gimp_image_delete_guide (gimage, guide);
	  break;
100

101
	case ORIENTATION_VERTICAL:
Sven Neumann's avatar
Sven Neumann committed
102
	  undo_push_guide (gimage, guide);
103 104 105 106
	  guide->position += offset_x;
	  if (guide->position < 0 || guide->position > new_width)
	    gimp_image_delete_guide (gimage, guide);
	  break;
107

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

  /*  Don't forget the selection mask!  */
Michael Natterer's avatar
Michael Natterer committed
114 115
  gimp_channel_resize (gimage->selection_mask,
		       new_width, new_height, offset_x, offset_y);
116 117 118
  gimage_mask_invalidate (gimage);

  /*  Reposition all layers  */
119 120 121
  for (list = GIMP_LIST (gimage->layers)->list; 
       list; 
       list = g_list_next (list))
122
    {
123
      layer = (GimpLayer *) list->data;
124

125
      gimp_layer_translate (layer, offset_x, offset_y);
126 127 128
    }

  /*  Make sure the projection matches the gimage size  */
129
  gimp_image_projection_allocate (gimage);
130 131 132 133 134

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

135 136
  undo_push_group_end (gimage);

137
  gimp_viewable_size_changed (GIMP_VIEWABLE (gimage));
138

139
  gimp_unset_busy (gimage->gimp);
140
}