Optimize away divisions in RAST_ADDR/MASK macros.
[bertos.git] / gfx / line.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  * \author Stefano Fedrigo <aleph@develer.com>
13  *
14  * \brief Line drawing graphics routines
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.2  2006/02/10 12:26:58  bernie
20  *#* Check CONFIG_* constraints.
21  *#*
22  *#* Revision 1.1  2006/01/24 02:17:49  bernie
23  *#* Split out gfx.c into bitmap.c and line.c.
24  *#*
25  *#*/
26
27 #include "gfx.h"
28 #include "gfx_p.h"
29
30 #include <cfg/debug.h>   /* ASSERT() */
31 #include <cfg/cpu.h>     /* CPU_HARVARD */
32 #include <cfg/macros.h>  /* SWAP() */
33 #include <appconfig.h>   /* CONFIG_GFX_CLIPPING */
34
35 /* Configuration sanity checks */
36 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
37         #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
38 #endif
39 #if !defined(CONFIG_GFX_VCOORDS) || (CONFIG_GFX_VCOORDS != 0 && CONFIG_GFX_VCOORDS != 1)
40         #error CONFIG_GFX_VCOORDS must be defined to either 0 or 1
41 #endif
42
43 /*!
44  * Draw a sloped line without performing clipping.
45  *
46  * Parameters are the same of gfx_line().
47  * This routine is based on the Bresenham Line-Drawing Algorithm.
48  *
49  * \note Passing coordinates outside the bitmap boundaries will
50  *       result in memory trashing.
51  *
52  * \todo Optimize for vertical and horiziontal lines.
53  *
54  * \sa gfx_line()
55  */
56 static void gfx_lineUnclipped(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
57 {
58         int x, y, e, len, adx, ady, signx, signy;
59
60         if (x2 > x1)
61         {
62                 /* left to right */
63                 signx = +1;
64                 adx = x2 - x1;
65         }
66         else
67         {
68                 /* right to left */
69                 signx = -1;
70                 adx = x1 - x2;
71         }
72
73         if (y2 > y1)
74         {
75                 /* top to bottom */
76                 signy = +1;
77                 ady = y2 - y1;
78         }
79         else
80         {
81                 /* bottom to top */
82                 signy = -1;
83                 ady = y1 - y2;
84         }
85
86         x = x1;
87         y = y1;
88
89         if (adx > ady)
90         {
91                 /* X-major line (octants 1/4/5/8) */
92
93                 len = adx;
94                 e = -adx;
95                 while (len--)
96                 {
97                         /* Sanity check */
98                         ASSERT((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
99                         BM_PLOT(bm, x, y);
100                         x += signx;
101                         e += ady;
102                         if (e >= 0)
103                         {
104                                 y += signy;
105                                 e -= adx;
106                         }
107                 }
108         }
109         else
110         {
111                 /* Y-major line (octants 2/3/6/7) */
112
113                 len = ady;
114                 e = -ady;
115                 while (len--)
116                 {
117                         /* Sanity check */
118                         ASSERT ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
119                         BM_PLOT(bm, x, y);
120                         y += signy;
121                         e += adx;
122                         if (e >= 0)
123                         {
124                                 x += signx;
125                                 e -= ady;
126                         }
127                 }
128         }
129 }
130
131 //! Helper routine for gfx_line().
132 static int gfx_findRegion(int x, int y, Rect *cr)
133 {
134         int code = 0;
135
136         if (y >= cr->ymax)
137                 code |= 1; /* below */
138         else if (y < cr->ymin)
139                 code |= 2; /* above */
140
141         if (x >= cr->xmax)
142                 code |= 4; /* right */
143         else if (x < cr->xmin)
144                 code |= 8; /* left */
145
146         return code;
147 }
148
149 /**
150  * Draw a sloped line segment.
151  *
152  * Draw a sloped line segment identified by the provided
153  * start and end coordinates on the bitmap \a bm.
154  *
155  * The line endpoints are clipped inside the current bitmap
156  * clipping rectangle using the Cohen-Sutherland algorithm,
157  * which is very fast.
158  *
159  * \note The point at coordinates \a x2 \a y2 is not drawn.
160  *
161  * \note This function does \b not update the current pen position.
162  *
163  * \todo Compute updated Bresenham error term.
164  */
165 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
166 {
167 #if CONFIG_GFX_CLIPPING
168         int clip1 = gfx_findRegion(x1, y1, &bm->cr);
169         int clip2 = gfx_findRegion(x2, y2, &bm->cr);
170
171         /* Loop while there is at least one point outside */
172         while (clip1 | clip2)
173         {
174                 /* Check for line totally outside */
175                 if (clip1 & clip2)
176                         return;
177
178                 int c = clip1 ? clip1 : clip2;
179                 int x, y;
180
181                 if (c & 1) /* Below */
182                 {
183                         x = x1 + (x2 - x1) * (bm->cr.ymax - y1) / (y2 - y1);
184                         y = bm->cr.ymax - 1;
185                 }
186                 else if (c & 2) /* Above */
187                 {
188                         x = x1 + (x2 - x1) * (bm->cr.ymin - y1) / (y2 - y1);
189                         y = bm->cr.ymin;
190                 }
191                 else if (c & 4) /* Right */
192                 {
193                         y = y1 + (y2 - y1) * (bm->cr.xmax - x1) / (x2 - x1);
194                         x = bm->cr.xmax - 1;
195                 }
196                 else /* Left */
197                 {
198                         y = y1 + (y2 - y1) * (bm->cr.xmin - x1) / (x2 - x1);
199                         x = bm->cr.xmin;
200                 }
201
202                 if (c == clip1) /* First endpoint was clipped */
203                 {
204                         // TODO: adjust Bresenham error term
205                         //coord_t clipdx = ABS(x - x1);
206                         //coord_t clipdy = ABS(y - y1);
207                         //e += (clipdy * e2) + ((clipdx - clipdy) * e1);
208
209                         x1 = x;
210                         y1 = y;
211                         clip1 = gfx_findRegion(x1, y1, &bm->cr);
212                 }
213                 else /* Second endpoint was clipped */
214                 {
215                         x2 = x;
216                         y2 = y;
217                         clip2 = gfx_findRegion(x2, y2, &bm->cr);
218                 }
219         }
220 #endif /* CONFIG_GFX_CLIPPING */
221
222         gfx_lineUnclipped(bm, x1, y1, x2, y2);
223 }
224
225 /*!
226  * Move the current pen position to the specified coordinates.
227  *
228  * The pen position is used for drawing operations such as
229  * gfx_lineTo(), which can be used to draw polygons.
230  */
231 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
232 {
233         bm->penX = x;
234         bm->penY = y;
235 }
236
237 /*!
238  * Draw a line from the current pen position to the new coordinates.
239  *
240  * \note This function moves the current pen position to the
241  *       new coordinates.
242  *
243  * \sa gfx_line()
244  */
245 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
246 {
247         gfx_line(bm, bm->penX, bm->penY, x, y);
248         gfx_moveTo(bm, x, y);
249 }
250
251
252 /*!
253  * Draw the perimeter of an hollow rectangle.
254  *
255  * \note The bottom-right corner of the rectangle is drawn at (x2-1;y2-1).
256  * \note This function does \b not update the current pen position.
257  */
258 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
259 {
260         /* Sort coords (needed for correct bottom-right semantics) */
261         if (x1 > x2) SWAP(x1, x2);
262         if (y1 > y2) SWAP(y1, y2);
263
264         /* Draw rectangle */
265         gfx_line(bm, x1,   y1,   x2-1, y1);
266         gfx_line(bm, x2-1, y1,   x2-1, y2-1);
267         gfx_line(bm, x2-1, y2-1, x1,   y2-1);
268         gfx_line(bm, x1,   y2-1, x1,   y1);
269 }
270
271
272 /*!
273  * Fill a rectangular area with \a color.
274  *
275  * \note The bottom-right border of the rectangle is not drawn.
276  *
277  * \note This function does \b not update the current pen position.
278  */
279 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
280 {
281         coord_t x, y;
282
283         /* Sort coords */
284         if (x1 > x2) SWAP(x1, x2);
285         if (y1 > y2) SWAP(y1, y2);
286
287 #if CONFIG_GFX_CLIPPING
288         /* Clip rect to bitmap clip region */
289         if (x1 < bm->cr.xmin)   x1 = bm->cr.xmin;
290         if (x2 < bm->cr.xmin)   x2 = bm->cr.xmin;
291         if (x1 > bm->cr.xmax)   x1 = bm->cr.xmax;
292         if (x2 > bm->cr.xmax)   x2 = bm->cr.xmax;
293         if (y1 < bm->cr.ymin)   y1 = bm->cr.ymin;
294         if (y2 < bm->cr.ymin)   y2 = bm->cr.ymin;
295         if (y1 > bm->cr.ymax)   y1 = bm->cr.ymax;
296         if (y2 > bm->cr.ymax)   y2 = bm->cr.ymax;
297 #endif /* CONFIG_GFX_CLIPPING */
298
299         /* NOTE: Code paths are duplicated for efficiency */
300         if (color) /* fill */
301         {
302                 for (x = x1; x < x2; x++)
303                         for (y = y1; y < y2; y++)
304                                 BM_PLOT(bm, x, y);
305         }
306         else /* clear */
307         {
308                 for (x = x1; x < x2; x++)
309                         for (y = y1; y < y2; y++)
310                                 BM_CLEAR(bm, x, y);
311         }
312 }
313
314
315 /*!
316  * Draw a filled rectangle.
317  *
318  * \note The bottom-right border of the rectangle is not drawn.
319  *
320  * \note This function does \b not update the current pen position.
321  */
322 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
323 {
324         gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
325 }
326
327
328 /*!
329  * Clear a rectangular area.
330  *
331  * \note The bottom-right border of the rectangle is not cleared.
332  *
333  * \note This function does \b not update the current pen position.
334  */
335 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
336 {
337         gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
338 }
339
340
341 #if CONFIG_GFX_VCOORDS
342 /*!
343  * Imposta gli estremi del sistema di coordinate cartesiane rispetto
344  * al rettangolo di clipping della bitmap.
345  */
346 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
347 {
348         ASSERT(x1 != x2);
349         ASSERT(y1 != y2);
350
351         bm->orgX    = x1;
352         bm->orgY    = y1;
353         bm->scaleX  = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
354         bm->scaleY  = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
355
356 /*      DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
357                 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
358 */
359 }
360
361
362 /*!
363  * Transform a coordinate from the current reference system to a
364  * pixel offset within the bitmap.
365  */
366 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
367 {
368         return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
369 }
370
371 /*!
372  * Transform a coordinate from the current reference system to a
373  * pixel offset within the bitmap.
374  */
375 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
376 {
377         return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
378 }
379
380
381 /*!
382  * Draw a line from (x1;y1) to (x2;y2).
383  */
384 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
385 {
386         gfx_line(bm,
387                 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
388                 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
389 }
390 #endif /* CONFIG_GFX_VCOORDS */