1
/* scatter_hsv.c -- This is a plug-in for GIMP (1.0's API)
2
* Author: Shuji Narazaki <narazaki@InetQ.or.jp>
3
* Time-stamp: <2000-01-08 02:49:39 yasuhiro>
6
* Copyright (C) 1997 Shuji Narazaki <narazaki@InetQ.or.jp>
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
#include <libgimp/gimp.h>
28
#include <libgimp/gimpui.h>
30
#include "libgimp/stdplugins-intl.h"
33
#define HSV_NOISE_PROC "plug-in-hsv-noise"
34
#define SCATTER_HSV_PROC "plug-in-scatter-hsv"
35
#define PLUG_IN_BINARY "noise-hsv"
36
#define SCALE_WIDTH 100
40
static void query (void);
41
static void run (const gchar *name,
43
const GimpParam *param,
45
GimpParam **return_vals);
47
static void scatter_hsv (GimpDrawable *drawable);
48
static gboolean scatter_hsv_dialog (GimpDrawable *drawable);
49
static void scatter_hsv_preview (GimpPreview *preview);
51
static void scatter_hsv_scatter (guchar *r,
55
static gint randomize_value (gint now,
58
gboolean wraps_around,
62
const GimpPlugInInfo PLUG_IN_INFO =
66
query, /* query_proc */
74
gint saturation_distance;
78
static ValueType VALS =
91
static const GimpParamDef args [] =
93
{ GIMP_PDB_INT32, "run-mode", "Interactive, non-interactive" },
94
{ GIMP_PDB_IMAGE, "image", "Input image (not used)" },
95
{ GIMP_PDB_DRAWABLE, "drawable", "Input drawable" },
96
{ GIMP_PDB_INT32, "holdness", "convolution strength" },
97
{ GIMP_PDB_INT32, "hue-distance", "scattering of hue angle [0,180]" },
98
{ GIMP_PDB_INT32, "saturation-distance", "distribution distance on saturation axis [0,255]" },
99
{ GIMP_PDB_INT32, "value-distance", "distribution distance on value axis [0,255]" }
102
gimp_install_procedure (HSV_NOISE_PROC,
103
N_("Randomize hue/saturation/value independently"),
104
"Scattering pixel values in HSV space",
105
"Shuji Narazaki (narazaki@InetQ.or.jp)",
111
G_N_ELEMENTS (args), 0,
114
gimp_plugin_menu_register (HSV_NOISE_PROC, "<Image>/Filters/Noise");
116
gimp_install_procedure (SCATTER_HSV_PROC,
117
"Scattering pixel values in HSV space",
118
"Scattering pixel values in HSV space",
119
"Shuji Narazaki (narazaki@InetQ.or.jp)",
125
G_N_ELEMENTS (args), 0,
130
run (const gchar *name,
132
const GimpParam *param,
134
GimpParam **return_vals)
136
static GimpParam values[1];
137
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
138
GimpRunMode run_mode;
139
GimpDrawable *drawable;
143
run_mode = param[0].data.d_int32;
144
drawable = gimp_drawable_get (param[2].data.d_int32);
147
*return_vals = values;
149
values[0].type = GIMP_PDB_STATUS;
150
values[0].data.d_status = status;
154
case GIMP_RUN_INTERACTIVE:
155
gimp_get_data (HSV_NOISE_PROC, &VALS);
156
if (!gimp_drawable_is_rgb (drawable->drawable_id))
158
g_message (_("Can only operate on RGB drawables."));
161
if (! scatter_hsv_dialog (drawable))
165
case GIMP_RUN_NONINTERACTIVE:
166
VALS.holdness = CLAMP (param[3].data.d_int32, 1, 8);
167
VALS.hue_distance = CLAMP (param[4].data.d_int32, 0, 180);
168
VALS.saturation_distance = CLAMP (param[5].data.d_int32, 0, 255);
169
VALS.value_distance = CLAMP (param[6].data.d_int32, 0, 255);
172
case GIMP_RUN_WITH_LAST_VALS:
173
gimp_get_data (HSV_NOISE_PROC, &VALS);
177
scatter_hsv (drawable);
179
if (run_mode != GIMP_RUN_NONINTERACTIVE)
180
gimp_displays_flush();
181
if (run_mode == GIMP_RUN_INTERACTIVE && status == GIMP_PDB_SUCCESS )
182
gimp_set_data (HSV_NOISE_PROC, &VALS, sizeof (ValueType));
184
values[0].type = GIMP_PDB_STATUS;
185
values[0].data.d_status = status;
189
scatter_hsv_func (const guchar *src,
200
scatter_hsv_scatter (&h, &s, &v);
211
scatter_hsv (GimpDrawable *drawable)
213
gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1));
215
gimp_progress_init (_("HSV Noise"));
217
gimp_rgn_iterate2 (drawable, 0 /* unused */, scatter_hsv_func, NULL);
219
gimp_drawable_detach (drawable);
223
randomize_value (gint now,
226
gboolean wraps_around,
229
gint flag, new, steps, index;
232
steps = max - min + 1;
233
rand_val = g_random_double ();
234
for (index = 1; index < VALS.holdness; index++)
236
double tmp = g_random_double ();
241
if (g_random_double () < 0.5)
246
new = now + flag * ((int) (rand_max * rand_val) % steps);
266
scatter_hsv_scatter (guchar *r,
274
h = *r; s = *g; v = *b;
276
gimp_rgb_to_hsv_int (&h, &s, &v);
278
/* there is no need for scattering hue of desaturated pixels here */
279
if ((VALS.hue_distance > 0) && (s > 0))
280
h = randomize_value (h, 0, 359, TRUE, VALS.hue_distance);
282
/* desaturated pixels get random hue before increasing saturation */
283
if (VALS.saturation_distance > 0) {
285
h = g_random_int_range (0, 360);
286
s = randomize_value (s, 0, 255, FALSE, VALS.saturation_distance);
289
if (VALS.value_distance > 0)
290
v = randomize_value (v, 0, 255, FALSE, VALS.value_distance);
292
h1 = h; s1 = s; v1 = v;
294
gimp_hsv_to_rgb_int (&h, &s, &v); /* don't believe ! */
296
h2 = h; s2 = s; v2 = v;
298
gimp_rgb_to_hsv_int (&h2, &s2, &v2); /* h2 should be h1. But... */
300
if ((abs (h1 - h2) <= VALS.hue_distance) &&
301
(abs (s1 - s2) <= VALS.saturation_distance) &&
302
(abs (v1 - v2) <= VALS.value_distance))
311
scatter_hsv_preview (GimpPreview *preview)
313
GimpDrawable *drawable;
314
GimpPixelRgn src_rgn;
322
gimp_drawable_preview_get_drawable (GIMP_DRAWABLE_PREVIEW (preview));
324
gimp_preview_get_position (preview, &x1, &y1);
325
gimp_preview_get_size (preview, &width, &height);
329
src = g_new (guchar, width * height * bpp);
330
dst = g_new (guchar, width * height * bpp);
332
gimp_pixel_rgn_init (&src_rgn, drawable,
333
x1, y1, width, height,
335
gimp_pixel_rgn_get_rect (&src_rgn, src, x1, y1, width, height);
337
for (i = 0; i < width * height; i++)
338
scatter_hsv_func (src + i * bpp, dst + i * bpp, bpp, NULL);
340
gimp_preview_draw_buffer (preview, dst, width * bpp);
350
scatter_hsv_dialog (GimpDrawable *drawable)
353
GtkWidget *main_vbox;
359
gimp_ui_init (PLUG_IN_BINARY, TRUE);
361
dialog = gimp_dialog_new (_("HSV Noise"), PLUG_IN_BINARY,
363
gimp_standard_help_func, HSV_NOISE_PROC,
365
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
366
GTK_STOCK_OK, GTK_RESPONSE_OK,
370
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
375
gimp_window_set_transient (GTK_WINDOW (dialog));
377
main_vbox = gtk_vbox_new (FALSE, 12);
378
gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
379
gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), main_vbox);
380
gtk_widget_show (main_vbox);
382
preview = gimp_drawable_preview_new (drawable, NULL);
383
gtk_box_pack_start (GTK_BOX (main_vbox), preview, TRUE, TRUE, 0);
384
gtk_widget_show (preview);
386
g_signal_connect (preview, "invalidated",
387
G_CALLBACK (scatter_hsv_preview),
390
table = gtk_table_new (4, 3, FALSE);
391
gtk_table_set_col_spacings (GTK_TABLE (table), 6);
392
gtk_table_set_row_spacings (GTK_TABLE (table), 6);
393
gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
394
gtk_widget_show (table);
396
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
397
_("_Holdness:"), SCALE_WIDTH, ENTRY_WIDTH,
398
VALS.holdness, 1, 8, 1, 2, 0,
401
g_signal_connect (adj, "value-changed",
402
G_CALLBACK (gimp_int_adjustment_update),
404
g_signal_connect_swapped (adj, "value-changed",
405
G_CALLBACK (gimp_preview_invalidate),
408
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 1,
409
_("H_ue:"), SCALE_WIDTH, ENTRY_WIDTH,
410
VALS.hue_distance, 0, 180, 1, 6, 0,
413
g_signal_connect (adj, "value-changed",
414
G_CALLBACK (gimp_int_adjustment_update),
416
g_signal_connect_swapped (adj, "value-changed",
417
G_CALLBACK (gimp_preview_invalidate),
420
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 2,
421
_("_Saturation:"), SCALE_WIDTH, ENTRY_WIDTH,
422
VALS.saturation_distance, 0, 255, 1, 8, 0,
425
g_signal_connect (adj, "value-changed",
426
G_CALLBACK (gimp_int_adjustment_update),
427
&VALS.saturation_distance);
428
g_signal_connect_swapped (adj, "value-changed",
429
G_CALLBACK (gimp_preview_invalidate),
432
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 3,
433
_("_Value:"), SCALE_WIDTH, ENTRY_WIDTH,
434
VALS.value_distance, 0, 255, 1, 8, 0,
437
g_signal_connect (adj, "value-changed",
438
G_CALLBACK (gimp_int_adjustment_update),
439
&VALS.value_distance);
440
g_signal_connect_swapped (adj, "value-changed",
441
G_CALLBACK (gimp_preview_invalidate),
444
gtk_widget_show (dialog);
446
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
448
gtk_widget_destroy (dialog);