Const correctness for some new functions.
[bertos.git] / gfx / gfx.h
1 /*!
2  * \file
3  * Copyright 2003, 2004, 2005 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.7  2006/01/26 00:36:48  bernie
18  *#* Const correctness for some new functions.
19  *#*
20  *#* Revision 1.6  2006/01/23 23:13:04  bernie
21  *#* RECT_WIDTH(), RECT_HEIGHT(), RASTER_SIZE(): New macros.
22  *#*
23  *#* Revision 1.5  2006/01/17 02:31:29  bernie
24  *#* Add bitmap format support; Improve some comments.
25  *#*
26  *#* Revision 1.4  2006/01/16 03:30:57  bernie
27  *#* Make header C++ friendly.
28  *#*
29  *#* Revision 1.3  2005/11/27 23:33:40  bernie
30  *#* Use appconfig.h instead of cfg/config.h.
31  *#*
32  *#* Revision 1.2  2005/11/04 18:17:45  bernie
33  *#* Fix header guards and includes for new location of gfx module.
34  *#*
35  *#* Revision 1.1  2005/11/04 18:11:35  bernie
36  *#* Move graphics stuff from mware/ to gfx/.
37  *#*
38  *#* Revision 1.12  2005/11/04 16:20:02  bernie
39  *#* Fix reference to README.devlib in header.
40  *#*
41  *#* Revision 1.11  2005/04/11 19:10:28  bernie
42  *#* Include top-level headers from cfg/ subdir.
43  *#*
44  *#* Revision 1.10  2005/03/01 23:26:45  bernie
45  *#* Use new CPU-neutral program-memory API.
46  *#*
47  *#* Revision 1.9  2005/01/20 18:46:31  aleph
48  *#* Fix progmem includes.
49  *#*/
50
51 #ifndef GFX_GFX_H
52 #define GFX_GFX_H
53
54 #include <appconfig.h>
55 #include <cfg/compiler.h>
56 #include <cfg/cpu.h>
57
58 EXTERN_C_BEGIN
59
60 /*! Common type for coordinates expressed in pixel units */
61 typedef int coord_t;
62
63 #if CONFIG_GFX_VCOORDS
64 /*! Common type for coordinates expressed in logical units */
65 typedef float vcoord_t;
66 #endif /* CONFIG_GFX_VCOORDS */
67
68
69 /**
70  * Describe a rectangular area with coordinates expressed in pixels.
71  *
72  * The rectangle is represented in terms of its top/left and
73  * right/bottom borders.
74  *
75  * In some cases, rectangles are assumed to obey to the
76  * following invariants:
77  *
78  *    xmin <= xmax
79  *    ymin <= ymax
80  *
81  * Oddly, the xmin and ymin coordinates are inclusive, while the
82  * xmax and ymax coordinates are non-inclusive.  This design
83  * decision makes several computations simpler and lets you
84  * specify empty (0x0) rectangles without breaking the
85  * invariants.
86  *
87  * Computing the size of a rectangle can be done by simply
88  * subtracting the maximum X or Y coordinate from the minimum
89  * X or Y coordinate.
90  */
91 typedef struct Rect { coord_t xmin, ymin, xmax, ymax; } Rect;
92
93 /**
94  * Return the width of a rectangle in pixels.
95  *
96  * \note The argument \a r is evaluated twice.
97  */
98 #define RECT_WIDTH(r)   ((r)->xmax - (r)->xmin)
99
100 /**
101  * Return the height of a rectangle in pixels.
102  *
103  * \note The argument \a r is evaluated twice.
104  */
105 #define RECT_HEIGHT(r)  ((r)->ymax - (r)->ymin)
106
107
108 /*!
109  * Control structure to draw in a bitmap
110  */
111 typedef struct Bitmap
112 {
113         uint8_t *raster;        /*!< Pointer to byte array to hold the data */
114         coord_t width, height;  /*!< Width/Height in pixels */
115         coord_t stride;         /*!< Bytes per row. */
116         coord_t penX, penY;     /*!< Current pen position MoveTo()/LineTo() */
117
118         Rect cr;                /*!< Clip drawing inside this rectangle */
119
120 #if CONFIG_GFX_VCOORDS
121         /*!
122          * \name Logical coordinate system
123          * \{
124          */
125         vcoord_t orgX, orgY;
126         vcoord_t scaleX, scaleY;
127         /*\}*/
128 #endif /* CONFIG_GFX_VCOORDS */
129
130 } Bitmap;
131
132 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
133         /**
134          * Compute the size in bytes of a raster suitable for
135          * holding a bitmap of \a width x \a height pixels.
136          */
137         #define RASTER_SIZE(width, height) ( ((width) + 7 / 8) * (height) )
138
139 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
140         /**
141          * Compute the size in bytes of a raster suitable for
142          * holding a bitmap of \a width x \a height pixels.
143          */
144         #define RASTER_SIZE(width, height) ( (width) * (((height) + 7) / 8) )
145 #else
146         #error Unknown value of CONFIG_BITMAP_FMT
147 #endif /* CONFIG_BITMAP_FMT */
148
149
150 /* Function prototypes */
151 extern void gfx_bitmapInit (Bitmap *bm, uint8_t *raster, coord_t w, coord_t h);
152 extern void gfx_bitmapClear(Bitmap *bm);
153 extern void gfx_blit       (Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy);
154 extern void gfx_line       (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
155 extern void gfx_rectDraw   (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
156 extern void gfx_rectFillC  (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color);
157 extern void gfx_rectFill   (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
158 extern void gfx_rectClear  (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
159 extern void gfx_moveTo     (Bitmap *bm, coord_t x,  coord_t y);
160 extern void gfx_lineTo     (Bitmap *bm, coord_t x,  coord_t y);
161 extern void gfx_setClipRect(Bitmap *bm, coord_t xmin, coord_t ymin, coord_t xmax, coord_t ymax);
162
163 #if CPU_HARVARD
164         #include <mware/pgm.h>
165         extern void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster);
166 #endif
167
168 #if CONFIG_GFX_VCOORDS
169 extern void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
170 extern coord_t gfx_transformX(Bitmap *bm, vcoord_t x);
171 extern coord_t gfx_transformY(Bitmap *bm, vcoord_t y);
172 extern void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
173 #endif /* CONFIG_GFX_VCOORDS */
174
175 EXTERN_C_END
176
177 #endif /* GFX_GFX_H */