X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=mware%2Fgfx.c;h=05c8c82a89680acc43be19e95920ce5819e0105a;hb=0d0db1a58035a25ebb6ca6eaa035ec13b4c581a0;hp=d03e0a226923e0ebea89ff7a69a8c284b83a006f;hpb=96f0ef786b54356c56cc3d4e4f0838df2505cfcc;p=bertos.git diff --git a/mware/gfx.c b/mware/gfx.c index d03e0a22..05c8c82a 100755 --- a/mware/gfx.c +++ b/mware/gfx.c @@ -1,7 +1,7 @@ /*! * \file * @@ -12,49 +12,75 @@ * \author Stefano Fedrigo * * \brief General pourpose graphics routines - */ - -/* - * $Log$ - * Revision 1.2 2004/06/03 11:27:09 bernie - * Add dual-license information. * + * \todo Rename functions to match the coding-standard. */ +/*#* + *#* $Log$ + *#* Revision 1.9 2004/10/21 10:58:33 bernie + *#* Add a TODO item. + *#* + *#* Revision 1.8 2004/09/20 03:29:22 bernie + *#* Relax assertion. + *#* + *#* Revision 1.7 2004/09/14 21:01:08 bernie + *#* Rename rectangle drawing functions; Unify filled/cleared implementations. + *#* + *#* Revision 1.4 2004/08/24 16:53:10 bernie + *#* Use new-style config macros. + *#* + *#* Revision 1.3 2004/08/04 03:16:59 bernie + *#* Switch to new DevLib CONFIG_ convention. + *#* + *#* Revision 1.2 2004/06/03 11:27:09 bernie + *#* Add dual-license information. + *#* + *#*/ + #include "gfx.h" -#include "config.h" -#include +#include "config.h" /* CONFIG_GFX_CLIPPING */ +#include +#include /* CPU_AVR */ +#include /* SWAP() */ #include /*! - * Plot a point in bitmap. + * Plot a point in bitmap \a bm. + * * \note bm is evaluated twice */ #define BM_PLOT(bm, x, y) \ ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) ) /*! - * Clear a point in bitmap. + * Clear a point in bitmap \a bm. + * * \note bm is evaluated twice */ #define BM_CLEAR(bm, x, y) \ ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) ) -/*! Swap a with b */ -#define SWAP(a, b) \ +/*! + * Set a point in bitmap \a bm to the specified color. + * + * \note bm is evaluated twice + * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR. + * \see BM_PLOT BM_CLEAR + */ +#define BM_DRAWPIXEL(bm, x, y, fg_pen) \ do { \ - (void)(&a == &b); /* type check */ \ - typeof(a) tmp; \ - tmp = (a); \ - (a) = (b); \ - (b) = tmp; \ + uint8_t *p = (bm)->raster + ((y) / 8) * (bm)->width + (x); \ + uint8_t mask = 1 << ((y) % 8); \ + *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \ } while (0) - /*! * Initialize a Bitmap structure with the provided parameters. + * + * \note The pen position is reset to the origin. */ void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h) { @@ -68,6 +94,8 @@ void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h) /*! * Clear the whole bitmap surface to all zeros + * + * \note This function does \b not update the current pen position */ void gfx_ClearBitmap(Bitmap *bm) { @@ -75,22 +103,32 @@ void gfx_ClearBitmap(Bitmap *bm) } +#if CPU_AVR /*! * Copy a raster picture located in program memory in the bitmap. * The size of the raster to copy *must* be the same of the raster bitmap. + * + * \note This function does \b not update the current pen position */ void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster) { memcpy_P(bm->raster, raster, bm->height/8 * bm->width); } +#endif /* CPU_AVR */ +/*! + * Draw a line on the bitmap \a bm using the specified start and end + * coordinates. + * + * \note This function does \b not update the current pen position + */ void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) { int x, y, e, len, adx, ady, signx, signy; -#ifdef CONFIG_GFX_CLIPPING +#if CONFIG_GFX_CLIPPING /* FIXME: broken */ #define XMIN 0 @@ -146,7 +184,7 @@ void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) #undef XMAX #undef YMAX -#endif /* CONFIG_GRAPH_CLIPPING */ +#endif /* CONFIG_GFX_CLIPPING */ if (x2 > x1) @@ -221,13 +259,21 @@ void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) } +/*! + * Move the current pen position to the specified coordinates. + */ void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y) { bm->penX = x; bm->penY = y; } - +/*! + * Draw a line from the current pen position to the new coordinates. + * + * \note This function moves the current pen position to the + * new coordinates. + */ void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y) { gfx_DrawLine(bm, bm->penX, bm->penY, x, y); @@ -236,41 +282,14 @@ void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y) /*! - * Draw a filled rectangle. - * \note The bottom-right border of the rectangle is not drawn. - */ -void gfx_FillRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) -{ - coord_t x, y; - - /* Sort coords */ - if (x1 > x2) SWAP(x1, x2); - if (y1 > y2) SWAP(y1, y2); - - /* Clip rect to bitmap bounds */ - if (x1 < 0) x1 = 0; - if (x2 < 0) x2 = 0; - if (x1 > bm->width) x1 = bm->width; - if (x2 > bm->width) x2 = bm->width; - if (y1 < 0) y1 = 0; - if (y2 < 0) y2 = 0; - if (y1 > bm->width) y1 = bm->width; - if (y2 > bm->width) y2 = bm->width; - - /* Draw rectangle */ - for (x = x1; x < x2; x++) - for (y = y1; y < y2; y++) - BM_PLOT(bm, x, y); -} - - -/*! - * Draw an empty rectangle. + * Draw an the outline of an hollow rectangle. + * * \note The bottom-right border of the rectangle is not drawn. + * \note This function does \b not update the current pen position */ -void gfx_DrawRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) +void gfx_RectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) { - /* Sort coords */ + /* Sort coords (needed for correct bottom-right semantics) */ if (x1 > x2) SWAP(x1, x2); if (y1 > y2) SWAP(y1, y2); @@ -283,10 +302,13 @@ void gfx_DrawRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) /*! - * Clear a rectangular area. + * Fill a rectangular area with \a color. + * * \note The bottom-right border of the rectangle is not drawn. + * + * \note This function does \b not update the current pen position */ -void gfx_ClearRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) +void gfx_RectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color) { coord_t x, y; @@ -304,10 +326,48 @@ void gfx_ClearRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) if (y1 > bm->width) y1 = bm->width; if (y2 > bm->width) y2 = bm->width; - /* Draw rectangle */ - for (x = x1; x < x2; x++) - for (y = y1; y < y2; y++) - BM_CLEAR(bm, x, y); + /* + * Draw rectangle + * NOTE: Code paths are duplicated for efficiency + */ + if (color) /* fill */ + { + for (x = x1; x < x2; x++) + for (y = y1; y < y2; y++) + BM_PLOT(bm, x, y); + } + else /* clear */ + { + for (x = x1; x < x2; x++) + for (y = y1; y < y2; y++) + BM_CLEAR(bm, x, y); + } +} + + +/*! + * Draw a filled rectangle. + * + * \note The bottom-right border of the rectangle is not drawn. + * + * \note This function does \b not update the current pen position + */ +void gfx_RectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) +{ + gfx_RectFillC(bm, x1, y1, x2, y2, 0xFF); +} + + +/*! + * Clear a rectangular area. + * + * \note The bottom-right border of the rectangle is not drawn. + * + * \note This function does \b not update the current pen position + */ +void gfx_RectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2) +{ + gfx_RectFillC(bm, x1, y1, x2, y2, 0x00); } @@ -320,8 +380,8 @@ void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord ASSERT(miny < maxy); ASSERT(miny >= 0); ASSERT(minx >= 0); - ASSERT(maxx < bm->width); - ASSERT(maxy < bm->height); + ASSERT(maxx <= bm->width); + ASSERT(maxy <= bm->height); bm->cr.xmin = minx; bm->cr.ymin = miny; @@ -334,7 +394,7 @@ void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord } -#ifdef CONFIG_LCD_VCOORDS +#if CONFIG_GFX_VCOORDS /*! * Imposta gli estremi del sistema di coordinate cartesiane rispetto * al rettangolo di clipping della bitmap. @@ -346,8 +406,8 @@ void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t bm->orgX = x1; bm->orgY = y1; - bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin) / (vcoord_t)(x2 - x1); /* +1 */ - bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin) / (vcoord_t)(y2 - y1); /* +1 */ + bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1); + bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1); /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n", bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);) @@ -383,4 +443,4 @@ void gfx_VDrawLine(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y gfx_TransformX(bm, x1), gfx_TransformY(bm, y1), gfx_TransformY(bm, x2), gfx_TransformY(bm, y2)); } -#endif /* CONFIG_LCD_VCOORDS */ +#endif /* CONFIG_GFX_VCOORDS */