Rename rectangle drawing functions; Unify filled/cleared implementations.
authorbernie <bernie@38d2e660-2303-0410-9eaa-f027e97ec537>
Tue, 14 Sep 2004 21:01:08 +0000 (21:01 +0000)
committerbernie <bernie@38d2e660-2303-0410-9eaa-f027e97ec537>
Tue, 14 Sep 2004 21:01:08 +0000 (21:01 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@200 38d2e660-2303-0410-9eaa-f027e97ec537

mware/gfx.c
mware/gfx.h

index 9f9cf6215c57ef984f76f49464b2ecc8ca033775..9e4c30f640a7940b71f62ace003fef1cab52ca65 100755 (executable)
@@ -1,7 +1,7 @@
 /*!
  * \file
  * <!--
- * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
  * This file is part of DevLib - See devlib/README for information.
  * -->
@@ -16,8 +16,8 @@
 
 /*#*
  *#* $Log$
- *#* Revision 1.6  2004/09/06 21:50:46  bernie
- *#* Clarify side-effects in documentation.
+ *#* 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.
  *#*/
 
 #include "gfx.h"
-#include "config.h"
-#include <drv/kdebug.h>
+#include "config.h"  /* CONFIG_GFX_CLIPPING */
+#include <debug.h>
+#include <cpu.h>     /* CPU_AVR */
+#include <macros.h>  /* SWAP() */
 
 #include <string.h>
 
 
 /*!
- * 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.
  *
@@ -88,6 +95,7 @@ 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.
@@ -98,6 +106,7 @@ void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster)
 {
        memcpy_P(bm->raster, raster, bm->height/8 * bm->width);
 }
+#endif /* CPU_AVR */
 
 
 /*!
@@ -265,13 +274,33 @@ void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y)
 
 
 /*!
- * Draw a filled 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_RectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
+{
+       /* Sort coords (needed for correct bottom-right semantics) */
+       if (x1 > x2) SWAP(x1, x2);
+       if (y1 > y2) SWAP(y1, y2);
+
+       /* Draw rectangle */
+       gfx_DrawLine(bm, x1,   y1,   x2-1, y1);
+       gfx_DrawLine(bm, x2-1, y1,   x2-1, y2-1);
+       gfx_DrawLine(bm, x2-1, y2-1, x1,   y2-1);
+       gfx_DrawLine(bm, x1,   y2-1, x1,   y1);
+}
+
+
+/*!
+ * 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_FillRect(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;
 
@@ -289,60 +318,48 @@ void gfx_FillRect(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_PLOT(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 an empty rectangle.
+ * 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_DrawRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
+void gfx_RectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
 {
-       /* Sort coords */
-       if (x1 > x2) SWAP(x1, x2);
-       if (y1 > y2) SWAP(y1, y2);
-
-       /* Draw rectangle */
-       gfx_DrawLine(bm, x1,   y1,   x2-1, y1);
-       gfx_DrawLine(bm, x2-1, y1,   x2-1, y2-1);
-       gfx_DrawLine(bm, x2-1, y2-1, x1,   y2-1);
-       gfx_DrawLine(bm, x1,   y2-1, x1,   y1);
+       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_ClearRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
+void gfx_RectClear(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_CLEAR(bm, x, y);
+       gfx_RectFillC(bm, x1, y1, x2, y2, 0x00);
 }
 
 
@@ -381,8 +398,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);)
index 792a49c39bd7b864b9c0792f9515a531da362e9f..0239ad1754b3616cf3c466f9978d6a9d5244e8ac 100755 (executable)
@@ -1,7 +1,7 @@
 /*!
  * \file
- * Copyright (C) 2003,2004 Develer S.r.l. (http://www.develer.com/)
- * Copyright (C) 1999 Bernardo Innocenti <bernie@develer.com>
+ * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
  * This file is part of DevLib - See devlib/README for information.
  *
  * \version $Id$
@@ -14,8 +14,8 @@
 
 /*#*
  *#* $Log$
- *#* Revision 1.5  2004/08/25 14:12:09  rasky
- *#* Aggiornato il comment block dei log RCS
+ *#* Revision 1.6  2004/09/14 21:01:08  bernie
+ *#* Rename rectangle drawing functions; Unify filled/cleared implementations.
  *#*
  *#* Revision 1.4  2004/08/10 07:00:16  bernie
  *#* Add missing header.
  *#*
  *#* Revision 1.1  2004/05/23 15:43:16  bernie
  *#* Import mware modules.
- *#*
- *#* Revision 1.4  2004/02/09 00:21:28  aleph
- *#* Various gfx fixes
- *#*
- *#* Revision 1.3  2004/01/27 23:24:19  aleph
- *#* Add new graphics primitives
- *#*
- *#* Revision 1.2  2004/01/07 23:33:01  aleph
- *#* Change copyright email
- *#*
- *#* Revision 1.1  2004/01/07 19:05:31  aleph
- *#* Add graphics routines
- *#*
  *#*/
 
 #ifndef MWARE_GFX_H
@@ -91,13 +78,19 @@ extern void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h);
 extern void gfx_ClearBitmap(Bitmap *bm);
 extern void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster);
 extern void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
-extern void gfx_FillRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
-extern void gfx_DrawRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
-extern void gfx_ClearRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
+extern void gfx_RectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
+extern void gfx_RectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color);
+extern void gfx_RectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
+extern void gfx_RectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
 extern void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y);
 extern void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y);
 extern void gfx_SetClipRect(Bitmap *bm, coord_t xmin, coord_t ymin, coord_t xmax, coord_t ymax);
 
+/* DEPRECATED names */
+#define gfx_DrawRect  gfx_RectDraw
+#define gfx_FillRect  gfx_RectFill
+#define gfx_ClearRect gfx_RectClear
+
 #if CONFIG_GFX_VCOORDS
 extern void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
 extern coord_t gfx_TransformX(Bitmap *bm, vcoord_t x);