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.2 2005/11/04 18:17:45 bernie
20 *#* Fix header guards and includes for new location of gfx module.
22 *#* Revision 1.1 2005/11/04 18:11:35 bernie
23 *#* Move graphics stuff from mware/ to gfx/.
25 *#* Revision 1.14 2005/11/04 16:20:02 bernie
26 *#* Fix reference to README.devlib in header.
28 *#* Revision 1.13 2005/04/11 19:10:28 bernie
29 *#* Include top-level headers from cfg/ subdir.
31 *#* Revision 1.12 2005/03/01 23:26:45 bernie
32 *#* Use new CPU-neutral program-memory API.
36 #include <cfg/config.h> /* CONFIG_GFX_CLIPPING */
37 #include <cfg/debug.h>
38 #include <cfg/cpu.h> /* CPU_HARVARD */
39 #include <cfg/macros.h> /* SWAP() */
45 * Plot a point in bitmap \a bm.
47 * \note bm is evaluated twice
49 #define BM_PLOT(bm, x, y) \
50 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
53 * Clear a point in bitmap \a bm.
55 * \note bm is evaluated twice
57 #define BM_CLEAR(bm, x, y) \
58 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
61 * Set a point in bitmap \a bm to the specified color.
63 * \note bm is evaluated twice
64 * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
65 * \see BM_PLOT BM_CLEAR
67 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
69 uint8_t *p = (bm)->raster + ((y) / 8) * (bm)->width + (x); \
70 uint8_t mask = 1 << ((y) % 8); \
71 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
75 * Initialize a Bitmap structure with the provided parameters.
77 * \note The pen position is reset to the origin.
79 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
90 * Clear the whole bitmap surface to the background color.
92 * \note This function does \b not update the current pen position
94 void gfx_bitmapClear(Bitmap *bm)
96 memset(bm->raster, 0, (bm->width * bm->height) / 8);
102 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
105 * Copy a raster picture located in program memory in the bitmap.
106 * The size of the raster to copy *must* be the same of the raster bitmap.
108 * \note This function does \b not update the current pen position
110 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
112 memcpy_P(bm->raster, raster, (bm->height / 8) * bm->width);
114 #endif /* CPU_HARVARD */
118 * Draw a line on the bitmap \a bm using the specified start and end
121 * \note This function does \b not update the current pen position.
123 * \todo Optimize for vertical and horiziontal lines.
125 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
127 int x, y, e, len, adx, ady, signx, signy;
130 #if CONFIG_GFX_CLIPPING
135 #define XMAX (bm->width - 1)
136 #define YMAX (bm->height - 1)
141 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
146 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
151 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
156 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
162 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
167 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
172 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
177 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
186 #endif /* CONFIG_GFX_CLIPPING */
220 /* X-major line (octants 1/4/5/8) */
227 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
240 /* Y-major line (octants 2/3/6/7) */
247 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
262 * Move the current pen position to the specified coordinates.
264 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
271 * Draw a line from the current pen position to the new coordinates.
273 * \note This function moves the current pen position to the
276 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
278 gfx_line(bm, bm->penX, bm->penY, x, y);
279 gfx_moveTo(bm, x, y);
284 * Draw an the outline of an hollow rectangle.
286 * \note The bottom-right border of the rectangle is not drawn.
287 * \note This function does \b not update the current pen position
289 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
291 /* Sort coords (needed for correct bottom-right semantics) */
292 if (x1 > x2) SWAP(x1, x2);
293 if (y1 > y2) SWAP(y1, y2);
296 gfx_line(bm, x1, y1, x2-1, y1);
297 gfx_line(bm, x2-1, y1, x2-1, y2-1);
298 gfx_line(bm, x2-1, y2-1, x1, y2-1);
299 gfx_line(bm, x1, y2-1, x1, y1);
304 * Fill a rectangular area with \a color.
306 * \note The bottom-right border of the rectangle is not drawn.
308 * \note This function does \b not update the current pen position
310 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
315 if (x1 > x2) SWAP(x1, x2);
316 if (y1 > y2) SWAP(y1, y2);
318 /* Clip rect to bitmap bounds */
321 if (x1 > bm->width) x1 = bm->width;
322 if (x2 > bm->width) x2 = bm->width;
325 if (y1 > bm->width) y1 = bm->width;
326 if (y2 > bm->width) y2 = bm->width;
330 * NOTE: Code paths are duplicated for efficiency
332 if (color) /* fill */
334 for (x = x1; x < x2; x++)
335 for (y = y1; y < y2; y++)
340 for (x = x1; x < x2; x++)
341 for (y = y1; y < y2; y++)
348 * Draw a filled rectangle.
350 * \note The bottom-right border of the rectangle is not drawn.
352 * \note This function does \b not update the current pen position
354 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
356 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
361 * Clear a rectangular area.
363 * \note The bottom-right border of the rectangle is not drawn.
365 * \note This function does \b not update the current pen position
367 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
369 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
374 * Imposta un rettangolo di clipping per il disegno nella bitmap.
376 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
382 ASSERT(maxx <= bm->width);
383 ASSERT(maxy <= bm->height);
390 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
391 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
396 #if CONFIG_GFX_VCOORDS
398 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
399 * al rettangolo di clipping della bitmap.
401 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
408 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
409 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
411 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
412 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
418 * Transform a coordinate from the current reference system to a
419 * pixel offset within the bitmap.
421 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
423 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
427 * Transform a coordinate from the current reference system to a
428 * pixel offset within the bitmap.
430 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
432 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
437 * Draw a line from (x1;y1) to (x2;y2).
439 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
442 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
443 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
445 #endif /* CONFIG_GFX_VCOORDS */