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

Last change on this file since 2778 was 2778, checked in by Jean-Yves Lamoureux, 14 years ago
  • Moved *line_template.c to .h files to avoid problems with autotools (sam)
  • Added antialiasing support to polylines
  • Ho, and previous commit (r2777) contains width/height/pitch accessors to pipi_image_t, too. LOL.
File size: 8.4 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 * 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, int aa)
112{
113    int i;
114    struct line s;
115
116    if(img->last_modified == PIPI_PIXELS_RGBA_C)
117    {
118        if(!aa)
119        {
120            uint32_t  *dstdata;
121            dstdata = (uint32_t *)pipi_getpixels(img, PIPI_PIXELS_RGBA_C)->pixels;
122            s.color32 = c;
123            s.buf_u32 = dstdata;
124            s.draw = draw_aliased_line_u32;
125        }
126        else
127        {
128            float  *dstdata;
129            dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels;
130            s.colorf[2] = ((c&0x00FF0000)>>16)/255.0f; /* XXX FIXME */
131            s.colorf[1] = ((c&0x0000FF00)>>8)/255.0f;  /* XXX FIXME */
132            s.colorf[0] = (c&0x000000FF)/255.0f;       /* XXX FIXME */
133            s.buf_f = dstdata;
134            s.draw = draw_antialiased_line_float;
135        }
136    }
137    else if(img->last_modified == PIPI_PIXELS_Y_F)
138    {
139        float  *dstdata;
140        dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_Y_F)->pixels;
141        s.colorf[0] = c/255.0f; /* XXX FIXME */
142        s.buf_f = dstdata;
143        s.draw = aa==0?draw_aliased_line_gray:draw_antialiased_line_gray;
144    }
145    else
146    {
147        float  *dstdata;
148        dstdata = (float *)pipi_getpixels(img, PIPI_PIXELS_RGBA_F)->pixels;
149        s.colorf[0] = (c&0x00FF0000)/255.0f; /* XXX FIXME */
150        s.colorf[1] = (c&0x0000FF00)/255.0f; /* XXX FIXME */
151        s.colorf[2] = (c&0x000000FF)/255.0f; /* XXX FIXME */
152        s.buf_f = dstdata;
153        s.draw = aa==0?draw_aliased_line_float:draw_antialiased_line_float;
154        img->last_modified = PIPI_PIXELS_RGBA_F;
155    }
156
157    for(i = 0; i < n; i++)
158    {
159        s.x1 = x[i];
160        s.y1 = y[i];
161        s.x2 = x[i+1];
162        s.y2 = y[i+1];
163        clip_line(img, &s);
164    }
165    return 0;
166}
167
168
169
170
171
172/*
173 * XXX: The following functions are local.
174 */
175/* Generic Cohen-Sutherland line clipping function. */
176static void clip_line(pipi_image_t *img, struct line* s)
177{
178    uint8_t bits1, bits2;
179
180    bits1 = clip_bits(img, s->x1, s->y1);
181    bits2 = clip_bits(img, s->x2, s->y2);
182
183    if(bits1 & bits2)
184        return;
185
186    if(bits1 == 0)
187    {
188        if(bits2 == 0)
189            s->draw(img, s);
190        else
191        {
192            int tmp;
193            tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
194            tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
195            clip_line(img, s);
196        }
197        return;
198    }
199
200    if(bits1 & (1<<0))
201    {
202        s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
203        s->x1 = 0;
204    }
205    else if(bits1 & (1<<1))
206    {
207        int xmax = img->w - 1;
208        s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
209        s->x1 = xmax;
210    }
211    else if(bits1 & (1<<2))
212    {
213        s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
214        s->y1 = 0;
215    }
216    else if(bits1 & (1<<3))
217    {
218        int ymax = img->h - 1;
219        s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
220        s->y1 = ymax;
221    }
222
223    clip_line(img, s);
224}
225
226/* Helper function for clip_line(). */
227static uint8_t clip_bits(pipi_image_t *img, int x, int y)
228{
229    uint8_t b = 0;
230
231    if(x < 0)
232        b |= (1<<0);
233    else if(x >= (int)img->w)
234        b |= (1<<1);
235
236    if(y < 0)
237        b |= (1<<2);
238    else if(y >= (int)img->h)
239        b |= (1<<3);
240
241    return b;
242}
243
244
245
246/* Solid line drawing function, using Bresenham's mid-point line
247 * scan-conversion algorithm. */
248static void draw_aliased_line_u32(pipi_image_t *img, struct line* s)
249{
250#undef  ASSIGN
251#define ASSIGN(x, y, w) s->buf_u32[x+y*w] = s->color32;
252#include "line_template.h"
253}
254static void draw_aliased_line_float(pipi_image_t *img, struct line* s)
255{
256#undef  ASSIGN
257#define ASSIGN(x, y, w) s->buf_f[(x*4)+y*(w*4)]     = s->colorf[0]; \
258                         s->buf_f[1 + (x*4)+y*(w*4)] = s->colorf[1]; \
259                         s->buf_f[2 + (x*4)+y*(w*4)] = s->colorf[2];
260#include "line_template.h"
261}
262static void draw_aliased_line_gray(pipi_image_t *img, struct line* s)
263{
264#undef  ASSIGN
265#define ASSIGN(x, y, w) s->buf_f[x+y*w]     = s->colorf[0];
266#include "line_template.h"
267}
268
269/* Xiaolin Wu's line algorithm, as seen at http://portal.acm.org/citation.cfm?id=122734 */
270
271/* math.h doesn't like y0 (sucker) */
272float floorf(float x);
273float truncf(float x);
274float fabsf(float x);
275static float fractf(float d) { return (d - floorf(d)); }
276static float fractinvf(float d) { return (1 - (d - floorf(d))); }
277
278static void draw_antialiased_line_float(pipi_image_t *img, struct line* s)
279{
280/* Is that an horrible mess ? Yes, it is. */
281#undef  PLOT
282#define PLOT(x, y, c)  \
283    s->buf_f[(((int)(x)*4))+((int)(y))*(img->w*4)] = \
284        (c*s->colorf[0]) + (1-c) * s->buf_f[(((int)(x)*4))+((int)(y))*(img->w*4)]; \
285    s->buf_f[(1+((int)(x)*4))+((int)(y))*(img->w*4)] = \
286        (c*s->colorf[1]) + (1-c) * s->buf_f[(1+((int)(x)*4))+((int)(y))*(img->w*4)]; \
287    s->buf_f[(2+((int)(x)*4))+((int)(y))*(img->w*4)] = \
288        (c*s->colorf[2]) + (1-c) * s->buf_f[(2+((int)(x)*4))+((int)(y))*(img->w*4)];
289#include "aline_template.h"
290}
291
292
293static void draw_antialiased_line_gray(pipi_image_t *img, struct line* s)
294{
295#undef  PLOT
296#define PLOT(x, y, c) s->buf_f[((int)(x))+((int)(y))*img->w] =  \
297    (c*s->colorf[0]) + (1-c) * s->buf_f[((int)(x))+((int)(y))*img->w];
298#include "aline_template.h"
299}
Note: See TracBrowser for help on using the repository browser.