Update preset.
[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 Bernie Innocenti <bernie@codewiz.org>
31  *
32  * -->
33  *
34  * \author Bernie Innocenti <bernie@codewiz.org>
35  * \author Stefano Fedrigo <aleph@develer.com>
36  *
37  * \brief Bitmap manipulation routines.
38  * \sa gfx.h
39  */
40
41 #include "gfx.h"
42 #include "gfx_p.h"
43
44 #include "cfg/cfg_gfx.h"  /* CONFIG_GFX_CLIPPING */
45 #include <cfg/macros.h>   /* MIN() */
46 #include <cfg/debug.h>    /* ASSERT() */
47
48 #include <cpu/attr.h>     /* CPU_HARVARD */
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  * \note This function bypasses the current clipping area.
96  */
97 void gfx_bitmapClear(Bitmap *bm)
98 {
99         memset(bm->raster, 0, RAST_SIZE(bm->width, bm->height));
100 }
101
102
103 #if CPU_HARVARD
104
105 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
106
107 /**
108  * Copy a raster picture located in program memory in the bitmap.
109  * The size of the raster to copy *must* be the same of the raster bitmap.
110  *
111  * \note This function does \b not update the current pen position
112  */
113 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
114 {
115         memcpy_P(bm->raster, raster, RAST_SIZE(bm->width, bm->height));
116 }
117 #endif /* CPU_HARVARD */
118
119 #if CONFIG_GFX_CLIPPING
120         /**
121          * Clip destination coordinates inside a clipping range.
122          *
123          * This macro helps a drawing operation to adjust its
124          * destination X and Y coordinates inside the destination
125          * clipping range.
126          *
127          * The source start coordinate is adjusted as well
128          * when destination start clipping occurs.
129          */
130         #define gfx_clip(dmin, dmax, smin, cmin, cmax) \
131                 do { \
132                         if ((dmin) < (cmin)) \
133                         { \
134                                 (smin) += (cmin) - (dmin); \
135                                 (dmin) = (cmin); \
136                         } \
137                         (dmax) = MIN((dmax), (cmax)); \
138                 } while(0)
139
140 #else /* !CONFIG_GFX_CLIPPING */
141
142         #define gfx_clip(dmin, dmax, smin, cmin, cmax) do { } while (0)
143
144 #endif /* !CONFIG_GFX_CLIPPING */
145
146
147 /**
148  * Copy a rectangular area of a bitmap on another bitmap.
149  *
150  * Blitting is a common copy operation involving two bitmaps.
151  * A rectangular area of the source bitmap is copied bit-wise
152  * to a different position in the destination bitmap.
153  *
154  * \note Using the same bitmap for \a src and \a dst is unsupported.
155  *
156  * \param dst  Bitmap where the operation writes.
157  * \param rect The (xmin;ymin) coordinates provide the top/left offset
158  *             for drawing in the destination bitmap.  If the source
159  *             bitmap is larger than the rectangle, drawing is clipped.
160  * \param src  Bitmap containing the source pixels.
161  * \param srcx Starting X offset in the source bitmap.
162  * \param srcy Starting Y offset in the source bitmap.
163  */
164 void gfx_blit(Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy)
165 {
166         coord_t dxmin, dymin, dxmax, dymax;
167         coord_t dx, dy, sx, sy;
168
169         /*
170          * Pre-clip coordinates inside src->width/height.
171          */
172         dxmin = rect->xmin;
173         dymin = rect->ymin;
174         dxmax = MIN(rect->xmax, rect->xmin + src->width);
175         dymax = MIN(rect->ymax, rect->ymin + src->height);
176
177         /* Perform regular clipping */
178         gfx_clip(dxmin, dxmax, srcx, dst->cr.xmin, dst->cr.xmax);
179         gfx_clip(dymin, dymax, srcy, 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 = srcx; dx < dxmax; ++dx, ++sx)
186                 for (dy = dymin, sy = srcy; dy < dymax; ++dy, ++sy)
187                         BM_DRAWPIXEL(dst, dx, dy, BM_READPIXEL(src, sx, sy));
188 }
189
190 /**
191  * Blit a raster to a Bitmap.
192  *
193  * \todo Merge this function into gfx_blit()
194  *
195  * \see gfx_blit()
196  */
197 void gfx_blitRaster(Bitmap *dst, coord_t dxmin, coord_t dymin,
198                 const uint8_t *raster, coord_t w, coord_t h, coord_t stride)
199 {
200         coord_t dxmax = dxmin + w, dymax = dymin + h;
201         coord_t sxmin = 0, symin = 0;
202         coord_t dx, dy, sx, sy;
203
204         /* Perform regular clipping */
205         gfx_clip(dxmin, dxmax, sxmin, dst->cr.xmin, dst->cr.xmax);
206         gfx_clip(dymin, dymax, symin, dst->cr.ymin, dst->cr.ymax);
207
208         //kprintf("dxmin=%d, sxmin=%d, dxmax=%d; ", dxmin, sxmin, dxmax);
209         //kprintf("dymin=%d, symin=%d, dymax=%d\n", dymin, symin, dymax);
210
211         /* TODO: make it not as dog slow as this */
212         for (dx = dxmin, sx = sxmin; dx < dxmax; ++dx, ++sx)
213                 for (dy = dymin, sy = symin; dy < dymax; ++dy, ++sy)
214                         BM_DRAWPIXEL(dst, dx, dy, RAST_READPIXEL(raster, sx, sy, stride));
215 }
216
217 /**
218  * Blit an Image to a Bitmap.
219  *
220  * \see gfx_blit()
221  */
222 void gfx_blitImage(Bitmap *dst, coord_t dxmin, coord_t dymin, const Image *image)
223 {
224         ASSERT(image);
225
226         gfx_blitRaster(dst, dxmin, dymin,
227                         image->raster, image->width, image->height, image->stride);
228 }
229
230
231 #if CONFIG_GFX_CLIPPING || CONFIG_GFX_VCOORDS
232
233 /**
234  * Set the bitmap clipping rectangle to the specified coordinates.
235  *
236  * All drawing performed on the bitmap will be clipped inside this
237  * rectangle.
238  *
239  * The clipping rectangle is also used as a bounding box for the
240  * logical view of the virtual coordinate system.
241  *
242  * \note Following the convention used for all other operations, the
243  *       top-left pixels of the rectangle are included, while the
244  *       bottom-right pixels are considered outside the clipping region.
245  *
246  * \see gfx_setViewRect
247  */
248 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
249 {
250         ASSERT(minx < maxx);
251         ASSERT(miny < maxy);
252         ASSERT(miny >= 0);
253         ASSERT(minx >= 0);
254         ASSERT(maxx <= bm->width);
255         ASSERT(maxy <= bm->height);
256
257         bm->cr.xmin = minx;
258         bm->cr.ymin = miny;
259         bm->cr.xmax = maxx;
260         bm->cr.ymax = maxy;
261
262 //      kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
263 //              bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);
264 }
265
266 #endif /* CONFIG_GFX_CLIPPING */
267