~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
/* Ripple --- image filter plug-in for The Gimp image manipulation program
 * Copyright (C) 1997 Brian Degenhardt
 *
 * 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.
 *
 * Please direct all comments, questions, bug reports  etc to Brian Degenhardt
 * bdegenha@ucsd.edu
 *
 * You can contact the original The Gimp authors at gimp@xcf.berkeley.edu
 */
#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#include <gtk/gtk.h>

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

#include "libgimp/stdplugins-intl.h"


/* Some useful macros */
#define SCALE_WIDTH     200
#define TILE_CACHE_SIZE  16

#define HORIZONTAL 0
#define VERTICAL   1

#define SMEAR 0
#define WRAP  1
#define BLACK 2

#define SAWTOOTH 0
#define SINE     1

typedef struct
{
  gint     period;
  gint     amplitude;
  gint     orientation;
  gint     edges;
  gint     waveform;
  gboolean antialias;
  gboolean tile;
  gboolean preview;
} RippleValues;


/* 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 void      ripple              (GimpDrawable *drawable,
                                      GimpPreview  *preview);

static gboolean  ripple_dialog       (GimpDrawable *drawable);

static gdouble   displace_amount     (gint      location);
static void      average_two_pixels  (guchar   *dest,
                                      guchar    pixels[4][4],
                                      gdouble   x,
                                      gint      bpp,
                                      gboolean  has_alpha);
static void      average_four_pixels (guchar   *dest,
                                      guchar    pixels[4][4],
                                      gdouble   x,
                                      gint      bpp,
                                      gboolean  has_alpha);

/***** Local vars *****/

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

static RippleValues rvals =
{
  20,         /* period      */
  5,          /* amplitude   */
  HORIZONTAL, /* orientation */
  WRAP,       /* edges       */
  SINE,       /* waveform    */
  TRUE,       /* antialias   */
  FALSE,      /* tile        */
  TRUE        /* preview     */
};

/***** Functions *****/

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,    "period",      "period; number of pixels for one wave to complete" },
    { GIMP_PDB_INT32,    "amplitude",   "amplitude; maximum displacement of wave" },
    { GIMP_PDB_INT32,    "orientation", "orientation; 0 = Horizontal, 1 = Vertical" },
    { GIMP_PDB_INT32,    "edges",       "edges; 0 = smear, 1 =  wrap, 2 = black" },
    { GIMP_PDB_INT32,    "waveform",    "0 = sawtooth, 1 = sine wave" },
    { GIMP_PDB_INT32,    "antialias",   "antialias; True or False" },
    { GIMP_PDB_INT32,    "tile",        "tile; if this is true, the image will retain it's tilability" }
  };

  gimp_install_procedure ("plug_in_ripple",
                          "Ripple the contents of the specified drawable",
                          "Ripples the pixels of the specified drawable. Each row or column will be displaced a certain number of pixels coinciding with the given wave form",
                          "Brian Degenhardt <bdegenha@ucsd.edu>",
                          "Brian Degenhardt",
                          "1997",
                          N_("_Ripple..."),
                          "RGB*, GRAY*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (args), 0,
                          args, NULL);

  gimp_plugin_menu_register ("plug_in_ripple", "<Image>/Filters/Distorts");
}

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

  run_mode = param[0].data.d_int32;

  INIT_I18N ();

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

  /*  set the tile cache size  */
  gimp_tile_cache_ntiles (TILE_CACHE_SIZE);

  *nreturn_vals = 1;
  *return_vals  = values;

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

  switch (run_mode)
    {
    case GIMP_RUN_INTERACTIVE:
      /*  Possibly retrieve data  */
      gimp_get_data ("plug_in_ripple", &rvals);

      /*  First acquire information with a dialog  */
      if (! ripple_dialog (drawable))
        return;
      break;

    case GIMP_RUN_NONINTERACTIVE:
      /*  Make sure all the arguments are there!  */
      if (nparams != 10)
        {
          status = GIMP_PDB_CALLING_ERROR;
        }
      else
        {
          rvals.period = param[3].data.d_int32;
          rvals.amplitude = param[4].data.d_int32;
          rvals.orientation = (param[5].data.d_int32) ? VERTICAL : HORIZONTAL;
          rvals.edges = (param[6].data.d_int32);
          rvals.waveform = param[7].data.d_int32;
          rvals.antialias = (param[8].data.d_int32) ? TRUE : FALSE;
          rvals.tile = (param[9].data.d_int32) ? TRUE : FALSE;

          if (rvals.edges < SMEAR || rvals.edges > BLACK)
            status = GIMP_PDB_CALLING_ERROR;
        }
      break;

    case GIMP_RUN_WITH_LAST_VALS:
      /*  Possibly retrieve data  */
      gimp_get_data ("plug_in_ripple", &rvals);
      break;

    default:
      break;
    }

  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 (_("Rippling..."));

          /*  run the ripple effect  */
          ripple (drawable, NULL);

          if (run_mode != GIMP_RUN_NONINTERACTIVE)
            gimp_displays_flush ();

          /*  Store data  */
          if (run_mode == GIMP_RUN_INTERACTIVE)
            gimp_set_data ("plug_in_ripple", &rvals, sizeof (RippleValues));
        }
      else
        {
          /* gimp_message ("ripple: cannot operate on indexed color images"); */
          status = GIMP_PDB_EXECUTION_ERROR;
        }
    }

  values[0].data.d_status = status;

  gimp_drawable_detach (drawable);
}

typedef struct {
  GimpPixelFetcher      *pft;
  gint                   width;
  gint                   height;
  gboolean               has_alpha;
} RippleParam_t;

static void
ripple_vertical (gint x,
                 gint y,
                 guchar *dest,
                 gint bpp,
                 gpointer data)
{
  RippleParam_t *param = (RippleParam_t*) data;
  GimpPixelFetcher *pft;
  guchar   pixel[4][4];
  gdouble  needy;
  gint     yi, height;

  pft = param->pft;
  height = param->height;

  needy = y + displace_amount(x);
  yi = floor(needy);

  /* Tile the image. */
  if (rvals.edges == WRAP)
    {
      needy = fmod(needy + height, height);
      yi = (yi + height) % height;
    }
  /* Smear out the edges of the image by repeating pixels. */
  else if (rvals.edges == SMEAR)
    {
      yi = CLAMP (yi, 0, height - 1);
    }

  if (rvals.antialias)
    {
      if (yi == height - 1)
        {
          gimp_pixel_fetcher_get_pixel (pft, x, yi, dest);
        }
      else if (needy < 0 && needy > -1)
        {
          gimp_pixel_fetcher_get_pixel (pft, x, 0, dest);
        }
      else if (yi == height - 2 || yi == 0)
        {
          gimp_pixel_fetcher_get_pixel (pft, x, yi, pixel[0]);
          gimp_pixel_fetcher_get_pixel (pft, x, yi + 1, pixel[1]);

          average_two_pixels (dest, pixel, needy, bpp, param->has_alpha);
        }
      else
        {
          gimp_pixel_fetcher_get_pixel (pft, x, yi, pixel[0]);
          gimp_pixel_fetcher_get_pixel (pft, x, yi + 1, pixel[1]);
          gimp_pixel_fetcher_get_pixel (pft, x, yi - 1, pixel[2]);
          gimp_pixel_fetcher_get_pixel (pft, x, yi + 2, pixel[3]);

          average_four_pixels (dest, pixel, needy, bpp, param->has_alpha);
        }
    } /* antialias */
  else
    {
      gimp_pixel_fetcher_get_pixel (pft, x, yi, dest);
    }
}

static void
ripple_horizontal (gint x,
                 gint y,
                 guchar *dest,
                 gint bpp,
                 gpointer data)
{
  RippleParam_t *param = (RippleParam_t*) data;
  GimpPixelFetcher *pft;
  guchar   pixel[4][4];
  gdouble  needx;
  gint     xi, width;

  pft = param->pft;
  width = param->width;

  needx = x + displace_amount(y);
  xi = floor (needx);

  /* Tile the image. */
  if (rvals.edges == WRAP)
    {
      needx = fmod((needx + width), width);
      xi = (xi + width) % width;
    }
  /* Smear out the edges of the image by repeating pixels. */
  else if (rvals.edges == SMEAR)
    {
      xi = CLAMP(xi, 0, width - 1);
    }

  if (rvals.antialias)
    {
      if (xi == width - 1)
        {
          gimp_pixel_fetcher_get_pixel (pft, xi, y, dest);
        }
      else if (floor(needx) ==  -1)
        {
          gimp_pixel_fetcher_get_pixel (pft, 0, y, dest);
        }

      else if (xi == width - 2 || xi == 0)
        {
          gimp_pixel_fetcher_get_pixel (pft, xi, y, pixel[0]);
          gimp_pixel_fetcher_get_pixel (pft, xi + 1, y, pixel[1]);

          average_two_pixels (dest, pixel, needx, bpp, param->has_alpha);
        }
      else
        {
          gimp_pixel_fetcher_get_pixel (pft, xi, y, pixel[0]);
          gimp_pixel_fetcher_get_pixel (pft, xi + 1, y, pixel[1]);
          gimp_pixel_fetcher_get_pixel (pft, xi - 1, y, pixel[2]);
          gimp_pixel_fetcher_get_pixel (pft, xi + 2, y, pixel[3]);

          average_four_pixels (dest, pixel, needx, bpp, param->has_alpha);
        }
    } /* antialias */

  else
    {
      gimp_pixel_fetcher_get_pixel (pft, xi, y, dest);
    }
}

static void
ripple (GimpDrawable *drawable,
        GimpPreview  *preview)
{
  RippleParam_t param;
  gint          edges;
  gint          period;

  param.pft       = gimp_pixel_fetcher_new (drawable, FALSE);
  param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
  param.width     = drawable->width;
  param.height    = drawable->height;

  edges  = rvals.edges;
  period = rvals.period;

  if (rvals.tile)
    {
      rvals.edges = WRAP;
      rvals.period = (param.width / (param.width / rvals.period) *
                      (rvals.orientation == HORIZONTAL) +
                      param.height / (param.height / rvals.period) *
                      (rvals.orientation == VERTICAL));
    }

  if (preview)
    {
      guchar *buffer, *d;
      gint    bpp = gimp_drawable_bpp (drawable->drawable_id);
      gint    width, height;
      gint    x, y;
      gint    x1, y1;

      gimp_preview_get_position (preview, &x1, &y1);
      gimp_preview_get_size (preview, &width, &height);

      d = buffer = g_new (guchar, width * height * bpp);

      for (y = 0; y < height ; y++)
        for (x = 0; x < width ; x++)
          {
             if (rvals.orientation == VERTICAL)
               ripple_vertical (x1 + x, y1 + y, d, bpp, &param);
             else
               ripple_horizontal (x1 + x, y1 + y, d, bpp, &param);
             d += bpp;
          }

      gimp_preview_draw_buffer (preview, buffer, width * bpp);
      g_free (buffer);
    }
  else
    {
      GimpRgnIterator *iter;

      iter = gimp_rgn_iterator_new (drawable, 0);
      gimp_rgn_iterator_dest (iter, (rvals.orientation == VERTICAL)
                              ? ripple_vertical : ripple_horizontal, &param);
      gimp_rgn_iterator_free (iter);
    }

  rvals.edges  = edges;
  rvals.period = period;

  gimp_pixel_fetcher_destroy (param.pft);
}

static gboolean
ripple_dialog (GimpDrawable *drawable)
{
  GtkWidget *dialog;
  GtkWidget *main_vbox;
  GtkWidget *preview;
  GtkWidget *toggle;
  GtkWidget *toggle_vbox;
  GtkWidget *frame;
  GtkWidget *table;
  GtkObject *scale_data;
  GtkWidget *horizontal;
  GtkWidget *vertical;
  GtkWidget *wrap;
  GtkWidget *smear;
  GtkWidget *black;
  GtkWidget *sawtooth;
  GtkWidget *sine;
  gboolean   run;

  gimp_ui_init ("ripple", TRUE);

  dialog = gimp_dialog_new (_("Ripple"), "ripple",
                            NULL, 0,
                            gimp_standard_help_func, "plug-in-ripple",

                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                            GTK_STOCK_OK,     GTK_RESPONSE_OK,

                            NULL);

  /*  The main vbox  */
  main_vbox = gtk_vbox_new (FALSE, 12);
  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
  gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
  gtk_widget_show (main_vbox);

  preview = gimp_drawable_preview_new (drawable, &rvals.preview);
  gtk_box_pack_start_defaults (GTK_BOX (main_vbox), preview);
  gtk_widget_show (preview);
  g_signal_connect_swapped (preview, "invalidated",
                            G_CALLBACK (ripple),
                            drawable);

  /* The table to hold the four frames of options */
  table = gtk_table_new (2, 2, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 12);
  gtk_table_set_row_spacings (GTK_TABLE (table), 12);
  gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);

  /*  Options section  */
  frame = gimp_frame_new ( _("Options"));
  gtk_table_attach (GTK_TABLE (table), frame, 0, 1, 0, 1,
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_widget_show (frame);

  toggle_vbox = gtk_vbox_new (FALSE, 6);
  gtk_container_add (GTK_CONTAINER (frame), toggle_vbox);
  gtk_widget_show (toggle_vbox);

  toggle = gtk_check_button_new_with_mnemonic (_("_Antialiasing"));
  gtk_box_pack_start (GTK_BOX (toggle_vbox), toggle, FALSE, FALSE, 0);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), rvals.antialias);
  gtk_widget_show (toggle);

  g_signal_connect (toggle, "toggled",
                    G_CALLBACK (gimp_toggle_button_update),
                    &rvals.antialias);
  g_signal_connect_swapped (toggle, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);

  toggle = gtk_check_button_new_with_mnemonic ( _("_Retain tilability"));
  gtk_box_pack_start (GTK_BOX (toggle_vbox), toggle, FALSE, FALSE, 0);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), (rvals.tile));
  gtk_widget_show (toggle);

  g_signal_connect (toggle, "toggled",
                    G_CALLBACK (gimp_toggle_button_update),
                    &rvals.tile);
  g_signal_connect_swapped (toggle, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);

  /*  Orientation toggle box  */
  frame = gimp_int_radio_group_new (TRUE, _("Orientation"),
                                    G_CALLBACK (gimp_radio_button_update),
                                    &rvals.orientation, rvals.orientation,

                                    _("_Horizontal"), HORIZONTAL, &horizontal,
                                    _("_Vertical"),   VERTICAL,   &vertical,

                                    NULL);

  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 0, 1,
                    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_widget_show (frame);

  g_signal_connect_swapped (horizontal, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);
  g_signal_connect_swapped (vertical, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);

  /*  Edges toggle box  */
  frame = gimp_int_radio_group_new (TRUE, _("Edges"),
                                    G_CALLBACK (gimp_radio_button_update),
                                    &rvals.edges, rvals.edges,

                                    _("_Wrap"),  WRAP,  &wrap,
                                    _("_Smear"), SMEAR, &smear,
                                    _("_Black"), BLACK, &black,

                                    NULL);

  gtk_table_attach (GTK_TABLE (table), frame, 0, 1, 1, 2,
                    GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);
  gtk_widget_show (frame);

  g_object_set_data (G_OBJECT (toggle), "inverse_sensitive", frame);
  gimp_toggle_button_sensitive_update (GTK_TOGGLE_BUTTON (toggle));

  g_signal_connect_swapped (wrap, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);
  g_signal_connect_swapped (smear, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);
  g_signal_connect_swapped (black, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);

  /*  Wave toggle box  */
  frame = gimp_int_radio_group_new (TRUE, _("Wave Type"),
                                    G_CALLBACK (gimp_radio_button_update),
                                    &rvals.waveform, rvals.waveform,

                                    _("Saw_tooth"), SAWTOOTH, &sawtooth,
                                    _("S_ine"),     SINE,     &sine,

                                    NULL);
  gtk_table_attach (GTK_TABLE (table), frame, 1, 2, 1, 2,
                    GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);
  gtk_widget_show (frame);

  g_signal_connect_swapped (sawtooth, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);
  g_signal_connect_swapped (sine, "toggled",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);

  gtk_widget_show (table);

  table = gtk_table_new (2, 3, FALSE);
  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
  gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);

  /*  Period  */
  scale_data = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
                                     _("_Period:"), SCALE_WIDTH, 0,
                                     rvals.period, 0, 200, 1, 10, 0,
                                     TRUE, 0, 0,
                                     NULL, NULL);
  g_signal_connect (scale_data, "value_changed",
                    G_CALLBACK (gimp_int_adjustment_update),
                    &rvals.period);
  g_signal_connect_swapped (scale_data, "value_changed",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);

  /*  Amplitude  */
  scale_data = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
                                     _("A_mplitude:"), SCALE_WIDTH, 0,
                                     rvals.amplitude, 0, 200, 1, 10, 0,
                                     TRUE, 0, 0,
                                     NULL, NULL);
  g_signal_connect (scale_data, "value_changed",
                    G_CALLBACK (gimp_int_adjustment_update),
                    &rvals.amplitude);
  g_signal_connect_swapped (scale_data, "value_changed",
                            G_CALLBACK (gimp_preview_invalidate),
                            preview);

  gtk_widget_show (frame);
  gtk_widget_show (table);
  gtk_widget_show (dialog);

  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);

  gtk_widget_destroy (dialog);

  return run;
}

static void
average_two_pixels (guchar   *dest,
                    guchar    pixels[4][4],
                    gdouble   x,
                    gint      bpp,
                    gboolean  has_alpha)
{
  gint b;

  x = fmod (x, 1.0);

  if (has_alpha)
    {
      gdouble xa0 = pixels[0][bpp-1] * (1.0 - x);
      gdouble xa1 = pixels[1][bpp-1] * x;
      gdouble alpha;

      alpha = xa0 + xa1;

      if (alpha)
        for (b = 0; b < bpp-1; b++)
          dest[b] = (xa0 * pixels[0][b] + xa1 * pixels[1][b]) / alpha;

      dest[bpp-1] = alpha;
    }
  else
    {
      for (b = 0; b < bpp; b++)
        dest[b] = (1.0 - x) * pixels[0][b] + x * pixels[1][b];
    }
}

static void
average_four_pixels (guchar   *dest,
                     guchar    pixels[4][4],
                     gdouble   x,
                     gint      bpp,
                     gboolean  has_alpha)
{
  gint b;

  x = fmod (x, 1.0);

  if (has_alpha)
    {
      gdouble xa0 = pixels[0][bpp-1] * (1.0 - x) / 2;
      gdouble xa1 = pixels[1][bpp-1] * x / 2;
      gdouble xa2 = pixels[2][bpp-1] * (1.0 - x) / 2;
      gdouble xa3 = pixels[3][bpp-1] * x / 2;
      gdouble alpha;

      alpha = xa0 + xa1 + xa2 + xa3;

      if (alpha)
        for (b = 0; b < bpp-1; b++)
          dest[b] = (xa0 * pixels[0][b] +
                     xa1 * pixels[1][b] +
                     xa2 * pixels[2][b] +
                     xa3 * pixels[3][b]) / alpha;

      dest[bpp-1] = alpha;
    }
  else
    {
      for (b = 0; b < bpp; b++)
        dest[b] = ((1.0 - x) * (pixels[0][b] + pixels[2][b]) / 2 +
                   x         * (pixels[1][b] + pixels[3][b]) / 2);
    }
}

static gdouble
displace_amount (gint location)
{
  switch (rvals.waveform)
    {
    case SINE:
      return (rvals.amplitude *
              sin (location * (2 * G_PI) / (gdouble) rvals.period));
    case SAWTOOTH:
      return floor (rvals.amplitude *
                    (fabs ((((location % rvals.period) /
                             (gdouble) rvals.period) * 4) - 2) - 1));
    }

  return 0.0;
}