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.4 2004/08/24 16:53:10 bernie
20 * Use new-style config macros.
22 * Revision 1.3 2004/08/04 03:16:59 bernie
23 * Switch to new DevLib CONFIG_ convention.
25 * Revision 1.2 2004/06/03 11:27:09 bernie
26 * Add dual-license information.
32 #include <drv/kdebug.h>
38 * Plot a point in bitmap.
39 * \note bm is evaluated twice
41 #define BM_PLOT(bm, x, y) \
42 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
45 * Clear a point in bitmap.
46 * \note bm is evaluated twice
48 #define BM_CLEAR(bm, x, y) \
49 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
54 (void)(&a == &b); /* type check */ \
63 * Initialize a Bitmap structure with the provided parameters.
65 void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
76 * Clear the whole bitmap surface to all zeros
78 void gfx_ClearBitmap(Bitmap *bm)
80 memset(bm->raster, 0, (bm->width * bm->height) / 8);
85 * Copy a raster picture located in program memory in the bitmap.
86 * The size of the raster to copy *must* be the same of the raster bitmap.
88 void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster)
90 memcpy_P(bm->raster, raster, bm->height/8 * bm->width);
94 void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
96 int x, y, e, len, adx, ady, signx, signy;
99 #if CONFIG_GFX_CLIPPING
104 #define XMAX (bm->width - 1)
105 #define YMAX (bm->height - 1)
110 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
115 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
120 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
125 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
131 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
136 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
141 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
146 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
155 #endif /* CONFIG_GFX_CLIPPING */
189 /* X-major line (octants 1/4/5/8) */
196 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
209 /* Y-major line (octants 2/3/6/7) */
216 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
230 void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y)
237 void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y)
239 gfx_DrawLine(bm, bm->penX, bm->penY, x, y);
240 gfx_MoveTo(bm, x, y);
245 * Draw a filled rectangle.
246 * \note The bottom-right border of the rectangle is not drawn.
248 void gfx_FillRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
253 if (x1 > x2) SWAP(x1, x2);
254 if (y1 > y2) SWAP(y1, y2);
256 /* Clip rect to bitmap bounds */
259 if (x1 > bm->width) x1 = bm->width;
260 if (x2 > bm->width) x2 = bm->width;
263 if (y1 > bm->width) y1 = bm->width;
264 if (y2 > bm->width) y2 = bm->width;
267 for (x = x1; x < x2; x++)
268 for (y = y1; y < y2; y++)
274 * Draw an empty rectangle.
275 * \note The bottom-right border of the rectangle is not drawn.
277 void gfx_DrawRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
280 if (x1 > x2) SWAP(x1, x2);
281 if (y1 > y2) SWAP(y1, y2);
284 gfx_DrawLine(bm, x1, y1, x2-1, y1);
285 gfx_DrawLine(bm, x2-1, y1, x2-1, y2-1);
286 gfx_DrawLine(bm, x2-1, y2-1, x1, y2-1);
287 gfx_DrawLine(bm, x1, y2-1, x1, y1);
292 * Clear a rectangular area.
293 * \note The bottom-right border of the rectangle is not drawn.
295 void gfx_ClearRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
300 if (x1 > x2) SWAP(x1, x2);
301 if (y1 > y2) SWAP(y1, y2);
303 /* Clip rect to bitmap bounds */
306 if (x1 > bm->width) x1 = bm->width;
307 if (x2 > bm->width) x2 = bm->width;
310 if (y1 > bm->width) y1 = bm->width;
311 if (y2 > bm->width) y2 = bm->width;
314 for (x = x1; x < x2; x++)
315 for (y = y1; y < y2; y++)
321 * Imposta un rettangolo di clipping per il disegno nella bitmap
323 void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
329 ASSERT(maxx < bm->width);
330 ASSERT(maxy < bm->height);
337 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
338 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
343 #if CONFIG_GFX_VCOORDS
345 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
346 * al rettangolo di clipping della bitmap.
348 void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
355 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin) / (vcoord_t)(x2 - x1); /* +1 */
356 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin) / (vcoord_t)(y2 - y1); /* +1 */
358 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
359 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
365 * Transform a coordinate from the current reference system to a
366 * pixel offset within the bitmap.
368 coord_t gfx_TransformX(Bitmap *bm, vcoord_t x)
370 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
374 * Transform a coordinate from the current reference system to a
375 * pixel offset within the bitmap.
377 coord_t gfx_TransformY(Bitmap *bm, vcoord_t y)
379 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
384 * Draw a line from (x1;y1) to (x2;y2)
386 void gfx_VDrawLine(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
389 gfx_TransformX(bm, x1), gfx_TransformY(bm, y1),
390 gfx_TransformY(bm, x2), gfx_TransformY(bm, y2));
392 #endif /* CONFIG_GFX_VCOORDS */