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
16 * \todo Rename functions to match the coding-standard.
21 *#* Revision 1.9 2004/10/21 10:58:33 bernie
24 *#* Revision 1.8 2004/09/20 03:29:22 bernie
27 *#* Revision 1.7 2004/09/14 21:01:08 bernie
28 *#* Rename rectangle drawing functions; Unify filled/cleared implementations.
30 *#* Revision 1.4 2004/08/24 16:53:10 bernie
31 *#* Use new-style config macros.
33 *#* Revision 1.3 2004/08/04 03:16:59 bernie
34 *#* Switch to new DevLib CONFIG_ convention.
36 *#* Revision 1.2 2004/06/03 11:27:09 bernie
37 *#* Add dual-license information.
42 #include "config.h" /* CONFIG_GFX_CLIPPING */
44 #include <cpu.h> /* CPU_AVR */
45 #include <macros.h> /* SWAP() */
51 * Plot a point in bitmap \a bm.
53 * \note bm is evaluated twice
55 #define BM_PLOT(bm, x, y) \
56 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
59 * Clear a point in bitmap \a bm.
61 * \note bm is evaluated twice
63 #define BM_CLEAR(bm, x, y) \
64 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
67 * Set a point in bitmap \a bm to the specified color.
69 * \note bm is evaluated twice
70 * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
71 * \see BM_PLOT BM_CLEAR
73 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
75 uint8_t *p = (bm)->raster + ((y) / 8) * (bm)->width + (x); \
76 uint8_t mask = 1 << ((y) % 8); \
77 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
81 * Initialize a Bitmap structure with the provided parameters.
83 * \note The pen position is reset to the origin.
85 void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
96 * Clear the whole bitmap surface to all zeros
98 * \note This function does \b not update the current pen position
100 void gfx_ClearBitmap(Bitmap *bm)
102 memset(bm->raster, 0, (bm->width * bm->height) / 8);
108 * Copy a raster picture located in program memory in the bitmap.
109 * The size of the raster to copy *must* be the same of the raster bitmap.
111 * \note This function does \b not update the current pen position
113 void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster)
115 memcpy_P(bm->raster, raster, bm->height/8 * bm->width);
121 * Draw a line on the bitmap \a bm using the specified start and end
124 * \note This function does \b not update the current pen position
126 void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
128 int x, y, e, len, adx, ady, signx, signy;
131 #if CONFIG_GFX_CLIPPING
136 #define XMAX (bm->width - 1)
137 #define YMAX (bm->height - 1)
142 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
147 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
152 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
157 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
163 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
168 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
173 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
178 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
187 #endif /* CONFIG_GFX_CLIPPING */
221 /* X-major line (octants 1/4/5/8) */
228 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
241 /* Y-major line (octants 2/3/6/7) */
248 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
263 * Move the current pen position to the specified coordinates.
265 void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y)
272 * Draw a line from the current pen position to the new coordinates.
274 * \note This function moves the current pen position to the
277 void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y)
279 gfx_DrawLine(bm, bm->penX, bm->penY, x, y);
280 gfx_MoveTo(bm, x, y);
285 * Draw an the outline of an hollow rectangle.
287 * \note The bottom-right border of the rectangle is not drawn.
288 * \note This function does \b not update the current pen position
290 void gfx_RectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
292 /* Sort coords (needed for correct bottom-right semantics) */
293 if (x1 > x2) SWAP(x1, x2);
294 if (y1 > y2) SWAP(y1, y2);
297 gfx_DrawLine(bm, x1, y1, x2-1, y1);
298 gfx_DrawLine(bm, x2-1, y1, x2-1, y2-1);
299 gfx_DrawLine(bm, x2-1, y2-1, x1, y2-1);
300 gfx_DrawLine(bm, x1, y2-1, x1, y1);
305 * Fill a rectangular area with \a color.
307 * \note The bottom-right border of the rectangle is not drawn.
309 * \note This function does \b not update the current pen position
311 void gfx_RectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
316 if (x1 > x2) SWAP(x1, x2);
317 if (y1 > y2) SWAP(y1, y2);
319 /* Clip rect to bitmap bounds */
322 if (x1 > bm->width) x1 = bm->width;
323 if (x2 > bm->width) x2 = bm->width;
326 if (y1 > bm->width) y1 = bm->width;
327 if (y2 > bm->width) y2 = bm->width;
331 * NOTE: Code paths are duplicated for efficiency
333 if (color) /* fill */
335 for (x = x1; x < x2; x++)
336 for (y = y1; y < y2; y++)
341 for (x = x1; x < x2; x++)
342 for (y = y1; y < y2; y++)
349 * Draw a filled rectangle.
351 * \note The bottom-right border of the rectangle is not drawn.
353 * \note This function does \b not update the current pen position
355 void gfx_RectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
357 gfx_RectFillC(bm, x1, y1, x2, y2, 0xFF);
362 * Clear a rectangular area.
364 * \note The bottom-right border of the rectangle is not drawn.
366 * \note This function does \b not update the current pen position
368 void gfx_RectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
370 gfx_RectFillC(bm, x1, y1, x2, y2, 0x00);
375 * Imposta un rettangolo di clipping per il disegno nella bitmap
377 void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
383 ASSERT(maxx <= bm->width);
384 ASSERT(maxy <= bm->height);
391 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
392 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
397 #if CONFIG_GFX_VCOORDS
399 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
400 * al rettangolo di clipping della bitmap.
402 void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
409 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
410 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
412 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
413 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
419 * Transform a coordinate from the current reference system to a
420 * pixel offset within the bitmap.
422 coord_t gfx_TransformX(Bitmap *bm, vcoord_t x)
424 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
428 * Transform a coordinate from the current reference system to a
429 * pixel offset within the bitmap.
431 coord_t gfx_TransformY(Bitmap *bm, vcoord_t y)
433 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
438 * Draw a line from (x1;y1) to (x2;y2)
440 void gfx_VDrawLine(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
443 gfx_TransformX(bm, x1), gfx_TransformY(bm, y1),
444 gfx_TransformY(bm, x2), gfx_TransformY(bm, y2));
446 #endif /* CONFIG_GFX_VCOORDS */