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