4 * Copyright 2003, 2004, 2005 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 General pourpose graphics routines
19 *#* Revision 1.6 2006/01/23 23:13:39 bernie
20 *#* gfx_blit(): New function, but dog slow for now.
22 *#* Revision 1.5 2006/01/17 22:59:23 bernie
23 *#* Implement correct line clipping algorithm.
25 *#* Revision 1.4 2006/01/17 02:31:29 bernie
26 *#* Add bitmap format support; Improve some comments.
28 *#* Revision 1.3 2005/11/27 23:33:40 bernie
29 *#* Use appconfig.h instead of cfg/config.h.
31 *#* Revision 1.2 2005/11/04 18:17:45 bernie
32 *#* Fix header guards and includes for new location of gfx module.
34 *#* Revision 1.1 2005/11/04 18:11:35 bernie
35 *#* Move graphics stuff from mware/ to gfx/.
37 *#* Revision 1.14 2005/11/04 16:20:02 bernie
38 *#* Fix reference to README.devlib in header.
40 *#* Revision 1.13 2005/04/11 19:10:28 bernie
41 *#* Include top-level headers from cfg/ subdir.
43 *#* Revision 1.12 2005/03/01 23:26:45 bernie
44 *#* Use new CPU-neutral program-memory API.
48 #include <appconfig.h> /* CONFIG_GFX_CLIPPING */
49 #include <cfg/debug.h>
50 #include <cfg/cpu.h> /* CPU_HARVARD */
51 #include <cfg/macros.h> /* SWAP() */
56 * \name Known pixel formats for bitmap representation.
59 #define BITMAP_FMT_PLANAR_H_MSB 1 /**< Planar pixels, horizontal bytes, MSB left. */
60 #define BITMAP_FMT_PLANAR_V_LSB 2 /**< Planar pixels, vertical bytes, LSB top. */
63 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
65 #define BM_ADDR(bm, x, y) ((bm)->raster + (y) * (bm)->stride + ((x) / 8))
66 #define BM_MASK(bm, x, y) (1 << (7 - (x) % 8))
68 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
70 #define BM_ADDR(bm, x, y) ((bm)->raster + ((y) / 8) * (bm)->stride + (x))
71 #define BM_MASK(bm, x, y) (1 << ((y) % 8))
74 #error Unknown value of CONFIG_BITMAP_FMT
75 #endif /* CONFIG_BITMAP_FMT */
78 * Plot a pixel in bitmap \a bm.
80 * \note bm is evaluated twice.
81 * \see BM_CLEAR BM_DRAWPIXEL
83 #define BM_PLOT(bm, x, y) \
84 ( *BM_ADDR(bm, x, y) |= BM_MASK(bm, x, y) )
87 * Clear a pixel in bitmap \a bm.
89 * \note bm is evaluated twice.
90 * \see BM_PLOT BM_DRAWPIXEL
92 #define BM_CLEAR(bm, x, y) \
93 ( *BM_ADDR(bm, x, y) &= ~BM_MASK(bm, x, y) )
96 * Set a pixel in bitmap \a bm to the specified color.
98 * \note bm is evaluated twice.
99 * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
100 * \see BM_PLOT BM_CLEAR
102 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
104 uint8_t *p = BM_ADDR(bm, x, y); \
105 uint8_t mask = BM_MASK(bm, x, y); \
106 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
110 * Get the value of the pixel in bitmap \a bm.
112 * \return The returned value is either 0 or 1.
114 * \note bm is evaluated twice.
117 #define BM_READPIXEL(bm, x, y) \
118 ( *BM_ADDR(bm, x, y) & BM_MASK(bm, x, y) ? 1 : 0 )
121 * Initialize a Bitmap structure with the provided parameters.
123 * \note The pen position is reset to the origin.
125 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
130 #if (CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB)
131 bm->stride = (w + 7) / 8;
132 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
135 #error Unknown value of CONFIG_BITMAP_FMT
136 #endif /* CONFIG_BITMAP_FMT */
140 #if CONFIG_GFX_CLIPPING
145 #endif /* CONFIG_GFX_CLIPPING */
150 * Clear the whole bitmap surface to the background color.
152 * \note This function does \b not update the current pen position.
154 void gfx_bitmapClear(Bitmap *bm)
156 memset(bm->raster, 0, RASTER_SIZE(bm->width, bm->height));
162 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
165 * Copy a raster picture located in program memory in the bitmap.
166 * The size of the raster to copy *must* be the same of the raster bitmap.
168 * \note This function does \b not update the current pen position
170 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
172 memcpy_P(bm->raster, raster, (bm->height / 8) * bm->width);
174 #endif /* CPU_HARVARD */
177 * Copy a rectangular area of a bitmap on another bitmap.
179 * Blitting is a common copy operation involving two bitmaps.
180 * A rectangular area of the source bitmap is copied bit-wise
181 * to a different position in the destination bitmap.
183 * \note Using the same bitmap for \a src and \a dst is unsupported.
185 * \param dst Bitmap where the operation writes
188 void gfx_blit(Bitmap *dst, Rect *rect, Bitmap *src, coord_t srcx, coord_t srcy)
190 coord_t dxmin, dymin, dxmax, dymax;
191 coord_t dx, dy, sx, sy;
194 * Clip coordinates inside dst->cr and src->width/height.
197 if (dxmin < dst->cr.xmin)
199 srcx += dst->cr.xmin - dxmin;
200 dxmin = dst->cr.xmin;
203 if (dymin < dst->cr.ymin)
205 srcy += dst->cr.ymin - dymin;
206 dymin = dst->cr.ymin;
208 dxmax = MIN(MIN(rect->xmax, rect->xmin + src->width), dst->cr.xmax);
209 dymax = MIN(MIN(rect->ymax, rect->ymin + src->height), dst->cr.ymax);
211 /* TODO: make it not as dog slow as this */
212 for (dx = dxmin, sx = srcx; dx < dxmax; ++dx, ++sx)
213 for (dy = dymin, sy = srcy; dy < dymax; ++dy, ++sy)
214 BM_DRAWPIXEL(dst, dx, dy, BM_READPIXEL(src, sx, sy));
218 * Draw a sloped line without performing clipping.
220 * Parameters are the same of gfx_line().
221 * This routine is based on the Bresenham Line-Drawing Algorithm.
223 * \note Passing coordinates outside the bitmap boundaries will
224 * result in memory trashing.
226 * \todo Optimize for vertical and horiziontal lines.
230 static void gfx_lineUnclipped(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
232 int x, y, e, len, adx, ady, signx, signy;
265 /* X-major line (octants 1/4/5/8) */
272 ASSERT((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
285 /* Y-major line (octants 2/3/6/7) */
292 ASSERT ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
305 //! Helper routine for gfx_line().
306 static int gfx_findRegion(int x, int y, Rect *cr)
311 code |= 1; /* below */
312 else if (y < cr->ymin)
313 code |= 2; /* above */
316 code |= 4; /* right */
317 else if (x < cr->xmin)
318 code |= 8; /* left */
324 * Draw a sloped line segment.
326 * Draw a sloped line segment identified by the provided
327 * start and end coordinates on the bitmap \a bm.
329 * The line endpoints are clipped inside the current bitmap
330 * clipping rectangle using the Cohen-Sutherland algorithm,
331 * which is very fast.
333 * \note The point at coordinates \a x2 \a y2 is not drawn.
335 * \note This function does \b not update the current pen position.
337 * \todo Compute updated Bresenham error term.
339 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
341 #if CONFIG_GFX_CLIPPING
342 int clip1 = gfx_findRegion(x1, y1, &bm->cr);
343 int clip2 = gfx_findRegion(x2, y2, &bm->cr);
345 /* Loop while there is at least one point outside */
346 while (clip1 | clip2)
348 /* Check for line totally outside */
352 int c = clip1 ? clip1 : clip2;
355 if (c & 1) /* Below */
357 x = x1 + (x2 - x1) * (bm->cr.ymax - y1) / (y2 - y1);
360 else if (c & 2) /* Above */
362 x = x1 + (x2 - x1) * (bm->cr.ymin - y1) / (y2 - y1);
365 else if (c & 4) /* Right */
367 y = y1 + (y2 - y1) * (bm->cr.xmax - x1) / (x2 - x1);
372 y = y1 + (y2 - y1) * (bm->cr.xmin - x1) / (x2 - x1);
376 if (c == clip1) /* First endpoint was clipped */
378 // TODO: adjust Bresenham error term
379 //coord_t clipdx = ABS(x - x1);
380 //coord_t clipdy = ABS(y - y1);
381 //e += (clipdy * e2) + ((clipdx - clipdy) * e1);
385 clip1 = gfx_findRegion(x1, y1, &bm->cr);
387 else /* Second endpoint was clipped */
391 clip2 = gfx_findRegion(x2, y2, &bm->cr);
394 #endif /* CONFIG_GFX_CLIPPING */
396 gfx_lineUnclipped(bm, x1, y1, x2, y2);
400 * Move the current pen position to the specified coordinates.
402 * The pen position is used for drawing operations such as
403 * gfx_lineTo(), which can be used to draw polygons.
405 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
412 * Draw a line from the current pen position to the new coordinates.
414 * \note This function moves the current pen position to the
419 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
421 gfx_line(bm, bm->penX, bm->penY, x, y);
422 gfx_moveTo(bm, x, y);
427 * Draw the perimeter of an hollow rectangle.
429 * \note The bottom-right corner of the rectangle is drawn at (x2-1;y2-1).
430 * \note This function does \b not update the current pen position.
432 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
434 /* Sort coords (needed for correct bottom-right semantics) */
435 if (x1 > x2) SWAP(x1, x2);
436 if (y1 > y2) SWAP(y1, y2);
439 gfx_line(bm, x1, y1, x2-1, y1);
440 gfx_line(bm, x2-1, y1, x2-1, y2-1);
441 gfx_line(bm, x2-1, y2-1, x1, y2-1);
442 gfx_line(bm, x1, y2-1, x1, y1);
447 * Fill a rectangular area with \a color.
449 * \note The bottom-right border of the rectangle is not drawn.
451 * \note This function does \b not update the current pen position.
453 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
458 if (x1 > x2) SWAP(x1, x2);
459 if (y1 > y2) SWAP(y1, y2);
461 #if CONFIG_GFX_CLIPPING
462 /* Clip rect to bitmap clip region */
463 if (x1 < bm->cr.xmin) x1 = bm->cr.xmin;
464 if (x2 < bm->cr.xmin) x2 = bm->cr.xmin;
465 if (x1 > bm->cr.xmax) x1 = bm->cr.xmax;
466 if (x2 > bm->cr.xmax) x2 = bm->cr.xmax;
467 if (y1 < bm->cr.ymin) y1 = bm->cr.ymin;
468 if (y2 < bm->cr.ymin) y2 = bm->cr.ymin;
469 if (y1 > bm->cr.ymax) y1 = bm->cr.ymax;
470 if (y2 > bm->cr.ymax) y2 = bm->cr.ymax;
473 /* NOTE: Code paths are duplicated for efficiency */
474 if (color) /* fill */
476 for (x = x1; x < x2; x++)
477 for (y = y1; y < y2; y++)
482 for (x = x1; x < x2; x++)
483 for (y = y1; y < y2; y++)
490 * Draw a filled rectangle.
492 * \note The bottom-right border of the rectangle is not drawn.
494 * \note This function does \b not update the current pen position.
496 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
498 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
503 * Clear a rectangular area.
505 * \note The bottom-right border of the rectangle is not cleared.
507 * \note This function does \b not update the current pen position.
509 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
511 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
516 * Set the bitmap clipping rectangle to the specified coordinates.
518 * All drawing performed on the bitmap will be clipped inside this
521 * \note Following the convention used in all other operations, the
522 * top-left pixels of the rectangle are included, while the
523 * bottom-right pixels are considered outside the clipping region.
525 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
531 ASSERT(maxx <= bm->width);
532 ASSERT(maxy <= bm->height);
539 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
540 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
545 #if CONFIG_GFX_VCOORDS
547 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
548 * al rettangolo di clipping della bitmap.
550 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
557 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
558 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
560 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
561 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
567 * Transform a coordinate from the current reference system to a
568 * pixel offset within the bitmap.
570 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
572 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
576 * Transform a coordinate from the current reference system to a
577 * pixel offset within the bitmap.
579 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
581 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
586 * Draw a line from (x1;y1) to (x2;y2).
588 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
591 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
592 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
594 #endif /* CONFIG_GFX_VCOORDS */