gimpdrawable-offset.c 9.94 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
Elliot Lee's avatar
Elliot Lee committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
Sven Neumann's avatar
Sven Neumann committed
18

19 20
#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
21
#include <string.h>
Michael Natterer's avatar
Michael Natterer committed
22

23
#include <gegl.h>
Sven Neumann's avatar
Sven Neumann committed
24

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

Michael Natterer's avatar
Michael Natterer committed
28
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
29

Michael Natterer's avatar
Michael Natterer committed
30 31 32
#include "base/pixel-region.h"
#include "base/tile-manager.h"

33 34
#include "paint-funcs/paint-funcs.h"

Michael Natterer's avatar
Michael Natterer committed
35
#include "gimp.h"
36
#include "gimpcontext.h"
37
#include "gimpdrawable.h"
Michael Natterer's avatar
Michael Natterer committed
38 39
#include "gimpdrawable-offset.h"
#include "gimpimage.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
  GimpItem    *item;
53
  PixelRegion  srcPR, destPR;
Elliot Lee's avatar
Elliot Lee committed
54
  TileManager *new_tiles;
55 56 57 58
  gint         width, height;
  gint         src_x, src_y;
  gint         dest_x, dest_y;
  guchar       fill[MAX_CHANNELS] = { 0 };
Elliot Lee's avatar
Elliot Lee committed
59

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

  item = GIMP_ITEM (drawable);
64

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

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

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

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

88
  new_tiles = tile_manager_new (width, height, gimp_drawable_bytes (drawable));
Elliot Lee's avatar
Elliot Lee committed
89 90 91 92
  if (offset_x >= 0)
    {
      src_x = 0;
      dest_x = offset_x;
93
      width = CLAMP ((width - offset_x), 0, width);
Elliot Lee's avatar
Elliot Lee committed
94 95 96 97 98
    }
  else
    {
      src_x = -offset_x;
      dest_x = 0;
99
      width = CLAMP ((width + offset_x), 0, width);
Elliot Lee's avatar
Elliot Lee committed
100 101 102 103 104 105
    }

  if (offset_y >= 0)
    {
      src_y = 0;
      dest_y = offset_y;
106
      height = CLAMP ((height - offset_y), 0, height);
Elliot Lee's avatar
Elliot Lee committed
107 108 109 110 111
    }
  else
    {
      src_y = -offset_y;
      dest_y = 0;
112
      height = CLAMP ((height + offset_y), 0, height);
Elliot Lee's avatar
Elliot Lee committed
113 114 115 116 117
    }

  /*  Copy the center region  */
  if (width && height)
    {
118
      pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
119
                         src_x, src_y, width, height, FALSE);
120
      pixel_region_init (&destPR, new_tiles,
121
                         dest_x, dest_y, width, height, TRUE);
Elliot Lee's avatar
Elliot Lee committed
122 123 124 125 126 127 128 129

      copy_region (&srcPR, &destPR);
    }

  /*  Copy appropriately for wrap around  */
  if (wrap_around == TRUE)
    {
      if (offset_x >= 0 && offset_y >= 0)
130
        {
131 132
          src_x = gimp_item_get_width  (item) - offset_x;
          src_y = gimp_item_get_height (item) - offset_y;
133
        }
Elliot Lee's avatar
Elliot Lee committed
134
      else if (offset_x >= 0 && offset_y < 0)
135
        {
136
          src_x = gimp_item_get_width (item) - offset_x;
137 138
          src_y = 0;
        }
Elliot Lee's avatar
Elliot Lee committed
139
      else if (offset_x < 0 && offset_y >= 0)
140 141
        {
          src_x = 0;
142
          src_y = gimp_item_get_height (item) - offset_y;
143
        }
Elliot Lee's avatar
Elliot Lee committed
144
      else if (offset_x < 0 && offset_y < 0)
145 146 147 148
        {
          src_x = 0;
          src_y = 0;
        }
Elliot Lee's avatar
Elliot Lee committed
149

150
      dest_x = (src_x + offset_x) % gimp_item_get_width (item);
Elliot Lee's avatar
Elliot Lee committed
151
      if (dest_x < 0)
152
        dest_x = gimp_item_get_width (item) + dest_x;
153

154
      dest_y = (src_y + offset_y) % gimp_item_get_height (item);
Elliot Lee's avatar
Elliot Lee committed
155
      if (dest_y < 0)
156
        dest_y = gimp_item_get_height (item) + dest_y;
Elliot Lee's avatar
Elliot Lee committed
157 158 159

      /*  intersecting region  */
      if (offset_x != 0 && offset_y != 0)
160 161 162
        {
          pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
                             src_x, src_y,
163
                             ABS (offset_x), ABS (offset_y),
164 165 166
                             FALSE);
          pixel_region_init (&destPR, new_tiles,
                             dest_x, dest_y,
167
                             ABS (offset_x), ABS (offset_y),
168 169 170
                             TRUE);
          copy_region (&srcPR, &destPR);
        }
Elliot Lee's avatar
Elliot Lee committed
171 172 173

      /*  X offset  */
      if (offset_x != 0)
174 175 176 177 178
        {
          if (offset_y >= 0)
            {
              pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
                                 src_x, 0, ABS (offset_x),
179
                                 gimp_item_get_height (item) - ABS (offset_y),
180 181 182 183
                                 FALSE);
              pixel_region_init (&destPR, new_tiles,
                                 dest_x, dest_y + offset_y,
                                 ABS (offset_x),
184
                                 gimp_item_get_height (item) - ABS (offset_y),
185 186 187 188 189 190 191
                                 TRUE);
            }
          else if (offset_y < 0)
            {
              pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
                                 src_x, src_y - offset_y,
                                 ABS (offset_x),
192
                                 gimp_item_get_height (item) - ABS (offset_y),
193 194 195 196
                                 FALSE);
              pixel_region_init (&destPR, new_tiles,
                                 dest_x, 0,
                                 ABS (offset_x),
197
                                 gimp_item_get_height (item) - ABS (offset_y),
198 199 200 201 202
                                 TRUE);
            }

          copy_region (&srcPR, &destPR);
        }
Elliot Lee's avatar
Elliot Lee committed
203 204 205

      /*  X offset  */
      if (offset_y != 0)
206 207 208 209 210
        {
          if (offset_x >= 0)
            {
              pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
                                 0, src_y,
211
                                 gimp_item_get_width (item) - ABS (offset_x),
212 213
                                 ABS (offset_y), FALSE);
              pixel_region_init (&destPR, new_tiles, dest_x + offset_x, dest_y,
214
                                 gimp_item_get_width (item) - ABS (offset_x),
215 216 217 218 219 220
                                 ABS (offset_y), TRUE);
            }
          else if (offset_x < 0)
            {
              pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
                                 src_x - offset_x, src_y,
221
                                 gimp_item_get_width (item) - ABS (offset_x),
222 223
                                 ABS (offset_y), FALSE);
              pixel_region_init (&destPR, new_tiles, 0, dest_y,
224
                                 gimp_item_get_width (item) - ABS (offset_x),
225 226 227 228 229
                                 ABS (offset_y), TRUE);
            }

          copy_region (&srcPR, &destPR);
        }
Elliot Lee's avatar
Elliot Lee committed
230 231 232 233
    }
  /*  Otherwise, fill the vacated regions  */
  else
    {
234
      if (fill_type == GIMP_OFFSET_BACKGROUND)
235 236
        {
          GimpRGB color;
Michael Natterer's avatar
Michael Natterer committed
237

238
          gimp_context_get_background (context, &color);
239

240
          gimp_rgb_get_uchar (&color, &fill[0], &fill[1], &fill[2]);
241

242 243 244
          if (gimp_drawable_has_alpha (drawable))
            fill[gimp_drawable_bytes (drawable) - 1] = OPAQUE_OPACITY;
        }
Elliot Lee's avatar
Elliot Lee committed
245 246

      if (offset_x >= 0 && offset_y >= 0)
247 248 249 250
        {
          dest_x = 0;
          dest_y = 0;
        }
Elliot Lee's avatar
Elliot Lee committed
251
      else if (offset_x >= 0 && offset_y < 0)
252 253
        {
          dest_x = 0;
254
          dest_y = gimp_item_get_height (item) + offset_y;
255
        }
Elliot Lee's avatar
Elliot Lee committed
256
      else if (offset_x < 0 && offset_y >= 0)
257
        {
258
          dest_x = gimp_item_get_width (item) + offset_x;
259 260
          dest_y = 0;
        }
Elliot Lee's avatar
Elliot Lee committed
261
      else if (offset_x < 0 && offset_y < 0)
262
        {
263 264
          dest_x = gimp_item_get_width (item) + offset_x;
          dest_y = gimp_item_get_height (item) + offset_y;
265
        }
Elliot Lee's avatar
Elliot Lee committed
266 267 268

      /*  intersecting region  */
      if (offset_x != 0 && offset_y != 0)
269 270 271 272 273
        {
          pixel_region_init (&destPR, new_tiles, dest_x, dest_y,
                             ABS (offset_x), ABS (offset_y), TRUE);
          color_region (&destPR, fill);
        }
Elliot Lee's avatar
Elliot Lee committed
274 275 276

      /*  X offset  */
      if (offset_x != 0)
277 278 279 280 281
        {
          if (offset_y >= 0)
            pixel_region_init (&destPR, new_tiles,
                               dest_x, dest_y + offset_y,
                               ABS (offset_x),
282
                               gimp_item_get_height (item) - ABS (offset_y),
283 284 285 286 287
                               TRUE);
          else if (offset_y < 0)
            pixel_region_init (&destPR, new_tiles,
                               dest_x, 0,
                               ABS (offset_x),
288
                               gimp_item_get_height (item) - ABS (offset_y),
289 290 291 292
                               TRUE);

          color_region (&destPR, fill);
        }
Elliot Lee's avatar
Elliot Lee committed
293 294 295

      /*  X offset  */
      if (offset_y != 0)
296 297 298 299 300
        {
          if (offset_x >= 0)
            pixel_region_init (&destPR, new_tiles,
                               dest_x + offset_x,
                               dest_y,
301
                               gimp_item_get_width (item) - ABS (offset_x),
302 303 304 305 306
                               ABS (offset_y),
                               TRUE);
          else if (offset_x < 0)
            pixel_region_init (&destPR, new_tiles,
                               0, dest_y,
307
                               gimp_item_get_width (item) - ABS (offset_x),
308 309 310 311 312
                               ABS (offset_y),
                               TRUE);

          color_region (&destPR, fill);
        }
Elliot Lee's avatar
Elliot Lee committed
313 314
    }

315
  gimp_drawable_set_tiles (drawable, gimp_item_is_attached (item),
316 317
                           _("Offset Drawable"), new_tiles,
                           gimp_drawable_type (drawable));
318
  tile_manager_unref (new_tiles);
Elliot Lee's avatar
Elliot Lee committed
319
}