Remove Bitmap::cr when not needed.
[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.18  2006/08/01 12:45:01  bernie
18  *#* Remove Bitmap::cr when not needed.
19  *#*
20  *#* Revision 1.17  2006/07/19 12:56:26  bernie
21  *#* Convert to new Doxygen style.
22  *#*
23  *#* Revision 1.16  2006/05/27 17:17:34  bernie
24  *#* Optimize away divisions in RAST_ADDR/MASK macros.
25  *#*
26  *#* Revision 1.15  2006/05/25 23:35:40  bernie
27  *#* Cleanup.
28  *#*
29  *#* Revision 1.14  2006/04/27 05:39:24  bernie
30  *#* Enhance text rendering to arbitrary x,y coords.
31  *#*
32  *#* Revision 1.13  2006/04/11 00:08:24  bernie
33  *#* text_offset(): New function, but I'm not quite confident with the design.
34  *#*
35  *#* Revision 1.12  2006/03/27 04:48:56  bernie
36  *#* gfx_blitImage(): New function; gfx_blitRaster(): Fix clipping bug.
37  *#*
38  *#* Revision 1.11  2006/03/07 22:18:04  bernie
39  *#* Correctly compute text width for prop fonts; Make styles a per-bitmap attribute.
40  *#*
41  *#* Revision 1.10  2006/02/17 22:24:40  bernie
42  *#* Fix undefined preprocessor symbol.
43  *#*
44  *#* Revision 1.9  2006/02/15 09:10:15  bernie
45  *#* Implement prop fonts; Fix algo styles.
46  *#*
47  *#* Revision 1.8  2006/02/10 12:28:33  bernie
48  *#* Add font support in bitmaps; Make bitmap formats public.
49  *#*
50  *#* Revision 1.7  2006/01/26 00:36:48  bernie
51  *#* Const correctness for some new functions.
52  *#*
53  *#* Revision 1.6  2006/01/23 23:13:04  bernie
54  *#* RECT_WIDTH(), RECT_HEIGHT(), RASTER_SIZE(): New macros.
55  *#*
56  *#* Revision 1.5  2006/01/17 02:31:29  bernie
57  *#* Add bitmap format support; Improve some comments.
58  *#*/
59
60 #ifndef GFX_GFX_H
61 #define GFX_GFX_H
62
63 #include <cfg/compiler.h>
64 #include <cfg/cpu.h>   /* CPU_HARVARD */
65
66 #include <appconfig.h> /* CONFIG_GFX_* */
67
68 /**
69  * \name Known pixel formats for bitmap representation.
70  * \{
71  */
72 #define BITMAP_FMT_PLANAR_H_MSB  1  /**< Planar pixels, horizontal bytes, MSB left. */
73 #define BITMAP_FMT_PLANAR_V_LSB  2  /**< Planar pixels, vertical bytes, LSB top. */
74 /* \} */
75
76 #if !defined(CONFIG_BITMAP_FMT) || (CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_H_MSB && CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_V_LSB)
77         #error CONFIG_BITMAP_FMT must be defined to either BITMAP_FMT_PLANAR_H_LSB or BITMAP_FMT_PLANAR_V_LSB
78 #endif
79 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
80         #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
81 #endif
82 #if !defined(CONFIG_GFX_TEXT) || (CONFIG_GFX_TEXT != 0 && CONFIG_GFX_TEXT != 1)
83         #error CONFIG_GFX_TEXT must be defined to either 0 or 1
84 #endif
85
86 EXTERN_C_BEGIN
87
88 /** Common type for coordinates expressed in pixel units */
89 typedef int coord_t;
90 typedef unsigned int ucoord_t;
91
92 #if CONFIG_GFX_VCOORDS
93 /** Common type for coordinates expressed in logical units */
94 typedef float vcoord_t;
95 #endif /* CONFIG_GFX_VCOORDS */
96
97
98 /**
99  * Describe a rectangular area with coordinates expressed in pixels.
100  *
101  * The rectangle is represented in terms of its top/left and
102  * right/bottom borders.
103  *
104  * In some cases, rectangles are assumed to obey to the
105  * following invariants:
106  *
107  *    xmin <= xmax
108  *    ymin <= ymax
109  *
110  * Oddly, the xmin and ymin coordinates are inclusive, while the
111  * xmax and ymax coordinates are non-inclusive.  This design
112  * decision makes several computations simpler and lets you
113  * specify empty (0x0) rectangles without breaking the
114  * invariants.
115  *
116  * Computing the size of a rectangle can be done by simply
117  * subtracting the maximum X or Y coordinate from the minimum
118  * X or Y coordinate.
119  */
120 typedef struct Rect { coord_t xmin, ymin, xmax, ymax; } Rect;
121
122 /**
123  * Return the width of a rectangle in pixels.
124  *
125  * \note The argument \a r is evaluated twice.
126  */
127 #define RECT_WIDTH(r)   ((r)->xmax - (r)->xmin)
128
129 /**
130  * Return the height of a rectangle in pixels.
131  *
132  * \note The argument \a r is evaluated twice.
133  */
134 #define RECT_HEIGHT(r)  ((r)->ymax - (r)->ymin)
135
136 /* Fwd decl */
137 struct Font;
138
139 /**
140  * Control structure to draw in a bitmap
141  *
142  * \todo For better ortogonality, split this structure into
143  *       an Image and a plain drawing context called Painter.
144  */
145 typedef struct Bitmap
146 {
147         uint8_t *raster;        /**< Pointer to byte array to hold the data */
148         coord_t width, height;  /**< Width/Height in pixels */
149         coord_t stride;         /**< Bytes per row. */
150         coord_t penX, penY;     /**< Current pen position MoveTo()/LineTo() */
151
152 #if CONFIG_GFX_CLIPPING || CONFIG_GFX_VCOORDS
153         Rect cr;                /**< Clip drawing inside this rectangle */
154 #endif
155
156 #if CONFIG_GFX_TEXT
157         const struct Font *font;/**< Current font for text rendering. */
158
159         /**
160          * Algorithmic text style flags.
161          *
162          * The text rendering routine can apply a few simple transformations
163          * to the current font in order to generate common styles such as
164          * bold, italic and underline from plain glyphs.
165          *
166          * \see text_style()
167          */
168         uint8_t styles;
169 #endif /* CONFIG_GFX_TEXT */
170
171 #if CONFIG_GFX_VCOORDS
172         /**
173          * \name Logical coordinate system
174          * \{
175          */
176         vcoord_t orgX, orgY;
177         vcoord_t scaleX, scaleY;
178         /*\}*/
179 #endif /* CONFIG_GFX_VCOORDS */
180
181 } Bitmap;
182
183 /**
184  * Hold image pixels.
185  *
186  * \todo Use this as Bitmap and change Bitmap to Drawable.
187  */
188 typedef struct Image
189 {
190         const uint8_t *raster;   /**< Pointer to byte array to hold the data. */
191         coord_t width;     /**< Raster width in pixels. */
192         coord_t height;    /**< Raster height in pixels. */
193         coord_t stride;    /**< Bytes per row. */
194 } Image;
195
196 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
197         /**
198          * Compute the size in bytes of a raster suitable for
199          * holding a bitmap of \a width x \a height pixels.
200          */
201         #define RAST_SIZE(width, height) ( ((width) + 7 / 8) * (height) )
202
203 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
204         /**
205          * Compute the size in bytes of a raster suitable for
206          * holding a bitmap of \a width x \a height pixels.
207          */
208         #define RAST_SIZE(width, height) ( (width) * (((height) + 7) / 8) )
209 #else
210         #error Unknown value of CONFIG_BITMAP_FMT
211 #endif /* CONFIG_BITMAP_FMT */
212
213 #define RASTER_SIZE RAST_SIZE /* OBSOLETE */
214
215 /* Function prototypes */
216 void gfx_bitmapInit (Bitmap *bm, uint8_t *raster, coord_t w, coord_t h);
217 void gfx_bitmapClear(Bitmap *bm);
218 void gfx_blit       (Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy);
219 void gfx_blitRaster (Bitmap *dst, coord_t dx, coord_t dy, const uint8_t *raster, coord_t w, coord_t h, coord_t stride);
220 void gfx_blitImage  (Bitmap *dst, coord_t dx, coord_t dy, const Image *image);
221 void gfx_line       (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
222 void gfx_rectDraw   (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
223 void gfx_rectFillC  (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color);
224 void gfx_rectFill   (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
225 void gfx_rectClear  (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
226 void gfx_moveTo     (Bitmap *bm, coord_t x,  coord_t y);
227 void gfx_lineTo     (Bitmap *bm, coord_t x,  coord_t y);
228 void gfx_setClipRect(Bitmap *bm, coord_t xmin, coord_t ymin, coord_t xmax, coord_t ymax);
229
230 #if CPU_HARVARD
231         #include <mware/pgm.h>
232         void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster);
233 #endif
234
235 #if CONFIG_GFX_TEXT
236 INLINE void gfx_setFont(Bitmap *bm, const struct Font *font)
237 {
238         bm->font = font;
239 }
240 #endif
241
242 #if CONFIG_GFX_VCOORDS
243 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
244 coord_t gfx_transformX(Bitmap *bm, vcoord_t x);
245 coord_t gfx_transformY(Bitmap *bm, vcoord_t y);
246 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
247 #endif /* CONFIG_GFX_VCOORDS */
248
249 EXTERN_C_END
250
251 #endif /* GFX_GFX_H */