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.2 2004/06/03 11:27:09 bernie
20 * Add dual-license information.
26 #include <drv/kdebug.h>
32 * Plot a point in bitmap.
33 * \note bm is evaluated twice
35 #define BM_PLOT(bm, x, y) \
36 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
39 * Clear a point in bitmap.
40 * \note bm is evaluated twice
42 #define BM_CLEAR(bm, x, y) \
43 ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
48 (void)(&a == &b); /* type check */ \
57 * Initialize a Bitmap structure with the provided parameters.
59 void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
70 * Clear the whole bitmap surface to all zeros
72 void gfx_ClearBitmap(Bitmap *bm)
74 memset(bm->raster, 0, (bm->width * bm->height) / 8);
79 * Copy a raster picture located in program memory in the bitmap.
80 * The size of the raster to copy *must* be the same of the raster bitmap.
82 void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster)
84 memcpy_P(bm->raster, raster, bm->height/8 * bm->width);
88 void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
90 int x, y, e, len, adx, ady, signx, signy;
93 #ifdef CONFIG_GFX_CLIPPING
98 #define XMAX (bm->width - 1)
99 #define YMAX (bm->height - 1)
104 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
109 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
114 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
119 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
125 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
130 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
135 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
140 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
149 #endif /* CONFIG_GRAPH_CLIPPING */
183 /* X-major line (octants 1/4/5/8) */
190 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
203 /* Y-major line (octants 2/3/6/7) */
210 if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
224 void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y)
231 void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y)
233 gfx_DrawLine(bm, bm->penX, bm->penY, x, y);
234 gfx_MoveTo(bm, x, y);
239 * Draw a filled rectangle.
240 * \note The bottom-right border of the rectangle is not drawn.
242 void gfx_FillRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
247 if (x1 > x2) SWAP(x1, x2);
248 if (y1 > y2) SWAP(y1, y2);
250 /* Clip rect to bitmap bounds */
253 if (x1 > bm->width) x1 = bm->width;
254 if (x2 > bm->width) x2 = bm->width;
257 if (y1 > bm->width) y1 = bm->width;
258 if (y2 > bm->width) y2 = bm->width;
261 for (x = x1; x < x2; x++)
262 for (y = y1; y < y2; y++)
268 * Draw an empty rectangle.
269 * \note The bottom-right border of the rectangle is not drawn.
271 void gfx_DrawRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
274 if (x1 > x2) SWAP(x1, x2);
275 if (y1 > y2) SWAP(y1, y2);
278 gfx_DrawLine(bm, x1, y1, x2-1, y1);
279 gfx_DrawLine(bm, x2-1, y1, x2-1, y2-1);
280 gfx_DrawLine(bm, x2-1, y2-1, x1, y2-1);
281 gfx_DrawLine(bm, x1, y2-1, x1, y1);
286 * Clear a rectangular area.
287 * \note The bottom-right border of the rectangle is not drawn.
289 void gfx_ClearRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
294 if (x1 > x2) SWAP(x1, x2);
295 if (y1 > y2) SWAP(y1, y2);
297 /* Clip rect to bitmap bounds */
300 if (x1 > bm->width) x1 = bm->width;
301 if (x2 > bm->width) x2 = bm->width;
304 if (y1 > bm->width) y1 = bm->width;
305 if (y2 > bm->width) y2 = bm->width;
308 for (x = x1; x < x2; x++)
309 for (y = y1; y < y2; y++)
315 * Imposta un rettangolo di clipping per il disegno nella bitmap
317 void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
323 ASSERT(maxx < bm->width);
324 ASSERT(maxy < bm->height);
331 /* DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
332 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
337 #ifdef CONFIG_LCD_VCOORDS
339 * Imposta gli estremi del sistema di coordinate cartesiane rispetto
340 * al rettangolo di clipping della bitmap.
342 void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
349 bm->scaleX = (vcoord_t)(bm->cr.xmax - bm->cr.xmin) / (vcoord_t)(x2 - x1); /* +1 */
350 bm->scaleY = (vcoord_t)(bm->cr.ymax - bm->cr.ymin) / (vcoord_t)(y2 - y1); /* +1 */
352 /* DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
353 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
359 * Transform a coordinate from the current reference system to a
360 * pixel offset within the bitmap.
362 coord_t gfx_TransformX(Bitmap *bm, vcoord_t x)
364 return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
368 * Transform a coordinate from the current reference system to a
369 * pixel offset within the bitmap.
371 coord_t gfx_TransformY(Bitmap *bm, vcoord_t y)
373 return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
378 * Draw a line from (x1;y1) to (x2;y2)
380 void gfx_VDrawLine(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
383 gfx_TransformX(bm, x1), gfx_TransformY(bm, y1),
384 gfx_TransformY(bm, x2), gfx_TransformY(bm, y2));
386 #endif /* CONFIG_LCD_VCOORDS */