summaryrefslogtreecommitdiffstats
path: root/gfx/cairo/pixman-dither.patch
blob: 633a8d7282d5ea8216352fe1569d2037e658b01f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
diff --git a/gfx/cairo/libpixman/src/pixman-dither.h b/gfx/cairo/libpixman/src/pixman-dither.h
new file mode 100644
--- /dev/null
+++ b/gfx/cairo/libpixman/src/pixman-dither.h
@@ -0,0 +1,51 @@
+#define R16_BITS     5
+#define G16_BITS     6
+#define B16_BITS     5
+
+#define R16_SHIFT    (B16_BITS + G16_BITS)
+#define G16_SHIFT    (B16_BITS)
+#define B16_SHIFT    0
+
+#define MASK 0xff
+#define ONE_HALF 0x80
+
+#define A_SHIFT 8 * 3
+#define R_SHIFT 8 * 2
+#define G_SHIFT 8
+#define A_MASK 0xff000000
+#define R_MASK 0xff0000
+#define G_MASK 0xff00
+
+#define RB_MASK 0xff00ff
+#define AG_MASK 0xff00ff00
+#define RB_ONE_HALF 0x800080
+#define RB_MASK_PLUS_ONE 0x10000100
+
+#define ALPHA_8(x) ((x) >> A_SHIFT)
+#define RED_8(x) (((x) >> R_SHIFT) & MASK)
+#define GREEN_8(x) (((x) >> G_SHIFT) & MASK)
+#define BLUE_8(x) ((x) & MASK)
+
+// This uses the same dithering technique that Skia does.
+// It is essentially preturbing the lower bit based on the
+// high bit
+static inline uint16_t dither_32_to_16(uint32_t c)
+{
+    uint8_t b = BLUE_8(c);
+    uint8_t g = GREEN_8(c);
+    uint8_t r = RED_8(c);
+    r = ((r << 1) - ((r >> (8 - R16_BITS) << (8 - R16_BITS)) | (r >> R16_BITS))) >> (8 - R16_BITS);
+    g = ((g << 1) - ((g >> (8 - G16_BITS) << (8 - G16_BITS)) | (g >> G16_BITS))) >> (8 - G16_BITS);
+    b = ((b << 1) - ((b >> (8 - B16_BITS) << (8 - B16_BITS)) | (b >> B16_BITS))) >> (8 - B16_BITS);
+    return ((r << R16_SHIFT) | (g << G16_SHIFT) | (b << B16_SHIFT));
+}
+
+static inline uint16_t dither_8888_to_0565(uint32_t color, pixman_bool_t toggle)
+{
+    // alternate between a preturbed truncation and a regular truncation
+    if (toggle) {
+	return dither_32_to_16(color);
+    } else {
+	return CONVERT_8888_TO_0565(color);
+    }
+}
diff --git a/gfx/cairo/libpixman/src/pixman-linear-gradient.c b/gfx/cairo/libpixman/src/pixman-linear-gradient.c
--- a/gfx/cairo/libpixman/src/pixman-linear-gradient.c
+++ b/gfx/cairo/libpixman/src/pixman-linear-gradient.c
@@ -26,16 +26,18 @@
  */
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 #include <stdlib.h>
 #include "pixman-private.h"
 
+#include "pixman-dither.h"
+
 static pixman_bool_t
 linear_gradient_is_horizontal (pixman_image_t *image,
 			       int             x,
 			       int             y,
 			       int             width,
 			       int             height)
 {
     linear_gradient_t *linear = (linear_gradient_t *)image;
@@ -222,25 +224,28 @@ linear_get_scanline_narrow (pixman_iter_
     return iter->buffer;
 }
 
 static uint16_t convert_8888_to_0565(uint32_t color)
 {
     return CONVERT_8888_TO_0565(color);
 }
 
+
+
 static uint32_t *
 linear_get_scanline_16 (pixman_iter_t  *iter,
 			const uint32_t *mask)
 {
     pixman_image_t *image  = iter->image;
     int             x      = iter->x;
     int             y      = iter->y;
     int             width  = iter->width;
     uint16_t *      buffer = (uint16_t*)iter->buffer;
+    pixman_bool_t   toggle = ((x ^ y) & 1);
 
     pixman_vector_t v, unit;
     pixman_fixed_32_32_t l;
     pixman_fixed_48_16_t dx, dy;
     gradient_t *gradient = (gradient_t *)image;
     linear_gradient_t *linear = (linear_gradient_t *)image;
     uint16_t *end = buffer + width;
     pixman_gradient_walker_t walker;
@@ -294,34 +299,47 @@ linear_get_scanline_16 (pixman_iter_t  *
 	    t = ((dx * v.vector[0] + dy * v.vector[1]) - 
 		 (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden;
 	    inc = (dx * unit.vector[0] + dy * unit.vector[1]) * invden;
 	}
 	next_inc = 0;
 
 	if (((pixman_fixed_32_32_t )(inc * width)) == 0)
 	{
-	    register uint16_t color;
+	    register uint32_t color;
+	    uint16_t dither_diff;
+	    uint16_t color16;
+	    uint16_t color16b;
 
-	    color = convert_8888_to_0565(_pixman_gradient_walker_pixel (&walker, t));
-	    while (buffer < end)
-		*buffer++ = color;
+	    color = _pixman_gradient_walker_pixel (&walker, t);
+	    color16 = dither_8888_to_0565(color, toggle);
+	    color16b = dither_8888_to_0565(color, toggle^1);
+	    // compute the difference
+	    dither_diff =  color16 ^ color16b;
+	    while (buffer < end) {
+		*buffer++ = color16;
+		// use dither_diff to toggle between color16 and color16b
+		color16 ^= dither_diff;
+		toggle ^= 1;
+	    }
 	}
 	else
 	{
 	    int i;
 
 	    i = 0;
 	    while (buffer < end)
 	    {
 		if (!mask || *mask++)
 		{
-		    *buffer = convert_8888_to_0565(_pixman_gradient_walker_pixel (&walker,
-										  t + next_inc));
+		    *buffer = dither_8888_to_0565(_pixman_gradient_walker_pixel (&walker,
+										 t + next_inc),
+						  toggle);
 		}
+		toggle ^= 1;
 		i++;
 		next_inc = inc * i;
 		buffer++;
 	    }
 	}
     }
     else
     {
@@ -340,18 +358,20 @@ linear_get_scanline_16 (pixman_iter_t  *
 
 		    invden = pixman_fixed_1 * (double) pixman_fixed_1 /
 			(l * (double) v.vector[2]);
 		    v2 = v.vector[2] * (1. / pixman_fixed_1);
 		    t = ((dx * v.vector[0] + dy * v.vector[1]) - 
 			 (dx * linear->p1.x + dy * linear->p1.y) * v2) * invden;
 		}
 
-		*buffer = convert_8888_to_0565(_pixman_gradient_walker_pixel (&walker, t));
+		*buffer = dither_8888_to_0565(_pixman_gradient_walker_pixel (&walker, t),
+					      toggle);
 	    }
+	    toggle ^= 1;
 
 	    ++buffer;
 
 	    v.vector[0] += unit.vector[0];
 	    v.vector[1] += unit.vector[1];
 	    v.vector[2] += unit.vector[2];
 	}
     }
@@ -369,17 +389,18 @@ linear_get_scanline_wide (pixman_iter_t 
     pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, iter->width);
 
     return buffer;
 }
 
 void
 _pixman_linear_gradient_iter_init (pixman_image_t *image, pixman_iter_t  *iter)
 {
-    if (linear_gradient_is_horizontal (
+    // XXX: we can't use this optimization when dithering
+    if (0 && linear_gradient_is_horizontal (
 	    iter->image, iter->x, iter->y, iter->width, iter->height))
     {
 	if (iter->flags & ITER_16)
 	    linear_get_scanline_16 (iter, NULL);
 	else if (iter->flags & ITER_NARROW)
 	    linear_get_scanline_narrow (iter, NULL);
 	else
 	    linear_get_scanline_wide (iter, NULL);
diff --git a/gfx/cairo/libpixman/src/pixman-radial-gradient.c b/gfx/cairo/libpixman/src/pixman-radial-gradient.c
--- a/gfx/cairo/libpixman/src/pixman-radial-gradient.c
+++ b/gfx/cairo/libpixman/src/pixman-radial-gradient.c
@@ -29,16 +29,18 @@
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
 #endif
 #include <stdlib.h>
 #include <math.h>
 #include "pixman-private.h"
 
+#include "pixman-dither.h"
+
 static inline pixman_fixed_32_32_t
 dot (pixman_fixed_48_16_t x1,
      pixman_fixed_48_16_t y1,
      pixman_fixed_48_16_t z1,
      pixman_fixed_48_16_t x2,
      pixman_fixed_48_16_t y2,
      pixman_fixed_48_16_t z2)
 {
@@ -489,16 +491,17 @@ radial_get_scanline_16 (pixman_iter_t *i
      *   <=> for every p, the radiuses associated with the two t solutions
      *       have opposite sign
      */
     pixman_image_t *image = iter->image;
     int x = iter->x;
     int y = iter->y;
     int width = iter->width;
     uint16_t *buffer = iter->buffer;
+    pixman_bool_t toggle = ((x ^ y) & 1);
 
     gradient_t *gradient = (gradient_t *)image;
     radial_gradient_t *radial = (radial_gradient_t *)image;
     uint16_t *end = buffer + width;
     pixman_gradient_walker_t walker;
     pixman_vector_t v, unit;
 
     /* reference point is the center of the pixel */
@@ -575,25 +578,27 @@ radial_get_scanline_16 (pixman_iter_t *i
 		  unit.vector[0], unit.vector[1], 0);
 	ddc = 2 * dot (unit.vector[0], unit.vector[1], 0,
 		       unit.vector[0], unit.vector[1], 0);
 
 	while (buffer < end)
 	{
 	    if (!mask || *mask++)
 	    {
-		*buffer = convert_8888_to_0565(
+		*buffer = dither_8888_to_0565(
 			  radial_compute_color (radial->a, b, c,
 						radial->inva,
 						radial->delta.radius,
 						radial->mindr,
 						&walker,
-						image->common.repeat));
+						image->common.repeat),
+			  toggle);
 	    }
 
+	    toggle ^= 1;
 	    b += db;
 	    c += dc;
 	    dc += ddc;
 	    ++buffer;
 	}
     }
     else
     {
@@ -621,31 +626,33 @@ radial_get_scanline_16 (pixman_iter_t *i
 			      radial->delta.x, radial->delta.y,
 			      radial->delta.radius);
 		    /*  / pixman_fixed_1 / pixman_fixed_1 */
 
 		    c = fdot (pdx, pdy, -radial->c1.radius,
 			      pdx, pdy, radial->c1.radius);
 		    /*  / pixman_fixed_1 / pixman_fixed_1 */
 
-		    *buffer = convert_8888_to_0565 (
+		    *buffer = dither_8888_to_0565 (
 			      radial_compute_color (radial->a, b, c,
 						    radial->inva,
 						    radial->delta.radius,
 						    radial->mindr,
 						    &walker,
-						    image->common.repeat));
+						    image->common.repeat),
+			      toggle);
 		}
 		else
 		{
 		    *buffer = 0;
 		}
 	    }
 
 	    ++buffer;
+	    toggle ^= 1;
 
 	    v.vector[0] += unit.vector[0];
 	    v.vector[1] += unit.vector[1];
 	    v.vector[2] += unit.vector[2];
 	}
     }
 
     iter->y++;