2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1997-11-25 06:05:25 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1997-11-25 06:05:25 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-25 06:05:25 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2000-02-17 04:44:39 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <string.h>
|
2000-01-14 20:41:00 +08:00
|
|
|
|
2011-04-28 21:50:39 +08:00
|
|
|
#include <cairo.h>
|
2008-10-10 04:24:04 +08:00
|
|
|
#include <gegl.h>
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2001-01-24 07:56:18 +08:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
|
|
|
#include "libgimpmath/gimpmath.h"
|
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "core-types.h"
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base/tile-manager.h"
|
|
|
|
|
2012-03-15 07:03:07 +08:00
|
|
|
#include "gegl/gimp-gegl-utils.h"
|
|
|
|
|
2001-07-07 20:17:23 +08:00
|
|
|
#include "gimp.h"
|
2001-02-02 02:44:22 +08:00
|
|
|
#include "gimpcontext.h"
|
2001-05-09 10:32:03 +08:00
|
|
|
#include "gimpdrawable.h"
|
2001-04-19 03:14:20 +08:00
|
|
|
#include "gimpdrawable-offset.h"
|
|
|
|
#include "gimpimage.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2003-02-14 22:14:29 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
void
|
2001-04-19 04:41:15 +08:00
|
|
|
gimp_drawable_offset (GimpDrawable *drawable,
|
2004-04-15 07:37:34 +08:00
|
|
|
GimpContext *context,
|
2006-04-12 20:49:29 +08:00
|
|
|
gboolean wrap_around,
|
|
|
|
GimpOffsetType fill_type,
|
|
|
|
gint offset_x,
|
|
|
|
gint offset_y)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2012-03-15 07:03:07 +08:00
|
|
|
GimpItem *item;
|
|
|
|
TileManager *new_tiles;
|
|
|
|
GeglBuffer *src_buffer;
|
|
|
|
GeglBuffer *dest_buffer;
|
|
|
|
GeglRectangle src_rect;
|
|
|
|
GeglRectangle dest_rect;
|
|
|
|
gint width, height;
|
|
|
|
gint src_x, src_y;
|
|
|
|
gint dest_x, dest_y;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-05-08 21:12:46 +08:00
|
|
|
g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
|
2004-04-15 07:37:34 +08:00
|
|
|
g_return_if_fail (GIMP_IS_CONTEXT (context));
|
2003-05-08 21:12:46 +08:00
|
|
|
|
|
|
|
item = GIMP_ITEM (drawable);
|
1998-01-22 15:02:57 +08:00
|
|
|
|
2008-11-03 08:09:01 +08:00
|
|
|
width = gimp_item_get_width (item);
|
|
|
|
height = gimp_item_get_height (item);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
if (wrap_around)
|
|
|
|
{
|
2000-10-27 06:02:44 +08:00
|
|
|
/* avoid modulo operation on negative values */
|
|
|
|
while (offset_x < 0)
|
2006-04-12 20:49:29 +08:00
|
|
|
offset_x += width;
|
2000-10-27 06:02:44 +08:00
|
|
|
while (offset_y < 0)
|
2006-04-12 20:49:29 +08:00
|
|
|
offset_y += height;
|
2004-03-14 19:34:31 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
offset_x %= width;
|
|
|
|
offset_y %= height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-01-26 07:06:12 +08:00
|
|
|
offset_x = CLAMP (offset_x, -width, width);
|
|
|
|
offset_y = CLAMP (offset_y, -height, height);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (offset_x == 0 && offset_y == 0)
|
|
|
|
return;
|
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
new_tiles = tile_manager_new (width, height, gimp_drawable_bytes (drawable));
|
2012-03-15 07:03:07 +08:00
|
|
|
|
2012-03-21 07:42:44 +08:00
|
|
|
src_buffer = gimp_drawable_get_buffer (drawable);
|
2012-03-18 04:21:36 +08:00
|
|
|
dest_buffer = gimp_tile_manager_create_buffer (new_tiles,
|
2012-03-21 07:53:08 +08:00
|
|
|
gimp_drawable_get_format (drawable));
|
2012-03-15 07:03:07 +08:00
|
|
|
|
2012-03-17 01:36:05 +08:00
|
|
|
if (! wrap_around)
|
|
|
|
{
|
|
|
|
if (fill_type == GIMP_OFFSET_BACKGROUND)
|
|
|
|
{
|
2012-03-20 17:27:28 +08:00
|
|
|
GimpRGB bg;
|
2012-03-17 01:36:05 +08:00
|
|
|
GeglColor *color;
|
|
|
|
|
2012-03-20 17:27:28 +08:00
|
|
|
gimp_context_get_background (context, &bg);
|
2012-03-17 01:36:05 +08:00
|
|
|
|
2012-03-20 17:27:28 +08:00
|
|
|
color = gimp_gegl_color_new (&bg);
|
2012-03-17 01:36:05 +08:00
|
|
|
gegl_buffer_set_color (dest_buffer, NULL, color);
|
|
|
|
g_object_unref (color);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gegl_buffer_clear (dest_buffer, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (offset_x >= 0)
|
|
|
|
{
|
|
|
|
src_x = 0;
|
|
|
|
dest_x = offset_x;
|
2000-01-26 07:06:12 +08:00
|
|
|
width = CLAMP ((width - offset_x), 0, width);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
src_x = -offset_x;
|
|
|
|
dest_x = 0;
|
2000-01-26 07:06:12 +08:00
|
|
|
width = CLAMP ((width + offset_x), 0, width);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (offset_y >= 0)
|
|
|
|
{
|
|
|
|
src_y = 0;
|
|
|
|
dest_y = offset_y;
|
2000-01-26 07:06:12 +08:00
|
|
|
height = CLAMP ((height - offset_y), 0, height);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
src_y = -offset_y;
|
|
|
|
dest_y = 0;
|
2000-01-26 07:06:12 +08:00
|
|
|
height = CLAMP ((height + offset_y), 0, height);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the center region */
|
|
|
|
if (width && height)
|
|
|
|
{
|
2012-03-21 08:08:40 +08:00
|
|
|
gegl_buffer_copy (src_buffer,
|
|
|
|
GIMP_GEGL_RECT (src_x, src_y, width, height),
|
|
|
|
dest_buffer,
|
|
|
|
GIMP_GEGL_RECT (dest_x,dest_y, width, height));
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2012-03-15 07:03:07 +08:00
|
|
|
if (wrap_around)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2012-03-15 07:03:07 +08:00
|
|
|
/* Copy appropriately for wrap around */
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
if (offset_x >= 0 && offset_y >= 0)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
2008-11-03 08:09:01 +08:00
|
|
|
src_x = gimp_item_get_width (item) - offset_x;
|
|
|
|
src_y = gimp_item_get_height (item) - offset_y;
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else if (offset_x >= 0 && offset_y < 0)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
2008-11-03 08:09:01 +08:00
|
|
|
src_x = gimp_item_get_width (item) - offset_x;
|
2006-04-12 20:49:29 +08:00
|
|
|
src_y = 0;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else if (offset_x < 0 && offset_y >= 0)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
|
|
|
src_x = 0;
|
2008-11-03 08:09:01 +08:00
|
|
|
src_y = gimp_item_get_height (item) - offset_y;
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
else if (offset_x < 0 && offset_y < 0)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
|
|
|
src_x = 0;
|
|
|
|
src_y = 0;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2008-11-03 08:09:01 +08:00
|
|
|
dest_x = (src_x + offset_x) % gimp_item_get_width (item);
|
1997-11-25 06:05:25 +08:00
|
|
|
if (dest_x < 0)
|
2008-11-03 08:09:01 +08:00
|
|
|
dest_x = gimp_item_get_width (item) + dest_x;
|
2003-05-08 21:12:46 +08:00
|
|
|
|
2008-11-03 08:09:01 +08:00
|
|
|
dest_y = (src_y + offset_y) % gimp_item_get_height (item);
|
1997-11-25 06:05:25 +08:00
|
|
|
if (dest_y < 0)
|
2008-11-03 08:09:01 +08:00
|
|
|
dest_y = gimp_item_get_height (item) + dest_y;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* intersecting region */
|
|
|
|
if (offset_x != 0 && offset_y != 0)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
2012-03-21 10:57:53 +08:00
|
|
|
gegl_buffer_copy (src_buffer,
|
|
|
|
GIMP_GEGL_RECT (src_x, src_y, ABS(offset_x), ABS(offset_y)),
|
|
|
|
dest_buffer,
|
|
|
|
GIMP_GEGL_RECT (dest_x, dest_y, 0, 0));
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* X offset */
|
|
|
|
if (offset_x != 0)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
|
|
|
if (offset_y >= 0)
|
|
|
|
{
|
2012-03-15 07:03:07 +08:00
|
|
|
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;
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
|
|
|
else if (offset_y < 0)
|
|
|
|
{
|
2012-03-15 07:03:07 +08:00
|
|
|
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;
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
|
|
|
|
2012-03-15 07:03:07 +08:00
|
|
|
gegl_buffer_copy (src_buffer, &src_rect, dest_buffer, &dest_rect);
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* X offset */
|
|
|
|
if (offset_y != 0)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
|
|
|
if (offset_x >= 0)
|
|
|
|
{
|
2012-03-15 07:03:07 +08:00
|
|
|
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;
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
|
|
|
else if (offset_x < 0)
|
|
|
|
{
|
2012-03-15 07:03:07 +08:00
|
|
|
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;
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
|
|
|
|
2012-03-15 07:03:07 +08:00
|
|
|
gegl_buffer_copy (src_buffer, &src_rect, dest_buffer, &dest_rect);
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2012-03-15 07:03:07 +08:00
|
|
|
g_object_unref (dest_buffer);
|
|
|
|
|
2004-11-16 21:41:55 +08:00
|
|
|
gimp_drawable_set_tiles (drawable, gimp_item_is_attached (item),
|
2010-06-08 19:24:11 +08:00
|
|
|
C_("undo-type", "Offset Drawable"), new_tiles,
|
2008-11-04 04:51:46 +08:00
|
|
|
gimp_drawable_type (drawable));
|
2004-03-14 19:34:31 +08:00
|
|
|
tile_manager_unref (new_tiles);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|