convmatrix.c 25.1 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
/* Convolution Matrix plug-in for the GIMP -- Version 0.1
 * Copyright (C) 1997 Lauri Alanko <la@iki.fi>
 *
 *
 * 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
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18 19 20 21 22 23
 *
 * The GNU General Public License is also available from
 * http://www.fsf.org/copyleft/gpl.html
 * 
 *
 * CHANGELOG:
24 25 26
 * v0.13	15.12.2000
 * 	Made the PDB interface actually work.   (Simon Budig <simon@gimp.org>)
 *
Elliot Lee's avatar
Elliot Lee committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
 * v0.12	15.9.1997
 *	Got rid of the unportable snprintf. Also made some _tiny_ GUI fixes.
 *
 * v0.11	20.7.1997
 *	Negative values in the matrix are now abs'ed when used to weight
 *      alpha. Embossing effects should work properly now. Also fixed a
 *      totally idiotic bug with embossing.
 *
 * v0.1 	2.7.1997
 *	Initial release. Works... kinda.
 * 
 * 
 * TODO:
 * 
 * - remove channels selector (that's what the channels dialog is for)
 * - remove idiotic slowdowns
 * - clean up code
 * - preview
 * - optimize properly
 * - save & load matrices
 * - spiffy frontend for designing matrices
 * 
 * What else?
 * 
 * 
 */

Tor Lillqvist's avatar
Tor Lillqvist committed
54 55
#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
56 57 58 59 60
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
61
#ifdef HAVE_UNISTD_H
Elliot Lee's avatar
Elliot Lee committed
62
#include <unistd.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
63
#endif
Elliot Lee's avatar
Elliot Lee committed
64

65 66
#include <gtk/gtk.h>

67 68
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
69

70
#include "libgimp/stdplugins-intl.h"
Elliot Lee's avatar
Elliot Lee committed
71

72

73 74 75 76 77 78 79
typedef enum
{
  EXTEND,
  WRAP,
  CLEAR,
  MIRROR
} BorderMode;
Elliot Lee's avatar
Elliot Lee committed
80

81
static GimpDrawable *drawable;
Elliot Lee's avatar
Elliot Lee committed
82

83
static gchar * const channel_labels[] =
84
{
85 86 87 88 89
  N_("Gr_ey"),
  N_("Re_d"),
  N_("_Green"),
  N_("_Blue"),
  N_("_Alpha")
Elliot Lee's avatar
Elliot Lee committed
90 91
};

92
static gchar * const bmode_labels[] =
93
{
94 95 96
  N_("E_xtend"),
  N_("_Wrap"),
  N_("Cro_p")
Elliot Lee's avatar
Elliot Lee committed
97 98 99
};

/* Declare local functions. */
100
static void query (void);
101 102
static void run   (gchar      *name,
		   gint        nparams,
Sven Neumann's avatar
Sven Neumann committed
103
		   GimpParam  *param,
104
		   gint       *nreturn_vals,
Sven Neumann's avatar
Sven Neumann committed
105
		   GimpParam **return_vals);
Elliot Lee's avatar
Elliot Lee committed
106

107 108 109 110
static gint dialog (void);

static void doit         (void);
static void check_config (void);
Elliot Lee's avatar
Elliot Lee committed
111

Sven Neumann's avatar
Sven Neumann committed
112
GimpPlugInInfo PLUG_IN_INFO =
Elliot Lee's avatar
Elliot Lee committed
113
{
114 115 116 117
  NULL,   /* init_proc  */
  NULL,   /* quit_proc  */
  query,  /* query_proc */
  run,    /* run_proc   */
Elliot Lee's avatar
Elliot Lee committed
118 119
};

120 121
static gint bytes;
static gint run_flag = 0;
Elliot Lee's avatar
Elliot Lee committed
122

123 124
typedef struct
{
125 126 127 128
  gfloat     matrix[5][5];
  gfloat     divisor;
  gfloat     offset;
  gint       alpha_alg;
129
  BorderMode bmode;
130 131
  gint       channels[5];
  gint       autoset;
Elliot Lee's avatar
Elliot Lee committed
132 133
} config;

134
static const config default_config =
Elliot Lee's avatar
Elliot Lee committed
135
{
136 137 138 139 140 141 142 143 144 145 146 147 148
  {
    { 0.0, 0.0, 0.0, 0.0, 0.0 },
    { 0.0, 0.0, 0.0, 0.0, 0.0 },
    { 0.0, 0.0, 1.0, 0.0, 0.0 },
    { 0.0, 0.0, 0.0, 0.0, 0.0 },
    { 0.0, 0.0, 0.0, 0.0, 0.0 }
  },                 /* matrix */
  1,                 /* divisor */
  0,                 /* offset */
  1,                 /* Alpha-handling algorithm */     
  CLEAR,             /* border-mode */
  { 1, 1, 1, 1, 1 }, /* Channels mask */
  0                  /* autoset */
Elliot Lee's avatar
Elliot Lee committed
149 150
};

151
static config my_config;
Elliot Lee's avatar
Elliot Lee committed
152

153 154 155 156 157 158 159 160 161 162 163
struct
{
  GtkWidget *matrix[5][5];
  GtkWidget *divisor;
  GtkWidget *offset;
  GtkWidget *alpha_alg;
  GtkWidget *bmode[3];
  GtkWidget *channels[5];
  GtkWidget *autoset;
  GtkWidget *ok;
} my_widgets;
Elliot Lee's avatar
Elliot Lee committed
164 165


166
MAIN ()
Elliot Lee's avatar
Elliot Lee committed
167

168 169
static void
query (void)
Elliot Lee's avatar
Elliot Lee committed
170
{
Sven Neumann's avatar
Sven Neumann committed
171
  static GimpParamDef args[] =
172
  {
173 174
    { GIMP_PDB_INT32,    "run_mode", "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE,    "image",    "Input image (unused)" },
175 176 177 178 179 180 181 182 183 184
    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
    { GIMP_PDB_INT32, "argc_matrix", "The number of elements in the following array. Should be always 25." },
    { GIMP_PDB_FLOATARRAY, "matrix", "The 5x5 convolution matrix" },
    { GIMP_PDB_INT32, "alpha_alg", "Enable weighting by alpha channel" },
    { GIMP_PDB_FLOAT, "divisor", "Divisor" },
    { GIMP_PDB_FLOAT, "offset", "Offset" },

    { GIMP_PDB_INT32, "argc_channels", "The number of elements in following array. Should be always 5." },
    { GIMP_PDB_INT32ARRAY, "channels", "Mask of the channels to be filtered" },
    { GIMP_PDB_INT32, "bmode", "Mode for treating image borders" }
185 186 187
  };

  gimp_install_procedure ("plug_in_convmatrix",
Marc Lehmann's avatar
Marc Lehmann committed
188
			  "A generic 5x5 convolution matrix",
189 190 191 192 193 194
			  "",
			  "Lauri Alanko",
			  "Lauri Alanko",
			  "1997",
			  N_("<Image>/Filters/Generic/Convolution Matrix..."),
			  "RGB*, GRAY*",
Sven Neumann's avatar
Sven Neumann committed
195
			  GIMP_PLUGIN,
196
			  G_N_ELEMENTS (args), 0,
197
			  args, NULL);
Elliot Lee's avatar
Elliot Lee committed
198 199
}

200
static void
201 202
run (gchar      *name,
     gint        nparams,
Sven Neumann's avatar
Sven Neumann committed
203
     GimpParam  *param,
204
     gint       *nreturn_vals,
Sven Neumann's avatar
Sven Neumann committed
205
     GimpParam **return_vals)
206
{
207
  static GimpParam  values[1];
208
  GimpRunMode   run_mode;
Sven Neumann's avatar
Sven Neumann committed
209
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
210
  gint              x, y;
Elliot Lee's avatar
Elliot Lee committed
211

212
  INIT_I18N_UI();
213

214 215
  (void) name; /* Shut up warnings about unused parameters. */

216 217
  *nreturn_vals = 1;
  *return_vals = values;
Elliot Lee's avatar
Elliot Lee committed
218

219
  run_mode = param[0].data.d_int32;
Elliot Lee's avatar
Elliot Lee committed
220
	
221 222 223
  /*  Get the specified drawable  */
  drawable = gimp_drawable_get (param[2].data.d_drawable);

224 225 226 227 228 229 230 231 232 233
  /*  The plug-in is not able to handle images smaller than 3 pixels  */
  if (drawable->width < 3 || drawable->height < 3)
    {
      g_message (_("Convolution Matrix does not work\non layers smaller than 3 pixels."));
      status = GIMP_PDB_EXECUTION_ERROR;
      values[0].type = GIMP_PDB_STATUS;
      values[0].data.d_status = status;
      return;
    }

234
  my_config = default_config;
Sven Neumann's avatar
Sven Neumann committed
235
  if (run_mode == GIMP_RUN_NONINTERACTIVE)
236
    {
237
      if (nparams != 11)
Sven Neumann's avatar
Sven Neumann committed
238
	status = GIMP_PDB_CALLING_ERROR;
239 240
      else
	{
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	  if (param[3].data.d_int32 != 25) {
	    status = GIMP_PDB_CALLING_ERROR;
	  } else {
	    for (y = 0; y < 5; y++)
	      for (x = 0; x < 5; x++)
		my_config.matrix[x][y]=param[4].data.d_floatarray[y*5+x];
	  }

	  my_config.alpha_alg = param[5].data.d_int32;
	  my_config.divisor   = param[6].data.d_float;
	  my_config.offset    = param[7].data.d_float;


	  if (param[8].data.d_int32 != 5) {
	    status = GIMP_PDB_CALLING_ERROR;
	  } else {
	    for (y = 0; y < 5; y++)
	      my_config.channels[y] = param[9].data.d_int32array[y];
	  }

	  my_config.bmode     = param[10].data.d_int32;
262

263
	  check_config ();
Elliot Lee's avatar
Elliot Lee committed
264
	}
265 266 267 268
    }
  else
    {
      gimp_get_data ("plug_in_convmatrix", &my_config);
Elliot Lee's avatar
Elliot Lee committed
269

Sven Neumann's avatar
Sven Neumann committed
270
      if (run_mode == GIMP_RUN_INTERACTIVE)
271 272 273 274 275 276 277 278
	{
	  /*  Oh boy. We get to do a dialog box, because we can't really
	   *  expect the user to set us up with the right values using gdb.
	   */
	  check_config ();
	  if (!dialog())
	    {
	      /* The dialog was closed, or something similarly evil happened. */
Sven Neumann's avatar
Sven Neumann committed
279
	      status = GIMP_PDB_EXECUTION_ERROR;
280 281 282
	    }
	}
    }
Elliot Lee's avatar
Elliot Lee committed
283

Sven Neumann's avatar
Sven Neumann committed
284
  if (status == GIMP_PDB_SUCCESS)
285 286
    {
      /*  Make sure that the drawable is gray or RGB color  */
287 288
      if (gimp_drawable_is_rgb(drawable->drawable_id) ||
	  gimp_drawable_is_gray(drawable->drawable_id))
289 290 291 292
	{
	  gimp_progress_init (_("Applying convolution"));
	  gimp_tile_cache_ntiles (2 * (drawable->width /
				       gimp_tile_width () + 1));
Elliot Lee's avatar
Elliot Lee committed
293

294
	  doit ();
Elliot Lee's avatar
Elliot Lee committed
295

Sven Neumann's avatar
Sven Neumann committed
296
	  if (run_mode != GIMP_RUN_NONINTERACTIVE)
297
	    gimp_displays_flush ();
Elliot Lee's avatar
Elliot Lee committed
298

Sven Neumann's avatar
Sven Neumann committed
299
	  if (run_mode == GIMP_RUN_INTERACTIVE)
300
	    gimp_set_data ("plug_in_convmatrix", &my_config, sizeof (my_config));
Elliot Lee's avatar
Elliot Lee committed
301
	}
302 303
      else
	{
Sven Neumann's avatar
Sven Neumann committed
304
	  status = GIMP_PDB_EXECUTION_ERROR;
305 306 307
	}
      gimp_drawable_detach (drawable);
    }
Elliot Lee's avatar
Elliot Lee committed
308

Sven Neumann's avatar
Sven Neumann committed
309
  values[0].type = GIMP_PDB_STATUS;
310
  values[0].data.d_status = status;
Elliot Lee's avatar
Elliot Lee committed
311 312 313
}


314 315 316
/*  A generic wrapper to gimp_pixel_rgn_get_row which handles unlimited
 *  wrapping or gives you transparent regions outside the image
 */
Elliot Lee's avatar
Elliot Lee committed
317

318
static void
Sven Neumann's avatar
Sven Neumann committed
319
my_get_row (GimpPixelRgn *PR,
320 321 322 323
	    guchar       *dest,
	    gint          x,
	    gint          y,
	    gint          w)
324
{
325 326 327 328 329 330
  gint width, height, bytes;
  gint i;

  width  = PR->drawable->width;
  height = PR->drawable->height;
  bytes  = PR->drawable->bpp;
331 332 333 334 335 336 337 338 339 340 341 342

  /* Y-wrappings */

  switch (my_config.bmode)
    {
    case WRAP:
      /* Wrapped, so we get the proper row from the other side */
      while (y < 0) /* This is the _sure_ way to wrap. :) */
	y += height;
      while (y >= height)
	y -= height;
      break;
343

344 345 346 347 348 349
    case CLEAR:
      /* Beyond borders, so set full transparent. */
      if (y < 0 || y >= height)
	{
	  memset (dest, 0, w * bytes);
	  return; /* Done, so back. */
Elliot Lee's avatar
Elliot Lee committed
350
	}
351 352 353 354 355 356 357 358 359 360 361
    case MIRROR:
      /* The border lines are _not_ duplicated in the mirror image */
      /* is this right? */
      while (y < 0 || y >= height)
	{
	  if (y < 0)
	    y = -y; /* y=-y-1 */
	  if (y >= height)
	    y = 2 * height - y - 2; /* y=2*height-y-1 */  
	}
      break;
362

363
    case EXTEND:
364
      y = CLAMP (y , 0 , height - 1);
365 366
      break;
    }
367

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 403 404 405 406
  switch (my_config.bmode)
    {
    case CLEAR:
      if (x < 0)
	{
	  i = MIN (w, -x);
	  memset (dest, 0, i * bytes);
	  dest += i * bytes;
	  w -= i;
	  x += i;
	}
      if (w)
	{
	  i = MIN (w, width);
	  gimp_pixel_rgn_get_row (PR, dest, x, y, i);
	  dest += i * bytes;
	  w -= i;
	  x += i;
	}
      if (w)
	memset (dest, 0, w * bytes);
      break;

    case WRAP:
      while (x < 0)
	x += width;
      i = MIN (w, width - x);
      gimp_pixel_rgn_get_row (PR, dest, x, y, i);
      w -= i;
      dest += i * bytes;
      x = 0;
      while (w)
	{
	  i = MIN (w, width);
	  gimp_pixel_rgn_get_row (PR, dest, x, y, i);
	  w -= i;
	  dest += i * bytes;
	}
      break;
407

408 409 410 411 412 413 414
    case EXTEND:
      if (x < 0)
	{
	  gimp_pixel_rgn_get_pixel (PR, dest, 0, y);
	  x++;
	  w--;
	  dest += bytes;
415

416 417 418 419 420 421
	  while (x < 0 && w)
	    {
	      for (i = 0; i < bytes; i++)
		{
		  *dest = *(dest - bytes);
		  dest++;
Elliot Lee's avatar
Elliot Lee committed
422
		}
423 424 425
	      x++;
	      w--;
	    }
Elliot Lee's avatar
Elliot Lee committed
426
	}
427
      if (w && width - x > 0)
428
	{
429
	  i = MIN (w, width - x);
430 431 432
	  gimp_pixel_rgn_get_row (PR, dest, x, y, i);
	  w -= i;
	  dest += i * bytes;
Elliot Lee's avatar
Elliot Lee committed
433
	}
434 435 436 437 438 439
      while (w)
	{
	  for (i = 0; i < bytes; i++)
	    {
	      *dest= *(dest - bytes);
	      dest++;
Elliot Lee's avatar
Elliot Lee committed
440
	    }
441 442
	  x++;
	  w--;
Elliot Lee's avatar
Elliot Lee committed
443
	}
444
      break;
445

446 447 448
    case MIRROR: /* Not yet handled */
      break;
    }
Elliot Lee's avatar
Elliot Lee committed
449 450
}

451 452 453 454 455
static gfloat
calcmatrix (guchar **srcrow,
	    gint     xoff,
	    gint     i)
{
456 457 458 459 460
  static gfloat matrixsum = 0;
  static gint bytes       = 0;

  gfloat sum      = 0;
  gfloat alphasum = 0;
461
  gfloat temp;
462
  gint   x, y;
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

  if (!bytes)
    {
      bytes = drawable->bpp;
      for (y = 0; y < 5; y++)
	for (x = 0; x < 5; x++)
	  {
	    temp = my_config.matrix[x][y];
	    matrixsum += ABS (temp);
	  }
    }
  for (y = 0; y < 5; y++)
    for (x = 0; x < 5; x++)
      {
	temp = my_config.matrix[x][y];
	if (i != (bytes - 1) && my_config.alpha_alg == 1)
	  {
	    temp *= srcrow[y][xoff + x * bytes +bytes - 1 - i];
	    alphasum += ABS (temp);
	  }
	temp *= srcrow[y][xoff + x * bytes];
	sum += temp;
      }
  sum /= my_config.divisor;
  if (i != (bytes - 1) && my_config.alpha_alg == 1)
    {
      if (alphasum != 0)
	sum = sum * matrixsum / alphasum;
      else
	sum = 0;
      /* sum = srcrow[2][xoff + 2 * bytes] * my_config.matrix[2][2];*/
    }
  sum += my_config.offset;

  return sum;
}
static void
doit (void)
{
502 503 504
  GimpPixelRgn  srcPR, destPR;
  gint          width, height, row, col;
  gint          w, h, i;
505
  gint 		sx1, sy1, sx2, sy2;
506
  gint          x1, x2, y1, y2;
507 508 509 510 511 512 513
  guchar       *destrow[3];
  guchar       *srcrow[5];
  guchar       *temprow;
  gfloat        sum;
  gint          xoff;
  gint          chanmask[4];

514 515 516 517 518 519
  /* Get the input area. This is the bounding box of the selection in
   *  the image (or the entire image if there is no selection). Only
   *  operating on the input area is simply an optimization. It doesn't
   *  need to be done for correct operation. (It simply makes it go
   *  faster, since fewer pixels need to be operated on).
   */
520
  gimp_drawable_mask_bounds (drawable->drawable_id, &sx1, &sy1, &sx2, &sy2);
521 522 523 524 525 526 527 528 529 530
  w = sx2 - sx1;
  h = sy2 - sy1;

  /* Get the size of the input image. (This will/must be the same
   *  as the size of the output image.
   */
  width  = drawable->width;
  height = drawable->height;
  bytes  = drawable->bpp;

531
  if (gimp_drawable_is_rgb (drawable->drawable_id))
532 533 534 535
    for (i = 0; i <3; i++)
      chanmask[i] = my_config.channels[i + 1];
  else /* Grayscale */
    chanmask[0] = my_config.channels[0];
536

537
  if (gimp_drawable_has_alpha (drawable->drawable_id))
538 539 540 541 542 543 544 545
    chanmask[bytes - 1] = my_config.channels[4];

  for (i = 0; i < 5; i++)
    srcrow[i] = g_new (guchar, (w + 4) * bytes);
  for (i = 0; i < 3; i++)
    destrow[i]= g_new (guchar, w * bytes);

  /*  initialize the pixel regions  */
546 547 548 549
  x1 = MAX (sx1 - 2, 0);
  y1 = MAX (sy1 - 2, 0);
  x2 = MIN (sx2 + 2, width);
  y2 = MIN (sy2 + 2, height);
550
  gimp_pixel_rgn_init (&srcPR, drawable,
551
                       x1, y1, x2 - x1, y2 - y1, FALSE, FALSE);
552 553 554 555
  gimp_pixel_rgn_init (&destPR, drawable, sx1, sy1, w, h, TRUE, TRUE);

  /* initialize source arrays */
  for (i = 0; i < 5; i++)
556
    my_get_row (&srcPR, srcrow[i], sx1 - 2, sy1 + i - 2, w + 4);
557 558 559 560

  for (row = sy1; row < sy2; row++)
    {
      xoff = 0;
561

562 563 564 565 566 567 568
      for (col = sx1; col < sx2; col++)
	for (i = 0; i < bytes; i++)
	  {
	    if (chanmask[i] <= 0)
	      sum = srcrow[2][xoff + 2 * bytes];
	    else
	      sum = calcmatrix(srcrow, xoff, i);
569

570 571 572
	    destrow[2][xoff]= (guchar) CLAMP (sum, 0, 255);
	    xoff++;
	  }
573

574 575
      if (row > sy1 + 1)
	gimp_pixel_rgn_set_row (&destPR, destrow[0], sx1, row - 2, w);
576

577 578 579 580 581
      temprow = destrow[0];
      destrow[0] = destrow[1];
      destrow[1] = destrow[2];
      destrow[2] = temprow;
      temprow = srcrow[0];
582

583 584
      for (i = 0; i < 4; i++)
	srcrow[i] = srcrow[i + 1];
585

586 587
      srcrow[4] = temprow;
      my_get_row (&srcPR, srcrow[4], sx1 - 2, row + 3, w + 4);
588

589 590
      if (row % 10 == 0)
        gimp_progress_update ((double) (row - sy1) / h);
591
    }
592

593 594 595 596 597 598 599 600 601 602
  /* put the final rows in the buffer in place */
  if (h < 3)
    gimp_pixel_rgn_set_row (&destPR, destrow[2], sx1, row - 3, w);
  if (h > 1)
    gimp_pixel_rgn_set_row (&destPR, destrow[0], sx1, row - 2, w);
  if (h > 2)
    gimp_pixel_rgn_set_row (&destPR, destrow[1], sx1, row - 1, w);

  /*  update the timred region  */
  gimp_drawable_flush (drawable);
603 604
  gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
  gimp_drawable_update (drawable->drawable_id, sx1, sy1, sx2 - sx1, sy2 - sy1);
Elliot Lee's avatar
Elliot Lee committed
605 606 607 608 609 610
}

/***************************************************
 * GUI stuff
 */

611 612 613 614
static void
fprint (gfloat  f,
	gchar  *buffer)
{
615
  gint i, t;
616 617

  sprintf (buffer, "%.7f", f);
618

619
  for (t = 0; buffer[t] != '.'; t++);
620

621
  i = t + 1;
622

623 624 625 626
  while (buffer[i] != '\0')
    {
      if (buffer[i] != '0')
	t = i + 1;
627

628 629
      i++;
    }
630

631
  buffer[t] = '\0';
Elliot Lee's avatar
Elliot Lee committed
632 633
}

634 635 636
static void
redraw_matrix (void)
{
637
  gint x,y;
638
  gchar buffer[12];
Elliot Lee's avatar
Elliot Lee committed
639

640 641 642 643
  for (y = 0; y < 5; y++)
    for (x = 0; x < 5; x++)
      {
	fprint (my_config.matrix[x][y], buffer);
Elliot Lee's avatar
Elliot Lee committed
644

645 646
	gtk_entry_set_text (GTK_ENTRY (my_widgets.matrix[x][y]), buffer);
      }
Elliot Lee's avatar
Elliot Lee committed
647 648
}

649 650 651
static void
redraw_channels (void)
{
652
  gint i;
Elliot Lee's avatar
Elliot Lee committed
653

654 655 656
  for (i = 0; i < 5; i++)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my_widgets.channels[i]),
				  my_config.channels[i] > 0);
Elliot Lee's avatar
Elliot Lee committed
657 658
}

659 660 661 662 663
static void
redraw_autoset (void)
{
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my_widgets.autoset),
				my_config.autoset);
Elliot Lee's avatar
Elliot Lee committed
664 665
}

666 667 668 669 670
static void
redraw_alpha_alg (void)
{
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my_widgets.alpha_alg),
				my_config.alpha_alg > 0);
Elliot Lee's avatar
Elliot Lee committed
671 672
}

673 674 675 676
static void
redraw_off_and_div (void)
{
  gchar buffer[12];
Elliot Lee's avatar
Elliot Lee committed
677

678 679 680 681
  fprint (my_config.divisor, buffer);
  gtk_entry_set_text (GTK_ENTRY (my_widgets.divisor), buffer);
  fprint (my_config.offset, buffer);
  gtk_entry_set_text (GTK_ENTRY (my_widgets.offset), buffer);
Elliot Lee's avatar
Elliot Lee committed
682 683
}

684 685 686 687 688
static void
redraw_bmode (void)
{
  gtk_toggle_button_set_active
    (GTK_TOGGLE_BUTTON (my_widgets.bmode[my_config.bmode]), TRUE);
Elliot Lee's avatar
Elliot Lee committed
689 690
}

691 692 693 694 695 696 697 698 699 700
static void
redraw_all (void)
{
  redraw_matrix ();
  redraw_off_and_div ();
  redraw_autoset ();
  redraw_alpha_alg ();
  redraw_bmode ();
  redraw_channels ();
}
Elliot Lee's avatar
Elliot Lee committed
701

702 703 704
static void
check_matrix (void)
{
705 706 707
  gint      x, y;
  gboolean  valid = FALSE;
  gfloat    sum   = 0.0;
708 709 710 711 712 713

  for (y = 0; y < 5; y++)
    for (x = 0; x < 5; x++)
      {
	sum += my_config.matrix[x][y];
	if (my_config.matrix[x][y] != 0.0)
714
	  valid = TRUE;
715 716 717 718 719 720 721 722
      }

  if (my_config.autoset)
    {
      if (sum > 0)
	{
	  my_config.offset = 0;
	  my_config.divisor = sum;
Elliot Lee's avatar
Elliot Lee committed
723
	}
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
      else if (sum < 0)
	{
	  my_config.offset = 255;
	  my_config.divisor = -sum;
	}
      else
	{
	  my_config.offset = 128;
	  /* The sum is 0, so this is probably some sort of
	   * embossing filter. Should divisor be autoset to 1
	   * or left undefined, ie. for the user to define? */
	  my_config.divisor = 1;
	}
      redraw_off_and_div ();
    }
  /* gtk_widget_set_sensitive(my_widgets.ok,valid); */
Elliot Lee's avatar
Elliot Lee committed
740 741
}

742 743 744 745
static void
ok_callback (GtkWidget *widget,
	     gpointer   data)
{
746
  run_flag = TRUE;
Elliot Lee's avatar
Elliot Lee committed
747

748
  gtk_widget_destroy (GTK_WIDGET (data));
Elliot Lee's avatar
Elliot Lee committed
749 750
}

751 752 753
/* Checks that the configuration is valid for the image type */
static void
check_config (void)
Elliot Lee's avatar
Elliot Lee committed
754
{
755
  gint i;
756 757 758 759

  for (i = 0; i < 5; i++)
    if (my_config.channels[i] < 0)
      my_config.channels[i] = 0;
760

761
  if (gimp_drawable_is_rgb (drawable->drawable_id))
762
    my_config.channels[0] = -1;
763
  else if (gimp_drawable_is_gray (drawable->drawable_id))
764 765
    for (i = 1; i < 4; i++)
      my_config.channels[i] = -1;
766

767
  if (!gimp_drawable_has_alpha (drawable->drawable_id))
768 769
    {
      my_config.channels[4] = -1;
770 771
      my_config.alpha_alg   = -1;
      my_config.bmode       = EXTEND;
772
    }
Elliot Lee's avatar
Elliot Lee committed
773 774
}

775 776 777
static void
defaults_callback (GtkWidget *widget,
		   gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
778
{
779 780 781
  my_config = default_config;
  check_config ();
  redraw_all ();
Elliot Lee's avatar
Elliot Lee committed
782 783
}

784 785 786
static void
entry_callback (GtkWidget *widget,
		gpointer   data)
Elliot Lee's avatar
Elliot Lee committed
787
{
788
  gfloat *value = (gfloat *) data;
Elliot Lee's avatar
Elliot Lee committed
789

790
  *value = atof (gtk_entry_get_text (GTK_ENTRY (widget)));
Elliot Lee's avatar
Elliot Lee committed
791

792 793 794 795 796 797 798 799 800
#if 0
  check_matrix ();
#else
  if (widget == my_widgets.divisor)
    gtk_widget_set_sensitive (GTK_WIDGET (my_widgets.ok), (*value != 0.0));
  else if (widget != my_widgets.offset)
    check_matrix ();
#endif	
}
Elliot Lee's avatar
Elliot Lee committed
801

802 803 804 805
static void
my_toggle_callback (GtkWidget *widget,
		    gpointer   data)
{
806
  gint val = GTK_TOGGLE_BUTTON (widget)->active;
Elliot Lee's avatar
Elliot Lee committed
807

808
  if (val)
809
    *(gint *) data = TRUE;
810
  else
811
    *(gint *) data = FALSE;
Elliot Lee's avatar
Elliot Lee committed
812

813 814 815 816 817 818 819
  if (widget == my_widgets.alpha_alg)
    {
      gtk_widget_set_sensitive (my_widgets.bmode[CLEAR], val);
      if (val == 0 && my_config.bmode == CLEAR)
	{
	  my_config.bmode = EXTEND;
	  redraw_bmode ();
Elliot Lee's avatar
Elliot Lee committed
820
	}
821 822 823 824 825 826 827
    }
  else if (widget == my_widgets.autoset)
    {
      gtk_widget_set_sensitive (my_widgets.divisor, !val);
      gtk_widget_set_sensitive (my_widgets.offset, !val);
      check_matrix ();
    }
Elliot Lee's avatar
Elliot Lee committed
828 829
}

830 831 832 833 834 835
static void
my_bmode_callback (GtkWidget *widget,
		   gpointer   data)
{
  my_config.bmode = (int) data - 1;
}
Elliot Lee's avatar
Elliot Lee committed
836

837 838 839 840 841 842 843 844 845 846 847 848 849
static gint
dialog (void)
{
  GtkWidget *dlg;
  GtkWidget *main_hbox;
  GtkWidget *table;
  GtkWidget *label;
  GtkWidget *entry;
  GtkWidget *button;
  GtkWidget *box;
  GtkWidget *inbox;
  GtkWidget *yetanotherbox;
  GtkWidget *frame;
850 851 852
  gchar      buffer[32];
  gint       x, y, i;
  GSList    *group;
853 854

  gimp_ui_init ("convmatrix", FALSE);
855 856

  dlg = gimp_dialog_new (_("Convolution Matrix"), "convmatrix",
857
			 gimp_standard_help_func, "filters/convmatrix.html",
858 859 860
			 GTK_WIN_POS_MOUSE,
			 FALSE, TRUE, FALSE,

861
			 GTK_STOCK_CANCEL, gtk_widget_destroy,
862
			 NULL, 1, NULL, FALSE, TRUE,
863 864 865 866

			 GIMP_STOCK_RESET, defaults_callback,
			 NULL, 1, NULL, FALSE, FALSE,

Sven Neumann's avatar
Sven Neumann committed
867 868
			 GTK_STOCK_OK, ok_callback,
			 NULL, NULL, &my_widgets.ok, TRUE, FALSE,
869 870 871

			 NULL);

872 873 874
  g_signal_connect (G_OBJECT (dlg), "destroy",
                    G_CALLBACK (gtk_main_quit),
                    NULL);
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

  main_hbox = gtk_hbox_new (FALSE, 4);
  gtk_container_set_border_width (GTK_CONTAINER (main_hbox), 6);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), main_hbox,
		      TRUE, TRUE, 0);

  yetanotherbox = gtk_vbox_new (FALSE, 4);
  gtk_box_pack_start (GTK_BOX (main_hbox), yetanotherbox, FALSE, FALSE, 0);

  frame = gtk_frame_new (_("Matrix"));
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  gtk_box_pack_start (GTK_BOX (yetanotherbox), frame, FALSE, FALSE, 0);

  inbox = gtk_vbox_new (FALSE, 6);
  gtk_container_set_border_width (GTK_CONTAINER (inbox), 4);
  gtk_container_add (GTK_CONTAINER (frame), inbox);

  table = gtk_table_new (5, 5, FALSE);
  gtk_table_set_row_spacings (GTK_TABLE (table), 4);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_box_pack_start (GTK_BOX (inbox), table, FALSE, FALSE, 0);

  for (y = 0; y < 5; y++)
    for (x = 0; x < 5; x++)
      {
	my_widgets.matrix[x][y] = entry = gtk_entry_new ();
901
	gtk_widget_set_size_request (entry, 40, -1);
902 903 904 905
	gtk_table_attach (GTK_TABLE (table), entry, x, x+1, y, y+1,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_entry_set_text (GTK_ENTRY (entry), buffer);
	gtk_widget_show (entry);
906 907 908 909

	g_signal_connect (G_OBJECT (entry), "changed",
                          G_CALLBACK (entry_callback),
                          &my_config.matrix[x][y]);
910 911 912 913 914 915 916 917 918 919 920
      }

  gtk_widget_show (table);

  box = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (inbox), box, FALSE, FALSE, 0);

  table = gtk_table_new (1, 2, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_box_pack_start (GTK_BOX (box), table, TRUE, FALSE, 0);

921
  label = gtk_label_new_with_mnemonic (_("D_ivisor:"));
922 923 924 925 926
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 0, 1);
  gtk_widget_show (label);

  my_widgets.divisor = entry = gtk_entry_new ();
927
  gtk_widget_set_size_request (entry, 40, -1);
928 929
  gtk_table_attach_defaults (GTK_TABLE (table), entry, 1, 2, 0, 1);
  gtk_widget_show (entry);
930
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
931

932 933 934 935
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (entry_callback),
                    &my_config.divisor);

936 937 938 939 940 941
  gtk_widget_show (table);

  table = gtk_table_new (1, 2, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  gtk_box_pack_start (GTK_BOX (box), table, TRUE, FALSE, 0);

942
  label = gtk_label_new_with_mnemonic (_("O_ffset:"));
943 944 945 946 947
  gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 0, 1);
  gtk_widget_show (label);

  my_widgets.offset = entry = gtk_entry_new ();
948
  gtk_widget_set_size_request (entry, 40, -1);
949 950
  gtk_table_attach_defaults (GTK_TABLE (table), entry, 1, 2, 0, 1);
  gtk_widget_show (entry);
951
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
952

953 954 955 956
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (entry_callback),
                    &my_config.offset);

957 958 959 960 961 962 963 964 965 966 967
  gtk_widget_show (table);

  gtk_widget_show (box);

  gtk_widget_show (inbox);
  gtk_widget_show (frame);

  box = gtk_vbox_new (FALSE, 2);
  gtk_box_pack_start (GTK_BOX (yetanotherbox), box, FALSE, FALSE, 0);

  my_widgets.autoset = button =
968
    gtk_check_button_new_with_mnemonic (_("A_utomatic"));
969 970 971
  gtk_box_pack_start (GTK_BOX (box), button, TRUE, FALSE, 0);
  gtk_widget_show (button);

972 973 974 975
  g_signal_connect (G_OBJECT (button), "toggled",
                    G_CALLBACK (my_toggle_callback),
                    &my_config.autoset);

976
  my_widgets.alpha_alg = button =
977
    gtk_check_button_new_with_mnemonic (_("A_lpha-weighting"));
978 979 980 981 982
  if (my_config.alpha_alg == -1)
    gtk_widget_set_sensitive (button, FALSE);
  gtk_box_pack_start (GTK_BOX (box), button, TRUE, FALSE, 0);
  gtk_widget_show (button);

983 984 985 986
  g_signal_connect (G_OBJECT (button), "toggled",
                    G_CALLBACK (my_toggle_callback),
                    &my_config.alpha_alg);

987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
  gtk_widget_show (box);
  gtk_widget_show (yetanotherbox);

  inbox = gtk_vbox_new (FALSE, 4);
  gtk_box_pack_start (GTK_BOX (main_hbox), inbox, FALSE, FALSE, 0);

  frame = gtk_frame_new (_("Border"));
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  gtk_box_pack_start (GTK_BOX (inbox), frame, FALSE, FALSE, 0);

  box = gtk_vbox_new (FALSE, 1);
  gtk_container_set_border_width (GTK_CONTAINER (box), 2);
  gtk_container_add (GTK_CONTAINER (frame), box);

  group = NULL;

  for (i = 0; i < 3; i++)
    {
      my_widgets.bmode[i] = button =
1006
	gtk_radio_button_new_with_mnemonic (group, gettext (bmode_labels[i]));
1007
      group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));
1008 1009
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
      gtk_widget_show (button);
1010 1011 1012 1013

      g_signal_connect (G_OBJECT (button), "toggled",
                        G_CALLBACK (my_bmode_callback),
                        (gpointer) (i + 1));
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
    }

  gtk_widget_show (box);
  gtk_widget_show (frame);

  frame=gtk_frame_new (_("Channels"));
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  gtk_box_pack_start (GTK_BOX (inbox), frame, FALSE, FALSE, 0);

  box = gtk_vbox_new (FALSE, 1);
  gtk_container_set_border_width (GTK_CONTAINER (box), 2);
  gtk_container_add (GTK_CONTAINER (frame), box);

  for (i = 0; i < 5; i++)
    {
      my_widgets.channels[i] = button =
1030
	gtk_check_button_new_with_mnemonic (gettext (channel_labels[i]));
1031

1032 1033
      if (my_config.channels[i] < 0)
	gtk_widget_set_sensitive (button, FALSE);
1034

1035 1036
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
      gtk_widget_show (button);
1037 1038 1039 1040

      g_signal_connect (G_OBJECT (button), "toggled",
                        G_CALLBACK (my_toggle_callback),
                        &my_config.channels[i]);
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
    }

  gtk_widget_show(box);
  gtk_widget_show (frame);

  gtk_widget_show (inbox);

  gtk_widget_show (main_hbox);

  gtk_widget_show (dlg);
  redraw_all ();

1053 1054
  gtk_widget_set_sensitive (my_widgets.bmode[CLEAR], 
			    (my_config.alpha_alg > 0));
1055 1056 1057 1058 1059 1060

  gtk_main ();
  gdk_flush ();

  return run_flag;
}