gimpbrushpipe.c 17.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 * Copyright (C) 1999 Adrian Likins and Tor Lillqvist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

20 21 22
#include "config.h"

#include <stdio.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <ctype.h>
26

27
#include "apptypes.h"
28 29 30 31 32
#include "appenv.h"
#include "brush_header.h"
#include "pattern_header.h"
#include "patterns.h"
#include "gimpbrush.h"
33 34
#include "gimpbrushpipe.h"
#include "gimpbrushpipeP.h"
35 36
#include "paint_core.h"
#include "gimprc.h"
37

38
#include "libgimp/gimpintl.h"
39
#include "libgimp/gimpmath.h"
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/* Code duplicated from plug-ins/common/gpb.c...
 * The struct, and code to parse/build it probably should be in libgimp.
 */

/* Parameters related to one single gih file, collected in a struct
 * just for clarity.
 */
#define MAXDIM 4
static struct {
  gint step;
  gint ncells;
  gint dim;
  gint cols;
  gint rows;
  gchar *placement;
  gint rank[MAXDIM];
  gchar *selection[MAXDIM];
} gihparms;


61 62 63 64
static GimpBrushClass* gimp_brush_class;
static GtkObjectClass* gimp_object_class;

static GimpBrush *gimp_brush_pixmap_select_brush (PaintCore *paint_core);
65
static gboolean gimp_brush_pixmap_want_null_motion (PaintCore *paint_core);
66 67 68 69 70 71 72 73

static void paint_line_pixmap_mask(GImage	   *dest,
				   GimpDrawable    *drawable,
				   GimpBrushPixmap *brush,
				   guchar	   *d,
				   int		    x,
				   int              y,
				   int              bytes,
74 75
				   int              width,
				   int              mode);
76

77
static void
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
gimp_brush_pixmap_destroy (GtkObject *object)
{
  GimpBrushPixmap *pixmap;

  g_return_if_fail (GIMP_IS_BRUSH_PIXMAP (object));

  pixmap = GIMP_BRUSH_PIXMAP (object);
  
  temp_buf_free (pixmap->pixmap_mask);

  (* GTK_OBJECT_CLASS (gimp_object_class)->destroy) (object);
}

static void
gimp_brush_pixmap_class_init (GimpBrushPixmapClass *klass)
{
  GtkObjectClass *object_class;
  GimpBrushClass *brush_class;
  
  object_class = GTK_OBJECT_CLASS (klass);
  brush_class = GIMP_BRUSH_CLASS (klass);

  gimp_brush_class = gtk_type_class (gimp_brush_get_type ());

  object_class->destroy =  gimp_brush_pixmap_destroy;
  brush_class->select_brush = gimp_brush_pixmap_select_brush;
104
  brush_class->want_null_motion = gimp_brush_pixmap_want_null_motion;
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
}

void
gimp_brush_pixmap_init (GimpBrushPixmap *brush)
{
  brush->pixmap_mask = NULL;
  brush->pipe = NULL;
}

GtkType
gimp_brush_pixmap_get_type (void)
{
  static GtkType type=0;
  if(!type){
    GtkTypeInfo info={
      "GimpBrushPixmap",
      sizeof (GimpBrushPixmap),
      sizeof (GimpBrushPixmapClass),
      (GtkClassInitFunc) gimp_brush_pixmap_class_init,
      (GtkObjectInitFunc) gimp_brush_pixmap_init,
     /* reserved_1 */ NULL,
     /* reserved_2 */ NULL,
    (GtkClassInitFunc) NULL};
    type = gtk_type_unique (GIMP_TYPE_BRUSH, &info);
  }
  return type;
}
132

133 134 135
static GimpBrush *
gimp_brush_pixmap_select_brush (PaintCore *paint_core)
{
136
  GimpBrushPipe *pipe;
137
  int i, brushix, ix;
138
  double angle;
139 140 141

  g_return_val_if_fail (GIMP_IS_BRUSH_PIXMAP (paint_core->brush), NULL);

142
  pipe = GIMP_BRUSH_PIXMAP (paint_core->brush)->pipe;
143

144 145
  if (pipe->nbrushes == 1)
    return GIMP_BRUSH (pipe->current);
146

147 148 149 150 151 152
  brushix = 0;
  for (i = 0; i < pipe->dimension; i++)
    {
      switch (pipe->select[i])
	{
	case PIPE_SELECT_CONSTANT:
153
	  ix = pipe->index[i];
154 155
	  break;
	case PIPE_SELECT_INCREMENTAL:
156
	  ix = (pipe->index[i] + 1) % pipe->rank[i];
157 158 159 160
	  break;
	case PIPE_SELECT_ANGULAR:
	  angle = atan2 (paint_core->cury - paint_core->lasty,
			 paint_core->curx - paint_core->lastx);
161 162 163
	  /* Offset angle to be compatible with PSP tubes */
	  angle += G_PI_2;
	  /* Map it to the [0..2*G_PI) interval */
164 165
	  if (angle < 0)
	    angle += 2.*G_PI;
166 167 168
	  else if (angle > 2.*G_PI)
	    angle -= 2.*G_PI;
	  ix = RINT (angle / (2.*G_PI) * pipe->rank[i]);
169 170 171
	  break;
	case PIPE_SELECT_RANDOM:
	  /* This probably isn't the right way */
172
	  ix = rand () % pipe->rank[i];
173 174
	  break;
	case PIPE_SELECT_PRESSURE:
175
	  ix = RINT (paint_core->curpressure * (pipe->rank[i] - 1));
176 177
	  break;
	case PIPE_SELECT_TILT_X:
178
	  ix = RINT (paint_core->curxtilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
179 180
	  break;
	case PIPE_SELECT_TILT_Y:
181
	  ix = RINT (paint_core->curytilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
182 183
	  break;
	}
184 185 186
      pipe->index[i] = BOUNDS (ix, 0, pipe->rank[i]-1);
      brushix += pipe->stride[i] * pipe->index[i];
      /* g_print ("ix at %d: %d, brushix: %d\n", i, ix, brushix); */
187 188
    }

189
  /* Make sure is inside bounds */
190
  brushix = BOUNDS (brushix, 0, pipe->nbrushes-1);
191

192 193 194
  pipe->current = pipe->brushes[brushix];

  return GIMP_BRUSH (pipe->current);
195
}
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
static gboolean
gimp_brush_pixmap_want_null_motion (PaintCore *paint_core)
{
  GimpBrushPipe *pipe;
  int i;

  g_return_val_if_fail (GIMP_IS_BRUSH_PIXMAP (paint_core->brush), TRUE);

  pipe = GIMP_BRUSH_PIXMAP (paint_core->brush)->pipe;

  if (pipe->nbrushes == 1)
    return TRUE;

  for (i = 0; i < pipe->dimension; i++)
    if (pipe->select[i] == PIPE_SELECT_ANGULAR)
      return FALSE;

  return TRUE;
}

217 218 219
static void
gimp_brush_pipe_destroy(GtkObject *object)
{
220 221 222 223 224 225 226 227 228
  GimpBrushPipe *pipe;
  int i;

  g_return_if_fail (object != NULL);
  g_return_if_fail (GIMP_IS_BRUSH_PIPE (object));

  pipe = GIMP_BRUSH_PIPE (object);

  g_free (pipe->rank);
229
  g_free (pipe->stride);
230 231 232 233 234 235 236 237 238 239

  for (i = 1; i < pipe->nbrushes; i++)
    gimp_object_destroy (pipe->brushes[i]);

  g_free (pipe->brushes);
  g_free (pipe->select);
  g_free (pipe->index);

  if (GTK_OBJECT_CLASS (gimp_object_class)->destroy)
    (* GTK_OBJECT_CLASS (gimp_object_class)->destroy) (object);
240 241 242 243 244 245 246
}

static void
gimp_brush_pipe_class_init (GimpBrushPipeClass *klass)
{
  GtkObjectClass *object_class;
  
247 248 249 250
  object_class = GTK_OBJECT_CLASS (klass);

  gimp_object_class = gtk_type_class (GIMP_TYPE_OBJECT);
  object_class->destroy =  gimp_brush_pipe_destroy;
251 252 253
}

void
254
gimp_brush_pipe_init (GimpBrushPipe *pipe)
255
{
256 257 258 259 260
  pipe->dimension = 0;
  pipe->rank = NULL;
  pipe->nbrushes = 0;
  pipe->select = NULL;
  pipe->index = NULL;
261 262
}

263 264
GtkType
gimp_brush_pipe_get_type (void)
265 266
{
  static GtkType type=0;
267
  if (!type){
268 269
    GtkTypeInfo info={
      "GimpBrushPipe",
270 271 272 273 274 275
      sizeof (GimpBrushPipe),
      sizeof (GimpBrushPipeClass),
      (GtkClassInitFunc) gimp_brush_pipe_class_init,
      (GtkObjectInitFunc) gimp_brush_pipe_init,
     /* reserved_1 */ NULL,
     /* reserved_2 */ NULL,
276
    (GtkClassInitFunc) NULL};
277
    type = gtk_type_unique (GIMP_TYPE_BRUSH_PIXMAP, &info);
278 279 280 281
  }
  return type;
}

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
static void
init_pipe_parameters ()
{
  int i;

  gihparms.step = 100;
  gihparms.ncells = 1;
  gihparms.dim = 1;
  gihparms.cols = 1;
  gihparms.rows = 1;
  gihparms.placement = "constant";
  for (i = 0; i < MAXDIM; i++)
    gihparms.selection[i] = "random";
  gihparms.rank[0] = 1;
  for (i = 1; i < MAXDIM; i++)
    gihparms.rank[i] = 0;
}

static void
parse_brush_pipe_parameters (gchar *parameters)
{
  guchar *p, *q, *r, *s;	/* Don't you love single-char identifiers?  */
  gint i;

  q = parameters;
  while ((p = strtok (q, " \r\n")) != NULL)
    {
      q = NULL;
      r = strchr (p, ':');
      if (r)
	*r = 0;

      if (strcmp (p, "ncells") == 0)
	{
	  if (r)
	    gihparms.ncells = atoi (r + 1);
	}
      else if (strcmp (p, "step") == 0)
	{
	  if (r)
	    gihparms.step = atoi (r + 1);
	}
      else if (strcmp (p, "dim") == 0)
	{
	  if (r)
	    gihparms.dim = atoi (r + 1);
	}
      else if (strcmp (p, "cols") == 0)
	{
	  if (r)
	    gihparms.cols = atoi (r + 1);
	}
      else if (strcmp (p, "rows") == 0)
	{
	  if (r)
	    gihparms.rows = atoi (r + 1);
	}
      else if (strcmp (p, "placement") == 0)
	{
	  if (r)
	    gihparms.placement = g_strdup (r + 1);
	}
      else if (strncmp (p, "rank", strlen ("rank")) == 0)
	{
	  if (r)
	    {
	      i = atoi (p + strlen ("rank"));
	      if (i >= 0 && i < gihparms.dim)
		gihparms.rank[i] = atoi (r + 1);
	    }
	}
      else if (strncmp (p, "sel", strlen ("sel")) == 0)
	{
	  if (r)
	    {
	      i = atoi (p + strlen ("sel"));
	      if (i >= 0 && i < gihparms.dim)
		gihparms.selection[i] = g_strdup (r + 1);
	    }
	}
      if (r)
	*r = ':';
    }
}

367
GimpBrushPipe *
368
gimp_brush_pipe_load (char *filename)
369 370 371 372
{
  GimpBrushPipe *pipe;
  GPatternP pattern;
  FILE *fp;
373 374
  guchar buf[1024];
  guchar *name;
375
  int i;
376
  int num_of_brushes;
377 378
  int totalcells;
  gchar *params;
379

380 381
  if ((fp = fopen (filename, "rb")) == NULL)
    return NULL;
382

383 384 385 386 387 388 389 390 391
  /* The file format starts with a painfully simple text header
   * and we use a painfully simple way to read it
   */
  if (fgets (buf, 1024, fp) == NULL)
    {
      fclose (fp);
      return NULL;
    }
  buf[strlen (buf) - 1] = 0;
392

393 394
  pipe = GIMP_BRUSH_PIPE (gimp_type_new (GIMP_TYPE_BRUSH_PIPE));
  name = g_strdup (buf);
395 396

  /* get the number of brushes */
397 398 399 400 401 402 403 404 405 406 407 408 409 410
  if (fgets (buf, 1024, fp) == NULL)
    {
      fclose (fp);
      gimp_object_destroy (pipe);
      return NULL;
    }
  num_of_brushes = strtol(buf, &params, 10);
  if (num_of_brushes < 1)
    {
      g_message (_("pixmap brush pipe should have at least one brush"));
      fclose (fp);
      gimp_object_destroy (pipe);
      return NULL;
    }
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
  while (*params && isspace(*params))
    params++;

  if (*params)
    {
      init_pipe_parameters ();
      parse_brush_pipe_parameters (params);
      pipe->dimension = gihparms.dim;
      pipe->rank = g_new (int, pipe->dimension);
      pipe->select = g_new (PipeSelectModes, pipe->dimension);
      pipe->index = g_new (int, pipe->dimension);
      for (i = 0; i < pipe->dimension; i++)
	{
	  pipe->rank[i] = gihparms.rank[i];
	  if (strcmp (gihparms.selection[i], "incremental") == 0)
	    pipe->select[i] = PIPE_SELECT_INCREMENTAL;
	  else if (strcmp (gihparms.selection[i], "angular") == 0)
	    pipe->select[i] = PIPE_SELECT_ANGULAR;
	  else if (strcmp (gihparms.selection[i], "velocity") == 0)
	    pipe->select[i] = PIPE_SELECT_VELOCITY;
	  else if (strcmp (gihparms.selection[i], "random") == 0)
	    pipe->select[i] = PIPE_SELECT_RANDOM;
	  else if (strcmp (gihparms.selection[i], "pressure") == 0)
	    pipe->select[i] = PIPE_SELECT_PRESSURE;
	  else if (strcmp (gihparms.selection[i], "xtilt") == 0)
	    pipe->select[i] = PIPE_SELECT_TILT_X;
	  else if (strcmp (gihparms.selection[i], "ytilt") == 0)
	    pipe->select[i] = PIPE_SELECT_TILT_Y;
	  else
	    pipe->select[i] = PIPE_SELECT_CONSTANT;
	  pipe->index[i] = 0;
	}
    }
  else
    {
      pipe->dimension = 1;
      pipe->rank = g_new (int, 1);
      pipe->rank[0] = num_of_brushes;
      pipe->select = g_new (PipeSelectModes, 1);
      pipe->select[0] = PIPE_SELECT_INCREMENTAL;
      pipe->index = g_new (int, 1);
      pipe->index[0] = 0;
    }

  totalcells = 1;		/* Not all necessarily present, maybe */
  for (i = 0; i < pipe->dimension; i++)
    totalcells *= pipe->rank[i];
  pipe->stride = g_new (int, pipe->dimension);
  for (i = 0; i < pipe->dimension; i++)
    {
      if (i == 0)
	pipe->stride[i] = totalcells / pipe->rank[i];
      else
	pipe->stride[i] = pipe->stride[i-1] / pipe->rank[i];
    }
  g_assert (pipe->stride[pipe->dimension-1] == 1);
468

469 470 471 472
  pattern = (GPatternP) g_malloc (sizeof (GPattern));
  pattern->filename = NULL;
  pattern->name = NULL;
  pattern->mask = NULL;
473

474 475
  pipe->brushes =
    (GimpBrushPixmap **) g_new0 (GimpBrushPixmap *, num_of_brushes);
476

477 478
  /* First pixmap brush in the list is the pipe itself */
  pipe->brushes[0] = GIMP_BRUSH_PIXMAP (pipe);
479

480 481 482 483 484 485
  /* Current pixmap brush is the first one. */
  pipe->current = pipe->brushes[0];

  while (pipe->nbrushes < num_of_brushes)
    {
      if (pipe->nbrushes > 0)
486
	{
487 488 489
	  pipe->brushes[pipe->nbrushes] =
	    GIMP_BRUSH_PIXMAP (gimp_type_new (GIMP_TYPE_BRUSH_PIXMAP));
	  GIMP_BRUSH (pipe->brushes[pipe->nbrushes])->name = NULL;
490
	}
491 492
      
      pipe->brushes[pipe->nbrushes]->pipe = pipe;
493

494 495 496 497
      /* load the brush */
      if (!gimp_brush_load_brush (GIMP_BRUSH (pipe->brushes[pipe->nbrushes]),
				  fp, filename)
	  || !load_pattern_pattern (pattern, fp, filename))
498
       {
499 500 501 502
	  g_message (_("failed to load one of the pixmap brushes in the pipe"));
	  fclose (fp);
	  g_free (pattern);
	  gimp_object_destroy (pipe);
503 504
	  return NULL;
       }
505 506 507 508 509 510 511 512 513

      if (pipe->nbrushes == 0)
	{
	  /* Replace name with the whole pipe's name */
	  GIMP_BRUSH (pipe)->name = name;
	}
      pipe->brushes[pipe->nbrushes]->pixmap_mask = pattern->mask;

      pipe->nbrushes++;
514
    }
515 516

  /*  Clean up  */
517 518
  fclose (fp);

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
  g_free(pattern);
  return pipe;
}

GimpBrushPipe *
gimp_brush_pixmap_load (char *filename)
{
  GimpBrushPipe *pipe;
  GPatternP pattern;
  FILE *fp;

  if ((fp = fopen (filename, "rb")) == NULL)
    return NULL;

  pipe = GIMP_BRUSH_PIPE (gimp_type_new (GIMP_TYPE_BRUSH_PIPE));
534

535 536 537 538 539 540 541 542
  /* A (single) pixmap brush is a pixmap pipe brush with just one pixmap */
  pipe->dimension = 1;
  pipe->rank = g_new (int, 1);
  pipe->rank[0] = 1;
  pipe->select = g_new (PipeSelectModes, 1);
  pipe->select[0] = PIPE_SELECT_INCREMENTAL;
  pipe->index = g_new (int, 1);
  pipe->index[0] = 0;
543

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
  pattern = (GPatternP) g_malloc (sizeof (GPattern));
  pattern->filename = NULL;
  pattern->name = NULL;
  pattern->mask = NULL;

  pipe->brushes = (GimpBrushPixmap **) g_new (GimpBrushPixmap *, 1);

  pipe->brushes[0] = GIMP_BRUSH_PIXMAP (pipe);
  pipe->current = pipe->brushes[0];

  pipe->brushes[0]->pipe = pipe;

  /* load the brush */
  if (!gimp_brush_load_brush (GIMP_BRUSH (pipe->brushes[0]),
			      fp, filename)
      || !load_pattern_pattern (pattern, fp, filename))
    {
      g_message (_("failed to load pixmap brush"));
      fclose (fp);
      g_free (pattern);
      gimp_object_destroy (pipe);
      return NULL;
    }
  
  pipe->brushes[0]->pixmap_mask = pattern->mask;

  pipe->nbrushes = 1;
  /*  Clean up  */
  fclose (fp);

  g_free(pattern);
575
  return pipe;
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
}

TempBuf *
gimp_brush_pixmap_pixmap (GimpBrushPixmap *brush)
{
  g_return_val_if_fail (brush != NULL, NULL);
  g_return_val_if_fail (GIMP_IS_BRUSH_PIXMAP (brush), NULL);

  return brush->pixmap_mask;
}

void
color_area_with_pixmap (PaintCore *paint_core,
			GImage *dest,
			GimpDrawable *drawable,
591 592
			TempBuf *area,
			int mode)
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
{

  PixelRegion destPR;
  void *pr;
  guchar *d;
  int ulx, uly, offsetx, offsety, y;
  GimpBrushPixmap *pixmap;
  
  g_return_if_fail (GIMP_IS_BRUSH_PIXMAP (paint_core->brush));

  pixmap = GIMP_BRUSH_PIXMAP (paint_core->brush);

  destPR.bytes = area->bytes;
  destPR.x = 0; destPR.y = 0;
  destPR.w = area->width;
  destPR.h = area->height;
  destPR.rowstride = destPR.bytes * area->width;
  destPR.data = temp_buf_data (area);
		
  pr = pixel_regions_register (1, &destPR);
613

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
  /* Calculate upper left corner of brush as in
   * paint_core_get_paint_area.  Ugly to have to do this here, too.
   */

  ulx = (int) paint_core->curx - (paint_core->brush->mask->width >> 1);
  uly = (int) paint_core->cury - (paint_core->brush->mask->height >> 1);

  offsetx = area->x - ulx;
  offsety = area->y - uly;

  for (; pr != NULL; pr = pixel_regions_process (pr))
    {
      d = destPR.data;
      for (y = 0; y < destPR.h; y++)
	{
	  paint_line_pixmap_mask (dest, drawable, pixmap,
				  d, offsetx, y + offsety,
631
				  destPR.bytes, destPR.w, mode);
632 633 634 635 636 637 638 639 640 641 642 643 644
	  d += destPR.rowstride;
	}
    }
}

static void
paint_line_pixmap_mask (GImage          *dest,
			GimpDrawable    *drawable,
			GimpBrushPixmap *brush,
			guchar	        *d,
			int		 x,
			int              y,
			int              bytes,
645 646
			int              width,
			int              mode)
647 648
{
  guchar *b, *p;
649 650 651 652 653 654
  int x_index;
  gdouble alpha;
  gdouble factor = 0.00392156986;  /* 1.0/255.0 */
  gint i;
  gint j;
  guchar *mask;
655 656 657 658 659 660 661 662 663 664 665

  /*  Make sure x, y are positive  */
  while (x < 0)
    x += brush->pixmap_mask->width;
  while (y < 0)
    y += brush->pixmap_mask->height;

  /* Point to the approriate scanline */
  b = temp_buf_data (brush->pixmap_mask) +
    (y % brush->pixmap_mask->height) * brush->pixmap_mask->width * brush->pixmap_mask->bytes;
    
666 667 668
  /* ditto, except for the brush mask, so we can pre-multiply the alpha value */
  mask = temp_buf_data((brush->gbrush).mask) +
    (y % brush->pixmap_mask->height) * brush->pixmap_mask->width;
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

  if(mode == SOFT)
    {
      for (i = 0; i < width; i++)
	{
	  /* attempt to avoid doing this calc twice in the loop */
	  x_index = ((i + x) % brush->pixmap_mask->width);
	  p = b + x_index * brush->pixmap_mask->bytes;
	  d[bytes-1] = mask[x_index];

	  /* multiply alpha into the pixmap data */
	  /* maybe we could do this at tool creation or brush switch time? */
	  /* and compute it for the whole brush at once and cache it?  */
	  if(alpha = d[bytes-1] * factor)
	    {
	      d[0] *= alpha;
	      d[1] *= alpha;
	      d[2] *= alpha;
	    }
	  /* printf("i: %i d->r: %i d->g: %i d->b: %i d->a: %i\n",i,(int)d[0], (int)d[1], (int)d[2], (int)d[3]); */
	  gimage_transform_color (dest, drawable, p, d, RGB);
	  d += bytes;
	}
    }
  else
694
    {
695 696 697 698 699 700 701 702 703 704 705 706 707
      for (i = 0; i < width; i++)
	{
	  /* attempt to avoid doing this calc twice in the loop */
	  x_index = ((i + x) % brush->pixmap_mask->width);
	  p = b + x_index * brush->pixmap_mask->bytes;
	  d[bytes-1] = 255;

	  /* multiply alpha into the pixmap data */
	  /* maybe we could do this at tool creation or brush switch time? */
	  /* and compute it for the whole brush at once and cache it?  */
	  gimage_transform_color (dest, drawable, p, d, RGB);
	  d += bytes;
	}
708
    }
709
}
710