1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
/* Softglow filter for the GIMP for BIPS
22
* This filter screens a desaturated, sigmoidally transferred
23
* and gaussian blurred version of the drawable over itself
24
* to create a "softglow" photographic effect.
31
#include <libgimp/gimp.h>
32
#include <libgimp/gimpui.h>
34
#include "libgimp/stdplugins-intl.h"
37
/* Some useful macros */
39
#define TILE_CACHE_SIZE 48
40
#define SIGMOIDAL_BASE 2
41
#define SIGMOIDAL_RANGE 20
43
#define INT_MULT(a,b,t) ((t) = (a) * (b) + 0x80, ((((t) >> 8) + (t)) >> 8))
54
* Function prototypes.
57
static void query (void);
58
static void run (const gchar *name,
60
const GimpParam *param,
62
GimpParam **return_vals);
64
static void softglow (GimpDrawable *drawable,
65
GimpPreview *preview);
66
static gboolean softglow_dialog (GimpDrawable *drawable);
69
* Gaussian blur helper functions
71
static void find_constants (gdouble n_p[],
78
static void transfer_pixels (gdouble *src1,
84
/***** Local vars *****/
86
GimpPlugInInfo PLUG_IN_INFO =
94
static SoftglowVals svals =
96
10.0, /* glow_radius */
97
0.75, /* brightness */
102
/***** Functions *****/
109
static GimpParamDef args[] =
111
{ GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
112
{ GIMP_PDB_IMAGE, "image", "Input image (unused)" },
113
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
114
{ GIMP_PDB_FLOAT, "glow_radius", "Glow radius (radius in pixels)" },
115
{ GIMP_PDB_FLOAT, "brightness", "Glow brightness (0.0 - 1.0)" },
116
{ GIMP_PDB_FLOAT, "sharpness", "Glow sharpness (0.0 - 1.0)" },
120
"Gives an image a softglow effect by intensifying the highlights in the "
121
"image. This is done by screening a modified version of the drawable "
122
"with itself. The modified version is desaturated and then a sigmoidal "
123
"transfer function is applied to force the distribution of intensities "
124
"into very small and very large only. This desaturated version is then "
125
"blurred to give it a fuzzy 'vaseline-on-the-lens' effect. The glow "
126
"radius parameter controls the sharpness of the glow effect. The "
127
"brightness parameter controls the degree of intensification applied "
128
"to image highlights. The sharpness parameter controls how defined or "
129
"alternatively, diffuse, the glow effect should be.";
131
gimp_install_procedure ("plug_in_softglow",
132
"Photographic 'softglow' effect by intensifying "
136
"Bit Specialists, Inc.",
141
G_N_ELEMENTS (args), 0,
144
gimp_plugin_menu_register ("plug_in_softglow", "<Image>/Filters/Artistic");
148
run (const gchar *name,
150
const GimpParam *param,
152
GimpParam **return_vals)
154
static GimpParam values[1];
155
GimpRunMode run_mode;
156
GimpDrawable *drawable;
157
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
159
run_mode = param[0].data.d_int32;
161
/* Get the specified drawable */
162
drawable = gimp_drawable_get (param[2].data.d_drawable);
164
/* set the tile cache size */
165
gimp_tile_cache_ntiles (TILE_CACHE_SIZE);
168
*return_vals = values;
170
values[0].type = GIMP_PDB_STATUS;
171
values[0].data.d_status = status;
177
case GIMP_RUN_INTERACTIVE:
178
/* Possibly retrieve data */
179
gimp_get_data ("plug_in_softglow", &svals);
181
/* First acquire information with a dialog */
182
if (! softglow_dialog (drawable))
186
case GIMP_RUN_NONINTERACTIVE:
187
svals.glow_radius = param[3].data.d_float;
188
svals.brightness = param[4].data.d_float;
189
svals.sharpness = param[5].data.d_float;
192
case GIMP_RUN_WITH_LAST_VALS:
193
/* Possibly retrieve data */
194
gimp_get_data ("plug_in_softglow", &svals);
201
if (status == GIMP_PDB_SUCCESS)
203
/* Make sure that the drawable is RGB or GRAY color */
204
if (gimp_drawable_is_rgb (drawable->drawable_id) ||
205
gimp_drawable_is_gray (drawable->drawable_id))
207
gimp_progress_init ("Softglow...");
209
softglow (drawable, NULL);
211
if (run_mode != GIMP_RUN_NONINTERACTIVE)
212
gimp_displays_flush ();
215
if (run_mode == GIMP_RUN_INTERACTIVE)
216
gimp_set_data ("plug_in_softglow", &svals, sizeof (SoftglowVals));
220
g_message (_("Cannot operate on indexed color images."));
221
status = GIMP_PDB_EXECUTION_ERROR;
225
values[0].data.d_status = status;
227
gimp_drawable_detach (drawable);
231
softglow (GimpDrawable *drawable,
232
GimpPreview *preview)
234
GimpPixelRgn src_rgn, dest_rgn;
240
guchar *src, *sp_p, *sp_m;
241
gdouble n_p[5], n_m[5];
242
gdouble d_p[5], d_m[5];
243
gdouble bd_p[5], bd_m[5];
244
gdouble *val_p, *val_m, *vp, *vm;
249
gint progress, max_progress;
259
gimp_preview_get_position (preview, &x1, &y1);
260
gimp_preview_get_size (preview, &width, &height);
266
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
271
bytes = drawable->bpp;
272
has_alpha = gimp_drawable_has_alpha (drawable->drawable_id);
274
val_p = g_new (gdouble, MAX (width, height));
275
val_m = g_new (gdouble, MAX (width, height));
277
dest = g_new0 (guchar, width * height);
280
max_progress = width * height * 3;
282
/* Initialize the pixel regions. */
283
gimp_pixel_rgn_init (&src_rgn, drawable, x1, y1, width, height, FALSE, FALSE);
285
for (pr = gimp_pixel_rgns_register (1, &src_rgn);
287
pr = gimp_pixel_rgns_process (pr))
289
guchar *src_ptr = src_rgn.data;
290
guchar *dest_ptr = dest + (src_rgn.y - y1) * width + (src_rgn.x - x1);
292
for (row = 0; row < src_rgn.h; row++)
294
for (col = 0; col < src_rgn.w; col++)
298
dest_ptr[col] = (guchar) gimp_rgb_to_l_int (src_ptr[col * bytes + 0],
299
src_ptr[col * bytes + 1],
300
src_ptr[col * bytes + 2]);
302
dest_ptr[col] = (guchar) src_ptr[col * bytes];
304
/* compute sigmoidal transfer */
305
val = dest_ptr[col] / 255.0;
306
val = 255.0 / (1 + exp (-(SIGMOIDAL_BASE + (svals.sharpness * SIGMOIDAL_RANGE)) * (val - 0.5)));
307
val = val * svals.brightness;
308
dest_ptr[col] = (guchar) CLAMP (val, 0, 255);
311
src_ptr += src_rgn.rowstride;
317
progress += src_rgn.w * src_rgn.h;
318
gimp_progress_update ((gdouble) progress / (gdouble) max_progress);
322
/* Calculate the standard deviations */
323
radius = fabs (svals.glow_radius) + 1.0;
324
std_dev = sqrt (-(radius * radius) / (2 * log (1.0 / 255.0)));
326
/* derive the constants for calculating the gaussian from the std dev */
327
find_constants (n_p, n_m, d_p, d_m, bd_p, bd_m, std_dev);
329
/* First the vertical pass */
330
for (col = 0; col < width; col++)
332
memset (val_p, 0, height * sizeof (gdouble));
333
memset (val_m, 0, height * sizeof (gdouble));
337
sp_m = src + width * (height - 1);
339
vm = val_m + (height - 1);
341
/* Set up the first vals */
342
initial_p[0] = sp_p[0];
343
initial_m[0] = sp_m[0];
345
for (row = 0; row < height; row++)
347
gdouble *vpptr, *vmptr;
349
terms = (row < 4) ? row : 4;
351
vpptr = vp; vmptr = vm;
352
for (i = 0; i <= terms; i++)
354
*vpptr += n_p[i] * sp_p[-i * width] - d_p[i] * vp[-i];
355
*vmptr += n_m[i] * sp_m[i * width] - d_m[i] * vm[i];
357
for (j = i; j <= 4; j++)
359
*vpptr += (n_p[j] - bd_p[j]) * initial_p[0];
360
*vmptr += (n_m[j] - bd_m[j]) * initial_m[0];
369
transfer_pixels (val_p, val_m, dest + col, width, height);
375
gimp_progress_update ((gdouble) progress / (gdouble) max_progress);
379
for (row = 0; row < height; row++)
381
memset (val_p, 0, width * sizeof (gdouble));
382
memset (val_m, 0, width * sizeof (gdouble));
384
src = dest + row * width;
387
sp_m = src + width - 1;
389
vm = val_m + width - 1;
391
/* Set up the first vals */
392
initial_p[0] = sp_p[0];
393
initial_m[0] = sp_m[0];
395
for (col = 0; col < width; col++)
397
gdouble *vpptr, *vmptr;
399
terms = (col < 4) ? col : 4;
401
vpptr = vp; vmptr = vm;
403
for (i = 0; i <= terms; i++)
405
*vpptr += n_p[i] * sp_p[-i] - d_p[i] * vp[-i];
406
*vmptr += n_m[i] * sp_m[i] - d_m[i] * vm[i];
409
for (j = i; j <= 4; j++)
411
*vpptr += (n_p[j] - bd_p[j]) * initial_p[0];
412
*vmptr += (n_m[j] - bd_m[j]) * initial_m[0];
421
transfer_pixels (val_p, val_m, dest + row * width, 1, width);
427
gimp_progress_update ((gdouble) progress / (gdouble) max_progress);
431
/* Initialize the pixel regions. */
432
gimp_pixel_rgn_init (&src_rgn, drawable, x1, y1, width, height, FALSE, FALSE);
433
gimp_pixel_rgn_init (&dest_rgn, drawable,
434
x1, y1, width, height, (preview == NULL), TRUE);
436
for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn);
438
pr = gimp_pixel_rgns_process (pr))
440
guchar *src_ptr = src_rgn.data;
441
guchar *dest_ptr = dest_rgn.data;
442
guchar *blur_ptr = dest + (src_rgn.y - y1) * width + (src_rgn.x - x1);
444
for (row = 0; row < src_rgn.h; row++)
446
for (col = 0; col < src_rgn.w; col++)
449
for (b = 0; b < (has_alpha ? (bytes - 1) : bytes); b++)
450
dest_ptr[col * bytes + b] =
451
255 - INT_MULT((255 - src_ptr[col * bytes + b]),
452
(255 - blur_ptr[col]), tmp);
454
dest_ptr[col * bytes + b] = src_ptr[col * bytes + b];
457
src_ptr += src_rgn.rowstride;
458
dest_ptr += dest_rgn.rowstride;
464
gimp_drawable_preview_draw_region (GIMP_DRAWABLE_PREVIEW (preview),
469
progress += src_rgn.w * src_rgn.h;
470
gimp_progress_update ((gdouble) progress / (gdouble) max_progress);
476
/* merge the shadow, update the drawable */
477
gimp_drawable_flush (drawable);
478
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
479
gimp_drawable_update (drawable->drawable_id,
480
x1, y1, (x2 - x1), (y2 - y1));
483
/* free up buffers */
490
* Gaussian blur helper functions
494
transfer_pixels (gdouble *src1,
503
for (i = 0; i < width; i++)
505
sum = src1[i] + src2[i];
507
sum = CLAMP0255 (sum);
509
*dest = (guchar) sum;
515
find_constants (gdouble n_p[],
524
gdouble constants [8];
527
/* The constants used in the implemenation of a casual sequence
528
* using a 4th order approximation of the gaussian operator
531
div = sqrt(2 * G_PI) * std_dev;
533
constants [0] = -1.783 / std_dev;
534
constants [1] = -1.723 / std_dev;
535
constants [2] = 0.6318 / std_dev;
536
constants [3] = 1.997 / std_dev;
537
constants [4] = 1.6803 / div;
538
constants [5] = 3.735 / div;
539
constants [6] = -0.6803 / div;
540
constants [7] = -0.2598 / div;
542
n_p [0] = constants[4] + constants[6];
543
n_p [1] = exp (constants[1]) *
544
(constants[7] * sin (constants[3]) -
545
(constants[6] + 2 * constants[4]) * cos (constants[3])) +
547
(constants[5] * sin (constants[2]) -
548
(2 * constants[6] + constants[4]) * cos (constants[2]));
549
n_p [2] = 2 * exp (constants[0] + constants[1]) *
550
((constants[4] + constants[6]) * cos (constants[3]) * cos (constants[2]) -
551
constants[5] * cos (constants[3]) * sin (constants[2]) -
552
constants[7] * cos (constants[2]) * sin (constants[3])) +
553
constants[6] * exp (2 * constants[0]) +
554
constants[4] * exp (2 * constants[1]);
555
n_p [3] = exp (constants[1] + 2 * constants[0]) *
556
(constants[7] * sin (constants[3]) - constants[6] * cos (constants[3])) +
557
exp (constants[0] + 2 * constants[1]) *
558
(constants[5] * sin (constants[2]) - constants[4] * cos (constants[2]));
562
d_p [1] = -2 * exp (constants[1]) * cos (constants[3]) -
563
2 * exp (constants[0]) * cos (constants[2]);
564
d_p [2] = 4 * cos (constants[3]) * cos (constants[2]) * exp (constants[0] + constants[1]) +
565
exp (2 * constants[1]) + exp (2 * constants[0]);
566
d_p [3] = -2 * cos (constants[2]) * exp (constants[0] + 2 * constants[1]) -
567
2 * cos (constants[3]) * exp (constants[1] + 2 * constants[0]);
568
d_p [4] = exp (2 * constants[0] + 2 * constants[1]);
570
#ifndef ORIGINAL_READABLE_CODE
571
memcpy(d_m, d_p, 5 * sizeof(gdouble));
573
for (i = 0; i <= 4; i++)
578
for (i = 1; i <= 4; i++)
579
n_m [i] = n_p[i] - d_p[i] * n_p[0];
582
gdouble sum_n_p, sum_n_m, sum_d;
589
for (i = 0; i <= 4; i++)
596
#ifndef ORIGINAL_READABLE_CODE
601
a = sum_n_p / (1 + sum_d);
602
b = sum_n_m / (1 + sum_d);
605
for (i = 0; i <= 4; i++)
607
bd_p[i] = d_p[i] * a;
608
bd_m[i] = d_m[i] * b;
613
/*******************************************************/
615
/*******************************************************/
618
softglow_dialog (GimpDrawable *drawable)
621
GtkWidget *main_vbox;
624
GtkObject *scale_data;
627
gimp_ui_init ("softglow", FALSE);
629
dialog = gimp_dialog_new (_("Softglow"), "softglow",
631
gimp_standard_help_func, "plug-in-softglow",
633
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
634
GTK_STOCK_OK, GTK_RESPONSE_OK,
638
main_vbox = gtk_vbox_new (FALSE, 12);
639
gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
640
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
641
gtk_widget_show (main_vbox);
643
preview = gimp_drawable_preview_new (drawable, NULL);
644
gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
645
gtk_widget_show (preview);
647
g_signal_connect_swapped (preview, "invalidated",
648
G_CALLBACK (softglow),
651
table = gtk_table_new (3, 3, FALSE);
652
gtk_table_set_col_spacings (GTK_TABLE (table), 6);
653
gtk_table_set_row_spacings (GTK_TABLE (table), 6);
654
gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
655
gtk_widget_show (table);
657
/* Label, scale, entry for svals.amount */
658
scale_data = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
659
_("_Glow radius:"), 100, 5,
660
svals.glow_radius, 1.0, 50.0, 1, 5.0, 2,
664
g_signal_connect (scale_data, "value_changed",
665
G_CALLBACK (gimp_double_adjustment_update),
667
g_signal_connect_swapped (scale_data, "value_changed",
668
G_CALLBACK (gimp_preview_invalidate),
671
/* Label, scale, entry for svals.amount */
672
scale_data = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
673
_("_Brightness:"), 100, 5,
674
svals.brightness, 0.0, 1.0, 0.01, 0.1, 2,
678
g_signal_connect (scale_data, "value_changed",
679
G_CALLBACK (gimp_double_adjustment_update),
681
g_signal_connect_swapped (scale_data, "value_changed",
682
G_CALLBACK (gimp_preview_invalidate),
685
/* Label, scale, entry for svals.amount */
686
scale_data = gimp_scale_entry_new (GTK_TABLE (table), 0, 2,
687
_("_Sharpness:"), 100, 5,
688
svals.sharpness, 0.0, 1.0, 0.01, 0.1, 2,
692
g_signal_connect (scale_data, "value_changed",
693
G_CALLBACK (gimp_double_adjustment_update),
695
g_signal_connect_swapped (scale_data, "value_changed",
696
G_CALLBACK (gimp_preview_invalidate),
699
gtk_widget_show (dialog);
701
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
703
gtk_widget_destroy (dialog);