From 61ac7167d6b514fb5388e7f981508fd83ee70eaa Mon Sep 17 00:00:00 2001 From: Michael Natterer Date: Sun, 1 Feb 2004 13:22:09 +0000 Subject: [PATCH] Reworked random seed handling once more: 2004-02-01 Michael Natterer Reworked random seed handling once more: * libgimpwidgets/gimpwidgets.c (gimp_random_seed_new): make sure the displayed random seed value is always the one which will be used, whether "Randomize" is checked or not. * plug-ins/common/blur.c * plug-ins/common/plasma.c * plug-ins/common/randomize.c * plug-ins/common/sinus.c * plug-ins/common/snoise.c * plug-ins/maze/algorithms.c * plug-ins/maze/maze.c: if running interactively, always take the seed value produced by the gimp_random_seed widget and don't touch it because the widgets honors the "randomize" toggle correctly now. For noninteractive and last_vals runs, look at the "randomize" boolean and generate a seed if it is TRUE. Initialize the random number generators from the seed determined by either of the above ways and don't fiddle with it in the inner algorithms itself. * plug-ins/gflare/gflare.c (plugin_run): initialize the GRand from the seed unconditionally. (ed_make_page_sflare): no need to get the entry from the random seed widget to get its adjustment. Instead get the adjustment directly. (The random handling code of gflare looks somewhat broken, but that applies to the whole plug-in). --- ChangeLog | 36 ++++++++++++++++++++++ libgimpwidgets/gimpwidgets.c | 60 +++++++++++++++++++++++------------- plug-ins/common/blur.c | 15 +++++---- plug-ins/common/plasma.c | 11 ++----- plug-ins/common/randomize.c | 42 +++++++++++++++---------- plug-ins/common/sinus.c | 16 +++++++--- plug-ins/common/snoise.c | 10 ++++-- plug-ins/gflare/gflare.c | 15 ++------- plug-ins/maze/algorithms.c | 18 +++-------- plug-ins/maze/maze.c | 24 +++++++-------- 10 files changed, 146 insertions(+), 101 deletions(-) diff --git a/ChangeLog b/ChangeLog index 601d6b6e2d..50cefe88a1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,39 @@ +2004-02-01 Michael Natterer + + Reworked random seed handling once more: + + * libgimpwidgets/gimpwidgets.c (gimp_random_seed_new): make sure + the displayed random seed value is always the one which will be + used, whether "Randomize" is checked or not. + + * plug-ins/common/blur.c + * plug-ins/common/plasma.c + * plug-ins/common/randomize.c + * plug-ins/common/sinus.c + * plug-ins/common/snoise.c + * plug-ins/maze/algorithms.c + * plug-ins/maze/maze.c: if running interactively, always take + the seed value produced by the gimp_random_seed widget and don't + touch it because the widgets honors the "randomize" toggle + correctly now. + + For noninteractive and last_vals runs, look at the "randomize" + boolean and generate a seed if it is TRUE. + + Initialize the random number generators from the seed determined + by either of the above ways and don't fiddle with it in the inner + algorithms itself. + + * plug-ins/gflare/gflare.c (plugin_run): initialize the GRand + from the seed unconditionally. + + (ed_make_page_sflare): no need to get the entry from the random + seed widget to get its adjustment. Instead get the adjustment + directly. + + (The random handling code of gflare looks somewhat broken, but + that applies to the whole plug-in). + 2004-02-01 Michael Natterer * app/plug-in/plug-ins.c (plug_ins_init): when removing a diff --git a/libgimpwidgets/gimpwidgets.c b/libgimpwidgets/gimpwidgets.c index f37132bb8f..48f2f2a517 100644 --- a/libgimpwidgets/gimpwidgets.c +++ b/libgimpwidgets/gimpwidgets.c @@ -1214,16 +1214,23 @@ static void gimp_random_seed_update (GtkWidget *widget, gpointer data) { - GtkWidget *w = data; + GtkWidget *spinbutton = data; - gtk_spin_button_set_value (GTK_SPIN_BUTTON (w), - (guint) g_random_int ()); + /* Generate a new seed if the "New Seed" button was clicked or + * of the "Randomize" toggle is activated + */ + if (! GTK_IS_TOGGLE_BUTTON (widget) || + gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget))) + { + gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton), + (guint) g_random_int ()); + } } /** * gimp_random_seed_new: * @seed: A pointer to the variable which stores the random seed. - * @random_seed: A pointer to a boolean indicating whether seed should be + * @random_seed: A pointer to a boolean indicating whether seed should be * initialised randomly or not. * * Creates a widget that allows the user to control how the random number @@ -1233,7 +1240,8 @@ gimp_random_seed_update (GtkWidget *widget, * a #GtkButton for setting a random seed. **/ GtkWidget * -gimp_random_seed_new (guint *seed, gboolean *random_seed) +gimp_random_seed_new (guint *seed, + gboolean *random_seed) { GtkWidget *hbox; GtkWidget *toggle; @@ -1241,61 +1249,69 @@ gimp_random_seed_new (guint *seed, gboolean *random_seed) GtkObject *adj; GtkWidget *button; + g_return_val_if_fail (seed != NULL, NULL); + g_return_val_if_fail (random_seed != NULL, NULL); + hbox = gtk_hbox_new (FALSE, 4); /* If we're being asked to generate a random seed, generate one. */ - /* I'm not sure this should be here if (*random_seed) - { - *seed = g_random_int (); - } - */ + *seed = g_random_int (); spinbutton = gimp_spin_button_new (&adj, *seed, 0, (guint32) -1 , 1, 10, 0, 1, 0); gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0); + gtk_widget_show (spinbutton); + g_signal_connect (adj, "value_changed", G_CALLBACK (gimp_uint_adjustment_update), seed); - gtk_widget_show (spinbutton); gimp_help_set_help_data (spinbutton, _("Use this value for random number generator " "seed - this allows you to repeat a " "given \"random\" operation"), NULL); - button = gtk_button_new_with_mnemonic (_("_New seed")); + button = gtk_button_new_with_mnemonic (_("_New Seed")); gtk_misc_set_padding (GTK_MISC (GTK_BIN (button)->child), 2, 0); - /* Send spinbutton as data so that we can change the value in - * gimp_random_seed_update() */ - g_signal_connect (button, "clicked", - G_CALLBACK (gimp_random_seed_update), - spinbutton); gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0); gtk_widget_show (button); + /* Send spinbutton as data so that we can change the value in + * gimp_random_seed_update() + */ + g_signal_connect (button, "clicked", + G_CALLBACK (gimp_random_seed_update), + spinbutton); + gimp_help_set_help_data (button, - _("Seed random number generator with a generated random number"), + _("Seed random number generator with a generated " + "random number"), NULL); toggle = gtk_check_button_new_with_label (_("Randomize")); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), *random_seed); gtk_box_pack_start (GTK_BOX (hbox), toggle, FALSE, FALSE, 0); gtk_widget_show (toggle); - + g_signal_connect (toggle, "toggled", G_CALLBACK (gimp_toggle_button_update), random_seed); + /* Need to create a new seed when the "Randomize" toggle is activated */ + g_signal_connect (toggle, "toggled", + G_CALLBACK (gimp_random_seed_update), + spinbutton); + g_object_set_data (G_OBJECT (hbox), "spinbutton", spinbutton); g_object_set_data (G_OBJECT (hbox), "button", button); g_object_set_data (G_OBJECT (hbox), "toggle", toggle); - /* Set sensitivity data for the toggle, this stuff makes - * gimp_toggle_button_sensitive_update work */ + /* Set sensitivity data for the toggle, this stuff makes + * gimp_toggle_button_sensitive_update work + */ g_object_set_data (G_OBJECT (toggle), "inverse_sensitive", spinbutton); g_object_set_data (G_OBJECT (spinbutton), "inverse_sensitive", button); - // g_object_set_data (G_OBJECT (button), "inverse_sensitive", adj); /* Initialise sensitivity */ gimp_toggle_button_update (toggle, random_seed); diff --git a/plug-ins/common/blur.c b/plug-ins/common/blur.c index 394db0c4fc..360b220d7f 100644 --- a/plug-ins/common/blur.c +++ b/plug-ins/common/blur.c @@ -251,7 +251,7 @@ run (const gchar *name, */ case GIMP_RUN_INTERACTIVE: gimp_get_data (PLUG_IN_NAME, &pivals); - if (!blur_dialog ()) /* return on Cancel */ + if (! blur_dialog ()) /* return on Cancel */ return; break; @@ -273,6 +273,9 @@ run (const gchar *name, pivals.blur_rcount = (gdouble) MAX (1.0, pivals.blur_rcount); pivals.blur_randomize = (gboolean) param[5].data.d_int32; pivals.blur_seed = (gint) param[6].data.d_int32; + + if (pivals.blur_randomize) + pivals.blur_seed = g_random_int (); } else if ((strcmp (name, PLUG_IN_NAME) == 0) && (nparams == 3)) @@ -293,6 +296,9 @@ run (const gchar *name, */ case GIMP_RUN_WITH_LAST_VALS: gimp_get_data (PLUG_IN_NAME, &pivals); + + if (pivals.blur_randomize) + pivals.blur_seed = g_random_int (); break; /* @@ -310,11 +316,8 @@ run (const gchar *name, gimp_progress_init (_("Blurring...")); gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1)); - /* - * Initialize the rand() function seed - */ - if (pivals.blur_randomize) - g_random_set_seed (pivals.blur_seed); + + g_random_set_seed (pivals.blur_seed); blur (drawable); /* diff --git a/plug-ins/common/plasma.c b/plug-ins/common/plasma.c index a8cdd78e6c..35df5dad6a 100644 --- a/plug-ins/common/plasma.c +++ b/plug-ins/common/plasma.c @@ -237,7 +237,7 @@ run (const gchar *name, } else { - pvals.seed = (guint32) param[3].data.d_int32; + pvals.seed = (guint32) param[3].data.d_int32; pvals.turbulence = (gdouble) param[4].data.d_float; if (pvals.turbulence <= 0) @@ -248,6 +248,7 @@ run (const gchar *name, case GIMP_RUN_WITH_LAST_VALS: /* Possibly retrieve data */ gimp_get_data ("plug_in_plasma", &pvals); + if (pvals.random_seed) pvals.seed = g_random_int (); break; @@ -347,11 +348,6 @@ plasma_dialog (GimpDrawable *drawable, G_CALLBACK (plasma_seed_changed_callback), drawable); - g_signal_connect_swapped (GIMP_RANDOM_SEED_TOGGLE (seed), - "toggled", - G_CALLBACK (plasma_seed_changed_callback), - drawable); - adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 1, _("T_urbulence:"), SCALE_WIDTH, 0, pvals.turbulence, @@ -378,9 +374,6 @@ static void plasma_seed_changed_callback (GimpDrawable *drawable, gpointer data) { - if (pvals.random_seed) - pvals.seed = g_random_int (); - plasma (drawable, TRUE); } diff --git a/plug-ins/common/randomize.c b/plug-ins/common/randomize.c index 26c39413f6..45d56591eb 100644 --- a/plug-ins/common/randomize.c +++ b/plug-ins/common/randomize.c @@ -314,9 +314,9 @@ run (const gchar *name, /* * Make sure the drawable type is appropriate. */ - if (gimp_drawable_is_rgb(drawable->drawable_id) || - gimp_drawable_is_gray(drawable->drawable_id) || - gimp_drawable_is_indexed(drawable->drawable_id)) + if (gimp_drawable_is_rgb (drawable->drawable_id) || + gimp_drawable_is_gray (drawable->drawable_id) || + gimp_drawable_is_indexed (drawable->drawable_id)) { switch (run_mode) { @@ -324,8 +324,9 @@ run (const gchar *name, * If we're running interactively, pop up the dialog box. */ case GIMP_RUN_INTERACTIVE: - gimp_get_data(PLUG_IN_NAME[rndm_type - 1], &pivals); - if (!randomize_dialog()) /* return on Cancel */ + gimp_get_data (PLUG_IN_NAME[rndm_type - 1], &pivals); + + if (! randomize_dialog ()) /* return on Cancel */ return; break; /* @@ -341,10 +342,13 @@ run (const gchar *name, } else { - pivals.rndm_pct = (gdouble) param[3].data.d_float; + pivals.rndm_pct = (gdouble) param[3].data.d_float; pivals.rndm_rcount = (gdouble) param[4].data.d_float; - pivals.randomize = (gboolean) param[5].data.d_int32; - pivals.seed = (gint) param[6].data.d_int32; + pivals.randomize = (gboolean) param[5].data.d_int32; + pivals.seed = (gint) param[6].data.d_int32; + + if (pivals.randomize) + pivals.seed = g_random_int (); if ((rndm_type != RNDM_PICK && rndm_type != RNDM_SLUR && @@ -361,6 +365,9 @@ run (const gchar *name, */ case GIMP_RUN_WITH_LAST_VALS: gimp_get_data (PLUG_IN_NAME[rndm_type - 1], &pivals); + + if (pivals.randomize) + pivals.seed = g_random_int (); break; /* * Hopefully we never get here! @@ -368,6 +375,7 @@ run (const gchar *name, default: break; } + if (status == GIMP_PDB_SUCCESS) { /* @@ -379,16 +387,16 @@ run (const gchar *name, case RNDM_PICK: rndm_type_str = "pick"; break; case RNDM_SLUR: rndm_type_str = "slur"; break; } + sprintf (prog_label, "%s (%s)...", - gettext(RNDM_VERSION[rndm_type - 1]), - gettext(rndm_type_str)); - gimp_progress_init(prog_label); - gimp_tile_cache_ntiles(2 * (drawable->width / gimp_tile_width() + 1)); + gettext (RNDM_VERSION[rndm_type - 1]), + gettext (rndm_type_str)); + gimp_progress_init (prog_label); + gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1)); /* * Initialize the g_rand() function seed */ - if (!pivals.randomize) - g_rand_set_seed (gr, pivals.seed); + g_rand_set_seed (gr, pivals.seed); randomize (drawable, gr); /* @@ -396,15 +404,15 @@ run (const gchar *name, */ if (run_mode != GIMP_RUN_NONINTERACTIVE) { - gimp_displays_flush(); + gimp_displays_flush (); } /* * If we use the dialog popup, set the data for future use. */ if (run_mode == GIMP_RUN_INTERACTIVE) { - gimp_set_data(PLUG_IN_NAME[rndm_type - 1], &pivals, - sizeof(RandomizeVals)); + gimp_set_data (PLUG_IN_NAME[rndm_type - 1], &pivals, + sizeof (RandomizeVals)); } } } diff --git a/plug-ins/common/sinus.c b/plug-ins/common/sinus.c index 4f5424590a..6df431127d 100644 --- a/plug-ins/common/sinus.c +++ b/plug-ins/common/sinus.c @@ -72,7 +72,7 @@ typedef struct glong colors; GimpRGB col1; GimpRGB col2; - gboolean random_seed; + gboolean random_seed; } SinusVals; static SinusVals svals = @@ -87,7 +87,8 @@ static SinusVals svals = LINEAR, USE_COLORS, { 1.0, 1.0, 0.0, 1.0 }, - { 0.0, 0.0, 1.0, 1.0 } + { 0.0, 0.0, 1.0, 1.0 }, + FALSE }; typedef struct @@ -231,7 +232,7 @@ run (const gchar *name, thePreview = mw_preview_build_virgin(drawable); drawable_is_grayscale = gimp_drawable_is_gray (drawable->drawable_id); - if (!sinus_dialog()) + if (! sinus_dialog ()) return; break; @@ -257,12 +258,18 @@ run (const gchar *name, gimp_rgb_set_alpha (&svals.col2, param[13].data.d_float); svals.colorization = param[14].data.d_int32; svals.blend_power = param[15].data.d_float; + + if (svals.random_seed) + svals.seed = g_random_int (); } break; case GIMP_RUN_WITH_LAST_VALS: /* Possibly retrieve data */ gimp_get_data ("plug_in_sinus", &svals); + + if (svals.random_seed) + svals.seed = g_random_int (); break; default: @@ -313,8 +320,7 @@ prepare_coef (params *p) gr = g_rand_new (); - if (!svals.random_seed) - g_rand_set_seed (gr, svals.seed); + g_rand_set_seed (gr, svals.seed); switch (svals.colorization) { diff --git a/plug-ins/common/snoise.c b/plug-ins/common/snoise.c index 0b5f6a6754..421bb81193 100644 --- a/plug-ins/common/snoise.c +++ b/plug-ins/common/snoise.c @@ -231,12 +231,18 @@ run (const gchar *name, snvals.detail = param[6].data.d_int32; snvals.xsize = param[7].data.d_float; snvals.ysize = param[8].data.d_float; + + if (snvals.random_seed) + snvals.seed = g_random_int (); } break; case GIMP_RUN_WITH_LAST_VALS: /* Possibly retrieve data */ gimp_get_data ("plug_in_solid_noise", &snvals); + + if (snvals.random_seed) + snvals.seed = g_random_int (); break; default: @@ -358,8 +364,8 @@ solid_noise_init (void) GRand *gr; gr = g_rand_new (); - if (!snvals.random_seed) - g_rand_set_seed (gr, snvals.seed); + + g_rand_set_seed (gr, snvals.seed); /* Force sane parameters */ snvals.detail = CLAMP (snvals.detail, 0, 15); diff --git a/plug-ins/gflare/gflare.c b/plug-ins/gflare/gflare.c index 15118768a7..d56acce605 100644 --- a/plug-ins/gflare/gflare.c +++ b/plug-ins/gflare/gflare.c @@ -926,11 +926,6 @@ plugin_run (const gchar *name, break; case GIMP_RUN_NONINTERACTIVE: -#if 0 - printf("Currently non interactive call of gradient flare is not supported\n"); - status = GIMP_PDB_CALLING_ERROR; - break; -#endif if (nparams != 14) { status = GIMP_PDB_CALLING_ERROR; @@ -1883,8 +1878,7 @@ calc_place_sflare (void) prob[i] = sum2 / sum; } - if (!gflare->random_seed) - g_rand_set_seed (gr, gflare->sflare_seed); + g_rand_set_seed (gr, gflare->sflare_seed); for (n = 0; n < SFLARE_NUM; n++) { @@ -3696,7 +3690,6 @@ ed_make_page_sflare (GFlareEditor *ed, GtkObject *adj; gchar buf[256]; gint row; - gboolean randomize = FALSE; vbox = gtk_vbox_new (FALSE, 4); gtk_container_set_border_width (GTK_CONTAINER (vbox), 6); @@ -3853,14 +3846,10 @@ ed_make_page_sflare (GFlareEditor *ed, gtk_widget_show (label); seed = gimp_random_seed_new (&gflare->sflare_seed, &gflare->random_seed); - - entry = GTK_WIDGET (GIMP_RANDOM_SEED_SPINBUTTON (seed)); - gtk_box_pack_start (GTK_BOX (seed_hbox), seed, FALSE, TRUE, 0); gtk_widget_show (seed); - g_signal_connect (GTK_SPIN_BUTTON (entry)->adjustment, - "value_changed", + g_signal_connect (GIMP_RANDOM_SEED_SPINBUTTON_ADJ (seed), "value_changed", G_CALLBACK (ed_preview_update), NULL); diff --git a/plug-ins/maze/algorithms.c b/plug-ins/maze/algorithms.c index f599325c1b..39b66287f0 100644 --- a/plug-ins/maze/algorithms.c +++ b/plug-ins/maze/algorithms.c @@ -280,14 +280,9 @@ prim(gint pos, gchar *maz, guint x, guint y) guint progress=0, max_progress; char d, i; guint c=0; - gint rnd; + gint rnd = mvals.seed; - if (mvals.random_seed) - rnd = g_random_int (); - else - rnd = mvals.seed; - - g_rand_set_seed (gr, rnd); + g_rand_set_seed (gr, rnd); gimp_progress_init (_("Constructing maze using Prim's Algorithm...")); @@ -461,14 +456,9 @@ prim_tileable(gchar *maz, guint x, guint y) guint progress=0, max_progress; char d, i; guint c=0; - gint rnd; + gint rnd = mvals.seed; - if (mvals.random_seed) - rnd = g_random_int (); - else - rnd = mvals.seed; - - g_rand_set_seed (gr, rnd); + g_rand_set_seed (gr, rnd); gimp_progress_init (_("Constructing tileable maze using Prim's Algorithm...")); diff --git a/plug-ins/maze/maze.c b/plug-ins/maze/maze.c index 57fd5a83aa..726b3f9953 100644 --- a/plug-ins/maze/maze.c +++ b/plug-ins/maze/maze.c @@ -212,7 +212,8 @@ run (const gchar *name, /* The interface needs to know the dimensions of the image... */ gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); - sel_w=x2-x1; sel_h=y2-y1; + sel_w = x2 - x1; + sel_h = y2 - y1; /* Acquire info with a dialog */ if (! maze_dialog ()) @@ -235,12 +236,18 @@ run (const gchar *name, mvals.seed = (guint32) param[7].data.d_int32; mvals.multiple = (gint16) param[8].data.d_int16; mvals.offset = (gint16) param[9].data.d_int16; + + if (mvals.random_seed) + mvals.seed = g_random_int (); } break; case GIMP_RUN_WITH_LAST_VALS: /* Possibly retrieve data */ gimp_get_data ("plug_in_maze", &mvals); + + if (mvals.random_seed) + mvals.seed = g_random_int (); break; default: @@ -367,10 +374,7 @@ maze (GimpDrawable * drawable) switch (mvals.algorithm) { case DEPTH_FIRST: - if (mvals.random_seed) - mazegen_tileable (0, maz, mw, mh, g_random_int ()); - else - mazegen_tileable (0, maz, mw, mh, mvals.seed); + mazegen_tileable (0, maz, mw, mh, mvals.seed); break; case PRIMS_ALGORITHM: @@ -400,10 +404,7 @@ maze (GimpDrawable * drawable) switch (mvals.algorithm) { case DEPTH_FIRST: - if (mvals.random_seed) - mazegen (maz_yy+maz_xx, maz, mw, mh, g_random_int()); - else - mazegen (maz_yy+maz_xx, maz, mw, mh, mvals.seed); + mazegen (maz_yy+maz_xx, maz, mw, mh, mvals.seed); break; case PRIMS_ALGORITHM: @@ -425,10 +426,7 @@ maze (GimpDrawable * drawable) switch (mvals.algorithm) { case DEPTH_FIRST: - if (mvals.random_seed) - mazegen (pos, maz, mw, mh, g_random_int ()); - else - mazegen (pos, maz, mw, mh, mvals.seed); + mazegen (pos, maz, mw, mh, mvals.seed); break; case PRIMS_ALGORITHM: