paint_core.c 37.2 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
/* 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
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
17 18
 */
#include <stdlib.h>
19
#include <stdio.h>     /* temporary for debuggin */
Elliot Lee's avatar
Elliot Lee committed
20 21 22
#include <string.h>
#include <math.h>
#include "appenv.h"
23
#include "gimpbrushlist.h"
Elliot Lee's avatar
Elliot Lee committed
24 25 26 27 28
#include "channels_dialog.h"
#include "drawable.h"
#include "errors.h"
#include "gdisplay.h"
#include "gimage_mask.h"
29
#include "gimprc.h"
30
#include "gradient.h"  /* for grad_get_color_at() */
Elliot Lee's avatar
Elliot Lee committed
31 32 33 34 35 36 37
#include "layers_dialog.h"
#include "paint_funcs.h"
#include "paint_core.h"
#include "selection.h"
#include "tools.h"
#include "undo.h"

38 39
#include "libgimp/gimpintl.h"

scott's avatar
scott committed
40
#include "tile.h"			/* ick. */
41

Elliot Lee's avatar
Elliot Lee committed
42 43 44 45 46 47 48 49
#define    SQR(x) ((x) * (x))
#define    EPSILON  0.00001

/*  global variables--for use in the various paint tools  */
PaintCore  non_gui_paint_core;

/*  local function prototypes  */
static MaskBuf * paint_core_subsample_mask  (MaskBuf *, double, double);
50
static MaskBuf * paint_core_pressurize_mask  (MaskBuf *, double, double, double);
Elliot Lee's avatar
Elliot Lee committed
51 52
static MaskBuf * paint_core_solidify_mask   (MaskBuf *);
static MaskBuf * paint_core_get_brush_mask  (PaintCore *, int);
53 54
static void      paint_core_paste           (PaintCore *, MaskBuf *, GimpDrawable *, int, int, int, int);
static void      paint_core_replace         (PaintCore *, MaskBuf *, GimpDrawable *, int, int, int);
Elliot Lee's avatar
Elliot Lee committed
55 56
static void      paint_to_canvas_tiles      (PaintCore *, MaskBuf *, int);
static void      paint_to_canvas_buf        (PaintCore *, MaskBuf *, int);
57
static void      set_undo_tiles             (GimpDrawable *, int, int, int, int);
Elliot Lee's avatar
Elliot Lee committed
58
static void      set_canvas_tiles           (int, int, int, int);
59
static int paint_core_invalidate_cache(GimpBrush *brush, gpointer *blah);
Elliot Lee's avatar
Elliot Lee committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/***********************************************************************/


/*  undo blocks variables  */
static TileManager *  undo_tiles = NULL;
static TileManager *  canvas_tiles = NULL;


/***********************************************************************/


/*  paint buffers variables  */
static TempBuf *  orig_buf = NULL;
static TempBuf *  canvas_buf = NULL;


/*  brush buffers  */
78
static MaskBuf *  pressure_brush;
Elliot Lee's avatar
Elliot Lee committed
79
static MaskBuf *  solid_brush;
80
static MaskBuf *  kernel_brushes[5][5];
Elliot Lee's avatar
Elliot Lee committed
81 82 83 84 85 86 87 88 89 90 91 92 93


/*  paint buffers utility functions  */
static void        free_paint_buffers     (void);


/***********************************************************************/


#define KERNEL_WIDTH   3
#define KERNEL_HEIGHT  3

/*  Brush pixel subsampling kernels  */
94
static const int subsample[5][5][9] = {
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
	{
		{ 64, 64, 0, 64, 64, 0, 0, 0, 0, },
		{ 32, 96, 0, 32, 96, 0, 0, 0, 0, },
		{ 0, 128, 0, 0, 128, 0, 0, 0, 0, },
		{ 0, 96, 32, 0, 96, 32, 0, 0, 0, },
		{ 0, 64, 64, 0, 64, 64, 0, 0, 0, },
	},
	{
		{ 32, 32, 0, 96, 96, 0, 0, 0, 0, },
		{ 16, 48, 0, 48, 144, 0, 0, 0, 0, },
		{ 0, 64, 0, 0, 192, 0, 0, 0, 0, },
		{ 0, 48, 16, 0, 144, 48, 0, 0, 0, },
		{ 0, 32, 32, 0, 96, 96, 0, 0, 0, },
	},
	{
		{ 0, 0, 0, 128, 128, 0, 0, 0, 0, },
		{ 0, 0, 0, 64, 192, 0, 0, 0, 0, },
		{ 0, 0, 0, 0, 256, 0, 0, 0, 0, },
		{ 0, 0, 0, 0, 192, 64, 0, 0, 0, },
		{ 0, 0, 0, 0, 128, 128, 0, 0, 0, },
	},
	{
		{ 0, 0, 0, 96, 96, 0, 32, 32, 0, },
		{ 0, 0, 0, 48, 144, 0, 16, 48, 0, },
		{ 0, 0, 0, 0, 192, 0, 0, 64, 0, },
		{ 0, 0, 0, 0, 144, 48, 0, 48, 16, },
		{ 0, 0, 0, 0, 96, 96, 0, 32, 32, },
	},
	{
		{ 0, 0, 0, 64, 64, 0, 64, 64, 0, },
		{ 0, 0, 0, 32, 96, 0, 32, 96, 0, },
		{ 0, 0, 0, 0, 128, 0, 0, 128, 0, },
		{ 0, 0, 0, 0, 96, 32, 0, 96, 32, },
		{ 0, 0, 0, 0, 64, 64, 0, 64, 64, },
	},
Elliot Lee's avatar
Elliot Lee committed
130 131 132 133 134 135 136 137 138 139 140 141
};

void
paint_core_button_press (tool, bevent, gdisp_ptr)
     Tool *tool;
     GdkEventButton *bevent;
     gpointer gdisp_ptr;
{
  PaintCore * paint_core;
  GDisplay * gdisp;
  int draw_line = 0;
  double x, y;
142
  GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
143 144 145 146 147

  gdisp = (GDisplay *) gdisp_ptr;
  paint_core = (PaintCore *) tool->private;

  gdisplay_untransform_coords_f (gdisp, (double) bevent->x, (double) bevent->y, &x, &y, TRUE);
148
  drawable = gimage_active_drawable (gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
149

150
  if (! paint_core_init (paint_core, drawable, x, y))
Elliot Lee's avatar
Elliot Lee committed
151 152
    return;

153 154 155
  paint_core->curpressure = bevent->pressure;
  paint_core->curxtilt = bevent->xtilt;
  paint_core->curytilt = bevent->ytilt;
Elliot Lee's avatar
Elliot Lee committed
156 157 158 159 160 161 162 163 164
  paint_core->state = bevent->state;

  /*  if this is a new image, reinit the core vals  */
  if (gdisp_ptr != tool->gdisp_ptr ||
      ! (bevent->state & GDK_SHIFT_MASK))
    {
      /*  initialize some values  */
      paint_core->startx = paint_core->lastx = paint_core->curx;
      paint_core->starty = paint_core->lasty = paint_core->cury;
165 166 167
      paint_core->startpressure = paint_core->lastpressure = paint_core->curpressure;
      paint_core->startytilt = paint_core->lastytilt = paint_core->curytilt;
      paint_core->startxtilt = paint_core->lastxtilt = paint_core->curxtilt;
Elliot Lee's avatar
Elliot Lee committed
168 169 170 171 172 173 174 175 176
    }
  /*  If shift is down and this is not the first paint
   *  stroke, then draw a line from the last coords to the pointer
   */
  else if (bevent->state & GDK_SHIFT_MASK)
    {
      draw_line = 1;
      paint_core->startx = paint_core->lastx;
      paint_core->starty = paint_core->lasty;
177 178 179
      paint_core->startpressure = paint_core->lastpressure;
      paint_core->startxtilt = paint_core->lastxtilt;
      paint_core->startytilt = paint_core->lastytilt;
Elliot Lee's avatar
Elliot Lee committed
180 181 182 183 184 185 186
    }

  tool->state = ACTIVE;
  tool->gdisp_ptr = gdisp_ptr;
  tool->paused_count = 0;

  /*  pause the current selection and grab the pointer  */
187
  gdisplays_selection_visibility (gdisp->gimage, SelectionPause);
Elliot Lee's avatar
Elliot Lee committed
188

189 190
  /* add motion memory if you press mod1 first ^ perfectmouse */
  if (((bevent->state & GDK_MOD1_MASK) != 0) != (perfectmouse != 0))
Elliot Lee's avatar
Elliot Lee committed
191 192 193 194 195
    gdk_pointer_grab (gdisp->canvas->window, FALSE,
		      GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
		      NULL, NULL, bevent->time);
  else
    gdk_pointer_grab (gdisp->canvas->window, FALSE,
196 197
		      GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK |
		      GDK_BUTTON_RELEASE_MASK,
Elliot Lee's avatar
Elliot Lee committed
198 199 200
		      NULL, NULL, bevent->time);
  
  /*  Let the specific painting function initialize itself  */
201
  (* paint_core->paint_func) (paint_core, drawable, INIT_PAINT);
Elliot Lee's avatar
Elliot Lee committed
202 203 204 205

  /*  Paint to the image  */
  if (draw_line)
    {
206
      paint_core_interpolate (paint_core, drawable);
Elliot Lee's avatar
Elliot Lee committed
207 208
      paint_core->lastx = paint_core->curx;
      paint_core->lasty = paint_core->cury;
209 210 211
      paint_core->lastpressure = paint_core->curpressure;
      paint_core->lastxtilt = paint_core->curxtilt;
      paint_core->lastytilt = paint_core->curytilt;
Elliot Lee's avatar
Elliot Lee committed
212 213
    }
  else
214
    (* paint_core->paint_func) (paint_core, drawable, MOTION_PAINT);
Elliot Lee's avatar
Elliot Lee committed
215

216
  gdisplay_flush_now (gdisp);
Elliot Lee's avatar
Elliot Lee committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
}

void
paint_core_button_release (tool, bevent, gdisp_ptr)
     Tool *tool;
     GdkEventButton *bevent;
     gpointer gdisp_ptr;
{
  GDisplay * gdisp;
  GImage * gimage;
  PaintCore * paint_core;

  gdisp = (GDisplay *) gdisp_ptr;
  gimage = gdisp->gimage;
  paint_core = (PaintCore *) tool->private;

  /*  resume the current selection and ungrab the pointer  */
234
  gdisplays_selection_visibility (gdisp->gimage, SelectionResume);
Elliot Lee's avatar
Elliot Lee committed
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

  gdk_pointer_ungrab (bevent->time);
  gdk_flush ();

  /*  Let the specific painting function finish up  */
  (* paint_core->paint_func) (paint_core, gimage_active_drawable (gdisp->gimage), FINISH_PAINT);

  /*  Set tool state to inactive -- no longer painting */
  tool->state = INACTIVE;

  paint_core_finish (paint_core, gimage_active_drawable (gdisp->gimage), tool->ID);
  gdisplays_flush ();
}

void
paint_core_motion (tool, mevent, gdisp_ptr)
     Tool *tool;
     GdkEventMotion *mevent;
     gpointer gdisp_ptr;
{
  GDisplay * gdisp;
  PaintCore * paint_core;

  gdisp = (GDisplay *) gdisp_ptr;
  paint_core = (PaintCore *) tool->private;

  gdisplay_untransform_coords_f (gdisp, (double) mevent->x, (double) mevent->y,
				 &paint_core->curx, &paint_core->cury, TRUE);
263 264 265
  paint_core->curpressure = mevent->pressure;
  paint_core->curxtilt = mevent->xtilt;
  paint_core->curytilt = mevent->ytilt;
Elliot Lee's avatar
Elliot Lee committed
266 267 268 269
  paint_core->state = mevent->state;

  paint_core_interpolate (paint_core, gimage_active_drawable (gdisp->gimage));

270
  gdisplay_flush_now (gdisp);
Elliot Lee's avatar
Elliot Lee committed
271 272 273

  paint_core->lastx = paint_core->curx;
  paint_core->lasty = paint_core->cury;
274 275 276
  paint_core->lastpressure = paint_core->curpressure;
  paint_core->lastxtilt = paint_core->curxtilt;
  paint_core->lastytilt = paint_core->curytilt;
Elliot Lee's avatar
Elliot Lee committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
}

void
paint_core_cursor_update (tool, mevent, gdisp_ptr)
     Tool *tool;
     GdkEventMotion *mevent;
     gpointer gdisp_ptr;
{
  GDisplay *gdisp;
  Layer *layer;
  GdkCursorType ctype = GDK_TOP_LEFT_ARROW;
  int x, y;

  gdisp = (GDisplay *) gdisp_ptr;

  gdisplay_untransform_coords (gdisp, mevent->x, mevent->y, &x, &y, FALSE, FALSE);
293 294 295 296 297 298 299
  if ((layer = gimage_get_active_layer (gdisp->gimage))) 
    {
      int off_x, off_y;
      drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
    if (x >= off_x && y >= off_y &&
	x < (off_x + drawable_width (GIMP_DRAWABLE(layer))) &&
	y < (off_y + drawable_height (GIMP_DRAWABLE(layer))))
Elliot Lee's avatar
Elliot Lee committed
300 301 302 303 304 305 306 307 308
      {
	/*  One more test--is there a selected region?
	 *  if so, is cursor inside?
	 */
	if (gimage_mask_is_empty (gdisp->gimage))
	  ctype = GDK_PENCIL;
	else if (gimage_mask_value (gdisp->gimage, x, y))
	  ctype = GDK_PENCIL;
      }
309
    }
Elliot Lee's avatar
Elliot Lee committed
310 311 312 313 314 315 316 317 318 319 320
  gdisplay_install_tool_cursor (gdisp, ctype);
}

void
paint_core_control (tool, action, gdisp_ptr)
     Tool * tool;
     int action;
     gpointer gdisp_ptr;
{
  PaintCore * paint_core;
  GDisplay *gdisp;
321
  GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
322 323 324

  gdisp = (GDisplay *) gdisp_ptr;
  paint_core = (PaintCore *) tool->private;
325
  drawable = gimage_active_drawable (gdisp->gimage);
Elliot Lee's avatar
Elliot Lee committed
326 327 328 329 330 331 332 333 334 335

  switch (action)
    {
    case PAUSE :
      draw_core_pause (paint_core->core, tool);
      break;
    case RESUME :
      draw_core_resume (paint_core->core, tool);
      break;
    case HALT :
336
      (* paint_core->paint_func) (paint_core, drawable, FINISH_PAINT);
Elliot Lee's avatar
Elliot Lee committed
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
      paint_core_cleanup ();
      break;
    }
}

void
paint_core_no_draw (tool)
     Tool * tool;
{
  return;
}

Tool *
paint_core_new (type)
     int type;
{
  Tool * tool;
  PaintCore * private;

  tool = (Tool *) g_malloc (sizeof (Tool));
  private = (PaintCore *) g_malloc (sizeof (PaintCore));

  private->core = draw_core_new (paint_core_no_draw);

  tool->type = type;
  tool->state = INACTIVE;
  tool->scroll_lock = 0;  /*  Allow scrolling  */
  tool->auto_snap_to = TRUE;
  tool->gdisp_ptr = NULL;
  tool->private = (void *) private;
367
  tool->preserve = TRUE;
Elliot Lee's avatar
Elliot Lee committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

  tool->button_press_func = paint_core_button_press;
  tool->button_release_func = paint_core_button_release;
  tool->motion_func = paint_core_motion;
  tool->arrow_keys_func = standard_arrow_keys_func;
  tool->cursor_update_func = paint_core_cursor_update;
  tool->control_func = paint_core_control;

  return tool;
}

void
paint_core_free (tool)
     Tool * tool;
{
  PaintCore * paint_core;

  paint_core = (PaintCore *) tool->private;

  /*  Make sure the selection core is not visible  */
  if (tool->state == ACTIVE && paint_core->core)
    draw_core_stop (paint_core->core, tool);

  /*  Free the selection core  */
  if (paint_core->core)
    draw_core_free (paint_core->core);

  /*  Cleanup memory  */
  paint_core_cleanup ();

  /*  Free the paint core  */
  g_free (paint_core);
}

int
403
paint_core_init (paint_core, drawable, x, y)
Elliot Lee's avatar
Elliot Lee committed
404
     PaintCore *paint_core;
405
     GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
406 407
     double x, y;
{
408 409
  static GimpBrushP brush = 0;
  
Elliot Lee's avatar
Elliot Lee committed
410 411 412
  paint_core->curx = x;
  paint_core->cury = y;

413 414 415 416 417
  /* Set up some defaults for non-gui use */
  paint_core->startpressure = paint_core->lastpressure = paint_core->curpressure = 0.5;
  paint_core->startxtilt = paint_core->lastxtilt = paint_core->curxtilt = 0;
  paint_core->startytilt = paint_core->lastytilt = paint_core->curytilt = 0;

Elliot Lee's avatar
Elliot Lee committed
418
  /*  Each buffer is the same size as the maximum bounds of the active brush... */
419 420 421 422 423 424 425
  if (brush && brush != get_active_brush ())
  {
    gtk_signal_disconnect_by_func(GTK_OBJECT(brush),
				  GTK_SIGNAL_FUNC(paint_core_invalidate_cache),
				  NULL);
    gtk_object_unref(GTK_OBJECT(brush));
  }
Elliot Lee's avatar
Elliot Lee committed
426 427
  if (!(brush = get_active_brush ()))
    {
428
      g_message (_("No brushes available for use with this tool."));
Elliot Lee's avatar
Elliot Lee committed
429 430
      return FALSE;
    }
431 432 433 434
  gtk_object_ref(GTK_OBJECT(brush));
  gtk_signal_connect(GTK_OBJECT (brush), "dirty",
		     GTK_SIGNAL_FUNC(paint_core_invalidate_cache),
		     NULL);
Elliot Lee's avatar
Elliot Lee committed
435

436 437
  paint_core->spacing = (double) gimp_brush_get_spacing () / 100.0;
/*  paint_core->spacing =
Elliot Lee's avatar
Elliot Lee committed
438
    (double) MAXIMUM (brush->mask->width, brush->mask->height) *
439
    ((double) gimp_brush_get_spacing () / 100.0);
Elliot Lee's avatar
Elliot Lee committed
440
  if (paint_core->spacing < 1.0)
441 442
    paint_core->spacing = 1.0; */
  paint_core->brush = brush;
Elliot Lee's avatar
Elliot Lee committed
443 444 445 446 447 448 449 450

  /*  free the block structures  */
  if (undo_tiles)
    tile_manager_destroy (undo_tiles);
  if (canvas_tiles)
    tile_manager_destroy (canvas_tiles);

  /*  Allocate the undo structure  */
451 452 453
  undo_tiles = tile_manager_new (drawable_width (drawable),
				 drawable_height (drawable),
				 drawable_bytes (drawable));
Elliot Lee's avatar
Elliot Lee committed
454 455

  /*  Allocate the canvas blocks structure  */
456 457
  canvas_tiles = tile_manager_new (drawable_width (drawable),
				   drawable_height (drawable), 1);
Elliot Lee's avatar
Elliot Lee committed
458 459 460 461 462 463 464 465 466

  /*  Get the initial undo extents  */
  paint_core->x1 = paint_core->x2 = paint_core->curx;
  paint_core->y1 = paint_core->y2 = paint_core->cury;
  paint_core->distance = 0.0;

  return TRUE;
}

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 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
void
paint_core_get_color_from_gradient (PaintCore *paint_core, double gradient_length, double *r, double *g, double *b,double *a, int mode)
{
  double y;
  double distance;    /* distance in current brush stroke */
  double position;    /* position in the gradient to ge the color from */
  double temp_opacity;  /* so i can blank out stuff */

  distance = paint_core->distance;
  y = ((double) distance / gradient_length);
  temp_opacity = 1.0; 
 

  /* if were past the first chunk... */
  if((y/gradient_length) > 1.0)
    {
      /* if this is an "odd" chunk..." */
      if((int)(y/gradient_length) & 1)
	{
	  /* draw it "normally" */
	  y = y - (gradient_length*(int)(y/gradient_length));
	}
      /* if this is an "even" chunk... */
      else 
	{
	  /* draw it "backwards"  */
	  switch (mode)
	    {
	    case LOOP_SAWTOOTH:
	      y = y - (gradient_length*(int)(y/gradient_length));
	      break;
	    case LOOP_TRIANGLE:
	      y = gradient_length - fmod(y,gradient_length);
	      break;
	    }
	}
      if(mode == ONCE_FORWARD || mode == ONCE_BACKWARDS)
	{
	  printf("got here \n");
	  /* for the once modes, set alpha to 0.0 */
	  temp_opacity = 0.0;
	}

    }
  /* if this is the first chunk... */
  else
    {
      /* draw it backwards */
      switch (mode)
	{
	case ONCE_FORWARD:
	case ONCE_END_COLOR:
	case LOOP_TRIANGLE:
  	  y = gradient_length - y;
	  break;
	default:
	  /* all the other modes go here ;-> */
	  break;
	}
      /* if it doesnt need to be reveresed, let y be y ;-> */
    }
  /* stolen from the fade effect in paintbrush */

  /* model this on a gaussian curve */
Asbjørn Pettersen's avatar
Asbjørn Pettersen committed
531
  /* position = exp (- y * y * 0.5); */
532 533 534 535 536 537 538 539
  position = y/gradient_length;
  grad_get_color_at(position,r,g,b,a);
  /* set opacity to zero if this isnt a repeater call */
  *a = (temp_opacity * *a);

}
  

Elliot Lee's avatar
Elliot Lee committed
540
void
541
paint_core_interpolate (paint_core, drawable)
Elliot Lee's avatar
Elliot Lee committed
542
     PaintCore *paint_core;
543
     GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
544 545
{
  int n;
546 547
  vector2d delta;
  double dpressure, dxtilt, dytilt;
Elliot Lee's avatar
Elliot Lee committed
548 549 550 551 552
  double left;
  double t;
  double initial;
  double dist;
  double total;
553 554 555 556
  double xd, yd;
  double mag;
  delta.x = paint_core->curx - paint_core->lastx;
  delta.y = paint_core->cury - paint_core->lasty;
557 558 559
  dpressure = paint_core->curpressure - paint_core->lastpressure;
  dxtilt = paint_core->curxtilt - paint_core->lastxtilt;
  dytilt = paint_core->curytilt - paint_core->lastytilt;
Elliot Lee's avatar
Elliot Lee committed
560

561
  if (!delta.x && !delta.y && !dpressure && !dxtilt && !dytilt)
Elliot Lee's avatar
Elliot Lee committed
562
    return;
563 564 565 566 567 568 569 570
  mag = vector2d_magnitude (&(paint_core->brush->x_axis));
  xd = vector2d_dot_product(&delta, &(paint_core->brush->x_axis)) / (mag*mag);

  mag = vector2d_magnitude (&(paint_core->brush->y_axis));
  yd = vector2d_dot_product(&delta, &(paint_core->brush->y_axis)) /
    SQR(vector2d_magnitude(&(paint_core->brush->y_axis)));

  dist = .5 * sqrt (xd*xd + yd*yd);
Elliot Lee's avatar
Elliot Lee committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584

  total = dist + paint_core->distance;
  initial = paint_core->distance;

  while (paint_core->distance < total)
    {
      n = (int) (paint_core->distance / paint_core->spacing + 1.0 + EPSILON);
      left = n * paint_core->spacing - paint_core->distance;
      paint_core->distance += left;

      if (paint_core->distance <= total)
	{
	  t = (paint_core->distance - initial) / dist;

585 586
	  paint_core->curx = paint_core->lastx + delta.x * t;
	  paint_core->cury = paint_core->lasty + delta.y * t;
587 588 589
	  paint_core->curpressure = paint_core->lastpressure + dpressure * t;
	  paint_core->curxtilt = paint_core->lastxtilt + dxtilt * t;
	  paint_core->curytilt = paint_core->lastytilt + dytilt * t;
590
	  (* paint_core->paint_func) (paint_core, drawable, MOTION_PAINT);
Elliot Lee's avatar
Elliot Lee committed
591 592 593 594
	}
    }

  paint_core->distance = total;
595 596
  paint_core->curx = paint_core->lastx + delta.x;
  paint_core->cury = paint_core->lasty + delta.y;
597 598 599
  paint_core->curpressure = paint_core->lastpressure + dpressure;
  paint_core->curxtilt = paint_core->lastxtilt + dxtilt;
  paint_core->curytilt = paint_core->lastytilt + dytilt;
Elliot Lee's avatar
Elliot Lee committed
600 601 602
}

void
603
paint_core_finish (paint_core, drawable, tool_id)
Elliot Lee's avatar
Elliot Lee committed
604
     PaintCore *paint_core;
605
     GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
606 607 608 609 610
     int tool_id;
{
  GImage *gimage;
  PaintUndo *pu;

611
  if (! (gimage = drawable_gimage (drawable)))
Elliot Lee's avatar
Elliot Lee committed
612 613 614 615 616
    return;

  /*  Determine if any part of the image has been altered--
   *  if nothing has, then just return...
   */
617

Elliot Lee's avatar
Elliot Lee committed
618 619 620 621 622 623 624 625 626
  if ((paint_core->x2 == paint_core->x1) || (paint_core->y2 == paint_core->y1))
    return;

  undo_push_group_start (gimage, PAINT_CORE_UNDO);

  pu = (PaintUndo *) g_malloc (sizeof (PaintUndo));
  pu->tool_ID = tool_id;
  pu->lastx = paint_core->startx;
  pu->lasty = paint_core->starty;
627 628 629
  pu->lastpressure = paint_core->startpressure;
  pu->lastxtilt = paint_core->startxtilt;
  pu->lastytilt = paint_core->startytilt;
Elliot Lee's avatar
Elliot Lee committed
630 631 632 633 634

  /*  Push a paint undo  */
  undo_push_paint (gimage, pu);

  /*  push an undo  */
635
  drawable_apply_image (drawable, paint_core->x1, paint_core->y1,
Elliot Lee's avatar
Elliot Lee committed
636 637 638 639 640 641 642 643 644
			paint_core->x2, paint_core->y2, undo_tiles, TRUE);
  undo_tiles = NULL;

  /*  push the group end  */
  undo_push_group_end (gimage);

  /*  invalidate the drawable--have to do it here, because
   *  it is not done during the actual painting.
   */
645
  drawable_invalidate_preview (drawable);
Elliot Lee's avatar
Elliot Lee committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
}

void
paint_core_cleanup ()
{
  /*  CLEANUP  */
  /*  If the undo tiles exist, nuke them  */
  if (undo_tiles)
    {
      tile_manager_destroy (undo_tiles);
      undo_tiles = NULL;
    }

  /*  If the canvas blocks exist, nuke them  */
  if (canvas_tiles)
    {
      tile_manager_destroy (canvas_tiles);
      canvas_tiles = NULL;
    }

  /*  Free the temporary buffers if they exist  */
  free_paint_buffers ();
}


/************************/
/*  Painting functions  */
/************************/

TempBuf *
676
paint_core_get_paint_area (paint_core, drawable)
Elliot Lee's avatar
Elliot Lee committed
677
     PaintCore *paint_core;
678
     GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
679 680 681 682
{
  int x, y;
  int x1, y1, x2, y2;
  int bytes;
683
  int dwidth, dheight;
Elliot Lee's avatar
Elliot Lee committed
684

685 686
  bytes = drawable_has_alpha (drawable) ?
    drawable_bytes (drawable) : drawable_bytes (drawable) + 1;
Elliot Lee's avatar
Elliot Lee committed
687 688

  /*  adjust the x and y coordinates to the upper left corner of the brush  */
689 690
  x = (int) paint_core->curx - (paint_core->brush->mask->width >> 1);
  y = (int) paint_core->cury - (paint_core->brush->mask->height >> 1);
Elliot Lee's avatar
Elliot Lee committed
691

692 693 694 695 696
  dwidth = drawable_width (drawable);
  dheight = drawable_height (drawable);

  x1 = BOUNDS (x - 1, 0, dwidth);
  y1 = BOUNDS (y - 1, 0, dheight);
697
  x2 = BOUNDS (x + paint_core->brush->mask->width + 1,
698
	       0, dwidth);
699
  y2 = BOUNDS (y + paint_core->brush->mask->height + 1,
700
	       0, dheight);
Elliot Lee's avatar
Elliot Lee committed
701 702 703 704 705 706 707 708 709 710 711 712

  /*  configure the canvas buffer  */
  if ((x2 - x1) && (y2 - y1))
    canvas_buf = temp_buf_resize (canvas_buf, bytes, x1, y1,
				  (x2 - x1), (y2 - y1));
  else
    return NULL;

  return canvas_buf;
}

TempBuf *
713
paint_core_get_orig_image (paint_core, drawable, x1, y1, x2, y2)
Elliot Lee's avatar
Elliot Lee committed
714
     PaintCore *paint_core;
715
     GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
716 717 718 719 720 721
     int x1, y1;
     int x2, y2;
{
  PixelRegion srcPR, destPR;
  Tile *undo_tile;
  int h;
722
  int refd;
Elliot Lee's avatar
Elliot Lee committed
723
  int pixelwidth;
724
  int dwidth, dheight;
Elliot Lee's avatar
Elliot Lee committed
725 726 727
  unsigned char * s, * d;
  void * pr;

728
  orig_buf = temp_buf_resize (orig_buf, drawable_bytes (drawable),
Elliot Lee's avatar
Elliot Lee committed
729
			      x1, y1, (x2 - x1), (y2 - y1));
730 731 732 733 734 735 736 737

  dwidth = drawable_width (drawable);
  dheight = drawable_height (drawable);

  x1 = BOUNDS (x1, 0, dwidth);
  y1 = BOUNDS (y1, 0, dheight);
  x2 = BOUNDS (x2, 0, dwidth);
  y2 = BOUNDS (y2, 0, dheight);
Elliot Lee's avatar
Elliot Lee committed
738 739

  /*  configure the pixel regions  */
740 741
  pixel_region_init (&srcPR, drawable_data (drawable), x1, y1,
		     (x2 - x1), (y2 - y1), FALSE);
Elliot Lee's avatar
Elliot Lee committed
742 743 744 745 746 747 748
  destPR.bytes = orig_buf->bytes;
  destPR.x = 0; destPR.y = 0;
  destPR.w = (x2 - x1); destPR.h = (y2 - y1);
  destPR.rowstride = orig_buf->bytes * orig_buf->width;
  destPR.data = temp_buf_data (orig_buf) +
    (y1 - orig_buf->y) * destPR.rowstride + (x1 - orig_buf->x) * destPR.bytes;

749 750 751
  for (pr = pixel_regions_register (2, &srcPR, &destPR);
       pr != NULL;
       pr = pixel_regions_process (pr))
Elliot Lee's avatar
Elliot Lee committed
752 753
    {
      /*  If the undo tile corresponding to this location is valid, use it  */
754
      undo_tile = tile_manager_get_tile (undo_tiles, srcPR.x, srcPR.y,
755
					 FALSE, FALSE);
scott's avatar
scott committed
756
      if (tile_is_valid (undo_tile) == TRUE)
Elliot Lee's avatar
Elliot Lee committed
757
	{
758
	  refd = 1;
759
	  undo_tile = tile_manager_get_tile (undo_tiles, srcPR.x, srcPR.y,
760
					     TRUE, FALSE);
761
	  s = (unsigned char*)tile_data_pointer (undo_tile, 0, 0) +
scott's avatar
scott committed
762 763
	    srcPR.rowstride * (srcPR.y % TILE_HEIGHT) +
	    srcPR.bytes * (srcPR.x % TILE_WIDTH); /* dubious... */
Elliot Lee's avatar
Elliot Lee committed
764 765 766
	}
      else
	{
767
	  refd = 0;
Elliot Lee's avatar
Elliot Lee committed
768 769 770 771 772 773 774 775 776 777 778 779 780
	  s = srcPR.data;
	}

      d = destPR.data;
      pixelwidth = srcPR.w * srcPR.bytes;
      h = srcPR.h;
      while (h --)
	{
	  memcpy (d, s, pixelwidth);
	  s += srcPR.rowstride;
	  d += destPR.rowstride;
	}

781 782
      if (refd)
	tile_release (undo_tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
783 784 785 786 787 788
    }

  return orig_buf;
}

void
789
paint_core_paste_canvas (paint_core, drawable, brush_opacity, image_opacity, paint_mode,
Elliot Lee's avatar
Elliot Lee committed
790 791
			 brush_hardness, mode)
     PaintCore *paint_core;
792
     GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
793 794 795 796 797 798 799 800 801 802 803 804
     int brush_opacity;
     int image_opacity;
     int paint_mode;
     int brush_hardness;
     int mode;
{
  MaskBuf *brush_mask;

  /*  get the brush mask  */
  brush_mask = paint_core_get_brush_mask (paint_core, brush_hardness);

  /*  paste the canvas buf  */
805
  paint_core_paste (paint_core, brush_mask, drawable,
Elliot Lee's avatar
Elliot Lee committed
806 807 808
		    brush_opacity, image_opacity, paint_mode, mode);
}

809 810 811 812
/* Similar to paint_core_paste_canvas, but replaces the alpha channel
   rather than using it to composite (i.e. transparent over opaque
   becomes transparent rather than opauqe. */
void
813
paint_core_replace_canvas (paint_core, drawable, brush_opacity, image_opacity,
814 815
			   brush_hardness, mode)
     PaintCore *paint_core;
816
     GimpDrawable *drawable;
817 818 819 820 821 822 823 824 825 826 827
     int brush_opacity;
     int image_opacity;
     int brush_hardness;
     int mode;
{
  MaskBuf *brush_mask;

  /*  get the brush mask  */
  brush_mask = paint_core_get_brush_mask (paint_core, brush_hardness);

  /*  paste the canvas buf  */
828
  paint_core_replace (paint_core, brush_mask, drawable,
829 830 831
		      brush_opacity, image_opacity, mode);
}

832
static MaskBuf *last_brush_mask = NULL;
833 834
static int cache_invalid = 0;

835
static int paint_core_invalidate_cache(GimpBrush *brush, gpointer *blah)
836
{
837 838
/* Make sure we don't cache data for a brush that has changed */
  if (last_brush_mask == brush->mask)
839 840 841 842 843 844 845
  {
    cache_invalid = 1;
    return 1;
  }
  return 0;
}

Elliot Lee's avatar
Elliot Lee committed
846 847 848 849 850 851 852 853 854 855 856 857
/************************************************************
 *             LOCAL FUNCTION DEFINITIONS                   *
 ************************************************************/

static MaskBuf *
paint_core_subsample_mask (mask, x, y)
     MaskBuf * mask;
     double x, y;
{
  MaskBuf * dest;
  double left;
  unsigned char * m, * d;
858
  const int * k;
Elliot Lee's avatar
Elliot Lee committed
859
  int index1, index2;
860
  const int * kernel;
Elliot Lee's avatar
Elliot Lee committed
861 862 863 864 865
  int new_val;
  int i, j;
  int r, s;

  x += (x < 0) ? mask->width : 0;
866
  left = x - floor(x) + 0.125;
Elliot Lee's avatar
Elliot Lee committed
867 868 869
  index1 = (int) (left * 4);

  y += (y < 0) ? mask->height : 0;
870
  left = y - floor(y) + 0.125;
Elliot Lee's avatar
Elliot Lee committed
871 872 873 874
  index2 = (int) (left * 4);

  kernel = subsample[index2][index1];

875 876
  if ((mask == last_brush_mask) && kernel_brushes[index2][index1] &&
      !cache_invalid)
Elliot Lee's avatar
Elliot Lee committed
877
    return kernel_brushes[index2][index1];
878
  else if (mask != last_brush_mask || cache_invalid)
879 880
    for (i = 0; i < 5; i++)
      for (j = 0; j < 5; j++)
Elliot Lee's avatar
Elliot Lee committed
881 882 883 884 885 886
	{
	  if (kernel_brushes[i][j])
	    mask_buf_free (kernel_brushes[i][j]);
	  kernel_brushes[i][j] = NULL;
	}

887
  last_brush_mask = mask;
888
  cache_invalid = 0;
Elliot Lee's avatar
Elliot Lee committed
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
  kernel_brushes[index2][index1] = mask_buf_new (mask->width + 2, mask->height + 2);
  dest = kernel_brushes[index2][index1];

  m = mask_buf_data (mask);
  for (i = 0; i < mask->height; i++)
    {
      for (j = 0; j < mask->width; j++)
	{
	  k = kernel;
	  for (r = 0; r < KERNEL_HEIGHT; r++)
	    {
	      d = mask_buf_data (dest) + (i+r) * dest->width + j;
	      s = KERNEL_WIDTH;
	      while (s--)
		{
		  new_val = *d + ((*m * *k++) >> 8);
905
		  *d++ = MINIMUM (new_val, 255);
Elliot Lee's avatar
Elliot Lee committed
906 907 908 909 910 911 912 913 914
		}
	    }
	  m++;
	}
    }

  return dest;
}

915 916
/* #define FANCY_PRESSURE */

917 918 919 920 921 922 923 924 925 926 927 928
static MaskBuf *
paint_core_pressurize_mask (brush_mask, x, y, pressure)
     MaskBuf * brush_mask;
     double x, y;
     double pressure;
{
  static MaskBuf *last_brush = NULL;
  static unsigned char mapi[256];
  unsigned char *source;
  unsigned char *dest;
  MaskBuf *subsample_mask;
  int i;
929 930
#ifdef FANCY_PRESSURE
  static double map[256];
931
  double ds,s,c;
932
#endif
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

  /* Get the raw subsampled mask */
  subsample_mask = paint_core_subsample_mask(brush_mask, x, y);

  /* Special case pressure = 0.5 */
  if ((int)(pressure*100+0.5) == 50)
    return subsample_mask;

  /* Make sure we have the right sized buffer */
  if (brush_mask != last_brush)
    {
      if (pressure_brush)
	mask_buf_free (pressure_brush);
      pressure_brush = mask_buf_new (brush_mask->width + 2,
				     brush_mask->height +2);
    }

950
#ifdef FANCY_PRESSURE
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
  /* Create the pressure profile

     It is: I'(I) = tanh(20*(pressure-0.5)*I) : pressure > 0.5
            I'(I) = 1 - tanh(20*(0.5-pressure)*(1-I)) : pressure < 0.5

     It looks like:

        low pressure      medium pressure     high pressure

             |                   /                 --
             |    		/                 /
            /     	       /                 |
          --                  /	                 |

  */

  ds = (pressure - 0.5)*(20./256.);
  s = 0;
  c = 1.0;

  if (ds > 0)
    {
      for (i=0;i<256;i++)
	{
	  map[i] = s/c;
	  s += c*ds;
	  c += s*ds;
	}
      for (i=0;i<256;i++)
	mapi[i] = (int)(255*map[i]/map[255]);
    }
  else
    {
      ds = -ds;
      for (i=255;i>=0;i--)
	{
	  map[i] = s/c;
	  s += c*ds;
	  c += s*ds;
	}
      for (i=0;i<256;i++)
	mapi[i] = (int)(255*(1-map[i]/map[0]));
    }
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
#else /* ! FANCY_PRESSURE */

  for (i=0;i<256;i++)
    {
      int tmp = (pressure/0.5)*i;
      if (tmp > 255)
	mapi[i] = 255;
      else
	mapi[i] = tmp;
    }

#endif /* FANCY_PRESSURE */
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

  /* Now convert the brush */

  source = mask_buf_data (subsample_mask);
  dest = mask_buf_data (pressure_brush);

  i = subsample_mask->width * subsample_mask->height;
  while (i--)
    {
      *dest++ = mapi[(*source++)];
    }

  return pressure_brush;
}

Elliot Lee's avatar
Elliot Lee committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
static MaskBuf *
paint_core_solidify_mask (brush_mask)
     MaskBuf * brush_mask;
{
  static MaskBuf *last_brush = NULL;
  int i, j;
  unsigned char * data, * src;

  if (brush_mask == last_brush)
    return solid_brush;

  last_brush = brush_mask;
  if (solid_brush)
    mask_buf_free (solid_brush);
  solid_brush = mask_buf_new (brush_mask->width + 2, brush_mask->height + 2);

  /*  get the data and advance one line into it  */
  data = mask_buf_data (solid_brush) + solid_brush->width;
  src = mask_buf_data (brush_mask);

  for (i = 0; i < brush_mask->height; i++)
    {
      data++;
      for (j = 0; j < brush_mask->width; j++)
	{
Manish Singh's avatar
Manish Singh committed
1046
	  *data++ = (*src++) ? OPAQUE_OPACITY : TRANSPARENT_OPACITY;
Elliot Lee's avatar
Elliot Lee committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	}
      data++;
    }

  return solid_brush;
}

static MaskBuf *
paint_core_get_brush_mask (paint_core, brush_hardness)
     PaintCore * paint_core;
     int brush_hardness;
{
  MaskBuf * bm;

  switch (brush_hardness)
    {
    case SOFT:
1064
      bm = paint_core_subsample_mask (paint_core->brush->mask, paint_core->curx, paint_core->cury);
Elliot Lee's avatar
Elliot Lee committed
1065 1066
      break;
    case HARD:
1067
      bm = paint_core_solidify_mask (paint_core->brush->mask);
Elliot Lee's avatar
Elliot Lee committed
1068
      break;
1069
    case PRESSURE:
1070
      bm = paint_core_pressurize_mask (paint_core->brush->mask, paint_core->curx, paint_core->cury, paint_core->curpressure);
1071
      break;
Elliot Lee's avatar
Elliot Lee committed
1072 1073 1074 1075 1076 1077 1078 1079 1080
    default:
      bm = NULL;
      break;
    }

  return bm;
}

static void
1081
paint_core_paste (paint_core, brush_mask, drawable, brush_opacity, image_opacity, paint_mode, mode)
Elliot Lee's avatar
Elliot Lee committed
1082 1083
     PaintCore *paint_core;
     MaskBuf *brush_mask;
1084
     GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
     int brush_opacity;
     int image_opacity;
     int paint_mode;
     int mode;
{
  GImage *gimage;
  PixelRegion srcPR;
  TileManager *alt = NULL;
  int offx, offy;

1095
  if (! (gimage = drawable_gimage (drawable)))
Elliot Lee's avatar
Elliot Lee committed
1096 1097 1098
    return;

  /*  set undo blocks  */
1099
  set_undo_tiles (drawable,
Elliot Lee's avatar
Elliot Lee committed
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 1126 1127 1128 1129
		  canvas_buf->x, canvas_buf->y,
		  canvas_buf->width, canvas_buf->height);

  /*  If the mode is CONSTANT:
   *   combine the canvas buf, the brush mask to the canvas tiles
   */
  if (mode == CONSTANT)
    {
      /*  initialize any invalid canvas tiles  */
      set_canvas_tiles (canvas_buf->x, canvas_buf->y,
			canvas_buf->width, canvas_buf->height);

      paint_to_canvas_tiles (paint_core, brush_mask, brush_opacity);
      alt = undo_tiles;
    }
  /*  Otherwise:
   *   combine the canvas buf and the brush mask to the canvas buf
   */
  else  /*  mode != CONSTANT  */
    paint_to_canvas_buf (paint_core, brush_mask, brush_opacity);

  /*  intialize canvas buf source pixel regions  */
  srcPR.bytes = canvas_buf->bytes;
  srcPR.x = 0; srcPR.y = 0;
  srcPR.w = canvas_buf->width;
  srcPR.h = canvas_buf->height;
  srcPR.rowstride = canvas_buf->width * canvas_buf->bytes;
  srcPR.data = temp_buf_data (canvas_buf);

  /*  apply the paint area to the gimage  */
1130
  gimage_apply_image (gimage, drawable, &srcPR,
Elliot Lee's avatar
Elliot Lee committed
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
		      FALSE, image_opacity, paint_mode,
		      alt,  /*  specify an alternative src1  */
		      canvas_buf->x, canvas_buf->y);

  /*  Update the undo extents  */
  paint_core->x1 = MINIMUM (paint_core->x1, canvas_buf->x);
  paint_core->y1 = MINIMUM (paint_core->y1, canvas_buf->y);
  paint_core->x2 = MAXIMUM (paint_core->x2, (canvas_buf->x + canvas_buf->width));
  paint_core->y2 = MAXIMUM (paint_core->y2, (canvas_buf->y + canvas_buf->height));

  /*  Update the gimage--it is important to call gdisplays_update_area
1142 1143 1144
   *  instead of drawable_update because we don't want the drawable
   *  preview to be constantly invalidated
   */
1145
  drawable_offsets (drawable, &offx, &offy);
1146
  gdisplays_update_area (gimage, canvas_buf->x + offx, canvas_buf->y + offy,
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
			 canvas_buf->width, canvas_buf->height);
}

/* This works similarly to paint_core_paste. However, instead of combining
   the canvas to the paint core drawable using one of the combination
   modes, it uses a "replace" mode (i.e. transparent pixels in the 
   canvas erase the paint core drawable).

   When not drawing on alpha-enabled images, it just paints using NORMAL
   mode.
*/
static void
1159
paint_core_replace (paint_core, brush_mask, drawable, brush_opacity, image_opacity, mode)
1160 1161
     PaintCore *paint_core;
     MaskBuf *brush_mask;
1162
     GimpDrawable *drawable;
1163 1164 1165 1166 1167 1168 1169 1170
     int brush_opacity;
     int image_opacity;
     int mode;
{
  GImage *gimage;
  PixelRegion srcPR, maskPR;
  int offx, offy;

1171
  if (!drawable_has_alpha (drawable))
1172
    {
1173
      paint_core_paste (paint_core, brush_mask, drawable,
1174 1175 1176 1177 1178 1179 1180
			brush_opacity, image_opacity, NORMAL_MODE,
			mode);
      return;
    }

  if (mode != INCREMENTAL)
    {
1181
      g_message (_("paint_core_replace only works in INCREMENTAL mode"));
1182 1183 1184
      return;
    }

1185
  if (! (gimage = drawable_gimage (drawable)))
1186 1187 1188
    return;

  /*  set undo blocks  */
1189
  set_undo_tiles (drawable,
1190 1191 1192 1193 1194
		  canvas_buf->x, canvas_buf->y,
		  canvas_buf->width, canvas_buf->height);

  maskPR.bytes = 1;
  maskPR.x = 0; maskPR.y = 0;
1195 1196
  maskPR.w = canvas_buf->width;
  maskPR.h = canvas_buf->height;
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  maskPR.rowstride = maskPR.bytes * brush_mask->width;
  maskPR.data = mask_buf_data (brush_mask);
  
  /*  intialize canvas buf source pixel regions  */
  srcPR.bytes = canvas_buf->bytes;
  srcPR.x = 0; srcPR.y = 0;
  srcPR.w = canvas_buf->width;
  srcPR.h = canvas_buf->height;
  srcPR.rowstride = canvas_buf->width * canvas_buf->bytes;
  srcPR.data = temp_buf_data (canvas_buf);

  /*  apply the paint area to the gimage  */
1209
  gimage_replace_image (gimage, drawable, &srcPR,
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
			FALSE, image_opacity,
			&maskPR,
			canvas_buf->x, canvas_buf->y);

  /*  Update the undo extents  */
  paint_core->x1 = MINIMUM (paint_core->x1, canvas_buf->x);
  paint_core->y1 = MINIMUM (paint_core->y1, canvas_buf->y);
  paint_core->x2 = MAXIMUM (paint_core->x2, (canvas_buf->x + canvas_buf->width));
  paint_core->y2 = MAXIMUM (paint_core->y2, (canvas_buf->y + canvas_buf->height));

  /*  Update the gimage--it is important to call gdisplays_update_area
Elliot Lee's avatar
Elliot Lee committed
1221 1222 1223
   *  instead of drawable_update because we don't want the drawable
   *  preview to be constantly invalidated
   */
1224
  drawable_offsets (drawable, &offx, &offy);
1225
  gdisplays_update_area (gimage, canvas_buf->x + offx, canvas_buf->y + offy,
Elliot Lee's avatar
Elliot Lee committed
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
			 canvas_buf->width, canvas_buf->height);
}

static void
paint_to_canvas_tiles (paint_core, brush_mask, brush_opacity)
     PaintCore *paint_core;
     MaskBuf *brush_mask;
     int brush_opacity;
{
  PixelRegion srcPR, maskPR;
  int x, y;
  int xoff, yoff;

  /*   combine the brush mask and the canvas tiles  */
  pixel_region_init (&srcPR, canvas_tiles,
		     canvas_buf->x, canvas_buf->y,
		     canvas_buf->width, canvas_buf->height, TRUE);

  x = (int) paint_core->curx - (brush_mask->width >> 1);
  y = (int) paint_core->cury - (brush_mask->height >> 1);
  xoff = (x < 0) ? -x : 0;
  yoff = (y < 0) ? -y : 0;

  maskPR.bytes = 1;
  maskPR.x = 0; maskPR.y = 0;
  maskPR.w = srcPR.w;
  maskPR.h = srcPR.h;
  maskPR.rowstride = maskPR.bytes * brush_mask->width;
  maskPR.data = mask_buf_data (brush_mask) + yoff * maskPR.rowstride + xoff * maskPR.bytes;

  /*  combine the mask and canvas tiles  */
  combine_mask_and_region (&srcPR, &maskPR, brush_opacity);

  /*  combine the canvas tiles and the canvas buf  */
  srcPR.bytes = canvas_buf->bytes;
  srcPR.x = 0; srcPR.y = 0;
  srcPR.w = canvas_buf->width;
  srcPR.h = canvas_buf->height;
  srcPR.rowstride = canvas_buf->width * canvas_buf->bytes;
  srcPR.data = temp_buf_data (canvas_buf);

  pixel_region_init (&maskPR, canvas_tiles,
		     canvas_buf->x, canvas_buf->y,
		     canvas_buf->width, canvas_buf->height, FALSE);

  /*  apply the canvas tiles to the canvas buf  */
Manish Singh's avatar
Manish Singh committed
1272
  apply_mask_to_region (&srcPR, &maskPR, OPAQUE_OPACITY);
Elliot Lee's avatar
Elliot Lee committed
1273 1274 1275 1276 1277 1278 1279 1280 1281
}

static void
paint_to_canvas_buf (paint_core, brush_mask, brush_opacity)
     PaintCore *paint_core;
     MaskBuf *brush_mask;
     int brush_opacity;
{
  PixelRegion srcPR, maskPR;
1282 1283 1284 1285 1286 1287 1288 1289
  int x, y;
  int xoff, yoff;

  x = (int) paint_core->curx - (brush_mask->width >> 1);
  y = (int) paint_core->cury - (brush_mask->height >> 1);
  xoff = (x < 0) ? -x : 0;
  yoff = (y < 0) ? -y : 0;

Elliot Lee's avatar
Elliot Lee committed
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303

  /*  combine the canvas buf and the brush mask to the canvas buf  */
  srcPR.bytes = canvas_buf->bytes;
  srcPR.x = 0; srcPR.y = 0;
  srcPR.w = canvas_buf->width;
  srcPR.h = canvas_buf->height;
  srcPR.rowstride = canvas_buf->width * canvas_buf->bytes;
  srcPR.data = temp_buf_data (canvas_buf);

  maskPR.bytes = 1;
  maskPR.x = 0; maskPR.y = 0;
  maskPR.w = srcPR.w;
  maskPR.h = srcPR.h;
  maskPR.rowstride = maskPR.bytes * brush_mask->width;
1304
  maskPR.data = mask_buf_data (brush_mask) + yoff * maskPR.rowstride + xoff * maskPR.bytes;
Elliot Lee's avatar
Elliot Lee committed
1305 1306 1307 1308 1309 1310

  /*  apply the mask  */
  apply_mask_to_region (&srcPR, &maskPR, brush_opacity);
}

static void
1311 1312
set_undo_tiles (drawable, x, y, w, h)
     GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
1313 1314 1315 1316 1317 1318 1319
     int x, y;
     int w, h;
{
  int i, j;
  Tile *src_tile;
  Tile *dest_tile;

1320 1321
  if (undo_tiles == NULL) 
    {
1322
      g_warning (_("set_undo_tiles: undo_tiles is null"));
1323 1324 1325
      return;
    }

Elliot Lee's avatar
Elliot Lee committed
1326 1327 1328 1329
  for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
    {
      for (j = x; j < (x + w); j += (TILE_WIDTH - (j % TILE_WIDTH)))
	{
1330
	  dest_tile = tile_manager_get_tile (undo_tiles, j, i, FALSE, FALSE);
scott's avatar
scott committed
1331
	  if (tile_is_valid (dest_tile) == FALSE)
Elliot Lee's avatar
Elliot Lee committed
1332
	    {
1333 1334
	      src_tile = tile_manager_get_tile (drawable_data (drawable), j, i, TRUE, FALSE);
	      tile_manager_map_tile (undo_tiles, j, i, src_tile);
1335
	      tile_release (src_tile, FALSE);
Elliot Lee's avatar
Elliot Lee committed
1336 1337 1338