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