Rename color_correct() to round_color()
authorSøren Sandmann Pedersen <ssp@redhat.com>
Tue, 20 Dec 2011 00:53:28 +0000 (19:53 -0500)
committerSøren Sandmann Pedersen <ssp@redhat.com>
Tue, 10 Jan 2012 14:04:45 +0000 (09:04 -0500)
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
test/utils.c
test/utils.h

index edf7257..201c5b8 100644 (file)
@@ -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);
 
index 038fd2b..17bca28 100644 (file)
@@ -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));
 }
index fbbd30b..22abbc2 100644 (file)
@@ -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);