From 801c6d61b1a1d9208d8cf1676000164f145cb02d Mon Sep 17 00:00:00 2001 From: Maurits Rijk Date: Thu, 30 Jan 2003 21:58:11 +0000 Subject: [PATCH] Mostly minor code clean-up. Fixed one memory leak. --- ChangeLog | 11 ++ plug-ins/common/c_astretch.c | 165 ++++++------------ plug-ins/common/color_enhance.c | 300 ++++++++++++-------------------- plug-ins/common/cubism.c | 99 +++-------- plug-ins/common/edge.c | 97 +---------- plug-ins/common/gradmap.c | 116 ++++-------- plug-ins/common/max_rgb.c | 183 +++++++------------ plug-ins/common/normalize.c | 162 ++++++----------- 8 files changed, 365 insertions(+), 768 deletions(-) diff --git a/ChangeLog b/ChangeLog index e9c17373bc..31e930f768 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2003-01-30 Maurits Rijk + + * plug-ins/common/c_astretch.c + * plug-ins/common/edge.c + * plug-ins/common/cubism.c: code clean-up, plugged a few memory leaks + 2003-01-30 Sven Neumann * libgimp/gimpcompat.h: removed GIMP_ENABLE_COMPAT_CRUFT guards. @@ -89,6 +95,11 @@ * app/gui/module-browser.c: pack the button box non-expanding, removed cruft from the ModuleBrowser struct. +2003-01-28 Maurits Rijk + + * plug-ins/common/color_enhance.c: + * plug-ins/common/gradmap.c: minor code clean-up + 2003-01-28 Tor Lillqvist * libgimp/gimpui.def: Add missing entry points. diff --git a/plug-ins/common/c_astretch.c b/plug-ins/common/c_astretch.c index d24068ab7d..b0ea224591 100644 --- a/plug-ins/common/c_astretch.c +++ b/plug-ins/common/c_astretch.c @@ -58,6 +58,7 @@ GimpPlugInInfo PLUG_IN_INFO = run, /* run_proc */ }; +static GimpRunMode run_mode; MAIN () @@ -100,7 +101,6 @@ run (gchar *name, { static GimpParam values[1]; GimpDrawable *drawable; - GimpRunMode run_mode; GimpPDBStatusType status = GIMP_PDB_SUCCESS; gint32 image_ID; @@ -146,9 +146,8 @@ run (gchar *name, gimp_drawable_detach (drawable); } - static void -indexed_c_astretch (gint32 image_ID) /* a.d.m. */ +indexed_c_astretch (gint32 image_ID) { guchar *cmap; gint ncols,i; @@ -185,127 +184,73 @@ indexed_c_astretch (gint32 image_ID) /* a.d.m. */ gimp_image_set_cmap (image_ID, cmap, ncols); } +typedef struct { + gint alpha; + guchar lut[256][3]; + guchar min[3]; + guchar max[3]; + gboolean has_alpha; +} AutoStretchParam_t; + +static void +find_min_max (guchar *src, gint bpp, gpointer data) +{ + AutoStretchParam_t *param = (AutoStretchParam_t*) data; + gint b; + + for (b = 0; b < param->alpha; b++) + { + if (!param->has_alpha || src[param->alpha]) + { + if (src[b] < param->min[b]) + param->min[b] = src[b]; + if (src[b] > param->max[b]) + param->max[b] = src[b]; + } + } +} + +static void +c_astretch_func (guchar *src, guchar *dest, gint bpp, gpointer data) +{ + AutoStretchParam_t *param = (AutoStretchParam_t*) data; + gint b; + + for (b = 0; b < param->alpha; b++) + dest[b] = param->lut[src[b]][b]; + + if (param->has_alpha) + dest[param->alpha] = src[param->alpha]; +} static void c_astretch (GimpDrawable *drawable) { - GimpPixelRgn src_rgn, dest_rgn; - guchar *src, *s; - guchar *dest, *d; - guchar min[3], max[3]; - guchar range; - guchar lut[256][3]; - gint progress, max_progress; - gint has_alpha, alpha; - gint x1, y1, x2, y2; - gint x, y, b; - gpointer pr; + AutoStretchParam_t param; + gint b; - /* Get selection area */ - gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); - has_alpha = gimp_drawable_has_alpha (drawable->drawable_id); - alpha = (has_alpha) ? drawable->bpp - 1 : drawable->bpp; - - /* Initialize progress */ - progress = 0; - max_progress = (x2 - x1) * (y2 - y1) * 2; + param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id); + param.alpha = (param.has_alpha) ? drawable->bpp - 1 : drawable->bpp; /* Get minimum and maximum values for each channel */ - min[0] = min[1] = min[2] = 255; - max[0] = max[1] = max[2] = 0; + param.min[0] = param.min[1] = param.min[2] = 255; + param.max[0] = param.max[1] = param.max[2] = 0; - gimp_pixel_rgn_init (&src_rgn, drawable, - x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE); - - for (pr = gimp_pixel_rgns_register (1, &src_rgn); - pr != NULL; - pr = gimp_pixel_rgns_process (pr)) - { - src = src_rgn.data; - - for (y = 0; y < src_rgn.h; y++) - { - s = src; - for (x = 0; x < src_rgn.w; x++) - { - for (b = 0; b < alpha; b++) - { - if (!has_alpha || (has_alpha && s[alpha])) - { - if (s[b] < min[b]) - min[b] = s[b]; - if (s[b] > max[b]) - max[b] = s[b]; - } - } - - s += src_rgn.bpp; - } - - src += src_rgn.rowstride; - } - - /* Update progress */ - progress += src_rgn.w * src_rgn.h; - - gimp_progress_update ((double) progress / (double) max_progress); - } + gimp_rgn_iterate1 (drawable, run_mode, find_min_max, ¶m); /* Calculate LUTs with stretched contrast */ - for (b = 0; b < alpha; b++) + for (b = 0; b < param.alpha; b++) { - range = max[b] - min[b]; + gint range = param.max[b] - param.min[b]; + gint x; if (range != 0) - for (x = min[b]; x <= max[b]; x++) - lut[x][b] = 255 * (x - min[b]) / range; + for (x = param.min[b]; x <= param.max[b]; x++) + param.lut[x][b] = 255 * (x - param.min[b]) / range; else - lut[min[b]][b] = min[b]; + param.lut[param.min[b]][b] = param.min[b]; } - /* Now substitute pixel vales */ - 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); - - for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn); - pr != NULL; - pr = gimp_pixel_rgns_process (pr)) - { - src = src_rgn.data; - dest = dest_rgn.data; - - for (y = 0; y < src_rgn.h; y++) - { - s = src; - d = dest; - - for (x = 0; x < src_rgn.w; x++) - { - for (b = 0; b < alpha; b++) - d[b] = lut[s[b]][b]; - - if (has_alpha) - d[alpha] = s[alpha]; - - s += src_rgn.bpp; - d += dest_rgn.bpp; - } - - src += src_rgn.rowstride; - dest += dest_rgn.rowstride; - - } - - /* Update progress */ - progress += src_rgn.w * src_rgn.h; - - gimp_progress_update ((double) progress / (double) max_progress); - } - - /* update the 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)); + gimp_rgn_iterate2 (drawable, run_mode, c_astretch_func, ¶m); } + diff --git a/plug-ins/common/color_enhance.c b/plug-ins/common/color_enhance.c index 3a7add3196..b65c49aba8 100644 --- a/plug-ins/common/color_enhance.c +++ b/plug-ins/common/color_enhance.c @@ -39,6 +39,7 @@ #include "libgimp/stdplugins-intl.h" +static GimpRunMode run_mode; /* Declare local functions. */ @@ -104,7 +105,6 @@ run (gchar *name, { static GimpParam values[1]; GimpDrawable *drawable; - GimpRunMode run_mode; GimpPDBStatusType status = GIMP_PDB_SUCCESS; gint32 image_ID; @@ -149,222 +149,146 @@ run (gchar *name, gimp_drawable_detach (drawable); } +static gdouble +get_v (const guchar *src) +{ + gdouble h, z, v; + gint c, m, y; + gint k; + guchar map[3]; + + c = 255 - src[0]; + m = 255 - src[1]; + y = 255 - src[2]; + + k = c; + if (m < k) k = m; + if (y < k) k = y; + + map[0] = c - k; + map[1] = m - k; + map[2] = y - k; + + gimp_rgb_to_hsv4(map, &h, &z, &v); + + return v; +} static void -indexed_Color_Enhance (gint32 image_ID) /* a.d.m. */ +enhance_it (const guchar *src, guchar *dest, gdouble vlo, gdouble vhi) +{ + gdouble h, z, v; + gint c, m, y; + gint k; + guchar map[3]; + + c = 255 - src[0]; + m = 255 - src[1]; + y = 255 - src[2]; + + k = c; + if (m < k) k = m; + if (y < k) k = y; + + map[0] = c - k; + map[1] = m - k; + map[2] = y - k; + + gimp_rgb_to_hsv4(map, &h, &z, &v); + + if (vhi != vlo) + v = (v - vlo) / (vhi - vlo); + + gimp_hsv_to_rgb4(map, h, z, v); + + c = map[0]; + m = map[1]; + y = map[2]; + + c += k; + if (c > 255) c = 255; + m += k; + if (m > 255) m = 255; + y += k; + if (y > 255) y = 255; + + dest[0] = 255 - c; + dest[1] = 255 - m; + dest[2] = 255 - y; +} + +static void +indexed_Color_Enhance (gint32 image_ID) { guchar *cmap; gint ncols,i; - gdouble vhi = 0.0, vlo = 1.0; cmap = gimp_image_get_cmap (image_ID, &ncols); - if (cmap==NULL) + if (!cmap) { printf("Color_Enhance: cmap was NULL! Quitting...\n"); gimp_quit(); } - for (i=0;i vhi) vhi = v; if (v < vlo) vlo = v; } - for (i=0;i 255) c = 255; - m += k; - if (m > 255) m = 255; - y += k; - if (y > 255) y = 255; - cmap[i*3+0] = 255 - c; - cmap[i*3+1] = 255 - m; - cmap[i*3+2] = 255 - y; + enhance_it (&cmap[3 * i], &cmap[3 * i], vlo, vhi); } gimp_image_set_cmap (image_ID, cmap, ncols); } +typedef struct { + gdouble vhi; + gdouble vlo; + gboolean has_alpha; +} ColorEnhanceParam_t; + +static void +find_vhi_vlo (guchar *src, gint bpp, gpointer data) +{ + ColorEnhanceParam_t *param = (ColorEnhanceParam_t*) data; + + if (!param->has_alpha || src[3]) + { + gdouble v = get_v (src); + + if (v > param->vhi) param->vhi = v; + if (v < param->vlo) param->vlo = v; + } +} + +static void +color_enhance_func (guchar *src, guchar *dest, gint bpp, gpointer data) +{ + ColorEnhanceParam_t *param = (ColorEnhanceParam_t*) data; + + enhance_it (src, dest, param->vlo, param->vhi); + + if (param->has_alpha) + dest[3] = src[3]; +} static void Color_Enhance (GimpDrawable *drawable) { - GimpPixelRgn src_rgn, dest_rgn; - guchar *src, *s; - guchar *dest, *d; - gdouble vhi = 0.0, vlo = 1.0; - gint progress, max_progress; - gint has_alpha, alpha; - gint x1, y1, x2, y2; - gint x, y; - gpointer pr; + ColorEnhanceParam_t param; - /* Get selection area */ - gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); - has_alpha = gimp_drawable_has_alpha (drawable->drawable_id); - alpha = (has_alpha) ? drawable->bpp - 1 : drawable->bpp; + param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id); + param.vhi = 0.0; + param.vlo = 1.0; - /* Initialize progress */ - progress = 0; - max_progress = (x2 - x1) * (y2 - y1) * 2; - - gimp_pixel_rgn_init (&src_rgn, drawable, - x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE); - - for (pr = gimp_pixel_rgns_register (1, &src_rgn); - pr != NULL; - pr = gimp_pixel_rgns_process (pr)) - { - src = src_rgn.data; - - for (y = 0; y < src_rgn.h; y++) - { - s = src; - for (x = 0; x < src_rgn.w; x++) - { - if (!has_alpha || (has_alpha && s[alpha])) - { - gdouble h, z, v; - gint c, m, y; - gint k; - guchar map[3]; - - c = 255 - s[0]; - m = 255 - s[1]; - y = 255 - s[2]; - k = c; - if (m < k) k = m; - if (y < k) k = y; - map[0] = c - k; - map[1] = m - k; - map[2] = y - k; - gimp_rgb_to_hsv4(map, &h, &z, &v); - if (v > vhi) vhi = v; - if (v < vlo) vlo = v; - } - - s += src_rgn.bpp; - } - - src += src_rgn.rowstride; - } - - /* Update progress */ - progress += src_rgn.w * src_rgn.h; - - gimp_progress_update ((gdouble) progress / (gdouble) max_progress); - } - - /* Now substitute pixel vales */ - 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); - - for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn); - pr != NULL; - pr = gimp_pixel_rgns_process (pr)) - { - src = src_rgn.data; - dest = dest_rgn.data; - - for (y = 0; y < src_rgn.h; y++) - { - s = src; - d = dest; - - for (x = 0; x < src_rgn.w; x++) - { - gdouble h, z, v; - gint c, m, y; - gint k; - guchar map[3]; - - c = 255 - s[0]; - m = 255 - s[1]; - y = 255 - s[2]; - k = c; - if (m < k) k = m; - if (y < k) k = y; - map[0] = c - k; - map[1] = m - k; - map[2] = y - k; - gimp_rgb_to_hsv4(map, &h, &z, &v); - if (vhi!=vlo) - v = (v-vlo) / (vhi-vlo); - gimp_hsv_to_rgb4(map, h, z, v); - c = map[0]; - m = map[1]; - y = map[2]; - c += k; - if (c > 255) c = 255; - m += k; - if (m > 255) m = 255; - y += k; - if (y > 255) y = 255; - d[0] = 255 - c; - d[1] = 255 - m; - d[2] = 255 - y; - - if (has_alpha) - d[alpha] = s[alpha]; - - s += src_rgn.bpp; - d += dest_rgn.bpp; - } - - src += src_rgn.rowstride; - dest += dest_rgn.rowstride; - - } - - /* Update progress */ - progress += src_rgn.w * src_rgn.h; - - gimp_progress_update ((gdouble) progress / (gdouble) max_progress); - } - - /* update the 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)); + gimp_rgn_iterate1 (drawable, run_mode, find_vhi_vlo, ¶m); + gimp_rgn_iterate2 (drawable, run_mode, color_enhance_func, ¶m); } + diff --git a/plug-ins/common/cubism.c b/plug-ins/common/cubism.c index 159cf1e66c..59886cd14e 100644 --- a/plug-ins/common/cubism.c +++ b/plug-ins/common/cubism.c @@ -69,7 +69,6 @@ static void run (gchar *name, GimpParam **return_vals); static void cubism (GimpDrawable *drawable); -static void render_cubism (GimpDrawable *drawable); static void fill_poly_color (Polygon *poly, GimpDrawable *drawable, guchar *col); @@ -109,8 +108,6 @@ static void cubism_ok_callback (GtkWidget *widget, * Local variables */ -static guchar bg_col[4]; - static CubismVals cvals = { 10.0, /* tile_size */ @@ -253,52 +250,6 @@ run (gchar *name, gimp_drawable_detach (active_drawable); } -static void -cubism (GimpDrawable *drawable) -{ - GimpRGB background; - gint x1, y1, x2, y2; - - /* find the drawable mask bounds */ - gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); - - /* determine the background color */ - if (cvals.bg_color == BLACK) - { - bg_col[0] = bg_col[1] = bg_col[2] = bg_col[3] = 0; - } - else - { - gimp_palette_get_background (&background); - switch (gimp_drawable_type (drawable->drawable_id)) - { - case GIMP_RGBA_IMAGE: - bg_col[3] = 0; - case GIMP_RGB_IMAGE: - gimp_rgb_get_uchar (&background, - &bg_col[0], &bg_col[1], &bg_col[2]); - break; - case GIMP_GRAYA_IMAGE: - bg_col[1] = 0; - case GIMP_GRAY_IMAGE: - bg_col[0] = gimp_rgb_intensity_uchar (&background); - - default: - break; - } - } - - gimp_progress_init (_("Cubistic Transformation")); - - /* render the cubism */ - render_cubism (drawable); - - /* merge the shadow, update the 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)); -} - static gint cubism_dialog (void) { @@ -391,10 +342,10 @@ cubism_ok_callback (GtkWidget *widget, } static void -render_cubism (GimpDrawable *drawable) +cubism (GimpDrawable *drawable) { GimpPixelRgn src_rgn; - gdouble img_area, tile_area; + guchar bg_col[4]; gdouble x, y; gdouble width, height; gdouble theta; @@ -416,8 +367,18 @@ render_cubism (GimpDrawable *drawable) has_alpha = gimp_drawable_has_alpha (drawable->drawable_id); bytes = drawable->bpp; gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); - img_area = (x2 - x1) * (y2 - y1); - tile_area = SQR (cvals.tile_size); + + /* determine the background color */ + if (cvals.bg_color == BLACK) + { + bg_col[0] = bg_col[1] = bg_col[2] = bg_col[3] = 0; + } + else + { + gimp_get_bg_guchar (drawable, TRUE, bg_col); + } + + gimp_progress_init (_("Cubistic Transformation")); cols = ((x2 - x1) + cvals.tile_size - 1) / cvals.tile_size; rows = ((y2 - y1) + cvals.tile_size - 1) / cvals.tile_size; @@ -446,7 +407,7 @@ render_cubism (GimpDrawable *drawable) count = 0; gimp_pixel_rgn_init (&src_rgn, drawable, - x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE); + x1, y1, x2 - x1, y2 - y1, FALSE, FALSE); while (count < num_tiles) { @@ -456,10 +417,12 @@ render_cubism (GimpDrawable *drawable) - g_rand_double_range (gr, 0, cvals.tile_size/2.0) + x1; y = i * cvals.tile_size + (cvals.tile_size / 4.0) - g_rand_double_range (gr, 0, cvals.tile_size/2.0) + y1; - width = (cvals.tile_size + g_rand_double_range (gr, 0, cvals.tile_size / 4.0) - - cvals.tile_size / 8.0) * cvals.tile_saturation; - height = (cvals.tile_size + g_rand_double_range (gr, 0, cvals.tile_size / 4.0) - - cvals.tile_size / 8.0) * cvals.tile_saturation; + width = (cvals.tile_size + + g_rand_double_range (gr, 0, cvals.tile_size / 4.0) - + cvals.tile_size / 8.0) * cvals.tile_saturation; + height = (cvals.tile_size + + g_rand_double_range (gr, 0, cvals.tile_size / 4.0) - + cvals.tile_size / 8.0) * cvals.tile_saturation; theta = g_rand_double_range (gr, 0, 2 * G_PI); polygon_reset (&poly); polygon_add_point (&poly, -width / 2.0, -height / 2.0); @@ -470,20 +433,12 @@ render_cubism (GimpDrawable *drawable) polygon_translate (&poly, x, y); /* bounds check on x, y */ - ix = (int) x; - iy = (int) y; - if (ix < x1) - ix = x1; - if (ix >= x2) - ix = x2 - 1; - if (iy < y1) - iy = y1; - if (iy >= y2) - iy = y2 - 1; + ix = CLAMP (x, x1, x2 - 1); + iy = CLAMP (y, y1, y2 - 1); gimp_pixel_rgn_get_pixel (&src_rgn, col, ix, iy); - if (! has_alpha || (has_alpha && col[bytes - 1] != 0)) + if (!has_alpha || col[bytes - 1]) fill_poly_color (&poly, drawable, col); count++; @@ -494,6 +449,11 @@ render_cubism (GimpDrawable *drawable) gimp_progress_update (1.0); g_free (random_indices); g_rand_free (gr); + + /* merge the shadow, update the 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); } static inline gdouble @@ -659,7 +619,6 @@ fill_poly_color (Polygon *poly, { xx = (gdouble) j / (gdouble) SUPERSAMPLE + min_x; alpha = (gint) (val * calc_alpha_blend (vec, one_over_dist, xx - sx, yy - sy)); - gimp_pixel_rgn_get_pixel (&src_rgn, buf, x, y); #ifndef USE_READABLE_BUT_SLOW_CODE diff --git a/plug-ins/common/edge.c b/plug-ins/common/edge.c index d31e91481b..3b8aa47d28 100644 --- a/plug-ins/common/edge.c +++ b/plug-ins/common/edge.c @@ -44,6 +44,7 @@ #include "config.h" +#include #include #include @@ -88,8 +89,6 @@ static void run (gchar *name, static void edge (GimpDrawable *drawable); static gint edge_dialog (GimpDrawable *drawable); -static long long_sqrt (long n); - /***** Local vars *****/ GimpPlugInInfo PLUG_IN_INFO = @@ -232,94 +231,6 @@ run (gchar *name, gimp_drawable_detach (drawable); } -/********************************************************************** - TileBuf Util Routines End - **********************************************************************/ - -static long -long_sqrt (long n) -{ -#define lsqrt_max4pow (1UL << 30) - /* lsqrt_max4pow is the (machine-specific) largest power of 4 that can - * be represented in an unsigned long. - * - * Compute the integer square root of the integer argument n - * Method is to divide n by x computing the quotient x and remainder r - * Notice that the divisor x is changing as the quotient x changes - * - * Instead of shifting the dividend/remainder left, we shift the - * quotient/divisor right. The binary point starts at the extreme - * left, and shifts two bits at a time to the extreme right. - * - * The residue contains n-x^2. (Within these comments, the ^ operator - * signifies exponentiation rather than exclusive or. Also, the / - * operator returns fractions, rather than truncating, so 1/4 means - * one fourth, not zero.) - * - * Since (x + 1/2)^2 == x^2 + x + 1/4, - * n - (x + 1/2)^2 == (n - x^2) - (x + 1/4) - * Thus, we can increase x by 1/2 if we decrease (n-x^2) by (x+1/4) - */ - - gulong residue; /* n - x^2 */ - gulong root; /* x + 1/4 */ - gulong half; /* 1/2 */ - - residue = n; /* n - (x = 0)^2, with suitable alignment */ - - /* - * if the correct answer fits in two bits, pull it out of a magic hat - */ - if (residue <= 12) - return (0x03FFEA94 >> (residue *= 2)) & 3; - - root = lsqrt_max4pow; /* x + 1/4, shifted all the way left */ - /* half = root + root; 1/2, shifted likewise */ - - /* - * Unwind iterations corresponding to leading zero bits - */ - while (root > residue) - root >>= 2; - - /* - * Unwind the iteration corresponding to the first one bit - * Operations have been rearranged and combined for efficiency - * Initialization of half is folded into this iteration - */ - residue -= root; /* Decrease (n-x^2) by (0+1/4) */ - half = root >> 2; /* 1/4, with binary point shifted right 2 */ - root += half; /* x=1. (root is now (x=1)+1/4.) */ - half += half; /* 1/2, properly aligned */ - - /* - * Normal loop (there is at least one iteration remaining) - */ - do - { - if (root <= residue) /* Whenever we can, */ - { - residue -= root; /* decrease (n-x^2) by (x+1/4) */ - root += half; /* increase x by 1/2 */ - } - half >>= 2; /* Shift binary point 2 places right */ - root -= half; /* x{ +1/2 } +1/4 - 1/8 == x { +1/2 } 1/8 */ - root >>= 1; /* 2x{ +1 } +1/4, shifted right 2 places */ - } - while (half); /* When 1/2 == 0, bin. point is at far right */ - - /* - * round up if (x+1/2)^2 < n - */ - if (root < residue) - ++root; - - /* - * Guaranteed to be correctly rounded (or truncated) - */ - return root; -} - /********************************************************/ /* Edge Detection main */ /********************************************************/ @@ -421,7 +332,8 @@ edge (GimpDrawable *drawable) (PIX(2,2) - PIX(2,0)); #undef PIX /* common job ... */ - sum = long_sqrt ((long) sum1 * sum1 + (long) sum2 * sum2); + sum = (glong) (sqrt((long) sum1 * sum1 + + (long) sum2 * sum2) + 0.5); sum = (sum * scale) >> 16; /* arbitrary scaling factor */ if (sum > maxval) sum = maxval; @@ -458,7 +370,8 @@ edge (GimpDrawable *drawable) 2 * (pix12[chan] - pix10[chan]) + (pix22[chan] - pix20[chan]); /* common job ... */ - sum = long_sqrt ((long) sum1 * sum1 + (long) sum2 * sum2); + sum = (glong) (sqrt ((long) sum1 * sum1 + + (long) sum2 * sum2) + 0.5); sum = (sum * scale) >> 16; /* arbitrary scaling factor */ if (sum > maxval) sum = maxval; dest[chan] = sum; diff --git a/plug-ins/common/gradmap.c b/plug-ins/common/gradmap.c index 44b5b69bbd..a4c2017768 100644 --- a/plug-ins/common/gradmap.c +++ b/plug-ins/common/gradmap.c @@ -53,9 +53,6 @@ #include "config.h" -#include -#include - #include #include "libgimp/stdplugins-intl.h" @@ -71,6 +68,8 @@ static char rcsid[] = "$Id$"; #define TILE_CACHE_SIZE 32 #define LUMINOSITY(X) (INTENSITY (X[0], X[1], X[2])) +static GimpRunMode run_mode; + /* Declare a local function. */ static void query (void); @@ -135,7 +134,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; @@ -174,86 +172,46 @@ run (gchar *name, gimp_drawable_detach (drawable); } +typedef struct { + guchar *samples; + gboolean is_rgb; + gboolean has_alpha; +} GradMapParam_t; + +static void +gradmap_func (guchar *src, guchar *dest, gint bpp, gpointer data) +{ + GradMapParam_t *param = (GradMapParam_t*) data; + gint lum; + gint b; + guchar *samp; + + lum = (param->is_rgb) ? LUMINOSITY (src) : src[0]; + samp = ¶m->samples[lum * bpp]; + + if (param->has_alpha) + { + for (b = 0; b < bpp - 1; b++) + dest[b] = samp[b]; + dest[b] = ((guint)samp[b] * (guint)src[b]) / 255; + } + else + { + for (b = 0; b < bpp; b++) + dest[b] = samp[b]; + } +} + static void gradmap (GimpDrawable *drawable) { - GimpPixelRgn src_rgn, dest_rgn; - gpointer pr; - guchar *src_row, *dest_row; - guchar *src, *dest; - gint progress, max_progress; - gint x1, y1, x2, y2; - gint row, col; - gint bpp, color, has_alpha; - guchar *samples, *samp; - gint lum; /* luminosity */ - gint b; + GradMapParam_t param; - gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); + param.is_rgb = gimp_drawable_is_rgb (drawable->drawable_id); + param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id); + param.samples = get_samples (drawable); - bpp = gimp_drawable_bpp (drawable->drawable_id); - color = gimp_drawable_is_rgb (drawable->drawable_id); - has_alpha = gimp_drawable_has_alpha (drawable->drawable_id); - - samples = get_samples (drawable); - - 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); - - 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; - - for (row = 0; row < src_rgn.h; row++) - { - src = src_row; - dest = dest_row; - - for (col = 0; col < src_rgn.w; col++) - { - if (color) - { - lum = LUMINOSITY (src); - lum = CLAMP (lum, 0, 255); /* to make sure */ - } - else - lum = src[0]; - - samp = &samples[lum * bpp]; - if (has_alpha) - { - for (b = 0; b < bpp - 1; b++) - dest[b] = samp[b]; - dest[b] = ((guint)(samp[b]) * (guint)(src[b])) / 255; - } - else - { - for (b = 0; b < bpp; b++) - dest[b] = samp[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); - } - - g_free (samples); - - gimp_drawable_flush (drawable); - gimp_drawable_merge_shadow (drawable->drawable_id, TRUE); - gimp_drawable_update (drawable->drawable_id, x1, y1, (x2 - x1), (y2 - y1)); + gimp_rgn_iterate2 (drawable, run_mode, gradmap_func, ¶m); } /* diff --git a/plug-ins/common/max_rgb.c b/plug-ins/common/max_rgb.c index aca63e60e9..d259549d23 100644 --- a/plug-ins/common/max_rgb.c +++ b/plug-ins/common/max_rgb.c @@ -29,7 +29,6 @@ #include #include -#include #include @@ -51,8 +50,8 @@ static void run (gchar *name, gint *nreturn_vals, GimpParam **return_vals); -static GimpPDBStatusType main_function (GimpDrawable *drawable, - gboolean preview_mode); +static void main_function (GimpDrawable *drawable, + gboolean preview_mode); static gint dialog (GimpDrawable *drawable); static void ok_callback (GtkWidget *widget, @@ -95,6 +94,7 @@ static Interface interface = FALSE }; +static GimpRunMode run_mode; static GimpFixMePreview *preview; MAIN () @@ -136,7 +136,6 @@ run (gchar *name, GimpDrawable *drawable; static GimpParam values[1]; GimpPDBStatusType status = GIMP_PDB_EXECUTION_ERROR; - GimpRunMode run_mode; run_mode = param[0].data.d_int32; drawable = gimp_drawable_get (param[2].data.d_drawable); @@ -171,7 +170,7 @@ run (gchar *name, break; } - status = main_function (drawable, FALSE); + main_function (drawable, FALSE); if (run_mode != GIMP_RUN_NONINTERACTIVE) gimp_displays_flush (); @@ -181,137 +180,81 @@ run (gchar *name, values[0].data.d_status = status; } -static GimpPDBStatusType +typedef struct { + gint init_value; + gint flag; + gboolean has_alpha; +} MaxRgbParam_t; + +static void +max_rgb_func (guchar *src, guchar *dest, gint bpp, gpointer data) +{ + MaxRgbParam_t *param = (MaxRgbParam_t*) data; + gint ch, max_ch = 0; + guchar max, tmp_value; + + max = param->init_value; + for (ch = 0; ch < 3; ch++) + if (param->flag * max <= param->flag * (tmp_value = (*src++))) + { + if (max == tmp_value) + { + max_ch += 1 << ch; + } + else + { + max_ch = 1 << ch; /* clear memories of old channels */ + max = tmp_value; + } + } + + dest[0] = (max_ch & (1 << 0)) ? max : 0; + dest[1] = (max_ch & (1 << 1)) ? max : 0; + dest[2] = (max_ch & (1 << 2)) ? max : 0; + if (param->has_alpha) + dest[3] = src[3]; +} + +static void main_function (GimpDrawable *drawable, gboolean preview_mode) { - GimpPixelRgn src_rgn, dest_rgn; - guchar *src, *dest, *save_dest, *src_data, *dest_data; - gpointer pr = NULL; - gint x, y, x1, x2, y1, y2; - gint gap, total, processed = 0; - gint init_value, flag; - gint bpp = 3; - - init_value = (pvals.max_p > 0) ? 0 : 255; - flag = (0 < pvals.max_p) ? 1 : -1; - - if (preview_mode) - { - x1 = y1 = 0; - x2 = preview->width; - y2 = preview->height; - gap = 0; /* no alpha on preview */ - bpp = preview->bpp; - } - else - { - gap = (gimp_drawable_has_alpha (drawable->drawable_id)) ? 1 : 0; - gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); - gimp_tile_cache_ntiles (2 * (drawable->width / gimp_tile_width () + 1)); - 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); - pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn); - gimp_progress_init ( _("Max RGB: Scanning...")); - } + MaxRgbParam_t param; - total = (x2 - x1) * (y2 - y1); - if (total < 1) - return GIMP_PDB_EXECUTION_ERROR; + param.init_value = (pvals.max_p > 0) ? 0 : 255; + param.flag = (0 < pvals.max_p) ? 1 : -1; if (preview_mode) { - src_data = g_malloc (preview->rowstride * y2); - memcpy (src_data, preview->cache, preview->rowstride * y2); - dest_data = g_malloc (preview->rowstride * y2); - save_dest = dest_data; - - for (y = 0; y < y2; y++) + gint x, y; + gint bpp = preview->bpp; + + param.has_alpha = FALSE; /* no alpha on preview */ + + for (y = 0; y < preview->height; y++) { - src = src_data + y * preview->rowstride; - dest = dest_data + y * preview->rowstride; - - for (x = 0; x < x2; x++) + guchar *src = preview->cache + y * preview->rowstride; + guchar *dest = preview->buffer + y * preview->rowstride; + + for (x = 0; x < preview->width; x++) { - gint ch, max_ch = 0; - guchar max, tmp_value; - - max = init_value; - for (ch = 0; ch < 3; ch++) - if (flag * max <= flag * (tmp_value = (*src++))) - { - if (max == tmp_value) - { - max_ch += 1 << ch; - } - else - { - max_ch = 1 << ch; /* clear memories of old channels */ - max = tmp_value; - } - } - - for ( ch = 0; ch < 3; ch++) - *dest++ = (guchar)(((max_ch & (1 << ch)) > 0) ? max : 0); - - if (gap) - *dest++ = *src++; + max_rgb_func (src, dest, bpp, ¶m); + dest += bpp; + src += bpp; } } - - memcpy (preview->buffer, save_dest, preview->rowstride * y2); gtk_widget_queue_draw (preview->widget); } else { /* normal mode */ - for (; pr != NULL; pr = gimp_pixel_rgns_process (pr)) - { - for (y = 0; y < src_rgn.h; y++) - { - src = src_rgn.data + y * src_rgn.rowstride; - dest = dest_rgn.data + y * dest_rgn.rowstride; - - for (x = 0; x < src_rgn.w; x++) - { - gint ch, max_ch = 0; - guchar max, tmp_value; - - max = init_value; - for (ch = 0; ch < 3; ch++) - if (flag * max <= flag * (tmp_value = (*src++))) - { - if (max == tmp_value) - { - max_ch += 1 << ch; - } - else - { - max_ch = 1 << ch; /* clear memories of old channels */ - max = tmp_value; - } - - } - for ( ch = 0; ch < 3; ch++) - *dest++ = (guchar)(((max_ch & (1 << ch)) > 0) ? max : 0); - - if (gap) - *dest++=*src++; - - if ((++processed % (total / PROGRESS_UPDATE_NUM + 1)) == 0) - gimp_progress_update ((gdouble) processed / (gdouble) total); - } - } - } - gimp_progress_update (1.0); - gimp_drawable_flush (drawable); - gimp_drawable_merge_shadow (drawable->drawable_id, TRUE); - gimp_drawable_update (drawable->drawable_id, x1, y1, (x2 - x1), (y2 - y1)); + param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id); + + gimp_progress_init ( _("Max RGB: Scanning...")); + + gimp_rgn_iterate2 (drawable, run_mode, max_rgb_func, ¶m); + gimp_drawable_detach (drawable); - } - - return GIMP_PDB_SUCCESS; + } } diff --git a/plug-ins/common/normalize.c b/plug-ins/common/normalize.c index 9a18cfe079..fb50b2668e 100644 --- a/plug-ins/common/normalize.c +++ b/plug-ins/common/normalize.c @@ -58,6 +58,8 @@ GimpPlugInInfo PLUG_IN_INFO = run, /* run_proc */ }; +static GimpRunMode run_mode; + MAIN () @@ -101,7 +103,6 @@ run (gchar *name, { static GimpParam values[1]; GimpDrawable *drawable; - GimpRunMode run_mode; GimpPDBStatusType status = GIMP_PDB_SUCCESS; gint32 image_ID; @@ -182,126 +183,69 @@ indexed_normalize (gint32 image_ID) /* a.d.m. */ gimp_image_set_cmap (image_ID, cmap, ncols); } +typedef struct { + guchar lut[256]; + gdouble min; + gdouble max; + gint alpha; + gboolean has_alpha; +} NormalizeParam_t; + +static void +find_min_max (guchar *src, gint bpp, gpointer data) +{ + NormalizeParam_t *param = (NormalizeParam_t*) data; + gint b; + + for (b = 0; b < param->alpha; b++) + { + if (!param->has_alpha || src[param->alpha]) + { + if (src[b] < param->min) + param->min = src[b]; + if (src[b] > param->max) + param->max = src[b]; + } + } +} + +static void +normalize_func (guchar *src, guchar *dest, gint bpp, gpointer data) +{ + NormalizeParam_t *param = (NormalizeParam_t*) data; + gint b; + + for (b = 0; b < param->alpha; b++) + dest[b] = param->lut[src[b]]; + + if (param->has_alpha) + dest[param->alpha] = src[param->alpha]; +} static void normalize (GimpDrawable *drawable) { - GimpPixelRgn src_rgn, dest_rgn; - guchar *src, *s; - guchar *dest, *d; - guchar min, max; + NormalizeParam_t param; + gint x; guchar range; - guchar lut[256]; - gint progress, max_progress; - gint has_alpha, alpha; - gint x1, y1, x2, y2; - gint x, y, b; - gpointer pr; - /* Get selection area */ - gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); - has_alpha = gimp_drawable_has_alpha (drawable->drawable_id); - alpha = (has_alpha) ? drawable->bpp - 1 : drawable->bpp; + param.min = 255; + param.max = 0; + param.has_alpha = gimp_drawable_has_alpha (drawable->drawable_id); + param.alpha = (param.has_alpha) ? drawable->bpp - 1 : drawable->bpp; - /* Initialize progress */ - progress = 0; - max_progress = (x2 - x1) * (y2 - y1) * 2; - - /* Get minimum and maximum values for each channel */ - min = 255; - max = 0; - - gimp_pixel_rgn_init (&src_rgn, drawable, - x1, y1, (x2 - x1), (y2 - y1), FALSE, FALSE); - - for (pr = gimp_pixel_rgns_register (1, &src_rgn); - pr != NULL; - pr = gimp_pixel_rgns_process (pr)) - { - src = src_rgn.data; - - for (y = 0; y < src_rgn.h; y++) - { - s = src; - for (x = 0; x < src_rgn.w; x++) - { - for (b = 0; b < alpha; b++) - { - if (!has_alpha || (has_alpha && s[alpha])) - { - if (s[b] < min) - min = s[b]; - if (s[b] > max) - max = s[b]; - } - } - - s += src_rgn.bpp; - } - - src += src_rgn.rowstride; - } - - /* Update progress */ - progress += src_rgn.w * src_rgn.h; - - gimp_progress_update ((double) progress / (double) max_progress); - } + gimp_rgn_iterate1 (drawable, run_mode, find_min_max, ¶m); /* Calculate LUT */ - range = max - min; + range = param.max - param.min; if (range != 0) - for (x = min; x <= max; x++) - lut[x] = 255 * (x - min) / range; + for (x = param.min; x <= param.max; x++) + param.lut[x] = 255 * (x - param.min) / range; else - lut[min] = min; + param.lut[(gint)param.min] = param.min; - - /* Now substitute pixel vales */ - 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); - - for (pr = gimp_pixel_rgns_register (2, &src_rgn, &dest_rgn); - pr != NULL; - pr = gimp_pixel_rgns_process (pr)) - { - src = src_rgn.data; - dest = dest_rgn.data; - - for (y = 0; y < src_rgn.h; y++) - { - s = src; - d = dest; - - for (x = 0; x < src_rgn.w; x++) - { - for (b = 0; b < alpha; b++) - d[b] = lut[s[b]]; - - if (has_alpha) - d[alpha] = s[alpha]; - - s += src_rgn.bpp; - d += dest_rgn.bpp; - } - - src += src_rgn.rowstride; - dest += dest_rgn.rowstride; - - } - - /* Update progress */ - progress += src_rgn.w * src_rgn.h; - - gimp_progress_update ((double) progress / (double) max_progress); - } - - /* update the 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)); + gimp_rgn_iterate2 (drawable, run_mode, normalize_func, ¶m); } +