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.2 2006/02/10 12:26:58 bernie
20 *#* Check CONFIG_* constraints.
22 *#* Revision 1.1 2006/01/24 02:17:49 bernie
23 *#* Split out gfx.c into bitmap.c and line.c.
30 #include <cfg/debug.h> /* ASSERT() */
31 #include <cfg/cpu.h> /* CPU_HARVARD */
32 #include <cfg/macros.h> /* SWAP() */
33 #include <appconfig.h> /* CONFIG_GFX_CLIPPING */
35 /* Configuration sanity checks */
36 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
37 #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
39 #if !defined(CONFIG_GFX_VCOORDS) || (CONFIG_GFX_VCOORDS != 0 && CONFIG_GFX_VCOORDS != 1)
40 #error CONFIG_GFX_VCOORDS must be defined to either 0 or 1
44 * Draw a sloped line without performing clipping.
46 * Parameters are the same of gfx_line().
47 * This routine is based on the Bresenham Line-Drawing Algorithm.
49 * \note Passing coordinates outside the bitmap boundaries will
50 * result in memory trashing.
52 * \todo Optimize for vertical and horiziontal lines.
56 static void gfx_lineUnclipped(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
58 int x, y, e, len, adx, ady, signx, signy;
91 /* X-major line (octants 1/4/5/8) */
98 ASSERT((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
111 /* Y-major line (octants 2/3/6/7) */
118 ASSERT ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
131 //! Helper routine for gfx_line().
132 static int gfx_findRegion(int x, int y, Rect *cr)
137 code |= 1; /* below */
138 else if (y < cr->ymin)
139 code |= 2; /* above */
142 code |= 4; /* right */
143 else if (x < cr->xmin)
144 code |= 8; /* left */
150 * Draw a sloped line segment.
152 * Draw a sloped line segment identified by the provided
153 * start and end coordinates on the bitmap \a bm.
155 * The line endpoints are clipped inside the current bitmap
156 * clipping rectangle using the Cohen-Sutherland algorithm,
157 * which is very fast.
159 * \note The point at coordinates \a x2 \a y2 is not drawn.
161 * \note This function does \b not update the current pen position.
163 * \todo Compute updated Bresenham error term.
165 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
167 #if CONFIG_GFX_CLIPPING
168 int clip1 = gfx_findRegion(x1, y1, &bm->cr);
169 int clip2 = gfx_findRegion(x2, y2, &bm->cr);
171 /* Loop while there is at least one point outside */
172 while (clip1 | clip2)
174 /* Check for line totally outside */
178 int c = clip1 ? clip1 : clip2;
181 if (c & 1) /* Below */
183 x = x1 + (x2 - x1) * (bm->cr.ymax - y1) / (y2 - y1);
186 else if (c & 2) /* Above */
188 x = x1 + (x2 - x1) * (bm->cr.ymin - y1) / (y2 - y1);
191 else if (c & 4) /* Right */
193 y = y1 + (y2 - y1) * (bm->cr.xmax - x1) / (x2 - x1);
198 y = y1 + (y2 - y1) * (bm->cr.xmin - x1) / (x2 - x1);
202 if (c == clip1) /* First endpoint was clipped */
204 // TODO: adjust Bresenham error term
205 //coord_t clipdx = ABS(x - x1);
206 //coord_t clipdy = ABS(y - y1);
207 //e += (clipdy * e2) + ((clipdx - clipdy) * e1);
211 clip1 = gfx_findRegion(x1, y1, &bm->cr);
213 else /* Second endpoint was clipped */
217 clip2 = gfx_findRegion(x2, y2, &bm->cr);
220 #endif /* CONFIG_GFX_CLIPPING */
222 gfx_lineUnclipped(bm, x1, y1, x2, y2);
226 * Move the current pen position to the specified coordinates.
228 * The pen position is used for drawing operations such as
229 * gfx_lineTo(), which can be used to draw polygons.
231 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
238 * Draw a line from the current pen position to the new coordinates.
240 * \note This function moves the current pen position to the
245 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
247 gfx_line(bm, bm->penX, bm->penY, x, y);
248 gfx_moveTo(bm, x, y);
253 * Draw the perimeter of an hollow rectangle.
255 * \note The bottom-right corner of the rectangle is drawn at (x2-1;y2-1).
256 * \note This function does \b not update the current pen position.
258 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
260 /* Sort coords (needed for correct bottom-right semantics) */
261 if (x1 > x2) SWAP(x1, x2);
262 if (y1 > y2) SWAP(y1, y2);
265 gfx_line(bm, x1, y1, x2-1, y1);
266 gfx_line(bm, x2-1, y1, x2-1, y2-1);
267 gfx_line(bm, x2-1, y2-1, x1, y2-1);
268 gfx_line(bm, x1, y2-1, x1, y1);
273 * Fill a rectangular area with \a color.
275 * \note The bottom-right border of the rectangle is not drawn.
277 * \note This function does \b not update the current pen position.
279 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
284 if (x1 > x2) SWAP(x1, x2);
285 if (y1 > y2) SWAP(y1, y2);
287 #if CONFIG_GFX_CLIPPING
288 /* Clip rect to bitmap clip region */
289 if (x1 < bm->cr.xmin) x1 = bm->cr.xmin;
290 if (x2 < bm->cr.xmin) x2 = bm->cr.xmin;
291 if (x1 > bm->cr.xmax) x1 = bm->cr.xmax;
292 if (x2 > bm->cr.xmax) x2 = bm->cr.xmax;
293 if (y1 < bm->cr.ymin) y1 = bm->cr.ymin;
294 if (y2 < bm->cr.ymin) y2 = bm->cr.ymin;
295 if (y1 > bm->cr.ymax) y1 = bm->cr.ymax;
296 if (y2 > bm->cr.ymax) y2 = bm->cr.ymax;
297 #endif /* CONFIG_GFX_CLIPPING */
299 /* NOTE: Code paths are duplicated for efficiency */
300 if (color) /* fill */
302 for (x = x1; x < x2; x++)
303 for (y = y1; y < y2; y++)
308 for (x = x1; x < x2; x++)
309 for (y = y1; y < y2; y++)
316 * Draw a filled rectangle.
318 * \note The bottom-right border of the rectangle is not drawn.
320 * \note This function does \b not update the current pen position.
322 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
324 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
329 * Clear a rectangular area.
331 * \note The bottom-right border of the rectangle is not cleared.
333 * \note This function does \b not update the current pen position.
335 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
337 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
341 #if CONFIG_GFX_VCOORDS
343 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
344 * al rettangolo di clipping della bitmap.
346 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
353 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
354 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
356 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
357 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
363 * Transform a coordinate from the current reference system to a
364 * pixel offset within the bitmap.
366 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
368 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
372 * Transform a coordinate from the current reference system to a
373 * pixel offset within the bitmap.
375 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
377 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
382 * Draw a line from (x1;y1) to (x2;y2).
384 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
387 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
388 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
390 #endif /* CONFIG_GFX_VCOORDS */