gimpregioniterator.c 13.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpmisc.c
 * Contains all kinds of miscellaneous routines factored out from different
 * plug-ins. They stay here until their API has crystalized a bit and we can
 * put them into the file where they belong (Maurits Rijk 
 * <lpeek.mrijk@consunet.nl> if you want to blame someone for this mess)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

26 27
#include "config.h"

28
#include <stdio.h>
Hans Breuer's avatar
Hans Breuer committed
29 30

#include <glib.h>
31

32
#include "gimp.h"
33
#include "gimpmisc.h"
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

struct _GimpPixelFetcher
{
  gint          col, row;
  gint          img_width;
  gint	        img_height;
  gint 	        sel_x1, sel_y1, sel_x2, sel_y2;
  gint	        img_bpp;
  gint	        img_has_alpha;
  gint          tile_width, tile_height;
  guchar        bg_color[4];
  GimpDrawable *drawable;
  GimpTile     *tile;
  gboolean      tile_dirty;
  gboolean      shadow;
};

52 53 54 55 56 57
struct _GimpRgnIterator 
{
  GimpDrawable *drawable;
  gint 		x1, y1, x2, y2;
  GimpRunMode   run_mode;
};
58

59 60 61 62 63 64 65 66 67 68 69 70 71
GimpPixelFetcher *
gimp_pixel_fetcher_new (GimpDrawable *drawable)
{
  GimpPixelFetcher *pf;

  pf = g_new (GimpPixelFetcher, 1);

  gimp_drawable_mask_bounds (drawable->drawable_id,
			     &pf->sel_x1, &pf->sel_y1, 
			     &pf->sel_x2, &pf->sel_y2);

  pf->col           = -1;
  pf->row           = -1;
72 73 74
  pf->img_width     = gimp_drawable_width     (drawable->drawable_id);
  pf->img_height    = gimp_drawable_height    (drawable->drawable_id);
  pf->img_bpp       = gimp_drawable_bpp       (drawable->drawable_id);
75 76 77 78 79 80
  pf->img_has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
  pf->tile_width    = gimp_tile_width ();
  pf->tile_height   = gimp_tile_height ();
  pf->bg_color[0]   = 0;
  pf->bg_color[1]   = 0;
  pf->bg_color[2]   = 0;
81
  pf->bg_color[3]   = 255;
82 83 84 85
  pf->drawable      = drawable;
  pf->tile          = NULL;
  pf->tile_dirty    = FALSE;
  pf->shadow	    = FALSE;
86

87 88
  /* this allows us to use (slightly faster) do-while loops */
  g_assert (pf->img_bpp > 0);
89 90 91 92 93 94 95

  return pf;
}

void
gimp_pixel_fetcher_set_bg_color (GimpPixelFetcher *pf)
{
96
  GimpRGB background;
97 98 99 100 101

  gimp_palette_get_background (&background);

  switch (pf->img_bpp)
    {
102
    case 2: pf->bg_color[1] = 255;
103 104 105 106
    case 1:
      pf->bg_color[0] = gimp_rgb_intensity_uchar (&background);
      break;

107
    case 4: pf->bg_color[3] = 255;
108 109 110 111 112 113 114
    case 3:
      gimp_rgb_get_uchar (&background,
			  pf->bg_color, pf->bg_color + 1, pf->bg_color + 2);
      break;
    }
}

115 116
void		 
gimp_pixel_fetcher_set_shadow (GimpPixelFetcher *pf,
117
			       gboolean          shadow)
118 119 120 121
{
  pf->shadow = shadow;
}

122
static guchar *
123
gimp_pixel_fetcher_provide_tile (GimpPixelFetcher *pf,
124 125
				 gint              x,
				 gint              y)
126
{
127 128
  gint col, row;
  gint coloff, rowoff;
129 130 131 132 133 134

  col    = x / pf->tile_width;
  coloff = x % pf->tile_width;
  row    = y / pf->tile_height;
  rowoff = y % pf->tile_height;

135 136 137
  if ((col != pf->col) || (row != pf->row) || (pf->tile == NULL))
    {
      if (pf->tile != NULL)
138
	gimp_tile_unref (pf->tile, pf->tile_dirty);
139

140 141
      pf->tile = gimp_drawable_get_tile (pf->drawable, pf->shadow, row, col);
      pf->tile_dirty = FALSE;
142 143 144 145 146
      gimp_tile_ref (pf->tile);

      pf->col = col;
      pf->row = row;
    }
147 148 149 150 151 152
  
  return pf->tile->data + pf->img_bpp * (pf->tile->ewidth * rowoff + coloff);
}

void
gimp_pixel_fetcher_get_pixel (GimpPixelFetcher *pf,
153 154 155
			      gint              x,
			      gint              y,
			      guchar           *pixel)
156 157 158 159 160 161 162 163 164
{
  guchar *p;
  gint    i;

  if (x < pf->sel_x1 || x >= pf->sel_x2 ||
      y < pf->sel_y1 || y >= pf->sel_y2)
    {
      return;
    }
165

166
  p = gimp_pixel_fetcher_provide_tile (pf, x, y);
167

168 169
  i = pf->img_bpp;
  do
170
    *pixel++ = *p++;
171
  while (--i);
172 173
}

174 175
void
gimp_pixel_fetcher_put_pixel (GimpPixelFetcher *pf,
176 177
			      gint              x,
			      gint              y,
178
			      const guchar     *pixel)
179 180 181 182 183 184 185 186 187 188 189 190
{
  guchar *p;
  gint    i;

  if (x < pf->sel_x1 || x >= pf->sel_x2 ||
      y < pf->sel_y1 || y >= pf->sel_y2)
    {
      return;
    }

  p = gimp_pixel_fetcher_provide_tile (pf, x, y);

191 192
  i = pf->img_bpp;
  do
193
    *p++ = *pixel++;
194
  while (--i);
195 196 197 198

  pf->tile_dirty = TRUE;
}

199 200
void
gimp_pixel_fetcher_get_pixel2 (GimpPixelFetcher *pf,
201 202 203 204
			       gint              x,
			       gint              y,
			       gint		 wrapmode,
			       guchar           *pixel)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
{
  guchar *p;
  gint    i;

  if (x < 0 || x >= pf->img_width ||
      y < 0 || y >= pf->img_height)
    switch (wrapmode)
      {
      case PIXEL_WRAP:
	if (x < 0 || x >= pf->img_width)
	  {
	    x %= pf->img_width;
	    if (x < 0)
	      x += pf->img_width;
	  }
	if (y < 0 || y >= pf->img_height)
	  {
	    y %= pf->img_height;
	    if (y < 0)
	      y += pf->img_height;
	  }
	break;
227

228
      case PIXEL_SMEAR:
229 230
	x = CLAMP (x, 0, pf->img_width - 1);
	y = CLAMP (y, 0, pf->img_height - 1);
231
	break;
232

233 234 235 236
      case PIXEL_BLACK:
	if (x < 0 || x >= pf->img_width || 
	    y < 0 || y >= pf->img_height)
	  {
237 238
	    i = pf->img_bpp;
	    do
239
	      pixel[i] = 0;
240 241
	    while (--i);

242 243 244
	    return;
	  }
	break;
245

246 247 248 249
      default:
	return;
      }

250
  p = gimp_pixel_fetcher_provide_tile (pf, x, y);
251

252 253
  i = pf->img_bpp;
  do
254
    *pixel++ = *p++;
255
  while (--i);
256 257 258 259 260
}

void
gimp_pixel_fetcher_destroy (GimpPixelFetcher *pf)
{
261
  if (pf->tile)
262
    gimp_tile_unref (pf->tile, pf->tile_dirty);
263 264 265

  g_free (pf);
}
266

267 268 269 270 271
static void
gimp_get_color_guchar (GimpDrawable *drawable,
		       GimpRGB	    *color,
		       gboolean      transparent,
		       guchar       *bg)
272 273 274 275
{
  switch (gimp_drawable_type (drawable->drawable_id))
    {
    case GIMP_RGB_IMAGE :
276
      gimp_rgb_get_uchar (color, &bg[0], &bg[1], &bg[2]);
277 278 279 280
      bg[3] = 255;
      break;

    case GIMP_RGBA_IMAGE:
281
      gimp_rgb_get_uchar (color, &bg[0], &bg[1], &bg[2]);
282 283 284 285
      bg[3] = transparent ? 0 : 255;
      break;

    case GIMP_GRAY_IMAGE:
286
      bg[0] = gimp_rgb_intensity_uchar (color);
287 288 289 290
      bg[1] = 255;
      break;

    case GIMP_GRAYA_IMAGE:
291
      bg[0] = gimp_rgb_intensity_uchar (color);
292 293 294 295 296 297 298
      bg[1] = transparent ? 0 : 255;
      break;

    default:
      break;
    }
}
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
void		 
gimp_get_bg_guchar (GimpDrawable *drawable,
		    gboolean      transparent,
		    guchar       *bg)
{
  GimpRGB  background;

  gimp_palette_get_background (&background);
  gimp_get_color_guchar (drawable, &background, transparent, bg);
}

void		 
gimp_get_fg_guchar (GimpDrawable *drawable,
		    gboolean      transparent,
		    guchar       *fg)
{
  GimpRGB  foreground;

  gimp_palette_get_foreground (&foreground);
  gimp_get_color_guchar (drawable, &foreground, transparent, fg);
}

GimpRgnIterator*
gimp_rgn_iterator_new (GimpDrawable *drawable, GimpRunMode run_mode)
{
  GimpRgnIterator *iter = g_new (GimpRgnIterator, 1);
  
  iter->drawable = drawable;
  iter->run_mode = run_mode;
  gimp_drawable_mask_bounds (drawable->drawable_id, &iter->x1, &iter->y1, 
			     &iter->x2, &iter->y2);

  return iter;
}

void
gimp_rgn_iterator_free (GimpRgnIterator *iter)
{
  g_free (iter);
}

static void
gimp_rgn_iterator_iter_single (GimpRgnIterator *iter, GimpPixelRgn *srcPR, 
			       GimpRgnFuncSrc func, gpointer data)
{
  gpointer  pr;
  gint      total_area, area_so_far;

  total_area = (iter->x2 - iter->x1) * (iter->y2 - iter->y1);
  area_so_far   = 0;
  
  for (pr = gimp_pixel_rgns_register (1, srcPR);
       pr != NULL;
       pr = gimp_pixel_rgns_process (pr))
    {
      guchar *src = srcPR->data;
      gint    y;
      
      for (y = srcPR->y; y < srcPR->y + srcPR->h; y++)
	{
	  guchar *s = src;
	  gint x;
	  
	  for (x = srcPR->x; x < srcPR->x + srcPR->w; x++)
	    {
              func (x, y, s, srcPR->bpp, data);
	      s += srcPR->bpp;
	    }
	  
	  src += srcPR->rowstride;
	}
      
      if (iter->run_mode != GIMP_RUN_NONINTERACTIVE)
	{
	  area_so_far += srcPR->w * srcPR->h;
	  gimp_progress_update ((gdouble) area_so_far /
				(gdouble) total_area);
	}
    }
}

void
382 383
gimp_rgn_iterator_src (GimpRgnIterator *iter, 
		       GimpRgnFuncSrc func, 
384 385 386 387 388 389 390 391 392
		       gpointer data)
{
  GimpPixelRgn srcPR;
  
  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);
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
void		 
gimp_rgn_iterator_src_dest (GimpRgnIterator *iter, 
			    GimpRgnFuncSrcDest func, 
			    gpointer data)
{
  GimpPixelRgn srcPR, destPR;
  gint      x1, y1, x2, y2;
  gint	    bpp;
  gpointer  pr;
  gint      total_area, area_so_far;

  x1 = iter->x1;
  y1 = iter->y1;
  x2 = iter->x2;
  y2 = iter->y2;

  total_area = (x2 - x1) * (y2 - y1);
  area_so_far   = 0;

  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;
  
  for (pr = gimp_pixel_rgns_register (2, &srcPR, &destPR);
       pr != NULL;
       pr = gimp_pixel_rgns_process (pr))
    {
      gint    y;
      guchar* src  = srcPR.data;
      guchar* dest = destPR.data;
  
      for (y = srcPR.y; y < srcPR.y + srcPR.h; y++)
	{
	  gint x;
	  guchar *s = src;
	  guchar *d = dest;

	  for (x = srcPR.x; x < srcPR.x + srcPR.w; x++)
	    {
	      func (x, y, s, d, bpp, data);
	      s += bpp;
	      d += bpp;
	    }
	  
	  src  += srcPR.rowstride;
	  dest += destPR.rowstride;
	}

      if (iter->run_mode != GIMP_RUN_NONINTERACTIVE)
	{
	  area_so_far += srcPR.w * srcPR.h;
	  gimp_progress_update ((gdouble) area_so_far /
				(gdouble) total_area);
	}
    }

  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);
}

458 459 460 461 462 463 464 465 466
void
gimp_rgn_iterator_dest (GimpRgnIterator *iter, 
			GimpRgnFuncDest func, 
			gpointer data)
{
  GimpPixelRgn destPR;
  
  gimp_pixel_rgn_init (&destPR, iter->drawable, iter->x1, iter->y1, 
		       iter->x2 - iter->x1, iter->y2 - iter->y1, TRUE, TRUE);
467
  gimp_rgn_iterator_iter_single (iter, &destPR, (GimpRgnFuncSrc) func, data);
468 469 470 471 472 473 474 475
  
  /*  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);
}

476
static void
477 478 479 480 481 482
gimp_rgn_render_row (guchar       *src,
		     guchar       *dest,
		     gint          col,    /* row width in pixels */
		     gint 	   bpp,
		     GimpRgnFunc2  func,
		     gpointer      data)
483 484 485 486 487 488 489 490 491 492 493 494
{
  while (col--)
    {
      func (src, dest, bpp, data);
      src += bpp;
      dest += bpp;
    }
}

static void
gimp_rgn_render_region (const GimpPixelRgn *srcPR,
		        const GimpPixelRgn *destPR,
495 496
		        GimpRgnFunc2        func,
			gpointer            data)
497
{
498
  gint    row;
499 500 501 502 503 504 505 506 507 508 509 510 511
  guchar* src  = srcPR->data;
  guchar* dest = destPR->data;
  
  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;
    }
}

void
512 513 514 515
gimp_rgn_iterate1 (GimpDrawable *drawable,
		   GimpRunMode   run_mode,
		   GimpRgnFunc1  func,
		   gpointer      data)
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
{
  GimpPixelRgn srcPR;
  gint      x1, y1, x2, y2;
  gpointer  pr;
  gint      total_area, area_so_far;
  gint      progress_skip;

  gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);

  total_area = (x2 - x1) * (y2 - y1);

  area_so_far   = 0;
  progress_skip = 0;

  gimp_pixel_rgn_init (&srcPR, drawable,
		       x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);

  for (pr = gimp_pixel_rgns_register (1, &srcPR);
       pr != NULL;
       pr = gimp_pixel_rgns_process (pr))
    {
      guchar *src = srcPR.data;
538
      gint    y;
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557

      for (y = 0; y < srcPR.h; y++)
	{
	  guchar *s = src;
	  gint x;

	  for (x = 0; x < srcPR.w; x++)
	    {
              func (s, srcPR.bpp, data);
	      s += srcPR.bpp;
	    }

	  src += srcPR.rowstride;
	}

      if (run_mode != GIMP_RUN_NONINTERACTIVE)
	{
	  area_so_far += srcPR.w * srcPR.h;
	  if (((progress_skip++) % 10) == 0)
558 559
	    gimp_progress_update ((gdouble) area_so_far /
				  (gdouble) total_area);
560 561 562 563 564
	}
    }
}

void
565 566 567 568
gimp_rgn_iterate2 (GimpDrawable *drawable,
		   GimpRunMode   run_mode, 
		   GimpRgnFunc2  func,
		   gpointer      data)
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
{
  GimpPixelRgn srcPR, destPR;
  gint      x1, y1, x2, y2;
  gpointer  pr;
  gint      total_area, area_so_far;
  gint      progress_skip;

  gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);

  total_area = (x2 - x1) * (y2 - y1);

  area_so_far   = 0;
  progress_skip = 0;

  /* 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);
  
  for (pr = gimp_pixel_rgns_register (2, &srcPR, &destPR);
       pr != NULL;
       pr = gimp_pixel_rgns_process (pr))
    {
      gimp_rgn_render_region (&srcPR, &destPR, func, data);

      if (run_mode != GIMP_RUN_NONINTERACTIVE)
	{
	  area_so_far += srcPR.w * srcPR.h;
	  if (((progress_skip++) % 10) == 0)
599 600
	    gimp_progress_update ((gdouble) area_so_far /
				  (gdouble) total_area);
601 602 603 604 605 606 607 608
	}
    }

  /*  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));
}