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