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