Refactor BeRTOS to be in his own directory.
[bertos.git] / bertos / gfx / bitmap.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
31  *
32  * -->
33  *
34  * \author Bernardo Innocenti <bernie@develer.com>
35  * \author Stefano Fedrigo <aleph@develer.com>
36  *
37  * \brief Bitmap manipulation routines.
38  */
39
40 #include "gfx.h"
41 #include "gfx_p.h"
42
43 #include <cfg/debug.h>  /* ASSERT() */
44 #include <cpu/attr.h>   /* CPU_HARVARD */
45 #include <cfg/macros.h> /* MIN() */
46 #include <appconfig.h>  /* CONFIG_GFX_CLIPPING */
47
48 #include <string.h>     /* memset() */
49
50 #if CONFIG_GFX_TEXT
51 #include <gfx/font.h>   /* default_font */
52 #endif
53
54
55 /**
56  * Initialize a Bitmap structure with the provided parameters.
57  *
58  * \note The pen position is reset to the origin.
59  */
60 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
61 {
62         bm->raster = raster;
63         bm->width = w;
64         bm->height = h;
65         #if (CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB)
66                 bm->stride = (w + 7) / 8;
67         #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
68                 bm->stride = w;
69         #else
70                 #error Unknown value of CONFIG_BITMAP_FMT
71         #endif /* CONFIG_BITMAP_FMT */
72         bm->penX = 0;
73         bm->penY = 0;
74
75 #if CONFIG_GFX_TEXT
76         gfx_setFont(bm, &default_font);
77         bm->styles = 0;
78 #endif
79
80 #if CONFIG_GFX_CLIPPING
81         bm->cr.xmin = 0;
82         bm->cr.ymin = 0;
83         bm->cr.xmax = w;
84         bm->cr.ymax = h;
85 #endif /* CONFIG_GFX_CLIPPING */
86 }
87
88
89 /**
90  * Clear the whole bitmap surface to the background color.
91  *
92  * \note This function does \b not update the current pen position.
93  * \note This function bypasses the current clipping area.
94  */
95 void gfx_bitmapClear(Bitmap *bm)
96 {
97         memset(bm->raster, 0, RAST_SIZE(bm->width, bm->height));
98 }
99
100
101 #if CPU_HARVARD
102
103 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
104
105 /**
106  * Copy a raster picture located in program memory in the bitmap.
107  * The size of the raster to copy *must* be the same of the raster bitmap.
108  *
109  * \note This function does \b not update the current pen position
110  */
111 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
112 {
113         memcpy_P(bm->raster, raster, RAST_SIZE(bm->width, bm->height));
114 }
115 #endif /* CPU_HARVARD */
116
117 #if CONFIG_GFX_CLIPPING
118         /**
119          * Clip destination coordinates inside a clipping range.
120          *
121          * This macro helps a drawing operation to adjust its
122          * destination X and Y coordinates inside the destination
123          * clipping range.
124          *
125          * The source start coordinate is adjusted as well
126          * when destination start clipping occurs.
127          */
128         #define gfx_clip(dmin, dmax, smin, cmin, cmax) \
129                 do { \
130                         if ((dmin) < (cmin)) \
131                         { \
132                                 (smin) += (cmin) - (dmin); \
133                                 (dmin) = (cmin); \
134                         } \
135                         (dmax) = MIN((dmax), (cmax)); \
136                 } while(0)
137
138 #else /* !CONFIG_GFX_CLIPPING */
139
140         #define gfx_clip(dmin, dmax, smin, cmin, cmax) do { } while (0)
141
142 #endif /* !CONFIG_GFX_CLIPPING */
143
144
145 /**
146  * Copy a rectangular area of a bitmap on another bitmap.
147  *
148  * Blitting is a common copy operation involving two bitmaps.
149  * A rectangular area of the source bitmap is copied bit-wise
150  * to a different position in the destination bitmap.
151  *
152  * \note Using the same bitmap for \a src and \a dst is unsupported.
153  *
154  * \param dst  Bitmap where the operation writes.
155  * \param rect The (xmin;ymin) coordinates provide the top/left offset
156  *             for drawing in the destination bitmap.  If the source
157  *             bitmap is larger than the rectangle, drawing is clipped.
158  * \param src  Bitmap containing the source pixels.
159  * \param srcx Starting X offset in the source bitmap.
160  * \param srcy Starting Y offset in the source bitmap.
161  */
162 void gfx_blit(Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy)
163 {
164         coord_t dxmin, dymin, dxmax, dymax;
165         coord_t dx, dy, sx, sy;
166
167         /*
168          * Pre-clip coordinates inside src->width/height.
169          */
170         dxmin = rect->xmin;
171         dymin = rect->ymin;
172         dxmax = MIN(rect->xmax, rect->xmin + src->width);
173         dymax = MIN(rect->ymax, rect->ymin + src->height);
174
175         /* Perform regular clipping */
176         gfx_clip(dxmin, dxmax, srcx, dst->cr.xmin, dst->cr.xmax);
177         gfx_clip(dymin, dymax, srcy, dst->cr.ymin, dst->cr.ymax);
178
179         //kprintf("dxmin=%d, sxmin=%d, dxmax=%d; ", dxmin, sxmin, dxmax);
180         //kprintf("dymin=%d, symin=%d, dymax=%d\n", dymin, symin, dymax);
181
182         /* TODO: make it not as dog slow as this */
183         for (dx = dxmin, sx = srcx; dx < dxmax; ++dx, ++sx)
184                 for (dy = dymin, sy = srcy; dy < dymax; ++dy, ++sy)
185                         BM_DRAWPIXEL(dst, dx, dy, BM_READPIXEL(src, sx, sy));
186 }
187
188 /**
189  * Blit a raster to a Bitmap.
190  *
191  * \todo Merge this function into gfx_blit()
192  *
193  * \see gfx_blit()
194  */
195 void gfx_blitRaster(Bitmap *dst, coord_t dxmin, coord_t dymin,
196                 const uint8_t *raster, coord_t w, coord_t h, coord_t stride)
197 {
198         coord_t dxmax = dxmin + w, dymax = dymin + h;
199         coord_t sxmin = 0, symin = 0;
200         coord_t dx, dy, sx, sy;
201
202         /* Perform regular clipping */
203         gfx_clip(dxmin, dxmax, sxmin, dst->cr.xmin, dst->cr.xmax);
204         gfx_clip(dymin, dymax, symin, dst->cr.ymin, dst->cr.ymax);
205
206         //kprintf("dxmin=%d, sxmin=%d, dxmax=%d; ", dxmin, sxmin, dxmax);
207         //kprintf("dymin=%d, symin=%d, dymax=%d\n", dymin, symin, dymax);
208
209         /* TODO: make it not as dog slow as this */
210         for (dx = dxmin, sx = sxmin; dx < dxmax; ++dx, ++sx)
211                 for (dy = dymin, sy = symin; dy < dymax; ++dy, ++sy)
212                         BM_DRAWPIXEL(dst, dx, dy, RAST_READPIXEL(raster, sx, sy, stride));
213 }
214
215 /**
216  * Blit an Image to a Bitmap.
217  *
218  * \see gfx_blit()
219  */
220 void gfx_blitImage(Bitmap *dst, coord_t dxmin, coord_t dymin, const Image *image)
221 {
222         ASSERT(image);
223
224         gfx_blitRaster(dst, dxmin, dymin,
225                         image->raster, image->width, image->height, image->stride);
226 }
227
228
229 #if CONFIG_GFX_CLIPPING || CONFIG_GFX_VCOORDS
230
231 /**
232  * Set the bitmap clipping rectangle to the specified coordinates.
233  *
234  * All drawing performed on the bitmap will be clipped inside this
235  * rectangle.
236  *
237  * The clipping rectangle is also used as a bounding box for the
238  * logical view of the virtual coordinate system.
239  *
240  * \note Following the convention used for all other operations, the
241  *       top-left pixels of the rectangle are included, while the
242  *       bottom-right pixels are considered outside the clipping region.
243  *
244  * \see gfx_setViewRect
245  */
246 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
247 {
248         ASSERT(minx < maxx);
249         ASSERT(miny < maxy);
250         ASSERT(miny >= 0);
251         ASSERT(minx >= 0);
252         ASSERT(maxx <= bm->width);
253         ASSERT(maxy <= bm->height);
254
255         bm->cr.xmin = minx;
256         bm->cr.ymin = miny;
257         bm->cr.xmax = maxx;
258         bm->cr.ymax = maxy;
259
260 //      kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
261 //              bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);
262 }
263
264 #endif /* CONFIG_GFX_CLIPPING */
265