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.11 2006/07/19 12:56:26 bernie
20 *#* Convert to new Doxygen style.
22 *#* Revision 1.10 2006/05/27 17:21:15 bernie
23 *#* Factor out, simplify and document clipping.
25 *#* Revision 1.9 2006/05/25 23:35:40 bernie
28 *#* Revision 1.8 2006/03/27 04:48:56 bernie
29 *#* gfx_blitImage(): New function; gfx_blitRaster(): Fix clipping bug.
31 *#* Revision 1.7 2006/03/07 22:18:04 bernie
32 *#* Correctly compute text width for prop fonts; Make styles a per-bitmap attribute.
34 *#* Revision 1.6 2006/02/23 11:17:16 bernie
35 *#* Documentation fixes.
37 *#* Revision 1.5 2006/02/15 09:10:15 bernie
38 *#* Implement prop fonts; Fix algo styles.
40 *#* Revision 1.4 2006/02/10 12:32:33 bernie
41 *#* Add multiple font support in bitmaps; gfx_blitRaster(): New function.
43 *#* Revision 1.3 2006/01/26 00:36:48 bernie
44 *#* Const correctness for some new functions.
46 *#* Revision 1.2 2006/01/24 21:55:43 aleph
47 *#* gfx_blit_P(): use RASTER_SIZE() to calculate raster size
49 *#* Revision 1.1 2006/01/24 02:17:49 bernie
50 *#* Split out gfx.c into bitmap.c and line.c.
57 #include <cfg/debug.h> /* ASSERT() */
58 #include <cfg/cpu.h> /* CPU_HARVARD */
59 #include <cfg/macros.h> /* MIN() */
60 #include <appconfig.h> /* CONFIG_GFX_CLIPPING */
62 #include <string.h> /* memset() */
65 #include <gfx/font.h> /* default_font */
70 * Initialize a Bitmap structure with the provided parameters.
72 * \note The pen position is reset to the origin.
74 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
79 #if (CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB)
80 bm->stride = (w + 7) / 8;
81 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
84 #error Unknown value of CONFIG_BITMAP_FMT
85 #endif /* CONFIG_BITMAP_FMT */
90 gfx_setFont(bm, &default_font);
94 #if CONFIG_GFX_CLIPPING
99 #endif /* CONFIG_GFX_CLIPPING */
104 * Clear the whole bitmap surface to the background color.
106 * \note This function does \b not update the current pen position.
107 * \note This function bypasses the current clipping area.
109 void gfx_bitmapClear(Bitmap *bm)
111 memset(bm->raster, 0, RASTER_SIZE(bm->width, bm->height));
117 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
120 * Copy a raster picture located in program memory in the bitmap.
121 * The size of the raster to copy *must* be the same of the raster bitmap.
123 * \note This function does \b not update the current pen position
125 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
127 memcpy_P(bm->raster, raster, RASTER_SIZE(bm->width, bm->height));
129 #endif /* CPU_HARVARD */
131 #if CONFIG_GFX_CLIPPING
133 * Clip destination coordinates inside a clipping range.
135 * This macro helps a drawing operation to adjust its
136 * destination X and Y coordinates inside the destination
139 * The source start coordinate is adjusted as well
140 * when destination start clipping occurs.
142 #define gfx_clip(dmin, dmax, smin, cmin, cmax) \
144 if ((dmin) < (cmin)) \
146 (smin) += (cmin) - (dmin); \
149 (dmax) = MIN((dmax), (cmax)); \
152 #else /* !CONFIG_GFX_CLIPPING */
154 #define gfx_clip(dmin, dmax, smin, cmin, cmax) do { } while (0)
156 #endif /* !CONFIG_GFX_CLIPPING */
160 * Copy a rectangular area of a bitmap on another bitmap.
162 * Blitting is a common copy operation involving two bitmaps.
163 * A rectangular area of the source bitmap is copied bit-wise
164 * to a different position in the destination bitmap.
166 * \note Using the same bitmap for \a src and \a dst is unsupported.
168 * \param dst Bitmap where the operation writes.
169 * \param rect The (xmin;ymin) coordinates provide the top/left offset
170 * for drawing in the destination bitmap. If the source
171 * bitmap is larger than the rectangle, drawing is clipped.
172 * \param src Bitmap containing the source pixels.
173 * \param srcx Starting X offset in the source bitmap.
174 * \param srcy Starting Y offset in the source bitmap.
176 void gfx_blit(Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy)
178 coord_t dxmin, dymin, dxmax, dymax;
179 coord_t dx, dy, sx, sy;
182 * Pre-clip coordinates inside src->width/height.
186 dxmax = MIN(rect->xmax, rect->xmin + src->width);
187 dymax = MIN(rect->ymax, rect->ymin + src->height);
189 /* Perform regular clipping */
190 gfx_clip(dxmin, dxmax, srcx, dst->cr.xmin, dst->cr.xmax);
191 gfx_clip(dymin, dymax, srcy, dst->cr.ymin, dst->cr.ymax);
193 //kprintf("dxmin=%d, sxmin=%d, dxmax=%d; ", dxmin, sxmin, dxmax);
194 //kprintf("dymin=%d, symin=%d, dymax=%d\n", dymin, symin, dymax);
196 /* TODO: make it not as dog slow as this */
197 for (dx = dxmin, sx = srcx; dx < dxmax; ++dx, ++sx)
198 for (dy = dymin, sy = srcy; dy < dymax; ++dy, ++sy)
199 BM_DRAWPIXEL(dst, dx, dy, BM_READPIXEL(src, sx, sy));
203 * Blit a raster to a Bitmap.
205 * \todo Merge this function into gfx_blit()
209 void gfx_blitRaster(Bitmap *dst, coord_t dxmin, coord_t dymin,
210 const uint8_t *raster, coord_t w, coord_t h, coord_t stride)
212 coord_t dxmax = dxmin + w, dymax = dymin + h;
213 coord_t sxmin = 0, symin = 0;
214 coord_t dx, dy, sx, sy;
216 /* Perform regular clipping */
217 gfx_clip(dxmin, dxmax, sxmin, dst->cr.xmin, dst->cr.xmax);
218 gfx_clip(dymin, dymax, symin, dst->cr.ymin, dst->cr.ymax);
220 //kprintf("dxmin=%d, sxmin=%d, dxmax=%d; ", dxmin, sxmin, dxmax);
221 //kprintf("dymin=%d, symin=%d, dymax=%d\n", dymin, symin, dymax);
223 /* TODO: make it not as dog slow as this */
224 for (dx = dxmin, sx = sxmin; dx < dxmax; ++dx, ++sx)
225 for (dy = dymin, sy = symin; dy < dymax; ++dy, ++sy)
226 BM_DRAWPIXEL(dst, dx, dy, RAST_READPIXEL(raster, sx, sy, stride));
230 * Blit an Image to a Bitmap.
234 void gfx_blitImage(Bitmap *dst, coord_t dxmin, coord_t dymin, const Image *image)
238 gfx_blitRaster(dst, dxmin, dymin,
239 image->raster, image->width, image->height, image->stride);
243 #if CONFIG_GFX_CLIPPING
246 * Set the bitmap clipping rectangle to the specified coordinates.
248 * All drawing performed on the bitmap will be clipped inside this
251 * \note Following the convention used for all other operations, the
252 * top-left pixels of the rectangle are included, while the
253 * bottom-right pixels are considered outside the clipping region.
255 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
261 ASSERT(maxx <= bm->width);
262 ASSERT(maxy <= bm->height);
269 // kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
270 // bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);
273 #endif /* CONFIG_GFX_CLIPPING */