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.4 2006/01/17 02:31:29 bernie
20 *#* Add bitmap format support; Improve some comments.
22 *#* Revision 1.3 2005/11/27 23:33:40 bernie
23 *#* Use appconfig.h instead of cfg/config.h.
25 *#* Revision 1.2 2005/11/04 18:17:45 bernie
26 *#* Fix header guards and includes for new location of gfx module.
28 *#* Revision 1.1 2005/11/04 18:11:35 bernie
29 *#* Move graphics stuff from mware/ to gfx/.
31 *#* Revision 1.14 2005/11/04 16:20:02 bernie
32 *#* Fix reference to README.devlib in header.
34 *#* Revision 1.13 2005/04/11 19:10:28 bernie
35 *#* Include top-level headers from cfg/ subdir.
37 *#* Revision 1.12 2005/03/01 23:26:45 bernie
38 *#* Use new CPU-neutral program-memory API.
42 #include <appconfig.h> /* CONFIG_GFX_CLIPPING */
43 #include <cfg/debug.h>
44 #include <cfg/cpu.h> /* CPU_HARVARD */
45 #include <cfg/macros.h> /* SWAP() */
50 * \name Known pixel formats for bitmap representation.
53 #define BITMAP_FMT_PLANAR_H_MSB 1 /**< Planar pixels, horizontal bytes, MSB left. */
54 #define BITMAP_FMT_PLANAR_V_LSB 2 /**< Planar pixels, vertical bytes, LSB top. */
57 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
59 #define BM_ADDR(bm, x, y) ((bm)->raster + (y) * (bm)->stride + ((x) / 8))
60 #define BM_MASK(bm, x, y) (1 << (7 - (x) % 8))
62 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
64 #define BM_ADDR(bm, x, y) ((bm)->raster + ((y) / 8) * (bm)->stride + (x))
65 #define BM_MASK(bm, x, y) (1 << ((y) % 8))
68 #error Unknown value of CONFIG_BITMAP_FMT
69 #endif /* CONFIG_BITMAP_FMT */
72 * Plot a point in bitmap \a bm.
74 * \note bm is evaluated twice.
75 * \see BM_CLEAR BM_DRAWPIXEL
77 #define BM_PLOT(bm, x, y) \
78 ( *BM_ADDR(bm, x, y) |= BM_MASK(bm, x, y) )
81 * Clear a point in bitmap \a bm.
83 * \note bm is evaluated twice.
84 * \see BM_PLOT BM_DRAWPIXEL
86 #define BM_CLEAR(bm, x, y) \
87 ( *BM_ADDR(bm, x, y) &= ~BM_MASK(bm, x, y) )
90 * Set a point in bitmap \a bm to the specified color.
92 * \note bm is evaluated twice.
93 * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
94 * \see BM_PLOT BM_CLEAR
96 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
98 uint8_t *p = BM_ADDR(bm, x, y); \
99 uint8_t mask = BM_MASK(bm, x, y); \
100 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
105 * Initialize a Bitmap structure with the provided parameters.
107 * \note The pen position is reset to the origin.
109 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
114 #if (CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB)
115 bm->stride = (w + 7) / 8;
116 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
119 #error Unknown value of CONFIG_BITMAP_FMT
120 #endif /* CONFIG_BITMAP_FMT */
132 * Clear the whole bitmap surface to the background color.
134 * \note This function does \b not update the current pen position
136 void gfx_bitmapClear(Bitmap *bm)
138 memset(bm->raster, 0, (bm->width * bm->height) / 8);
144 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
147 * Copy a raster picture located in program memory in the bitmap.
148 * The size of the raster to copy *must* be the same of the raster bitmap.
150 * \note This function does \b not update the current pen position
152 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
154 memcpy_P(bm->raster, raster, (bm->height / 8) * bm->width);
156 #endif /* CPU_HARVARD */
160 * Draw a line on the bitmap \a bm using the specified start and end
163 * \note This function does \b not update the current pen position.
165 * \todo Optimize for vertical and horiziontal lines.
167 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
169 int x, y, e, len, adx, ady, signx, signy;
172 #if CONFIG_GFX_CLIPPING
175 #define XMIN (bm->cr.xmin)
176 #define YMIN (bm->cr.ymin)
177 #define XMAX (bm->cr.xmax - 1)
178 #define YMAX (bm->cr.ymax - 1)
184 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
190 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
196 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
202 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
209 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
215 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
221 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
227 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
236 #endif /* CONFIG_GFX_CLIPPING */
270 /* X-major line (octants 1/4/5/8) */
277 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
290 /* Y-major line (octants 2/3/6/7) */
297 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
312 * Move the current pen position to the specified coordinates.
314 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
321 * Draw a line from the current pen position to the new coordinates.
323 * \note This function moves the current pen position to the
326 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
328 gfx_line(bm, bm->penX, bm->penY, x, y);
329 gfx_moveTo(bm, x, y);
334 * Draw an the outline of an hollow rectangle.
336 * \note The bottom-right corner of the rectangle is drawn at (x2-1;y2-1).
337 * \note This function does \b not update the current pen position
339 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
341 /* Sort coords (needed for correct bottom-right semantics) */
342 if (x1 > x2) SWAP(x1, x2);
343 if (y1 > y2) SWAP(y1, y2);
346 gfx_line(bm, x1, y1, x2-1, y1);
347 gfx_line(bm, x2-1, y1, x2-1, y2-1);
348 gfx_line(bm, x2-1, y2-1, x1, y2-1);
349 gfx_line(bm, x1, y2-1, x1, y1);
354 * Fill a rectangular area with \a color.
356 * \note The bottom-right border of the rectangle is not drawn.
358 * \note This function does \b not update the current pen position.
360 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
365 if (x1 > x2) SWAP(x1, x2);
366 if (y1 > y2) SWAP(y1, y2);
368 /* Clip rect to bitmap bounds */
371 if (x1 > bm->width) x1 = bm->width;
372 if (x2 > bm->width) x2 = bm->width;
375 if (y1 > bm->width) y1 = bm->width;
376 if (y2 > bm->width) y2 = bm->width;
380 * NOTE: Code paths are duplicated for efficiency
382 if (color) /* fill */
384 for (x = x1; x < x2; x++)
385 for (y = y1; y < y2; y++)
390 for (x = x1; x < x2; x++)
391 for (y = y1; y < y2; y++)
398 * Draw a filled rectangle.
400 * \note The bottom-right border of the rectangle is not drawn.
402 * \note This function does \b not update the current pen position.
404 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
406 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
411 * Clear a rectangular area.
413 * \note The bottom-right border of the rectangle is not cleared.
415 * \note This function does \b not update the current pen position.
417 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
419 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
424 * Imposta un rettangolo di clipping per il disegno nella bitmap.
426 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
432 ASSERT(maxx <= bm->width);
433 ASSERT(maxy <= bm->height);
440 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
441 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
446 #if CONFIG_GFX_VCOORDS
448 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
449 * al rettangolo di clipping della bitmap.
451 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
458 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
459 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
461 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
462 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
468 * Transform a coordinate from the current reference system to a
469 * pixel offset within the bitmap.
471 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
473 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
477 * Transform a coordinate from the current reference system to a
478 * pixel offset within the bitmap.
480 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
482 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
487 * Draw a line from (x1;y1) to (x2;y2).
489 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
492 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
493 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
495 #endif /* CONFIG_GFX_VCOORDS */