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