3 * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
4 * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
9 * \author Bernardo Innocenti <bernie@develer.com>
10 * \author Stefano Fedrigo <aleph@develer.com>
12 * \brief General pourpose graphics routines
18 #include <cfg/compiler.h>
19 #include <cfg/cpu.h> /* CPU_HARVARD */
21 #include <appconfig.h> /* CONFIG_GFX_* */
24 * \name Known pixel formats for bitmap representation.
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. */
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
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
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
43 /** Common type for coordinates expressed in pixel units */
45 typedef unsigned int ucoord_t;
47 #if CONFIG_GFX_VCOORDS
48 /** Common type for coordinates expressed in logical units */
49 typedef float vcoord_t;
50 #endif /* CONFIG_GFX_VCOORDS */
54 * Describe a rectangular area with coordinates expressed in pixels.
56 * The rectangle is represented in terms of its top/left and
57 * right/bottom borders.
59 * In some cases, rectangles are assumed to obey to the
60 * following invariants:
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
71 * Computing the size of a rectangle can be done by simply
72 * subtracting the maximum X or Y coordinate from the minimum
75 typedef struct Rect { coord_t xmin, ymin, xmax, ymax; } Rect;
78 * Return the width of a rectangle in pixels.
80 * \note The argument \a r is evaluated twice.
82 #define RECT_WIDTH(r) ((r)->xmax - (r)->xmin)
85 * Return the height of a rectangle in pixels.
87 * \note The argument \a r is evaluated twice.
89 #define RECT_HEIGHT(r) ((r)->ymax - (r)->ymin)
95 * Control structure to draw in a bitmap
97 * \todo For better ortogonality, split this structure into
98 * an Image and a plain drawing context called Painter.
100 typedef struct Bitmap
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() */
107 #if CONFIG_GFX_CLIPPING || CONFIG_GFX_VCOORDS
108 Rect cr; /**< Clip drawing inside this rectangle */
112 const struct Font *font;/**< Current font for text rendering. */
115 * Algorithmic text style flags.
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.
124 #endif /* CONFIG_GFX_TEXT */
126 #if CONFIG_GFX_VCOORDS
128 * \name Logical coordinate system
132 vcoord_t scaleX, scaleY;
134 #endif /* CONFIG_GFX_VCOORDS */
141 * \todo Use this as Bitmap and change Bitmap to Drawable.
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. */
151 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
153 * Compute the size in bytes of a raster suitable for
154 * holding a bitmap of \a width x \a height pixels.
156 #define RAST_SIZE(width, height) ( ((width) + 7 / 8) * (height) )
158 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
160 * Compute the size in bytes of a raster suitable for
161 * holding a bitmap of \a width x \a height pixels.
163 #define RAST_SIZE(width, height) ( (width) * (((height) + 7) / 8) )
165 #error Unknown value of CONFIG_BITMAP_FMT
166 #endif /* CONFIG_BITMAP_FMT */
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);
184 #include <mware/pgm.h>
185 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster);
189 INLINE void gfx_setFont(Bitmap *bm, const struct Font *font)
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 */
204 #endif /* GFX_GFX_H */