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