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.
11 * \author Bernardo Innocenti <bernie@develer.com>
12 * \author Stefano Fedrigo <aleph@develer.com>
14 * \brief Bitmap manipulation routines.
19 *#* Revision 1.4 2006/02/10 12:32:33 bernie
20 *#* Add multiple font support in bitmaps; gfx_blitRaster(): New function.
22 *#* Revision 1.3 2006/01/26 00:36:48 bernie
23 *#* Const correctness for some new functions.
25 *#* Revision 1.2 2006/01/24 21:55:43 aleph
26 *#* gfx_blit_P(): use RASTER_SIZE() to calculate raster size
28 *#* Revision 1.1 2006/01/24 02:17:49 bernie
29 *#* Split out gfx.c into bitmap.c and line.c.
36 #include <cfg/debug.h> /* ASSERT() */
37 #include <cfg/cpu.h> /* CPU_HARVARD */
38 #include <cfg/macros.h> /* MIN() */
40 #include <string.h> /* memset() */
43 #include <gfx/font.h> /* default_font */
48 * Initialize a Bitmap structure with the provided parameters.
50 * \note The pen position is reset to the origin.
52 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
57 #if (CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB)
58 bm->stride = (w + 7) / 8;
59 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
62 #error Unknown value of CONFIG_BITMAP_FMT
63 #endif /* CONFIG_BITMAP_FMT */
68 gfx_setFont(bm, &default_font);
71 #if CONFIG_GFX_CLIPPING
76 #endif /* CONFIG_GFX_CLIPPING */
81 * Clear the whole bitmap surface to the background color.
83 * \note This function does \b not update the current pen position.
85 void gfx_bitmapClear(Bitmap *bm)
87 memset(bm->raster, 0, RASTER_SIZE(bm->width, bm->height));
93 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
96 * Copy a raster picture located in program memory in the bitmap.
97 * The size of the raster to copy *must* be the same of the raster bitmap.
99 * \note This function does \b not update the current pen position
101 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
103 memcpy_P(bm->raster, raster, RASTER_SIZE(bm->width, bm->height));
105 #endif /* CPU_HARVARD */
108 * Copy a rectangular area of a bitmap on another bitmap.
110 * Blitting is a common copy operation involving two bitmaps.
111 * A rectangular area of the source bitmap is copied bit-wise
112 * to a different position in the destination bitmap.
114 * \note Using the same bitmap for \a src and \a dst is unsupported.
116 * \param dst Bitmap where the operation writes
119 void gfx_blit(Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy)
121 coord_t dxmin, dymin, dxmax, dymax;
122 coord_t dx, dy, sx, sy;
125 * Clip coordinates inside dst->cr and src->width/height.
128 if (dxmin < dst->cr.xmin)
130 srcx += dst->cr.xmin - dxmin;
131 dxmin = dst->cr.xmin;
134 if (dymin < dst->cr.ymin)
136 srcy += dst->cr.ymin - dymin;
137 dymin = dst->cr.ymin;
139 dxmax = MIN(MIN(rect->xmax, rect->xmin + src->width), dst->cr.xmax);
140 dymax = MIN(MIN(rect->ymax, rect->ymin + src->height), dst->cr.ymax);
142 /* TODO: make it not as dog slow as this */
143 for (dx = dxmin, sx = srcx; dx < dxmax; ++dx, ++sx)
144 for (dy = dymin, sy = srcy; dy < dymax; ++dy, ++sy)
145 BM_DRAWPIXEL(dst, dx, dy, BM_READPIXEL(src, sx, sy));
149 void gfx_blitRaster(Bitmap *dst, coord_t dxmin, coord_t dymin, const uint8_t *raster, coord_t w, coord_t h)
151 coord_t dxmax, dymax;
152 coord_t sxmin = 0, symin = 0;
153 coord_t dx, dy, sx, sy;
156 * Clip coordinates inside dst->cr.
158 if (dx < dst->cr.xmin)
160 sxmin += dst->cr.xmin - dxmin;
161 dxmin = dst->cr.xmin;
163 if (dy < dst->cr.ymin)
165 symin += dst->cr.ymin - dymin;
166 dymin = dst->cr.ymin;
168 dxmax = MIN(dxmin + w, dst->cr.xmax);
169 dymax = MIN(dymin + h, dst->cr.ymax);
171 /* TODO: make it not as dog slow as this */
172 for (dx = dxmin, sx = sxmin; dx < dxmax; ++dx, ++sx)
173 for (dy = dymin, sy = symin; dy < dymax; ++dy, ++sy)
174 BM_DRAWPIXEL(dst, dx, dy,
175 (raster[sy * ((w + 7) / 8) + sx / 8] & (1 << (7 - sx % 8))) ? 1 : 0
181 * Set the bitmap clipping rectangle to the specified coordinates.
183 * All drawing performed on the bitmap will be clipped inside this
186 * \note Following the convention used in all other operations, the
187 * top-left pixels of the rectangle are included, while the
188 * bottom-right pixels are considered outside the clipping region.
190 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
196 ASSERT(maxx <= bm->width);
197 ASSERT(maxy <= bm->height);
204 /* kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
205 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);