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