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