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