2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2001-03-31 22:10:22 +08:00
|
|
|
* Copyright (C) 1995-2001 Spencer Kimball, Peter Mattis, and others
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2001-12-08 00:10:53 +08:00
|
|
|
#include <glib-object.h>
|
2001-03-31 22:10:22 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base-types.h"
|
2001-03-31 22:10:22 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "pixel-region.h"
|
|
|
|
#include "pixel-surround.h"
|
|
|
|
#include "tile-manager.h"
|
2001-03-31 22:10:22 +08:00
|
|
|
#include "tile.h"
|
|
|
|
|
|
|
|
|
2007-02-23 17:50:42 +08:00
|
|
|
struct _PixelSurround
|
|
|
|
{
|
2007-02-28 05:55:52 +08:00
|
|
|
TileManager *mgr; /* tile manager to access tiles from */
|
|
|
|
gint bpp; /* bytes per pixel in tile manager */
|
|
|
|
gint w; /* width of pixel surround area */
|
|
|
|
gint h; /* height of pixel surround area */
|
|
|
|
guchar bg[MAX_CHANNELS]; /* color to use for uncovered regions */
|
|
|
|
Tile *tile; /* locked tile (may be NULL) */
|
|
|
|
gint tile_x; /* origin of locked tile */
|
|
|
|
gint tile_y; /* origin of locked tile */
|
|
|
|
gint tile_w; /* width of locked tile */
|
|
|
|
gint tile_h; /* height of locked tile */
|
|
|
|
guchar buf[0];
|
2007-02-23 17:50:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
/* inlining this function gives a few percent speedup */
|
|
|
|
static inline gboolean
|
|
|
|
pixel_surround_get_tile (PixelSurround *surround,
|
|
|
|
gint x,
|
|
|
|
gint y)
|
|
|
|
{
|
|
|
|
/* do we still have a tile lock that we can use? */
|
|
|
|
if (surround->tile)
|
|
|
|
{
|
|
|
|
if (x < surround->tile_x || x >= surround->tile_x + surround->tile_w ||
|
|
|
|
y < surround->tile_y || y >= surround->tile_y + surround->tile_h)
|
|
|
|
{
|
|
|
|
tile_release (surround->tile, FALSE);
|
|
|
|
surround->tile = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if not, try to get one for the target pixel */
|
|
|
|
if (! surround->tile)
|
|
|
|
{
|
|
|
|
surround->tile = tile_manager_get_tile (surround->mgr, x, y, TRUE, FALSE);
|
|
|
|
|
|
|
|
if (surround->tile)
|
|
|
|
{
|
|
|
|
surround->tile_x = x & ~(TILE_WIDTH - 1);
|
|
|
|
surround->tile_y = y & ~(TILE_HEIGHT - 1);
|
|
|
|
surround->tile_w = tile_ewidth (surround->tile);
|
|
|
|
surround->tile_h = tile_eheight (surround->tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (surround->tile != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* pixel_surround_new:
|
|
|
|
* @tm: tile manager
|
|
|
|
* @width: width of surround region
|
|
|
|
* @height: height of surround region
|
|
|
|
* @bg: color to use for pixels that are not covered by the tile manager
|
|
|
|
*
|
|
|
|
* Return value: a new #PixelSurround.
|
|
|
|
*/
|
2007-02-23 17:50:42 +08:00
|
|
|
PixelSurround *
|
2007-02-24 00:19:42 +08:00
|
|
|
pixel_surround_new (TileManager *tiles,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
2007-02-23 17:50:42 +08:00
|
|
|
const guchar bg[MAX_CHANNELS])
|
2001-03-31 22:10:22 +08:00
|
|
|
{
|
2007-02-24 00:19:42 +08:00
|
|
|
PixelSurround *surround;
|
2007-02-23 17:50:42 +08:00
|
|
|
gint i;
|
2001-03-31 22:10:22 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
g_return_val_if_fail (tiles != NULL, NULL);
|
2007-02-23 17:50:42 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
surround = g_malloc (sizeof (PixelSurround) +
|
|
|
|
width * height * tile_manager_bpp (tiles));
|
2007-02-23 18:10:44 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
surround->mgr = tiles;
|
|
|
|
surround->bpp = tile_manager_bpp (tiles);
|
|
|
|
surround->w = width;
|
|
|
|
surround->h = height;
|
2001-03-31 22:10:22 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
for (i = 0; i < surround->bpp; ++i)
|
|
|
|
surround->bg[i] = bg[i];
|
2007-02-23 17:50:42 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
surround->tile = NULL;
|
2007-02-23 17:50:42 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
return surround;
|
2001-03-31 22:10:22 +08:00
|
|
|
}
|
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
/**
|
|
|
|
* pixel_surround_lock:
|
|
|
|
* @surround: a #PixelSurround
|
|
|
|
* @x: X coordinate of upper left corner
|
|
|
|
* @y: Y coordinate of upper left corner
|
|
|
|
* @rowstride: return location for rowstride
|
|
|
|
*
|
|
|
|
* Gives access to a region of pixels. The upper left corner is
|
|
|
|
* specified by the @x and @y parameters. The size of the region
|
|
|
|
* is determined by the dimensions given when creating the @surround.
|
|
|
|
*
|
|
|
|
* When you don't need to read from the pixels any longer, you should
|
|
|
|
* unlock the @surround using pixel_surround_unlock(). If you need a
|
|
|
|
* different region, just call pixel_surround_lock() again.
|
|
|
|
*
|
|
|
|
* Return value: pointer to pixel data (read-only)
|
|
|
|
*/
|
2007-02-23 17:50:42 +08:00
|
|
|
const guchar *
|
2007-02-24 00:19:42 +08:00
|
|
|
pixel_surround_lock (PixelSurround *surround,
|
2006-04-12 20:49:29 +08:00
|
|
|
gint x,
|
2007-02-23 17:50:42 +08:00
|
|
|
gint y,
|
|
|
|
gint *rowstride)
|
2001-03-31 22:10:22 +08:00
|
|
|
{
|
2007-02-23 18:59:02 +08:00
|
|
|
guchar *dest;
|
2007-02-24 00:19:42 +08:00
|
|
|
gint i = x % TILE_WIDTH;
|
|
|
|
gint j = y % TILE_HEIGHT;
|
2001-03-31 22:10:22 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
/* return a pointer to the tile data if the tile covers the whole region */
|
|
|
|
if (pixel_surround_get_tile (surround, x, y) &&
|
|
|
|
i + surround->w <= surround->tile_w &&
|
|
|
|
j + surround->h <= surround->tile_h)
|
2001-03-31 22:10:22 +08:00
|
|
|
{
|
2007-02-24 00:19:42 +08:00
|
|
|
*rowstride = surround->tile_w * surround->bpp;
|
2005-07-09 20:04:23 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
return tile_data_pointer (surround->tile, i, j);
|
2001-03-31 22:10:22 +08:00
|
|
|
}
|
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
/* otherwise, copy region to our internal buffer */
|
2007-02-28 05:55:52 +08:00
|
|
|
dest = surround->buf;
|
2007-02-23 17:50:42 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
for (j = y; j < y + surround->h; j++)
|
2001-03-31 22:10:22 +08:00
|
|
|
{
|
2007-02-24 00:19:42 +08:00
|
|
|
for (i = x; i < x + surround->w; i++)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
2007-02-23 18:59:02 +08:00
|
|
|
const guchar *src;
|
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
if (pixel_surround_get_tile (surround, i, j))
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
2007-02-24 00:19:42 +08:00
|
|
|
src = tile_data_pointer (surround->tile,
|
|
|
|
i % TILE_WIDTH, j % TILE_HEIGHT);
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-02-24 00:19:42 +08:00
|
|
|
src = surround->bg;
|
|
|
|
}
|
2007-02-23 18:59:02 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
switch (surround->bpp)
|
|
|
|
{
|
|
|
|
case 4: *dest++ = *src++;
|
|
|
|
case 3: *dest++ = *src++;
|
|
|
|
case 2: *dest++ = *src++;
|
|
|
|
case 1: *dest++ = *src++;
|
2006-04-12 20:49:29 +08:00
|
|
|
}
|
|
|
|
}
|
2001-03-31 22:10:22 +08:00
|
|
|
}
|
2005-09-04 03:03:53 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
*rowstride = surround->w * surround->bpp;
|
2001-03-31 22:10:22 +08:00
|
|
|
|
2007-02-28 05:55:52 +08:00
|
|
|
return surround->buf;
|
2001-03-31 22:10:22 +08:00
|
|
|
}
|
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
/**
|
|
|
|
* pixel_surround_release:
|
|
|
|
* @surround: #PixelSurround
|
|
|
|
*
|
|
|
|
* Unlocks pixels locked by @surround. See pixel_surround_lock().
|
|
|
|
*/
|
2001-03-31 22:10:22 +08:00
|
|
|
void
|
2007-02-24 00:19:42 +08:00
|
|
|
pixel_surround_release (PixelSurround *surround)
|
2001-03-31 22:10:22 +08:00
|
|
|
{
|
2007-02-24 00:19:42 +08:00
|
|
|
if (surround->tile)
|
2001-03-31 22:10:22 +08:00
|
|
|
{
|
2007-02-24 00:19:42 +08:00
|
|
|
tile_release (surround->tile, FALSE);
|
|
|
|
surround->tile = NULL;
|
2001-03-31 22:10:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
/**
|
|
|
|
* pixel_surround_destroy:
|
|
|
|
* @surround: #PixelSurround
|
|
|
|
*
|
|
|
|
* Unlocks pixels and frees any resources allocated for @surround. You
|
|
|
|
* must not use @surround any longer after calling this function.
|
|
|
|
*/
|
2001-03-31 22:10:22 +08:00
|
|
|
void
|
2007-02-24 00:19:42 +08:00
|
|
|
pixel_surround_destroy (PixelSurround *surround)
|
2001-03-31 22:10:22 +08:00
|
|
|
{
|
2007-02-24 00:19:42 +08:00
|
|
|
g_return_if_fail (surround != NULL);
|
2007-02-23 18:59:02 +08:00
|
|
|
|
2007-02-24 00:19:42 +08:00
|
|
|
pixel_surround_release (surround);
|
|
|
|
g_free (surround);
|
2001-03-31 22:10:22 +08:00
|
|
|
}
|