Commit 6776254b authored by Maurits Rijk's avatar Maurits Rijk
Browse files

Code clean-up for several plug-ins.

parent 9a7409af
2002-12-15 Maurits Rijk <lpeek.mrijk@consunet.nl>
* plug-ins/common/displace.c
* plug-ins/common/ripple.c
* plug-ins/common/shift.c
* plug-ins/common/spread.c
* plug-ins/common/tileit.c
* plug-ins/common/noisify.c: code clean-up, fixed memory leak,
better use of libgimp routines.
2002-12-15 Sven Neumann <sven@gimp.org>
 
* app/gui/user-install-dialog.c: set text and base colors as well
......
......@@ -58,10 +58,6 @@
#define ENTRY_WIDTH 75
#define TILE_CACHE_SIZE 48
#define WRAP 0
#define SMEAR 1
#define BLACK 2
typedef struct
{
gdouble amount_x;
......@@ -91,19 +87,6 @@ static void run (gchar *name,
static void displace (GimpDrawable *drawable);
static gint displace_dialog (GimpDrawable *drawable);
static GimpTile *displace_pixel (GimpDrawable *drawable,
GimpTile *tile,
gint width,
gint height,
gint x1,
gint y1,
gint x2,
gint y2,
gint x,
gint y,
gint *row,
gint *col,
guchar *pixel);
static gint displace_map_constrain (gint32 image_id,
gint32 drawable_id,
......@@ -136,7 +119,7 @@ static DisplaceVals dvals =
TRUE, /* do_y */
-1, /* displace_map_x */
-1, /* displace_map_y */
WRAP /* displace_type */
PIXEL_WRAP /* displace_type */
};
static DisplaceInterface dint =
......@@ -409,13 +392,13 @@ displace_dialog (GimpDrawable *drawable)
gtk_widget_show (toggle);
g_object_set_data (G_OBJECT (toggle), "gimp-item-data",
GINT_TO_POINTER (WRAP));
GINT_TO_POINTER (PIXEL_WRAP));
g_signal_connect (G_OBJECT (toggle), "toggled",
G_CALLBACK (gimp_radio_button_update),
&dvals.displace_type);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
dvals.displace_type == WRAP);
dvals.displace_type == PIXEL_WRAP);
toggle = gtk_radio_button_new_with_mnemonic (group, _("_Smear"));
group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
......@@ -423,13 +406,13 @@ displace_dialog (GimpDrawable *drawable)
gtk_widget_show (toggle);
g_object_set_data (G_OBJECT (toggle), "gimp-item-data",
GINT_TO_POINTER (SMEAR));
GINT_TO_POINTER (PIXEL_SMEAR));
g_signal_connect (G_OBJECT (toggle), "toggled",
G_CALLBACK (gimp_radio_button_update),
&dvals.displace_type);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
dvals.displace_type == SMEAR);
dvals.displace_type == PIXEL_SMEAR);
toggle = gtk_radio_button_new_with_mnemonic (group, _("_Black"));
group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
......@@ -437,13 +420,13 @@ displace_dialog (GimpDrawable *drawable)
gtk_widget_show (toggle);
g_object_set_data (G_OBJECT (toggle), "gimp-item-data",
GINT_TO_POINTER (BLACK));
GINT_TO_POINTER (PIXEL_BLACK));
g_signal_connect (G_OBJECT (toggle), "toggled",
G_CALLBACK (gimp_radio_button_update),
&dvals.displace_type);
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
dvals.displace_type == BLACK);
dvals.displace_type == PIXEL_BLACK);
gtk_widget_show (toggle_hbox);
gtk_widget_show (table);
......@@ -466,10 +449,8 @@ displace (GimpDrawable *drawable)
GimpPixelRgn dest_rgn;
GimpPixelRgn map_x_rgn;
GimpPixelRgn map_y_rgn;
GimpTile * tile = NULL;
gint row = -1;
gint col = -1;
gpointer pr;
GimpPixelFetcher *pft;
gint width;
gint height;
......@@ -502,7 +483,8 @@ displace (GimpDrawable *drawable)
mxrow = NULL;
myrow = NULL;
/* Get selection area */
pft = gimp_pixel_fetcher_new (drawable);
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
width = drawable->width;
......@@ -600,19 +582,23 @@ displace (GimpDrawable *drawable)
/* Calculations complete; now copy the proper pixel */
if (needx >= 0.0)
xi = (int) needx;
xi = (int) needx;
else
xi = -((int) -needx + 1);
xi = -((int) -needx + 1);
if (needy >= 0.0)
yi = (int) needy;
yi = (int) needy;
else
yi = -((int) -needy + 1);
yi = -((int) -needy + 1);
tile = displace_pixel (drawable, tile, width, height, x1, y1, x2, y2, xi, yi, &row, &col, pixel[0]);
tile = displace_pixel (drawable, tile, width, height, x1, y1, x2, y2, xi + 1, yi, &row, &col, pixel[1]);
tile = displace_pixel (drawable, tile, width, height, x1, y1, x2, y2, xi, yi + 1, &row, &col, pixel[2]);
tile = displace_pixel (drawable, tile, width, height, x1, y1, x2, y2, xi + 1, yi + 1, &row, &col, pixel[3]);
gimp_pixel_fetcher_get_pixel2 (pft, xi, yi,
dvals.displace_type, pixel[0]);
gimp_pixel_fetcher_get_pixel2 (pft, xi + 1, yi,
dvals.displace_type, pixel[1]);
gimp_pixel_fetcher_get_pixel2 (pft, xi, yi + 1,
dvals.displace_type, pixel[2]);
gimp_pixel_fetcher_get_pixel2 (pft, xi + 1, yi + 1,
dvals.displace_type, pixel[3]);
for (k = 0; k < bytes; k++)
{
......@@ -638,8 +624,7 @@ displace (GimpDrawable *drawable)
gimp_progress_update ((double) progress / (double) max_progress);
} /* for */
if (tile)
gimp_tile_unref (tile, FALSE);
gimp_pixel_fetcher_destroy (pft);
/* detach from the map drawables */
if (dvals.do_x)
......@@ -651,8 +636,7 @@ displace (GimpDrawable *drawable)
gimp_drawable_flush (drawable);
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
gimp_drawable_update (drawable->drawable_id, x1, y1, (x2 - x1), (y2 - y1));
} /* displace */
}
static gdouble
displace_map_give_value (guchar *pt,
......@@ -670,79 +654,9 @@ displace_map_give_value (guchar *pt,
{
val_alpha = pt[bytes - 1];
ret = ((ret - 127.5) * val_alpha / 255.0) + 127.5;
};
return (ret);
}
static GimpTile *
displace_pixel (GimpDrawable *drawable,
GimpTile *tile,
gint width,
gint height,
gint x1,
gint y1,
gint x2,
gint y2,
gint x,
gint y,
gint *row,
gint *col,
guchar *pixel)
{
static guchar empty_pixel[4] = {0, 0, 0, 0};
guchar *data;
gint b;
/* Tile the image. */
if (dvals.displace_type == WRAP)
{
if (x < 0)
x = width - (-x % width);
else
x %= width;
if (y < 0)
y = height - (-y % height);
else
y %= height;
}
/* Smear out the edges of the image by repeating pixels. */
else if (dvals.displace_type == SMEAR)
{
if (x < 0)
x = 0;
else if (x > width - 1)
x = width - 1;
if (y < 0)
y = 0;
else if (y > height - 1)
y = height - 1;
}
if (x >= x1 && y >= y1 && x < x2 && y < y2)
{
if ((x >> 6 != *col) || (y >> 6 != *row))
{
*col = x / 64;
*row = y / 64;
if (tile)
gimp_tile_unref (tile, FALSE);
tile = gimp_drawable_get_tile (drawable, FALSE, *row, *col);
gimp_tile_ref (tile);
}
data = tile->data + tile->bpp * (tile->ewidth * (y % 64) + (x % 64));
}
else
data = empty_pixel;
for (b = 0; b < drawable->bpp; b++)
pixel[b] = data[b];
return tile;
return ret;
}
/* Displace interface functions */
......
......@@ -37,12 +37,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#ifdef __GNUC__
#warning GTK_DISABLE_DEPRECATED
#endif
#undef GTK_DISABLE_DEPRECATED
#include <gtk/gtk.h>
......@@ -54,7 +48,6 @@
#define SCALE_WIDTH 125
#define TILE_CACHE_SIZE 16
#define PREVIEW_SIZE 128
typedef struct
{
......@@ -82,10 +75,6 @@ static void noisify (GimpDrawable *drawable,
gboolean preview_mode);
static gdouble gauss (GRand *gr);
static void fill_preview (GtkWidget *preview_widget,
GimpDrawable *drawable);
static GtkWidget *preview_widget (GimpDrawable *drawable);
static gint noisify_dialog (GimpDrawable *drawable,
gint channels);
static void noisify_ok_callback (GtkWidget *widget,
......@@ -114,11 +103,8 @@ static NoisifyInterface noise_int =
FALSE /* run */
};
static GtkWidget *preview;
static guchar *preview_cache;
static gint preview_cache_rowstride;
static gint preview_cache_bpp;
static GimpRunMode run_mode;
static GimpFixMePreview *preview;
MAIN ()
......@@ -159,7 +145,6 @@ run (gchar *name,
{
static GimpParam values[1];
GimpDrawable *drawable;
GimpRunMode run_mode;
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
run_mode = param[0].data.d_int32;
......@@ -231,7 +216,6 @@ run (gchar *name,
/* Store data */
if (run_mode == GIMP_RUN_INTERACTIVE) {
gimp_set_data ("plug_in_noisify", &nvals, sizeof (NoisifyVals));
g_free(preview_cache);
}
}
else
......@@ -245,75 +229,26 @@ run (gchar *name,
gimp_drawable_detach (drawable);
}
static void
preview_do_row(gint row,
gint width,
guchar *even,
guchar *odd,
guchar *src)
static void
noisify_func (guchar *src, guchar *dest, gint bpp, GRand *gr)
{
gint x;
guchar *p0 = even;
guchar *p1 = odd;
gdouble r, g, b, a;
gdouble c0, c1;
gint noise = 0, b;
if (!nvals.independent)
noise = (gint) (nvals.noise[0] * gauss (gr) * 127);
for (x = 0; x < width; x++)
for (b = 0; b < bpp; b++)
{
if (preview_cache_bpp == 4)
if (nvals.noise[b] > 0.0)
{
r = ((gdouble)src[x*4+0]) / 255.0;
g = ((gdouble)src[x*4+1]) / 255.0;
b = ((gdouble)src[x*4+2]) / 255.0;
a = ((gdouble)src[x*4+3]) / 255.0;
}
else if (preview_cache_bpp == 3)
{
r = ((gdouble)src[x*3+0]) / 255.0;
g = ((gdouble)src[x*3+1]) / 255.0;
b = ((gdouble)src[x*3+2]) / 255.0;
a = 1.0;
if (nvals.independent)
noise = (gint) (nvals.noise[b] * gauss (gr) * 127);
gint p = src[b] + noise;
dest[b] = CLAMP0255(p);
}
else
{
r = ((gdouble)src[x*preview_cache_bpp+0]) / 255.0;
g = b = r;
if (preview_cache_bpp == 2)
a = ((gdouble)src[x*preview_cache_bpp+1]) / 255.0;
else
a = 1.0;
}
if ((x / GIMP_CHECK_SIZE) & 1)
{
c0 = GIMP_CHECK_LIGHT;
c1 = GIMP_CHECK_DARK;
}
else
{
c0 = GIMP_CHECK_DARK;
c1 = GIMP_CHECK_LIGHT;
}
*p0++ = (c0 + (r - c0) * a) * 255.0;
*p0++ = (c0 + (g - c0) * a) * 255.0;
*p0++ = (c0 + (b - c0) * a) * 255.0;
*p1++ = (c1 + (r - c1) * a) * 255.0;
*p1++ = (c1 + (g - c1) * a) * 255.0;
*p1++ = (c1 + (b - c1) * a) * 255.0;
} /* for */
if ((row / GIMP_CHECK_SIZE) & 1)
{
gtk_preview_draw_row (GTK_PREVIEW (preview), (guchar *)odd, 0, row, width);
}
else
{
gtk_preview_draw_row (GTK_PREVIEW (preview), (guchar *)even, 0, row, width);
dest[b] = src[b];
}
}
......@@ -321,153 +256,70 @@ static void
noisify (GimpDrawable *drawable,
gboolean preview_mode)
{
GimpPixelRgn src_rgn, dest_rgn;
guchar *src_row, *dest_row;
guchar *src, *dest, *dest_data;
gint row, col, b;
gint x1, y1, x2, y2, p, bpp = 3;
gint noise;
gint progress = 0, max_progress = 0;
gpointer pr;
gint row_stride = 0;
guchar *odd = NULL;
guchar *even = NULL;
GRand *gr;
/* initialize */
noise = 0;
gr = g_rand_new ();
if (preview_mode)
{
x1 = y1 = 0;
x2 = GTK_PREVIEW (preview)->buffer_width;
y2 = GTK_PREVIEW (preview)->buffer_height;
bpp = preview_cache_bpp;
row_stride = preview_cache_rowstride;
even = g_malloc (x2 * 3);
odd = g_malloc (x2 * 3);
}
else
{
gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2);
gimp_pixel_rgn_init (&src_rgn, drawable,
x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE);
gimp_pixel_rgn_init (&dest_rgn, drawable,
x1, y1, (x2 - x1), (y2 - y1), TRUE, TRUE);
/* Initialize progress */
progress = 0;
max_progress = (x2 - x1) * (y2 - y1);
}
guchar *src, *dest, *dest_data;
gint width, height, row_stride;
gint row, col;
gint bpp;
if (preview_mode)
{
dest_data = g_malloc (row_stride * y2);
width = preview->width;
height = preview->height;
bpp = preview->bpp;
row_stride = preview->rowstride;
dest_data = g_malloc (row_stride);
for (row = 0; row < y2; row++)
for (row = 0; row < height; row++)
{
src = preview_cache + row * row_stride;
dest = dest_data + row * row_stride;
for (col = 0; col < x2; col++)
src = preview->cache + row * row_stride;
dest = dest_data;
for (col = 0; col < width; col++)
{
if (nvals.independent == FALSE)
noise = (gint) (nvals.noise[0] * gauss (gr) * 127);
for (b = 0; b < bpp; b++)
{
if (nvals.independent == TRUE)
noise = (gint) (nvals.noise[b] * gauss (gr) * 127);
if (nvals.noise[b] > 0.0)
{
p = src[b] + noise;
if (p < 0)
p = 0;
else if (p > 255)
p = 255;
dest[b] = p;
}
else
dest[b] = src[b];
}
noisify_func (src, dest, bpp, gr);
src += bpp;
dest += bpp;
}
}
for (row = 0; row < y2; row++)
{
preview_do_row(row,x2,even,odd,dest_data + row * row_stride);
gimp_fixme_preview_do_row(preview, row, width, dest_data);
}
gtk_widget_queue_draw (preview);
g_free(even);
g_free(odd);
g_free (dest_data);
gtk_widget_queue_draw (preview->widget);
}
else
{
for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn);
pr != NULL;
pr = gimp_pixel_rgns_process (pr))
{
src_row = src_rgn.data;
dest_row = dest_rgn.data;
gimp_rgn_iterate2 (drawable, run_mode, (GimpRgnFunc2) noisify_func, gr);
}
for (row = 0; row < src_rgn.h; row++)
{
src = src_row;
dest = dest_row;
for (col = 0; col < src_rgn.w; col++)
{
if (nvals.independent == FALSE)
noise = (gint) (nvals.noise[0] * gauss (gr) * 127);
for (b = 0; b < src_rgn.bpp; b++)
{
if (nvals.independent == TRUE)
noise = (gint) (nvals.noise[b] * gauss (gr) * 127);
if (nvals.noise[b] > 0.0)
{
p = src[b] + noise;
if (p < 0)
p = 0;
else if (p > 255)
p = 255;
dest[b] = p;
}
else
dest[b] = src[b];
}
src += src_rgn.bpp;
dest += dest_rgn.bpp;
}
src_row += src_rgn.rowstride;
dest_row += dest_rgn.rowstride;
}
/* Update progress */
progress += src_rgn.w * src_rgn.h;
gimp_progress_update ((double) progress / (double) max_progress);
}
/* update the blurred region */
gimp_drawable_flush (drawable);
gimp_drawable_merge_shadow (drawable->drawable_id, TRUE);
gimp_drawable_update (drawable->drawable_id, x1, y1, (x2 - x1), (y2 - y1));
g_rand_free (gr);
} /* endif normal mode */
}
g_rand_free (gr);
static void
noisify_add_channel (GtkWidget *table, gint channel, gchar *name,
GimpDrawable *drawable)
{
GtkObject *adj;
adj = gimp_scale_entry_new (GTK_TABLE (table), 0, channel + 1,
name, SCALE_WIDTH, 0,
nvals.noise[channel], 0.0, 1.0, 0.01, 0.1, 2,
TRUE, 0, 0,
NULL, NULL);
g_object_set_data (G_OBJECT (adj), "drawable", drawable);
g_signal_connect (G_OBJECT (adj), "value_changed",
G_CALLBACK (noisify_double_adjustment_update),
&nvals.noise[channel]);
noise_int.channel_adj[channel] = adj;
}
static gint
......@@ -480,9 +332,6 @@ noisify_dialog (GimpDrawable *drawable,
GtkWidget *toggle;
GtkWidget *frame;
GtkWidget *table;
GtkObject *adj;
gchar *buffer;
gint i;
gimp_ui_init ("noisify", FALSE);
......@@ -520,10 +369,11 @@ noisify_dialog (GimpDrawable *drawable,
gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
gtk_container_add (GTK_CONTAINER (abox), frame);
gtk_widget_show (frame);
preview = preview_widget (drawable);
gtk_container_add (GTK_CONTAINER (frame), preview);
preview = gimp_fixme_preview_new (NULL);
gimp_fixme_preview_fill (preview, drawable);
gtk_container_add (GTK_CONTAINER (frame), preview->widget);
noisify (drawable, TRUE); /* preview noisify */
gtk_widget_show (preview);
gtk_widget_show (preview->widget);