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