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