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