gimage.c 73.7 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/* The GIMP -- an image manipulation program
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <math.h>
#include "appenv.h"
#include "channels_dialog.h"
#include "drawable.h"
#include "errors.h"
#include "floating_sel.h"
#include "gdisplay.h"
#include "general.h"
#include "gimage.h"
#include "gimage_mask.h"
#include "indexed_palette.h"
#include "interface.h"
#include "layers_dialog.h"
#include "paint_funcs.h"
#include "palette.h"
#include "plug_in.h"
#include "undo.h"

40 41 42 43
#include "tile_manager_pvt.h"		/* ick. */
#include "layer_pvt.h"
#include "drawable_pvt.h"		/* ick ick. */

Elliot Lee's avatar
Elliot Lee committed
44 45 46 47 48 49 50 51 52 53
/*  Local function declarations  */
static void     gimage_free_projection       (GImage *);
static void     gimage_allocate_shadow       (GImage *, int, int, int);
static GImage * gimage_create                (void);
static void     gimage_allocate_projection   (GImage *);
static void     gimage_free_layers           (GImage *);
static void     gimage_free_channels         (GImage *);
static void     gimage_construct_layers      (GImage *, int, int, int, int);
static void     gimage_construct_channels    (GImage *, int, int, int, int);
static void     gimage_initialize_projection (GImage *, int, int, int, int);
54
static void     gimage_get_active_channels   (GImage *, GimpDrawable *, int *);
Elliot Lee's avatar
Elliot Lee committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

/*  projection functions  */
static void     project_intensity            (GImage *, Layer *, PixelRegion *,
					      PixelRegion *, PixelRegion *);
static void     project_intensity_alpha      (GImage *, Layer *, PixelRegion *,
					      PixelRegion *, PixelRegion *);
static void     project_indexed              (GImage *, Layer *, PixelRegion *,
					      PixelRegion *);
static void     project_channel              (GImage *, Channel *, PixelRegion *,
					      PixelRegion *);

/*
 *  Global variables
 */
int valid_combinations[][MAX_CHANNELS + 1] =
{
  /* RGB GIMAGE */
  { -1, -1, -1, COMBINE_INTEN_INTEN, COMBINE_INTEN_INTEN_A },
  /* RGBA GIMAGE */
  { -1, -1, -1, COMBINE_INTEN_A_INTEN, COMBINE_INTEN_A_INTEN_A },
  /* GRAY GIMAGE */
  { -1, COMBINE_INTEN_INTEN, COMBINE_INTEN_INTEN_A, -1, -1 },
  /* GRAYA GIMAGE */
  { -1, COMBINE_INTEN_A_INTEN, COMBINE_INTEN_A_INTEN_A, -1, -1 },
  /* INDEXED GIMAGE */
  { -1, COMBINE_INDEXED_INDEXED, COMBINE_INDEXED_INDEXED_A, -1, -1 },
  /* INDEXEDA GIMAGE */
  { -1, -1, COMBINE_INDEXED_A_INDEXED_A, -1, -1 },
};


/*
 *  Static variables
 */
static int global_gimage_ID = 1;
link_ptr image_list = NULL;


/* static functions */

static GImage *
gimage_create (void)
{
  GImage *gimage;

  gimage = (GImage *) g_malloc (sizeof (GImage));

  gimage->has_filename = 0;
  gimage->num_cols = 0;
  gimage->cmap = NULL;
  gimage->ID = global_gimage_ID++;
  gimage->ref_count = 0;
  gimage->instance_count = 0;
  gimage->shadow = NULL;
  gimage->dirty = 1;
  gimage->undo_on = TRUE;
  gimage->flat = TRUE;
  gimage->construct_flag = -1;
  gimage->projection = NULL;
  gimage->guides = NULL;
  gimage->layers = NULL;
  gimage->channels = NULL;
  gimage->layer_stack = NULL;
  gimage->undo_stack = NULL;
  gimage->redo_stack = NULL;
  gimage->undo_bytes = 0;
  gimage->undo_levels = 0;
  gimage->pushing_undo_group = 0;
  gimage->comp_preview_valid[0] = FALSE;
  gimage->comp_preview_valid[1] = FALSE;
  gimage->comp_preview_valid[2] = FALSE;
  gimage->comp_preview = NULL;

  image_list = append_to_list (image_list, (void *) gimage);

  return gimage;
}

static void
gimage_allocate_projection (GImage *gimage)
{
  if (gimage->projection)
    gimage_free_projection (gimage);

  /*  Find the number of bytes required for the projection.
   *  This includes the intensity channels and an alpha channel
   *  if one doesn't exist.
   */
  switch (gimage_base_type (gimage))
    {
    case RGB:
    case INDEXED:
      gimage->proj_bytes = 4;
      gimage->proj_type = RGBA_GIMAGE;
      break;
    case GRAY:
      gimage->proj_bytes = 2;
      gimage->proj_type = GRAYA_GIMAGE;
      break;
    default:
      warning ("gimage type unsupported.\n");
      break;
    }

  /*  allocate the new projection  */
  gimage->projection = tile_manager_new (gimage->width, gimage->height, gimage->proj_bytes);
  gimage->projection->user_data = (void *) gimage;
  tile_manager_set_validate_proc (gimage->projection, gimage_validate);
}

static void
gimage_free_projection (GImage *gimage)
{
  if (gimage->projection)
    tile_manager_destroy (gimage->projection);

  gimage->projection = NULL;
}

static void
gimage_allocate_shadow (GImage *gimage, int width, int height, int bpp)
{
  /*  allocate the new projection  */
  gimage->shadow = tile_manager_new (width, height, bpp);
}


/* function definitions */

GImage *
gimage_new (int width, int height, int base_type)
{
  GImage *gimage;
  int i;

  gimage = gimage_create ();

  gimage->filename = NULL;
  gimage->width = width;
  gimage->height = height;

  gimage->base_type = base_type;

  switch (base_type)
    {
    case RGB:
    case GRAY:
      break;
    case INDEXED:
      /* always allocate 256 colors for the colormap */
      gimage->num_cols = 0;
      gimage->cmap = (unsigned char *) g_malloc (COLORMAP_SIZE);
      memset (gimage->cmap, 0, COLORMAP_SIZE);
      break;
    default:
      break;
    }

  /*  configure the active pointers  */
214 215 216
  gimage->active_layer = NULL;
  gimage->active_channel = NULL;  /* no default active channel */
  gimage->floating_sel = NULL;
Elliot Lee's avatar
Elliot Lee committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 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

  /*  set all color channels visible and active  */
  for (i = 0; i < MAX_CHANNELS; i++)
    {
      gimage->visible[i] = 1;
      gimage->active[i] = 1;
    }

  /* create the selection mask */
  gimage->selection_mask = channel_new_mask (gimage->ID, gimage->width, gimage->height);

  lc_dialog_update_image_list ();
  indexed_palette_update_image_list ();

  return gimage;
}


void
gimage_set_filename (GImage *gimage, char *filename)
{
  char *new_filename;

  new_filename = g_strdup (filename);
  if (gimage->has_filename)
    g_free (gimage->filename);

  if (filename && filename[0])
    {
      gimage->filename = new_filename;
      gimage->has_filename = TRUE;
    }
  else
    {
      gimage->filename = NULL;
      gimage->has_filename = FALSE;
    }

  gdisplays_update_title (gimage->ID);
  lc_dialog_update_image_list ();
  indexed_palette_update_image_list ();
}


void
gimage_resize (GImage *gimage, int new_width, int new_height,
	       int offset_x, int offset_y)
{
  Channel *channel;
  Layer *layer;
  Layer *floating_layer;
  link_ptr list;

  if (new_width <= 0 || new_height <= 0) 
    {
      warning("gimage_resize: width and height must be positive");
      return;
    }

  /*  Get the floating layer if one exists  */
  floating_layer = gimage_floating_sel (gimage);

  undo_push_group_start (gimage, GIMAGE_MOD_UNDO);

  /*  Relax the floating selection  */
  if (floating_layer)
    floating_sel_relax (floating_layer, TRUE);

  /*  Push the image size to the stack  */
  undo_push_gimage_mod (gimage);

  /*  Set the new width and height  */
  gimage->width = new_width;
  gimage->height = new_height;

  /*  Resize all channels  */
  list = gimage->channels;
  while (list)
    {
      channel = (Channel *) list->data;
      channel_resize (channel, new_width, new_height, offset_x, offset_y);
      list = next_item (list);
    }

  /*  Don't forget the selection mask!  */
  channel_resize (gimage->selection_mask, new_width, new_height, offset_x, offset_y);
  gimage_mask_invalidate (gimage);

  /*  Reposition all layers  */
  list = gimage->layers;
  while (list)
    {
      layer = (Layer *) list->data;
      layer_translate (layer, offset_x, offset_y);
      list = next_item (list);
    }

  /*  Make sure the projection matches the gimage size  */
  gimage_projection_realloc (gimage);

  /*  Rigor the floating selection  */
  if (floating_layer)
    floating_sel_rigor (floating_layer, TRUE);

  undo_push_group_end (gimage);

  /*  shrink wrap and update all views  */
  channel_invalidate_previews (gimage->ID);
  layer_invalidate_previews (gimage->ID);
  gimage_invalidate_preview (gimage);
  gdisplays_update_full (gimage->ID);
  gdisplays_shrink_wrap (gimage->ID);
}


void
gimage_scale (GImage *gimage, int new_width, int new_height)
{
  Channel *channel;
  Layer *layer;
  Layer *floating_layer;
  link_ptr list;
  int old_width, old_height;
  int layer_width, layer_height;

  /*  Get the floating layer if one exists  */
  floating_layer = gimage_floating_sel (gimage);

  undo_push_group_start (gimage, GIMAGE_MOD_UNDO);

  /*  Relax the floating selection  */
  if (floating_layer)
    floating_sel_relax (floating_layer, TRUE);

  /*  Push the image size to the stack  */
  undo_push_gimage_mod (gimage);

  /*  Set the new width and height  */
  old_width = gimage->width;
  old_height = gimage->height;
  gimage->width = new_width;
  gimage->height = new_height;

  /*  Scale all channels  */
  list = gimage->channels;
  while (list)
    {
      channel = (Channel *) list->data;
      channel_scale (channel, new_width, new_height);
      list = next_item (list);
    }

  /*  Don't forget the selection mask!  */
  channel_scale (gimage->selection_mask, new_width, new_height);
  gimage_mask_invalidate (gimage);

  /*  Scale all layers  */
  list = gimage->layers;
  while (list)
    {
      layer = (Layer *) list->data;

379 380
      layer_width = (new_width * drawable_width (GIMP_DRAWABLE(layer))) / old_width;
      layer_height = (new_height * drawable_height (GIMP_DRAWABLE(layer))) / old_height;
Elliot Lee's avatar
Elliot Lee committed
381 382 383 384 385 386 387 388 389 390 391 392 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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
      layer_scale (layer, layer_width, layer_height, FALSE);
      list = next_item (list);
    }

  /*  Make sure the projection matches the gimage size  */
  gimage_projection_realloc (gimage);

  /*  Rigor the floating selection  */
  if (floating_layer)
    floating_sel_rigor (floating_layer, TRUE);

  undo_push_group_end (gimage);

  /*  shrink wrap and update all views  */
  channel_invalidate_previews (gimage->ID);
  layer_invalidate_previews (gimage->ID);
  gimage_invalidate_preview (gimage);
  gdisplays_update_full (gimage->ID);
  gdisplays_shrink_wrap (gimage->ID);
}


GImage *
gimage_get_named (char *name)
{
  link_ptr tmp = image_list;
  GImage *gimage;
  char *str;

  while (tmp)
    {
      gimage = tmp->data;
      str = prune_filename (gimage_filename (gimage));
      if (strcmp (str, name) == 0)
	return gimage;

      tmp = next_item (tmp);
    }

  return NULL;
}


GImage *
gimage_get_ID (int ID)
{
  link_ptr tmp = image_list;
  GImage *gimage;

  while (tmp)
    {
      gimage = (GImage *) tmp->data;
      if (gimage->ID == ID)
	return gimage;

      tmp = next_item (tmp);
    }

  return NULL;
}


TileManager *
gimage_shadow (GImage *gimage, int width, int height, int bpp)
{
  if (gimage->shadow &&
      ((width != gimage->shadow->levels[0].width) ||
       (height != gimage->shadow->levels[0].height) ||
       (bpp != gimage->shadow->levels[0].bpp)))
    gimage_free_shadow (gimage);
  else if (gimage->shadow)
    return gimage->shadow;

  gimage_allocate_shadow (gimage, width, height, bpp);

  return gimage->shadow;
}


void
gimage_free_shadow (GImage *gimage)
{
  /*  Free the shadow buffer from the specified gimage if it exists  */
  if (gimage->shadow)
    tile_manager_destroy (gimage->shadow);

  gimage->shadow = NULL;
}


void
gimage_delete (GImage *gimage)
{
  gimage->ref_count--;

  if (gimage->ref_count <= 0)
    {
      /*  free the undo list  */
      undo_free (gimage);

      /*  remove this image from the global list  */
      image_list = remove_from_list (image_list, (void *) gimage);

      gimage_free_projection (gimage);
      gimage_free_shadow (gimage);

      if (gimage->cmap)
	g_free (gimage->cmap);

      if (gimage->has_filename)
	g_free (gimage->filename);

      gimage_free_layers (gimage);
      gimage_free_channels (gimage);
      channel_delete (gimage->selection_mask);

      g_free (gimage);

      lc_dialog_update_image_list ();

      indexed_palette_update_image_list ();
    }
}

void
506
gimage_apply_image (GImage *gimage, GimpDrawable *drawable, PixelRegion *src2PR,
Elliot Lee's avatar
Elliot Lee committed
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
		    int undo, int opacity, int mode,
		    /*  alternative to using drawable tiles as src1: */
		    TileManager *src1_tiles,
		    int x, int y)
{
  Channel * mask;
  int x1, y1, x2, y2;
  int offset_x, offset_y;
  PixelRegion src1PR, destPR, maskPR;
  int operation;
  int active [MAX_CHANNELS];

  /*  get the selection mask if one exists  */
  mask = (gimage_mask_is_empty (gimage)) ?
    NULL : gimage_get_mask (gimage);

  /*  configure the active channel array  */
524
  gimage_get_active_channels (gimage, drawable, active);
Elliot Lee's avatar
Elliot Lee committed
525 526 527 528

  /*  determine what sort of operation is being attempted and
   *  if it's actually legal...
   */
529
  operation = valid_combinations [drawable_type (drawable)][src2PR->bytes];
Elliot Lee's avatar
Elliot Lee committed
530 531 532 533 534 535 536
  if (operation == -1)
    {
      warning ("gimage_apply_image sent illegal parameters\n");
      return;
    }

  /*  get the layer offsets  */
537
  drawable_offsets (drawable, &offset_x, &offset_y);
Elliot Lee's avatar
Elliot Lee committed
538 539

  /*  make sure the image application coordinates are within gimage bounds  */
540 541 542 543
  x1 = BOUNDS (x, 0, drawable_width (drawable));
  y1 = BOUNDS (y, 0, drawable_height (drawable));
  x2 = BOUNDS (x + src2PR->w, 0, drawable_width (drawable));
  y2 = BOUNDS (y + src2PR->h, 0, drawable_height (drawable));
Elliot Lee's avatar
Elliot Lee committed
544 545 546 547 548 549 550

  if (mask)
    {
      /*  make sure coordinates are in mask bounds ...
       *  we need to add the layer offset to transform coords
       *  into the mask coordinate system
       */
551 552 553 554
      x1 = BOUNDS (x1, -offset_x, drawable_width (GIMP_DRAWABLE(mask)) - offset_x);
      y1 = BOUNDS (y1, -offset_y, drawable_height (GIMP_DRAWABLE(mask)) - offset_y);
      x2 = BOUNDS (x2, -offset_x, drawable_width (GIMP_DRAWABLE(mask)) - offset_x);
      y2 = BOUNDS (y2, -offset_y, drawable_height (GIMP_DRAWABLE(mask)) - offset_y);
Elliot Lee's avatar
Elliot Lee committed
555 556 557 558
    }

  /*  If the calling procedure specified an undo step...  */
  if (undo)
559
    drawable_apply_image (drawable, x1, y1, x2, y2, NULL, FALSE);
Elliot Lee's avatar
Elliot Lee committed
560 561 562 563 564 565 566

  /* configure the pixel regions
   *  If an alternative to using the drawable's data as src1 was provided...
   */
  if (src1_tiles)
    pixel_region_init (&src1PR, src1_tiles, x1, y1, (x2 - x1), (y2 - y1), FALSE);
  else
567 568
    pixel_region_init (&src1PR, drawable_data (drawable), x1, y1, (x2 - x1), (y2 - y1), FALSE);
  pixel_region_init (&destPR, drawable_data (drawable), x1, y1, (x2 - x1), (y2 - y1), TRUE);
Elliot Lee's avatar
Elliot Lee committed
569 570 571 572 573 574 575 576 577 578 579 580 581
  pixel_region_resize (src2PR, src2PR->x + (x1 - x), src2PR->y + (y1 - y), (x2 - x1), (y2 - y1));

  if (mask)
    {
      int mx, my;

      /*  configure the mask pixel region
       *  don't use x1 and y1 because they are in layer
       *  coordinate system.  Need mask coordinate system
       */
      mx = x1 + offset_x;
      my = y1 + offset_y;

582
      pixel_region_init (&maskPR, drawable_data (GIMP_DRAWABLE(mask)), mx, my, (x2 - x1), (y2 - y1), FALSE);
Elliot Lee's avatar
Elliot Lee committed
583 584 585 586 587 588 589 590
      combine_regions (&src1PR, src2PR, &destPR, &maskPR, NULL,
		       opacity, mode, active, operation);
    }
  else
    combine_regions (&src1PR, src2PR, &destPR, NULL, NULL,
		     opacity, mode, active, operation);
}

591 592 593 594 595 596 597 598
/* Similar to gimage_apply_image but works in "replace" mode (i.e.
   transparent pixels in src2 make the result transparent rather
   than opaque.

   Takes an additional mask pixel region as well.

*/
void
599
gimage_replace_image (GImage *gimage, GimpDrawable *drawable, PixelRegion *src2PR,
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
		      int undo, int opacity,
		      PixelRegion *maskPR,
		      int x, int y)
{
  Channel * mask;
  int x1, y1, x2, y2;
  int offset_x, offset_y;
  PixelRegion src1PR, destPR;
  PixelRegion mask2PR, tempPR;
  unsigned char *temp_data;
  int operation;
  int active [MAX_CHANNELS];

  /*  get the selection mask if one exists  */
  mask = (gimage_mask_is_empty (gimage)) ?
    NULL : gimage_get_mask (gimage);

  /*  configure the active channel array  */
618
  gimage_get_active_channels (gimage, drawable, active);
619 620 621 622

  /*  determine what sort of operation is being attempted and
   *  if it's actually legal...
   */
623
  operation = valid_combinations [drawable_type (drawable)][src2PR->bytes];
624 625 626 627 628 629 630
  if (operation == -1)
    {
      warning ("gimage_apply_image sent illegal parameters\n");
      return;
    }

  /*  get the layer offsets  */
631
  drawable_offsets (drawable, &offset_x, &offset_y);
632 633

  /*  make sure the image application coordinates are within gimage bounds  */
634 635 636 637
  x1 = BOUNDS (x, 0, drawable_width (drawable));
  y1 = BOUNDS (y, 0, drawable_height (drawable));
  x2 = BOUNDS (x + src2PR->w, 0, drawable_width (drawable));
  y2 = BOUNDS (y + src2PR->h, 0, drawable_height (drawable));
638 639 640 641 642 643 644

  if (mask)
    {
      /*  make sure coordinates are in mask bounds ...
       *  we need to add the layer offset to transform coords
       *  into the mask coordinate system
       */
645 646 647 648
      x1 = BOUNDS (x1, -offset_x, drawable_width (GIMP_DRAWABLE(mask)) - offset_x);
      y1 = BOUNDS (y1, -offset_y, drawable_height (GIMP_DRAWABLE(mask)) - offset_y);
      x2 = BOUNDS (x2, -offset_x, drawable_width (GIMP_DRAWABLE(mask)) - offset_x);
      y2 = BOUNDS (y2, -offset_y, drawable_height (GIMP_DRAWABLE(mask)) - offset_y);
649 650 651 652
    }

  /*  If the calling procedure specified an undo step...  */
  if (undo)
653
    drawable_apply_image (drawable, x1, y1, x2, y2, NULL, FALSE);
654 655 656 657

  /* configure the pixel regions
   *  If an alternative to using the drawable's data as src1 was provided...
   */
658 659
  pixel_region_init (&src1PR, drawable_data (drawable), x1, y1, (x2 - x1), (y2 - y1), FALSE);
  pixel_region_init (&destPR, drawable_data (drawable), x1, y1, (x2 - x1), (y2 - y1), TRUE);
660 661 662 663 664 665 666 667 668 669 670 671 672
  pixel_region_resize (src2PR, src2PR->x + (x1 - x), src2PR->y + (y1 - y), (x2 - x1), (y2 - y1));

  if (mask)
    {
      int mx, my;

      /*  configure the mask pixel region
       *  don't use x1 and y1 because they are in layer
       *  coordinate system.  Need mask coordinate system
       */
      mx = x1 + offset_x;
      my = y1 + offset_y;

673
      pixel_region_init (&mask2PR, drawable_data (GIMP_DRAWABLE(mask)), mx, my, (x2 - x1), (y2 - y1), FALSE);
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692

      tempPR.bytes = 1;
      tempPR.x = 0;
      tempPR.y = 0;
      tempPR.w = x2 - x1;
      tempPR.h = y2 - y1;
      tempPR.rowstride = mask2PR.rowstride;
      temp_data = g_malloc (tempPR.h * tempPR.rowstride);
      tempPR.data = temp_data;

      copy_region (&mask2PR, &tempPR);

      /* apparently, region operations can mutate some PR data. */
      tempPR.x = 0;
      tempPR.y = 0;
      tempPR.w = x2 - x1;
      tempPR.h = y2 - y1;
      tempPR.data = temp_data;

Manish Singh's avatar
Manish Singh committed
693
      apply_mask_to_region (&tempPR, maskPR, OPAQUE_OPACITY);
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710

      tempPR.x = 0;
      tempPR.y = 0;
      tempPR.w = x2 - x1;
      tempPR.h = y2 - y1;
      tempPR.data = temp_data;

      combine_regions_replace (&src1PR, src2PR, &destPR, &tempPR, NULL,
		       opacity, active, operation);

      g_free (temp_data);
    }
  else
    combine_regions_replace (&src1PR, src2PR, &destPR, maskPR, NULL,
		     opacity, active, operation);
}

Elliot Lee's avatar
Elliot Lee committed
711 712

void
713
gimage_get_foreground (GImage *gimage, GimpDrawable *drawable, unsigned char *fg)
Elliot Lee's avatar
Elliot Lee committed
714 715 716 717 718 719
{
  unsigned char pfg[3];

  /*  Get the palette color  */
  palette_get_foreground (&pfg[0], &pfg[1], &pfg[2]);

720
  gimage_transform_color (gimage, drawable, pfg, fg, RGB);
Elliot Lee's avatar
Elliot Lee committed
721 722 723 724
}


void
725
gimage_get_background (GImage *gimage, GimpDrawable *drawable, unsigned char *bg)
Elliot Lee's avatar
Elliot Lee committed
726 727 728 729 730 731
{
  unsigned char pbg[3];

  /*  Get the palette color  */
  palette_get_background (&pbg[0], &pbg[1], &pbg[2]);

732
  gimage_transform_color (gimage, drawable, pbg, bg, RGB);
Elliot Lee's avatar
Elliot Lee committed
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
}


void
gimage_get_color (GImage *gimage, int d_type,
		  unsigned char *rgb, unsigned char *src)
{
  switch (d_type)
    {
    case RGB_GIMAGE: case RGBA_GIMAGE:
      map_to_color (0, NULL, src, rgb);
      break;
    case GRAY_GIMAGE: case GRAYA_GIMAGE:
      map_to_color (1, NULL, src, rgb);
      break;
    case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
      map_to_color (2, gimage->cmap, src, rgb);
      break;
    }
}


void
756
gimage_transform_color (GImage *gimage, GimpDrawable *drawable,
Elliot Lee's avatar
Elliot Lee committed
757 758 759 760 761
			unsigned char *src, unsigned char *dest, int type)
{
#define INTENSITY(r,g,b) (r * 0.30 + g * 0.59 + b * 0.11 + 0.001)
  int d_type;

762
  d_type = (drawable != NULL) ? drawable_type (drawable) :
Elliot Lee's avatar
Elliot Lee committed
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
    gimage_base_type_with_alpha (gimage);

  switch (type)
    {
    case RGB:
      switch (d_type)
	{
	case RGB_GIMAGE: case RGBA_GIMAGE:
	  /*  Straight copy  */
	  *dest++ = *src++;
	  *dest++ = *src++;
	  *dest++ = *src++;
	  break;
	case GRAY_GIMAGE: case GRAYA_GIMAGE:
	  /*  NTSC conversion  */
	  *dest = INTENSITY (src[RED_PIX],
			     src[GREEN_PIX],
			     src[BLUE_PIX]);
	  break;
	case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
	  /*  Least squares method  */
	  *dest = map_rgb_to_indexed (gimage->cmap,
				      gimage->num_cols,
				      gimage->ID,
				      src[RED_PIX],
				      src[GREEN_PIX],
				      src[BLUE_PIX]);
	  break;
	}
      break;
    case GRAY:
      switch (d_type)
	{
	case RGB_GIMAGE: case RGBA_GIMAGE:
	  /*  Gray to RG&B */
	  *dest++ = *src;
	  *dest++ = *src;
	  *dest++ = *src;
	  break;
	case GRAY_GIMAGE: case GRAYA_GIMAGE:
	  /*  Straight copy  */
	  *dest = *src;
	  break;
	case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
	  /*  Least squares method  */
	  *dest = map_rgb_to_indexed (gimage->cmap,
				      gimage->num_cols,
				      gimage->ID,
				      src[GRAY_PIX],
				      src[GRAY_PIX],
				      src[GRAY_PIX]);
	  break;
	}
      break;
    default:
      break;
    }
}

Guide*
gimage_add_hguide (GImage *gimage)
{
  Guide *guide;

  guide = g_new (Guide, 1);
  guide->position = -1;
  guide->orientation = HORIZONTAL_GUIDE;

  gimage->guides = g_list_prepend (gimage->guides, guide);

  return guide;
}

Guide*
gimage_add_vguide (GImage *gimage)
{
  Guide *guide;

  guide = g_new (Guide, 1);
  guide->position = -1;
  guide->orientation = VERTICAL_GUIDE;

  gimage->guides = g_list_prepend (gimage->guides, guide);

  return guide;
}

void
gimage_add_guide (GImage *gimage,
		  Guide  *guide)
{
  gimage->guides = g_list_prepend (gimage->guides, guide);
}

void
gimage_remove_guide (GImage *gimage,
		     Guide  *guide)
{
  gimage->guides = g_list_remove (gimage->guides, guide);
}

void
gimage_delete_guide (GImage *gimage,
		     Guide  *guide)
{
  gimage->guides = g_list_remove (gimage->guides, guide);
  g_free (guide);
}


/************************************************************/
/*  Projection functions                                    */
/************************************************************/

static void
project_intensity (GImage *gimage, Layer *layer,
		   PixelRegion *src, PixelRegion *dest, PixelRegion *mask)
{
  if (! gimage->construct_flag)
    initial_region (src, dest, mask, NULL, layer->opacity,
		    layer->mode, gimage->visible, INITIAL_INTENSITY);
  else
    combine_regions (dest, src, dest, mask, NULL, layer->opacity,
		     layer->mode, gimage->visible, COMBINE_INTEN_A_INTEN);
}


static void
project_intensity_alpha (GImage *gimage, Layer *layer,
			 PixelRegion *src, PixelRegion *dest,
			 PixelRegion *mask)
{
  if (! gimage->construct_flag)
    initial_region (src, dest, mask, NULL, layer->opacity,
		    layer->mode, gimage->visible, INITIAL_INTENSITY_ALPHA);
  else
    combine_regions (dest, src, dest, mask, NULL, layer->opacity,
		     layer->mode, gimage->visible, COMBINE_INTEN_A_INTEN_A);
}


static void
project_indexed (GImage *gimage, Layer *layer,
		 PixelRegion *src, PixelRegion *dest)
{
  if (! gimage->construct_flag)
    initial_region (src, dest, NULL, gimage->cmap, layer->opacity,
		    layer->mode, gimage->visible, INITIAL_INDEXED);
  else
    warning ("Unable to project indexed image.");
}


static void
project_indexed_alpha (GImage *gimage, Layer *layer,
		       PixelRegion *src, PixelRegion *dest,
		       PixelRegion *mask)
{
  if (! gimage->construct_flag)
    initial_region (src, dest, mask, gimage->cmap, layer->opacity,
		    layer->mode, gimage->visible, INITIAL_INDEXED_ALPHA);
  else
    combine_regions (dest, src, dest, mask, gimage->cmap, layer->opacity,
		     layer->mode, gimage->visible, COMBINE_INTEN_A_INDEXED_A);
}


static void
project_channel (GImage *gimage, Channel *channel,
		 PixelRegion *src, PixelRegion *src2)
{
  int type;

  if (! gimage->construct_flag)
    {
      type = (channel->show_masked) ?
	INITIAL_CHANNEL_MASK : INITIAL_CHANNEL_SELECTION;
      initial_region (src2, src, NULL, channel->col, channel->opacity,
		      NORMAL, NULL, type);
    }
  else
    {
      type = (channel->show_masked) ?
	COMBINE_INTEN_A_CHANNEL_MASK : COMBINE_INTEN_A_CHANNEL_SELECTION;
      combine_regions (src, src2, src, NULL, channel->col, channel->opacity,
		       NORMAL, NULL, type);
    }
}


/************************************************************/
/*  Layer/Channel functions                                 */
/************************************************************/


static void
gimage_free_layers (GImage *gimage)
{
  link_ptr list = gimage->layers;
  Layer * layer;

  while (list)
    {
      layer = (Layer *) list->data;
      layer_delete (layer);
      list = next_item (list);
    }
  free_list (gimage->layers);
  free_list (gimage->layer_stack);
}


static void
gimage_free_channels (GImage *gimage)
{
  link_ptr list = gimage->channels;
  Channel * channel;

  while (list)
    {
      channel = (Channel *) list->data;
      channel_delete (channel);
      list = next_item (list);
    }
  free_list (gimage->channels);
}


static void
gimage_construct_layers (GImage *gimage, int x, int y, int w, int h)
{
  Layer * layer;
  int x1, y1, x2, y2;
  PixelRegion src1PR, src2PR, maskPR;
  PixelRegion * mask;
  link_ptr list = gimage->layers;
  link_ptr reverse_list = NULL;
1000
  int off_x, off_y;
Elliot Lee's avatar
Elliot Lee committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

  /*  composite the floating selection if it exists  */
  if ((layer = gimage_floating_sel (gimage)))
    floating_sel_composite (layer, x, y, w, h, FALSE);

  /*  If all channels are not visible, simply return  */
  switch (gimage_base_type (gimage))
    {
    case RGB:
      if (! gimage_get_component_visible (gimage, Red) &&
	  ! gimage_get_component_visible (gimage, Green) &&
	  ! gimage_get_component_visible (gimage, Blue))
	return;
      break;
    case GRAY:
      if (! gimage_get_component_visible (gimage, Gray))
	return;
      break;
    case INDEXED:
      if (! gimage_get_component_visible (gimage, Indexed))
	return;
      break;
    }

  while (list)
    {
      layer = (Layer *) list->data;

      /*  only add layers that are visible and not floating selections to the list  */
1030
      if (!layer_is_floating_sel (layer) && drawable_visible (GIMP_DRAWABLE(layer)))
Elliot Lee's avatar
Elliot Lee committed
1031 1032 1033 1034 1035 1036 1037 1038
	reverse_list = add_to_list (reverse_list, layer);

      list = next_item (list);
    }

  while (reverse_list)
    {
      layer = (Layer *) reverse_list->data;
1039
      drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
Elliot Lee's avatar
Elliot Lee committed
1040

1041 1042 1043 1044
      x1 = BOUNDS (off_x, x, x + w);
      y1 = BOUNDS (off_y, y, y + h);
      x2 = BOUNDS (off_x + drawable_width (GIMP_DRAWABLE(layer)), x, x + w);
      y2 = BOUNDS (off_y + drawable_height (GIMP_DRAWABLE(layer)), y, y + h);
Elliot Lee's avatar
Elliot Lee committed
1045 1046 1047 1048 1049 1050 1051

      /* configure the pixel regions  */
      pixel_region_init (&src1PR, gimage_projection (gimage), x1, y1, (x2 - x1), (y2 - y1), TRUE);

      /*  If we're showing the layer mask instead of the layer...  */
      if (layer->mask && layer->show_mask)
	{
1052 1053
	  pixel_region_init (&src2PR, drawable_data (GIMP_DRAWABLE(layer->mask)),
			     (x1 - off_x), (y1 - off_y),
Elliot Lee's avatar
Elliot Lee committed
1054 1055 1056 1057 1058 1059 1060
			     (x2 - x1), (y2 - y1), FALSE);

	  copy_gray_to_region (&src2PR, &src1PR);
	}
      /*  Otherwise, normal  */
      else
	{
1061 1062
	  pixel_region_init (&src2PR, drawable_data (GIMP_DRAWABLE(layer)),
			     (x1 - off_x), (y1 - off_y),
Elliot Lee's avatar
Elliot Lee committed
1063 1064 1065 1066
			     (x2 - x1), (y2 - y1), FALSE);

	  if (layer->mask && layer->apply_mask)
	    {
1067 1068
	      pixel_region_init (&maskPR, drawable_data (GIMP_DRAWABLE(layer->mask)),
				 (x1 - off_x), (y1 - off_y),
Elliot Lee's avatar
Elliot Lee committed
1069 1070 1071 1072 1073 1074 1075 1076 1077
				 (x2 - x1), (y2 - y1), FALSE);
	      mask = &maskPR;
	    }
	  else
	    mask = NULL;

	  /*  Based on the type of the layer, project the layer onto the
	   *  projection image...
	   */
1078
	  switch (drawable_type (GIMP_DRAWABLE(layer)))
Elliot Lee's avatar
Elliot Lee committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
	    {
	    case RGB_GIMAGE: case GRAY_GIMAGE:
	      /* no mask possible */
	      project_intensity (gimage, layer, &src2PR, &src1PR, mask);
	      break;
	    case RGBA_GIMAGE: case GRAYA_GIMAGE:
	      project_intensity_alpha (gimage, layer, &src2PR, &src1PR, mask);
	      break;
	    case INDEXED_GIMAGE:
	      /* no mask possible */
	      project_indexed (gimage, layer, &src2PR, &src1PR);
	      break;
	    case INDEXEDA_GIMAGE:
	      project_indexed_alpha (gimage, layer, &src2PR, &src1PR, mask);
	      break;
	    default:
	      break;
	    }
	}
      gimage->construct_flag = 1;  /*  something was projected  */

      reverse_list = next_item (reverse_list);
    }

  free_list (reverse_list);
}


static void
gimage_construct_channels (GImage *gimage, int x, int y, int w, int h)
{
  Channel * channel;
  PixelRegion src1PR, src2PR;
  link_ptr list = gimage->channels;
  link_ptr reverse_list = NULL;

  /*  reverse the channel list  */
  while (list)
    {
      reverse_list = add_to_list (reverse_list, list->data);
      list = next_item (list);
    }

  while (reverse_list)
    {
      channel = (Channel *) reverse_list->data;

1126
      if (drawable_visible (GIMP_DRAWABLE(channel)))
Elliot Lee's avatar
Elliot Lee committed
1127 1128 1129
	{
	  /* configure the pixel regions  */
	  pixel_region_init (&src1PR, gimage_projection (gimage), x, y, w, h, TRUE);
1130
	  pixel_region_init (&src2PR, drawable_data (GIMP_DRAWABLE(channel)), x, y, w, h, FALSE);
Elliot Lee's avatar
Elliot Lee committed
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

	  project_channel (gimage, channel, &src1PR, &src2PR);

	  gimage->construct_flag = 1;
	}

      reverse_list = next_item (reverse_list);
    }

  free_list (reverse_list);
}


static void
gimage_initialize_projection (GImage *gimage, int x, int y, int w, int h)
{
  link_ptr list;
  Layer *layer;
  int coverage = 0;
  PixelRegion PR;
  unsigned char clear[4] = { 0, 0, 0, 0 };

  /*  this function determines whether a visible layer
   *  provides complete coverage over the image.  If not,
   *  the projection is initialized to transparent
   */
  list = gimage->layers;
  while (list)
    {
1160
      int off_x, off_y;
Elliot Lee's avatar
Elliot Lee committed
1161
      layer = (Layer *) list->data;
1162 1163
      drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
      if (drawable_visible (GIMP_DRAWABLE(layer)) &&
Elliot Lee's avatar
Elliot Lee committed
1164
	  ! layer_has_alpha (layer) &&
1165 1166 1167 1168
	  (off_x <= x) &&
	  (off_y <= y) &&
	  (off_x + drawable_width (GIMP_DRAWABLE(layer)) >= x + w) &&
	  (off_y + drawable_height (GIMP_DRAWABLE(layer)) >= y + h))
Elliot Lee's avatar
Elliot Lee committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
	coverage = 1;

      list = next_item (list);
    }

  if (!coverage)
    {
      pixel_region_init (&PR, gimage_projection (gimage), x, y, w, h, TRUE);
      color_region (&PR, clear);
    }
}


static void
1183
gimage_get_active_channels (GImage *gimage, GimpDrawable *drawable, int *active)
Elliot Lee's avatar
Elliot Lee committed
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
{
  Layer * layer;
  int i;

  /*  first, blindly copy the gimage active channels  */
  for (i = 0; i < MAX_CHANNELS; i++)
    active[i] = gimage->active[i];

  /*  If the drawable is a channel (a saved selection, etc.)
   *  make sure that the alpha channel is not valid
   */
1195
  if (drawable_channel (drawable) != NULL)
Elliot Lee's avatar
Elliot Lee committed
1196 1197 1198 1199 1200 1201
    active[ALPHA_G_PIX] = 0;  /*  no alpha values in channels  */
  else
    {
      /*  otherwise, check whether preserve transparency is
       *  enabled in the layer and if the layer has alpha
       */
1202
      if ((layer = drawable_layer (drawable)))
Elliot Lee's avatar
Elliot Lee committed
1203
	if (layer_has_alpha (layer) && layer->preserve_trans)
1204
	  active[drawable_bytes (GIMP_DRAWABLE(layer)) - 1] = 0;
Elliot Lee's avatar
Elliot Lee committed
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
    }
}


void
gimage_inflate (GImage *gimage)
{
  /*  Make sure the projection image is allocated  */
  gimage_allocate_projection (gimage);

  gimage->flat = FALSE;

  /*  update the gdisplay titles  */
  gdisplays_update_title (gimage->ID);
}


void
gimage_deflate (GImage *gimage)
{
  /*  Make sure the projection image is deallocated  */
  gimage_free_projection (gimage);

  gimage->flat = TRUE;

  /*  update the gdisplay titles  */
  gdisplays_update_title (gimage->ID);
}


void
gimage_construct (GImage *gimage, int x, int y, int w, int h)
{
  /*  if the gimage is not flat, construction is necessary.  */
  if (! gimage_is_flat (gimage))
    {
      /*  set the construct flag, used to determine if anything
       *  has been written to the gimage raw image yet.
       */
      gimage->construct_flag = 0;

      /*  First, determine if the projection image needs to be
       *  initialized--this is the case when there are no visible
       *  layers that cover the entire canvas--either because layers
       *  are offset or only a floating selection is visible
       */
      gimage_initialize_projection (gimage, x, y, w, h);

      /*  call functions which process the list of layers and
       *  the list of channels
       */
      gimage_construct_layers (gimage, x, y, w, h);
      gimage_construct_channels (gimage, x, y, w, h);
    }
}

void
gimage_invalidate (GImage *gimage, int x, int y, int w, int h, int x1, int y1,
		   int x2, int y2)
{
  Tile *tile;
  TileManager *tm;
  int i, j;
  int startx, starty;
  int endx, endy;
  int tilex, tiley;
  int flat;

  flat = gimage_is_flat (gimage);
  tm = gimage_projection (gimage);

  startx = x;
  starty = y;
  endx = x + w;
  endy = y + h;

  /*  invalidate all tiles which are located outside of the displayed area
   *   all tiles inside the displayed area are constructed.
   */
  for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
    for (j = x; j < (x + w); j += (TILE_WIDTH - (j % TILE_WIDTH)))
      {
	tile = tile_manager_get_tile (tm, j, i, 0);

	/*  invalidate all lower level tiles  */
	/*tile_manager_invalidate_tiles (gimage_projection (gimage), tile);*/

	if (! flat)
	  {
	    /*  check if the tile is outside the bounds  */
	    if ((MIN ((j + tile->ewidth), x2) - MAX (j, x1)) <= 0)
	      {
		tile->valid = FALSE;
		if (j < x1)
		  startx = MAX (startx, (j + tile->ewidth));
		else
		  endx = MIN (endx, j);
	      }
	    else if (MIN ((i + tile->eheight), y2) - MAX (i, y1) <= 0)
	      {
		tile->valid = FALSE;
		if (i < y1)
		  starty = MAX (starty, (i + tile->eheight));
		else
		  endy = MIN (endy, i);
	      }
	    else
	      {
		/*  If the tile is not valid, make sure we get the entire tile
		 *   in the construction extents
		 */
		if (tile->valid == FALSE)
		  {
		    tilex = j - (j % TILE_WIDTH);
		    tiley = i - (i % TILE_HEIGHT);

		    startx = MIN (startx, tilex);
		    endx = MAX (endx, tilex + tile->ewidth);
		    starty = MIN (starty, tiley);
		    endy = MAX (endy, tiley + tile->eheight);

		    tile->valid = TRUE;
		  }
	      }
	  }
      }

  if (! flat && (endx - startx) > 0 && (endy - starty) > 0)
    gimage_construct (gimage, startx, starty, (endx - startx), (endy - starty));
}

void
gimage_validate (TileManager *tm, Tile *tile, int level)
{
  GImage *gimage;
  int x, y;
  int w, h;

  /*  Get the gimage from the tilemanager  */
  gimage = (GImage *) tm->user_data;

  /*  Find the coordinates of this tile  */
  x = TILE_WIDTH * (tile->tile_num % tm->levels[0].ntile_cols);
  y = TILE_HEIGHT * (tile->tile_num / tm->levels[0].ntile_cols);
  w = tile->ewidth;
  h = tile->eheight;

  gimage_construct (gimage, x, y, w, h);
}

int
1356
gimage_get_layer_index (GImage *gimage, Layer *layer_arg)
Elliot Lee's avatar
Elliot Lee committed
1357 1358 1359 1360 1361 1362 1363 1364
{
  Layer *layer;
  link_ptr layers = gimage->layers;
  int index = 0;

  while (layers)
    {
      layer = (Layer *) layers->data;
1365
      if (layer == layer_arg)
Elliot Lee's avatar
Elliot Lee committed
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
	return index;

      index++;
      layers = next_item (layers);
    }

  return -1;
}

int
1376
gimage_get_channel_index (GImage *gimage, Channel *channel_ID)
Elliot Lee's avatar
Elliot Lee committed
1377 1378 1379 1380 1381 1382 1383 1384
{
  Channel *channel;
  link_ptr channels = gimage->channels;
  int index = 0;

  while (channels)
    {
      channel = (Channel *) channels->data;
1385
      if (channel == channel_ID)
Elliot Lee's avatar
Elliot Lee committed
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
	return index;

      index++;
      channels = next_item (channels);
    }

  return -1;
}


Layer *
gimage_get_active_layer (GImage *gimage)
{
1399
  return gimage->active_layer;
Elliot Lee's avatar
Elliot Lee committed
1400 1401 1402 1403 1404 1405
}


Channel *
gimage_get_active_channel (GImage *gimage)
{
1406
  return gimage->active_channel;
Elliot Lee's avatar
Elliot Lee committed
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
}


int
gimage_get_component_active (GImage *gimage, ChannelType type)
{
  /*  No sanity checking here...  */
  switch (type)
    {
    case Red:     return gimage->active[RED_PIX]; break;
    case Green:   return gimage->active[GREEN_PIX]; break;
    case Blue:    return gimage->active[BLUE_PIX]; break;
    case Gray:    return gimage->active[GRAY_PIX]; break;
    case Indexed: return gimage->active[INDEXED_PIX]; break;
    default:      return 0; break;
    }
}


int
gimage_get_component_visible (GImage *gimage, ChannelType type)
{
  /*  No sanity checking here...  */
  switch (type)
    {
    case Red:     return gimage->visible[RED_PIX]; break;
    case Green:   return gimage->visible[GREEN_PIX]; break;
    case Blue:    return gimage->visible[BLUE_PIX]; break;
    case Gray:    return gimage->visible[GRAY_PIX]; break;
    case Indexed: return gimage->visible[INDEXED_PIX]; break;
    default:      return 0; break;
    }
}


Channel *
gimage_get_mask (GImage *gimage)
{
  return gimage->selection_mask;
}


int
gimage_layer_boundary (GImage *gimage, BoundSeg **segs, int *num_segs)
{
  Layer *layer;

  /*  The second boundary corresponds to the active layer's
   *  perimeter...
   */
1457
  if ((layer = gimage->active_layer))
Elliot Lee's avatar
Elliot Lee committed
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
    {
      *segs = layer_boundary (layer, num_segs);
      return 1;
    }
  else
    {
      *segs = NULL;
      *num_segs = 0;
      return 0;
    }
}


Layer *
1472
gimage_set_active_layer (GImage *gimage, Layer * layer)
Elliot Lee's avatar
Elliot Lee committed
1473 1474 1475 1476 1477
{

  /*  First, find the layer in the gimage
   *  If it isn't valid, find the first layer that is
   */
1478
  if (gimage_get_layer_index (gimage, layer) == -1)
Elliot Lee's avatar
Elliot Lee committed
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
    {
      if (! gimage->layers)
	return NULL;
      layer = (Layer *) gimage->layers->data;
    }

  if (! layer)
    return NULL;

  /*  Configure the layer stack to reflect this change  */
  gimage->layer_stack = remove_from_list (gimage->layer_stack, (void *) layer);
  gimage->layer_stack = add_to_list (gimage->layer_stack, (void *) layer);

  /*  invalidate the selection boundary because of a layer modification  */
  layer_invalidate_boundary (layer);

  /*  Set the active layer  */
1496 1497
  gimage->active_layer = layer;
  gimage->active_channel = NULL;
Elliot Lee's avatar
Elliot Lee committed
1498 1499 1500 1501 1502 1503 1504

  /*  return the layer  */
  return layer;
}


Channel *
1505
gimage_set_active_channel (GImage *gimage, Channel * channel)
Elliot Lee's avatar
Elliot Lee committed
1506 1507 1508 1509 1510 1511 1512 1513 1514
{

  /*  Not if there is a floating selection  */
  if (gimage_floating_sel (gimage))
    return NULL;

  /*  First, find the channel
   *  If it doesn't exist, find the first channel that does
   */
1515
  if (! channel) 
Elliot Lee's avatar
Elliot Lee committed
1516 1517 1518
    {
      if (! gimage->channels)
	{
1519
	  gimage->active_channel = NULL;
Elliot Lee's avatar
Elliot Lee committed
1520 1521 1522 1523 1524 1525
	  return NULL;
	}
      channel = (Channel *) gimage->channels->data;
    }

  /*  Set the active channel  */
1526
  gimage->active_channel = channel;
Elliot Lee's avatar
Elliot Lee committed
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538

  /*  return the channel  */
  return channel;
}


Channel *
gimage_unset_active_channel (GImage *gimage)
{
  Channel *channel;

  /*  make sure there is an active channel  */
1539
  if (! (channel = gimage->active_channel))
Elliot Lee's avatar
Elliot Lee committed
1540 1541 1542
    return NULL;

  /*  Set the active channel  */
1543
  gimage->active_channel = NULL;
Elliot Lee's avatar
Elliot Lee committed
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610

  return channel;
}


void
gimage_set_component_active (GImage *gimage, ChannelType type, int value)
{
  /*  No sanity checking here...  */
  switch (type)
    {
    case Red:     gimage->active[RED_PIX] = value; break;
    case Green:   gimage->active[GREEN_PIX] = value; break;
    case Blue:    gimage->active[BLUE_PIX] = value; break;
    case Gray:    gimage->active[GRAY_PIX] = value; break;
    case Indexed: gimage->active[INDEXED_PIX] = value; break;
    case Auxillary: break;
    }

  /*  If there is an active channel and we mess with the components,
   *  the active channel gets unset...
   */
  if (type != Auxillary)
    gimage_unset_active_channel (gimage);
}


void
gimage_set_component_visible (GImage *gimage, ChannelType type, int value)
{
  /*  No sanity checking here...  */
  switch (type)
    {
    case Red:     gimage->visible[RED_PIX] = value; break;
    case Green:   gimage->visible[GREEN_PIX] = value; break;
    case Blue:    gimage->visible[BLUE_PIX] = value; break;
    case Gray:    gimage->visible[GRAY_PIX] = value; break;
    case Indexed: gimage->visible[INDEXED_PIX] = value; break;
    default:      break;
    }
}


Layer *
gimage_pick_correlate_layer (GImage *gimage, int x, int y)
{
  Layer *layer;
  link_ptr list;

  list = gimage->layers;
  while (list)
    {
      layer = (Layer *) list->data;
      if (layer_pick_correlate (layer, x, y))
	return layer;

      list = next_item (list);
    }

  return NULL;
}


void
gimage_set_layer_mask_apply (GImage *gimage, int layer_id)
{
  Layer *layer;
1611
  int off_x, off_y;
Elliot Lee's avatar
Elliot Lee committed
1612 1613 1614 1615 1616 1617 1618 1619

  /*  find the layer  */
  if (! (layer = layer_get_ID (layer_id)))
    return;
  if (! layer->mask)
    return;

  layer->apply_mask = ! layer->apply_mask;
1620 1621 1622 1623
  drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
  gdisplays_update_area (gimage->ID, off_x, off_y,
			 drawable_width (GIMP_DRAWABLE(layer)), 
			 drawable_height (GIMP_DRAWABLE(layer)));
Elliot Lee's avatar
Elliot Lee committed
1624 1625 1626 1627
}


void
1628
gimage_set_layer_mask_edit (GImage *gimage, Layer * layer, int edit)
Elliot Lee's avatar
Elliot Lee committed
1629 1630
{
  /*  find the layer  */
1631
  if (!layer)
Elliot Lee's avatar
Elliot Lee committed
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
    return;

  if (layer->mask)
    layer->edit_mask = edit;
}


void
gimage_set_layer_mask_show (GImage *gimage, int layer_id)
{
  Layer *layer;
1643
  int off_x, off_y;
Elliot Lee's avatar
Elliot Lee committed
1644 1645 1646 1647 1648 1649 1650 1651

  /*  find the layer  */
  if (! (layer = layer_get_ID (layer_id)))
    return;
  if (! layer->mask)
    return;

  layer->show_mask = ! layer->show_mask;
1652 1653 1654
  drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
  gdisplays_update_area (gimage->ID, off_x, off_y,
			 drawable_width (GIMP_DRAWABLE(layer)), drawable_height (GIMP_DRAWABLE(layer)));
Elliot Lee's avatar
Elliot Lee committed
1655 1656 1657 1658
}


Layer *
1659
gimage_raise_layer (GImage *gimage, Layer *layer_arg)
Elliot Lee's avatar
Elliot Lee committed
1660 1661 1662 1663 1664 1665 1666
{
  Layer *layer;
  Layer *prev_layer;
  link_ptr list;
  link_ptr prev;
  int x1, y1, x2, y2;
  int index = -1;
1667 1668
  int off_x, off_y;
  int off2_x, off2_y;
Elliot Lee's avatar
Elliot Lee committed
1669 1670 1671 1672 1673 1674 1675 1676 1677 1678

  list = gimage->layers;
  prev = NULL; prev_layer = NULL;

  while (list)
    {
      layer = (Layer *) list->data;
      if (prev)
	prev_layer = (Layer *) prev->data;

1679
      if (layer == layer_arg)
Elliot Lee's avatar
Elliot Lee committed
1680 1681 1682 1683 1684 1685 1686 1687
	{
	  /*  We can only raise a layer if it has an alpha channel &&
	   *  If it's not already the top layer
	   */
	  if (prev && layer_has_alpha (layer) && layer_has_alpha (prev_layer))
	    {
	      list->data = prev_layer;
	      prev->data = layer;
1688 1689
	      drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
	      drawable_offsets (GIMP_DRAWABLE(prev_layer), &off2_x, &off2_y);
Elliot Lee's avatar
Elliot Lee committed
1690 1691

	      /*  calculate minimum area to update  */
1692 1693 1694 1695 1696 1697
	      x1 = MAXIMUM (off_x, off2_x);
	      y1 = MAXIMUM (off_y, off2_y);
	      x2 = MINIMUM (off_x + drawable_width (GIMP_DRAWABLE(layer)),
			    off2_x + drawable_width (GIMP_DRAWABLE(prev_layer)));
	      y2 = MINIMUM (off_y + drawable_height (GIMP_DRAWABLE(layer)),
			    off2_y + drawable_height (GIMP_DRAWABLE(prev_layer)));
Elliot Lee's avatar
Elliot Lee committed
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
	      if ((x2 - x1) > 0 && (y2 - y1) > 0)
		gdisplays_update_area (gimage->ID, x1, y1, (x2 - x1), (y2 - y1));

	      /*  invalidate the composite preview  */
	      gimage_invalidate_preview (gimage);

	      return prev_layer;
	    }
	  else
	    {
	      message_box ("Layer cannot be raised any further", NULL, NULL);
	      return NULL;
	    }
	}

      prev = list;
      index++;
      list = next_item (list);
    }

  return NULL;
}


Layer *
1723
gimage_lower_layer (GImage *gimage, Layer *layer_arg)
Elliot Lee's avatar
Elliot Lee committed
1724 1725 1726 1727 1728 1729 1730
{
  Layer *layer;
  Layer *next_layer;
  link_ptr list;
  link_ptr next;
  int x1, y1, x2, y2;
  int index = 0;
1731 1732
  int off_x, off_y;
  int off2_x, off2_y;
Elliot Lee's avatar
Elliot Lee committed
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746

  next_layer = NULL;

  list = gimage->layers;

  while (list)
    {
      layer = (Layer *) list->data;
      next = next_item (list);

      if (next)
	next_layer = (Layer *) next->data;
      index++;

1747
      if (layer == layer_arg)
Elliot Lee's avatar
Elliot Lee committed
1748 1749 1750 1751 1752 1753 1754 1755 1756
	{
	  /*  We can only lower a layer if it has an alpha channel &&
	   *  The layer beneath it has an alpha channel &&
	   *  If it's not already the bottom layer
	   */
	  if (next && layer_has_alpha (layer) && layer_has_alpha (next_layer))
	    {
	      list->data = next_layer;
	      next->data = layer;
1757 1758
	      drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
	      drawable_offsets (GIMP_DRAWABLE(next_layer), &off2_x, &off2_y);
Elliot Lee's avatar
Elliot Lee committed
1759 1760

	      /*  calculate minimum area to update  */
1761 1762 1763 1764 1765 1766
	      x1 = MAXIMUM (off_x, off2_x);
	      y1 = MAXIMUM (off_y, off2_y);
	      x2 = MINIMUM (off_x + drawable_width (GIMP_DRAWABLE(layer)),
			    off2_x + drawable_width (GIMP_DRAWABLE(next_layer)));
	      y2 = MINIMUM (off_y + drawable_height (GIMP_DRAWABLE(layer)),
			    off2_y + drawable_height (GIMP_DRAWABLE(next_layer)));
Elliot Lee's avatar
Elliot Lee committed
1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
	      if ((x2 - x1) > 0 && (y2 - y1) > 0)
		gdisplays_update_area (gimage->ID, x1, y1, (x2 - x1), (y2 - y1));

	      /*  invalidate the composite preview  */
	      gimage_invalidate_preview (gimage);

	      return next_layer;
	    }
	  else
	    {
	      message_box ("Layer cannot be lowered any further", NULL, NULL);
	      return NULL;
	    }
	}

      list = next;
    }

  return NULL;
}


Layer *
gimage_merge_visible_layers (GImage *gimage, MergeType merge_type)
{
  link_ptr layer_list;
  link_ptr merge_list = NULL;
  Layer *layer;

  layer_list = gimage->layers;
  while (layer_list)
    {
      layer = (Layer *) layer_list->data;
1800
      if (drawable_visible (GIMP_DRAWABLE(layer)))
Elliot Lee's avatar
Elliot Lee committed
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
	merge_list = append_to_list (merge_list, layer);

      layer_list = next_item (layer_list);
    }

  if (merge_list && merge_list->next)
    {
      layer = gimage_merge_layers (gimage, merge_list, merge_type);
      free_list (merge_list);
      return layer;
    }
  else
    {
      message_box ("There are not enough visible layers for a merge.\nThere must be at least two.",
		   NULL, NULL);
      free_list (merge_list);
      return NULL;
    }
}


Layer *
gimage_flatten (GImage *gimage)
{
  link_ptr layer_list;
  link_ptr merge_list = NULL;
  Layer *layer;

  layer_list = gimage->layers;
  while (layer_list)
    {
      layer = (Layer *) layer_list->data;
1833
      if (drawable_visible (GIMP_DRAWABLE(layer)))
Elliot Lee's avatar
Elliot Lee committed
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
	merge_list = append_to_list (merge_list, layer);

      layer_list = next_item (layer_list);
    }

  layer = gimage_merge_layers (gimage, merge_list, FlattenImage);
  free_list (merge_list);
  return layer;
}


Layer *
gimage_merge_layers (GImage *gimage, link_ptr merge_list, MergeType merge_type)
{
  link_ptr reverse_list = NULL;
  PixelRegion src1PR, src2PR, maskPR;
  PixelRegion * mask;
  Layer *merge_layer;
  Layer *layer;
1853
  Layer *bottom;
Elliot Lee's avatar
Elliot Lee committed
1854 1855 1856 1857 1858 1859 1860 1861
  unsigned char bg[4] = {0, 0, 0, 0};
  int type;
  int count;
  int x1, y1, x2, y2;
  int x3, y3, x4, y4;
  int operation;
  int position;
  int active[MAX_CHANNELS] = {1, 1, 1, 1};
1862
  int off_x, off_y;
Elliot Lee's avatar
Elliot Lee committed
1863 1864 1865 1866

  layer = NULL;
  type  = RGBA_GIMAGE;
  x1 = y1 = x2 = y2 = 0;