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