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