4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
30 * Copyright 1999 Bernie Innocenti <bernie@codewiz.org>
33 * \defgroup gfx Graphics routines
36 * \author Bernie Innocenti <bernie@codewiz.org>
37 * \author Stefano Fedrigo <aleph@develer.com>
39 * \brief General pourpose graphics routines
41 * $WIZ$ module_name = "gfx"
42 * $WIZ$ module_configuration = "bertos/cfg/cfg_gfx.h"
43 * $WIZ$ module_depends = "bitmap", "line", "luBS14"
49 #include "cfg/cfg_gfx.h" /* CONFIG_GFX_* */
50 #include <cfg/compiler.h>
52 #include <cpu/attr.h> /* CPU_HARVARD */
55 #define CONFIG_CHART_TYPE_X uint8_t ///< Type for the chart dataset
56 #define CONFIG_CHART_TYPE_Y uint8_t ///< Type for the chart dataset
59 * \name Known pixel formats for bitmap representation.
61 * $WIZ$ bitmap_format = "BITMAP_FMT_PLANAR_H_MSB", "BITMAP_FMT_PLANAR_V_LSB"
63 #define BITMAP_FMT_PLANAR_H_MSB 1 /**< Planar pixels, horizontal bytes, MSB left. */
64 #define BITMAP_FMT_PLANAR_V_LSB 2 /**< Planar pixels, vertical bytes, LSB top. */
67 #if !defined(CONFIG_BITMAP_FMT) || (CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_H_MSB && CONFIG_BITMAP_FMT != BITMAP_FMT_PLANAR_V_LSB)
68 #error CONFIG_BITMAP_FMT must be defined to either BITMAP_FMT_PLANAR_H_LSB or BITMAP_FMT_PLANAR_V_LSB
70 #if !defined(CONFIG_GFX_CLIPPING) || (CONFIG_GFX_CLIPPING != 0 && CONFIG_GFX_CLIPPING != 1)
71 #error CONFIG_GFX_CLIPPING must be defined to either 0 or 1
73 #if !defined(CONFIG_GFX_TEXT) || (CONFIG_GFX_TEXT != 0 && CONFIG_GFX_TEXT != 1)
74 #error CONFIG_GFX_TEXT must be defined to either 0 or 1
79 /** Common type for coordinates expressed in pixel units */
81 typedef unsigned int ucoord_t;
83 #if CONFIG_GFX_VCOORDS
84 /** Common type for coordinates expressed in logical units */
85 typedef float vcoord_t;
86 #endif /* CONFIG_GFX_VCOORDS */
90 * Describe a rectangular area with coordinates expressed in pixels.
92 * The rectangle is represented in terms of its top/left and
93 * right/bottom borders.
95 * In some cases, rectangles are assumed to obey to the
96 * following invariants:
101 * Oddly, the xmin and ymin coordinates are inclusive, while the
102 * xmax and ymax coordinates are non-inclusive. This design
103 * decision makes several computations simpler and lets you
104 * specify empty (0x0) rectangles without breaking the
107 * Computing the size of a rectangle can be done by simply
108 * subtracting the maximum X or Y coordinate from the minimum
111 typedef struct Rect { coord_t xmin, ymin, xmax, ymax; } Rect;
114 * Return the width of a rectangle in pixels.
116 * \note The argument \a r is evaluated twice.
118 #define RECT_WIDTH(r) ((r)->xmax - (r)->xmin)
121 * Return the height of a rectangle in pixels.
123 * \note The argument \a r is evaluated twice.
125 #define RECT_HEIGHT(r) ((r)->ymax - (r)->ymin)
131 * Control structure to draw in a bitmap
133 * \todo For better ortogonality, split this structure into
134 * an Image and a plain drawing context called Painter.
136 typedef struct Bitmap
138 uint8_t *raster; /**< Pointer to byte array to hold the data */
139 coord_t width, height; /**< Width/Height in pixels */
140 coord_t stride; /**< Bytes per row. */
141 coord_t penX, penY; /**< Current pen position MoveTo()/LineTo() */
143 #if CONFIG_GFX_CLIPPING || CONFIG_GFX_VCOORDS
144 Rect cr; /**< Clip drawing inside this rectangle */
148 const struct Font *font;/**< Current font for text rendering. */
151 * Algorithmic text style flags.
153 * The text rendering routine can apply a few simple transformations
154 * to the current font in order to generate common styles such as
155 * bold, italic and underline from plain glyphs.
160 #endif /* CONFIG_GFX_TEXT */
162 #if CONFIG_GFX_VCOORDS
164 * \name Logical coordinate system
168 vcoord_t scaleX, scaleY;
170 #endif /* CONFIG_GFX_VCOORDS */
177 * \todo Use this as Bitmap and change Bitmap to Drawable.
181 const uint8_t *raster; /**< Pointer to byte array to hold the data. */
182 coord_t width; /**< Raster width in pixels. */
183 coord_t height; /**< Raster height in pixels. */
184 coord_t stride; /**< Bytes per row. */
187 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
189 * Compute the size in bytes of a raster suitable for
190 * holding a bitmap of \a width x \a height pixels.
192 #define RAST_SIZE(width, height) ( (((width) + 7) / 8) * (height) )
194 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
196 * Compute the size in bytes of a raster suitable for
197 * holding a bitmap of \a width x \a height pixels.
199 #define RAST_SIZE(width, height) ( (width) * (((height) + 7) / 8) )
201 #error Unknown value of CONFIG_BITMAP_FMT
202 #endif /* CONFIG_BITMAP_FMT */
204 /* Function prototypes */
205 void gfx_bitmapInit (Bitmap *bm, uint8_t *raster, coord_t w, coord_t h);
206 void gfx_bitmapClear(Bitmap *bm);
207 void gfx_blit (Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy);
208 void gfx_blitRaster (Bitmap *dst, coord_t dx, coord_t dy, const uint8_t *raster, coord_t w, coord_t h, coord_t stride);
209 void gfx_blitImage (Bitmap *dst, coord_t dx, coord_t dy, const Image *image);
210 void gfx_line (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
211 void gfx_rectDraw (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
212 void gfx_rectFillC (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color);
213 void gfx_rectFill (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
214 void gfx_rectClear (Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2);
215 void gfx_moveTo (Bitmap *bm, coord_t x, coord_t y);
216 void gfx_lineTo (Bitmap *bm, coord_t x, coord_t y);
217 void gfx_setClipRect(Bitmap *bm, coord_t xmin, coord_t ymin, coord_t xmax, coord_t ymax);
221 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster);
225 INLINE void gfx_setFont(Bitmap *bm, const struct Font *font)
231 #if CONFIG_GFX_VCOORDS
232 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
233 coord_t gfx_transformX(Bitmap *bm, vcoord_t x);
234 coord_t gfx_transformY(Bitmap *bm, vcoord_t y);
235 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2);
236 #endif /* CONFIG_GFX_VCOORDS */
240 /** \} */ //defgroup gfx
241 #endif /* GFX_GFX_H */