gfx_setClipRect(): Also define when CONFIG_GFX_VCOORDS is enabled; Extend documentation.
[bertos.git] / gfx / bitmap.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  * \author Stefano Fedrigo <aleph@develer.com>
13  *
14  * \brief Bitmap manipulation routines.
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.12  2006/08/01 12:23:39  bernie
20  *#* gfx_setClipRect(): Also define when CONFIG_GFX_VCOORDS is enabled; Extend documentation.
21  *#*
22  *#* Revision 1.11  2006/07/19 12:56:26  bernie
23  *#* Convert to new Doxygen style.
24  *#*
25  *#* Revision 1.10  2006/05/27 17:21:15  bernie
26  *#* Factor out, simplify and document clipping.
27  *#*
28  *#* Revision 1.9  2006/05/25 23:35:40  bernie
29  *#* Cleanup.
30  *#*
31  *#* Revision 1.8  2006/03/27 04:48:56  bernie
32  *#* gfx_blitImage(): New function; gfx_blitRaster(): Fix clipping bug.
33  *#*
34  *#* Revision 1.7  2006/03/07 22:18:04  bernie
35  *#* Correctly compute text width for prop fonts; Make styles a per-bitmap attribute.
36  *#*
37  *#* Revision 1.6  2006/02/23 11:17:16  bernie
38  *#* Documentation fixes.
39  *#*
40  *#* Revision 1.5  2006/02/15 09:10:15  bernie
41  *#* Implement prop fonts; Fix algo styles.
42  *#*
43  *#* Revision 1.4  2006/02/10 12:32:33  bernie
44  *#* Add multiple font support in bitmaps; gfx_blitRaster(): New function.
45  *#*
46  *#* Revision 1.3  2006/01/26 00:36:48  bernie
47  *#* Const correctness for some new functions.
48  *#*
49  *#* Revision 1.2  2006/01/24 21:55:43  aleph
50  *#* gfx_blit_P(): use RASTER_SIZE() to calculate raster size
51  *#*
52  *#* Revision 1.1  2006/01/24 02:17:49  bernie
53  *#* Split out gfx.c into bitmap.c and line.c.
54  *#*
55  *#*/
56
57 #include "gfx.h"
58 #include "gfx_p.h"
59
60 #include <cfg/debug.h>  /* ASSERT() */
61 #include <cfg/cpu.h>    /* CPU_HARVARD */
62 #include <cfg/macros.h> /* MIN() */
63 #include <appconfig.h>  /* CONFIG_GFX_CLIPPING */
64
65 #include <string.h>     /* memset() */
66
67 #if CONFIG_GFX_TEXT
68 #include <gfx/font.h>   /* default_font */
69 #endif
70
71
72 /**
73  * Initialize a Bitmap structure with the provided parameters.
74  *
75  * \note The pen position is reset to the origin.
76  */
77 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
78 {
79         bm->raster = raster;
80         bm->width = w;
81         bm->height = h;
82         #if (CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB)
83                 bm->stride = (w + 7) / 8;
84         #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
85                 bm->stride = w;
86         #else
87                 #error Unknown value of CONFIG_BITMAP_FMT
88         #endif /* CONFIG_BITMAP_FMT */
89         bm->penX = 0;
90         bm->penY = 0;
91
92 #if CONFIG_GFX_TEXT
93         gfx_setFont(bm, &default_font);
94         bm->styles = 0;
95 #endif
96
97 #if CONFIG_GFX_CLIPPING
98         bm->cr.xmin = 0;
99         bm->cr.ymin = 0;
100         bm->cr.xmax = w;
101         bm->cr.ymax = h;
102 #endif /* CONFIG_GFX_CLIPPING */
103 }
104
105
106 /**
107  * Clear the whole bitmap surface to the background color.
108  *
109  * \note This function does \b not update the current pen position.
110  * \note This function bypasses the current clipping area.
111  */
112 void gfx_bitmapClear(Bitmap *bm)
113 {
114         memset(bm->raster, 0, RASTER_SIZE(bm->width, bm->height));
115 }
116
117
118 #if CPU_HARVARD
119
120 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
121
122 /**
123  * Copy a raster picture located in program memory in the bitmap.
124  * The size of the raster to copy *must* be the same of the raster bitmap.
125  *
126  * \note This function does \b not update the current pen position
127  */
128 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
129 {
130         memcpy_P(bm->raster, raster, RASTER_SIZE(bm->width, bm->height));
131 }
132 #endif /* CPU_HARVARD */
133
134 #if CONFIG_GFX_CLIPPING
135         /**
136          * Clip destination coordinates inside a clipping range.
137          *
138          * This macro helps a drawing operation to adjust its
139          * destination X and Y coordinates inside the destination
140          * clipping range.
141          *
142          * The source start coordinate is adjusted as well
143          * when destination start clipping occurs.
144          */
145         #define gfx_clip(dmin, dmax, smin, cmin, cmax) \
146                 do { \
147                         if ((dmin) < (cmin)) \
148                         { \
149                                 (smin) += (cmin) - (dmin); \
150                                 (dmin) = (cmin); \
151                         } \
152                         (dmax) = MIN((dmax), (cmax)); \
153                 } while(0)
154
155 #else /* !CONFIG_GFX_CLIPPING */
156
157         #define gfx_clip(dmin, dmax, smin, cmin, cmax) do { } while (0)
158
159 #endif /* !CONFIG_GFX_CLIPPING */
160
161
162 /**
163  * Copy a rectangular area of a bitmap on another bitmap.
164  *
165  * Blitting is a common copy operation involving two bitmaps.
166  * A rectangular area of the source bitmap is copied bit-wise
167  * to a different position in the destination bitmap.
168  *
169  * \note Using the same bitmap for \a src and \a dst is unsupported.
170  *
171  * \param dst  Bitmap where the operation writes.
172  * \param rect The (xmin;ymin) coordinates provide the top/left offset
173  *             for drawing in the destination bitmap.  If the source
174  *             bitmap is larger than the rectangle, drawing is clipped.
175  * \param src  Bitmap containing the source pixels.
176  * \param srcx Starting X offset in the source bitmap.
177  * \param srcy Starting Y offset in the source bitmap.
178  */
179 void gfx_blit(Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy)
180 {
181         coord_t dxmin, dymin, dxmax, dymax;
182         coord_t dx, dy, sx, sy;
183
184         /*
185          * Pre-clip coordinates inside src->width/height.
186          */
187         dxmin = rect->xmin;
188         dymin = rect->ymin;
189         dxmax = MIN(rect->xmax, rect->xmin + src->width);
190         dymax = MIN(rect->ymax, rect->ymin + src->height);
191
192         /* Perform regular clipping */
193         gfx_clip(dxmin, dxmax, srcx, dst->cr.xmin, dst->cr.xmax);
194         gfx_clip(dymin, dymax, srcy, dst->cr.ymin, dst->cr.ymax);
195
196         //kprintf("dxmin=%d, sxmin=%d, dxmax=%d; ", dxmin, sxmin, dxmax);
197         //kprintf("dymin=%d, symin=%d, dymax=%d\n", dymin, symin, dymax);
198
199         /* TODO: make it not as dog slow as this */
200         for (dx = dxmin, sx = srcx; dx < dxmax; ++dx, ++sx)
201                 for (dy = dymin, sy = srcy; dy < dymax; ++dy, ++sy)
202                         BM_DRAWPIXEL(dst, dx, dy, BM_READPIXEL(src, sx, sy));
203 }
204
205 /**
206  * Blit a raster to a Bitmap.
207  *
208  * \todo Merge this function into gfx_blit()
209  *
210  * \see gfx_blit()
211  */
212 void gfx_blitRaster(Bitmap *dst, coord_t dxmin, coord_t dymin,
213                 const uint8_t *raster, coord_t w, coord_t h, coord_t stride)
214 {
215         coord_t dxmax = dxmin + w, dymax = dymin + h;
216         coord_t sxmin = 0, symin = 0;
217         coord_t dx, dy, sx, sy;
218
219         /* Perform regular clipping */
220         gfx_clip(dxmin, dxmax, sxmin, dst->cr.xmin, dst->cr.xmax);
221         gfx_clip(dymin, dymax, symin, dst->cr.ymin, dst->cr.ymax);
222
223         //kprintf("dxmin=%d, sxmin=%d, dxmax=%d; ", dxmin, sxmin, dxmax);
224         //kprintf("dymin=%d, symin=%d, dymax=%d\n", dymin, symin, dymax);
225
226         /* TODO: make it not as dog slow as this */
227         for (dx = dxmin, sx = sxmin; dx < dxmax; ++dx, ++sx)
228                 for (dy = dymin, sy = symin; dy < dymax; ++dy, ++sy)
229                         BM_DRAWPIXEL(dst, dx, dy, RAST_READPIXEL(raster, sx, sy, stride));
230 }
231
232 /**
233  * Blit an Image to a Bitmap.
234  *
235  * \see gfx_blit()
236  */
237 void gfx_blitImage(Bitmap *dst, coord_t dxmin, coord_t dymin, const Image *image)
238 {
239         ASSERT(image);
240
241         gfx_blitRaster(dst, dxmin, dymin,
242                         image->raster, image->width, image->height, image->stride);
243 }
244
245
246 #if CONFIG_GFX_CLIPPING || CONFIG_GFX_VCOORDS
247
248 /**
249  * Set the bitmap clipping rectangle to the specified coordinates.
250  *
251  * All drawing performed on the bitmap will be clipped inside this
252  * rectangle.
253  *
254  * The clipping rectangle is also used as a bounding box for the
255  * logical view of the virtual coordinate system.
256  *
257  * \note Following the convention used for all other operations, the
258  *       top-left pixels of the rectangle are included, while the
259  *       bottom-right pixels are considered outside the clipping region.
260  *
261  * \see gfx_setViewRect
262  */
263 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
264 {
265         ASSERT(minx < maxx);
266         ASSERT(miny < maxy);
267         ASSERT(miny >= 0);
268         ASSERT(minx >= 0);
269         ASSERT(maxx <= bm->width);
270         ASSERT(maxy <= bm->height);
271
272         bm->cr.xmin = minx;
273         bm->cr.ymin = miny;
274         bm->cr.xmax = maxx;
275         bm->cr.ymax = maxy;
276
277 //      kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
278 //              bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);
279 }
280
281 #endif /* CONFIG_GFX_CLIPPING */
282