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