[Pixman] [PATCH 2/4] Rename color_correct() to round_color()

Søren Sandmann sandmann at cs.au.dk
Tue Jan 10 14:29:21 PST 2012


From: Søren Sandmann Pedersen <ssp at redhat.com>

And do the rounding from float to int in the same way cairo does: by
multiplying with (1 << width), then subtracting one when the input was 1.0.
---
 test/composite.c |    8 ++++----
 test/utils.c     |   34 ++++++++++++++++++++--------------
 test/utils.h     |    3 +--
 3 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/test/composite.c b/test/composite.c
index edf7257..201c5b8 100644
--- a/test/composite.c
+++ b/test/composite.c
@@ -640,7 +640,7 @@ composite_test (image_t *dst,
 	tmsk = *mask->color;
 	if (mask->size)
 	{
-	    color_correct (mask->format->format, &tmsk);
+	    round_color (mask->format->format, &tmsk);
 
 	    if (component_alpha &&
 		PIXMAN_FORMAT_R (mask->format->format) == 0)
@@ -663,13 +663,13 @@ composite_test (image_t *dst,
     get_pixel (dst->image, dst->format->format, &result);
 
     tdst = *dst->color;
-    color_correct (dst->format->format, &tdst);
+    round_color (dst->format->format, &tdst);
     tsrc = *src->color;
     if (src->size)
-	color_correct (src->format->format, &tsrc);
+	round_color (src->format->format, &tsrc);
     do_composite (op->op, &tsrc, mask ? &tmsk : NULL, &tdst,
 		  &expected, component_alpha);
-    color_correct (dst->format->format, &expected);
+    round_color (dst->format->format, &expected);
 
     diff = eval_diff (&expected, &result, dst->format->format);
 
diff --git a/test/utils.c b/test/utils.c
index 038fd2b..17bca28 100644
--- a/test/utils.c
+++ b/test/utils.c
@@ -704,14 +704,23 @@ initialize_palette (pixman_indexed_t *palette, uint32_t depth, int is_rgb)
     }
 }
 
-void
-color_correct (pixman_format_code_t format,
-	       color_t *color)
+static double
+round_channel (double p, int m)
 {
-#define MASK(x) ((1 << (x)) - 1)
-#define round_pix(pix, m)						\
-    ((int)((pix) * (MASK(m)) + .5) / (double) (MASK(m)))
+    int t;
+    double r;
+
+    t = p * ((1 << m));
+    t -= t >> m;
+
+    r = t / (double)((1 << m) - 1);
+
+    return r;
+}
 
+void
+round_color (pixman_format_code_t format, color_t *color)
+{
     if (PIXMAN_FORMAT_R (format) == 0)
     {
 	color->r = 0.0;
@@ -720,16 +729,13 @@ color_correct (pixman_format_code_t format,
     }
     else
     {
-	color->r = round_pix (color->r, PIXMAN_FORMAT_R (format));
-	color->g = round_pix (color->g, PIXMAN_FORMAT_G (format));
-	color->b = round_pix (color->b, PIXMAN_FORMAT_B (format));
+	color->r = round_channel (color->r, PIXMAN_FORMAT_R (format));
+	color->g = round_channel (color->g, PIXMAN_FORMAT_G (format));
+	color->b = round_channel (color->b, PIXMAN_FORMAT_B (format));
     }
 
     if (PIXMAN_FORMAT_A (format) == 0)
-	color->a = 1.0;
+	color->a = 1;
     else
-	color->a = round_pix (color->a, PIXMAN_FORMAT_A (format));
-
-#undef round_pix
-#undef MASK
+	color->a = round_channel (color->a, PIXMAN_FORMAT_A (format));
 }
diff --git a/test/utils.h b/test/utils.h
index fbbd30b..22abbc2 100644
--- a/test/utils.h
+++ b/test/utils.h
@@ -158,5 +158,4 @@ typedef struct
 } color_t;
 
 void
-color_correct (pixman_format_code_t format,
-	       color_t *color);
+round_color (pixman_format_code_t format, color_t *color);
-- 
1.7.4



More information about the Pixman mailing list