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