home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / common / warp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-28  |  57.9 KB  |  1,742 lines

  1. /* Warp  --- image filter plug-in for The Gimp image manipulation program
  2.  * Copyright (C) 1997 John P. Beale
  3.  * Much of the 'warp' is from the Displace plug-in: 1996 Stephen Robert Norris
  4.  * Much of the 'displace' code taken in turn  from the pinch plug-in 
  5.  *   which is by 1996 Federico Mena Quintero
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * You can contact me (the warp author) at beale@best.com
  22.  * Please send me any patches or enhancements to this code.
  23.  * You can contact the original The Gimp authors at gimp@xcf.berkeley.edu
  24.  *
  25.  * --------------------------------------------------------------------
  26.  * Warp Program structure: after running the user interface and setting the
  27.  * parameters, warp generates a brand-new image (later to be deleted 
  28.  * before the user ever sees it) which contains two grayscale layers,
  29.  * representing the X and Y gradients of the "control" image. For this
  30.  * purpose, all channels of the control image are summed for a scalar
  31.  * value at each pixel coordinate for the gradient operation.
  32.  *
  33.  * The X,Y components of the calculated gradient are then used to displace pixels
  34.  * from the source image into the destination image. The displacement vector is
  35.  * rotated a user-specified amount first. This displacement operation happens
  36.  * iteratively, generating a new displaced image from each prior image. 
  37.  * -------------------------------------------------------------------
  38.  *
  39.  * Revision History:
  40.  * Version 0.37  12/19/98 Fixed Tooltips and freeing memory
  41.  * Version 0.36  11/9/97  Changed XY vector layers  back to own image
  42.  *               fixed 'undo' problem (hopefully)
  43.  *
  44.  * Version 0.35  11/3/97  Added vector-map, mag-map, grad-map to
  45.  *               diff vector instead of separate operation
  46.  *               further futzing with drawable updates
  47.  *               starting adding tooltips
  48.  *
  49.  * Version 0.34  10/30/97   'Fixed' drawable update problem
  50.  *               Added 16-bit resolution to differential map
  51.  *             Added substep increments for finer control
  52.  *
  53.  * Version 0.33  10/26/97   Added 'angle increment' to user interface
  54.  *
  55.  * Version 0.32  10/25/97   Added magnitude control map (secondary control)
  56.  *               Changed undo behavior to be one undo-step per warp call.
  57.  *
  58.  * Version 0.31  10/25/97   Fixed src/dest pixregions so program works
  59.  *               with multiple-layer images. Still don't know
  60.  *               exactly what I did to fix it :-/  Also, added 'color' option for
  61.  *               border pixels to use the current selected foreground color.
  62.  *
  63.  * Version 0.3   10/20/97  Initial release for Gimp 0.99.xx
  64.  */
  65.  
  66. #include "config.h"
  67.  
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include <unistd.h>
  71. #include <signal.h>
  72. #include <time.h>                  /* time(NULL) for random # seed */
  73.  
  74. #include <gtk/gtk.h>
  75.  
  76. #include <libgimp/gimp.h>
  77. #include <libgimp/gimpui.h>
  78.  
  79. #include "libgimp/stdplugins-intl.h"
  80.  
  81.  
  82. /* Some useful macros */
  83.  
  84. #define ENTRY_WIDTH     75
  85. #define TILE_CACHE_SIZE 30  /* was 48. There is a cache flush problem in GIMP preventing sequential updates */
  86. #define MIN_ARGS         6  /* minimum number of arguments required */
  87.  
  88. enum
  89. {
  90.   WRAP,
  91.   SMEAR,
  92.   BLACK,
  93.   COLOR
  94. };
  95.  
  96. typedef struct
  97. {
  98.   gdouble amount;
  99.   gint    warp_map;
  100.   gint    iter;
  101.   gdouble dither;
  102.   gdouble angle;
  103.   gint    wrap_type;
  104.   gint    mag_map;
  105.   gint    mag_use;
  106.   gint    substeps;
  107.   gint    grad_map;
  108.   gdouble grad_scale;
  109.   gint    vector_map;
  110.   gdouble vector_scale;
  111.   gdouble vector_angle;
  112. } WarpVals;
  113.  
  114. typedef struct
  115. {
  116.   gint run;
  117. } WarpInterface;
  118.  
  119. /*
  120.  * Function prototypes.
  121.  */
  122.  
  123. static void      query  (void);
  124. static void      run    (gchar    *name,
  125.              gint      nparams,
  126.              GimpParam   *param,
  127.              gint     *nreturn_vals,
  128.              GimpParam  **return_vals);
  129.  
  130. static void      blur16           (GimpDrawable *drawable);
  131.  
  132. static void      diff             (GimpDrawable *drawable, 
  133.                    gint32    *xl_id, 
  134.                    gint32    *yl_id);
  135.  
  136. static void      diff_prepare_row (GimpPixelRgn  *pixel_rgn,
  137.                    guchar     *data,
  138.                    int         x,
  139.                    int         y,
  140.                    int         w);
  141.  
  142. static void      warp_one         (GimpDrawable *draw, 
  143.                    GimpDrawable *new,
  144.                    GimpDrawable *map_x, 
  145.                    GimpDrawable *map_y,
  146.                    GimpDrawable *mag_draw,
  147.                    gint       first_time,
  148.                    gint       step);
  149.  
  150. static void      warp        (GimpDrawable  *drawable,
  151.                   GimpDrawable **map_x_p,
  152.                   GimpDrawable **map_y_p);
  153.  
  154. static gint      warp_dialog (GimpDrawable *drawable);
  155. static GimpTile *   warp_pixel  (GimpDrawable *drawable,
  156.                   GimpTile     *tile,
  157.                   gint       width,
  158.                   gint       height,
  159.                   gint       x1,
  160.                   gint       y1,
  161.                   gint       x2,
  162.                   gint       y2,
  163.                   gint       x,
  164.                   gint       y,
  165.                   gint      *row,
  166.                   gint      *col,
  167.                   guchar    *pixel);
  168.  
  169. static guchar    bilinear        (gdouble  x,
  170.                   gdouble  y,
  171.                   guchar  *v);
  172.  
  173. static gint      bilinear16      (gdouble  x,
  174.                   gdouble  y,
  175.                   gint    *v);
  176.  
  177. static gint      warp_map_constrain       (gint32     image_id,
  178.                        gint32     drawable_id,
  179.                        gpointer   data);
  180. static void      warp_map_callback        (gint32     id,
  181.                        gpointer   data);
  182. static void      warp_map_mag_callback    (gint32     id,
  183.                        gpointer   data);
  184. static void      warp_map_grad_callback   (gint32     id,
  185.                        gpointer   data);
  186. static void      warp_map_vector_callback (gint32     id,
  187.                        gpointer   data);
  188. static void      warp_ok_callback         (GtkWidget *widget,
  189.                        gpointer   data);
  190.  
  191. static gdouble   warp_map_mag_give_value  (guchar    *pt, 
  192.                        gint       alpha, 
  193.                        gint       bytes);
  194.  
  195. /* -------------------------------------------------------------------------- */
  196. /*   Variables global over entire plug-in scope                               */
  197. /* -------------------------------------------------------------------------- */
  198.  
  199. GimpPlugInInfo PLUG_IN_INFO =
  200. {
  201.   NULL,  /* init_proc  */
  202.   NULL,  /* quit_proc  */
  203.   query, /* query_proc */
  204.   run,   /* run_proc   */
  205. };
  206.  
  207. static WarpVals dvals =
  208. {
  209.   10.0,   /* amount       */
  210.   -1,     /* warp_map     */
  211.   5,      /* iterations   */
  212.   0.0,    /* dither       */
  213.   90.0,   /* angle        */
  214.   WRAP,   /* wrap_type    */
  215.   -1,     /* mag_map      */
  216.   FALSE,  /* mag_use      */
  217.   1,      /* substeps     */
  218.   -1,     /* grad_map     */
  219.   0.0,    /* grad_scale   */
  220.   -1,     /* vector_map   */
  221.   0.0,    /* vector_scale */
  222.   0.0     /* vector_angle */
  223. };
  224.  
  225. static WarpInterface dint =
  226. {
  227.   FALSE,  /* run */
  228. };
  229.  
  230. /* -------------------------------------------------------------------------- */
  231.  
  232. /* static gint         display_diff_map = TRUE;   show 16-bit diff. vectormap */
  233. static gint         progress = 0;              /* progress indicator bar      */
  234. static guint        tile_width, tile_height;   /* size of an image tile       */
  235. static GimpRunModeType run_mode;                  /* interactive, non-, etc.     */
  236. static guchar       color_pixel[4] = {0, 0, 0, 255};  /* current fg color     */
  237.  
  238. /* -------------------------------------------------------------------------- */
  239.  
  240. /***** Functions *****/
  241.  
  242. MAIN ()
  243.  
  244. static void
  245. query (void)
  246. {
  247.   static GimpParamDef args[] =
  248.   {
  249.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  250.     { GIMP_PDB_IMAGE, "image", "Input image (unused)" },
  251.     { GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
  252.     { GIMP_PDB_FLOAT, "amount", "Pixel displacement multiplier" },
  253.     { GIMP_PDB_DRAWABLE, "warp_map", "Displacement control map" },
  254.     { GIMP_PDB_INT32, "iter", "Iteration count (last required argument)" },
  255.     { GIMP_PDB_FLOAT, "dither", "Random dither amount (first optional argument)" },
  256.     { GIMP_PDB_FLOAT, "angle", "Angle of gradient vector rotation" },
  257.     { GIMP_PDB_INT32, "wrap_type", "Edge behavior: { WRAP (0), SMEAR (1), BLACK (2), COLOR (3) }" },
  258.     { GIMP_PDB_DRAWABLE, "mag_map", "Magnitude control map" },
  259.     { GIMP_PDB_INT32, "mag_use", "Use magnitude map: { FALSE (0), TRUE (1) }" },
  260.     { GIMP_PDB_INT32, "substeps", "Substeps between image updates" },
  261.     { GIMP_PDB_INT32, "grad_map", "Gradient control map" },
  262.     { GIMP_PDB_FLOAT, "grad_scale", "Scaling factor for gradient map (0=don't use)" },
  263.     { GIMP_PDB_INT32, "vector_map", "Fixed vector control map" },
  264.     { GIMP_PDB_FLOAT, "vector_scale", "Scaling factor for fixed vector map (0=don't use)" },
  265.     { GIMP_PDB_FLOAT, "vector_angle", "Angle for fixed vector map" }
  266.   };
  267.   static gint nargs = sizeof (args) / sizeof (args[0]);
  268.  
  269.   gimp_install_procedure ("plug_in_warp",
  270.               "Twist or smear an image. (only first six arguments are required)",
  271.               "Smears an image along vector paths calculated as "
  272.               "the gradient of a separate control matrix. The "
  273.               "effect can look like brushstrokes of acrylic or "
  274.               "watercolor paint, in some cases.",
  275.               "John P. Beale",
  276.               "John P. Beale",
  277.               "1997",
  278.               N_("<Image>/Filters/Map/Warp..."),
  279.               "RGB*, GRAY*",
  280.               GIMP_PLUGIN,
  281.               nargs, 0,
  282.               args, NULL);
  283. }
  284.  
  285. static void
  286. run (gchar  *name,
  287.      gint    nparams,
  288.      GimpParam  *param,
  289.      gint   *nreturn_vals,
  290.      GimpParam **return_vals)
  291. {
  292.   static GimpParam values[1];
  293.   GimpDrawable *drawable;
  294.   GimpDrawable *map_x = NULL;   /* satisfy compiler complaints */
  295.   GimpDrawable *map_y = NULL;
  296.   gint32 image_ID;           /* image id of drawable */
  297.  
  298.   GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  299.   gint pcnt;                 /* parameter counter for scanning input params. */
  300.  
  301.   run_mode = param[0].data.d_int32;
  302.  
  303.   tile_width = gimp_tile_width();    /* initialize some globals */
  304.   tile_height = gimp_tile_height();
  305.  
  306.   /* get currently selected foreground pixel color */
  307.   gimp_palette_get_foreground (&color_pixel[0],
  308.                    &color_pixel[1],
  309.                    &color_pixel[2]);
  310.  
  311.   /*  Get the specified drawable  */
  312.   drawable = gimp_drawable_get (param[2].data.d_drawable);
  313.  
  314.   *nreturn_vals = 1;
  315.   *return_vals  = values;
  316.   values[0].type          = GIMP_PDB_STATUS;
  317.   values[0].data.d_status = status;
  318.  
  319.   switch (run_mode)
  320.     {
  321.     case GIMP_RUN_INTERACTIVE:
  322.       INIT_I18N_UI();
  323.       /*  Possibly retrieve data  */
  324.       gimp_get_data ("plug_in_warp", &dvals);
  325.  
  326.       /*  First acquire information with a dialog  */
  327.       if (! warp_dialog (drawable))
  328.     return;
  329.       break;
  330.  
  331.     case GIMP_RUN_NONINTERACTIVE:
  332.       INIT_I18N();
  333.       /*  Make sure minimum args
  334.        *  (mode, image, draw, amount, warp_map, iter) are there 
  335.        */
  336.       if (nparams < MIN_ARGS)
  337.     {
  338.       status = GIMP_PDB_CALLING_ERROR;
  339.     }
  340.       else
  341.     {
  342.       pcnt = MIN_ARGS;                          /* parameter counter */
  343.       dvals.amount   = param[3].data.d_float;
  344.       dvals.warp_map = param[4].data.d_int32;
  345.       dvals.iter     = param[5].data.d_int32;
  346.       if (nparams > pcnt++) dvals.dither       = param[6].data.d_float;
  347.       if (nparams > pcnt++) dvals.angle        = param[7].data.d_float;
  348.       if (nparams > pcnt++) dvals.wrap_type    = param[8].data.d_int32;
  349.       if (nparams > pcnt++) dvals.mag_map      = param[9].data.d_int32;
  350.       if (nparams > pcnt++) dvals.mag_use      = param[10].data.d_int32;
  351.       if (nparams > pcnt++) dvals.substeps     = param[11].data.d_int32;
  352.       if (nparams > pcnt++) dvals.grad_map     = param[12].data.d_int32;
  353.       if (nparams > pcnt++) dvals.grad_scale   = param[13].data.d_float;
  354.       if (nparams > pcnt++) dvals.vector_map   = param[14].data.d_int32;
  355.       if (nparams > pcnt++) dvals.vector_scale = param[15].data.d_float;
  356.       if (nparams > pcnt++) dvals.vector_angle = param[16].data.d_float;
  357.     }
  358.       break;
  359.  
  360.     case GIMP_RUN_WITH_LAST_VALS:
  361.       /*  Possibly retrieve data  */
  362.       gimp_get_data ("plug_in_warp", &dvals);
  363.       break;
  364.  
  365.     default:
  366.       break;
  367.     }
  368.  
  369.   if (status == GIMP_PDB_SUCCESS)
  370.     {
  371.       /*  set the tile cache size  */
  372.       gimp_tile_cache_ntiles (TILE_CACHE_SIZE);
  373.  
  374.       /*  run the warp effect  */
  375.       warp (drawable, &map_x, &map_y);
  376.  
  377.       /*  Store data  */
  378.       if (run_mode == GIMP_RUN_INTERACTIVE)
  379.     gimp_set_data ("plug_in_warp", &dvals, sizeof (WarpVals));
  380.     }
  381.  
  382.   values[0].data.d_status = status;
  383.  
  384.   image_ID = gimp_layer_get_image_id(map_x->id);
  385.   gimp_image_delete(image_ID);
  386.   gimp_displays_flush();
  387.  
  388.   /*
  389.   if (display_diff_map == FALSE) {
  390.     gimp_layer_delete(map_x->id);
  391.     gimp_layer_delete(map_y->id);
  392.   } else {
  393.     image_ID = gimp_layer_get_image_id(drawable->id);
  394.     gimp_image_undo_disable(image_ID);
  395.     gimp_image_undo_enable(image_ID);
  396.   }
  397.   */
  398.  
  399.   gimp_drawable_detach (map_x);
  400.   gimp_drawable_detach (map_y);
  401.  
  402.   if (run_mode != GIMP_RUN_NONINTERACTIVE)
  403.     gimp_displays_flush ();
  404. }
  405.  
  406. static int
  407. warp_dialog (GimpDrawable *drawable)
  408. {
  409.   GtkWidget *dlg;
  410.   GtkWidget *vbox;
  411.   GtkWidget *label;
  412.   GtkWidget *toggle;
  413.   GtkWidget *toggle_hbox;
  414.   GtkWidget *frame;
  415.   GtkWidget *table;
  416.   GtkWidget *otable;
  417.   GtkWidget *spinbutton;
  418.   GtkObject *adj;
  419.   GtkWidget *option_menu;
  420.   GtkWidget *option_menu_mag;
  421.   GtkWidget *option_menu_grad;
  422.   GtkWidget *option_menu_vector;
  423.   GtkWidget *menu;
  424.   GtkWidget *magmenu;
  425.   GtkWidget *gradmenu;
  426.   GtkWidget *vectormenu;
  427.  
  428.   GSList  *group = NULL;
  429.  
  430.   gimp_ui_init ("warp", FALSE);
  431.  
  432.   dlg = gimp_dialog_new (_("Warp"), "warp",
  433.              gimp_standard_help_func, "filters/warp.html",
  434.              GTK_WIN_POS_MOUSE,
  435.              FALSE, TRUE, FALSE,
  436.  
  437.              _("OK"), warp_ok_callback,
  438.              NULL, NULL, NULL, TRUE, FALSE,
  439.              _("Cancel"), gtk_widget_destroy,
  440.              NULL, 1, NULL, FALSE, TRUE,
  441.  
  442.              NULL);
  443.  
  444.   gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
  445.               GTK_SIGNAL_FUNC (gtk_main_quit),
  446.               NULL);
  447.  
  448.   gimp_help_init ();
  449.  
  450.   vbox = gtk_vbox_new (FALSE, 4);
  451.   gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
  452.   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox, TRUE, TRUE, 0);
  453.   gtk_widget_show (vbox);
  454.  
  455.   frame = gtk_frame_new (_("Main Options"));
  456.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  457.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  458.  
  459.   table = gtk_table_new (3, 3, FALSE);
  460.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  461.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  462.   gtk_container_set_border_width (GTK_CONTAINER (table), 4);
  463.   gtk_container_add (GTK_CONTAINER (frame), table);
  464.  
  465.   /*  amount, iter */
  466.   spinbutton = gimp_spin_button_new (&adj, dvals.amount,
  467.                      -1000, 1000, /* ??? */
  468.                      1, 10, 0, 1, 2);
  469.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
  470.                  _("Step Size:"), 1.0, 0.5,
  471.                  spinbutton, 1, TRUE);
  472.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  473.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  474.               &dvals.amount);
  475.  
  476.   spinbutton = gimp_spin_button_new (&adj, dvals.iter,
  477.                      1, 100, 1, 5, 0, 1, 0);
  478.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
  479.                  _("Iterations:"), 1.0, 0.5,
  480.                  spinbutton, 1, TRUE);
  481.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  482.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  483.               &dvals.iter);
  484.  
  485.   /*  Displacement map menu  */
  486.   label = gtk_label_new (_("Displacement Map:"));
  487.   gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
  488.             GTK_FILL, GTK_FILL, 0, 0);
  489.   gtk_widget_show (label);
  490.  
  491.   option_menu = gtk_option_menu_new ();
  492.   gtk_table_attach (GTK_TABLE (table), option_menu, 2, 3, 1, 2,
  493.             GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  494.   menu = gimp_drawable_menu_new (warp_map_constrain, warp_map_callback,
  495.                  drawable, dvals.warp_map);
  496.   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
  497.   gtk_widget_show (option_menu);
  498.  
  499.   /* ======================================================================= */
  500.  
  501.   /*  Displacement Type  */
  502.   label = gtk_label_new (_("On Edges:"));
  503.   gtk_misc_set_alignment (GTK_MISC (label), 1.0, 0.5);
  504.   gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
  505.             GTK_FILL, GTK_FILL, 0, 0);
  506.   gtk_widget_show (label);
  507.  
  508.   toggle_hbox = gtk_hbox_new (FALSE, 4);
  509.   gtk_table_attach (GTK_TABLE (table), toggle_hbox, 1, 3, 2, 3,
  510.             GTK_FILL, GTK_FILL, 0, 0);
  511.  
  512.   toggle = gtk_radio_button_new_with_label (group, _("Wrap"));
  513.   group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  514.   gtk_box_pack_start (GTK_BOX (toggle_hbox), toggle, FALSE, FALSE, 0);
  515.   gtk_object_set_user_data (GTK_OBJECT (toggle), (gpointer) WRAP);
  516.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  517.               (GtkSignalFunc) gimp_radio_button_update,
  518.               &dvals.wrap_type);
  519.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  520.                 dvals.wrap_type == WRAP);
  521.   gtk_widget_show (toggle);
  522.  
  523.   toggle = gtk_radio_button_new_with_label (group, _("Smear"));
  524.   group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  525.   gtk_box_pack_start (GTK_BOX (toggle_hbox), toggle, FALSE, FALSE, 0);
  526.   gtk_object_set_user_data (GTK_OBJECT (toggle), (gpointer) SMEAR);
  527.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  528.               (GtkSignalFunc) gimp_radio_button_update,
  529.               &dvals.wrap_type);
  530.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  531.                 dvals.wrap_type == SMEAR);
  532.   gtk_widget_show (toggle);
  533.  
  534.   toggle = gtk_radio_button_new_with_label (group, _("Black"));
  535.   group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  536.   gtk_box_pack_start (GTK_BOX (toggle_hbox), toggle, FALSE, FALSE, 0);
  537.   gtk_object_set_user_data (GTK_OBJECT (toggle), (gpointer) BLACK);
  538.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  539.               (GtkSignalFunc) gimp_radio_button_update,
  540.               &dvals.wrap_type);
  541.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  542.                 dvals.wrap_type == BLACK);
  543.   gtk_widget_show (toggle);
  544.  
  545.   toggle = gtk_radio_button_new_with_label (group, _("FG Color"));
  546.   group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  547.   gtk_box_pack_start (GTK_BOX (toggle_hbox), toggle, FALSE, FALSE, 0);
  548.   gtk_object_set_user_data (GTK_OBJECT (toggle), (gpointer) COLOR);
  549.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  550.               (GtkSignalFunc) gimp_radio_button_update,
  551.               &dvals.wrap_type);
  552.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
  553.                 dvals.wrap_type == COLOR);
  554.   gtk_widget_show (toggle);
  555.  
  556.   gtk_widget_show (toggle_hbox);
  557.  
  558.   gtk_widget_show (table);
  559.   gtk_widget_show (frame);
  560.  
  561.   /* -------------------------------------------------------------------- */
  562.   /* ---------    The secondary table         --------------------------  */
  563.  
  564.   frame = gtk_frame_new (_("Secondary Options"));
  565.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  566.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  567.  
  568.   table = gtk_table_new (3, 3, FALSE);
  569.   gtk_table_set_row_spacings (GTK_TABLE (table), 2);
  570.   gtk_table_set_col_spacings (GTK_TABLE (table), 4);
  571.   gtk_container_set_border_width (GTK_CONTAINER (table), 4);
  572.   gtk_container_add (GTK_CONTAINER (frame), table);
  573.  
  574.   spinbutton = gimp_spin_button_new (&adj, dvals.dither,
  575.                      0, 100, 1, 10, 0, 1, 2);
  576.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 0,
  577.                  _("Dither Size:"), 1.0, 0.5,
  578.                  spinbutton, 1, TRUE);
  579.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  580.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  581.               &dvals.dither);
  582.  
  583.   spinbutton = gimp_spin_button_new (&adj, dvals.angle,
  584.                      0, 360, 1, 15, 0, 1, 1);
  585.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 1,
  586.                  _("Rotation Angle:"), 1.0, 0.5,
  587.                  spinbutton, 1, TRUE);
  588.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  589.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  590.               &dvals.angle);
  591.  
  592.   spinbutton = gimp_spin_button_new (&adj, dvals.substeps,
  593.                      1, 100, 1, 5, 0, 1, 0);
  594.   gimp_table_attach_aligned (GTK_TABLE (table), 0, 2,
  595.                  _("Substeps:"), 1.0, 0.5,
  596.                  spinbutton, 1, TRUE);
  597.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  598.               GTK_SIGNAL_FUNC (gimp_int_adjustment_update),
  599.               &dvals.substeps);
  600.  
  601.   /*  Magnitude map menu  */
  602.   label = gtk_label_new (_("Magnitude Map:"));
  603.   gtk_table_attach (GTK_TABLE (table), label, 2, 3, 0, 1,
  604.             GTK_FILL, GTK_FILL, 0, 0);
  605.   gtk_widget_show (label);
  606.  
  607.   option_menu_mag = gtk_option_menu_new ();
  608.   gtk_table_attach (GTK_TABLE (table), option_menu_mag, 2, 3, 1, 2,
  609.             GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  610.   magmenu = gimp_drawable_menu_new (warp_map_constrain, warp_map_mag_callback,
  611.                     drawable, dvals.mag_map);
  612.   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu_mag), magmenu);
  613.   gtk_widget_show (option_menu_mag);
  614.  
  615.   /*  Magnitude Usage  */
  616.   toggle_hbox = gtk_hbox_new (FALSE, 4);
  617.   gtk_container_set_border_width (GTK_CONTAINER (toggle_hbox), 1);
  618.   gtk_table_attach (GTK_TABLE (table), toggle_hbox, 2, 3, 2, 3,
  619.             GTK_FILL, GTK_FILL, 0, 0);
  620.  
  621.   toggle = gtk_check_button_new_with_label (_("Use Mag Map"));
  622.   gtk_box_pack_start (GTK_BOX (toggle_hbox), toggle, FALSE, FALSE, 0);
  623.   gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
  624.               (GtkSignalFunc) gimp_toggle_button_update,
  625.               &dvals.mag_use);
  626.   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), dvals.mag_use);
  627.   gtk_widget_show (toggle);
  628.  
  629.   gtk_widget_show (toggle_hbox);
  630.  
  631.   gtk_widget_show (table);
  632.   gtk_widget_show (frame);
  633.  
  634.   /* -------------------------------------------------------------------- */
  635.   /* ---------    The "other" table         --------------------------  */
  636.  
  637.   frame = gtk_frame_new (_("Other Options"));
  638.   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  639.   gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  640.  
  641.   otable = gtk_table_new (3, 3, FALSE);
  642.   gtk_table_set_row_spacings (GTK_TABLE (otable), 2);
  643.   gtk_table_set_col_spacings (GTK_TABLE (otable), 4);
  644.   gtk_container_set_border_width (GTK_CONTAINER (otable), 4);
  645.   gtk_container_add (GTK_CONTAINER (frame), otable);
  646.  
  647.   spinbutton = gimp_spin_button_new (&adj, dvals.grad_scale,
  648.                      -1000, 1000, /* ??? */
  649.                      0.01, 0.1, 0, 1, 3);
  650.   gimp_table_attach_aligned (GTK_TABLE (otable), 0, 0,
  651.                  _("Gradient Scale:"), 1.0, 0.5,
  652.                  spinbutton, 1, TRUE);
  653.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  654.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  655.               &dvals.grad_scale);
  656.  
  657.   /* ---------  Gradient map menu ----------------  */
  658.  
  659.   option_menu_grad = gtk_option_menu_new ();
  660.   gtk_table_attach (GTK_TABLE (otable), option_menu_grad, 2, 3, 0, 1,
  661.             GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  662.   gradmenu = gimp_drawable_menu_new (warp_map_constrain, warp_map_grad_callback,
  663.                  drawable, dvals.grad_map);
  664.   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu_grad), gradmenu);
  665.   gimp_help_set_help_data (option_menu_grad,
  666.                _("Gradient map selection menu"), NULL);
  667.  
  668.   gtk_widget_show (option_menu_grad);
  669.  
  670.   /* ---------------------------------------------- */
  671.  
  672.   spinbutton = gimp_spin_button_new (&adj, dvals.vector_scale,
  673.                      -1000, 1000, /* ??? */
  674.                      0.01, 0.1, 0, 1, 3);
  675.   gimp_table_attach_aligned (GTK_TABLE (otable), 0, 1,
  676.                  _("Vector Mag:"), 1.0, 0.5,
  677.                  spinbutton, 1, TRUE);
  678.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  679.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  680.               &dvals.vector_scale);
  681.  
  682.   /* -------------------------------------------------------- */
  683.  
  684.   spinbutton = gimp_spin_button_new (&adj, dvals.vector_angle,
  685.                      0, 360, 1, 15, 0, 1, 1);
  686.   gimp_table_attach_aligned (GTK_TABLE (otable), 0, 2,
  687.                  _("Angle:"), 1.0, 0.5,
  688.                  spinbutton, 1, TRUE);
  689.   gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
  690.               GTK_SIGNAL_FUNC (gimp_double_adjustment_update),
  691.               &dvals.vector_angle);
  692.  
  693.   /* ---------  Vector map menu ----------------  */
  694.   option_menu_vector = gtk_option_menu_new ();
  695.   gtk_table_attach (GTK_TABLE (otable), option_menu_vector, 2, 3, 1, 2,
  696.             GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  697.   vectormenu = gimp_drawable_menu_new (warp_map_constrain,
  698.                        warp_map_vector_callback,
  699.                        drawable, dvals.vector_map);
  700.   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu_vector), vectormenu);
  701.   gimp_help_set_help_data (option_menu_vector,
  702.                _("Fixed-direction-vector map selection menu"),
  703.                NULL);
  704.  
  705.   gtk_widget_show (option_menu_vector);
  706.  
  707.   gtk_widget_show (otable);
  708.   gtk_widget_show (frame);
  709.  
  710.   gtk_widget_show (dlg);
  711.  
  712.   gtk_main ();
  713.   gimp_help_free ();
  714.   gdk_flush ();
  715.  
  716.   return dint.run;
  717. }
  718. /* ---------------------------------------------------------------------- */
  719.  
  720. static void
  721. blur16 (GimpDrawable *drawable)
  722. {
  723.   /*  blur a 2-or-more byte-per-pixel drawable,
  724.    *  1st 2 bytes interpreted as a 16-bit height field.
  725.    */
  726.   GimpPixelRgn srcPR, destPR;
  727.   gint width, height;
  728.   gint src_bytes;
  729.   gint dest_bytes;
  730.   gint dest_bytes_inc;
  731.   gint offb, off1;
  732.  
  733.   guchar *dest, *d;  /* pointers to rows of X and Y diff. data */
  734.   guchar *prev_row, *pr;
  735.   guchar *cur_row, *cr;
  736.   guchar *next_row, *nr;
  737.   guchar *tmp;
  738.   gint row, col;  /* relating to indexing into pixel row arrays */
  739.   gint x1, y1, x2, y2;
  740.   gdouble pval;          /* average pixel value of pixel & neighbors */
  741.  
  742.   /* --------------------------------------- */
  743.  
  744.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  745.  
  746.   width = drawable->width;     /* size of input drawable*/
  747.   height = drawable->height;
  748.   src_bytes = drawable->bpp;   /* bytes per pixel in SOURCE drawable, must be 2 or more  */
  749.   dest_bytes = drawable->bpp;   /* bytes per pixel in SOURCE drawable, >= 2  */
  750.   dest_bytes_inc = dest_bytes - 2;  /* this is most likely zero, but I guess it's more conservative... */
  751.  
  752.   /*  allocate row buffers for source & dest. data  */
  753.  
  754.   prev_row = (guchar *) malloc ((x2 - x1 + 2) * src_bytes);
  755.   cur_row = (guchar *) malloc ((x2 - x1 + 2) * src_bytes);
  756.   next_row = (guchar *) malloc ((x2 - x1 + 2) * src_bytes);
  757.   dest = (guchar *) malloc ((x2 - x1) * src_bytes);
  758.  
  759.   /* initialize the pixel regions (read from source, write into dest)  */
  760.   gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);
  761.   gimp_pixel_rgn_init (&destPR, drawable, 0, 0, width, height, TRUE, TRUE);
  762.  
  763.   pr = prev_row + src_bytes;   /* row arrays are prepared for indexing to -1 (!) */
  764.   cr = cur_row + src_bytes;
  765.   nr = next_row + src_bytes;
  766.  
  767.   diff_prepare_row (&srcPR, pr, x1, y1, (x2 - x1));
  768.   diff_prepare_row (&srcPR, cr, x1, y1+1, (x2 - x1));
  769.  
  770.   /*  loop through the rows, applying the smoothing function  */
  771.   for (row = y1; row < y2; row++)
  772.     {
  773.       /*  prepare the next row  */
  774.       diff_prepare_row (&srcPR, nr, x1, row + 1, (x2 - x1));
  775.  
  776.       d = dest;
  777.       for (col = 0; col < (x2 - x1); col++) /* over columns of pixels */
  778.     {
  779.       offb = col*src_bytes;    /* base of byte pointer offset */
  780.       off1 = offb+1;                 /* offset into row arrays */
  781.  
  782.       pval = (256.0*pr[offb - src_bytes] + pr[off1 - src_bytes] +
  783.           256.0*pr[offb] + pr[off1] +
  784.           256.0*pr[offb + src_bytes] + pr[off1 + src_bytes] +
  785.           256.0*cr[offb - src_bytes] + cr[off1 - src_bytes] +
  786.           256.0*cr[offb]  + cr[off1] +
  787.           256.0*cr[offb + src_bytes] + cr[off1 + src_bytes] +
  788.           256.0*nr[offb - src_bytes] + nr[off1 - src_bytes] +
  789.           256.0*nr[offb] + nr[off1] +
  790.           256.0*nr[offb + src_bytes]) + nr[off1 + src_bytes];
  791.  
  792.       pval /= 9.0;  /* take the average */
  793.       *d++ = (guchar) (((gint)pval)>>8);   /* high-order byte */
  794.       *d++ = (guchar) (((gint)pval)%256);  /* low-order byte */
  795.       d += dest_bytes_inc;       /* move data pointer on to next destination pixel */
  796.          
  797.     }
  798.       /*  store the dest  */
  799.       gimp_pixel_rgn_set_row (&destPR, dest, x1, row, (x2 - x1));
  800.  
  801.       /*  shuffle the row pointers  */
  802.       tmp = pr;
  803.       pr = cr;
  804.       cr = nr;
  805.       nr = tmp;  
  806.  
  807.       if ((row % 5) == 0)
  808.     gimp_progress_update ((double) row / (double) (y2 - y1));
  809.     }
  810.  
  811.   /*  update the region  */
  812.   gimp_drawable_flush (drawable);
  813.   gimp_drawable_merge_shadow (drawable->id, TRUE);
  814.   gimp_drawable_update (drawable->id, x1, y1, (x2 - x1), (y2 - y1));
  815.  
  816.   free (prev_row);  /* row buffers allocated at top of fn. */
  817.   free (cur_row);
  818.   free (next_row);
  819.   free (dest);
  820.  
  821. } /* end blur16() */
  822.  
  823.  
  824. /* ====================================================================== */
  825. /* Get one row of pixels from the PixelRegion and put them in 'data'      */
  826.  
  827. static void
  828. diff_prepare_row (GimpPixelRgn *pixel_rgn,
  829.           guchar    *data,
  830.           int        x,
  831.           int        y,
  832.           int        w)
  833. {
  834.   int b;
  835.  
  836.   if (y == 0)    /* wrap around */
  837.     gimp_pixel_rgn_get_row (pixel_rgn, data, x, (pixel_rgn->h - 1), w);
  838.   else if (y == pixel_rgn->h)
  839.     gimp_pixel_rgn_get_row (pixel_rgn, data, x, 1, w);
  840.   else
  841.     gimp_pixel_rgn_get_row (pixel_rgn, data, x, y, w);
  842.  
  843.   /*  Fill in edge pixels  */
  844.   for (b = 0; b < pixel_rgn->bpp; b++)
  845.     {
  846.       data[-pixel_rgn->bpp + b] = data[b];
  847.       data[w * pixel_rgn->bpp + b] = data[(w - 1) * pixel_rgn->bpp + b];
  848.     }
  849. }
  850.  
  851. /* -------------------------------------------------------------------------- */
  852. /*  'diff' combines the input drawables to prepare the two                    */
  853. /*  16-bit (X,Y) vector displacement maps                                     */
  854. /* -------------------------------------------------------------------------- */
  855.  
  856. static void
  857. diff (GimpDrawable *drawable, 
  858.       gint32    *xl_id, 
  859.       gint32    *yl_id)
  860. {
  861.   GimpDrawable *draw_xd, *draw_yd;  /* vector disp. drawables */
  862.   GimpDrawable *mdraw, *vdraw, *gdraw;
  863.   gint32 image_id;           /* image holding X and Y diff. arrays */
  864.   gint32 new_image_id;       /* image holding X and Y diff. layers */
  865.   gint32 layer_active;       /* currently active layer */
  866.   gint32 xlayer_id, ylayer_id;   /* individual X and Y layer ID numbers */
  867.   GimpPixelRgn srcPR, destxPR, destyPR;
  868.   GimpPixelRgn vecPR, magPR, gradPR;
  869.   gint width, height;
  870.   gint src_bytes;
  871.   gint mbytes=0;
  872.   gint vbytes=0;
  873.   gint gbytes=0;   /* bytes-per-pixel of various source drawables */
  874.   gint dest_bytes;
  875.   gint dest_bytes_inc;
  876.   gint do_gradmap = FALSE;          /* whether to add in gradient of gradmap to final diff. map */
  877.   gint do_vecmap = FALSE;          /* whether to add in a fixed vector scaled by the vector map */
  878.   gint do_magmap = FALSE;          /* whether to multiply result by the magnitude map */
  879.  
  880.   guchar *destx, *dx, *desty, *dy;  /* pointers to rows of X and Y diff. data */
  881.   guchar *tmp;
  882.   guchar *prev_row, *pr;
  883.   guchar *cur_row, *cr;
  884.   guchar *next_row, *nr;
  885.   guchar *prev_row_g, *prg=NULL;          /* pointers to gradient map data */
  886.   guchar *cur_row_g, *crg=NULL;
  887.   guchar *next_row_g, *nrg=NULL;
  888.   guchar *cur_row_v, *crv=NULL;      /* pointers to vector map data */
  889.   guchar *cur_row_m, *crm=NULL;      /* pointers to magnitude map data */
  890.   gint row, col, offb, off, bytes;   /* relating to indexing into pixel row arrays */
  891.   gint x1, y1, x2, y2;
  892.   gint dvalx, dvaly;                  /* differential value at particular pixel */
  893.   gdouble tx, ty;                     /* temporary x,y differential value increments from gradmap, etc. */
  894.   gdouble rdx, rdy;                     /* x,y differential values: real #s */
  895.   gdouble rscalefac;                  /* scaling factor for x,y differential of 'curl' map */
  896.   gdouble gscalefac;                  /* scaling factor for x,y differential of 'gradient' map */
  897.   gdouble r, theta, dtheta;           /* rectangular<-> spherical coordinate transform for vector rotation */
  898.   gdouble scale_vec_x, scale_vec_y;   /* fixed vector X,Y component scaling factors */
  899.   gint has_alpha, ind;
  900.   
  901.   /* ----------------------------------------------------------------------- */
  902.  
  903.   if (dvals.grad_scale != 0.0)
  904.       do_gradmap = TRUE;    /* add in gradient of gradmap if scale != 0.000 */
  905.   if (dvals.vector_scale != 0.0)   /* add in gradient of vectormap if scale != 0.000 */
  906.       do_vecmap = TRUE;
  907.   do_magmap = (dvals.mag_use == TRUE);   /* multiply by magnitude map if so requested */
  908.  
  909.   /* Get the input area. This is the bounding box of the selection in
  910.    *  the image (or the entire image if there is no selection). Only
  911.    *  operating on the input area is simply an optimization. It doesn't
  912.    *  need to be done for correct operation. (It simply makes it go
  913.    *  faster, since fewer pixels need to be operated on).
  914.    */
  915.   gimp_drawable_mask_bounds (drawable->id, &x1, &y1, &x2, &y2);
  916.  
  917.   /* Get the size of the input image. (This will/must be the same
  918.    *  as the size of the output image.
  919.    */
  920.   width = drawable->width;
  921.   height = drawable->height;
  922.   src_bytes = drawable->bpp;                /* bytes per pixel in SOURCE drawable */
  923.   has_alpha = gimp_drawable_has_alpha(drawable->id);
  924.  
  925.   /* -- Add two layers: X and Y Displacement vectors -- */
  926.   /* -- I'm using a RGB  drawable and using the first two bytes for a 
  927.         16-bit pixel value. This is either clever, or a kluge, 
  928.         depending on your point of view.  */
  929.  
  930.   image_id = gimp_layer_get_image_id(drawable->id);
  931.   layer_active = gimp_image_get_active_layer(image_id);
  932.  
  933.   new_image_id = gimp_image_new(width, height, GIMP_RGB); /* create new image for X,Y diff */
  934.  
  935.   xlayer_id = gimp_layer_new(new_image_id, "Warp_X_Vectors",
  936.                  width, height,
  937.                  GIMP_RGB_IMAGE, 100.0, GIMP_NORMAL_MODE);
  938.  
  939.   ylayer_id = gimp_layer_new(new_image_id, "Warp_Y_Vectors",
  940.                  width, height,
  941.                  GIMP_RGB_IMAGE, 100.0, GIMP_NORMAL_MODE);
  942.  
  943.   draw_yd = gimp_drawable_get (ylayer_id);
  944.   draw_xd = gimp_drawable_get (xlayer_id);
  945.  
  946.     gimp_image_add_layer (new_image_id, xlayer_id, 1);
  947.     gimp_image_add_layer (new_image_id, ylayer_id, 1);
  948.     gimp_drawable_fill(xlayer_id, GIMP_BG_IMAGE_FILL);
  949.     gimp_drawable_fill(ylayer_id, GIMP_BG_IMAGE_FILL);
  950.     gimp_image_set_active_layer(image_id, layer_active);
  951.  
  952.   dest_bytes = draw_xd->bpp;                /* bytes per pixel in destination drawable(s) */
  953.   /* for a GRAYA drawable, I would expect this to be two bytes; any more would be excess */
  954.   dest_bytes_inc = dest_bytes - 2;
  955.  
  956.  
  957.   /*  allocate row buffers for source & dest. data  */
  958.  
  959.   prev_row = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  960.   cur_row  = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  961.   next_row = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  962.  
  963.   prev_row_g = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  964.   cur_row_g  = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  965.   next_row_g = g_new (guchar, (x2 - x1 + 2) * src_bytes);
  966.  
  967.   cur_row_v = g_new (guchar, (x2 - x1 + 2) * src_bytes);  /* vector map */
  968.   cur_row_m = g_new (guchar, (x2 - x1 + 2) * src_bytes);  /* magnitude map */
  969.  
  970.   destx = g_new (guchar, (x2 - x1) * dest_bytes);
  971.   desty = g_new (guchar, (x2 - x1) * dest_bytes);
  972.  
  973.   if ((desty==NULL) || (destx==NULL) || (cur_row_m==NULL) || (cur_row_v==NULL)
  974.     || (next_row_g==NULL) || (cur_row_g==NULL) || (prev_row_g==NULL)
  975.     || (next_row==NULL) || (cur_row==NULL) || (prev_row==NULL)) {
  976.    fprintf(stderr, "Warp diff: error allocating memory.\n");
  977.    exit(1);
  978.   }
  979.  
  980.   /*  initialize the source and destination pixel regions  */
  981.   gimp_pixel_rgn_init (&srcPR, drawable, 0, 0, width, height, FALSE, FALSE);  /* 'curl' vector-rotation input */
  982.   gimp_pixel_rgn_init (&destxPR, draw_xd, 0, 0, width, height, TRUE, FALSE);  /* destination: X diff output */
  983.   gimp_pixel_rgn_init (&destyPR, draw_yd, 0, 0, width, height, TRUE, FALSE);  /* Y diff output */
  984.  
  985.   pr = prev_row + src_bytes;
  986.   cr = cur_row + src_bytes;
  987.   nr = next_row + src_bytes;
  988.  
  989.   diff_prepare_row (&srcPR, pr, x1, y1, (x2 - x1));
  990.   diff_prepare_row (&srcPR, cr, x1, y1+1, (x2 - x1));
  991.  
  992.  /* fixed-vector (x,y) component scale factors */
  993.   scale_vec_x = dvals.vector_scale*cos((90-dvals.vector_angle)*G_PI/180.0)*256.0/10;
  994.   scale_vec_y = dvals.vector_scale*sin((90-dvals.vector_angle)*G_PI/180.0)*256.0/10;
  995.  
  996.   if (do_vecmap) {
  997.     /*    fprintf(stderr,"%f %f  x,y vector components.\n",scale_vec_x,scale_vec_y); */
  998.  
  999.     vdraw = gimp_drawable_get(dvals.vector_map);
  1000.     vbytes = vdraw->bpp;                /* bytes per pixel in SOURCE drawable */
  1001.     gimp_pixel_rgn_init (&vecPR, vdraw, 0, 0, width, height, FALSE, FALSE);          /* fixed-vector scale-map */
  1002.     crv = cur_row_v + vbytes;
  1003.     diff_prepare_row (&vecPR, crv, x1, y1, (x2 - x1));
  1004.   }
  1005.   if (do_gradmap) {
  1006.     gdraw = gimp_drawable_get(dvals.grad_map);
  1007.     gbytes = gdraw->bpp;
  1008.     gimp_pixel_rgn_init (&gradPR, gdraw, 0, 0, width, height, FALSE, FALSE);          /* fixed-vector scale-map */
  1009.     prg = prev_row_g + gbytes;
  1010.     crg = cur_row_g + gbytes;
  1011.     nrg = next_row_g + gbytes;
  1012.     diff_prepare_row (&gradPR, prg, x1, y1 - 1, (x2 - x1));
  1013.     diff_prepare_row (&gradPR, crg, x1, y1, (x2 - x1));
  1014.   }
  1015.   if (do_magmap) {
  1016.     mdraw = gimp_drawable_get(dvals.mag_map);
  1017.     mbytes = mdraw->bpp;
  1018.     gimp_pixel_rgn_init (&magPR, mdraw, 0, 0, width, height, FALSE, FALSE);          /* fixed-vector scale-map */
  1019.     crm = cur_row_m + mbytes;
  1020.     diff_prepare_row (&magPR, crm, x1, y1, (x2 - x1));
  1021.   }
  1022.  
  1023.   dtheta = dvals.angle * G_PI / 180.0;
  1024.   rscalefac = 256.0 / (3*src_bytes);         /* note that '3' is rather arbitrary here. */
  1025.   gscalefac = dvals.grad_scale* 256.0 / (3*gbytes);            /* scale factor for gradient map components */
  1026.  
  1027.   /*  loop through the rows, applying the differential convolution  */
  1028.   for (row = y1; row < y2; row++)
  1029.     {
  1030.       /*  prepare the next row  */
  1031.       diff_prepare_row (&srcPR, nr, x1, row + 1, (x2 - x1));
  1032.   
  1033.       if (do_magmap)
  1034.         diff_prepare_row (&magPR, crm, x1, row + 1, (x2 - x1));
  1035.       if (do_vecmap)
  1036.         diff_prepare_row (&vecPR, crv, x1, row + 1, (x2 - x1));
  1037.       if (do_gradmap)
  1038.         diff_prepare_row (&gradPR, crg, x1, row + 1, (x2 - x1));
  1039.  
  1040.       dx = destx;
  1041.       dy = desty;
  1042.       ind = 0;
  1043.  
  1044.       for (col = 0; col < (x2 - x1); col++) /* over columns of pixels */
  1045.       {
  1046.          rdx = 0.0;
  1047.          rdy = 0.0;
  1048.          ty = 0.0;
  1049.          tx = 0.0;
  1050.  
  1051.          offb = col*src_bytes;    /* base of byte pointer offset */
  1052.          for (bytes=0; bytes < src_bytes; bytes++) /* add all channels together */
  1053.            {
  1054.            off = offb+bytes;                 /* offset into row arrays */
  1055.           rdx += ((gint) -pr[off - src_bytes]   + (gint) pr[off + src_bytes] +
  1056.               (gint) -2*cr[off - src_bytes] + (gint) 2*cr[off + src_bytes] +
  1057.               (gint) -nr[off - src_bytes]   + (gint) nr[off + src_bytes]);
  1058.  
  1059.            rdy += ((gint) -pr[off - src_bytes] - (gint)2*pr[off] - (gint) pr[off + src_bytes] +
  1060.               (gint) nr[off - src_bytes] + (gint)2*nr[off] + (gint) nr[off + src_bytes]);
  1061.            }
  1062.  
  1063.          rdx *= rscalefac;   /* take average, then reduce. Assume max. rdx now 65535 */
  1064.          rdy *= rscalefac;   /* take average, then reduce */     
  1065.  
  1066.          theta = atan2(rdy,rdx);          /* convert to polar, then back to rectang. coords */
  1067.          r = sqrt(rdy*rdy + rdx*rdx);
  1068.          theta += dtheta;              /* rotate gradient vector by this angle (radians) */
  1069.          rdx = r * cos(theta);
  1070.          rdy = r * sin(theta);
  1071.  
  1072.              if (do_gradmap) {
  1073.            
  1074.            offb = col*gbytes;     /* base of byte pointer offset into pixel values (R,G,B,Alpha, etc.) */
  1075.              for (bytes=0; bytes < src_bytes; bytes++) /* add all channels together */
  1076.             {
  1077.            off = offb+bytes;                 /* offset into row arrays */
  1078.           tx += ((gint) -prg[off - gbytes]   + (gint) prg[off + gbytes] +
  1079.               (gint) -2*crg[off - gbytes] + (gint) 2*crg[off + gbytes] +
  1080.               (gint) -nrg[off - gbytes]   + (gint) nrg[off + gbytes]);
  1081.  
  1082.            ty += ((gint) -prg[off - gbytes] - (gint)2*prg[off] - (gint) prg[off + gbytes] +
  1083.               (gint) nrg[off - gbytes] + (gint)2*nrg[off] + (gint) nrg[off + gbytes]);
  1084.             }
  1085.            tx *= gscalefac;
  1086.            ty *= gscalefac;
  1087.  
  1088.            rdx += tx;         /* add gradient component in to the other one */
  1089.            rdy += ty;
  1090.  
  1091.              } /* if (do_gradmap) */
  1092.          
  1093.  
  1094.          if (do_vecmap) {  /* add in fixed vector scaled by  vec. map data */
  1095.            tx = (gdouble) crv[col*vbytes];       /* use first byte only */
  1096.            rdx += scale_vec_x * tx;
  1097.            rdy += scale_vec_y * tx;
  1098.  
  1099.          } /* if (do_vecmap) */
  1100.  
  1101.          if (do_magmap) {  /* multiply result by mag. map data */
  1102.            tx = (gdouble) crm[col*mbytes];
  1103.            rdx = (rdx * tx)/(255.0);
  1104.            rdy = (rdy * tx)/(255.0);
  1105.  
  1106.          } /* if do_magmap */
  1107.  
  1108.  
  1109.          dvalx = rdx + (2<<14);         /* take zero point to be 2^15, since this is two bytes */
  1110.          dvaly = rdy + (2<<14);
  1111.  
  1112.              if (dvalx<0) dvalx=0;
  1113.          if (dvalx>65535) dvalx=65535;
  1114.          *dx++ = (guchar) (dvalx >> 8);    /* store high order byte in value channel */
  1115.              *dx++ = (guchar) (dvalx % 256);   /* store low order byte in alpha channel */
  1116.          dx += dest_bytes_inc;       /* move data pointer on to next destination pixel */
  1117.          
  1118.          if (dvaly<0) dvaly=0;
  1119.          if (dvaly>65535) dvaly=65535;
  1120.          *dy++ = (guchar) (dvaly >> 8);
  1121.              *dy++ = (guchar) (dvaly % 256);
  1122.          dy += dest_bytes_inc;
  1123.  
  1124.       } /* ------------------------------- for (col...) ----------------  */
  1125.  
  1126.       /*  store the dest  */
  1127.       gimp_pixel_rgn_set_row (&destxPR, destx, x1, row, (x2 - x1));
  1128.       gimp_pixel_rgn_set_row (&destyPR, desty, x1, row, (x2 - x1));
  1129.  
  1130.       /*  swap around the pointers to row buffers  */
  1131.       tmp = pr;
  1132.       pr = cr;
  1133.       cr = nr;
  1134.       nr = tmp;
  1135.  
  1136.       if (do_gradmap) {
  1137.         tmp = prg;
  1138.         prg = crg;
  1139.         crg = nrg;
  1140.         nrg = tmp;
  1141.       }
  1142.  
  1143.       if ((row % 5) == 0)
  1144.     gimp_progress_update ((double) row / (double) (y2 - y1));
  1145.  
  1146.     } /* for (row..) */
  1147.  
  1148.   /*  update the region  */
  1149.   gimp_drawable_flush (draw_xd);
  1150.   gimp_drawable_flush (draw_yd);
  1151.  
  1152.   gimp_drawable_update (draw_xd->id, x1, y1, (x2 - x1), (y2 - y1));
  1153.   gimp_drawable_update (draw_yd->id, x1, y1, (x2 - x1), (y2 - y1));
  1154.  
  1155.   /*
  1156.   if (display_diff_map) {
  1157.     gimp_display_new(new_image_id);
  1158.   }
  1159.   */
  1160.  
  1161.   gimp_displays_flush();  /* make sure layer is visible */
  1162.  
  1163.   gimp_progress_init ( _("Smoothing X gradient..."));
  1164.   blur16(draw_xd); 
  1165.   gimp_progress_init ( _("Smoothing Y gradient..."));
  1166.   blur16(draw_yd);
  1167.  
  1168.   g_free (prev_row);  /* row buffers allocated at top of fn. */
  1169.   g_free (cur_row);
  1170.   g_free (next_row);
  1171.   g_free (prev_row_g);  /* row buffers allocated at top of fn. */
  1172.   g_free (cur_row_g);
  1173.   g_free (next_row_g);
  1174.   g_free (cur_row_v);
  1175.   g_free (cur_row_m);
  1176.  
  1177.   g_free (destx);
  1178.   g_free (desty);
  1179.  
  1180.   *xl_id = xlayer_id;  /* pass back the X and Y layer ID numbers */
  1181.   *yl_id = ylayer_id;
  1182.  
  1183. } /* end diff() */
  1184.  
  1185. /* -------------------------------------------------------------------------- */
  1186. /*            The Warp displacement is done here.                             */
  1187. /* -------------------------------------------------------------------------- */
  1188.  
  1189. static void      
  1190. warp (GimpDrawable  *orig_draw,
  1191.       GimpDrawable **map_x,
  1192.       GimpDrawable **map_y)
  1193. {
  1194.   GimpDrawable *disp_map;    /* Displacement map, ie, control array */
  1195.   GimpDrawable *mag_draw;    /* Magnitude multiplier factor map */
  1196.  
  1197.   gchar *string;          /* string to hold title of progress bar window */
  1198.  
  1199.   gint    first_time = TRUE;
  1200.   gint    width;
  1201.   gint    height;
  1202.   gint    bytes;
  1203.   gint orig_image_id;
  1204.   gint image_type;
  1205.  
  1206.  
  1207.   gint    x1, y1, x2, y2;
  1208.  
  1209.   gint32 xdlayer = -1;
  1210.   gint32 ydlayer = -1;
  1211.  
  1212.   gint warp_iter;      /* index var. over all "warp" Displacement iterations */
  1213.  
  1214.  
  1215.   disp_map = gimp_drawable_get(dvals.warp_map);
  1216.   mag_draw = gimp_drawable_get(dvals.mag_map);
  1217.  
  1218.   /* calculate new X,Y Displacement image maps */
  1219.  
  1220.   gimp_progress_init ( _("Finding XY gradient..."));
  1221.  
  1222.   diff(disp_map, &xdlayer, &ydlayer);    /* generate x,y differential images (arrays) */
  1223.  
  1224.   /* Get selection area */
  1225.    gimp_drawable_mask_bounds (orig_draw->id, &x1, &y1, &x2, &y2);
  1226.  
  1227.    width  = orig_draw->width;
  1228.    height = orig_draw->height;
  1229.    bytes  = orig_draw->bpp;
  1230.    image_type = gimp_drawable_type(orig_draw->id);
  1231.  
  1232.    *map_x = gimp_drawable_get(xdlayer);
  1233.    *map_y = gimp_drawable_get(ydlayer);
  1234.  
  1235.    orig_image_id = gimp_layer_get_image_id(orig_draw->id);
  1236.  
  1237.    /*   gimp_image_lower_layer(orig_image_id, new_layer_id); */ /* hide it! */
  1238.  
  1239.    /*   gimp_layer_set_opacity(new_layer_id, 0.0); */
  1240.  
  1241.    for (warp_iter = 0; warp_iter < dvals.iter; warp_iter++)
  1242.    {
  1243.      if (run_mode != GIMP_RUN_NONINTERACTIVE) {
  1244.        string = g_strdup_printf (_("Flow Step %d..."), warp_iter+1);
  1245.        gimp_progress_init (string);
  1246.        g_free (string);
  1247.        progress = 0;
  1248.        gimp_progress_update (0);
  1249.      }
  1250.      warp_one(orig_draw, orig_draw, *map_x, *map_y, mag_draw, first_time, warp_iter);
  1251.  
  1252.      gimp_drawable_update (orig_draw->id, x1, y1, (x2 - x1), (y2 - y1));
  1253.  
  1254.      if (run_mode != GIMP_RUN_NONINTERACTIVE)
  1255.        gimp_displays_flush();
  1256.  
  1257.  
  1258.      first_time = FALSE;
  1259.  
  1260.    } /*  end for (warp_iter) */
  1261.  
  1262.    /* gimp_image_add_layer (orig_image_id, new_layer_id, 1); */  /* make layer visible in 'layers' dialog */
  1263.  
  1264. } /* Warp */
  1265.  
  1266. /* -------------------------------------------------------------------------- */
  1267.  
  1268. static void
  1269. warp_one (GimpDrawable *draw, 
  1270.       GimpDrawable *new,
  1271.       GimpDrawable *map_x, 
  1272.       GimpDrawable *map_y,
  1273.       GimpDrawable *mag_draw,
  1274.       gint       first_time,
  1275.       gint       step)
  1276. {
  1277.   GimpPixelRgn src_rgn;
  1278.   GimpPixelRgn dest_rgn;
  1279.   GimpPixelRgn map_x_rgn;
  1280.   GimpPixelRgn map_y_rgn;
  1281.   GimpPixelRgn mag_rgn;
  1282.   GimpTile   * tile = NULL;
  1283.   GimpTile   * xtile = NULL;
  1284.   GimpTile   * ytile = NULL;
  1285.   gint row=-1;
  1286.   gint xrow=-1;
  1287.   gint yrow=-1;
  1288.   gint col=-1;
  1289.   gint xcol=-1;
  1290.   gint ycol=-1;
  1291.  
  1292.   gpointer  pr;
  1293.  
  1294.   gint    width = -1;
  1295.   gint    height = -1;
  1296.   gint    dest_bytes=-1;
  1297.   gint    dmap_bytes=-1;
  1298.  
  1299.   guchar *destrow, *dest;
  1300.   guchar *srcrow, *src;
  1301.   guchar *mxrow=NULL, *mx;  /* NULL ptr. to make gcc's -Wall fn. happy */
  1302.   guchar *myrow=NULL, *my;
  1303.  
  1304.   guchar *mmagrow=NULL, *mmag=NULL;
  1305.   guchar  pixel[4][4];
  1306.   gint    x1, y1, x2, y2;
  1307.   gint    x, y;
  1308.   gint    max_progress;
  1309.  
  1310.   gdouble needx, needy;
  1311.   gdouble xval=0;      /* initialize to quiet compiler grumbles */
  1312.   gdouble yval=0;      /* interpolated vector displacement */
  1313.   gdouble scalefac;        /* multiplier for vector displacement scaling */
  1314.   gdouble dscalefac;       /* multiplier for incremental displacement vectors */
  1315.   gint    xi, yi;
  1316.   gint    substep;         /* loop variable counting displacement vector substeps */
  1317.  
  1318.   guchar  values[4];
  1319.   gint    ivalues[4];
  1320.   guchar  val;
  1321.  
  1322.   gint k;
  1323.  
  1324.   gdouble dx, dy;           /* X and Y Displacement, integer from GRAY map */
  1325.  
  1326.   gint    xm_alpha = 0;
  1327.   gint    ym_alpha = 0;
  1328.   gint    mmag_alpha = 0;
  1329.   gint    xm_bytes = 1;
  1330.   gint    ym_bytes = 1;
  1331.   gint    mmag_bytes = 1;
  1332.  
  1333.  
  1334.   srand(time(NULL));                   /* seed random # generator */
  1335.  
  1336.   /* ================ Outer Loop calculation ================================ */
  1337.  
  1338.   /* Get selection area */
  1339.  
  1340.    gimp_drawable_mask_bounds (draw->id, &x1, &y1, &x2, &y2);
  1341.    width  = draw->width;
  1342.    height = draw->height;
  1343.    dest_bytes  = draw->bpp;
  1344.  
  1345.    dmap_bytes = map_x->bpp;
  1346.  
  1347.    max_progress = (x2 - x1) * (y2 - y1);
  1348.  
  1349.  
  1350.    /*  --------- Register the (many) pixel regions ----------  */
  1351.  
  1352.    gimp_pixel_rgn_init (&src_rgn, draw, x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
  1353.  
  1354.    /* only push undo-stack the first time through. Thanks Spencer! */
  1355.    if (first_time==TRUE)
  1356.      gimp_pixel_rgn_init (&dest_rgn, new, x1, y1, (x2 - x1), (y2 - y1), TRUE, TRUE);
  1357.    else
  1358.      /*     gimp_pixel_rgn_init (&dest_rgn, new, x1, y1, (x2 - x1), (y2 - y1), TRUE, FALSE); */
  1359.      gimp_pixel_rgn_init (&dest_rgn, new, x1, y1, (x2 - x1), (y2 - y1), TRUE, TRUE);
  1360.  
  1361.  
  1362.    gimp_pixel_rgn_init (&map_x_rgn, map_x, x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
  1363.    if (gimp_drawable_has_alpha(map_x->id))
  1364.     xm_alpha = 1;
  1365.    xm_bytes = gimp_drawable_bpp(map_x->id);
  1366.  
  1367.    gimp_pixel_rgn_init (&map_y_rgn, map_y, x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
  1368.    if (gimp_drawable_has_alpha(map_y->id))
  1369.     ym_alpha = 1;
  1370.    ym_bytes = gimp_drawable_bpp(map_y->id);
  1371.  
  1372.  
  1373.    if (dvals.mag_use == TRUE) {
  1374.      gimp_pixel_rgn_init (&mag_rgn, mag_draw, x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
  1375.      if (gimp_drawable_has_alpha(mag_draw->id))
  1376.        mmag_alpha = 1;
  1377.      mmag_bytes = gimp_drawable_bpp(mag_draw->id);
  1378.  
  1379.      pr = gimp_pixel_rgns_register (5, &src_rgn, &dest_rgn, &map_x_rgn, &map_y_rgn, &mag_rgn);
  1380.    } else {
  1381.      pr = gimp_pixel_rgns_register (4, &src_rgn, &dest_rgn, &map_x_rgn, &map_y_rgn);
  1382.    }
  1383.  
  1384.    dscalefac = (dvals.amount) / (256* 127.5 * dvals.substeps);  /* substep displacement vector scale factor */
  1385.  
  1386.  
  1387.    for (pr = pr; pr != NULL; pr = gimp_pixel_rgns_process (pr))
  1388.     {  
  1389.  
  1390.       srcrow = src_rgn.data;
  1391.       destrow = dest_rgn.data;
  1392.       mxrow = map_x_rgn.data;
  1393.       myrow = map_y_rgn.data;
  1394.       if (dvals.mag_use == TRUE) 
  1395.     mmagrow = mag_rgn.data;
  1396.             
  1397.       /* loop over destination pixels */
  1398.       for (y = dest_rgn.y; y < (dest_rgn.y + dest_rgn.h); y++)
  1399.     {
  1400.       src = srcrow;
  1401.       dest = destrow;
  1402.       mx = mxrow;
  1403.       my = myrow;
  1404.  
  1405.           if (dvals.mag_use == TRUE) 
  1406.         mmag = mmagrow;
  1407.       
  1408.       for (x = dest_rgn.x; x < (dest_rgn.x + dest_rgn.w); x++)
  1409.         {
  1410.           /* ----- Find displacement vector (amnt_x, amnt_y) ------------ */
  1411.  
  1412.               dx = dscalefac * ((256.0*mx[0])+mx[1] -32768);  /* 16-bit values */ 
  1413.           dy = dscalefac * ((256.0*my[0])+my[1] -32768);
  1414.  
  1415.           if (dvals.mag_use == TRUE) {
  1416.         scalefac = warp_map_mag_give_value(mmag, mmag_alpha, mmag_bytes)/255.0;
  1417.         dx *= scalefac;
  1418.         dy *= scalefac;
  1419.           }
  1420.           
  1421.           if (dvals.dither != 0.0) {       /* random dither is +/- dvals.dither pixels */
  1422.         dx += dvals.dither*((gdouble)(rand() - (G_MAXRAND >> 1)) / (G_MAXRAND >> 1));
  1423.         dy += dvals.dither*((gdouble)(rand() - (G_MAXRAND >> 1)) / (G_MAXRAND >> 1));
  1424.           }
  1425.           
  1426.           if (dvals.substeps != 1) {   /* trace (substeps) iterations of displacement vector */
  1427.  
  1428.         for (substep = 1; substep < dvals.substeps; substep++) {
  1429.  
  1430.                 needx = x + dx;   /* In this (substep) loop, (x,y) remain fixed. (dx,dy) vary each step. */
  1431.               needy = y + dy;
  1432.  
  1433.               if (needx >= 0.0)    xi = (int) needx;
  1434.             else           xi = -((int) -needx + 1);
  1435.  
  1436.           if (needy >= 0.0)    yi = (int) needy;
  1437.             else               yi = -((int) -needy + 1);
  1438.  
  1439.              /* get 4 neighboring DX values from DiffX drawable for linear interpolation */
  1440.               xtile = warp_pixel (map_x, xtile, width, height, x1, y1, x2, y2, xi, yi, &xrow, &xcol, pixel[0]);
  1441.               xtile = warp_pixel (map_x, xtile, width, height, x1, y1, x2, y2, xi + 1, yi, &xrow, &xcol, pixel[1]);
  1442.               xtile = warp_pixel (map_x, xtile, width, height, x1, y1, x2, y2, xi, yi + 1, &xrow, &xcol, pixel[2]);
  1443.               xtile = warp_pixel (map_x, xtile, width, height, x1, y1, x2, y2, xi + 1, yi + 1, &xrow, &xcol, pixel[3]);
  1444.           
  1445.               ivalues[0] = 256*pixel[0][0] + pixel[0][1];
  1446.           ivalues[1] = 256*pixel[1][0] + pixel[1][1];
  1447.           ivalues[2] = 256*pixel[2][0] + pixel[2][1];
  1448.           ivalues[3] = 256*pixel[3][0] + pixel[3][1];
  1449.           xval = bilinear16(needx, needy, ivalues);
  1450.  
  1451.              /* get 4 neighboring DY values from DiffY drawable for linear interpolation */
  1452.               ytile = warp_pixel (map_y, ytile, width, height, x1, y1, x2, y2, xi, yi, &yrow, &ycol, pixel[0]);
  1453.               ytile = warp_pixel (map_y, ytile, width, height, x1, y1, x2, y2, xi + 1, yi, &yrow, &ycol, pixel[1]);
  1454.               ytile = warp_pixel (map_y, ytile, width, height, x1, y1, x2, y2, xi, yi + 1, &yrow, &ycol, pixel[2]);
  1455.               ytile = warp_pixel (map_y, ytile, width, height, x1, y1, x2, y2, xi + 1, yi + 1, &yrow, &ycol, pixel[3]);
  1456.           
  1457.               ivalues[0] = 256*pixel[0][0] + pixel[0][1];
  1458.           ivalues[1] = 256*pixel[1][0] + pixel[1][1];
  1459.           ivalues[2] = 256*pixel[2][0] + pixel[2][1];
  1460.           ivalues[3] = 256*pixel[3][0] + pixel[3][1];
  1461.           yval = bilinear16(needx, needy, ivalues);
  1462.  
  1463.           dx += dscalefac * (xval-32768);      /* move displacement vector to this new value */
  1464.           dy += dscalefac * (yval-32768);
  1465.  
  1466.         } /* for (substep) */
  1467.           } /* if (substeps != 0) */
  1468.  
  1469.  
  1470.           /* --------------------------------------------------------- */
  1471.  
  1472.           needx = x + dx;
  1473.           needy = y + dy;
  1474.  
  1475.           mx += xm_bytes;         /* pointers into x,y displacement maps */
  1476.           my += ym_bytes;
  1477.  
  1478.           if (dvals.mag_use == TRUE)
  1479.         mmag += mmag_bytes;
  1480.           
  1481.           /* Calculations complete; now copy the proper pixel */
  1482.           
  1483.           if (needx >= 0.0)
  1484.               xi = (int) needx;
  1485.           else
  1486.               xi = -((int) -needx + 1);
  1487.  
  1488.           if (needy >= 0.0)
  1489.               yi = (int) needy;
  1490.           else
  1491.               yi = -((int) -needy + 1);
  1492.           
  1493.           /* get 4 neighboring pixel values from source drawable for linear interpolation */
  1494.           tile = warp_pixel (draw, tile, width, height, x1, y1, x2, y2, xi, yi, &row, &col, pixel[0]);
  1495.           tile = warp_pixel (draw, tile, width, height, x1, y1, x2, y2, xi + 1, yi, &row, &col, pixel[1]);
  1496.           tile = warp_pixel (draw, tile, width, height, x1, y1, x2, y2, xi, yi + 1, &row, &col, pixel[2]);
  1497.           tile = warp_pixel (draw, tile, width, height, x1, y1, x2, y2, xi + 1, yi + 1, &row, &col, pixel[3]);
  1498.           
  1499.           for (k = 0; k < dest_bytes; k++)
  1500.         {
  1501.           values[0] = pixel[0][k];
  1502.           values[1] = pixel[1][k];
  1503.           values[2] = pixel[2][k];
  1504.           values[3] = pixel[3][k];
  1505.           val = bilinear(needx, needy, values);
  1506.           
  1507.           *dest++ = val;
  1508.         } /* for k */
  1509.  
  1510.         } /* for x */
  1511.       
  1512.       /*      srcrow += src_rgn.rowstride; */
  1513.       srcrow += src_rgn.rowstride;
  1514.       destrow += dest_rgn.rowstride;
  1515.       mxrow += map_x_rgn.rowstride;
  1516.       myrow += map_y_rgn.rowstride;
  1517.           if (dvals.mag_use == TRUE)
  1518.         mmagrow += mag_rgn.rowstride;
  1519.  
  1520.     } /* for y */
  1521.  
  1522.       progress += (dest_rgn.w * dest_rgn.h);
  1523.       gimp_progress_update ((double) progress / (double) max_progress);
  1524.  
  1525.   
  1526.     } /* for pr */
  1527.  
  1528.    if (tile != NULL)
  1529.     gimp_tile_unref (tile, FALSE);
  1530.  
  1531.    if (xtile != NULL)
  1532.     gimp_tile_unref (xtile, FALSE);
  1533.  
  1534.    if (ytile != NULL)
  1535.     gimp_tile_unref (ytile, FALSE);
  1536.  
  1537.      /*  update the region  */
  1538.    gimp_drawable_flush (new);
  1539.  
  1540.    gimp_drawable_merge_shadow(draw->id, (first_time == TRUE));
  1541.   
  1542. } /* warp_one */
  1543.  
  1544. /* ------------------------------------------------------------------------- */
  1545.  
  1546. static gdouble
  1547. warp_map_mag_give_value (guchar *pt,
  1548.              gint    alpha,
  1549.              gint    bytes)
  1550. {
  1551.   gdouble ret, val_alpha;
  1552.   
  1553.   if (bytes >= 3)
  1554.     ret =  (pt[0] + pt[1] + pt[2])/3.0;
  1555.   else
  1556.     ret = (gdouble) *pt;
  1557.   
  1558.   if (alpha)
  1559.     {
  1560.       val_alpha = pt[bytes - 1];
  1561.       ret = (ret * val_alpha / 255.0);
  1562.     };
  1563.   
  1564.   return (ret);
  1565. }
  1566.  
  1567.  
  1568. static GimpTile *
  1569. warp_pixel (GimpDrawable *drawable,
  1570.         GimpTile     *tile,
  1571.         gint       width,
  1572.         gint       height,
  1573.         gint       x1,
  1574.         gint       y1,
  1575.         gint       x2,
  1576.         gint       y2,
  1577.         gint       x,
  1578.         gint       y,
  1579.         gint      *row,
  1580.         gint      *col,
  1581.         guchar    *pixel)
  1582. {
  1583.   static guchar empty_pixel[4] = {0, 0, 0, 0};
  1584.   guchar *data;
  1585.   gint b;
  1586.  
  1587.   /* Tile the image. */
  1588.   if (dvals.wrap_type == WRAP)
  1589.     {
  1590.       if (x < 0)
  1591.     x = width - (-x % width);
  1592.       else
  1593.     x %= width;
  1594.  
  1595.       if (y < 0)
  1596.     y = height - (-y % height);
  1597.       else
  1598.     y %= height;
  1599.     }
  1600.   /* Smear out the edges of the image by repeating pixels. */
  1601.   else if (dvals.wrap_type == SMEAR)
  1602.     {
  1603.       if (x < 0)
  1604.     x = 0;
  1605.       else if (x > width - 1)
  1606.     x = width - 1;
  1607.  
  1608.       if (y < 0)
  1609.     y = 0;
  1610.       else if (y > height - 1)
  1611.     y = height - 1;
  1612.     }
  1613.  
  1614.   if (x >= x1 && y >= y1 && x < x2 && y < y2)
  1615.     {
  1616.       if ((( (guint) (x / tile_width)) != *col) || (( (guint) (y / tile_height)) != *row))
  1617.     {
  1618.       *col = x / tile_width;
  1619.       *row = y / tile_height;
  1620.       if (tile)
  1621.         gimp_tile_unref (tile, FALSE);
  1622.       tile = gimp_drawable_get_tile (drawable, FALSE, *row, *col);
  1623.       gimp_tile_ref (tile);
  1624.     }
  1625.  
  1626.       data = tile->data + tile->bpp * (tile->ewidth * (y % tile_height) + (x % tile_width));
  1627.     }
  1628.   else
  1629.     {
  1630.       if (dvals.wrap_type == BLACK)
  1631.         data = empty_pixel;
  1632.       else
  1633.         data = color_pixel;      /* must have selected COLOR type */
  1634.     }
  1635.  
  1636.   for (b = 0; b < drawable->bpp; b++)
  1637.     pixel[b] = data[b];
  1638.  
  1639.   return tile;
  1640. }
  1641.  
  1642. static guchar
  1643. bilinear (gdouble  x,
  1644.       gdouble  y,
  1645.       guchar  *v)
  1646. {
  1647.   gdouble m0, m1;
  1648.  
  1649.   x = fmod(x, 1.0);
  1650.   y = fmod(y, 1.0);
  1651.  
  1652.   if (x < 0)
  1653.     x += 1.0;
  1654.   if (y < 0)
  1655.     y += 1.0;
  1656.  
  1657.   m0 = (gdouble) v[0] + x * ((gdouble) v[1] - v[0]);
  1658.   m1 = (gdouble) v[2] + x * ((gdouble) v[3] - v[2]);
  1659.  
  1660.   return (guchar) (m0 + y * (m1 - m0));
  1661. } /* bilinear */
  1662.  
  1663. static gint
  1664. bilinear16 (gdouble  x,
  1665.         gdouble  y,
  1666.         gint    *v)
  1667. {
  1668.   gdouble m0, m1;
  1669.  
  1670.   x = fmod(x, 1.0);
  1671.   y = fmod(y, 1.0);
  1672.  
  1673.   if (x < 0)
  1674.     x += 1.0;
  1675.   if (y < 0)
  1676.     y += 1.0;
  1677.  
  1678.   m0 = (gdouble) v[0] + x * ((gdouble) v[1] - v[0]);
  1679.   m1 = (gdouble) v[2] + x * ((gdouble) v[3] - v[2]);
  1680.  
  1681.   return (gint) (m0 + y * (m1 - m0));
  1682. } /* bilinear16 */
  1683.  
  1684.  
  1685. /*  Warp interface functions  */
  1686.  
  1687. static gint
  1688. warp_map_constrain (gint32     image_id,
  1689.             gint32     drawable_id,
  1690.             gpointer   data)
  1691. {
  1692.   GimpDrawable *drawable;
  1693.  
  1694.   drawable = (GimpDrawable *) data;
  1695.  
  1696.   if (drawable_id == -1)
  1697.     return TRUE;
  1698.  
  1699.   if (gimp_drawable_width (drawable_id) == drawable->width &&
  1700.       gimp_drawable_height (drawable_id) == drawable->height)
  1701.     return TRUE;
  1702.   else
  1703.     return FALSE;
  1704. }
  1705.  
  1706. static void
  1707. warp_map_callback (gint32   id,
  1708.            gpointer data)
  1709. {
  1710.   dvals.warp_map = id;
  1711. }
  1712.  
  1713. static void
  1714. warp_map_mag_callback (gint32   id,
  1715.                gpointer data)
  1716. {
  1717.   dvals.mag_map = id;
  1718. }
  1719.  
  1720. static void
  1721. warp_map_grad_callback (gint32   id,
  1722.             gpointer data)
  1723. {
  1724.   dvals.grad_map = id;
  1725. }
  1726.  
  1727. static void
  1728. warp_map_vector_callback (gint32   id,
  1729.               gpointer data)
  1730. {
  1731.   dvals.vector_map = id;
  1732. }
  1733.  
  1734. static void
  1735. warp_ok_callback (GtkWidget *widget,
  1736.           gpointer   data)
  1737. {
  1738.   dint.run = TRUE;
  1739.  
  1740.   gtk_widget_destroy (GTK_WIDGET (data));
  1741. }
  1742.