source: libpipi/trunk/pipi/paint/aline_template.h @ 2788

Last change on this file since 2788 was 2788, checked in by Jean-Yves Lamoureux, 14 years ago
  • Added a bezier curve primitive (2 control points).
  • Fixed a float overflow in antialiased lines (this algorithm is a mess, and I need to rewrite it)
  • Wrote a bunch of craderies degueulasses to avoid having y1 already defined in math.h
  • Did I say this antialiased line implementation sucks ?
File size: 2.9 KB
Line 
1/*
2 *  libpipi       Proper image processing implementation library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org
5 *                All Rights Reserved
6 *
7 *  $Id$
8 *
9 *  This library is free software. It comes without any warranty, to
10 *  the extent permitted by applicable law. You can redistribute it
11 *  and/or modify it under the terms of the Do What The Fuck You Want
12 *  To Public License, Version 2, as published by Sam Hocevar. See
13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16/*
17 * aline_template.c: antialiased (Wu) line rendering template
18 */
19
20float x1 = s->x1, y1 = s->y1, x2 = s->x2, y2 = s->y2;
21float g, xd, yd, xgap, xend, yend, xf, yf, val1, val2;
22int x, y, ix1, ix2, iy1, iy2;
23
24xd = x2 - x1;
25yd = y2 - y1;
26
27/* "Horizontal" line (X greater than Y)*/
28if (fabsf(xd) > fabsf(yd)) {
29                if (x1 > x2)
30    {
31        float tmp;
32        tmp = x1; x1 = x2; x2 = tmp;
33        tmp = y1; y1 = y2; y2 = tmp;
34        xd = (x2-x1);
35        yd = (y2-y1);
36                }
37                g = yd/xd;
38
39                xend = truncf(x1+0.5);
40                yend = y1 + g*(xend-x1);
41                xgap = fractinvf(x1+0.5);
42                ix1 = (int)xend;
43                iy1 = (int)yend;
44                val1 = fractinvf(yend)*xgap;
45                val2 = fractf(yend)*xgap;
46
47    PLOT(ix1, iy1,   val1);
48    PLOT(ix1, (iy1+1)<y1?(iy1+1):iy1, val2);
49
50                yf = yend+g;
51                xend = truncf(x2+0.5);
52                yend = y2 + g*(xend-x2);
53                xgap = fractinvf(x2-0.5);
54                ix2 = (int)xend;
55                iy2 = (int)yend;
56                val1 = fractinvf(yend)*xgap;
57                val2 = fractf(yend)*xgap;
58
59    PLOT(ix2, iy2,   val1);
60    PLOT(ix2, iy2+1<y2?iy2+1:iy2, val2);
61
62                for (x = (ix1+1); x < ix2; x++)
63    {
64        float focus;
65
66        val1 = fractinvf(yf);
67        val2 = fractf(yf);
68        focus = (1.0 - fabsf(val1-val2));
69        val1 += 0.3*focus;
70        val2 += 0.3*focus;
71
72        PLOT(x, yf, val1);
73        PLOT(x, (yf+1)<y1?(yf+1):yf, val2);
74
75        yf = yf + g;
76                }
77}
78/* "Vertical" line (Y greater than X)*/
79else {
80                if (x1 > x2)
81    {
82        float tmp;
83        tmp = x1; x1 = x2; x2 = tmp;
84        tmp = y1; y1 = y2; y2 = tmp;
85        xd = (x2-x1);
86        yd = (y2-y1);
87                }
88
89                g = xd/yd;
90
91                xend = truncf(x1+0.5);
92                yend = y1 + g*(xend-x1);
93                xgap = fractf(x1+0.5);
94                ix1 = (int)xend;
95                iy1 = (int)yend;
96                val1 = fractinvf(yend)*xgap;
97                val2 = fractf(yend)*xgap;
98
99    PLOT(ix1, iy1, val1);
100    PLOT(ix1, (iy1+1)<y1?(iy1+1):iy1, val2);
101
102                xf = xend + g;
103
104                xend = truncf(x2+0.5);
105                yend = y2 + g*(xend-x2);
106                xgap = fractinvf(x2-0.5);
107                ix2 = (int)xend;
108                iy2 = (int)yend;
109                val1 = fractinvf(yend)*xgap;
110                val2 = fractf(yend)*xgap;
111
112    PLOT(ix2, iy2,   val1);
113    PLOT(ix2, (iy2+1)<y2?(iy2+1):iy2, val2);
114
115
116                for (y = (iy1+1); y < iy2; y++)
117    {
118        float focus;
119        int   vx = xf;
120        val1 = fractinvf(xf);
121        val2 = fractf(xf);
122        focus = (1.0 - fabsf(val1-val2));
123        val1 += 0.3*focus;
124        val2 += 0.3*focus;
125        PLOT(vx, y, val1);
126        vx++;
127        PLOT(vx, y, val2);
128        xf = xf + g;
129                }
130}
Note: See TracBrowser for help on using the repository browser.