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.
9 * \author Bernardo Innocenti <bernie@develer.com>
10 * \author Stefano Fedrigo <aleph@develer.com>
12 * \brief Bitmap manipulation routines.
18 #include <cfg/debug.h> /* ASSERT() */
19 #include <cfg/cpu.h> /* CPU_HARVARD */
20 #include <cfg/macros.h> /* MIN() */
21 #include <appconfig.h> /* CONFIG_GFX_CLIPPING */
23 #include <string.h> /* memset() */
26 #include <gfx/font.h> /* default_font */
31 * Initialize a Bitmap structure with the provided parameters.
33 * \note The pen position is reset to the origin.
35 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
40 #if (CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB)
41 bm->stride = (w + 7) / 8;
42 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
45 #error Unknown value of CONFIG_BITMAP_FMT
46 #endif /* CONFIG_BITMAP_FMT */
51 gfx_setFont(bm, &default_font);
55 #if CONFIG_GFX_CLIPPING
60 #endif /* CONFIG_GFX_CLIPPING */
65 * Clear the whole bitmap surface to the background color.
67 * \note This function does \b not update the current pen position.
68 * \note This function bypasses the current clipping area.
70 void gfx_bitmapClear(Bitmap *bm)
72 memset(bm->raster, 0, RAST_SIZE(bm->width, bm->height));
78 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
81 * Copy a raster picture located in program memory in the bitmap.
82 * The size of the raster to copy *must* be the same of the raster bitmap.
84 * \note This function does \b not update the current pen position
86 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
88 memcpy_P(bm->raster, raster, RAST_SIZE(bm->width, bm->height));
90 #endif /* CPU_HARVARD */
92 #if CONFIG_GFX_CLIPPING
94 * Clip destination coordinates inside a clipping range.
96 * This macro helps a drawing operation to adjust its
97 * destination X and Y coordinates inside the destination
100 * The source start coordinate is adjusted as well
101 * when destination start clipping occurs.
103 #define gfx_clip(dmin, dmax, smin, cmin, cmax) \
105 if ((dmin) < (cmin)) \
107 (smin) += (cmin) - (dmin); \
110 (dmax) = MIN((dmax), (cmax)); \
113 #else /* !CONFIG_GFX_CLIPPING */
115 #define gfx_clip(dmin, dmax, smin, cmin, cmax) do { } while (0)
117 #endif /* !CONFIG_GFX_CLIPPING */
121 * Copy a rectangular area of a bitmap on another bitmap.
123 * Blitting is a common copy operation involving two bitmaps.
124 * A rectangular area of the source bitmap is copied bit-wise
125 * to a different position in the destination bitmap.
127 * \note Using the same bitmap for \a src and \a dst is unsupported.
129 * \param dst Bitmap where the operation writes.
130 * \param rect The (xmin;ymin) coordinates provide the top/left offset
131 * for drawing in the destination bitmap. If the source
132 * bitmap is larger than the rectangle, drawing is clipped.
133 * \param src Bitmap containing the source pixels.
134 * \param srcx Starting X offset in the source bitmap.
135 * \param srcy Starting Y offset in the source bitmap.
137 void gfx_blit(Bitmap *dst, const Rect *rect, const Bitmap *src, coord_t srcx, coord_t srcy)
139 coord_t dxmin, dymin, dxmax, dymax;
140 coord_t dx, dy, sx, sy;
143 * Pre-clip coordinates inside src->width/height.
147 dxmax = MIN(rect->xmax, rect->xmin + src->width);
148 dymax = MIN(rect->ymax, rect->ymin + src->height);
150 /* Perform regular clipping */
151 gfx_clip(dxmin, dxmax, srcx, dst->cr.xmin, dst->cr.xmax);
152 gfx_clip(dymin, dymax, srcy, dst->cr.ymin, dst->cr.ymax);
154 //kprintf("dxmin=%d, sxmin=%d, dxmax=%d; ", dxmin, sxmin, dxmax);
155 //kprintf("dymin=%d, symin=%d, dymax=%d\n", dymin, symin, dymax);
157 /* TODO: make it not as dog slow as this */
158 for (dx = dxmin, sx = srcx; dx < dxmax; ++dx, ++sx)
159 for (dy = dymin, sy = srcy; dy < dymax; ++dy, ++sy)
160 BM_DRAWPIXEL(dst, dx, dy, BM_READPIXEL(src, sx, sy));
164 * Blit a raster to a Bitmap.
166 * \todo Merge this function into gfx_blit()
170 void gfx_blitRaster(Bitmap *dst, coord_t dxmin, coord_t dymin,
171 const uint8_t *raster, coord_t w, coord_t h, coord_t stride)
173 coord_t dxmax = dxmin + w, dymax = dymin + h;
174 coord_t sxmin = 0, symin = 0;
175 coord_t dx, dy, sx, sy;
177 /* Perform regular clipping */
178 gfx_clip(dxmin, dxmax, sxmin, dst->cr.xmin, dst->cr.xmax);
179 gfx_clip(dymin, dymax, symin, dst->cr.ymin, dst->cr.ymax);
181 //kprintf("dxmin=%d, sxmin=%d, dxmax=%d; ", dxmin, sxmin, dxmax);
182 //kprintf("dymin=%d, symin=%d, dymax=%d\n", dymin, symin, dymax);
184 /* TODO: make it not as dog slow as this */
185 for (dx = dxmin, sx = sxmin; dx < dxmax; ++dx, ++sx)
186 for (dy = dymin, sy = symin; dy < dymax; ++dy, ++sy)
187 BM_DRAWPIXEL(dst, dx, dy, RAST_READPIXEL(raster, sx, sy, stride));
191 * Blit an Image to a Bitmap.
195 void gfx_blitImage(Bitmap *dst, coord_t dxmin, coord_t dymin, const Image *image)
199 gfx_blitRaster(dst, dxmin, dymin,
200 image->raster, image->width, image->height, image->stride);
204 #if CONFIG_GFX_CLIPPING || CONFIG_GFX_VCOORDS
207 * Set the bitmap clipping rectangle to the specified coordinates.
209 * All drawing performed on the bitmap will be clipped inside this
212 * The clipping rectangle is also used as a bounding box for the
213 * logical view of the virtual coordinate system.
215 * \note Following the convention used for all other operations, the
216 * top-left pixels of the rectangle are included, while the
217 * bottom-right pixels are considered outside the clipping region.
219 * \see gfx_setViewRect
221 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
227 ASSERT(maxx <= bm->width);
228 ASSERT(maxy <= bm->height);
235 // kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
236 // bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);
239 #endif /* CONFIG_GFX_CLIPPING */