Add font support in bitmaps; Make bitmap formats public.
[bertos.git] / gfx / gfx.h
1 /*!
2  * \file
3  * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
4  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
5  * This file is part of DevLib - See README.devlib for information.
6  *
7  * \version $Id$
8  *
9  * \author Bernardo Innocenti <bernie@develer.com>
10  * \author Stefano Fedrigo <aleph@develer.com>
11  *
12  * \brief General pourpose graphics routines
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.8  2006/02/10 12:28:33  bernie
18  *#* Add font support in bitmaps; Make bitmap formats public.
19  *#*
20  *#* Revision 1.7  2006/01/26 00:36:48  bernie
21  *#* Const correctness for some new functions.
22  *#*
23  *#* Revision 1.6  2006/01/23 23:13:04  bernie
24  *#* RECT_WIDTH(), RECT_HEIGHT(), RASTER_SIZE(): New macros.
25  *#*
26  *#* Revision 1.5  2006/01/17 02:31:29  bernie
27  *#* Add bitmap format support; Improve some comments.
28  *#*/
29
30 #ifndef GFX_GFX_H
31 #define GFX_GFX_H
32
33 #include <cfg/compiler.h>
34 #include <cfg/cpu.h>   /* CPU_HARVARD */
35
36 #include <appconfig.h> /* CONFIG_GFX_* */
37
38 #if !defined(CONFIG_BITMAP_FMT) || (CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_H_MSB && CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_V_LSB)
39         #error CONFIG_BITMAP_FMT must be defined to either BITMAP_FMT_PLANAR_H_LSB or BITMAP_FMT_PLANAR_V_LSB
40 #endif
41 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
42         #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
43 #endif
44 #if !defined(CONFIG_GFX_TEXT) || (CONFIG_GFX_TEXT != 0 && CONFIG_GFX_TEXT != 1)
45         #error CONFIG_GFX_TEXT must be defined to either 0 or 1
46 #endif
47
48 /**
49  * \name Known pixel formats for bitmap representation.
50  * \{
51  */
52 #define BITMAP_FMT_PLANAR_H_MSB  1  /**< Planar pixels, horizontal bytes, MSB left. */
53 #define BITMAP_FMT_PLANAR_V_LSB  2  /**< Planar pixels, vertical bytes, LSB top. */
54 /* \} */
55
56 EXTERN_C_BEGIN
57
58 /*! Common type for coordinates expressed in pixel units */
59 typedef int coord_t;
60
61 #if CONFIG_GFX_VCOORDS
62 /*! Common type for coordinates expressed in logical units */
63 typedef float vcoord_t;
64 #endif /* CONFIG_GFX_VCOORDS */
65
66
67 /**
68  * Describe a rectangular area with coordinates expressed in pixels.
69  *
70  * The rectangle is represented in terms of its top/left and
71  * right/bottom borders.
72  *
73  * In some cases, rectangles are assumed to obey to the
74  * following invariants:
75  *
76  *    xmin <= xmax
77  *    ymin <= ymax
78  *
79  * Oddly, the xmin and ymin coordinates are inclusive, while the
80  * xmax and ymax coordinates are non-inclusive.  This design
81  * decision makes several computations simpler and lets you
82  * specify empty (0x0) rectangles without breaking the
83  * invariants.
84  *
85  * Computing the size of a rectangle can be done by simply
86  * subtracting the maximum X or Y coordinate from the minimum
87  * X or Y coordinate.
88  */
89 typedef struct Rect { coord_t xmin, ymin, xmax, ymax; } Rect;
90
91 /**
92  * Return the width of a rectangle in pixels.
93  *
94  * \note The argument \a r is evaluated twice.
95  */
96 #define RECT_WIDTH(r)   ((r)->xmax - (r)->xmin)
97
98 /**
99  * Return the height of a rectangle in pixels.
100  *
101  * \note The argument \a r is evaluated twice.
102  */
103 #define RECT_HEIGHT(r)  ((r)->ymax - (r)->ymin)
104
105 /* Fwd decl */
106 struct Font;
107
108 /*!
109  * Control structure to draw in a bitmap
110  */
111 typedef struct Bitmap
112 {
113         uint8_t *raster;        /*!< Pointer to byte array to hold the data */
114         coord_t width, height;  /*!< Width/Height in pixels */
115         coord_t stride;         /*!< Bytes per row. */
116         coord_t penX, penY;     /*!< Current pen position MoveTo()/LineTo() */
117
118         Rect cr;                /*!< Clip drawing inside this rectangle */
119
120 #if CONFIG_GFX_TEXT
121         struct Font *font;      /**< Current font for text rendering. */
122 #endif
123 #if CONFIG_GFX_VCOORDS
124         /*!
125          * \name Logical coordinate system
126          * \{
127          */
128         vcoord_t orgX, orgY;
129         vcoord_t scaleX, scaleY;
130         /*\}*/
131 #endif /* CONFIG_GFX_VCOORDS */
132
133 } Bitmap;
134
135 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
136         /**
137          * Compute the size in bytes of a raster suitable for
138          * holding a bitmap of \a width x \a height pixels.
139          */
140         #define RASTER_SIZE(width, height) ( ((width) + 7 / 8) * (height) )
141
142 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
143         /**
144          * Compute the size in bytes of a raster suitable for
145          * holding a bitmap of \a width x \a height pixels.
146          */
147         #define RASTER_SIZE(width, height) ( (width) * (((height) + 7) / 8) )
148 #else
149         #error Unknown value of CONFIG_BITMAP_FMT
150 #endif /* CONFIG_BITMAP_FMT */
151
152
153 /* Function prototypes */
154 void gfx_bitmapInit (Bitmap *bm, uint8_t *raster, coord_t w, coord_t h);
155 void gfx_bitmapClear(Bitmap *bm);
156 void gfx_blit       (Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy);
157 void gfx_blitRaster (Bitmap *dst, coord_t dx, coord_t dy, const uint8_t *raster, coord_t w, coord_t h);
158 void gfx_line       (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
159 void gfx_rectDraw   (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
160 void gfx_rectFillC  (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color);
161 void gfx_rectFill   (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
162 void gfx_rectClear  (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
163 void gfx_moveTo     (Bitmap *bm, coord_t x,  coord_t y);
164 void gfx_lineTo     (Bitmap *bm, coord_t x,  coord_t y);
165 void gfx_setClipRect(Bitmap *bm, coord_t xmin, coord_t ymin, coord_t xmax, coord_t ymax);
166
167 #if CPU_HARVARD
168         #include <mware/pgm.h>
169         void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster);
170 #endif
171
172 #if CONFIG_GFX_TEXT
173 INLINE void gfx_setFont(Bitmap *bm, Font *font)
174 {
175         bm->font = font;
176 }
177 #endif
178
179 #if CONFIG_GFX_VCOORDS
180 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
181 coord_t gfx_transformX(Bitmap *bm, vcoord_t x);
182 coord_t gfx_transformY(Bitmap *bm, vcoord_t y);
183 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
184 #endif /* CONFIG_GFX_VCOORDS */
185
186 EXTERN_C_END
187
188 #endif /* GFX_GFX_H */