source: libpipi/trunk/pipi/paint/line.c @ 2777

Revision 2777, 7.6 KB checked in by jylam, 5 years ago (diff)
  • Added Wu-Xaolin antialiased lines (Still lacks RGBA32 transparency code, with a fallback to float for now)
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 * line.c: line rendering functions
18 */
19
20#include "config.h"
21#include "common.h"
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "pipi.h"
28#include "pipi_internals.h"
29
30struct line
31{
32    int x1, y1;
33    int x2, y2;
34    void (*draw) (pipi_image_t*, struct line*);
35    union {
36        uint32_t color32;
37        float    colorf[3];
38    };
39
40    union {
41        uint32_t *buf_u32;
42        float    *buf_f;
43    };
44
45};
46static void clip_line(pipi_image_t*, struct line*);
47static uint8_t clip_bits(pipi_image_t*, int, int);
48static void draw_aliased_line_u32(pipi_image_t*, struct line*);
49static void draw_aliased_line_gray(pipi_image_t *img, struct line* s);
50static void draw_aliased_line_float(pipi_image_t *img, struct line* s);
51static void draw_antialiased_line_float(pipi_image_t *img, struct line* s);
52static void draw_antialiased_line_gray(pipi_image_t *img, struct line* s);
53
54
55
56int pipi_draw_line(pipi_image_t *img , int x1, int y1, int x2, int y2, uint32_t c, int aa)
57{
58    struct line s;
59    s.x1 = x1;
60    s.y1 = y1;
61    s.x2 = x2;
62    s.y2 = y2;
63
64    /* No Transparency routine for u32 yet, fallback to float version */
65    if(img->last_modified == PIPI_PIXELS_RGBA_C)
66    {
67        if(!aa)
68        {
69            uint32_t  *dstdata;
70            dstdata = (uint32_t *)pipi_getpixels(img, PIPI_PIXELS_RGBA_C)->pixels;
71            s.color32 = c;
72            s.buf_u32 = dstdata;
73            s.draw = draw_aliased_line_u32;
74        }
75        else
76        {
77            float  *dstdata;
78            dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels;
79            s.colorf[2] = ((c&0x00FF0000)>>16)/255.0f; /* XXX FIXME */
80            s.colorf[1] = ((c&0x0000FF00)>>8)/255.0f;  /* XXX FIXME */
81            s.colorf[0] = (c&0x000000FF)/255.0f;       /* XXX FIXME */
82            s.buf_f = dstdata;
83            s.draw = draw_antialiased_line_float;
84        }
85    }
86    else if(img->last_modified == PIPI_PIXELS_Y_F)
87    {
88        float  *dstdata;
89        dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_Y_F)->pixels;
90        s.colorf[0] = c/255.0f; /* XXX FIXME */
91        s.buf_f = dstdata;
92        s.draw = aa==0?draw_aliased_line_gray:draw_antialiased_line_gray;
93    }
94    else
95    {
96        float  *dstdata;
97        dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels;
98        s.colorf[2] = ((c&0x00FF0000)>>16)/255.0f; /* XXX FIXME */
99        s.colorf[1] = ((c&0x0000FF00)>>8)/255.0f;  /* XXX FIXME */
100        s.colorf[0] = (c&0x000000FF)/255.0f;       /* XXX FIXME */
101        s.buf_f = dstdata;
102        s.draw = aa==0?draw_aliased_line_float:draw_antialiased_line_float;
103    }
104
105    clip_line(img, &s);
106    return 0;
107}
108
109
110int pipi_draw_polyline(pipi_image_t *img, int const x[], int const y[],
111                       int n, uint32_t c)
112{
113    int i;
114    struct line s;
115
116    if(img->last_modified == PIPI_PIXELS_RGBA_C)
117    {
118        uint32_t  *dstdata;
119        dstdata = (uint32_t *)pipi_getpixels(img, PIPI_PIXELS_RGBA_C)->pixels;
120        s.color32 = c;
121        s.buf_u32 = dstdata;
122        s.draw = draw_aliased_line_u32;
123    }
124    else
125    {
126        float  *dstdata;
127        dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels;
128        s.colorf[0] = (c&0x00FF0000)/255.0f; /* XXX FIXME */
129        s.colorf[1] = (c&0x0000FF00)/255.0f; /* XXX FIXME */
130        s.colorf[2] = (c&0x000000FF)/255.0f; /* XXX FIXME */
131        s.buf_f = dstdata;
132        s.draw = draw_aliased_line_float;
133        img->last_modified = PIPI_PIXELS_RGBA_F;
134    }
135
136    for(i = 0; i < n; i++)
137    {
138        s.x1 = x[i];
139        s.y1 = y[i];
140        s.x2 = x[i+1];
141        s.y2 = y[i+1];
142        clip_line(img, &s);
143    }
144    return 0;
145}
146
147
148
149
150
151/*
152 * XXX: The following functions are local.
153 */
154/* Generic Cohen-Sutherland line clipping function. */
155static void clip_line(pipi_image_t *img, struct line* s)
156{
157    uint8_t bits1, bits2;
158
159    bits1 = clip_bits(img, s->x1, s->y1);
160    bits2 = clip_bits(img, s->x2, s->y2);
161
162    if(bits1 & bits2)
163        return;
164
165    if(bits1 == 0)
166    {
167        if(bits2 == 0)
168            s->draw(img, s);
169        else
170        {
171            int tmp;
172            tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
173            tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
174            clip_line(img, s);
175        }
176        return;
177    }
178
179    if(bits1 & (1<<0))
180    {
181        s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
182        s->x1 = 0;
183    }
184    else if(bits1 & (1<<1))
185    {
186        int xmax = img->w - 1;
187        s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
188        s->x1 = xmax;
189    }
190    else if(bits1 & (1<<2))
191    {
192        s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
193        s->y1 = 0;
194    }
195    else if(bits1 & (1<<3))
196    {
197        int ymax = img->h - 1;
198        s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
199        s->y1 = ymax;
200    }
201
202    clip_line(img, s);
203}
204
205/* Helper function for clip_line(). */
206static uint8_t clip_bits(pipi_image_t *img, int x, int y)
207{
208    uint8_t b = 0;
209
210    if(x < 0)
211        b |= (1<<0);
212    else if(x >= (int)img->w)
213        b |= (1<<1);
214
215    if(y < 0)
216        b |= (1<<2);
217    else if(y >= (int)img->h)
218        b |= (1<<3);
219
220    return b;
221}
222
223
224
225/* Solid line drawing function, using Bresenham's mid-point line
226 * scan-conversion algorithm. */
227static void draw_aliased_line_u32(pipi_image_t *img, struct line* s)
228{
229#undef  ASSIGN
230#define ASSIGN(x, y, w) s->buf_u32[x+y*w] = s->color32;
231#include "line_template.c"
232}
233static void draw_aliased_line_float(pipi_image_t *img, struct line* s)
234{
235#undef  ASSIGN
236#define ASSIGN(x, y, w) s->buf_f[(x*4)+y*(w*4)]     = s->colorf[0]; \
237                         s->buf_f[1 + (x*4)+y*(w*4)] = s->colorf[1]; \
238                         s->buf_f[2 + (x*4)+y*(w*4)] = s->colorf[2];
239#include "line_template.c"
240}
241static void draw_aliased_line_gray(pipi_image_t *img, struct line* s)
242{
243#undef  ASSIGN
244#define ASSIGN(x, y, w) s->buf_f[x+y*w]     = s->colorf[0];
245#include "line_template.c"
246}
247
248/* Xiaolin Wu's line algorithm, as seen at http://portal.acm.org/citation.cfm?id=122734 */
249
250/* math.h doesn't like y0 (sucker) */
251float floorf(float x);
252float truncf(float x);
253float fabsf(float x);
254static float fractf(float d) { return (d - floorf(d)); }
255static float fractinvf(float d) { return (1 - (d - floorf(d))); }
256
257static void draw_antialiased_line_float(pipi_image_t *img, struct line* s)
258{
259/* Is that an horrible mess ? Yes, it is. */
260#undef  PLOT
261#define PLOT(x, y, c)  \
262    s->buf_f[(((int)(x)*4))+((int)(y))*(img->w*4)] = \
263        (c*s->colorf[0]) + (1-c) * s->buf_f[(((int)(x)*4))+((int)(y))*(img->w*4)]; \
264    s->buf_f[(1+((int)(x)*4))+((int)(y))*(img->w*4)] = \
265        (c*s->colorf[1]) + (1-c) * s->buf_f[(1+((int)(x)*4))+((int)(y))*(img->w*4)]; \
266    s->buf_f[(2+((int)(x)*4))+((int)(y))*(img->w*4)] = \
267        (c*s->colorf[2]) + (1-c) * s->buf_f[(2+((int)(x)*4))+((int)(y))*(img->w*4)];
268#include "aline_template.c"
269}
270
271
272static void draw_antialiased_line_gray(pipi_image_t *img, struct line* s)
273{
274#undef  PLOT
275#define PLOT(x, y, c) s->buf_f[((int)(x))+((int)(y))*img->w] =  \
276    (c*s->colorf[0]) + (1-c) * s->buf_f[((int)(x))+((int)(y))*img->w];
277#include "aline_template.c"
278}
Note: See TracBrowser for help on using the repository browser.