Optimize away divisions in RAST_ADDR/MASK macros.
[bertos.git] / gfx / gfx_p.h
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005, 2006 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.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  *
13  * \brief Graphics private header.
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.6  2006/05/27 17:17:34  bernie
19  *#* Optimize away divisions in RAST_ADDR/MASK macros.
20  *#*
21  *#* Revision 1.5  2006/05/25 23:35:40  bernie
22  *#* Cleanup.
23  *#*
24  *#* Revision 1.4  2006/03/22 09:50:37  bernie
25  *#* Use the same format for fonts and rasters.
26  *#*
27  *#* Revision 1.3  2006/02/15 09:10:15  bernie
28  *#* Implement prop fonts; Fix algo styles.
29  *#*
30  *#* Revision 1.2  2006/02/10 12:28:33  bernie
31  *#* Add font support in bitmaps; Make bitmap formats public.
32  *#*
33  *#* Revision 1.1  2006/01/26 00:32:49  bernie
34  *#* Graphics private header.
35  *#*
36  *#*/
37
38 #ifndef GFX_GFX_P_H
39 #define GFX_GFX_P_H
40
41 #include <gfx/gfx.h>
42
43 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
44
45         /* We use ucoord_t to let the compiler optimize away the division/modulo. */
46         #define RAST_ADDR(raster, x, y, stride) \
47                         ((raster) + (ucoord_t)(y) * (ucoord_t)(stride) + (ucoord_t)(x) / 8)
48         #define RAST_MASK(raster, x, y) \
49                         (1 << (7 - (ucoord_t)(x) % 8))
50
51 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
52
53         /* We use ucoord_t to let the compiler optimize away the division/modulo. */
54         #define RAST_ADDR(raster, x, y, stride) \
55                         ((raster) + ((ucoord_t)(y) / 8) * (ucoord_t)(stride) + (ucoord_t)(x))
56         #define RAST_MASK(raster, x, y) \
57                         (1 << ((ucoord_t)(y) % 8))
58
59 #else
60         #error Unknown value of CONFIG_BITMAP_FMT
61 #endif /* CONFIG_BITMAP_FMT */
62
63 #define BM_ADDR(bm, x, y)  RAST_ADDR((bm)->raster, (x), (y), (bm)->stride)
64 #define BM_MASK(bm, x, y)  RAST_MASK((bm)->raster, (x), (y))
65
66 /*!
67  * Plot a pixel in bitmap \a bm.
68  *
69  * \note bm is evaluated twice.
70  * \see BM_CLEAR BM_DRAWPIXEL
71  */
72 #define BM_PLOT(bm, x, y) \
73         ( *BM_ADDR(bm, x, y) |= BM_MASK(bm, x, y) )
74
75 /*!
76  * Clear a pixel in bitmap \a bm.
77  *
78  * \note bm is evaluated twice.
79  * \see BM_PLOT BM_DRAWPIXEL
80  */
81 #define BM_CLEAR(bm, x, y) \
82         ( *BM_ADDR(bm, x, y) &= ~BM_MASK(bm, x, y) )
83
84 /*!
85  * Set a pixel in bitmap \a bm to the specified color.
86  *
87  * \note bm is evaluated twice.
88  * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
89  * \see BM_PLOT BM_CLEAR
90  */
91 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
92         do { \
93                 uint8_t *p = BM_ADDR(bm, x, y); \
94                 uint8_t mask = BM_MASK(bm, x, y); \
95                 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
96         } while (0)
97
98 /*!
99  * Get the value of the pixel in bitmap \a bm.
100  *
101  * \return The returned value is either 0 or 1.
102  *
103  * \note bm is evaluated twice.
104  * \see BM_DRAWPIXEL
105  */
106 #define BM_READPIXEL(bm, x, y) \
107         ( *BM_ADDR(bm, x, y) & BM_MASK(bm, x, y) ? 1 : 0 )
108
109 #define RAST_READPIXEL(raster, x, y, stride) \
110                 ( *RAST_ADDR(raster, x, y, stride) & RAST_MASK(raster, x, y) ? 1 : 0 )
111
112 #endif /* GFX_GFX_P_H */