~ubuntu-branches/ubuntu/hoary/gimp/hoary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
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
531
532
533
534
535
536
537
538
539
540
541
542
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
/* 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * The GNU General Public License is also available from
 * http://www.fsf.org/copyleft/gpl.html
 *
 *
 * CHANGELOG:
 * v0.13	15.12.2000
 * 	Made the PDB interface actually work.   (Simon Budig <simon@gimp.org>)
 *
 * 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?
 *
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <gtk/gtk.h>

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "libgimp/stdplugins-intl.h"


#define RESPONSE_RESET 1

typedef enum
{
  EXTEND,
  WRAP,
  CLEAR,
  MIRROR
} BorderMode;

static GimpDrawable *drawable;

static gchar * const channel_labels[] =
{
  N_("Gr_ey"),
  N_("Re_d"),
  N_("_Green"),
  N_("_Blue"),
  N_("_Alpha")
};

static gchar * const bmode_labels[] =
{
  N_("E_xtend"),
  N_("_Wrap"),
  N_("Cro_p")
};

/* Declare local functions. */
static void query (void);
static void run   (const gchar      *name,
		   gint              nparams,
		   const GimpParam  *param,
		   gint             *nreturn_vals,
		   GimpParam       **return_vals);

static gboolean  dialog       (void);

static void      convmatrix   (void);
static void      check_config (void);


GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,   /* init_proc  */
  NULL,   /* quit_proc  */
  query,  /* query_proc */
  run,    /* run_proc   */
};

static gint     bytes;
static gboolean run_flag = FALSE;

typedef struct
{
  gfloat     matrix[5][5];
  gfloat     divisor;
  gfloat     offset;
  gint       alpha_alg;
  BorderMode bmode;
  gint       channels[5];
  gint       autoset;
} config;

static const config default_config =
{
  {
    { 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 */
};

static config my_config;

struct
{
  GtkWidget *matrix[5][5];
  GtkWidget *divisor;
  GtkWidget *offset;
  GtkWidget *alpha_alg;
  GtkWidget *bmode[3];
  GtkWidget *channels[5];
  GtkWidget *autoset;
} my_widgets;


MAIN ()

static void
query (void)
{
  static GimpParamDef args[] =
  {
    { GIMP_PDB_INT32,    "run_mode", "Interactive, non-interactive" },
    { GIMP_PDB_IMAGE,    "image",    "Input image (unused)" },
    { 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" }
  };

  gimp_install_procedure ("plug_in_convmatrix",
			  "A generic 5x5 convolution matrix",
			  "",
			  "Lauri Alanko",
			  "Lauri Alanko",
			  "1997",
			  N_("_Convolution Matrix..."),
			  "RGB*, GRAY*",
			  GIMP_PLUGIN,
			  G_N_ELEMENTS (args), 0,
			  args, NULL);

  gimp_plugin_menu_register ("plug_in_convmatrix", "<Image>/Filters/Generic");
}

static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
{
  static GimpParam  values[1];
  GimpRunMode       run_mode;
  GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  gint              x, y;

  INIT_I18N ();

  *nreturn_vals = 1;
  *return_vals = values;

  run_mode = param[0].data.d_int32;

  /*  Get the specified drawable  */
  drawable = gimp_drawable_get (param[2].data.d_drawable);

  /*  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 on layers "
                   "smaller than 3 pixels."));
      status = GIMP_PDB_EXECUTION_ERROR;
      values[0].type = GIMP_PDB_STATUS;
      values[0].data.d_status = status;
      return;
    }

  my_config = default_config;
  if (run_mode == GIMP_RUN_NONINTERACTIVE)
    {
      if (nparams != 11)
        {
          status = GIMP_PDB_CALLING_ERROR;
        }
      else
	{
	  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;

	  check_config ();
	}
    }
  else
    {
      gimp_get_data ("plug_in_convmatrix", &my_config);

      if (run_mode == GIMP_RUN_INTERACTIVE)
	{
	  /*  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. */
	      status = GIMP_PDB_EXECUTION_ERROR;
	    }
	}
    }

  if (status == GIMP_PDB_SUCCESS)
    {
      /*  Make sure that the drawable is gray or RGB color  */
      if (gimp_drawable_is_rgb (drawable->drawable_id) ||
	  gimp_drawable_is_gray (drawable->drawable_id))
	{
	  gimp_progress_init (_("Applying convolution"));
	  gimp_tile_cache_ntiles (2 * (drawable->width /
				       gimp_tile_width () + 1));

	  convmatrix ();

	  if (run_mode != GIMP_RUN_NONINTERACTIVE)
	    gimp_displays_flush ();

	  if (run_mode == GIMP_RUN_INTERACTIVE)
	    gimp_set_data ("plug_in_convmatrix",
                           &my_config, sizeof (my_config));
	}
      else
	{
	  status = GIMP_PDB_EXECUTION_ERROR;
	}

      gimp_drawable_detach (drawable);
    }

  values[0].type = GIMP_PDB_STATUS;
  values[0].data.d_status = status;
}


/*  A generic wrapper to gimp_pixel_rgn_get_row which handles unlimited
 *  wrapping or gives you transparent regions outside the image
 */

static void
my_get_row (GimpPixelRgn *PR,
	    guchar       *dest,
	    gint          x,
	    gint          y,
	    gint          w)
{
  gint width, height, bytes;
  gint i;

  width  = PR->drawable->width;
  height = PR->drawable->height;
  bytes  = PR->drawable->bpp;

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

    case CLEAR:
      /* Beyond borders, so set full transparent. */
      if (y < 0 || y >= height)
	{
	  memset (dest, 0, w * bytes);
	  return; /* Done, so back. */
	}
    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;

    case EXTEND:
      y = CLAMP (y , 0 , height - 1);
      break;
    }

  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;

    case EXTEND:
      if (x < 0)
	{
	  gimp_pixel_rgn_get_pixel (PR, dest, 0, y);
	  x++;
	  w--;
	  dest += bytes;

	  while (x < 0 && w)
	    {
	      for (i = 0; i < bytes; i++)
		{
		  *dest = *(dest - bytes);
		  dest++;
		}
	      x++;
	      w--;
	    }
	}
      if (w && width - x > 0)
	{
	  i = MIN (w, width - x);
	  gimp_pixel_rgn_get_row (PR, dest, x, y, i);
	  w -= i;
	  dest += i * bytes;
	}
      while (w)
	{
	  for (i = 0; i < bytes; i++)
	    {
	      *dest= *(dest - bytes);
	      dest++;
	    }
	  x++;
	  w--;
	}
      break;

    case MIRROR: /* Not yet handled */
      break;
    }
}

static gfloat
calcmatrix (guchar **srcrow,
	    gint     xoff,
	    gint     i)
{
  static gfloat matrixsum = 0;
  static gint bytes       = 0;

  gfloat sum      = 0;
  gfloat alphasum = 0;
  gfloat temp;
  gint   x, y;

  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
convmatrix (void)
{
  GimpPixelRgn  srcPR, destPR;
  gint          width, height, row, col;
  gint          w, h, i;
  gint 		sx1, sy1, sx2, sy2;
  gint          x1, x2, y1, y2;
  guchar       *destrow[3];
  guchar       *srcrow[5];
  guchar       *temprow;
  gfloat        sum;
  gint          xoff;
  gint          chanmask[4];

  /* 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).
   */
  gimp_drawable_mask_bounds (drawable->drawable_id, &sx1, &sy1, &sx2, &sy2);
  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;

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

  if (gimp_drawable_has_alpha (drawable->drawable_id))
    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  */
  x1 = MAX (sx1 - 2, 0);
  y1 = MAX (sy1 - 2, 0);
  x2 = MIN (sx2 + 2, width);
  y2 = MIN (sy2 + 2, height);
  gimp_pixel_rgn_init (&srcPR, drawable,
                       x1, y1, x2 - x1, y2 - y1, FALSE, FALSE);
  gimp_pixel_rgn_init (&destPR, drawable, sx1, sy1, w, h, TRUE, TRUE);

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

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

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

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

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

      temprow = destrow[0];
      destrow[0] = destrow[1];
      destrow[1] = destrow[2];
      destrow[2] = temprow;
      temprow = srcrow[0];

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

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

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

  /* 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);
  gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
  gimp_drawable_update (drawable->drawable_id, sx1, sy1, sx2 - sx1, sy2 - sy1);
}

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

static void
fprint (gfloat  f,
	gchar  *buffer,
        gsize   len)
{
  gint i, t;

  g_snprintf (buffer, len, "%.7f", f);
  buffer[len - 1] = '\0';

  for (t = 0; t < len - 1 && buffer[t] != '.'; t++);

  i = t + 1;

  while (buffer[i] != '\0')
    {
      if (buffer[i] != '0')
	t = i + 1;

      i++;
    }

  buffer[t] = '\0';
}

static void
redraw_matrix (void)
{
  gint  x, y;
  gchar buffer[12];

  for (y = 0; y < 5; y++)
    for (x = 0; x < 5; x++)
      {
        fprint (my_config.matrix[x][y], buffer, sizeof (buffer));
	gtk_entry_set_text (GTK_ENTRY (my_widgets.matrix[x][y]), buffer);
      }
}

static void
redraw_channels (void)
{
  gint i;

  for (i = 0; i < 5; i++)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my_widgets.channels[i]),
				  my_config.channels[i] > 0);
}

static void
redraw_autoset (void)
{
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my_widgets.autoset),
				my_config.autoset);
}

static void
redraw_alpha_alg (void)
{
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (my_widgets.alpha_alg),
				my_config.alpha_alg > 0);
}

static void
redraw_off_and_div (void)
{
  gchar buffer[12];

  fprint (my_config.divisor, buffer, sizeof (buffer));
  gtk_entry_set_text (GTK_ENTRY (my_widgets.divisor), buffer);

  fprint (my_config.offset, buffer, sizeof (buffer));
  gtk_entry_set_text (GTK_ENTRY (my_widgets.offset), buffer);
}

static void
redraw_bmode (void)
{
  gtk_toggle_button_set_active
    (GTK_TOGGLE_BUTTON (my_widgets.bmode[my_config.bmode]), TRUE);
}

static void
redraw_all (void)
{
  redraw_matrix ();
  redraw_off_and_div ();
  redraw_autoset ();
  redraw_alpha_alg ();
  redraw_bmode ();
  redraw_channels ();
}

static void
check_matrix (void)
{
  gint      x, y;
  gboolean  valid = FALSE;
  gfloat    sum   = 0.0;

  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)
	  valid = TRUE;
      }

  if (my_config.autoset)
    {
      if (sum > 0)
	{
	  my_config.offset = 0;
	  my_config.divisor = sum;
	}
      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); */
}

static void
response_callback (GtkWidget *widget,
                   gint       response_id,
		   gpointer   data)
{
  switch (response_id)
    {
    case RESPONSE_RESET:
      my_config = default_config;
      check_config ();
      redraw_all ();
      break;

    case GTK_RESPONSE_OK:
      run_flag = TRUE;

    default:
      gtk_widget_destroy (GTK_WIDGET (widget));
      break;
    }
}

/* Checks that the configuration is valid for the image type */
static void
check_config (void)
{
  gint i;

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

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

  if (!gimp_drawable_has_alpha (drawable->drawable_id))
    {
      my_config.channels[4] = -1;
      my_config.alpha_alg   = -1;
      my_config.bmode       = EXTEND;
    }
}

static void
entry_callback (GtkWidget *widget,
		gpointer   data)
{
  gfloat *value = (gfloat *) data;

  *value = atof (gtk_entry_get_text (GTK_ENTRY (widget)));

#if 0
  check_matrix ();
#else
  if (widget == my_widgets.divisor)
    gtk_dialog_set_response_sensitive (GTK_DIALOG (gtk_widget_get_toplevel (widget)),
                                       GTK_RESPONSE_OK, (*value != 0.0));
  else if (widget != my_widgets.offset)
    check_matrix ();
#endif
}

static void
my_toggle_callback (GtkWidget *widget,
		    gpointer   data)
{
  gint val = GTK_TOGGLE_BUTTON (widget)->active;

  if (val)
    *(gint *) data = TRUE;
  else
    *(gint *) data = FALSE;

  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 ();
	}
    }
  else if (widget == my_widgets.autoset)
    {
      gtk_widget_set_sensitive (my_widgets.divisor, !val);
      gtk_widget_set_sensitive (my_widgets.offset, !val);
      check_matrix ();
    }
}

static void
my_bmode_callback (GtkWidget *widget,
		   gpointer   data)
{
  my_config.bmode = GPOINTER_TO_INT (data) - 1;
}

static gboolean
dialog (void)
{
  GtkWidget *dlg;
  GtkWidget *main_hbox;
  GtkWidget *table;
  GtkWidget *label;
  GtkWidget *entry;
  GtkWidget *button;
  GtkWidget *box;
  GtkWidget *inbox;
  GtkWidget *vbox;
  GtkWidget *frame;
  gint       x, y, i;
  GSList    *group;

  gimp_ui_init ("convmatrix", FALSE);

  dlg = gimp_dialog_new (_("Convolution Matrix"), "convmatrix",
                         NULL, 0,
			 gimp_standard_help_func, "plug-in-convmatrix",

			 GIMP_STOCK_RESET, RESPONSE_RESET,
			 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			 GTK_STOCK_OK,     GTK_RESPONSE_OK,

			 NULL);

  g_signal_connect (dlg, "response",
                    G_CALLBACK (response_callback),
                    NULL);
  g_signal_connect (dlg, "destroy",
                    G_CALLBACK (gtk_main_quit),
                    NULL);

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

  vbox = gtk_vbox_new (FALSE, 12);
  gtk_box_pack_start (GTK_BOX (main_hbox), vbox, TRUE, TRUE, 0);

  frame = gimp_frame_new (_("Matrix"));
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

  inbox = gtk_vbox_new (FALSE, 12);
  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, TRUE, TRUE, 0);

  for (y = 0; y < 5; y++)
    for (x = 0; x < 5; x++)
      {
	my_widgets.matrix[x][y] = entry = gtk_entry_new ();
	gtk_widget_set_size_request (entry, 40, -1);
	gtk_table_attach (GTK_TABLE (table), entry, x, x+1, y, y+1,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_widget_show (entry);

	g_signal_connect (entry, "changed",
                          G_CALLBACK (entry_callback),
                          &my_config.matrix[x][y]);
      }

  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), 6);
  gtk_box_pack_start (GTK_BOX (box), table, TRUE, FALSE, 0);

  label = gtk_label_new_with_mnemonic (_("D_ivisor:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.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 ();
  gtk_widget_set_size_request (entry, 40, -1);
  gtk_table_attach_defaults (GTK_TABLE (table), entry, 1, 2, 0, 1);
  gtk_widget_show (entry);
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);

  g_signal_connect (entry, "changed",
                    G_CALLBACK (entry_callback),
                    &my_config.divisor);

  gtk_widget_show (table);

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

  label = gtk_label_new_with_mnemonic (_("O_ffset:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.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 ();
  gtk_widget_set_size_request (entry, 40, -1);
  gtk_table_attach_defaults (GTK_TABLE (table), entry, 1, 2, 0, 1);
  gtk_widget_show (entry);
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);

  g_signal_connect (entry, "changed",
                    G_CALLBACK (entry_callback),
                    &my_config.offset);

  gtk_widget_show (table);

  gtk_widget_show (box);

  gtk_widget_show (inbox);
  gtk_widget_show (frame);

  box = gtk_vbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (vbox), box, FALSE, FALSE, 0);

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

  g_signal_connect (button, "toggled",
                    G_CALLBACK (my_toggle_callback),
                    &my_config.autoset);

  my_widgets.alpha_alg = button =
    gtk_check_button_new_with_mnemonic (_("A_lpha-weighting"));
  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);

  g_signal_connect (button, "toggled",
                    G_CALLBACK (my_toggle_callback),
                    &my_config.alpha_alg);

  gtk_widget_show (box);
  gtk_widget_show (vbox);

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

  frame = gimp_frame_new (_("Border"));
  gtk_box_pack_start (GTK_BOX (inbox), frame, FALSE, FALSE, 0);

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

  group = NULL;

  for (i = 0; i < 3; i++)
    {
      my_widgets.bmode[i] = button =
	gtk_radio_button_new_with_mnemonic (group, gettext (bmode_labels[i]));
      group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));
      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
      gtk_widget_show (button);

      g_signal_connect (button, "toggled",
                        G_CALLBACK (my_bmode_callback),
                        GINT_TO_POINTER (i + 1));
    }

  gtk_widget_show (box);
  gtk_widget_show (frame);

  frame = gimp_frame_new (_("Channels"));
  gtk_box_pack_start (GTK_BOX (inbox), frame, FALSE, FALSE, 0);

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

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

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

      gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
      gtk_widget_show (button);

      g_signal_connect (button, "toggled",
                        G_CALLBACK (my_toggle_callback),
                        &my_config.channels[i]);
    }

  gtk_widget_show (box);
  gtk_widget_show (frame);

  gtk_widget_show (inbox);

  gtk_widget_show (main_hbox);

  gtk_widget_show (dlg);
  redraw_all ();

  gtk_widget_set_sensitive (my_widgets.bmode[CLEAR],
			    (my_config.alpha_alg > 0));

  gtk_main ();

  return run_flag;
}