ebcf9c8dc7e483ad66b074abe4caa611a9c48ac8
[bertos.git] / bertos / gfx / gfx.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999 Bernie Innocenti <bernie@codewiz.org>
31  *
32  *
33  * \author Bernie Innocenti <bernie@codewiz.org>
34  * \author Stefano Fedrigo <aleph@develer.com>
35  *
36  * \brief General pourpose graphics routines
37  *
38  * $WIZ$ module_name = "gfx"
39  * $WIZ$ module_configuration = "bertos/cfg/cfg_gfx.h"
40  */
41
42 #ifndef GFX_GFX_H
43 #define GFX_GFX_H
44
45 #include "cfg/cfg_gfx.h"    /* CONFIG_GFX_* */
46 #include <cfg/compiler.h>
47
48 #include <cpu/attr.h>       /* CPU_HARVARD */
49
50
51 #define CONFIG_CHART_TYPE_X uint8_t ///< Type for the chart dataset
52 #define CONFIG_CHART_TYPE_Y uint8_t ///< Type for the chart dataset
53
54 /**
55  * \name Known pixel formats for bitmap representation.
56  * \{
57  * $WIZ$ bitmap_format = "BITMAP_FMT_PLANAR_H_MSB", "BITMAP_FMT_PLANAR_V_LSB"
58  */
59 #define BITMAP_FMT_PLANAR_H_MSB  1  /**< Planar pixels, horizontal bytes, MSB left. */
60 #define BITMAP_FMT_PLANAR_V_LSB  2  /**< Planar pixels, vertical bytes, LSB top. */
61 /* \} */
62
63 #if !defined(CONFIG_BITMAP_FMT) || (CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_H_MSB && CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_V_LSB)
64         #error CONFIG_BITMAP_FMT must be defined to either BITMAP_FMT_PLANAR_H_LSB or BITMAP_FMT_PLANAR_V_LSB
65 #endif
66 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
67         #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
68 #endif
69 #if !defined(CONFIG_GFX_TEXT) || (CONFIG_GFX_TEXT != 0 && CONFIG_GFX_TEXT != 1)
70         #error CONFIG_GFX_TEXT must be defined to either 0 or 1
71 #endif
72
73 EXTERN_C_BEGIN
74
75 /** Common type for coordinates expressed in pixel units */
76 typedef int coord_t;
77 typedef unsigned int ucoord_t;
78
79 #if CONFIG_GFX_VCOORDS
80 /** Common type for coordinates expressed in logical units */
81 typedef float vcoord_t;
82 #endif /* CONFIG_GFX_VCOORDS */
83
84
85 /**
86  * Describe a rectangular area with coordinates expressed in pixels.
87  *
88  * The rectangle is represented in terms of its top/left and
89  * right/bottom borders.
90  *
91  * In some cases, rectangles are assumed to obey to the
92  * following invariants:
93  *
94  *    xmin <= xmax
95  *    ymin <= ymax
96  *
97  * Oddly, the xmin and ymin coordinates are inclusive, while the
98  * xmax and ymax coordinates are non-inclusive.  This design
99  * decision makes several computations simpler and lets you
100  * specify empty (0x0) rectangles without breaking the
101  * invariants.
102  *
103  * Computing the size of a rectangle can be done by simply
104  * subtracting the maximum X or Y coordinate from the minimum
105  * X or Y coordinate.
106  */
107 typedef struct Rect { coord_t xmin, ymin, xmax, ymax; } Rect;
108
109 /**
110  * Return the width of a rectangle in pixels.
111  *
112  * \note The argument \a r is evaluated twice.
113  */
114 #define RECT_WIDTH(r)   ((r)->xmax - (r)->xmin)
115
116 /**
117  * Return the height of a rectangle in pixels.
118  *
119  * \note The argument \a r is evaluated twice.
120  */
121 #define RECT_HEIGHT(r)  ((r)->ymax - (r)->ymin)
122
123 /* Fwd decl */
124 struct Font;
125
126 /**
127  * Control structure to draw in a bitmap
128  *
129  * \todo For better ortogonality, split this structure into
130  *       an Image and a plain drawing context called Painter.
131  */
132 typedef struct Bitmap
133 {
134         uint8_t *raster;        /**< Pointer to byte array to hold the data */
135         coord_t width, height;  /**< Width/Height in pixels */
136         coord_t stride;         /**< Bytes per row. */
137         coord_t penX, penY;     /**< Current pen position MoveTo()/LineTo() */
138
139 #if CONFIG_GFX_CLIPPING || CONFIG_GFX_VCOORDS
140         Rect cr;                /**< Clip drawing inside this rectangle */
141 #endif
142
143 #if CONFIG_GFX_TEXT
144         const struct Font *font;/**< Current font for text rendering. */
145
146         /**
147          * Algorithmic text style flags.
148          *
149          * The text rendering routine can apply a few simple transformations
150          * to the current font in order to generate common styles such as
151          * bold, italic and underline from plain glyphs.
152          *
153          * \see text_style()
154          */
155         uint8_t styles;
156 #endif /* CONFIG_GFX_TEXT */
157
158 #if CONFIG_GFX_VCOORDS
159         /**
160          * \name Logical coordinate system
161          * \{
162          */
163         vcoord_t orgX, orgY;
164         vcoord_t scaleX, scaleY;
165         /*\}*/
166 #endif /* CONFIG_GFX_VCOORDS */
167
168 } Bitmap;
169
170 /**
171  * Hold image pixels.
172  *
173  * \todo Use this as Bitmap and change Bitmap to Drawable.
174  */
175 typedef struct Image
176 {
177         const uint8_t *raster;   /**< Pointer to byte array to hold the data. */
178         coord_t width;     /**< Raster width in pixels. */
179         coord_t height;    /**< Raster height in pixels. */
180         coord_t stride;    /**< Bytes per row. */
181 } Image;
182
183 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
184         /**
185          * Compute the size in bytes of a raster suitable for
186          * holding a bitmap of \a width x \a height pixels.
187          */
188         #define RAST_SIZE(width, height) ( (((width) + 7) / 8) * (height) )
189
190 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
191         /**
192          * Compute the size in bytes of a raster suitable for
193          * holding a bitmap of \a width x \a height pixels.
194          */
195         #define RAST_SIZE(width, height) ( (width) * (((height) + 7) / 8) )
196 #else
197         #error Unknown value of CONFIG_BITMAP_FMT
198 #endif /* CONFIG_BITMAP_FMT */
199
200 /* Function prototypes */
201 void gfx_bitmapInit (Bitmap *bm, uint8_t *raster, coord_t w, coord_t h);
202 void gfx_bitmapClear(Bitmap *bm);
203 void gfx_blit       (Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy);
204 void gfx_blitRaster (Bitmap *dst, coord_t dx, coord_t dy, const uint8_t *raster, coord_t w, coord_t h, coord_t stride);
205 void gfx_blitImage  (Bitmap *dst, coord_t dx, coord_t dy, const Image *image);
206 void gfx_line       (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
207 void gfx_rectDraw   (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
208 void gfx_rectFillC  (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color);
209 void gfx_rectFill   (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
210 void gfx_rectClear  (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
211 void gfx_moveTo     (Bitmap *bm, coord_t x,  coord_t y);
212 void gfx_lineTo     (Bitmap *bm, coord_t x,  coord_t y);
213 void gfx_setClipRect(Bitmap *bm, coord_t xmin, coord_t ymin, coord_t xmax, coord_t ymax);
214
215 #if CPU_HARVARD
216         #include <cpu/pgm.h>
217         void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster);
218 #endif
219
220 #if CONFIG_GFX_TEXT
221 INLINE void gfx_setFont(Bitmap *bm, const struct Font *font)
222 {
223         bm->font = font;
224 }
225 #endif
226
227 #if CONFIG_GFX_VCOORDS
228 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
229 coord_t gfx_transformX(Bitmap *bm, vcoord_t x);
230 coord_t gfx_transformY(Bitmap *bm, vcoord_t y);
231 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
232 #endif /* CONFIG_GFX_VCOORDS */
233
234 EXTERN_C_END
235
236 #endif /* GFX_GFX_H */