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
45 #include "cfg/cfg_gfx.h" /* CONFIG_GFX_CLIPPING */
46 #include <cfg/debug.h> /* ASSERT() */
47 #include <cfg/macros.h> /* SWAP() */
49 /* Configuration sanity checks */
50 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
51 #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
53 #if !defined(CONFIG_GFX_VCOORDS) || (CONFIG_GFX_VCOORDS != 0 && CONFIG_GFX_VCOORDS != 1)
54 #error CONFIG_GFX_VCOORDS must be defined to either 0 or 1
58 * Draw a sloped line without performing clipping.
60 * Parameters are the same of gfx_line().
61 * This routine is based on the Bresenham Line-Drawing Algorithm.
63 * \note Passing coordinates outside the bitmap boundaries will
64 * result in memory trashing.
66 * \todo Optimize for vertical and horiziontal lines.
70 static void gfx_lineUnclipped(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
72 int x, y, e, len, adx, ady, signx, signy;
105 /* X-major line (octants 1/4/5/8) */
112 ASSERT((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
125 /* Y-major line (octants 2/3/6/7) */
132 ASSERT ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
145 #if CONFIG_GFX_CLIPPING
147 /// Helper routine for gfx_line().
148 static int gfx_findRegion(int x, int y, Rect *cr)
153 code |= 1; /* below */
154 else if (y < cr->ymin)
155 code |= 2; /* above */
158 code |= 4; /* right */
159 else if (x < cr->xmin)
160 code |= 8; /* left */
165 #endif /* CONFIG_CLIPPING */
168 * Draw a sloped line segment.
170 * Draw a sloped line segment identified by the provided
171 * start and end coordinates on the bitmap \a bm.
173 * The line endpoints are clipped inside the current bitmap
174 * clipping rectangle using the Cohen-Sutherland algorithm,
175 * which is very fast.
177 * \note The point at coordinates \a x2 \a y2 is not drawn.
179 * \note This function does \b not update the current pen position.
181 * \todo Compute updated Bresenham error term.
183 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
185 #if CONFIG_GFX_CLIPPING
186 int clip1 = gfx_findRegion(x1, y1, &bm->cr);
187 int clip2 = gfx_findRegion(x2, y2, &bm->cr);
189 /* Loop while there is at least one point outside */
190 while (clip1 | clip2)
192 /* Check for line totally outside */
196 int c = clip1 ? clip1 : clip2;
199 if (c & 1) /* Below */
201 x = x1 + (x2 - x1) * (bm->cr.ymax - y1) / (y2 - y1);
204 else if (c & 2) /* Above */
206 x = x1 + (x2 - x1) * (bm->cr.ymin - y1) / (y2 - y1);
209 else if (c & 4) /* Right */
211 y = y1 + (y2 - y1) * (bm->cr.xmax - x1) / (x2 - x1);
216 y = y1 + (y2 - y1) * (bm->cr.xmin - x1) / (x2 - x1);
220 if (c == clip1) /* First endpoint was clipped */
222 // TODO: adjust Bresenham error term
223 //coord_t clipdx = ABS(x - x1);
224 //coord_t clipdy = ABS(y - y1);
225 //e += (clipdy * e2) + ((clipdx - clipdy) * e1);
229 clip1 = gfx_findRegion(x1, y1, &bm->cr);
231 else /* Second endpoint was clipped */
235 clip2 = gfx_findRegion(x2, y2, &bm->cr);
238 #endif /* CONFIG_GFX_CLIPPING */
240 gfx_lineUnclipped(bm, x1, y1, x2, y2);
244 * Move the current pen position to the specified coordinates.
246 * The pen position is used for drawing operations such as
247 * gfx_lineTo(), which can be used to draw polygons.
249 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
256 * Draw a line from the current pen position to the new coordinates.
258 * \note This function moves the current pen position to the
263 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
265 gfx_line(bm, bm->penX, bm->penY, x, y);
266 gfx_moveTo(bm, x, y);
271 * Draw the perimeter of an hollow rectangle.
273 * \note The bottom-right corner of the rectangle is drawn at (x2-1;y2-1).
274 * \note This function does \b not update the current pen position.
276 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
278 /* Sort coords (needed for correct bottom-right semantics) */
279 if (x1 > x2) SWAP(x1, x2);
280 if (y1 > y2) SWAP(y1, y2);
283 gfx_line(bm, x1, y1, x2-1, y1);
284 gfx_line(bm, x2-1, y1, x2-1, y2-1);
285 gfx_line(bm, x2-1, y2-1, x1, y2-1);
286 gfx_line(bm, x1, y2-1, x1, y1);
291 * Fill a rectangular area with \a color.
293 * \note The bottom-right border of the rectangle is not drawn.
295 * \note This function does \b not update the current pen position.
297 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
302 if (x1 > x2) SWAP(x1, x2);
303 if (y1 > y2) SWAP(y1, y2);
305 #if CONFIG_GFX_CLIPPING
306 /* Clip rect to bitmap clip region */
307 if (x1 < bm->cr.xmin) x1 = bm->cr.xmin;
308 if (x2 < bm->cr.xmin) x2 = bm->cr.xmin;
309 if (x1 > bm->cr.xmax) x1 = bm->cr.xmax;
310 if (x2 > bm->cr.xmax) x2 = bm->cr.xmax;
311 if (y1 < bm->cr.ymin) y1 = bm->cr.ymin;
312 if (y2 < bm->cr.ymin) y2 = bm->cr.ymin;
313 if (y1 > bm->cr.ymax) y1 = bm->cr.ymax;
314 if (y2 > bm->cr.ymax) y2 = bm->cr.ymax;
315 #endif /* CONFIG_GFX_CLIPPING */
317 /* NOTE: Code paths are duplicated for efficiency */
318 if (color) /* fill */
320 for (x = x1; x < x2; x++)
321 for (y = y1; y < y2; y++)
326 for (x = x1; x < x2; x++)
327 for (y = y1; y < y2; y++)
334 * Draw a filled rectangle.
336 * \note The bottom-right border of the rectangle is not drawn.
338 * \note This function does \b not update the current pen position.
340 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
342 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
347 * Clear a rectangular area.
349 * \note The bottom-right border of the rectangle is not cleared.
351 * \note This function does \b not update the current pen position.
353 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
355 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
359 #if CONFIG_GFX_VCOORDS
361 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
362 * al rettangolo di clipping della bitmap.
364 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
371 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
372 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
374 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
375 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
381 * Transform a coordinate from the current reference system to a
382 * pixel offset within the bitmap.
384 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
386 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
390 * Transform a coordinate from the current reference system to a
391 * pixel offset within the bitmap.
393 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
395 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
400 * Draw a line from (x1;y1) to (x2;y2).
402 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
405 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
406 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
408 #endif /* CONFIG_GFX_VCOORDS */