4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See devlib/README 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.13 2005/04/11 19:10:28 bernie
20 *#* Include top-level headers from cfg/ subdir.
22 *#* Revision 1.12 2005/03/01 23:26:45 bernie
23 *#* Use new CPU-neutral program-memory API.
25 *#* Revision 1.11 2004/12/08 08:06:16 bernie
28 *#* Revision 1.10 2004/11/01 15:14:07 bernie
29 *#* Update to current coding conventions.
31 *#* Revision 1.9 2004/10/21 10:58:33 bernie
34 *#* Revision 1.8 2004/09/20 03:29:22 bernie
37 *#* Revision 1.7 2004/09/14 21:01:08 bernie
38 *#* Rename rectangle drawing functions; Unify filled/cleared implementations.
40 *#* Revision 1.4 2004/08/24 16:53:10 bernie
41 *#* Use new-style config macros.
43 *#* Revision 1.3 2004/08/04 03:16:59 bernie
44 *#* Switch to new DevLib CONFIG_ convention.
46 *#* Revision 1.2 2004/06/03 11:27:09 bernie
47 *#* Add dual-license information.
51 #include <cfg/config.h> /* CONFIG_GFX_CLIPPING */
52 #include <cfg/debug.h>
53 #include <cfg/cpu.h> /* CPU_HARVARD */
54 #include <cfg/macros.h> /* SWAP() */
60 * Plot a point in bitmap \a bm.
62 * \note bm is evaluated twice
64 #define BM_PLOT(bm, x, y) \
65 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
68 * Clear a point in bitmap \a bm.
70 * \note bm is evaluated twice
72 #define BM_CLEAR(bm, x, y) \
73 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
76 * Set a point in bitmap \a bm to the specified color.
78 * \note bm is evaluated twice
79 * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
80 * \see BM_PLOT BM_CLEAR
82 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
84 uint8_t *p = (bm)->raster + ((y) / 8) * (bm)->width + (x); \
85 uint8_t mask = 1 << ((y) % 8); \
86 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
90 * Initialize a Bitmap structure with the provided parameters.
92 * \note The pen position is reset to the origin.
94 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
105 * Clear the whole bitmap surface to the background color.
107 * \note This function does \b not update the current pen position
109 void gfx_bitmapClear(Bitmap *bm)
111 memset(bm->raster, 0, (bm->width * bm->height) / 8);
117 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
120 * Copy a raster picture located in program memory in the bitmap.
121 * The size of the raster to copy *must* be the same of the raster bitmap.
123 * \note This function does \b not update the current pen position
125 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
127 memcpy_P(bm->raster, raster, (bm->height / 8) * bm->width);
129 #endif /* CPU_HARVARD */
133 * Draw a line on the bitmap \a bm using the specified start and end
136 * \note This function does \b not update the current pen position.
138 * \todo Optimize for vertical and horiziontal lines.
140 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
142 int x, y, e, len, adx, ady, signx, signy;
145 #if CONFIG_GFX_CLIPPING
150 #define XMAX (bm->width - 1)
151 #define YMAX (bm->height - 1)
156 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
161 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
166 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
171 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
177 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
182 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
187 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
192 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
201 #endif /* CONFIG_GFX_CLIPPING */
235 /* X-major line (octants 1/4/5/8) */
242 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
255 /* Y-major line (octants 2/3/6/7) */
262 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
277 * Move the current pen position to the specified coordinates.
279 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
286 * Draw a line from the current pen position to the new coordinates.
288 * \note This function moves the current pen position to the
291 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
293 gfx_line(bm, bm->penX, bm->penY, x, y);
294 gfx_moveTo(bm, x, y);
299 * Draw an the outline of an hollow rectangle.
301 * \note The bottom-right border of the rectangle is not drawn.
302 * \note This function does \b not update the current pen position
304 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
306 /* Sort coords (needed for correct bottom-right semantics) */
307 if (x1 > x2) SWAP(x1, x2);
308 if (y1 > y2) SWAP(y1, y2);
311 gfx_line(bm, x1, y1, x2-1, y1);
312 gfx_line(bm, x2-1, y1, x2-1, y2-1);
313 gfx_line(bm, x2-1, y2-1, x1, y2-1);
314 gfx_line(bm, x1, y2-1, x1, y1);
319 * Fill a rectangular area with \a color.
321 * \note The bottom-right border of the rectangle is not drawn.
323 * \note This function does \b not update the current pen position
325 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
330 if (x1 > x2) SWAP(x1, x2);
331 if (y1 > y2) SWAP(y1, y2);
333 /* Clip rect to bitmap bounds */
336 if (x1 > bm->width) x1 = bm->width;
337 if (x2 > bm->width) x2 = bm->width;
340 if (y1 > bm->width) y1 = bm->width;
341 if (y2 > bm->width) y2 = bm->width;
345 * NOTE: Code paths are duplicated for efficiency
347 if (color) /* fill */
349 for (x = x1; x < x2; x++)
350 for (y = y1; y < y2; y++)
355 for (x = x1; x < x2; x++)
356 for (y = y1; y < y2; y++)
363 * Draw a filled rectangle.
365 * \note The bottom-right border of the rectangle is not drawn.
367 * \note This function does \b not update the current pen position
369 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
371 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
376 * Clear a rectangular area.
378 * \note The bottom-right border of the rectangle is not drawn.
380 * \note This function does \b not update the current pen position
382 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
384 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
389 * Imposta un rettangolo di clipping per il disegno nella bitmap.
391 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
397 ASSERT(maxx <= bm->width);
398 ASSERT(maxy <= bm->height);
405 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
406 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
411 #if CONFIG_GFX_VCOORDS
413 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
414 * al rettangolo di clipping della bitmap.
416 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
423 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
424 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
426 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
427 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
433 * Transform a coordinate from the current reference system to a
434 * pixel offset within the bitmap.
436 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
438 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
442 * Transform a coordinate from the current reference system to a
443 * pixel offset within the bitmap.
445 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
447 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
452 * Draw a line from (x1;y1) to (x2;y2).
454 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
457 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
458 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
460 #endif /* CONFIG_GFX_VCOORDS */