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