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.
9 * \author Bernardo Innocenti <bernie@develer.com>
10 * \author Stefano Fedrigo <aleph@develer.com>
12 * \brief General pourpose graphics routines
17 *#* Revision 1.17 2006/07/19 12:56:26 bernie
18 *#* Convert to new Doxygen style.
20 *#* Revision 1.16 2006/05/27 17:17:34 bernie
21 *#* Optimize away divisions in RAST_ADDR/MASK macros.
23 *#* Revision 1.15 2006/05/25 23:35:40 bernie
26 *#* Revision 1.14 2006/04/27 05:39:24 bernie
27 *#* Enhance text rendering to arbitrary x,y coords.
29 *#* Revision 1.13 2006/04/11 00:08:24 bernie
30 *#* text_offset(): New function, but I'm not quite confident with the design.
32 *#* Revision 1.12 2006/03/27 04:48:56 bernie
33 *#* gfx_blitImage(): New function; gfx_blitRaster(): Fix clipping bug.
35 *#* Revision 1.11 2006/03/07 22:18:04 bernie
36 *#* Correctly compute text width for prop fonts; Make styles a per-bitmap attribute.
38 *#* Revision 1.10 2006/02/17 22:24:40 bernie
39 *#* Fix undefined preprocessor symbol.
41 *#* Revision 1.9 2006/02/15 09:10:15 bernie
42 *#* Implement prop fonts; Fix algo styles.
44 *#* Revision 1.8 2006/02/10 12:28:33 bernie
45 *#* Add font support in bitmaps; Make bitmap formats public.
47 *#* Revision 1.7 2006/01/26 00:36:48 bernie
48 *#* Const correctness for some new functions.
50 *#* Revision 1.6 2006/01/23 23:13:04 bernie
51 *#* RECT_WIDTH(), RECT_HEIGHT(), RASTER_SIZE(): New macros.
53 *#* Revision 1.5 2006/01/17 02:31:29 bernie
54 *#* Add bitmap format support; Improve some comments.
60 #include <cfg/compiler.h>
61 #include <cfg/cpu.h> /* CPU_HARVARD */
63 #include <appconfig.h> /* CONFIG_GFX_* */
66 * \name Known pixel formats for bitmap representation.
69 #define BITMAP_FMT_PLANAR_H_MSB 1 /**< Planar pixels, horizontal bytes, MSB left. */
70 #define BITMAP_FMT_PLANAR_V_LSB 2 /**< Planar pixels, vertical bytes, LSB top. */
73 #if !defined(CONFIG_BITMAP_FMT) || (CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_H_MSB && CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_V_LSB)
74 #error CONFIG_BITMAP_FMT must be defined to either BITMAP_FMT_PLANAR_H_LSB or BITMAP_FMT_PLANAR_V_LSB
76 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
77 #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
79 #if !defined(CONFIG_GFX_TEXT) || (CONFIG_GFX_TEXT != 0 && CONFIG_GFX_TEXT != 1)
80 #error CONFIG_GFX_TEXT must be defined to either 0 or 1
85 /** Common type for coordinates expressed in pixel units */
87 typedef unsigned int ucoord_t;
89 #if CONFIG_GFX_VCOORDS
90 /** Common type for coordinates expressed in logical units */
91 typedef float vcoord_t;
92 #endif /* CONFIG_GFX_VCOORDS */
96 * Describe a rectangular area with coordinates expressed in pixels.
98 * The rectangle is represented in terms of its top/left and
99 * right/bottom borders.
101 * In some cases, rectangles are assumed to obey to the
102 * following invariants:
107 * Oddly, the xmin and ymin coordinates are inclusive, while the
108 * xmax and ymax coordinates are non-inclusive. This design
109 * decision makes several computations simpler and lets you
110 * specify empty (0x0) rectangles without breaking the
113 * Computing the size of a rectangle can be done by simply
114 * subtracting the maximum X or Y coordinate from the minimum
117 typedef struct Rect { coord_t xmin, ymin, xmax, ymax; } Rect;
120 * Return the width of a rectangle in pixels.
122 * \note The argument \a r is evaluated twice.
124 #define RECT_WIDTH(r) ((r)->xmax - (r)->xmin)
127 * Return the height of a rectangle in pixels.
129 * \note The argument \a r is evaluated twice.
131 #define RECT_HEIGHT(r) ((r)->ymax - (r)->ymin)
137 * Control structure to draw in a bitmap
139 * \todo For better ortogonality, split this structure into
140 * an Image and a plain drawing context called Painter.
142 typedef struct Bitmap
144 uint8_t *raster; /**< Pointer to byte array to hold the data */
145 coord_t width, height; /**< Width/Height in pixels */
146 coord_t stride; /**< Bytes per row. */
147 coord_t penX, penY; /**< Current pen position MoveTo()/LineTo() */
149 Rect cr; /**< Clip drawing inside this rectangle */
152 const struct Font *font;/**< Current font for text rendering. */
155 * Algorithmic text style flags.
157 * The text rendering routine can apply a few simple transformations
158 * to the current font in order to generate common styles such as
159 * bold, italic and underline from plain glyphs.
165 #if CONFIG_GFX_VCOORDS
167 * \name Logical coordinate system
171 vcoord_t scaleX, scaleY;
173 #endif /* CONFIG_GFX_VCOORDS */
180 * \todo Use this as Bitmap and change Bitmap to Drawable.
184 const uint8_t *raster; /**< Pointer to byte array to hold the data. */
185 coord_t width; /**< Raster width in pixels. */
186 coord_t height; /**< Raster height in pixels. */
187 coord_t stride; /**< Bytes per row. */
190 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
192 * Compute the size in bytes of a raster suitable for
193 * holding a bitmap of \a width x \a height pixels.
195 #define RAST_SIZE(width, height) ( ((width) + 7 / 8) * (height) )
197 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
199 * Compute the size in bytes of a raster suitable for
200 * holding a bitmap of \a width x \a height pixels.
202 #define RAST_SIZE(width, height) ( (width) * (((height) + 7) / 8) )
204 #error Unknown value of CONFIG_BITMAP_FMT
205 #endif /* CONFIG_BITMAP_FMT */
207 #define RASTER_SIZE RAST_SIZE /* OBSOLETE */
209 /* Function prototypes */
210 void gfx_bitmapInit (Bitmap *bm, uint8_t *raster, coord_t w, coord_t h);
211 void gfx_bitmapClear(Bitmap *bm);
212 void gfx_blit (Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy);
213 void gfx_blitRaster (Bitmap *dst, coord_t dx, coord_t dy, const uint8_t *raster, coord_t w, coord_t h, coord_t stride);
214 void gfx_blitImage (Bitmap *dst, coord_t dx, coord_t dy, const Image *image);
215 void gfx_line (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
216 void gfx_rectDraw (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
217 void gfx_rectFillC (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color);
218 void gfx_rectFill (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
219 void gfx_rectClear (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
220 void gfx_moveTo (Bitmap *bm, coord_t x, coord_t y);
221 void gfx_lineTo (Bitmap *bm, coord_t x, coord_t y);
222 void gfx_setClipRect(Bitmap *bm, coord_t xmin, coord_t ymin, coord_t xmax, coord_t ymax);
225 #include <mware/pgm.h>
226 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster);
230 INLINE void gfx_setFont(Bitmap *bm, const struct Font *font)
236 #if CONFIG_GFX_VCOORDS
237 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
238 coord_t gfx_transformX(Bitmap *bm, vcoord_t x);
239 coord_t gfx_transformY(Bitmap *bm, vcoord_t y);
240 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
241 #endif /* CONFIG_GFX_VCOORDS */
245 #endif /* GFX_GFX_H */