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.5 2006/01/17 22:59:23 bernie
20 *#* Implement correct line clipping algorithm.
22 *#* Revision 1.4 2006/01/17 02:31:29 bernie
23 *#* Add bitmap format support; Improve some comments.
25 *#* Revision 1.3 2005/11/27 23:33:40 bernie
26 *#* Use appconfig.h instead of cfg/config.h.
28 *#* Revision 1.2 2005/11/04 18:17:45 bernie
29 *#* Fix header guards and includes for new location of gfx module.
31 *#* Revision 1.1 2005/11/04 18:11:35 bernie
32 *#* Move graphics stuff from mware/ to gfx/.
34 *#* Revision 1.14 2005/11/04 16:20:02 bernie
35 *#* Fix reference to README.devlib in header.
37 *#* Revision 1.13 2005/04/11 19:10:28 bernie
38 *#* Include top-level headers from cfg/ subdir.
40 *#* Revision 1.12 2005/03/01 23:26:45 bernie
41 *#* Use new CPU-neutral program-memory API.
45 #include <appconfig.h> /* CONFIG_GFX_CLIPPING */
46 #include <cfg/debug.h>
47 #include <cfg/cpu.h> /* CPU_HARVARD */
48 #include <cfg/macros.h> /* SWAP() */
53 * \name Known pixel formats for bitmap representation.
56 #define BITMAP_FMT_PLANAR_H_MSB 1 /**< Planar pixels, horizontal bytes, MSB left. */
57 #define BITMAP_FMT_PLANAR_V_LSB 2 /**< Planar pixels, vertical bytes, LSB top. */
60 #if CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB
62 #define BM_ADDR(bm, x, y) ((bm)->raster + (y) * (bm)->stride + ((x) / 8))
63 #define BM_MASK(bm, x, y) (1 << (7 - (x) % 8))
65 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
67 #define BM_ADDR(bm, x, y) ((bm)->raster + ((y) / 8) * (bm)->stride + (x))
68 #define BM_MASK(bm, x, y) (1 << ((y) % 8))
71 #error Unknown value of CONFIG_BITMAP_FMT
72 #endif /* CONFIG_BITMAP_FMT */
75 * Plot a point in bitmap \a bm.
77 * \note bm is evaluated twice.
78 * \see BM_CLEAR BM_DRAWPIXEL
80 #define BM_PLOT(bm, x, y) \
81 ( *BM_ADDR(bm, x, y) |= BM_MASK(bm, x, y) )
84 * Clear a point in bitmap \a bm.
86 * \note bm is evaluated twice.
87 * \see BM_PLOT BM_DRAWPIXEL
89 #define BM_CLEAR(bm, x, y) \
90 ( *BM_ADDR(bm, x, y) &= ~BM_MASK(bm, x, y) )
93 * Set a point in bitmap \a bm to the specified color.
95 * \note bm is evaluated twice.
96 * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
97 * \see BM_PLOT BM_CLEAR
99 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
101 uint8_t *p = BM_ADDR(bm, x, y); \
102 uint8_t mask = BM_MASK(bm, x, y); \
103 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
108 * Initialize a Bitmap structure with the provided parameters.
110 * \note The pen position is reset to the origin.
112 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
117 #if (CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_H_MSB)
118 bm->stride = (w + 7) / 8;
119 #elif CONFIG_BITMAP_FMT == BITMAP_FMT_PLANAR_V_LSB
122 #error Unknown value of CONFIG_BITMAP_FMT
123 #endif /* CONFIG_BITMAP_FMT */
135 * Clear the whole bitmap surface to the background color.
137 * \note This function does \b not update the current pen position
139 void gfx_bitmapClear(Bitmap *bm)
141 memset(bm->raster, 0, (bm->width * bm->height) / 8);
147 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
150 * Copy a raster picture located in program memory in the bitmap.
151 * The size of the raster to copy *must* be the same of the raster bitmap.
153 * \note This function does \b not update the current pen position
155 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
157 memcpy_P(bm->raster, raster, (bm->height / 8) * bm->width);
159 #endif /* CPU_HARVARD */
163 * Draw a sloped line without performing clipping.
165 * Parameters are the same of gfx_line().
166 * This routine is based on the Bresenham Line-Drawing Algorithm.
168 * \note Passing coordinates outside the bitmap boundaries will
169 * result in memory trashing.
171 * \todo Optimize for vertical and horiziontal lines.
175 static void gfx_lineUnclipped(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
177 int x, y, e, len, adx, ady, signx, signy;
210 /* X-major line (octants 1/4/5/8) */
217 ASSERT((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
230 /* Y-major line (octants 2/3/6/7) */
237 ASSERT ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height));
250 //! Helper routine for gfx_line().
251 static int gfx_findRegion(int x, int y, Rect *cr)
256 code |= 1; /* below */
257 else if (y < cr->ymin)
258 code |= 2; /* above */
261 code |= 4; /* right */
262 else if (x < cr->xmin)
263 code |= 8; /* left */
269 * Draw a sloped line segment.
271 * Draw a sloped line segment identified by the provided
272 * start and end coordinates on the bitmap \a bm.
274 * The line endpoints are clipped inside the current bitmap
275 * clipping rectangle using the Cohen-Sutherland algorithm,
276 * which is very fast.
278 * \note The point at coordinates \a x2 \a y2 is not drawn.
280 * \note This function does \b not update the current pen position.
282 * \todo Compute updated Bresenham error term.
284 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
286 #if CONFIG_GFX_CLIPPING
287 int clip1 = gfx_findRegion(x1, y1, &bm->cr);
288 int clip2 = gfx_findRegion(x2, y2, &bm->cr);
290 /* Loop while there is at least one point outside */
291 while (clip1 | clip2)
293 /* Check for line totally outside */
297 int c = clip1 ? clip1 : clip2;
300 if (c & 1) /* Below */
302 x = x1 + (x2 - x1) * (bm->cr.ymax - y1) / (y2 - y1);
305 else if (c & 2) /* Above */
307 x = x1 + (x2 - x1) * (bm->cr.ymin - y1) / (y2 - y1);
310 else if (c & 4) /* Right */
312 y = y1 + (y2 - y1) * (bm->cr.xmax - x1) / (x2 - x1);
317 y = y1 + (y2 - y1) * (bm->cr.xmin - x1) / (x2 - x1);
321 if (c == clip1) /* First endpoint was clipped */
323 // TODO: adjust Bresenham error term
324 //coord_t clipdx = ABS(x - x1);
325 //coord_t clipdy = ABS(y - y1);
326 //e += (clipdy * e2) + ((clipdx - clipdy) * e1);
330 clip1 = gfx_findRegion(x1, y1, &bm->cr);
332 else /* Second endpoint was clipped */
336 clip2 = gfx_findRegion(x2, y2, &bm->cr);
339 #endif /* CONFIG_GFX_CLIPPING */
341 gfx_lineUnclipped(bm, x1, y1, x2, y2);
345 * Move the current pen position to the specified coordinates.
347 * The pen position is used for drawing operations such as
348 * gfx_lineTo(), which can be used to draw polygons.
350 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
357 * Draw a line from the current pen position to the new coordinates.
359 * \note This function moves the current pen position to the
364 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
366 gfx_line(bm, bm->penX, bm->penY, x, y);
367 gfx_moveTo(bm, x, y);
372 * Draw the perimeter of an hollow rectangle.
374 * \note The bottom-right corner of the rectangle is drawn at (x2-1;y2-1).
375 * \note This function does \b not update the current pen position.
377 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
379 /* Sort coords (needed for correct bottom-right semantics) */
380 if (x1 > x2) SWAP(x1, x2);
381 if (y1 > y2) SWAP(y1, y2);
384 gfx_line(bm, x1, y1, x2-1, y1);
385 gfx_line(bm, x2-1, y1, x2-1, y2-1);
386 gfx_line(bm, x2-1, y2-1, x1, y2-1);
387 gfx_line(bm, x1, y2-1, x1, y1);
392 * Fill a rectangular area with \a color.
394 * \note The bottom-right border of the rectangle is not drawn.
396 * \note This function does \b not update the current pen position.
398 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
403 if (x1 > x2) SWAP(x1, x2);
404 if (y1 > y2) SWAP(y1, y2);
406 #if CONFIG_GFX_CLIPPING
407 /* Clip rect to bitmap clip region */
408 if (x1 < bm->cr.xmin) x1 = bm->cr.xmin;
409 if (x2 < bm->cr.xmin) x2 = bm->cr.xmin;
410 if (x1 > bm->cr.xmax) x1 = bm->cr.xmax;
411 if (x2 > bm->cr.xmax) x2 = bm->cr.xmax;
412 if (y1 < bm->cr.ymin) y1 = bm->cr.ymin;
413 if (y2 < bm->cr.ymin) y2 = bm->cr.ymin;
414 if (y1 > bm->cr.ymax) y1 = bm->cr.ymax;
415 if (y2 > bm->cr.ymax) y2 = bm->cr.ymax;
418 /* NOTE: Code paths are duplicated for efficiency */
419 if (color) /* fill */
421 for (x = x1; x < x2; x++)
422 for (y = y1; y < y2; y++)
427 for (x = x1; x < x2; x++)
428 for (y = y1; y < y2; y++)
435 * Draw a filled rectangle.
437 * \note The bottom-right border of the rectangle is not drawn.
439 * \note This function does \b not update the current pen position.
441 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
443 gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
448 * Clear a rectangular area.
450 * \note The bottom-right border of the rectangle is not cleared.
452 * \note This function does \b not update the current pen position.
454 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
456 gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
461 * Set the bitmap clipping rectangle to the specified coordinates.
463 * All drawing performed on the bitmap will be clipped inside this
466 * \note Following the convention used in all other operations, the
467 * top-left pixels of the rectangle are included, while the
468 * bottom-right pixels are considered outside the clipping region.
470 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
476 ASSERT(maxx <= bm->width);
477 ASSERT(maxy <= bm->height);
484 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
485 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
490 #if CONFIG_GFX_VCOORDS
492 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
493 * al rettangolo di clipping della bitmap.
495 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
502 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
503 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
505 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
506 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
512 * Transform a coordinate from the current reference system to a
513 * pixel offset within the bitmap.
515 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
517 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
521 * Transform a coordinate from the current reference system to a
522 * pixel offset within the bitmap.
524 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
526 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
531 * Draw a line from (x1;y1) to (x2;y2).
533 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
536 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
537 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
539 #endif /* CONFIG_GFX_VCOORDS */