2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2001-06-26 20:09:43 +08:00
|
|
|
* 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
|
|
|
|
* 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-06-26 20:09:43 +08:00
|
|
|
|
|
|
|
#include "base-types.h"
|
|
|
|
|
2006-10-30 18:13:06 +08:00
|
|
|
#include "paint-funcs/paint-funcs.h"
|
|
|
|
|
2001-06-26 20:09:43 +08:00
|
|
|
#include "pixel-region.h"
|
|
|
|
#include "tile-manager.h"
|
2006-10-30 18:13:06 +08:00
|
|
|
#include "tile-manager-crop.h"
|
2001-06-26 20:09:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Crop the buffer to the size of pixels with non-zero transparency */
|
|
|
|
|
|
|
|
TileManager *
|
|
|
|
tile_manager_crop (TileManager *tiles,
|
2006-04-12 20:49:29 +08:00
|
|
|
gint border)
|
2001-06-26 20:09:43 +08:00
|
|
|
{
|
2007-12-13 17:57:28 +08:00
|
|
|
PixelRegion PR;
|
|
|
|
TileManager *new_tiles;
|
|
|
|
gint bytes, alpha;
|
|
|
|
gint x1, y1, x2, y2;
|
|
|
|
gboolean found;
|
|
|
|
gboolean empty;
|
|
|
|
gpointer pr;
|
|
|
|
const guchar black[MAX_CHANNELS] = { 0, 0, 0, 0 };
|
2001-06-26 20:09:43 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (tiles != NULL, NULL);
|
|
|
|
|
|
|
|
bytes = tile_manager_bpp (tiles);
|
|
|
|
alpha = bytes - 1;
|
|
|
|
|
|
|
|
/* go through and calculate the bounds */
|
|
|
|
x1 = tile_manager_width (tiles);
|
|
|
|
y1 = tile_manager_height (tiles);
|
|
|
|
x2 = 0;
|
|
|
|
y2 = 0;
|
|
|
|
|
|
|
|
pixel_region_init (&PR, tiles, 0, 0, x1, y1, FALSE);
|
|
|
|
|
2004-12-12 22:25:48 +08:00
|
|
|
for (pr = pixel_regions_register (1, &PR);
|
|
|
|
pr != NULL;
|
2001-06-26 20:09:43 +08:00
|
|
|
pr = pixel_regions_process (pr))
|
|
|
|
{
|
2007-12-13 17:57:28 +08:00
|
|
|
const guchar *data = PR.data + alpha;
|
|
|
|
gint ex = PR.x + PR.w;
|
|
|
|
gint ey = PR.y + PR.h;
|
|
|
|
gint x, y;
|
2001-06-26 20:09:43 +08:00
|
|
|
|
|
|
|
for (y = PR.y; y < ey; y++)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
|
|
|
found = FALSE;
|
|
|
|
|
|
|
|
for (x = PR.x; x < ex; x++, data += bytes)
|
|
|
|
if (*data)
|
|
|
|
{
|
|
|
|
if (x < x1)
|
|
|
|
x1 = x;
|
|
|
|
if (x > x2)
|
|
|
|
x2 = x;
|
|
|
|
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
if (y < y1)
|
|
|
|
y1 = y;
|
|
|
|
if (y > y2)
|
|
|
|
y2 = y;
|
|
|
|
}
|
|
|
|
}
|
2001-06-26 20:09:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
x2 = CLAMP (x2 + 1, 0, tile_manager_width (tiles));
|
|
|
|
y2 = CLAMP (y2 + 1, 0, tile_manager_height (tiles));
|
|
|
|
|
2004-12-12 22:25:48 +08:00
|
|
|
empty = (x1 == tile_manager_width (tiles) &&
|
2006-04-12 20:49:29 +08:00
|
|
|
y1 == tile_manager_height (tiles));
|
2001-06-26 20:09:43 +08:00
|
|
|
|
|
|
|
if (empty)
|
|
|
|
{
|
|
|
|
/* If there are no visible pixels, return NULL */
|
|
|
|
|
|
|
|
new_tiles = NULL;
|
|
|
|
}
|
2004-12-12 22:25:48 +08:00
|
|
|
else if (x1 == 0 && y1 == 0 &&
|
2006-04-12 20:49:29 +08:00
|
|
|
x2 == tile_manager_width (tiles) &&
|
|
|
|
y2 == tile_manager_height (tiles) &&
|
|
|
|
border == 0)
|
2001-06-26 20:09:43 +08:00
|
|
|
{
|
|
|
|
/* If no cropping, return original buffer */
|
|
|
|
|
|
|
|
new_tiles = tiles;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Otherwise, crop the original area */
|
|
|
|
|
|
|
|
PixelRegion srcPR, destPR;
|
|
|
|
gint new_width, new_height;
|
|
|
|
|
|
|
|
new_width = (x2 - x1) + border * 2;
|
|
|
|
new_height = (y2 - y1) + border * 2;
|
|
|
|
new_tiles = tile_manager_new (new_width, new_height, bytes);
|
|
|
|
|
|
|
|
/* If there is a border, make sure to clear the new tiles first */
|
|
|
|
if (border)
|
2006-04-12 20:49:29 +08:00
|
|
|
{
|
|
|
|
pixel_region_init (&destPR, new_tiles,
|
|
|
|
0, 0, new_width, border,
|
|
|
|
TRUE);
|
|
|
|
color_region (&destPR, black);
|
|
|
|
|
|
|
|
pixel_region_init (&destPR, new_tiles,
|
|
|
|
0, border, border, (y2 - y1),
|
|
|
|
TRUE);
|
|
|
|
color_region (&destPR, black);
|
|
|
|
|
|
|
|
pixel_region_init (&destPR, new_tiles,
|
|
|
|
new_width - border, border, border, (y2 - y1),
|
|
|
|
TRUE);
|
|
|
|
color_region (&destPR, black);
|
|
|
|
|
|
|
|
pixel_region_init (&destPR, new_tiles,
|
|
|
|
0, new_height - border, new_width, border,
|
|
|
|
TRUE);
|
|
|
|
color_region (&destPR, black);
|
|
|
|
}
|
2001-06-26 20:09:43 +08:00
|
|
|
|
2004-12-12 22:25:48 +08:00
|
|
|
pixel_region_init (&srcPR, tiles,
|
2006-04-12 20:49:29 +08:00
|
|
|
x1, y1, (x2 - x1), (y2 - y1), FALSE);
|
2004-12-12 22:25:48 +08:00
|
|
|
pixel_region_init (&destPR, new_tiles,
|
2006-04-12 20:49:29 +08:00
|
|
|
border, border, (x2 - x1), (y2 - y1), TRUE);
|
2001-06-26 20:09:43 +08:00
|
|
|
|
|
|
|
copy_region (&srcPR, &destPR);
|
|
|
|
|
|
|
|
tile_manager_set_offsets (new_tiles, x1, y1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_tiles;
|
|
|
|
}
|