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.4 2006/08/01 12:22:00 bernie
20 *#* gfx_findRegion(): Only define when CONFIG_GFX_CLIPPING is enabled.
22 *#* Revision 1.3 2006/07/19 12:56:26 bernie
23 *#* Convert to new Doxygen style.
25 *#* Revision 1.2 2006/02/10 12:26:58 bernie
26 *#* Check CONFIG_* constraints.
28 *#* Revision 1.1 2006/01/24 02:17:49 bernie
29 *#* Split out gfx.c into bitmap.c and line.c.
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 */
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
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
50 * Draw a sloped line without performing clipping.
52 * Parameters are the same of gfx_line().
53 * This routine is based on the Bresenham Line-Drawing Algorithm.
55 * \note Passing coordinates outside the bitmap boundaries will
56 * result in memory trashing.
58 * \todo Optimize for vertical and horiziontal lines.
62 static void gfx_lineUnclipped(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
64 int x, y, e, len, adx, ady, signx, signy;
97 /* X-major line (octants 1/4/5/8) */
104 ASSERT((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
117 /* Y-major line (octants 2/3/6/7) */
124 ASSERT ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
137 #if CONFIG_GFX_CLIPPING
139 /// Helper routine for gfx_line().
140 static int gfx_findRegion(int x, int y, Rect *cr)
145 code |= 1; /* below */
146 else if (y < cr->ymin)
147 code |= 2; /* above */
150 code |= 4; /* right */
151 else if (x < cr->xmin)
152 code |= 8; /* left */
157 #endif /* CONFIG_CLIPPING */
160 * Draw a sloped line segment.
162 * Draw a sloped line segment identified by the provided
163 * start and end coordinates on the bitmap \a bm.
165 * The line endpoints are clipped inside the current bitmap
166 * clipping rectangle using the Cohen-Sutherland algorithm,
167 * which is very fast.
169 * \note The point at coordinates \a x2 \a y2 is not drawn.
171 * \note This function does \b not update the current pen position.
173 * \todo Compute updated Bresenham error term.
175 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
177 #if CONFIG_GFX_CLIPPING
178 int clip1 = gfx_findRegion(x1, y1, &bm->cr);
179 int clip2 = gfx_findRegion(x2, y2, &bm->cr);
181 /* Loop while there is at least one point outside */
182 while (clip1 | clip2)
184 /* Check for line totally outside */
188 int c = clip1 ? clip1 : clip2;
191 if (c & 1) /* Below */
193 x = x1 + (x2 - x1) * (bm->cr.ymax - y1) / (y2 - y1);
196 else if (c & 2) /* Above */
198 x = x1 + (x2 - x1) * (bm->cr.ymin - y1) / (y2 - y1);
201 else if (c & 4) /* Right */
203 y = y1 + (y2 - y1) * (bm->cr.xmax - x1) / (x2 - x1);
208 y = y1 + (y2 - y1) * (bm->cr.xmin - x1) / (x2 - x1);
212 if (c == clip1) /* First endpoint was clipped */
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);
221 clip1 = gfx_findRegion(x1, y1, &bm->cr);
223 else /* Second endpoint was clipped */
227 clip2 = gfx_findRegion(x2, y2, &bm->cr);
230 #endif /* CONFIG_GFX_CLIPPING */
232 gfx_lineUnclipped(bm, x1, y1, x2, y2);
236 * Move the current pen position to the specified coordinates.
238 * The pen position is used for drawing operations such as
239 * gfx_lineTo(), which can be used to draw polygons.
241 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
248 * Draw a line from the current pen position to the new coordinates.
250 * \note This function moves the current pen position to the
255 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
257 gfx_line(bm, bm->penX, bm->penY, x, y);
258 gfx_moveTo(bm, x, y);
263 * Draw the perimeter of an hollow rectangle.
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.
268 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
270 /* Sort coords (needed for correct bottom-right semantics) */
271 if (x1 > x2) SWAP(x1, x2);
272 if (y1 > y2) SWAP(y1, y2);
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);
283 * Fill a rectangular area with \a color.
285 * \note The bottom-right border of the rectangle is not drawn.
287 * \note This function does \b not update the current pen position.
289 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
294 if (x1 > x2) SWAP(x1, x2);
295 if (y1 > y2) SWAP(y1, y2);
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 */
309 /* NOTE: Code paths are duplicated for efficiency */
310 if (color) /* fill */
312 for (x = x1; x < x2; x++)
313 for (y = y1; y < y2; y++)
318 for (x = x1; x < x2; x++)
319 for (y = y1; y < y2; y++)
326 * Draw a filled rectangle.
328 * \note The bottom-right border of the rectangle is not drawn.
330 * \note This function does \b not update the current pen position.
332 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
334 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
339 * Clear a rectangular area.
341 * \note The bottom-right border of the rectangle is not cleared.
343 * \note This function does \b not update the current pen position.
345 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
347 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
351 #if CONFIG_GFX_VCOORDS
353 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
354 * al rettangolo di clipping della bitmap.
356 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
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);
366 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
367 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
373 * Transform a coordinate from the current reference system to a
374 * pixel offset within the bitmap.
376 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
378 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
382 * Transform a coordinate from the current reference system to a
383 * pixel offset within the bitmap.
385 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
387 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
392 * Draw a line from (x1;y1) to (x2;y2).
394 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
397 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
398 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
400 #endif /* CONFIG_GFX_VCOORDS */