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