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