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 General pourpose graphics routines
19 *#* Revision 1.3 2005/11/27 23:33:40 bernie
20 *#* Use appconfig.h instead of cfg/config.h.
22 *#* Revision 1.2 2005/11/04 18:17:45 bernie
23 *#* Fix header guards and includes for new location of gfx module.
25 *#* Revision 1.1 2005/11/04 18:11:35 bernie
26 *#* Move graphics stuff from mware/ to gfx/.
28 *#* Revision 1.14 2005/11/04 16:20:02 bernie
29 *#* Fix reference to README.devlib in header.
31 *#* Revision 1.13 2005/04/11 19:10:28 bernie
32 *#* Include top-level headers from cfg/ subdir.
34 *#* Revision 1.12 2005/03/01 23:26:45 bernie
35 *#* Use new CPU-neutral program-memory API.
39 #include <appconfig.h> /* CONFIG_GFX_CLIPPING */
40 #include <cfg/debug.h>
41 #include <cfg/cpu.h> /* CPU_HARVARD */
42 #include <cfg/macros.h> /* SWAP() */
48 * Plot a point in bitmap \a bm.
50 * \note bm is evaluated twice
52 #define BM_PLOT(bm, x, y) \
53 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
56 * Clear a point in bitmap \a bm.
58 * \note bm is evaluated twice
60 #define BM_CLEAR(bm, x, y) \
61 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
64 * Set a point in bitmap \a bm to the specified color.
66 * \note bm is evaluated twice
67 * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
68 * \see BM_PLOT BM_CLEAR
70 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
72 uint8_t *p = (bm)->raster + ((y) / 8) * (bm)->width + (x); \
73 uint8_t mask = 1 << ((y) % 8); \
74 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
78 * Initialize a Bitmap structure with the provided parameters.
80 * \note The pen position is reset to the origin.
82 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
93 * Clear the whole bitmap surface to the background color.
95 * \note This function does \b not update the current pen position
97 void gfx_bitmapClear(Bitmap *bm)
99 memset(bm->raster, 0, (bm->width * bm->height) / 8);
105 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
108 * Copy a raster picture located in program memory in the bitmap.
109 * The size of the raster to copy *must* be the same of the raster bitmap.
111 * \note This function does \b not update the current pen position
113 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
115 memcpy_P(bm->raster, raster, (bm->height / 8) * bm->width);
117 #endif /* CPU_HARVARD */
121 * Draw a line on the bitmap \a bm using the specified start and end
124 * \note This function does \b not update the current pen position.
126 * \todo Optimize for vertical and horiziontal lines.
128 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
130 int x, y, e, len, adx, ady, signx, signy;
133 #if CONFIG_GFX_CLIPPING
138 #define XMAX (bm->width - 1)
139 #define YMAX (bm->height - 1)
144 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
149 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
154 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
159 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
165 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
170 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
175 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
180 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
189 #endif /* CONFIG_GFX_CLIPPING */
223 /* X-major line (octants 1/4/5/8) */
230 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
243 /* Y-major line (octants 2/3/6/7) */
250 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
265 * Move the current pen position to the specified coordinates.
267 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
274 * Draw a line from the current pen position to the new coordinates.
276 * \note This function moves the current pen position to the
279 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
281 gfx_line(bm, bm->penX, bm->penY, x, y);
282 gfx_moveTo(bm, x, y);
287 * Draw an the outline of an hollow rectangle.
289 * \note The bottom-right border of the rectangle is not drawn.
290 * \note This function does \b not update the current pen position
292 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
294 /* Sort coords (needed for correct bottom-right semantics) */
295 if (x1 > x2) SWAP(x1, x2);
296 if (y1 > y2) SWAP(y1, y2);
299 gfx_line(bm, x1, y1, x2-1, y1);
300 gfx_line(bm, x2-1, y1, x2-1, y2-1);
301 gfx_line(bm, x2-1, y2-1, x1, y2-1);
302 gfx_line(bm, x1, y2-1, x1, y1);
307 * Fill a rectangular area with \a color.
309 * \note The bottom-right border of the rectangle is not drawn.
311 * \note This function does \b not update the current pen position
313 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
318 if (x1 > x2) SWAP(x1, x2);
319 if (y1 > y2) SWAP(y1, y2);
321 /* Clip rect to bitmap bounds */
324 if (x1 > bm->width) x1 = bm->width;
325 if (x2 > bm->width) x2 = bm->width;
328 if (y1 > bm->width) y1 = bm->width;
329 if (y2 > bm->width) y2 = bm->width;
333 * 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 drawn.
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);
377 * Imposta un rettangolo di clipping per il disegno nella bitmap.
379 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
385 ASSERT(maxx <= bm->width);
386 ASSERT(maxy <= bm->height);
393 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
394 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
399 #if CONFIG_GFX_VCOORDS
401 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
402 * al rettangolo di clipping della bitmap.
404 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
411 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
412 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
414 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
415 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
421 * Transform a coordinate from the current reference system to a
422 * pixel offset within the bitmap.
424 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
426 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
430 * Transform a coordinate from the current reference system to a
431 * pixel offset within the bitmap.
433 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
435 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
440 * Draw a line from (x1;y1) to (x2;y2).
442 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
445 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
446 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
448 #endif /* CONFIG_GFX_VCOORDS */