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