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.6 2004/09/06 21:50:46 bernie
20 *#* Clarify side-effects in documentation.
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 * \note The pen position is reset to the origin.
70 void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
81 * Clear the whole bitmap surface to all zeros
83 * \note This function does \b not update the current pen position
85 void gfx_ClearBitmap(Bitmap *bm)
87 memset(bm->raster, 0, (bm->width * bm->height) / 8);
92 * Copy a raster picture located in program memory in the bitmap.
93 * The size of the raster to copy *must* be the same of the raster bitmap.
95 * \note This function does \b not update the current pen position
97 void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster)
99 memcpy_P(bm->raster, raster, bm->height/8 * bm->width);
104 * Draw a line on the bitmap \a bm using the specified start and end
107 * \note This function does \b not update the current pen position
109 void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
111 int x, y, e, len, adx, ady, signx, signy;
114 #if CONFIG_GFX_CLIPPING
119 #define XMAX (bm->width - 1)
120 #define YMAX (bm->height - 1)
125 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
130 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
135 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
140 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
146 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
151 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
156 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
161 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
170 #endif /* CONFIG_GFX_CLIPPING */
204 /* X-major line (octants 1/4/5/8) */
211 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
224 /* Y-major line (octants 2/3/6/7) */
231 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
246 * Move the current pen position to the specified coordinates.
248 void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y)
255 * Draw a line from the current pen position to the new coordinates.
257 * \note This function moves the current pen position to the
260 void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y)
262 gfx_DrawLine(bm, bm->penX, bm->penY, x, y);
263 gfx_MoveTo(bm, x, y);
268 * Draw a filled rectangle.
270 * \note The bottom-right border of the rectangle is not drawn.
272 * \note This function does \b not update the current pen position
274 void gfx_FillRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
279 if (x1 > x2) SWAP(x1, x2);
280 if (y1 > y2) SWAP(y1, y2);
282 /* Clip rect to bitmap bounds */
285 if (x1 > bm->width) x1 = bm->width;
286 if (x2 > bm->width) x2 = bm->width;
289 if (y1 > bm->width) y1 = bm->width;
290 if (y2 > bm->width) y2 = bm->width;
293 for (x = x1; x < x2; x++)
294 for (y = y1; y < y2; y++)
300 * Draw an empty rectangle.
302 * \note The bottom-right border of the rectangle is not drawn.
303 * \note This function does \b not update the current pen position
305 void gfx_DrawRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
308 if (x1 > x2) SWAP(x1, x2);
309 if (y1 > y2) SWAP(y1, y2);
312 gfx_DrawLine(bm, x1, y1, x2-1, y1);
313 gfx_DrawLine(bm, x2-1, y1, x2-1, y2-1);
314 gfx_DrawLine(bm, x2-1, y2-1, x1, y2-1);
315 gfx_DrawLine(bm, x1, y2-1, x1, y1);
320 * Clear a rectangular area.
321 * \note The bottom-right border of the rectangle is not drawn.
322 * \note This function does \b not update the current pen position
324 void gfx_ClearRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
329 if (x1 > x2) SWAP(x1, x2);
330 if (y1 > y2) SWAP(y1, y2);
332 /* Clip rect to bitmap bounds */
335 if (x1 > bm->width) x1 = bm->width;
336 if (x2 > bm->width) x2 = bm->width;
339 if (y1 > bm->width) y1 = bm->width;
340 if (y2 > bm->width) y2 = bm->width;
343 for (x = x1; x < x2; x++)
344 for (y = y1; y < y2; y++)
350 * Imposta un rettangolo di clipping per il disegno nella bitmap
352 void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
358 ASSERT(maxx < bm->width);
359 ASSERT(maxy < bm->height);
366 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
367 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
372 #if CONFIG_GFX_VCOORDS
374 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
375 * al rettangolo di clipping della bitmap.
377 void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
384 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin) / (vcoord_t)(x2 - x1); /* +1 */
385 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin) / (vcoord_t)(y2 - y1); /* +1 */
387 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
388 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
394 * Transform a coordinate from the current reference system to a
395 * pixel offset within the bitmap.
397 coord_t gfx_TransformX(Bitmap *bm, vcoord_t x)
399 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
403 * Transform a coordinate from the current reference system to a
404 * pixel offset within the bitmap.
406 coord_t gfx_TransformY(Bitmap *bm, vcoord_t y)
408 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
413 * Draw a line from (x1;y1) to (x2;y2)
415 void gfx_VDrawLine(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
418 gfx_TransformX(bm, x1), gfx_TransformY(bm, y1),
419 gfx_TransformY(bm, x2), gfx_TransformY(bm, y2));
421 #endif /* CONFIG_GFX_VCOORDS */