gimpdrawable-offset.c 7.06 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
Elliot Lee's avatar
Elliot Lee committed
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
Elliot Lee's avatar
Elliot Lee committed
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/>.
Elliot Lee's avatar
Elliot Lee committed
16
 */
Sven Neumann's avatar
Sven Neumann committed
17

18 19
#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
20
#include <string.h>
21

22
#include <cairo.h>
23
#include <gegl.h>
24
#include <gdk-pixbuf/gdk-pixbuf.h>
Sven Neumann's avatar
Sven Neumann committed
25

26 27 28
#include "libgimpcolor/gimpcolor.h"
#include "libgimpmath/gimpmath.h"

29
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
30

31
#include "gegl/gimp-gegl-loops.h"
32 33
#include "gegl/gimp-gegl-utils.h"

34
#include "gimp.h"
35
#include "gimpcontext.h"
36
#include "gimpdrawable.h"
37 38
#include "gimpdrawable-offset.h"
#include "gimpimage.h"
39
#include "gimppickable.h"
Elliot Lee's avatar
Elliot Lee committed
40

41
#include "gimp-intl.h"
42

Elliot Lee's avatar
Elliot Lee committed
43 44

void
45
gimp_drawable_offset (GimpDrawable   *drawable,
46
                      GimpContext    *context,
47 48 49 50
                      gboolean        wrap_around,
                      GimpOffsetType  fill_type,
                      gint            offset_x,
                      gint            offset_y)
Elliot Lee's avatar
Elliot Lee committed
51
{
52 53
  GimpItem      *item;
  GeglBuffer    *src_buffer;
54
  GeglBuffer    *new_buffer;
55 56 57 58 59
  GeglRectangle  src_rect;
  GeglRectangle  dest_rect;
  gint           width, height;
  gint           src_x, src_y;
  gint           dest_x, dest_y;
Elliot Lee's avatar
Elliot Lee committed
60

61
  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
62
  g_return_if_fail (GIMP_IS_CONTEXT (context));
63 64

  item = GIMP_ITEM (drawable);
65

66 67
  width  = gimp_item_get_width  (item);
  height = gimp_item_get_height (item);
Elliot Lee's avatar
Elliot Lee committed
68 69 70

  if (wrap_around)
    {
71 72
      /*  avoid modulo operation on negative values  */
      while (offset_x < 0)
73
        offset_x += width;
74
      while (offset_y < 0)
75
        offset_y += height;
76

Elliot Lee's avatar
Elliot Lee committed
77 78 79 80 81
      offset_x %= width;
      offset_y %= height;
    }
  else
    {
82 83
      offset_x = CLAMP (offset_x, -width, width);
      offset_y = CLAMP (offset_y, -height, height);
Elliot Lee's avatar
Elliot Lee committed
84 85 86 87 88
    }

  if (offset_x == 0 && offset_y == 0)
    return;

89
  src_buffer = gimp_drawable_get_buffer (drawable);
90

91 92
  new_buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, width, height),
                                gimp_drawable_get_format (drawable));
93

94 95 96 97
  if (! wrap_around)
    {
      if (fill_type == GIMP_OFFSET_BACKGROUND)
        {
98
          GimpRGB    bg;
99 100
          GeglColor *color;

101
          gimp_context_get_background (context, &bg);
102 103
          gimp_pickable_srgb_to_image_color (GIMP_PICKABLE (drawable),
                                             &bg, &bg);
104

105
          color = gimp_gegl_color_new (&bg, gimp_drawable_get_space (drawable));
106
          gegl_buffer_set_color (new_buffer, NULL, color);
107 108 109 110
          g_object_unref (color);
        }
    }

Elliot Lee's avatar
Elliot Lee committed
111 112 113 114
  if (offset_x >= 0)
    {
      src_x = 0;
      dest_x = offset_x;
115
      width = CLAMP ((width - offset_x), 0, width);
Elliot Lee's avatar
Elliot Lee committed
116 117 118 119 120
    }
  else
    {
      src_x = -offset_x;
      dest_x = 0;
121
      width = CLAMP ((width + offset_x), 0, width);
Elliot Lee's avatar
Elliot Lee committed
122 123 124 125 126 127
    }

  if (offset_y >= 0)
    {
      src_y = 0;
      dest_y = offset_y;
128
      height = CLAMP ((height - offset_y), 0, height);
Elliot Lee's avatar
Elliot Lee committed
129 130 131 132 133
    }
  else
    {
      src_y = -offset_y;
      dest_y = 0;
134
      height = CLAMP ((height + offset_y), 0, height);
Elliot Lee's avatar
Elliot Lee committed
135 136 137 138 139
    }

  /*  Copy the center region  */
  if (width && height)
    {
140 141 142 143 144
      gimp_gegl_buffer_copy (src_buffer,
                             GEGL_RECTANGLE (src_x,  src_y,  width, height),
                             GEGL_ABYSS_NONE,
                             new_buffer,
                             GEGL_RECTANGLE (dest_x,dest_y,  width, height));
Elliot Lee's avatar
Elliot Lee committed
145 146
    }

147
  if (wrap_around)
Elliot Lee's avatar
Elliot Lee committed
148
    {
149 150
      /*  Copy appropriately for wrap around  */

Elliot Lee's avatar
Elliot Lee committed
151
      if (offset_x >= 0 && offset_y >= 0)
152
        {
153 154
          src_x = gimp_item_get_width  (item) - offset_x;
          src_y = gimp_item_get_height (item) - offset_y;
155
        }
Elliot Lee's avatar
Elliot Lee committed
156
      else if (offset_x >= 0 && offset_y < 0)
157
        {
158
          src_x = gimp_item_get_width (item) - offset_x;
159 160
          src_y = 0;
        }
Elliot Lee's avatar
Elliot Lee committed
161
      else if (offset_x < 0 && offset_y >= 0)
162 163
        {
          src_x = 0;
164
          src_y = gimp_item_get_height (item) - offset_y;
165
        }
Elliot Lee's avatar
Elliot Lee committed
166
      else if (offset_x < 0 && offset_y < 0)
167 168 169 170
        {
          src_x = 0;
          src_y = 0;
        }
Elliot Lee's avatar
Elliot Lee committed
171

172
      dest_x = (src_x + offset_x) % gimp_item_get_width (item);
Elliot Lee's avatar
Elliot Lee committed
173
      if (dest_x < 0)
174
        dest_x = gimp_item_get_width (item) + dest_x;
175

176
      dest_y = (src_y + offset_y) % gimp_item_get_height (item);
Elliot Lee's avatar
Elliot Lee committed
177
      if (dest_y < 0)
178
        dest_y = gimp_item_get_height (item) + dest_y;
Elliot Lee's avatar
Elliot Lee committed
179 180 181

      /*  intersecting region  */
      if (offset_x != 0 && offset_y != 0)
182
        {
183 184 185 186 187 188 189
          gimp_gegl_buffer_copy (
            src_buffer,
            GEGL_RECTANGLE (src_x, src_y,
                            ABS (offset_x), ABS (offset_y)),
            GEGL_ABYSS_NONE,
            new_buffer,
            GEGL_RECTANGLE (dest_x, dest_y, 0, 0));
190
        }
Elliot Lee's avatar
Elliot Lee committed
191 192 193

      /*  X offset  */
      if (offset_x != 0)
194 195 196
        {
          if (offset_y >= 0)
            {
197 198 199 200 201 202 203
              src_rect.x      = src_x;
              src_rect.y      = 0;
              src_rect.width  = ABS (offset_x);
              src_rect.height = gimp_item_get_height (item) - ABS (offset_y);

              dest_rect.x = dest_x;
              dest_rect.y = dest_y + offset_y;
204 205 206
            }
          else if (offset_y < 0)
            {
207 208 209 210 211 212 213
              src_rect.x      = src_x;
              src_rect.y      = src_y - offset_y;
              src_rect.width  = ABS (offset_x);
              src_rect.height = gimp_item_get_height (item) - ABS (offset_y);

              dest_rect.x = dest_x;
              dest_rect.y = 0;
214 215
            }

216 217
          gimp_gegl_buffer_copy (src_buffer, &src_rect, GEGL_ABYSS_NONE,
                                 new_buffer, &dest_rect);
218
        }
Elliot Lee's avatar
Elliot Lee committed
219 220 221

      /*  X offset  */
      if (offset_y != 0)
222 223 224
        {
          if (offset_x >= 0)
            {
225 226 227 228 229 230 231
              src_rect.x      = 0;
              src_rect.y      = src_y;
              src_rect.width  = gimp_item_get_width (item) - ABS (offset_x);
              src_rect.height = ABS (offset_y);

              dest_rect.x = dest_x + offset_x;
              dest_rect.y = dest_y;
232 233 234
            }
          else if (offset_x < 0)
            {
235 236 237 238 239 240 241
              src_rect.x      = src_x - offset_x;
              src_rect.y      = src_y;
              src_rect.width  = gimp_item_get_width (item) - ABS (offset_x);
              src_rect.height = ABS (offset_y);

              dest_rect.x = 0;
              dest_rect.y = dest_y;
242 243
            }

244 245
          gimp_gegl_buffer_copy (src_buffer, &src_rect, GEGL_ABYSS_NONE,
                                 new_buffer, &dest_rect);
246
        }
Elliot Lee's avatar
Elliot Lee committed
247 248
    }

249 250 251 252
  gimp_drawable_set_buffer (drawable,
                            gimp_item_is_attached (item),
                            C_("undo-type", "Offset Drawable"),
                            new_buffer);
253
  g_object_unref (new_buffer);
Elliot Lee's avatar
Elliot Lee committed
254
}