4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
6 * This file is part of DevLib - See devlib/README 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.7 2004/09/14 21:01:08 bernie
20 *#* Rename rectangle drawing functions; Unify filled/cleared implementations.
22 *#* Revision 1.4 2004/08/24 16:53:10 bernie
23 *#* Use new-style config macros.
25 *#* Revision 1.3 2004/08/04 03:16:59 bernie
26 *#* Switch to new DevLib CONFIG_ convention.
28 *#* Revision 1.2 2004/06/03 11:27:09 bernie
29 *#* Add dual-license information.
34 #include "config.h" /* CONFIG_GFX_CLIPPING */
36 #include <cpu.h> /* CPU_AVR */
37 #include <macros.h> /* SWAP() */
43 * Plot a point in bitmap \a bm.
45 * \note bm is evaluated twice
47 #define BM_PLOT(bm, x, y) \
48 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
51 * Clear a point in bitmap \a bm.
53 * \note bm is evaluated twice
55 #define BM_CLEAR(bm, x, y) \
56 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
59 * Set a point in bitmap \a bm to the specified color.
61 * \note bm is evaluated twice
62 * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
63 * \see BM_PLOT BM_CLEAR
65 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
67 uint8_t *p = (bm)->raster + ((y) / 8) * (bm)->width + (x); \
68 uint8_t mask = 1 << ((y) % 8); \
69 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
73 * Initialize a Bitmap structure with the provided parameters.
75 * \note The pen position is reset to the origin.
77 void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
88 * Clear the whole bitmap surface to all zeros
90 * \note This function does \b not update the current pen position
92 void gfx_ClearBitmap(Bitmap *bm)
94 memset(bm->raster, 0, (bm->width * bm->height) / 8);
100 * Copy a raster picture located in program memory in the bitmap.
101 * The size of the raster to copy *must* be the same of the raster bitmap.
103 * \note This function does \b not update the current pen position
105 void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster)
107 memcpy_P(bm->raster, raster, bm->height/8 * bm->width);
113 * Draw a line on the bitmap \a bm using the specified start and end
116 * \note This function does \b not update the current pen position
118 void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
120 int x, y, e, len, adx, ady, signx, signy;
123 #if CONFIG_GFX_CLIPPING
128 #define XMAX (bm->width - 1)
129 #define YMAX (bm->height - 1)
134 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
139 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
144 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
149 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
155 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
160 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
165 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
170 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
179 #endif /* CONFIG_GFX_CLIPPING */
213 /* X-major line (octants 1/4/5/8) */
220 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
233 /* Y-major line (octants 2/3/6/7) */
240 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
255 * Move the current pen position to the specified coordinates.
257 void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y)
264 * Draw a line from the current pen position to the new coordinates.
266 * \note This function moves the current pen position to the
269 void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y)
271 gfx_DrawLine(bm, bm->penX, bm->penY, x, y);
272 gfx_MoveTo(bm, x, y);
277 * Draw an the outline of an hollow rectangle.
279 * \note The bottom-right border of the rectangle is not drawn.
280 * \note This function does \b not update the current pen position
282 void gfx_RectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
284 /* Sort coords (needed for correct bottom-right semantics) */
285 if (x1 > x2) SWAP(x1, x2);
286 if (y1 > y2) SWAP(y1, y2);
289 gfx_DrawLine(bm, x1, y1, x2-1, y1);
290 gfx_DrawLine(bm, x2-1, y1, x2-1, y2-1);
291 gfx_DrawLine(bm, x2-1, y2-1, x1, y2-1);
292 gfx_DrawLine(bm, x1, y2-1, x1, y1);
297 * Fill a rectangular area with \a color.
299 * \note The bottom-right border of the rectangle is not drawn.
301 * \note This function does \b not update the current pen position
303 void gfx_RectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
308 if (x1 > x2) SWAP(x1, x2);
309 if (y1 > y2) SWAP(y1, y2);
311 /* Clip rect to bitmap bounds */
314 if (x1 > bm->width) x1 = bm->width;
315 if (x2 > bm->width) x2 = bm->width;
318 if (y1 > bm->width) y1 = bm->width;
319 if (y2 > bm->width) y2 = bm->width;
323 * NOTE: Code paths are duplicated for efficiency
325 if (color) /* fill */
327 for (x = x1; x < x2; x++)
328 for (y = y1; y < y2; y++)
333 for (x = x1; x < x2; x++)
334 for (y = y1; y < y2; y++)
341 * Draw a filled rectangle.
343 * \note The bottom-right border of the rectangle is not drawn.
345 * \note This function does \b not update the current pen position
347 void gfx_RectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
349 gfx_RectFillC(bm, x1, y1, x2, y2, 0xFF);
354 * Clear a rectangular area.
356 * \note The bottom-right border of the rectangle is not drawn.
358 * \note This function does \b not update the current pen position
360 void gfx_RectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
362 gfx_RectFillC(bm, x1, y1, x2, y2, 0x00);
367 * Imposta un rettangolo di clipping per il disegno nella bitmap
369 void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
375 ASSERT(maxx < bm->width);
376 ASSERT(maxy < bm->height);
383 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
384 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
389 #if CONFIG_GFX_VCOORDS
391 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
392 * al rettangolo di clipping della bitmap.
394 void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
401 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
402 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
404 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
405 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
411 * Transform a coordinate from the current reference system to a
412 * pixel offset within the bitmap.
414 coord_t gfx_TransformX(Bitmap *bm, vcoord_t x)
416 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
420 * Transform a coordinate from the current reference system to a
421 * pixel offset within the bitmap.
423 coord_t gfx_TransformY(Bitmap *bm, vcoord_t y)
425 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
430 * Draw a line from (x1;y1) to (x2;y2)
432 void gfx_VDrawLine(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
435 gfx_TransformX(bm, x1), gfx_TransformY(bm, y1),
436 gfx_TransformY(bm, x2), gfx_TransformY(bm, y2));
438 #endif /* CONFIG_GFX_VCOORDS */