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