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.3 2004/08/04 03:16:59 bernie
20 * Switch to new DevLib CONFIG_ convention.
22 * Revision 1.2 2004/06/03 11:27:09 bernie
23 * Add dual-license information.
29 #include <drv/kdebug.h>
35 * Plot a point in bitmap.
36 * \note bm is evaluated twice
38 #define BM_PLOT(bm, x, y) \
39 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
42 * Clear a point in bitmap.
43 * \note bm is evaluated twice
45 #define BM_CLEAR(bm, x, y) \
46 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
51 (void)(&a == &b); /* type check */ \
60 * Initialize a Bitmap structure with the provided parameters.
62 void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
73 * Clear the whole bitmap surface to all zeros
75 void gfx_ClearBitmap(Bitmap *bm)
77 memset(bm->raster, 0, (bm->width * bm->height) / 8);
82 * Copy a raster picture located in program memory in the bitmap.
83 * The size of the raster to copy *must* be the same of the raster bitmap.
85 void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster)
87 memcpy_P(bm->raster, raster, bm->height/8 * bm->width);
91 void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
93 int x, y, e, len, adx, ady, signx, signy;
96 #ifdef CONFIG_GFX_CLIPPING
101 #define XMAX (bm->width - 1)
102 #define YMAX (bm->height - 1)
107 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
112 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
117 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
122 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
128 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
133 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
138 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
143 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
152 #endif /* CONFIG_GRAPH_CLIPPING */
186 /* X-major line (octants 1/4/5/8) */
193 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
206 /* Y-major line (octants 2/3/6/7) */
213 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
227 void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y)
234 void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y)
236 gfx_DrawLine(bm, bm->penX, bm->penY, x, y);
237 gfx_MoveTo(bm, x, y);
242 * Draw a filled rectangle.
243 * \note The bottom-right border of the rectangle is not drawn.
245 void gfx_FillRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
250 if (x1 > x2) SWAP(x1, x2);
251 if (y1 > y2) SWAP(y1, y2);
253 /* Clip rect to bitmap bounds */
256 if (x1 > bm->width) x1 = bm->width;
257 if (x2 > bm->width) x2 = bm->width;
260 if (y1 > bm->width) y1 = bm->width;
261 if (y2 > bm->width) y2 = bm->width;
264 for (x = x1; x < x2; x++)
265 for (y = y1; y < y2; y++)
271 * Draw an empty rectangle.
272 * \note The bottom-right border of the rectangle is not drawn.
274 void gfx_DrawRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
277 if (x1 > x2) SWAP(x1, x2);
278 if (y1 > y2) SWAP(y1, y2);
281 gfx_DrawLine(bm, x1, y1, x2-1, y1);
282 gfx_DrawLine(bm, x2-1, y1, x2-1, y2-1);
283 gfx_DrawLine(bm, x2-1, y2-1, x1, y2-1);
284 gfx_DrawLine(bm, x1, y2-1, x1, y1);
289 * Clear a rectangular area.
290 * \note The bottom-right border of the rectangle is not drawn.
292 void gfx_ClearRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
297 if (x1 > x2) SWAP(x1, x2);
298 if (y1 > y2) SWAP(y1, y2);
300 /* Clip rect to bitmap bounds */
303 if (x1 > bm->width) x1 = bm->width;
304 if (x2 > bm->width) x2 = bm->width;
307 if (y1 > bm->width) y1 = bm->width;
308 if (y2 > bm->width) y2 = bm->width;
311 for (x = x1; x < x2; x++)
312 for (y = y1; y < y2; y++)
318 * Imposta un rettangolo di clipping per il disegno nella bitmap
320 void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
326 ASSERT(maxx < bm->width);
327 ASSERT(maxy < bm->height);
334 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
335 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
340 #if CONFIG_GFX_VCOORDS
342 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
343 * al rettangolo di clipping della bitmap.
345 void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
352 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin) / (vcoord_t)(x2 - x1); /* +1 */
353 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin) / (vcoord_t)(y2 - y1); /* +1 */
355 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
356 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
362 * Transform a coordinate from the current reference system to a
363 * pixel offset within the bitmap.
365 coord_t gfx_TransformX(Bitmap *bm, vcoord_t x)
367 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
371 * Transform a coordinate from the current reference system to a
372 * pixel offset within the bitmap.
374 coord_t gfx_TransformY(Bitmap *bm, vcoord_t y)
376 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
381 * Draw a line from (x1;y1) to (x2;y2)
383 void gfx_VDrawLine(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
386 gfx_TransformX(bm, x1), gfx_TransformY(bm, y1),
387 gfx_TransformY(bm, x2), gfx_TransformY(bm, y2));
389 #endif /* CONFIG_GFX_VCOORDS */