IRQ_SAVE_DISABLE(): Really block signals.
[bertos.git] / gfx / gfx.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  * \author Stefano Fedrigo <aleph@develer.com>
13  *
14  * \brief General pourpose graphics routines
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.2  2005/11/04 18:17:45  bernie
20  *#* Fix header guards and includes for new location of gfx module.
21  *#*
22  *#* Revision 1.1  2005/11/04 18:11:35  bernie
23  *#* Move graphics stuff from mware/ to gfx/.
24  *#*
25  *#* Revision 1.14  2005/11/04 16:20:02  bernie
26  *#* Fix reference to README.devlib in header.
27  *#*
28  *#* Revision 1.13  2005/04/11 19:10:28  bernie
29  *#* Include top-level headers from cfg/ subdir.
30  *#*
31  *#* Revision 1.12  2005/03/01 23:26:45  bernie
32  *#* Use new CPU-neutral program-memory API.
33  *#*/
34
35 #include <gfx/gfx.h>
36 #include <cfg/config.h>  /* CONFIG_GFX_CLIPPING */
37 #include <cfg/debug.h>
38 #include <cfg/cpu.h>     /* CPU_HARVARD */
39 #include <cfg/macros.h>  /* SWAP() */
40
41 #include <string.h>
42
43
44 /*!
45  * Plot a point in bitmap \a bm.
46  *
47  * \note bm is evaluated twice
48  */
49 #define BM_PLOT(bm, x, y) \
50         ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
51
52 /*!
53  * Clear a point in bitmap \a bm.
54  *
55  * \note bm is evaluated twice
56  */
57 #define BM_CLEAR(bm, x, y) \
58         ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
59
60 /*!
61  * Set a point in bitmap \a bm to the specified color.
62  *
63  * \note bm is evaluated twice
64  * \note This macro is somewhat slower than BM_PLOT and BM_CLEAR.
65  * \see BM_PLOT BM_CLEAR
66  */
67 #define BM_DRAWPIXEL(bm, x, y, fg_pen) \
68         do { \
69                 uint8_t *p = (bm)->raster + ((y) / 8) * (bm)->width + (x); \
70                 uint8_t mask = 1 << ((y) % 8); \
71                 *p = (*p & ~mask) | ((fg_pen) ? mask : 0); \
72         } while (0)
73
74 /*!
75  * Initialize a Bitmap structure with the provided parameters.
76  *
77  * \note The pen position is reset to the origin.
78  */
79 void gfx_bitmapInit(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
80 {
81         bm->raster = raster;
82         bm->width = w;
83         bm->height = h;
84         bm->penX = 0;
85         bm->penY = 0;
86 }
87
88
89 /*!
90  * Clear the whole bitmap surface to the background color.
91  *
92  * \note This function does \b not update the current pen position
93  */
94 void gfx_bitmapClear(Bitmap *bm)
95 {
96         memset(bm->raster, 0, (bm->width * bm->height) / 8);
97 }
98
99
100 #if CPU_HARVARD
101
102 #include <avr/pgmspace.h> /* FIXME: memcpy_P() */
103
104 /*!
105  * Copy a raster picture located in program memory in the bitmap.
106  * The size of the raster to copy *must* be the same of the raster bitmap.
107  *
108  * \note This function does \b not update the current pen position
109  */
110 void gfx_blit_P(Bitmap *bm, const pgm_uint8_t *raster)
111 {
112         memcpy_P(bm->raster, raster, (bm->height / 8) * bm->width);
113 }
114 #endif /* CPU_HARVARD */
115
116
117 /*!
118  * Draw a line on the bitmap \a bm using the specified start and end
119  * coordinates.
120  *
121  * \note This function does \b not update the current pen position.
122  *
123  * \todo Optimize for vertical and horiziontal lines.
124  */
125 void gfx_line(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
126 {
127         int x, y, e, len, adx, ady, signx, signy;
128
129
130 #if CONFIG_GFX_CLIPPING
131         /* FIXME: broken */
132
133         #define XMIN 0
134         #define YMIN 0
135         #define XMAX (bm->width - 1)
136         #define YMAX (bm->height - 1)
137
138         /* Clipping */
139         if (x1 < XMIN)
140         {
141                 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
142                 x1 = XMIN;
143         }
144         if (y1 < YMIN)
145         {
146                 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
147                 y1 = YMIN;
148         }
149         if (x2 < XMIN)
150         {
151                 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
152                 x2 = XMIN;
153         }
154         if (y2 < YMIN)
155         {
156                 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
157                 y2 = YMIN;
158         }
159
160         if (x1 > XMAX)
161         {
162                 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
163                 x1 = XMAX;
164         }
165         if (y1 > YMAX)
166         {
167                 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
168                 y1 = YMAX;
169         }
170         if (x2 > XMAX)
171         {
172                 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
173                 x2 = XMAX;
174         }
175         if (y2 > YMAX)
176         {
177                 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
178                 y2 = YMAX;
179         }
180
181         #undef XMIN
182         #undef YMIN
183         #undef XMAX
184         #undef YMAX
185
186 #endif /* CONFIG_GFX_CLIPPING */
187
188
189         if (x2 > x1)
190         {
191                 /* left to right */
192                 signx = +1;
193                 adx = x2 - x1;
194         }
195         else
196         {
197                 /* right to left */
198                 signx = -1;
199                 adx = x1 - x2;
200         }
201
202         if (y2 > y1)
203         {
204                 /* top to bottom */
205                 signy = +1;
206                 ady = y2 - y1;
207         }
208         else
209         {
210                 /* bottom to top */
211                 signy = -1;
212                 ady = y1 - y2;
213         }
214
215         x = x1;
216         y = y1;
217
218         if (adx > ady)
219         {
220                 /* X-major line (octants 1/4/5/8) */
221
222                 len = adx;
223                 e = -adx;
224                 while (len--)
225                 {
226                         /* Sanity check */
227                         if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
228                                 BM_PLOT(bm, x, y);
229                         x += signx;
230                         e += ady;
231                         if (e >= 0)
232                         {
233                                 y += signy;
234                                 e -= adx;
235                         }
236                 }
237         }
238         else
239         {
240                 /* Y-major line (octants 2/3/6/7) */
241
242                 len = ady;
243                 e = -ady;
244                 while (len--)
245                 {
246                         /* Sanity check */
247                         if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
248                                 BM_PLOT(bm, x, y);
249                         y += signy;
250                         e += adx;
251                         if (e >= 0)
252                         {
253                                 x += signx;
254                                 e -= ady;
255                         }
256                 }
257         }
258 }
259
260
261 /*!
262  * Move the current pen position to the specified coordinates.
263  */
264 void gfx_moveTo(Bitmap *bm, coord_t x, coord_t y)
265 {
266         bm->penX = x;
267         bm->penY = y;
268 }
269
270 /*!
271  * Draw a line from the current pen position to the new coordinates.
272  *
273  * \note This function moves the current pen position to the
274  *       new coordinates.
275  */
276 void gfx_lineTo(Bitmap *bm, coord_t x, coord_t y)
277 {
278         gfx_line(bm, bm->penX, bm->penY, x, y);
279         gfx_moveTo(bm, x, y);
280 }
281
282
283 /*!
284  * Draw an the outline of an hollow rectangle.
285  *
286  * \note The bottom-right border of the rectangle is not drawn.
287  * \note This function does \b not update the current pen position
288  */
289 void gfx_rectDraw(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
290 {
291         /* Sort coords (needed for correct bottom-right semantics) */
292         if (x1 > x2) SWAP(x1, x2);
293         if (y1 > y2) SWAP(y1, y2);
294
295         /* Draw rectangle */
296         gfx_line(bm, x1,   y1,   x2-1, y1);
297         gfx_line(bm, x2-1, y1,   x2-1, y2-1);
298         gfx_line(bm, x2-1, y2-1, x1,   y2-1);
299         gfx_line(bm, x1,   y2-1, x1,   y1);
300 }
301
302
303 /*!
304  * Fill a rectangular area with \a color.
305  *
306  * \note The bottom-right border of the rectangle is not drawn.
307  *
308  * \note This function does \b not update the current pen position
309  */
310 void gfx_rectFillC(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2, uint8_t color)
311 {
312         coord_t x, y;
313
314         /* Sort coords */
315         if (x1 > x2) SWAP(x1, x2);
316         if (y1 > y2) SWAP(y1, y2);
317
318         /* Clip rect to bitmap bounds */
319         if (x1 < 0)             x1 = 0;
320         if (x2 < 0)             x2 = 0;
321         if (x1 > bm->width)     x1 = bm->width;
322         if (x2 > bm->width)     x2 = bm->width;
323         if (y1 < 0)             y1 = 0;
324         if (y2 < 0)             y2 = 0;
325         if (y1 > bm->width)     y1 = bm->width;
326         if (y2 > bm->width)     y2 = bm->width;
327
328         /*
329          * Draw rectangle
330          * NOTE: Code paths are duplicated for efficiency
331          */
332         if (color) /* fill */
333         {
334                 for (x = x1; x < x2; x++)
335                         for (y = y1; y < y2; y++)
336                                 BM_PLOT(bm, x, y);
337         }
338         else /* clear */
339         {
340                 for (x = x1; x < x2; x++)
341                         for (y = y1; y < y2; y++)
342                                 BM_CLEAR(bm, x, y);
343         }
344 }
345
346
347 /*!
348  * Draw a filled rectangle.
349  *
350  * \note The bottom-right border of the rectangle is not drawn.
351  *
352  * \note This function does \b not update the current pen position
353  */
354 void gfx_rectFill(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
355 {
356         gfx_rectFillC(bm, x1, y1, x2, y2, 0xFF);
357 }
358
359
360 /*!
361  * Clear a rectangular area.
362  *
363  * \note The bottom-right border of the rectangle is not drawn.
364  *
365  * \note This function does \b not update the current pen position
366  */
367 void gfx_rectClear(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
368 {
369         gfx_rectFillC(bm, x1, y1, x2, y2, 0x00);
370 }
371
372
373 /*!
374  * Imposta un rettangolo di clipping per il disegno nella bitmap.
375  */
376 void gfx_setClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
377 {
378         ASSERT(minx < maxx);
379         ASSERT(miny < maxy);
380         ASSERT(miny >= 0);
381         ASSERT(minx >= 0);
382         ASSERT(maxx <= bm->width);
383         ASSERT(maxy <= bm->height);
384
385         bm->cr.xmin = minx;
386         bm->cr.ymin = miny;
387         bm->cr.xmax = maxx;
388         bm->cr.ymax = maxy;
389
390 /*      DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
391                 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
392 */
393 }
394
395
396 #if CONFIG_GFX_VCOORDS
397 /*!
398  * Imposta gli estremi del sistema di coordinate cartesiane rispetto
399  * al rettangolo di clipping della bitmap.
400  */
401 void gfx_setViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
402 {
403         ASSERT(x1 != x2);
404         ASSERT(y1 != y2);
405
406         bm->orgX    = x1;
407         bm->orgY    = y1;
408         bm->scaleX  = (vcoord_t)(bm->cr.xmax - bm->cr.xmin - 1) / (vcoord_t)(x2 - x1);
409         bm->scaleY  = (vcoord_t)(bm->cr.ymax - bm->cr.ymin - 1) / (vcoord_t)(y2 - y1);
410
411 /*      DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
412                 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
413 */
414 }
415
416
417 /*!
418  * Transform a coordinate from the current reference system to a
419  * pixel offset within the bitmap.
420  */
421 coord_t gfx_transformX(Bitmap *bm, vcoord_t x)
422 {
423         return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
424 }
425
426 /*!
427  * Transform a coordinate from the current reference system to a
428  * pixel offset within the bitmap.
429  */
430 coord_t gfx_transformY(Bitmap *bm, vcoord_t y)
431 {
432         return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
433 }
434
435
436 /*!
437  * Draw a line from (x1;y1) to (x2;y2).
438  */
439 void gfx_vline(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
440 {
441         gfx_line(bm,
442                 gfx_transformX(bm, x1), gfx_transformY(bm, y1),
443                 gfx_transformY(bm, x2), gfx_transformY(bm, y2));
444 }
445 #endif /* CONFIG_GFX_VCOORDS */