Commit cd28dee8 authored by Dave Neary's avatar Dave Neary Committed by David Neary
Browse files

configure.in app/core/gimpbrushpipe.c app/gui/about-dialog.c



2002-11-20  Dave Neary  <bolsh@gimp.org>

        * configure.in
        * app/core/gimpbrushpipe.c
        * app/gui/about-dialog.c
        * app/paint-funcs/paint-funcs-generic.h
        * app/paint-funcs/paint-funcs.c
        * libgimpmath/gimpmath.h
        * libgimpwidgets/gimpwidgets.c
        * plug-ins/common/CML_explorer.c
        * plug-ins/common/blur.c
        * plug-ins/common/cubism.c
        * plug-ins/common/gee.c
        * plug-ins/common/gee_zoom.c
        * plug-ins/common/gqbist.c
        * plug-ins/common/jigsaw.c
        * plug-ins/common/lic.c
        * plug-ins/common/noisify.c
        * plug-ins/common/nova.c
        * plug-ins/common/papertile.c
        * plug-ins/common/plasma.c
        * plug-ins/common/randomize.c
        * plug-ins/common/sample_colorize.c
        * plug-ins/common/scatter_hsv.c
        * plug-ins/common/shift.c
        * plug-ins/common/sinus.c
        * plug-ins/common/smooth_palette.c
        * plug-ins/common/snoise.c
        * plug-ins/common/sparkle.c
        * plug-ins/common/spheredesigner.c
        * plug-ins/common/spread.c
        * plug-ins/common/warp.c
        * plug-ins/common/wind.c
        * plug-ins/flame/cmap.c
        * plug-ins/flame/flame.c
        * plug-ins/flame/libifs.c
        * plug-ins/gflare/gflare.c
        * plug-ins/gimpressionist/gimpressionist.c
        * plug-ins/gimpressionist/gimpressionist.h
        * plug-ins/gimpressionist/plasma.c
        * plug-ins/gimpressionist/repaint.c
        * plug-ins/ifscompose/ifscompose_utils.c
        * plug-ins/maze/algorithms.c
        * plug-ins/maze/maze.c
        * plug-ins/maze/maze.h
        * plug-ins/mosaic/mosaic.c: Change all occurrences of RAND_MAX,
        G_MAXRAND, rand(), srand(), lrand48(), srand48(), random(),
        srandom(), RAND_FUNC and SRAND_FUNC to the appropriate g_rand*
        equivalent. Programs which require seed setting for reproducible
        results, and anything in the core, gets a dedicated GRand * for
        the lifetime required. Programs which only ever used random
        numbers for tossing a coin, rolling a dice, etc use g_random
        functions. For the rest, judgement was used. Where it was easy, a
        GRand * object was used and g_rand_* functions were
        preferred. This fixes bug #67386 in HEAD.
parent ea2dd584
2002-11-20 Dave Neary <bolsh@gimp.org>
* configure.in
* app/core/gimpbrushpipe.c
* app/gui/about-dialog.c
* app/paint-funcs/paint-funcs-generic.h
* app/paint-funcs/paint-funcs.c
* libgimpmath/gimpmath.h
* libgimpwidgets/gimpwidgets.c
* plug-ins/common/CML_explorer.c
* plug-ins/common/blur.c
* plug-ins/common/cubism.c
* plug-ins/common/gee.c
* plug-ins/common/gee_zoom.c
* plug-ins/common/gqbist.c
* plug-ins/common/jigsaw.c
* plug-ins/common/lic.c
* plug-ins/common/noisify.c
* plug-ins/common/nova.c
* plug-ins/common/papertile.c
* plug-ins/common/plasma.c
* plug-ins/common/randomize.c
* plug-ins/common/sample_colorize.c
* plug-ins/common/scatter_hsv.c
* plug-ins/common/shift.c
* plug-ins/common/sinus.c
* plug-ins/common/smooth_palette.c
* plug-ins/common/snoise.c
* plug-ins/common/sparkle.c
* plug-ins/common/spheredesigner.c
* plug-ins/common/spread.c
* plug-ins/common/warp.c
* plug-ins/common/wind.c
* plug-ins/flame/cmap.c
* plug-ins/flame/flame.c
* plug-ins/flame/libifs.c
* plug-ins/gflare/gflare.c
* plug-ins/gimpressionist/gimpressionist.c
* plug-ins/gimpressionist/gimpressionist.h
* plug-ins/gimpressionist/plasma.c
* plug-ins/gimpressionist/repaint.c
* plug-ins/ifscompose/ifscompose_utils.c
* plug-ins/maze/algorithms.c
* plug-ins/maze/maze.c
* plug-ins/maze/maze.h
* plug-ins/mosaic/mosaic.c: Change all occurrences of RAND_MAX,
G_MAXRAND, rand(), srand(), lrand48(), srand48(), random(),
srandom(), RAND_FUNC and SRAND_FUNC to the appropriate g_rand*
equivalent. Programs which require seed setting for reproducible
results, and anything in the core, gets a dedicated GRand * for
the lifetime required. Programs which only ever used random
numbers for tossing a coin, rolling a dice, etc use g_random
functions. For the rest, judgement was used. Where it was easy, a
GRand * object was used and g_rand_* functions were
preferred. This fixes bug #67386 in HEAD.
2002-11-19 Maurits Rijk <lpeek.mrijk@consunet.nl>
 
* plug-ins/gimpressionist/brush.c
......
......@@ -211,6 +211,10 @@ gimp_brush_pipe_select_brush (GimpBrush *brush,
GimpBrushPipe *pipe;
gint i, brushix, ix;
gdouble angle;
GRand *gr;
/* Initialise random number generator */
gr = g_rand_new();
pipe = GIMP_BRUSH_PIPE (brush);
......@@ -239,7 +243,7 @@ gimp_brush_pipe_select_brush (GimpBrush *brush,
break;
case PIPE_SELECT_RANDOM:
/* This probably isn't the right way */
ix = rand () % pipe->rank[i];
ix = g_rand_int_range (gr, 0, pipe->rank[i]);
break;
case PIPE_SELECT_PRESSURE:
ix = RINT (cur_coords->pressure * (pipe->rank[i] - 1));
......@@ -264,6 +268,8 @@ gimp_brush_pipe_select_brush (GimpBrush *brush,
pipe->current = pipe->brushes[brushix];
g_rand_free (gr);
return GIMP_BRUSH (pipe->current);
}
......
......@@ -211,6 +211,10 @@ gimp_brush_pipe_select_brush (GimpBrush *brush,
GimpBrushPipe *pipe;
gint i, brushix, ix;
gdouble angle;
GRand *gr;
/* Initialise random number generator */
gr = g_rand_new();
pipe = GIMP_BRUSH_PIPE (brush);
......@@ -239,7 +243,7 @@ gimp_brush_pipe_select_brush (GimpBrush *brush,
break;
case PIPE_SELECT_RANDOM:
/* This probably isn't the right way */
ix = rand () % pipe->rank[i];
ix = g_rand_int_range (gr, 0, pipe->rank[i]);
break;
case PIPE_SELECT_PRESSURE:
ix = RINT (cur_coords->pressure * (pipe->rank[i] - 1));
......@@ -264,6 +268,8 @@ gimp_brush_pipe_select_brush (GimpBrush *brush,
pipe->current = pipe->brushes[brushix];
g_rand_free (gr);
return GIMP_BRUSH (pipe->current);
}
......
......@@ -122,6 +122,9 @@ about_dialog_create (void)
gint height;
gint i;
gchar *label_text;
GRand *gr;
gr = g_rand_new();
if (! about_dialog)
{
......@@ -273,7 +276,7 @@ about_dialog_create (void)
{
gint j;
j = rand() % nscroll_texts;
j = g_rand_int_range (gr, 0, nscroll_texts);
if (i != j)
{
gint t;
......@@ -283,7 +286,7 @@ about_dialog_create (void)
shuffle_array[i] = t;
}
}
cur_scroll_text = rand() % nscroll_texts;
cur_scroll_text = g_rand_int_range(gr, 0, nscroll_texts);
pango_layout_set_text (scroll_layout,
scroll_text[cur_scroll_text], -1);
......@@ -292,6 +295,8 @@ about_dialog_create (void)
gtk_window_present (GTK_WINDOW (about_dialog));
g_rand_free (gr);
return about_dialog;
}
......@@ -302,6 +307,9 @@ about_dialog_load_logo (GtkWidget *window)
GdkPixbuf *pixbuf;
GdkGC *gc;
gint i, j, k;
GRand *gr;
gr = g_rand_new();
if (logo_pixmap)
return TRUE;
......@@ -350,11 +358,11 @@ about_dialog_load_logo (GtkWidget *window)
dissolve_map = g_new (guchar, dissolve_width * dissolve_height);
srand (time (NULL));
for (i = 0, k = 0; i < dissolve_height; i++)
for (j = 0; j < dissolve_width; j++, k++)
dissolve_map[k] = rand () % ANIMATION_STEPS;
dissolve_map[k] = g_rand_int_range (gr, 0, ANIMATION_STEPS);
g_rand_free (gr);
return TRUE;
}
......
......@@ -122,6 +122,9 @@ about_dialog_create (void)
gint height;
gint i;
gchar *label_text;
GRand *gr;
gr = g_rand_new();
if (! about_dialog)
{
......@@ -273,7 +276,7 @@ about_dialog_create (void)
{
gint j;
j = rand() % nscroll_texts;
j = g_rand_int_range (gr, 0, nscroll_texts);
if (i != j)
{
gint t;
......@@ -283,7 +286,7 @@ about_dialog_create (void)
shuffle_array[i] = t;
}
}
cur_scroll_text = rand() % nscroll_texts;
cur_scroll_text = g_rand_int_range(gr, 0, nscroll_texts);
pango_layout_set_text (scroll_layout,
scroll_text[cur_scroll_text], -1);
......@@ -292,6 +295,8 @@ about_dialog_create (void)
gtk_window_present (GTK_WINDOW (about_dialog));
g_rand_free (gr);
return about_dialog;
}
......@@ -302,6 +307,9 @@ about_dialog_load_logo (GtkWidget *window)
GdkPixbuf *pixbuf;
GdkGC *gc;
gint i, j, k;
GRand *gr;
gr = g_rand_new();
if (logo_pixmap)
return TRUE;
......@@ -350,11 +358,11 @@ about_dialog_load_logo (GtkWidget *window)
dissolve_map = g_new (guchar, dissolve_width * dissolve_height);
srand (time (NULL));
for (i = 0, k = 0; i < dissolve_height; i++)
for (j = 0; j < dissolve_width; j++, k++)
dissolve_map[k] = rand () % ANIMATION_STEPS;
dissolve_map[k] = g_rand_int_range (gr, 0, ANIMATION_STEPS);
g_rand_free (gr);
return TRUE;
}
......
......@@ -66,7 +66,7 @@ struct apply_layer_mode_struct
static const guchar no_mask = OPAQUE_OPACITY;
static guchar add_lut[511];
static gint random_table[RANDOM_TABLE_SIZE];
static gint32 random_table[RANDOM_TABLE_SIZE];
void
color_pixels (guchar *dest,
......@@ -957,33 +957,16 @@ dissolve_pixels (const guchar *src,
guint has_alpha)
{
gint alpha, b;
gint rand_val;
gint32 rand_val;
GRand *gr;
gr = g_rand_new_with_seed (random_table[y % RANDOM_TABLE_SIZE]);
/* Ignore a few random values (this probably isn't necessary
* when using g_rand_*) */
for (b = 0; b < x; b ++)
g_rand_int(gr);
#if defined(ENABLE_MP) && defined(__GLIBC__)
/* The glibc 2.1 documentation recommends using the SVID random functions
* instead of rand_r
*/
struct drand48_data seed;
glong temp_val;
srand48_r (random_table[y % RANDOM_TABLE_SIZE], &seed);
for (b = 0; b < x; b++)
lrand48_r (&seed, &temp_val);
#elif defined(ENABLE_MP) && !defined(__GLIBC__)
/* If we are running with multiple threads rand_r give _much_ better
* performance than rand
*/
guint seed;
seed = random_table[y % RANDOM_TABLE_SIZE];
for (b = 0; b < x; b++)
rand_r (&seed);
#else
/* Set up the random number generator */
srand (random_table[y % RANDOM_TABLE_SIZE]);
for (b = 0; b < x; b++)
rand ();
#endif
alpha = db - 1;
while (length--)
......@@ -993,14 +976,8 @@ dissolve_pixels (const guchar *src,
dest[b] = src[b];
/* dissolve if random value is > opacity */
#if defined(ENABLE_MP) && defined(__GLIBC__)
lrand48_r (&seed, &temp_val);
rand_val = temp_val & 0xff;
#elif defined(ENABLE_MP) && !defined(__GLIBC__)
rand_val = (rand_r (&seed) & 0xff);
#else
rand_val = (rand () & 0xff);
#endif
rand_val = g_rand_int_range (gr, 0, 256);
if (has_alpha)
dest[alpha] = (rand_val > src[alpha]) ? 0 : src[alpha];
else
......@@ -1009,6 +986,9 @@ dissolve_pixels (const guchar *src,
dest += db;
src += sb;
}
g_rand_free (gr);
}
static inline void
......
......@@ -435,12 +435,13 @@ void
paint_funcs_setup (void)
{
guint i;
GRand *gr;
/* generate a table of random seeds */
srand (RANDOM_SEED);
gr = g_rand_new_with_seed (RANDOM_SEED);
for (i = 0; i < RANDOM_TABLE_SIZE; i++)
random_table[i] = rand ();
random_table[i] = g_rand_int (gr);
for (i = 0; i < 256; i++)
add_lut[i] = i;
......@@ -468,6 +469,7 @@ paint_funcs_setup (void)
#endif /* GIMP_ENABLE_MMX */
#endif /* HAVE_ASM_MMX */
g_rand_free(gr);
}
void
......
......@@ -487,26 +487,6 @@ if test $gimp_ok = no; then
fi
##############################################################
# Check for random number functions (FIXME: should use g_rand)
##############################################################
dnl Check for random number functions
LIBUCB=""
AC_CHECK_FUNC(random, [
AC_DEFINE(RAND_FUNC, random, [Define a random function. FIXME!])
AC_DEFINE(SRAND_FUNC, srandom, [Define a srandom function. FIXME!])],
[AC_CHECK_LIB(ucb, random, [
AC_DEFINE(RAND_FUNC, random)
AC_DEFINE(SRAND_FUNC, srandom)
LIBUCB='-lucb'],
[AC_CHECK_FUNC(lrand48,
[AC_DEFINE(RAND_FUNC, lrand48) AC_DEFINE(SRAND_FUNC, srand48)],
[AC_DEFINE(RAND_FUNC, rand) AC_DEFINE(SRAND_FUNC, srand)])])])
AC_SUBST(LIBUCB)
#############################
# Threads and multi processor
#############################
......
......@@ -46,12 +46,6 @@ G_BEGIN_DECLS
* Include this instead of just <math.h>.
*/
#ifndef RAND_MAX
#define G_MAXRAND G_MAXINT
#else
#define G_MAXRAND RAND_MAX
#endif
/* Use RINT() instead of rint() */
#ifdef HAVE_RINT
#define RINT(x) rint(x)
......
......@@ -965,7 +965,7 @@ gimp_random_seed_new (gint *seed,
hbox = gtk_hbox_new (FALSE, 4);
spinbutton = gimp_spin_button_new (&adj, *seed,
0, G_MAXRAND, 1, 10, 0, 1, 0);
0, (guint32) -1 , 1, 10, 0, 1, 0);
gtk_box_pack_start (GTK_BOX (hbox), spinbutton, FALSE, FALSE, 0);
g_signal_connect (G_OBJECT (adj), "value_changed",
G_CALLBACK (gimp_int_adjustment_update),
......
......@@ -77,7 +77,6 @@
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h> /* for seed of random number */
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
......@@ -102,7 +101,6 @@
#define PREVIEW_WIDTH 64
#define PREVIEW_HEIGHT 220
#define RANDOM ((gdouble) ((gdouble) rand ()/((gdouble) G_MAXRAND)))
#define CANNONIZE(p, x) (255*(((p).range_h - (p).range_l)*(x) + (p).range_l))
#define HCANNONIZE(p, x) (254*(((p).range_h - (p).range_l)*(x) + (p).range_l))
#define POS_IN_TORUS(i,size) ((i < 0) ? size + i : ((size <= i) ? i - size : i))
......@@ -559,6 +557,7 @@ CML_main_function (gint preview_p)
gdouble *hues, *sats, *vals;
gdouble *newh, *news, *newv;
gdouble *haux, *saux, *vaux;
GRand *gr;
/* open THE drawable */
drawable = gimp_drawable_get (drawable_id);
......@@ -630,23 +629,16 @@ CML_main_function (gint preview_p)
width_by_pixel, height_by_pixel,
FALSE, FALSE);
/* Note: Why seed is modulo of (1 << 15)? I use iscale_entry to control
the value. Adjustment widget in Scale uses gdouble(?). But it is not
precise to hold a long integer! Thus, in updating scale, the value will
be rounded. Since I can't imagine another solution, I use short (16bit)
integer as the seed of random number sequence. This approach works on
my i386-based Linux.
*/
if (VALS.initial_value < CML_INITIAL_RANDOM_FROM_SEED)
VALS.seed = time (NULL) % (1 << 15);
srand (VALS.seed);
gr = g_rand_new ();
if (VALS.initial_value == CML_INITIAL_RANDOM_FROM_SEED)
g_rand_set_seed (gr, VALS.seed);
for (index = 0; index < cell_num; index++)
{
switch (VALS.hue.arrange)
{
case RAND_POWER0:
haux [index] = RANDOM * 10;
haux [index] = g_rand_double_range (gr, 0, 10);
break;
case RAND_POWER2:
case MULTIPLY_GRADIENT:
......@@ -654,13 +646,13 @@ CML_main_function (gint preview_p)
break;
case RAND_POWER1:
case MULTIPLY_RANDOM0:
haux [index] = RANDOM;
haux [index] = g_rand_double (gr);
break;
case MULTIPLY_RANDOM1:
haux [index] = RANDOM * 2;
haux [index] = g_rand_double_range (gr, 0, 2);
break;
case RAND_AND_P:
haux [index] = (index % (2 * VALS.hue.diffusion_dist) == 0) ? (RANDOM) : VALS.hue.power;
haux [index] = (index % (2 * VALS.hue.diffusion_dist) == 0) ? g_rand_double (gr) : VALS.hue.power;
break;
default:
haux [index] = VALS.hue.power;
......@@ -669,7 +661,7 @@ CML_main_function (gint preview_p)
switch (VALS.sat.arrange)
{
case RAND_POWER0:
saux [index] = RANDOM * 10;
saux [index] = g_rand_double_range (gr, 0, 10);
break;
case RAND_POWER2:
case MULTIPLY_GRADIENT:
......@@ -677,13 +669,13 @@ CML_main_function (gint preview_p)
break;
case RAND_POWER1:
case MULTIPLY_RANDOM0:
saux [index] = RANDOM;
saux [index] = g_rand_double (gr);
break;
case MULTIPLY_RANDOM1:
saux [index] = RANDOM * 2;
saux [index] = g_rand_double_range (gr, 0, 2);
break;
case RAND_AND_P:
saux [index] = (index % (2 * VALS.sat.diffusion_dist) == 0) ? (RANDOM) : VALS.sat.power;
saux [index] = (index % (2 * VALS.sat.diffusion_dist) == 0) ? g_rand_double (gr) : VALS.sat.power;
break;
default:
saux [index] = VALS.sat.power;
......@@ -692,7 +684,7 @@ CML_main_function (gint preview_p)
switch (VALS.val.arrange)
{
case RAND_POWER0:
vaux [index] = RANDOM * 10;
vaux [index] = g_rand_double_range (gr, 0, 10);
break;
case RAND_POWER2:
case MULTIPLY_GRADIENT:
......@@ -700,13 +692,13 @@ CML_main_function (gint preview_p)
break;
case RAND_POWER1:
case MULTIPLY_RANDOM0:
vaux [index] = RANDOM;
vaux [index] = g_rand_double (gr);
break;
case MULTIPLY_RANDOM1:
vaux [index] = RANDOM * 2;
vaux [index] = g_rand_double_range (gr, 0, 2);
break;
case RAND_AND_P:
vaux [index] = (index % (2 * VALS.val.diffusion_dist) == 0) ? (RANDOM) : VALS.val.power;
vaux [index] = (index % (2 * VALS.val.diffusion_dist) == 0) ? g_rand_double (gr) : VALS.val.power;
break;
default:
vaux [index] = VALS.val.power;
......@@ -731,13 +723,13 @@ CML_main_function (gint preview_p)
break;
case CML_INITIAL_RANDOM_INDEPENDENT:
case CML_INITIAL_RANDOM_FROM_SEED:
hues[index] = RANDOM;
sats[index] = RANDOM;
vals[index] = RANDOM;
hues[index] = g_rand_double (gr);
sats[index] = g_rand_double (gr);
vals[index] = g_rand_double (gr);
break;
case CML_INITIAL_RANDOM_SHARED:
case CML_INITIAL_RANDOM_FROM_SEED_SHARED:
hues[index] = sats[index] = vals[index] = RANDOM;
hues[index] = sats[index] = vals[index] = g_rand_double (gr);
break;
}
}
......@@ -863,6 +855,8 @@ CML_main_function (gint preview_p)
gimp_drawable_detach (drawable);
}
g_rand_free (gr);
return GIMP_PDB_SUCCESS;
}
......@@ -930,7 +924,9 @@ CML_next_value (gdouble *vec,
gdouble hold_rate = 1 - param->mod_rate;
gdouble env_factor = 0;
gint index;
GRand *gr;
gr = g_rand_new();
self_mod_rate = (1 - param->env_sensitivity - param->ch_sensitivity);
switch (param->arrange)
......@@ -1001,9 +997,9 @@ CML_next_value (gdouble *vec,
break;
}
/* finalize */
if (RANDOM < param->mutation_rate)
if (g_rand_double (gr) < param->mutation_rate)
{
val += ((RANDOM < 0.5) ? -1.0 : 1.0) * param->mutation_dist * RANDOM;
val += ((g_rand_double (gr) < 0.5) ? -1.0 : 1.0) * param->mutation_dist * g_rand_double (gr);
}
if (param->cyclic_range)
{
......@@ -1016,6 +1012,9 @@ CML_next_value (gdouble *vec,
/* The range of val should be [0,1], not [0,1).
Cannonization shuold be done in color mapping phase. */
val = CLAMP (val, 0.0, 1);
g_rand_free (gr);
return val;
}
#undef AVE_DIST
......@@ -1387,7 +1386,7 @@ CML_explorer_dialog (void)
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
_("Seed:"), SCALE_WIDTH, 0,
VALS.seed, 0, G_MAXRAND, 1, 10, 0,
VALS.seed, 0, (guint32) -1, 1, 10, 0,
TRUE, 0, 0,
NULL, NULL);
CML_explorer_int_entry_init (&widget_pointers[3][3],
......
......@@ -81,7 +81,7 @@
#define PLUG_IN_NAME "plug_in_blur"
#define BLUR_VERSION "Blur 2.0"
#define SEED_TIME 10
#define SEED_DEFAULT 10
#define SEED_USER 11
#define SCALE_WIDTH 100
......@@ -96,15 +96,15 @@ typedef struct
{
gdouble blur_pct; /* likelihood of randomization (as %age) */
gdouble blur_rcount; /* repeat count */
gint seed_type; /* seed init. type - current time or user value */
gint blur_seed; /* seed value for rand() function */
gint seed_type; /* seed init. type - default or user value */
gint blur_seed; /* seed value for g_random_set_seed() function */
} BlurVals;
static BlurVals pivals =
{
100.0,
1.0,
SEED_TIME,
SEED_DEFAULT,
0,
};
......@@ -284,7 +284,7 @@ run (gchar *name,
pivals.blur_rcount = (gdouble) MAX (1.0, pivals.blur_rcount);
pivals.seed_type = (gint) param[5].data.d_int32;
pivals.seed_type = (gint) MIN (SEED_USER, param[5].data.d_int32);
pivals.seed_type = (gint) MAX (SEED_TIME, param[5].data.d_int32);
pivals.seed_type = (gint) MAX (SEED_DEFAULT, param[5].data.d_int32);
pivals.blur_seed = (gint) param[6].data.d_int32;
}