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.5 2004/08/25 14:12:09 rasky
20 *#* Aggiornato il comment block dei log RCS
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.
35 #include <drv/kdebug.h>
41 * Plot a point in bitmap.
42 * \note bm is evaluated twice
44 #define BM_PLOT(bm, x, y) \
45 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
48 * Clear a point in bitmap.
49 * \note bm is evaluated twice
51 #define BM_CLEAR(bm, x, y) \
52 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
57 (void)(&a == &b); /* type check */ \
66 * Initialize a Bitmap structure with the provided parameters.
68 void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
79 * Clear the whole bitmap surface to all zeros
81 void gfx_ClearBitmap(Bitmap *bm)
83 memset(bm->raster, 0, (bm->width * bm->height) / 8);
88 * Copy a raster picture located in program memory in the bitmap.
89 * The size of the raster to copy *must* be the same of the raster bitmap.
91 void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster)
93 memcpy_P(bm->raster, raster, bm->height/8 * bm->width);
97 void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
99 int x, y, e, len, adx, ady, signx, signy;
102 #if CONFIG_GFX_CLIPPING
107 #define XMAX (bm->width - 1)
108 #define YMAX (bm->height - 1)
113 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
118 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
123 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
128 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
134 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
139 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
144 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
149 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
158 #endif /* CONFIG_GFX_CLIPPING */
192 /* X-major line (octants 1/4/5/8) */
199 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
212 /* Y-major line (octants 2/3/6/7) */
219 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
233 void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y)
240 void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y)
242 gfx_DrawLine(bm, bm->penX, bm->penY, x, y);
243 gfx_MoveTo(bm, x, y);
248 * Draw a filled rectangle.
249 * \note The bottom-right border of the rectangle is not drawn.
251 void gfx_FillRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
256 if (x1 > x2) SWAP(x1, x2);
257 if (y1 > y2) SWAP(y1, y2);
259 /* Clip rect to bitmap bounds */
262 if (x1 > bm->width) x1 = bm->width;
263 if (x2 > bm->width) x2 = bm->width;
266 if (y1 > bm->width) y1 = bm->width;
267 if (y2 > bm->width) y2 = bm->width;
270 for (x = x1; x < x2; x++)
271 for (y = y1; y < y2; y++)
277 * Draw an empty rectangle.
278 * \note The bottom-right border of the rectangle is not drawn.
280 void gfx_DrawRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
283 if (x1 > x2) SWAP(x1, x2);
284 if (y1 > y2) SWAP(y1, y2);
287 gfx_DrawLine(bm, x1, y1, x2-1, y1);
288 gfx_DrawLine(bm, x2-1, y1, x2-1, y2-1);
289 gfx_DrawLine(bm, x2-1, y2-1, x1, y2-1);
290 gfx_DrawLine(bm, x1, y2-1, x1, y1);
295 * Clear a rectangular area.
296 * \note The bottom-right border of the rectangle is not drawn.
298 void gfx_ClearRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
303 if (x1 > x2) SWAP(x1, x2);
304 if (y1 > y2) SWAP(y1, y2);
306 /* Clip rect to bitmap bounds */
309 if (x1 > bm->width) x1 = bm->width;
310 if (x2 > bm->width) x2 = bm->width;
313 if (y1 > bm->width) y1 = bm->width;
314 if (y2 > bm->width) y2 = bm->width;
317 for (x = x1; x < x2; x++)
318 for (y = y1; y < y2; y++)
324 * Imposta un rettangolo di clipping per il disegno nella bitmap
326 void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
332 ASSERT(maxx < bm->width);
333 ASSERT(maxy < bm->height);
340 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
341 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
346 #if CONFIG_GFX_VCOORDS
348 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
349 * al rettangolo di clipping della bitmap.
351 void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
358 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin) / (vcoord_t)(x2 - x1); /* +1 */
359 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin) / (vcoord_t)(y2 - y1); /* +1 */
361 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
362 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
368 * Transform a coordinate from the current reference system to a
369 * pixel offset within the bitmap.
371 coord_t gfx_TransformX(Bitmap *bm, vcoord_t x)
373 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
377 * Transform a coordinate from the current reference system to a
378 * pixel offset within the bitmap.
380 coord_t gfx_TransformY(Bitmap *bm, vcoord_t y)
382 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
387 * Draw a line from (x1;y1) to (x2;y2)
389 void gfx_VDrawLine(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
392 gfx_TransformX(bm, x1), gfx_TransformY(bm, y1),
393 gfx_TransformY(bm, x2), gfx_TransformY(bm, y2));
395 #endif /* CONFIG_GFX_VCOORDS */