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.8 2004/09/20 03:29:22 bernie
22 *#* Revision 1.7 2004/09/14 21:01:08 bernie
23 *#* Rename rectangle drawing functions; Unify filled/cleared implementations.
25 *#* Revision 1.4 2004/08/24 16:53:10 bernie
26 *#* Use new-style config macros.
28 *#* Revision 1.3 2004/08/04 03:16:59 bernie
29 *#* Switch to new DevLib CONFIG_ convention.
31 *#* Revision 1.2 2004/06/03 11:27:09 bernie
32 *#* Add dual-license information.
37 #include "config.h" /* CONFIG_GFX_CLIPPING */
39 #include <cpu.h> /* CPU_AVR */
40 #include <macros.h> /* SWAP() */
46 * Plot a point in bitmap \a bm.
48 * \note bm is evaluated twice
50 #define BM_PLOT(bm, x, y) \
51 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
54 * Clear a point in bitmap \a bm.
56 * \note bm is evaluated twice
58 #define BM_CLEAR(bm, x, y) \
59 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
62 * Set a point in bitmap \a bm to the specified color.
64 * \note bm is evaluated twice
65 * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
66 * \see BM_PLOT BM_CLEAR
68 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
70 uint8_t *p = (bm)->raster + ((y) / 8) * (bm)->width + (x); \
71 uint8_t mask = 1 << ((y) % 8); \
72 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
76 * Initialize a Bitmap structure with the provided parameters.
78 * \note The pen position is reset to the origin.
80 void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
91 * Clear the whole bitmap surface to all zeros
93 * \note This function does \b not update the current pen position
95 void gfx_ClearBitmap(Bitmap *bm)
97 memset(bm->raster, 0, (bm->width * bm->height) / 8);
103 * Copy a raster picture located in program memory in the bitmap.
104 * The size of the raster to copy *must* be the same of the raster bitmap.
106 * \note This function does \b not update the current pen position
108 void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster)
110 memcpy_P(bm->raster, raster, bm->height/8 * bm->width);
116 * Draw a line on the bitmap \a bm using the specified start and end
119 * \note This function does \b not update the current pen position
121 void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
123 int x, y, e, len, adx, ady, signx, signy;
126 #if CONFIG_GFX_CLIPPING
131 #define XMAX (bm->width - 1)
132 #define YMAX (bm->height - 1)
137 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
142 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
147 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
152 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
158 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
163 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
168 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
173 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
182 #endif /* CONFIG_GFX_CLIPPING */
216 /* X-major line (octants 1/4/5/8) */
223 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
236 /* Y-major line (octants 2/3/6/7) */
243 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
258 * Move the current pen position to the specified coordinates.
260 void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y)
267 * Draw a line from the current pen position to the new coordinates.
269 * \note This function moves the current pen position to the
272 void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y)
274 gfx_DrawLine(bm, bm->penX, bm->penY, x, y);
275 gfx_MoveTo(bm, x, y);
280 * Draw an the outline of an hollow rectangle.
282 * \note The bottom-right border of the rectangle is not drawn.
283 * \note This function does \b not update the current pen position
285 void gfx_RectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
287 /* Sort coords (needed for correct bottom-right semantics) */
288 if (x1 > x2) SWAP(x1, x2);
289 if (y1 > y2) SWAP(y1, y2);
292 gfx_DrawLine(bm, x1, y1, x2-1, y1);
293 gfx_DrawLine(bm, x2-1, y1, x2-1, y2-1);
294 gfx_DrawLine(bm, x2-1, y2-1, x1, y2-1);
295 gfx_DrawLine(bm, x1, y2-1, x1, y1);
300 * Fill a rectangular area with \a color.
302 * \note The bottom-right border of the rectangle is not drawn.
304 * \note This function does \b not update the current pen position
306 void gfx_RectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
311 if (x1 > x2) SWAP(x1, x2);
312 if (y1 > y2) SWAP(y1, y2);
314 /* Clip rect to bitmap bounds */
317 if (x1 > bm->width) x1 = bm->width;
318 if (x2 > bm->width) x2 = bm->width;
321 if (y1 > bm->width) y1 = bm->width;
322 if (y2 > bm->width) y2 = bm->width;
326 * NOTE: Code paths are duplicated for efficiency
328 if (color) /* fill */
330 for (x = x1; x < x2; x++)
331 for (y = y1; y < y2; y++)
336 for (x = x1; x < x2; x++)
337 for (y = y1; y < y2; y++)
344 * Draw a filled rectangle.
346 * \note The bottom-right border of the rectangle is not drawn.
348 * \note This function does \b not update the current pen position
350 void gfx_RectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
352 gfx_RectFillC(bm, x1, y1, x2, y2, 0xFF);
357 * Clear a rectangular area.
359 * \note The bottom-right border of the rectangle is not drawn.
361 * \note This function does \b not update the current pen position
363 void gfx_RectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
365 gfx_RectFillC(bm, x1, y1, x2, y2, 0x00);
370 * Imposta un rettangolo di clipping per il disegno nella bitmap
372 void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
378 ASSERT(maxx <= bm->width);
379 ASSERT(maxy <= bm->height);
386 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
387 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
392 #if CONFIG_GFX_VCOORDS
394 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
395 * al rettangolo di clipping della bitmap.
397 void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
404 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
405 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
407 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
408 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
414 * Transform a coordinate from the current reference system to a
415 * pixel offset within the bitmap.
417 coord_t gfx_TransformX(Bitmap *bm, vcoord_t x)
419 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
423 * Transform a coordinate from the current reference system to a
424 * pixel offset within the bitmap.
426 coord_t gfx_TransformY(Bitmap *bm, vcoord_t y)
428 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
433 * Draw a line from (x1;y1) to (x2;y2)
435 void gfx_VDrawLine(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
438 gfx_TransformX(bm, x1), gfx_TransformY(bm, y1),
439 gfx_TransformY(bm, x2), gfx_TransformY(bm, y2));
441 #endif /* CONFIG_GFX_VCOORDS */