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