4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999 Bernie Innocenti <bernie@codewiz.org>
36 * \author Bernie Innocenti <bernie@codewiz.org>
37 * \author Stefano Fedrigo <aleph@develer.com>
39 * \brief Line drawing graphics routines
46 #include "cfg/cfg_gfx.h" /* CONFIG_GFX_CLIPPING */
47 #include <cfg/debug.h> /* ASSERT() */
48 #include <cfg/macros.h> /* SWAP() */
50 /* Configuration sanity checks */
51 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
52 #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
54 #if !defined(CONFIG_GFX_VCOORDS) || (CONFIG_GFX_VCOORDS != 0 && CONFIG_GFX_VCOORDS != 1)
55 #error CONFIG_GFX_VCOORDS must be defined to either 0 or 1
59 * Draw a sloped line without performing clipping.
61 * Parameters are the same of gfx_line().
62 * This routine is based on the Bresenham Line-Drawing Algorithm.
64 * \note Passing coordinates outside the bitmap boundaries will
65 * result in memory trashing.
67 * \todo Optimize for vertical and horiziontal lines.
71 static void gfx_lineUnclipped(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
73 int x, y, e, len, adx, ady, signx, signy;
106 /* X-major line (octants 1/4/5/8) */
113 ASSERT((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
126 /* Y-major line (octants 2/3/6/7) */
133 ASSERT ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
146 #if CONFIG_GFX_CLIPPING
148 /// Helper routine for gfx_line().
149 static int gfx_findRegion(int x, int y, Rect *cr)
154 code |= 1; /* below */
155 else if (y < cr->ymin)
156 code |= 2; /* above */
159 code |= 4; /* right */
160 else if (x < cr->xmin)
161 code |= 8; /* left */
166 #endif /* CONFIG_CLIPPING */
169 * Draw a sloped line segment.
171 * Draw a sloped line segment identified by the provided
172 * start and end coordinates on the bitmap \a bm.
174 * The line endpoints are clipped inside the current bitmap
175 * clipping rectangle using the Cohen-Sutherland algorithm,
176 * which is very fast.
178 * \note The point at coordinates \a x2 \a y2 is not drawn.
180 * \note This function does \b not update the current pen position.
182 * \todo Compute updated Bresenham error term.
184 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
186 #if CONFIG_GFX_CLIPPING
187 int clip1 = gfx_findRegion(x1, y1, &bm->cr);
188 int clip2 = gfx_findRegion(x2, y2, &bm->cr);
190 /* Loop while there is at least one point outside */
191 while (clip1 | clip2)
193 /* Check for line totally outside */
197 int c = clip1 ? clip1 : clip2;
200 if (c & 1) /* Below */
202 x = x1 + (x2 - x1) * (bm->cr.ymax - y1) / (y2 - y1);
205 else if (c & 2) /* Above */
207 x = x1 + (x2 - x1) * (bm->cr.ymin - y1) / (y2 - y1);
210 else if (c & 4) /* Right */
212 y = y1 + (y2 - y1) * (bm->cr.xmax - x1) / (x2 - x1);
217 y = y1 + (y2 - y1) * (bm->cr.xmin - x1) / (x2 - x1);
221 if (c == clip1) /* First endpoint was clipped */
223 // TODO: adjust Bresenham error term
224 //coord_t clipdx = ABS(x - x1);
225 //coord_t clipdy = ABS(y - y1);
226 //e += (clipdy * e2) + ((clipdx - clipdy) * e1);
230 clip1 = gfx_findRegion(x1, y1, &bm->cr);
232 else /* Second endpoint was clipped */
236 clip2 = gfx_findRegion(x2, y2, &bm->cr);
239 #endif /* CONFIG_GFX_CLIPPING */
241 gfx_lineUnclipped(bm, x1, y1, x2, y2);
245 * Move the current pen position to the specified coordinates.
247 * The pen position is used for drawing operations such as
248 * gfx_lineTo(), which can be used to draw polygons.
250 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
257 * Draw a line from the current pen position to the new coordinates.
259 * \note This function moves the current pen position to the
264 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
266 gfx_line(bm, bm->penX, bm->penY, x, y);
267 gfx_moveTo(bm, x, y);
272 * Draw the perimeter of an hollow rectangle.
274 * \note The bottom-right corner of the rectangle is drawn at (x2-1;y2-1).
275 * \note This function does \b not update the current pen position.
277 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
279 /* Sort coords (needed for correct bottom-right semantics) */
280 if (x1 > x2) SWAP(x1, x2);
281 if (y1 > y2) SWAP(y1, y2);
284 gfx_line(bm, x1, y1, x2-1, y1);
285 gfx_line(bm, x2-1, y1, x2-1, y2-1);
286 gfx_line(bm, x2-1, y2-1, x1, y2-1);
287 gfx_line(bm, x1, y2-1, x1, y1);
292 * Fill a rectangular area with \a color.
294 * \note The bottom-right border of the rectangle is not drawn.
296 * \note This function does \b not update the current pen position.
298 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
303 if (x1 > x2) SWAP(x1, x2);
304 if (y1 > y2) SWAP(y1, y2);
306 #if CONFIG_GFX_CLIPPING
307 /* Clip rect to bitmap clip region */
308 if (x1 < bm->cr.xmin) x1 = bm->cr.xmin;
309 if (x2 < bm->cr.xmin) x2 = bm->cr.xmin;
310 if (x1 > bm->cr.xmax) x1 = bm->cr.xmax;
311 if (x2 > bm->cr.xmax) x2 = bm->cr.xmax;
312 if (y1 < bm->cr.ymin) y1 = bm->cr.ymin;
313 if (y2 < bm->cr.ymin) y2 = bm->cr.ymin;
314 if (y1 > bm->cr.ymax) y1 = bm->cr.ymax;
315 if (y2 > bm->cr.ymax) y2 = bm->cr.ymax;
316 #endif /* CONFIG_GFX_CLIPPING */
318 /* NOTE: Code paths are duplicated for efficiency */
319 if (color) /* fill */
321 for (x = x1; x < x2; x++)
322 for (y = y1; y < y2; y++)
327 for (x = x1; x < x2; x++)
328 for (y = y1; y < y2; y++)
335 * Draw a filled rectangle.
337 * \note The bottom-right border of the rectangle is not drawn.
339 * \note This function does \b not update the current pen position.
341 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
343 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
348 * Clear a rectangular area.
350 * \note The bottom-right border of the rectangle is not cleared.
352 * \note This function does \b not update the current pen position.
354 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
356 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
360 #if CONFIG_GFX_VCOORDS
362 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
363 * al rettangolo di clipping della bitmap.
365 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
372 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
373 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
375 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
376 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
382 * Transform a coordinate from the current reference system to a
383 * pixel offset within the bitmap.
385 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
387 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
391 * Transform a coordinate from the current reference system to a
392 * pixel offset within the bitmap.
394 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
396 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
401 * Draw a line from (x1;y1) to (x2;y2).
403 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
406 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
407 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
409 #endif /* CONFIG_GFX_VCOORDS */