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