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/cfg_gfx.h" /* CONFIG_GFX_* */
19 #include <cfg/compiler.h>
21 #include <cpu/attr.h> /* CPU_HARVARD */
25 * \name Known pixel formats for bitmap representation.
28 #define BITMAP_FMT_PLANAR_H_MSB 1 /**< Planar pixels, horizontal bytes, MSB left. */
29 #define BITMAP_FMT_PLANAR_V_LSB 2 /**< Planar pixels, vertical bytes, LSB top. */
32 #if !defined(CONFIG_BITMAP_FMT) || (CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_H_MSB && CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_V_LSB)
33 #error CONFIG_BITMAP_FMT must be defined to either BITMAP_FMT_PLANAR_H_LSB or BITMAP_FMT_PLANAR_V_LSB
35 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
36 #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
38 #if !defined(CONFIG_GFX_TEXT) || (CONFIG_GFX_TEXT != 0 && CONFIG_GFX_TEXT != 1)
39 #error CONFIG_GFX_TEXT must be defined to either 0 or 1
44 /** Common type for coordinates expressed in pixel units */
46 typedef unsigned int ucoord_t;
48 #if CONFIG_GFX_VCOORDS
49 /** Common type for coordinates expressed in logical units */
50 typedef float vcoord_t;
51 #endif /* CONFIG_GFX_VCOORDS */
55 * Describe a rectangular area with coordinates expressed in pixels.
57 * The rectangle is represented in terms of its top/left and
58 * right/bottom borders.
60 * In some cases, rectangles are assumed to obey to the
61 * following invariants:
66 * Oddly, the xmin and ymin coordinates are inclusive, while the
67 * xmax and ymax coordinates are non-inclusive. This design
68 * decision makes several computations simpler and lets you
69 * specify empty (0x0) rectangles without breaking the
72 * Computing the size of a rectangle can be done by simply
73 * subtracting the maximum X or Y coordinate from the minimum
76 typedef struct Rect { coord_t xmin, ymin, xmax, ymax; } Rect;
79 * Return the width of a rectangle in pixels.
81 * \note The argument \a r is evaluated twice.
83 #define RECT_WIDTH(r) ((r)->xmax - (r)->xmin)
86 * Return the height of a rectangle in pixels.
88 * \note The argument \a r is evaluated twice.
90 #define RECT_HEIGHT(r) ((r)->ymax - (r)->ymin)
96 * Control structure to draw in a bitmap
98 * \todo For better ortogonality, split this structure into
99 * an Image and a plain drawing context called Painter.
101 typedef struct Bitmap
103 uint8_t *raster; /**< Pointer to byte array to hold the data */
104 coord_t width, height; /**< Width/Height in pixels */
105 coord_t stride; /**< Bytes per row. */
106 coord_t penX, penY; /**< Current pen position MoveTo()/LineTo() */
108 #if CONFIG_GFX_CLIPPING || CONFIG_GFX_VCOORDS
109 Rect cr; /**< Clip drawing inside this rectangle */
113 const struct Font *font;/**< Current font for text rendering. */
116 * Algorithmic text style flags.
118 * The text rendering routine can apply a few simple transformations
119 * to the current font in order to generate common styles such as
120 * bold, italic and underline from plain glyphs.
125 #endif /* CONFIG_GFX_TEXT */
127 #if CONFIG_GFX_VCOORDS
129 * \name Logical coordinate system
133 vcoord_t scaleX, scaleY;
135 #endif /* CONFIG_GFX_VCOORDS */
142 * \todo Use this as Bitmap and change Bitmap to Drawable.
146 const uint8_t *raster; /**< Pointer to byte array to hold the data. */
147 coord_t width; /**< Raster width in pixels. */
148 coord_t height; /**< Raster height in pixels. */
149 coord_t stride; /**< Bytes per row. */
152 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
154 * Compute the size in bytes of a raster suitable for
155 * holding a bitmap of \a width x \a height pixels.
157 #define RAST_SIZE(width, height) ( ((width) + 7 / 8) * (height) )
159 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
161 * Compute the size in bytes of a raster suitable for
162 * holding a bitmap of \a width x \a height pixels.
164 #define RAST_SIZE(width, height) ( (width) * (((height) + 7) / 8) )
166 #error Unknown value of CONFIG_BITMAP_FMT
167 #endif /* CONFIG_BITMAP_FMT */
169 /* Function prototypes */
170 void gfx_bitmapInit (Bitmap *bm, uint8_t *raster, coord_t w, coord_t h);
171 void gfx_bitmapClear(Bitmap *bm);
172 void gfx_blit (Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy);
173 void gfx_blitRaster (Bitmap *dst, coord_t dx, coord_t dy, const uint8_t *raster, coord_t w, coord_t h, coord_t stride);
174 void gfx_blitImage (Bitmap *dst, coord_t dx, coord_t dy, const Image *image);
175 void gfx_line (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
176 void gfx_rectDraw (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
177 void gfx_rectFillC (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color);
178 void gfx_rectFill (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
179 void gfx_rectClear (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
180 void gfx_moveTo (Bitmap *bm, coord_t x, coord_t y);
181 void gfx_lineTo (Bitmap *bm, coord_t x, coord_t y);
182 void gfx_setClipRect(Bitmap *bm, coord_t xmin, coord_t ymin, coord_t xmax, coord_t ymax);
185 #include <mware/pgm.h>
186 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster);
190 INLINE void gfx_setFont(Bitmap *bm, const struct Font *font)
196 #if CONFIG_GFX_VCOORDS
197 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
198 coord_t gfx_transformX(Bitmap *bm, vcoord_t x);
199 coord_t gfx_transformY(Bitmap *bm, vcoord_t y);
200 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
201 #endif /* CONFIG_GFX_VCOORDS */
205 #endif /* GFX_GFX_H */