From 4c0f1a2d5414c844af03e035e5051e9ff4c21e16 Mon Sep 17 00:00:00 2001 From: Sven Neumann Date: Sat, 10 Oct 2009 15:46:31 +0200 Subject: [PATCH] app: minor optimization and cleanups gimp_channel_add_segment() and gimp_channel_sub_segment() can just write directly if the value is 255, which it is quite commonly. --- app/core/gimpchannel-combine.c | 82 +++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/app/core/gimpchannel-combine.c b/app/core/gimpchannel-combine.c index 1c831a3f88..e56523b70a 100644 --- a/app/core/gimpchannel-combine.c +++ b/app/core/gimpchannel-combine.c @@ -17,6 +17,8 @@ #include "config.h" +#include + #include #include "libgimpbase/gimpbase.h" @@ -41,8 +43,6 @@ gimp_channel_add_segment (GimpChannel *mask, gint value) { PixelRegion maskPR; - guchar *data; - gint val; gint x2; gpointer pr; @@ -62,18 +62,33 @@ gimp_channel_add_segment (GimpChannel *mask, pixel_region_init (&maskPR, gimp_drawable_get_tiles (GIMP_DRAWABLE (mask)), x, y, width, 1, TRUE); - for (pr = pixel_regions_register (1, &maskPR); - pr != NULL; - pr = pixel_regions_process (pr)) + /* If the value is 255, there is no point in adding it to the + * existing selection mask, just set everything to 255. + */ + if (value == 255) { - data = maskPR.data; - width = maskPR.w; - while (width--) + for (pr = pixel_regions_register (1, &maskPR); + pr != NULL; + pr = pixel_regions_process (pr)) { - val = *data + value; - if (val > 255) - val = 255; - *data++ = val; + memset (maskPR.data, 255, maskPR.w); + } + } + else + { + for (pr = pixel_regions_register (1, &maskPR); + pr != NULL; + pr = pixel_regions_process (pr)) + { + guchar *data = maskPR.data; + + width = maskPR.w; + while (width--) + { + const gint val = *data + value; + + *data++ = val < 255 ? val : 255; + } } } } @@ -86,8 +101,6 @@ gimp_channel_sub_segment (GimpChannel *mask, gint value) { PixelRegion maskPR; - guchar *data; - gint val; gint x2; gpointer pr; @@ -108,18 +121,33 @@ gimp_channel_sub_segment (GimpChannel *mask, pixel_region_init (&maskPR, gimp_drawable_get_tiles (GIMP_DRAWABLE (mask)), x, y, width, 1, TRUE); - for (pr = pixel_regions_register (1, &maskPR); - pr != NULL; - pr = pixel_regions_process (pr)) + /* If the value is 255, there is no point in subtracting it from + * the existing selection mask, just set everything to 0. + */ + if (value == 255) { - data = maskPR.data; - width = maskPR.w; - while (width--) + for (pr = pixel_regions_register (1, &maskPR); + pr != NULL; + pr = pixel_regions_process (pr)) { - val = *data - value; - if (val < 0) - val = 0; - *data++ = val; + memset (maskPR.data, 0, maskPR.w); + } + } + else + { + for (pr = pixel_regions_register (1, &maskPR); + pr != NULL; + pr = pixel_regions_process (pr)) + { + guchar *data = maskPR.data; + + width = maskPR.w; + while (width--) + { + const gint val = *data - value; + + *data++ = val > 0 ? val : 0; + } } } } @@ -175,7 +203,9 @@ gimp_channel_combine_rect (GimpChannel *mask, mask->y2 = y + h; } else - mask->bounds_known = FALSE; + { + mask->bounds_known = FALSE; + } mask->x1 = CLAMP (mask->x1, 0, gimp_item_get_width (GIMP_ITEM (mask))); mask->y1 = CLAMP (mask->y1, 0, gimp_item_get_height (GIMP_ITEM (mask))); @@ -236,7 +266,7 @@ gimp_channel_combine_segment (GimpChannel *mask, break; case GIMP_CHANNEL_OP_INTERSECT: - /* Should not happend */ + /* Should not happen */ break; } }