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 Bernardo Innocenti <bernie@develer.com>
36 * \author Bernardo Innocenti <bernie@develer.com>
37 * \author Stefano Fedrigo <aleph@develer.com>
39 * \brief Line drawing graphics routines
44 *#* Revision 1.4 2006/08/01 12:22:00 bernie
45 *#* gfx_findRegion(): Only define when CONFIG_GFX_CLIPPING is enabled.
47 *#* Revision 1.3 2006/07/19 12:56:26 bernie
48 *#* Convert to new Doxygen style.
50 *#* Revision 1.2 2006/02/10 12:26:58 bernie
51 *#* Check CONFIG_* constraints.
53 *#* Revision 1.1 2006/01/24 02:17:49 bernie
54 *#* Split out gfx.c into bitmap.c and line.c.
61 #include <cfg/debug.h> /* ASSERT() */
62 #include <cfg/cpu.h> /* CPU_HARVARD */
63 #include <cfg/macros.h> /* SWAP() */
64 #include <appconfig.h> /* CONFIG_GFX_CLIPPING */
66 /* Configuration sanity checks */
67 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
68 #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
70 #if !defined(CONFIG_GFX_VCOORDS) || (CONFIG_GFX_VCOORDS != 0 && CONFIG_GFX_VCOORDS != 1)
71 #error CONFIG_GFX_VCOORDS must be defined to either 0 or 1
75 * Draw a sloped line without performing clipping.
77 * Parameters are the same of gfx_line().
78 * This routine is based on the Bresenham Line-Drawing Algorithm.
80 * \note Passing coordinates outside the bitmap boundaries will
81 * result in memory trashing.
83 * \todo Optimize for vertical and horiziontal lines.
87 static void gfx_lineUnclipped(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
89 int x, y, e, len, adx, ady, signx, signy;
122 /* X-major line (octants 1/4/5/8) */
129 ASSERT((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
142 /* Y-major line (octants 2/3/6/7) */
149 ASSERT ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
162 #if CONFIG_GFX_CLIPPING
164 /// Helper routine for gfx_line().
165 static int gfx_findRegion(int x, int y, Rect *cr)
170 code |= 1; /* below */
171 else if (y < cr->ymin)
172 code |= 2; /* above */
175 code |= 4; /* right */
176 else if (x < cr->xmin)
177 code |= 8; /* left */
182 #endif /* CONFIG_CLIPPING */
185 * Draw a sloped line segment.
187 * Draw a sloped line segment identified by the provided
188 * start and end coordinates on the bitmap \a bm.
190 * The line endpoints are clipped inside the current bitmap
191 * clipping rectangle using the Cohen-Sutherland algorithm,
192 * which is very fast.
194 * \note The point at coordinates \a x2 \a y2 is not drawn.
196 * \note This function does \b not update the current pen position.
198 * \todo Compute updated Bresenham error term.
200 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
202 #if CONFIG_GFX_CLIPPING
203 int clip1 = gfx_findRegion(x1, y1, &bm->cr);
204 int clip2 = gfx_findRegion(x2, y2, &bm->cr);
206 /* Loop while there is at least one point outside */
207 while (clip1 | clip2)
209 /* Check for line totally outside */
213 int c = clip1 ? clip1 : clip2;
216 if (c & 1) /* Below */
218 x = x1 + (x2 - x1) * (bm->cr.ymax - y1) / (y2 - y1);
221 else if (c & 2) /* Above */
223 x = x1 + (x2 - x1) * (bm->cr.ymin - y1) / (y2 - y1);
226 else if (c & 4) /* Right */
228 y = y1 + (y2 - y1) * (bm->cr.xmax - x1) / (x2 - x1);
233 y = y1 + (y2 - y1) * (bm->cr.xmin - x1) / (x2 - x1);
237 if (c == clip1) /* First endpoint was clipped */
239 // TODO: adjust Bresenham error term
240 //coord_t clipdx = ABS(x - x1);
241 //coord_t clipdy = ABS(y - y1);
242 //e += (clipdy * e2) + ((clipdx - clipdy) * e1);
246 clip1 = gfx_findRegion(x1, y1, &bm->cr);
248 else /* Second endpoint was clipped */
252 clip2 = gfx_findRegion(x2, y2, &bm->cr);
255 #endif /* CONFIG_GFX_CLIPPING */
257 gfx_lineUnclipped(bm, x1, y1, x2, y2);
261 * Move the current pen position to the specified coordinates.
263 * The pen position is used for drawing operations such as
264 * gfx_lineTo(), which can be used to draw polygons.
266 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
273 * Draw a line from the current pen position to the new coordinates.
275 * \note This function moves the current pen position to the
280 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
282 gfx_line(bm, bm->penX, bm->penY, x, y);
283 gfx_moveTo(bm, x, y);
288 * Draw the perimeter of an hollow rectangle.
290 * \note The bottom-right corner of the rectangle is drawn at (x2-1;y2-1).
291 * \note This function does \b not update the current pen position.
293 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
295 /* Sort coords (needed for correct bottom-right semantics) */
296 if (x1 > x2) SWAP(x1, x2);
297 if (y1 > y2) SWAP(y1, y2);
300 gfx_line(bm, x1, y1, x2-1, y1);
301 gfx_line(bm, x2-1, y1, x2-1, y2-1);
302 gfx_line(bm, x2-1, y2-1, x1, y2-1);
303 gfx_line(bm, x1, y2-1, x1, y1);
308 * Fill a rectangular area with \a color.
310 * \note The bottom-right border of the rectangle is not drawn.
312 * \note This function does \b not update the current pen position.
314 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
319 if (x1 > x2) SWAP(x1, x2);
320 if (y1 > y2) SWAP(y1, y2);
322 #if CONFIG_GFX_CLIPPING
323 /* Clip rect to bitmap clip region */
324 if (x1 < bm->cr.xmin) x1 = bm->cr.xmin;
325 if (x2 < bm->cr.xmin) x2 = bm->cr.xmin;
326 if (x1 > bm->cr.xmax) x1 = bm->cr.xmax;
327 if (x2 > bm->cr.xmax) x2 = bm->cr.xmax;
328 if (y1 < bm->cr.ymin) y1 = bm->cr.ymin;
329 if (y2 < bm->cr.ymin) y2 = bm->cr.ymin;
330 if (y1 > bm->cr.ymax) y1 = bm->cr.ymax;
331 if (y2 > bm->cr.ymax) y2 = bm->cr.ymax;
332 #endif /* CONFIG_GFX_CLIPPING */
334 /* NOTE: Code paths are duplicated for efficiency */
335 if (color) /* fill */
337 for (x = x1; x < x2; x++)
338 for (y = y1; y < y2; y++)
343 for (x = x1; x < x2; x++)
344 for (y = y1; y < y2; y++)
351 * Draw a filled rectangle.
353 * \note The bottom-right border of the rectangle is not drawn.
355 * \note This function does \b not update the current pen position.
357 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
359 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
364 * Clear a rectangular area.
366 * \note The bottom-right border of the rectangle is not cleared.
368 * \note This function does \b not update the current pen position.
370 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
372 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
376 #if CONFIG_GFX_VCOORDS
378 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
379 * al rettangolo di clipping della bitmap.
381 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
388 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
389 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
391 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
392 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
398 * Transform a coordinate from the current reference system to a
399 * pixel offset within the bitmap.
401 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
403 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
407 * Transform a coordinate from the current reference system to a
408 * pixel offset within the bitmap.
410 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
412 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
417 * Draw a line from (x1;y1) to (x2;y2).
419 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
422 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
423 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
425 #endif /* CONFIG_GFX_VCOORDS */