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.
11 * \author Bernardo Innocenti <bernie@develer.com>
12 * \author Stefano Fedrigo <aleph@develer.com>
14 * \brief Line drawing graphics routines
19 *#* Revision 1.3 2006/07/19 12:56:26 bernie
20 *#* Convert to new Doxygen style.
22 *#* Revision 1.2 2006/02/10 12:26:58 bernie
23 *#* Check CONFIG_* constraints.
25 *#* Revision 1.1 2006/01/24 02:17:49 bernie
26 *#* Split out gfx.c into bitmap.c and line.c.
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 */
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
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
47 * Draw a sloped line without performing clipping.
49 * Parameters are the same of gfx_line().
50 * This routine is based on the Bresenham Line-Drawing Algorithm.
52 * \note Passing coordinates outside the bitmap boundaries will
53 * result in memory trashing.
55 * \todo Optimize for vertical and horiziontal lines.
59 static void gfx_lineUnclipped(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
61 int x, y, e, len, adx, ady, signx, signy;
94 /* X-major line (octants 1/4/5/8) */
101 ASSERT((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
114 /* Y-major line (octants 2/3/6/7) */
121 ASSERT ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
134 /// Helper routine for gfx_line().
135 static int gfx_findRegion(int x, int y, Rect *cr)
140 code |= 1; /* below */
141 else if (y < cr->ymin)
142 code |= 2; /* above */
145 code |= 4; /* right */
146 else if (x < cr->xmin)
147 code |= 8; /* left */
153 * Draw a sloped line segment.
155 * Draw a sloped line segment identified by the provided
156 * start and end coordinates on the bitmap \a bm.
158 * The line endpoints are clipped inside the current bitmap
159 * clipping rectangle using the Cohen-Sutherland algorithm,
160 * which is very fast.
162 * \note The point at coordinates \a x2 \a y2 is not drawn.
164 * \note This function does \b not update the current pen position.
166 * \todo Compute updated Bresenham error term.
168 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
170 #if CONFIG_GFX_CLIPPING
171 int clip1 = gfx_findRegion(x1, y1, &bm->cr);
172 int clip2 = gfx_findRegion(x2, y2, &bm->cr);
174 /* Loop while there is at least one point outside */
175 while (clip1 | clip2)
177 /* Check for line totally outside */
181 int c = clip1 ? clip1 : clip2;
184 if (c & 1) /* Below */
186 x = x1 + (x2 - x1) * (bm->cr.ymax - y1) / (y2 - y1);
189 else if (c & 2) /* Above */
191 x = x1 + (x2 - x1) * (bm->cr.ymin - y1) / (y2 - y1);
194 else if (c & 4) /* Right */
196 y = y1 + (y2 - y1) * (bm->cr.xmax - x1) / (x2 - x1);
201 y = y1 + (y2 - y1) * (bm->cr.xmin - x1) / (x2 - x1);
205 if (c == clip1) /* First endpoint was clipped */
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);
214 clip1 = gfx_findRegion(x1, y1, &bm->cr);
216 else /* Second endpoint was clipped */
220 clip2 = gfx_findRegion(x2, y2, &bm->cr);
223 #endif /* CONFIG_GFX_CLIPPING */
225 gfx_lineUnclipped(bm, x1, y1, x2, y2);
229 * Move the current pen position to the specified coordinates.
231 * The pen position is used for drawing operations such as
232 * gfx_lineTo(), which can be used to draw polygons.
234 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
241 * Draw a line from the current pen position to the new coordinates.
243 * \note This function moves the current pen position to the
248 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
250 gfx_line(bm, bm->penX, bm->penY, x, y);
251 gfx_moveTo(bm, x, y);
256 * Draw the perimeter of an hollow rectangle.
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.
261 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
263 /* Sort coords (needed for correct bottom-right semantics) */
264 if (x1 > x2) SWAP(x1, x2);
265 if (y1 > y2) SWAP(y1, y2);
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);
276 * Fill a rectangular area with \a color.
278 * \note The bottom-right border of the rectangle is not drawn.
280 * \note This function does \b not update the current pen position.
282 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
287 if (x1 > x2) SWAP(x1, x2);
288 if (y1 > y2) SWAP(y1, y2);
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 */
302 /* NOTE: Code paths are duplicated for efficiency */
303 if (color) /* fill */
305 for (x = x1; x < x2; x++)
306 for (y = y1; y < y2; y++)
311 for (x = x1; x < x2; x++)
312 for (y = y1; y < y2; y++)
319 * Draw a filled rectangle.
321 * \note The bottom-right border of the rectangle is not drawn.
323 * \note This function does \b not update the current pen position.
325 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
327 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
332 * Clear a rectangular area.
334 * \note The bottom-right border of the rectangle is not cleared.
336 * \note This function does \b not update the current pen position.
338 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
340 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
344 #if CONFIG_GFX_VCOORDS
346 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
347 * al rettangolo di clipping della bitmap.
349 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
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);
359 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
360 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
366 * Transform a coordinate from the current reference system to a
367 * pixel offset within the bitmap.
369 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
371 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
375 * Transform a coordinate from the current reference system to a
376 * pixel offset within the bitmap.
378 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
380 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
385 * Draw a line from (x1;y1) to (x2;y2).
387 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
390 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
391 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
393 #endif /* CONFIG_GFX_VCOORDS */