Use p_step to avoid has_alpha comparison in loop
authorArkadiy Illarionov <qarkai@gmail.com>
Sun, 10 Mar 2024 10:52:01 +0000 (13:52 +0300)
committerColin Clark <colin.clark@cclark.uk>
Sun, 10 Mar 2024 11:25:18 +0000 (11:25 +0000)
src/dnd.cc
src/pixbuf-util.cc
src/similar.cc

index 600745e..63c7ee9 100644 (file)
@@ -64,13 +64,15 @@ static void pixbuf_draw_border(GdkPixbuf *pixbuf, gint w, gint h)
                *p = 0; p++; *p = 0; p++; *p = 0; p++;
                if (alpha) { *p= 255; p++; }
                }
+
+       const gint p_step = alpha ? 4 : 3;
        for (i = 1; i < h - 1; i++)
                {
                p = pix + rs * i;
                *p = 0; p++; *p = 0; p++; *p = 0; p++;
                if (alpha) *p= 255;
 
-               p = pix + rs * i + (w - 1) * ((alpha == TRUE) ? 4 : 3);
+               p = pix + rs * i + (w - 1) * p_step;
                *p = 0; p++; *p = 0; p++; *p = 0; p++;
                if (alpha) *p= 255;
                }
@@ -97,9 +99,11 @@ static void pixbuf_draw_rect_unused(GdkPixbuf *pixbuf, gint x, gint y, gint w, g
        rs = gdk_pixbuf_get_rowstride(pixbuf);
        pix = gdk_pixbuf_get_pixels(pixbuf);
 
+       const gint p_step = alpha ? 4 : 3;
+
        for (j = 0; j < h; j++)
                {
-               p = pix + (rs * (y + j)) + (x * ((alpha) ? 4 : 3));
+               p = pix + (rs * (y + j)) + (x * p_step);
                for (i = 0; i < w; i++)
                        {
                        *p = (*p * (256-val)) >> 8; p++;
index feef6a9..35abc2f 100644 (file)
@@ -732,18 +732,17 @@ void pixbuf_draw_rect_fill(GdkPixbuf *pb,
        prs = gdk_pixbuf_get_rowstride(pb);
        p_pix = gdk_pixbuf_get_pixels(pb);
 
+       const gint p_step = has_alpha ? 4 : 3;
+
        for (i = 0; i < h; i++)
                {
-               pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
+               pp = p_pix + (y + i) * prs + (x * p_step);
                for (j = 0; j < w; j++)
                        {
-                       *pp = (r * a + *pp * (256-a)) >> 8;
-                       pp++;
-                       *pp = (g * a + *pp * (256-a)) >> 8;
-                       pp++;
-                       *pp = (b * a + *pp * (256-a)) >> 8;
-                       pp++;
-                       if (has_alpha) pp++;
+                       pp[0] = (r * a + pp[0] * (256-a)) >> 8;
+                       pp[1] = (g * a + pp[1] * (256-a)) >> 8;
+                       pp[2] = (b * a + pp[2] * (256-a)) >> 8;
+                       pp += p_step;
                        }
                }
 }
@@ -791,9 +790,11 @@ void pixbuf_set_rect_fill(GdkPixbuf *pb,
        prs = gdk_pixbuf_get_rowstride(pb);
        p_pix = gdk_pixbuf_get_pixels(pb);
 
+       const gint p_step = has_alpha ? 4 : 3;
+
        for (i = 0; i < h; i++)
                {
-               pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
+               pp = p_pix + (y + i) * prs + (x * p_step);
                for (j = 0; j < w; j++)
                        {
                        *pp = r; pp++;
@@ -1123,13 +1124,10 @@ void pixbuf_draw_triangle(GdkPixbuf *pb,
 
                while (xa < xb)
                        {
-                       *pp = (r * a + *pp * (256-a)) >> 8;
-                       pp++;
-                       *pp = (g * a + *pp * (256-a)) >> 8;
-                       pp++;
-                       *pp = (b * a + *pp * (256-a)) >> 8;
-                       pp++;
-                       if (has_alpha) pp++;
+                       pp[0] = (r * a + pp[0] * (256-a)) >> 8;
+                       pp[1] = (g * a + pp[1] * (256-a)) >> 8;
+                       pp[2] = (b * a + pp[2] * (256-a)) >> 8;
+                       pp += p_step;
 
                        xa++;
                        }
@@ -1372,13 +1370,10 @@ static void pixbuf_draw_fade_linear(guchar *p_pix, gint prs, gboolean has_alpha,
                for (i = x1; i < x2; i++)
                        {
                        if (vertical) n = a - a * abs(i - s) / border;
-                       *pp = (r * n + *pp * (256-n)) >> 8;
-                       pp++;
-                       *pp = (g * n + *pp * (256-n)) >> 8;
-                       pp++;
-                       *pp = (b * n + *pp * (256-n)) >> 8;
-                       pp++;
-                       if (has_alpha) pp++;
+                       pp[0] = (r * n + pp[0] * (256-n)) >> 8;
+                       pp[1] = (g * n + pp[1] * (256-n)) >> 8;
+                       pp[2] = (b * n + pp[2] * (256-n)) >> 8;
+                       pp += p_step;
                        }
                }
 }
@@ -1404,13 +1399,10 @@ static void pixbuf_draw_fade_radius(guchar *p_pix, gint prs, gboolean has_alpha,
 
                        r = MIN(border, (gint)hypot(i - sx, j - sy));
                        n = a - a * r / border;
-                       *pp = (r * n + *pp * (256-n)) >> 8;
-                       pp++;
-                       *pp = (g * n + *pp * (256-n)) >> 8;
-                       pp++;
-                       *pp = (b * n + *pp * (256-n)) >> 8;
-                       pp++;
-                       if (has_alpha) pp++;
+                       pp[0] = (r * n + pp[0] * (256-n)) >> 8;
+                       pp[1] = (g * n + pp[1] * (256-n)) >> 8;
+                       pp[2] = (b * n + pp[2] * (256-n)) >> 8;
+                       pp += p_step;
                        }
                }
 }
@@ -1561,21 +1553,20 @@ void pixbuf_desaturate_rect(GdkPixbuf *pb,
        prs = gdk_pixbuf_get_rowstride(pb);
        p_pix = gdk_pixbuf_get_pixels(pb);
 
+       const gint p_step = has_alpha ? 4 : 3;
+
        for (i = 0; i < h; i++)
                {
-               pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
+               pp = p_pix + (y + i) * prs + (x * p_step);
                for (j = 0; j < w; j++)
                        {
                        guint8 grey;
 
                        grey = (pp[0] + pp[1] + pp[2]) / 3;
-                       *pp = grey;
-                       pp++;
-                       *pp = grey;
-                       pp++;
-                       *pp = grey;
-                       pp++;
-                       if (has_alpha) pp++;
+                       pp[0] = grey;
+                       pp[1] = grey;
+                       pp[2] = grey;
+                       pp += p_step;
                        }
                }
 }
@@ -1607,26 +1598,20 @@ void pixbuf_highlight_overunderexposed(GdkPixbuf *pb, gint x, gint y, gint w, gi
        prs = gdk_pixbuf_get_rowstride(pb);
        p_pix = gdk_pixbuf_get_pixels(pb);
 
+       const gint p_step = has_alpha ? 4 : 3;
+
        for (i = 0; i < h; i++)
                {
-               pp = p_pix + (y + i) * prs + (x * (has_alpha ? 4 : 3));
+               pp = p_pix + (y + i) * prs + (x * p_step);
                for (j = 0; j < w; j++)
                        {
                        if (pp[0] == 255 || pp[1] == 255 || pp[2] == 255 || pp[0] == 0 || pp[1] == 0 || pp[2] == 0)
                                {
-                               *pp = 255;
-                               pp++;
-                               *pp = 0;
-                               pp++;
-                               *pp = 0;
-                               pp++;
-                               if (has_alpha) pp++;
-                               }
-                       else
-                               {
-                               pp = pp + 3;
-                               if (has_alpha) pp++;
+                               pp[0] = 255;
+                               pp[1] = 0;
+                               pp[2] = 0;
                                }
+                       pp += p_step;
                        }
                }
 }
index 6b9a641..a2ae52c 100644 (file)
@@ -247,10 +247,10 @@ void image_sim_fill_data(ImageSimilarityData *sd, GdkPixbuf *pixbuf)
                                p = xpos + (y * rs);
                                for (x = i; x < i + x_inc; x++)
                                        {
-                                       r += *p; p++;
-                                       g += *p; p++;
-                                       b += *p; p++;
-                                       if (has_alpha) p++;
+                                       r += p[0];
+                                       g += p[1];
+                                       b += p[2];
+                                       p += p_step;
                                        }
                                }