gimpimage-flip.c 4.5 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20
#include <gdk-pixbuf/gdk-pixbuf.h>
21
#include <gegl.h>
22 23 24 25

#include "core-types.h"

#include "gimp.h"
26
#include "gimpcontainer.h"
27
#include "gimpcontext.h"
28
#include "gimpguide.h"
29 30 31
#include "gimpimage.h"
#include "gimpimage-flip.h"
#include "gimpimage-guides.h"
32
#include "gimpimage-sample-points.h"
33 34 35
#include "gimpimage-undo.h"
#include "gimpimage-undo-push.h"
#include "gimpitem.h"
36
#include "gimpobjectqueue.h"
37
#include "gimpprogress.h"
38
#include "gimpsamplepoint.h"
39 40 41


void
42
gimp_image_flip (GimpImage           *image,
43
                 GimpContext         *context,
44
                 GimpOrientationType  flip_type,
45
                 GimpProgress        *progress)
46
{
47 48 49 50
  GimpObjectQueue *queue;
  GimpItem        *item;
  GList           *list;
  gdouble          axis;
51

52
  g_return_if_fail (GIMP_IS_IMAGE (image));
53
  g_return_if_fail (GIMP_IS_CONTEXT (context));
54
  g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress));
55

56
  gimp_set_busy (image->gimp);
57 58 59 60

  switch (flip_type)
    {
    case GIMP_ORIENTATION_HORIZONTAL:
61
      axis = (gdouble) gimp_image_get_width (image) / 2.0;
62 63 64
      break;

    case GIMP_ORIENTATION_VERTICAL:
65
      axis = (gdouble) gimp_image_get_height (image) / 2.0;
66 67 68
      break;

    default:
69
      g_warning ("%s: unknown flip_type", G_STRFUNC);
70 71 72
      return;
    }

73 74
  queue    = gimp_object_queue_new (progress);
  progress = GIMP_PROGRESS (queue);
75

76 77 78 79
  gimp_object_queue_push_container (queue, gimp_image_get_layers (image));
  gimp_object_queue_push (queue, gimp_image_get_mask (image));
  gimp_object_queue_push_container (queue, gimp_image_get_channels (image));
  gimp_object_queue_push_container (queue, gimp_image_get_vectors (image));
80

81
  gimp_image_undo_group_start (image, GIMP_UNDO_GROUP_IMAGE_FLIP, NULL);
82

83 84
  /*  Flip all layers, channels (including selection mask), and vectors  */
  while ((item = gimp_object_queue_pop (queue)))
85
    {
86
      gimp_item_flip (item, context, flip_type, axis, FALSE);
87

88
      gimp_progress_set_value (progress, 1.0);
89 90 91
    }

  /*  Flip all Guides  */
92 93 94
  for (list = gimp_image_get_guides (image);
       list;
       list = g_list_next (list))
95
    {
96 97
      GimpGuide *guide    = list->data;
      gint       position = gimp_guide_get_position (guide);
98

99
      switch (gimp_guide_get_orientation (guide))
100 101
        {
        case GIMP_ORIENTATION_HORIZONTAL:
102
          if (flip_type == GIMP_ORIENTATION_VERTICAL)
103
            gimp_image_move_guide (image, guide,
104 105
                                   gimp_image_get_height (image) - position,
                                   TRUE);
106
          break;
107

108
        case GIMP_ORIENTATION_VERTICAL:
109
          if (flip_type == GIMP_ORIENTATION_HORIZONTAL)
110
            gimp_image_move_guide (image, guide,
111 112
                                   gimp_image_get_width (image) - position,
                                   TRUE);
113
          break;
114

115
        default:
116
          break;
117
        }
118 119
    }

120
  /*  Flip all sample points  */
121 122 123
  for (list = gimp_image_get_sample_points (image);
       list;
       list = g_list_next (list))
124 125
    {
      GimpSamplePoint *sample_point = list->data;
126 127 128 129
      gint             x;
      gint             y;

      gimp_sample_point_get_position (sample_point, &x, &y);
130 131

      if (flip_type == GIMP_ORIENTATION_VERTICAL)
132
        gimp_image_move_sample_point (image, sample_point,
133 134
                                      x,
                                      gimp_image_get_height (image) - y,
135 136 137
                                      TRUE);

      if (flip_type == GIMP_ORIENTATION_HORIZONTAL)
138
        gimp_image_move_sample_point (image, sample_point,
139 140
                                      gimp_image_get_width (image) - x,
                                      y,
141 142 143
                                      TRUE);
    }

144
  gimp_image_undo_group_end (image);
145

146 147
  g_object_unref (queue);

148
  gimp_unset_busy (image->gimp);
149
}