2004-01-13 18:26:10 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
|
|
|
|
*
|
|
|
|
* gimpregioniterator.c
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
2004-01-13 18:26:10 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2004-01-13 18:26:10 +08:00
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
2004-01-13 18:26:10 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "gimp.h"
|
|
|
|
#include "gimpregioniterator.h"
|
|
|
|
|
2004-08-31 04:43:48 +08:00
|
|
|
|
2004-01-13 18:26:10 +08:00
|
|
|
struct _GimpRgnIterator
|
|
|
|
{
|
|
|
|
GimpDrawable *drawable;
|
2007-05-22 22:04:35 +08:00
|
|
|
gint x1;
|
|
|
|
gint y1;
|
|
|
|
gint x2;
|
|
|
|
gint y2;
|
2004-01-13 18:26:10 +08:00
|
|
|
};
|
|
|
|
|
2004-08-31 04:43:48 +08:00
|
|
|
|
|
|
|
static void gimp_rgn_iterator_iter_single (GimpRgnIterator *iter,
|
|
|
|
GimpPixelRgn *srcPR,
|
|
|
|
GimpRgnFuncSrc func,
|
|
|
|
gpointer data);
|
2007-12-17 17:16:21 +08:00
|
|
|
static void gimp_rgn_render_row (const guchar *src,
|
2004-08-31 04:43:48 +08:00
|
|
|
guchar *dest,
|
|
|
|
gint col,
|
|
|
|
gint bpp,
|
|
|
|
GimpRgnFunc2 func,
|
|
|
|
gpointer data);
|
|
|
|
static void gimp_rgn_render_region (const GimpPixelRgn *srcPR,
|
|
|
|
const GimpPixelRgn *destPR,
|
|
|
|
GimpRgnFunc2 func,
|
|
|
|
gpointer data);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_rgn_iterator_new:
|
|
|
|
* @drawable: a #GimpDrawable
|
2004-09-29 21:39:23 +08:00
|
|
|
* @unused: ignored
|
2004-08-31 04:43:48 +08:00
|
|
|
*
|
2004-09-29 21:39:23 +08:00
|
|
|
* Creates a new #GimpRgnIterator for @drawable. The #GimpRunMode
|
2007-05-22 22:04:35 +08:00
|
|
|
* parameter is ignored. Use gimp_rgn_iterator_free() to free thsi
|
|
|
|
* iterator.
|
2004-08-31 04:43:48 +08:00
|
|
|
*
|
|
|
|
* Return value: a newly allocated #GimpRgnIterator.
|
|
|
|
**/
|
|
|
|
GimpRgnIterator *
|
2004-01-13 18:26:10 +08:00
|
|
|
gimp_rgn_iterator_new (GimpDrawable *drawable,
|
2004-09-29 07:08:17 +08:00
|
|
|
GimpRunMode unused)
|
2004-01-13 18:26:10 +08:00
|
|
|
{
|
2007-05-22 22:04:35 +08:00
|
|
|
GimpRgnIterator *iter;
|
|
|
|
|
|
|
|
g_return_val_if_fail (drawable != NULL, NULL);
|
|
|
|
|
|
|
|
iter = g_slice_new (GimpRgnIterator);
|
2004-01-13 18:26:10 +08:00
|
|
|
|
|
|
|
iter->drawable = drawable;
|
2004-09-29 07:08:17 +08:00
|
|
|
|
2004-01-13 18:26:10 +08:00
|
|
|
gimp_drawable_mask_bounds (drawable->drawable_id,
|
|
|
|
&iter->x1, &iter->y1,
|
|
|
|
&iter->x2, &iter->y2);
|
|
|
|
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2004-08-31 04:43:48 +08:00
|
|
|
/**
|
|
|
|
* gimp_rgn_iterator_free:
|
|
|
|
* @iter: a #GimpRgnIterator
|
|
|
|
*
|
|
|
|
* Frees the resources allocated for @iter.
|
|
|
|
**/
|
2004-01-13 18:26:10 +08:00
|
|
|
void
|
|
|
|
gimp_rgn_iterator_free (GimpRgnIterator *iter)
|
|
|
|
{
|
2007-05-22 22:04:35 +08:00
|
|
|
g_return_if_fail (iter != NULL);
|
|
|
|
|
|
|
|
g_slice_free (GimpRgnIterator, iter);
|
2004-01-13 18:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgn_iterator_src (GimpRgnIterator *iter,
|
|
|
|
GimpRgnFuncSrc func,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GimpPixelRgn srcPR;
|
|
|
|
|
2007-05-22 22:04:35 +08:00
|
|
|
g_return_if_fail (iter != NULL);
|
|
|
|
|
2004-01-13 18:26:10 +08:00
|
|
|
gimp_pixel_rgn_init (&srcPR, iter->drawable,
|
|
|
|
iter->x1, iter->y1,
|
|
|
|
iter->x2 - iter->x1, iter->y2 - iter->y1,
|
|
|
|
FALSE, FALSE);
|
|
|
|
gimp_rgn_iterator_iter_single (iter, &srcPR, func, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgn_iterator_src_dest (GimpRgnIterator *iter,
|
|
|
|
GimpRgnFuncSrcDest func,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GimpPixelRgn srcPR, destPR;
|
|
|
|
gint x1, y1, x2, y2;
|
2004-08-31 04:43:48 +08:00
|
|
|
gint bpp;
|
2007-12-17 17:16:21 +08:00
|
|
|
gint count;
|
2004-01-13 18:26:10 +08:00
|
|
|
gpointer pr;
|
|
|
|
gint total_area;
|
|
|
|
gint area_so_far;
|
|
|
|
|
2007-05-22 22:04:35 +08:00
|
|
|
g_return_if_fail (iter != NULL);
|
|
|
|
|
2004-01-13 18:26:10 +08:00
|
|
|
x1 = iter->x1;
|
|
|
|
y1 = iter->y1;
|
|
|
|
x2 = iter->x2;
|
|
|
|
y2 = iter->y2;
|
|
|
|
|
2007-12-17 17:16:21 +08:00
|
|
|
total_area = (x2 - x1) * (y2 - y1);
|
|
|
|
area_so_far = 0;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
|
|
|
gimp_pixel_rgn_init (&srcPR, iter->drawable, x1, y1, x2 - x1, y2 - y1,
|
|
|
|
FALSE, FALSE);
|
|
|
|
gimp_pixel_rgn_init (&destPR, iter->drawable, x1, y1, x2 - x1, y2 - y1,
|
|
|
|
TRUE, TRUE);
|
|
|
|
|
|
|
|
bpp = srcPR.bpp;
|
|
|
|
|
2007-12-17 17:16:21 +08:00
|
|
|
for (pr = gimp_pixel_rgns_register (2, &srcPR, &destPR), count = 0;
|
2004-01-13 18:26:10 +08:00
|
|
|
pr != NULL;
|
2007-12-17 17:16:21 +08:00
|
|
|
pr = gimp_pixel_rgns_process (pr), count++)
|
2004-01-13 18:26:10 +08:00
|
|
|
{
|
2007-12-17 17:16:21 +08:00
|
|
|
const guchar *src = srcPR.data;
|
|
|
|
guchar *dest = destPR.data;
|
|
|
|
gint y;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
|
|
|
for (y = srcPR.y; y < srcPR.y + srcPR.h; y++)
|
|
|
|
{
|
2007-12-17 17:16:21 +08:00
|
|
|
const guchar *s = src;
|
|
|
|
guchar *d = dest;
|
|
|
|
gint x;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
|
|
|
for (x = srcPR.x; x < srcPR.x + srcPR.w; x++)
|
|
|
|
{
|
|
|
|
func (x, y, s, d, bpp, data);
|
2007-12-17 17:16:21 +08:00
|
|
|
|
2004-01-13 18:26:10 +08:00
|
|
|
s += bpp;
|
|
|
|
d += bpp;
|
|
|
|
}
|
|
|
|
|
|
|
|
src += srcPR.rowstride;
|
|
|
|
dest += destPR.rowstride;
|
|
|
|
}
|
|
|
|
|
2004-08-31 04:07:08 +08:00
|
|
|
area_so_far += srcPR.w * srcPR.h;
|
2007-12-17 17:16:21 +08:00
|
|
|
|
2009-03-01 01:49:52 +08:00
|
|
|
if ((count % 16) == 0)
|
2007-12-17 17:16:21 +08:00
|
|
|
gimp_progress_update ((gdouble) area_so_far / (gdouble) total_area);
|
2004-01-13 18:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gimp_drawable_flush (iter->drawable);
|
|
|
|
gimp_drawable_merge_shadow (iter->drawable->drawable_id, TRUE);
|
|
|
|
gimp_drawable_update (iter->drawable->drawable_id,
|
|
|
|
x1, y1, x2 - x1, y2 - y1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgn_iterator_dest (GimpRgnIterator *iter,
|
|
|
|
GimpRgnFuncDest func,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GimpPixelRgn destPR;
|
|
|
|
|
2007-05-22 22:04:35 +08:00
|
|
|
g_return_if_fail (iter != NULL);
|
|
|
|
|
2004-01-13 18:26:10 +08:00
|
|
|
gimp_pixel_rgn_init (&destPR, iter->drawable,
|
|
|
|
iter->x1, iter->y1,
|
|
|
|
iter->x2 - iter->x1, iter->y2 - iter->y1,
|
|
|
|
TRUE, TRUE);
|
|
|
|
gimp_rgn_iterator_iter_single (iter, &destPR, (GimpRgnFuncSrc) func, data);
|
|
|
|
|
|
|
|
/* update the processed region */
|
|
|
|
gimp_drawable_flush (iter->drawable);
|
|
|
|
gimp_drawable_merge_shadow (iter->drawable->drawable_id, TRUE);
|
|
|
|
gimp_drawable_update (iter->drawable->drawable_id,
|
|
|
|
iter->x1, iter->y1,
|
|
|
|
iter->x2 - iter->x1, iter->y2 - iter->y1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgn_iterate1 (GimpDrawable *drawable,
|
2004-09-29 07:08:17 +08:00
|
|
|
GimpRunMode unused,
|
2004-01-13 18:26:10 +08:00
|
|
|
GimpRgnFunc1 func,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GimpPixelRgn srcPR;
|
|
|
|
gint x1, y1, x2, y2;
|
|
|
|
gpointer pr;
|
|
|
|
gint total_area;
|
|
|
|
gint area_so_far;
|
2007-12-17 17:16:21 +08:00
|
|
|
gint count;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
2007-05-22 22:04:35 +08:00
|
|
|
g_return_if_fail (drawable != NULL);
|
|
|
|
|
2004-01-13 18:26:10 +08:00
|
|
|
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
|
|
|
|
|
2007-12-17 17:16:21 +08:00
|
|
|
total_area = (x2 - x1) * (y2 - y1);
|
|
|
|
area_so_far = 0;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
2004-12-27 18:24:16 +08:00
|
|
|
if (total_area <= 0)
|
|
|
|
return;
|
|
|
|
|
2004-01-13 18:26:10 +08:00
|
|
|
gimp_pixel_rgn_init (&srcPR, drawable,
|
|
|
|
x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
|
|
|
|
|
2007-12-17 17:16:21 +08:00
|
|
|
for (pr = gimp_pixel_rgns_register (1, &srcPR), count = 0;
|
2004-01-13 18:26:10 +08:00
|
|
|
pr != NULL;
|
2007-12-17 17:16:21 +08:00
|
|
|
pr = gimp_pixel_rgns_process (pr), count++)
|
2004-01-13 18:26:10 +08:00
|
|
|
{
|
2007-12-17 17:16:21 +08:00
|
|
|
const guchar *src = srcPR.data;
|
|
|
|
gint y;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
|
|
|
for (y = 0; y < srcPR.h; y++)
|
|
|
|
{
|
2007-12-17 17:16:21 +08:00
|
|
|
const guchar *s = src;
|
|
|
|
gint x;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
|
|
|
for (x = 0; x < srcPR.w; x++)
|
|
|
|
{
|
|
|
|
func (s, srcPR.bpp, data);
|
|
|
|
s += srcPR.bpp;
|
|
|
|
}
|
|
|
|
|
|
|
|
src += srcPR.rowstride;
|
|
|
|
}
|
|
|
|
|
2004-08-31 04:07:08 +08:00
|
|
|
area_so_far += srcPR.w * srcPR.h;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
2009-03-01 01:49:52 +08:00
|
|
|
if ((count % 16) == 0)
|
2004-08-31 04:07:08 +08:00
|
|
|
gimp_progress_update ((gdouble) area_so_far / (gdouble) total_area);
|
2004-01-13 18:26:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_rgn_iterate2 (GimpDrawable *drawable,
|
2004-09-29 07:08:17 +08:00
|
|
|
GimpRunMode unused,
|
2004-01-13 18:26:10 +08:00
|
|
|
GimpRgnFunc2 func,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GimpPixelRgn srcPR, destPR;
|
|
|
|
gint x1, y1, x2, y2;
|
|
|
|
gpointer pr;
|
|
|
|
gint total_area;
|
|
|
|
gint area_so_far;
|
2007-12-17 17:16:21 +08:00
|
|
|
gint count;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
2007-05-22 22:04:35 +08:00
|
|
|
g_return_if_fail (drawable != NULL);
|
|
|
|
|
2004-01-13 18:26:10 +08:00
|
|
|
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
|
|
|
|
|
2007-12-17 17:16:21 +08:00
|
|
|
total_area = (x2 - x1) * (y2 - y1);
|
|
|
|
area_so_far = 0;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
2004-12-27 18:24:16 +08:00
|
|
|
if (total_area <= 0)
|
|
|
|
return;
|
|
|
|
|
2004-01-13 18:26:10 +08:00
|
|
|
/* Initialize the pixel regions. */
|
|
|
|
gimp_pixel_rgn_init (&srcPR, drawable, x1, y1, (x2 - x1), (y2 - y1),
|
|
|
|
FALSE, FALSE);
|
|
|
|
gimp_pixel_rgn_init (&destPR, drawable, x1, y1, (x2 - x1), (y2 - y1),
|
|
|
|
TRUE, TRUE);
|
|
|
|
|
2007-12-17 17:16:21 +08:00
|
|
|
for (pr = gimp_pixel_rgns_register (2, &srcPR, &destPR), count = 0;
|
2004-01-13 18:26:10 +08:00
|
|
|
pr != NULL;
|
2007-12-17 17:16:21 +08:00
|
|
|
pr = gimp_pixel_rgns_process (pr), count++)
|
2004-01-13 18:26:10 +08:00
|
|
|
{
|
|
|
|
gimp_rgn_render_region (&srcPR, &destPR, func, data);
|
|
|
|
|
2004-08-31 04:07:08 +08:00
|
|
|
area_so_far += srcPR.w * srcPR.h;
|
2004-01-13 18:26:10 +08:00
|
|
|
|
2009-03-01 01:49:52 +08:00
|
|
|
if ((count % 16) == 0)
|
2004-08-31 04:07:08 +08:00
|
|
|
gimp_progress_update ((gdouble) area_so_far / (gdouble) total_area);
|
2004-01-13 18:26:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update the processed region */
|
|
|
|
gimp_drawable_flush (drawable);
|
|
|
|
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
|
|
|
|
gimp_drawable_update (drawable->drawable_id, x1, y1, (x2 - x1), (y2 - y1));
|
|
|
|
}
|
2004-08-31 04:43:48 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_rgn_iterator_iter_single (GimpRgnIterator *iter,
|
|
|
|
GimpPixelRgn *srcPR,
|
|
|
|
GimpRgnFuncSrc func,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gpointer pr;
|
|
|
|
gint total_area;
|
|
|
|
gint area_so_far;
|
2007-12-17 17:16:21 +08:00
|
|
|
gint count;
|
2004-08-31 04:43:48 +08:00
|
|
|
|
2007-12-17 17:16:21 +08:00
|
|
|
total_area = (iter->x2 - iter->x1) * (iter->y2 - iter->y1);
|
|
|
|
area_so_far = 0;
|
2004-08-31 04:43:48 +08:00
|
|
|
|
2007-12-17 17:16:21 +08:00
|
|
|
for (pr = gimp_pixel_rgns_register (1, srcPR), count = 0;
|
2004-08-31 04:43:48 +08:00
|
|
|
pr != NULL;
|
2007-12-17 17:16:21 +08:00
|
|
|
pr = gimp_pixel_rgns_process (pr), count++)
|
2004-08-31 04:43:48 +08:00
|
|
|
{
|
2007-12-17 17:16:21 +08:00
|
|
|
const guchar *src = srcPR->data;
|
|
|
|
gint y;
|
2004-08-31 04:43:48 +08:00
|
|
|
|
|
|
|
for (y = srcPR->y; y < srcPR->y + srcPR->h; y++)
|
|
|
|
{
|
2007-12-17 17:16:21 +08:00
|
|
|
const guchar *s = src;
|
|
|
|
gint x;
|
2004-08-31 04:43:48 +08:00
|
|
|
|
|
|
|
for (x = srcPR->x; x < srcPR->x + srcPR->w; x++)
|
|
|
|
{
|
|
|
|
func (x, y, s, srcPR->bpp, data);
|
|
|
|
s += srcPR->bpp;
|
|
|
|
}
|
|
|
|
|
|
|
|
src += srcPR->rowstride;
|
|
|
|
}
|
|
|
|
|
|
|
|
area_so_far += srcPR->w * srcPR->h;
|
2007-12-17 17:16:21 +08:00
|
|
|
|
2009-03-01 01:49:52 +08:00
|
|
|
if ((count % 16) == 0)
|
2007-12-17 17:16:21 +08:00
|
|
|
gimp_progress_update ((gdouble) area_so_far / (gdouble) total_area);
|
2004-08-31 04:43:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-12-17 17:16:21 +08:00
|
|
|
gimp_rgn_render_row (const guchar *src,
|
2004-08-31 04:43:48 +08:00
|
|
|
guchar *dest,
|
|
|
|
gint col, /* row width in pixels */
|
|
|
|
gint bpp,
|
|
|
|
GimpRgnFunc2 func,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
while (col--)
|
|
|
|
{
|
|
|
|
func (src, dest, bpp, data);
|
2007-12-17 17:16:21 +08:00
|
|
|
|
|
|
|
src += bpp;
|
2004-08-31 04:43:48 +08:00
|
|
|
dest += bpp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_rgn_render_region (const GimpPixelRgn *srcPR,
|
|
|
|
const GimpPixelRgn *destPR,
|
|
|
|
GimpRgnFunc2 func,
|
|
|
|
gpointer data)
|
|
|
|
{
|
2007-12-17 17:16:21 +08:00
|
|
|
const guchar *src = srcPR->data;
|
|
|
|
guchar *dest = destPR->data;
|
|
|
|
gint row;
|
2004-08-31 04:43:48 +08:00
|
|
|
|
|
|
|
for (row = 0; row < srcPR->h; row++)
|
|
|
|
{
|
|
|
|
gimp_rgn_render_row (src, dest, srcPR->w, srcPR->bpp, func, data);
|
|
|
|
|
|
|
|
src += srcPR->rowstride;
|
|
|
|
dest += destPR->rowstride;
|
|
|
|
}
|
|
|
|
}
|