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