Replace if/else with continue to reduce indentation level.
[bertos.git] / mware / gfx.c
1 /*!
2  * \file
3  * <!--
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.
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.3  2004/08/04 03:16:59  bernie
20  * Switch to new DevLib CONFIG_ convention.
21  *
22  * Revision 1.2  2004/06/03 11:27:09  bernie
23  * Add dual-license information.
24  *
25  */
26
27 #include "gfx.h"
28 #include "config.h"
29 #include <drv/kdebug.h>
30
31 #include <string.h>
32
33
34 /*!
35  * Plot a point in bitmap.
36  * \note bm is evaluated twice
37  */
38 #define BM_PLOT(bm, x, y) \
39         ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) |= 1 << ((y) % 8) )
40
41 /*!
42  * Clear a point in bitmap.
43  * \note bm is evaluated twice
44  */
45 #define BM_CLEAR(bm, x, y) \
46         ( *((bm)->raster + ((y) / 8) * (bm)->width + (x)) &= ~(1 << ((y) % 8)) )
47
48 /*! Swap a with b */
49 #define SWAP(a, b) \
50         do { \
51                 (void)(&a == &b); /* type check */ \
52                 typeof(a) tmp; \
53                 tmp = (a); \
54                 (a) = (b); \
55                 (b) = tmp; \
56         } while (0)
57
58
59 /*!
60  * Initialize a Bitmap structure with the provided parameters.
61  */
62 void gfx_InitBitmap(Bitmap *bm, uint8_t *raster, coord_t w, coord_t h)
63 {
64         bm->raster = raster;
65         bm->width = w;
66         bm->height = h;
67         bm->penX = 0;
68         bm->penY = 0;
69 }
70
71
72 /*!
73  * Clear the whole bitmap surface to all zeros
74  */
75 void gfx_ClearBitmap(Bitmap *bm)
76 {
77         memset(bm->raster, 0, (bm->width * bm->height) / 8);
78 }
79
80
81 /*!
82  * Copy a raster picture located in program memory in the bitmap.
83  * The size of the raster to copy *must* be the same of the raster bitmap.
84  */
85 void gfx_blitBitmap_P(Bitmap *bm, const prog_uchar *raster)
86 {
87         memcpy_P(bm->raster, raster, bm->height/8 * bm->width);
88 }
89
90
91 void gfx_DrawLine(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
92 {
93         int x, y, e, len, adx, ady, signx, signy;
94
95
96 #ifdef CONFIG_GFX_CLIPPING
97         /* FIXME: broken */
98
99         #define XMIN 0
100         #define YMIN 0
101         #define XMAX (bm->width - 1)
102         #define YMAX (bm->height - 1)
103
104         /* Clipping */
105         if (x1 < XMIN)
106         {
107                 y1 = y2 - ((x2 - XMIN) * (y2 - y1)) / (x2 - x1);
108                 x1 = XMIN;
109         }
110         if (y1 < YMIN)
111         {
112                 x1 = x2 - ((y2 - YMIN) * (x2 - x1)) / (y2 - y1);
113                 y1 = YMIN;
114         }
115         if (x2 < XMIN)
116         {
117                 y2 = y2 - ((XMIN - x1) * (y2 - y1)) / (x2 - x1);
118                 x2 = XMIN;
119         }
120         if (y2 < YMIN)
121         {
122                 x2 = x2 - ((YMIN - y1) * (x2 - x1)) / (y2 - y1);
123                 y2 = YMIN;
124         }
125
126         if (x1 > XMAX)
127         {
128                 y1 = ((x2 - XMAX) * (y2 - y1)) / (x2 - x1);
129                 x1 = XMAX;
130         }
131         if (y1 > YMAX)
132         {
133                 x1 = ((y2 - YMAX) * (x2 - x1)) / (y2 - y1);
134                 y1 = YMAX;
135         }
136         if (x2 > XMAX)
137         {
138                 y2 = ((XMAX - x1) * (y2 - y1)) / (x2 - x1);
139                 x2 = XMAX;
140         }
141         if (y2 > YMAX)
142         {
143                 x2 = ((YMAX - y1) * (x2 - x1)) / (y2 - y1);
144                 y2 = YMAX;
145         }
146
147         #undef XMIN
148         #undef YMIN
149         #undef XMAX
150         #undef YMAX
151
152 #endif /* CONFIG_GRAPH_CLIPPING */
153
154
155         if (x2 > x1)
156         {
157                 /* left to right */
158                 signx = +1;
159                 adx = x2 - x1;
160         }
161         else
162         {
163                 /* right to left */
164                 signx = -1;
165                 adx = x1 - x2;
166         }
167
168         if (y2 > y1)
169         {
170                 /* top to bottom */
171                 signy = +1;
172                 ady = y2 - y1;
173         }
174         else
175         {
176                 /* bottom to top */
177                 signy = -1;
178                 ady = y1 - y2;
179         }
180
181         x = x1;
182         y = y1;
183
184         if (adx > ady)
185         {
186                 /* X-major line (octants 1/4/5/8) */
187
188                 len = adx;
189                 e = -adx;
190                 while (len--)
191                 {
192                         /* Sanity check */
193                         if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
194                                 BM_PLOT(bm, x, y);
195                         x += signx;
196                         e += ady;
197                         if (e >= 0)
198                         {
199                                 y += signy;
200                                 e -= adx;
201                         }
202                 }
203         }
204         else
205         {
206                 /* Y-major line (octants 2/3/6/7) */
207
208                 len = ady;
209                 e = -ady;
210                 while (len--)
211                 {
212                         /* Sanity check */
213                         if ((x >= 0) && (x < bm->width) && (y >= 0) && (y < bm->height))
214                                 BM_PLOT(bm, x, y);
215                         y += signy;
216                         e += adx;
217                         if (e >= 0)
218                         {
219                                 x += signx;
220                                 e -= ady;
221                         }
222                 }
223         }
224 }
225
226
227 void gfx_MoveTo(Bitmap *bm, coord_t x, coord_t y)
228 {
229         bm->penX = x;
230         bm->penY = y;
231 }
232
233
234 void gfx_LineTo(Bitmap *bm, coord_t x, coord_t y)
235 {
236         gfx_DrawLine(bm, bm->penX, bm->penY, x, y);
237         gfx_MoveTo(bm, x, y);
238 }
239
240
241 /*!
242  * Draw a filled rectangle.
243  * \note The bottom-right border of the rectangle is not drawn.
244  */
245 void gfx_FillRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
246 {
247         coord_t x, y;
248
249         /* Sort coords */
250         if (x1 > x2) SWAP(x1, x2);
251         if (y1 > y2) SWAP(y1, y2);
252
253         /* Clip rect to bitmap bounds */
254         if (x1 < 0)             x1 = 0;
255         if (x2 < 0)             x2 = 0;
256         if (x1 > bm->width)     x1 = bm->width;
257         if (x2 > bm->width)     x2 = bm->width;
258         if (y1 < 0)             y1 = 0;
259         if (y2 < 0)             y2 = 0;
260         if (y1 > bm->width)     y1 = bm->width;
261         if (y2 > bm->width)     y2 = bm->width;
262
263         /* Draw rectangle */
264         for (x = x1; x < x2; x++)
265                 for (y = y1; y < y2; y++)
266                         BM_PLOT(bm, x, y);
267 }
268
269
270 /*!
271  * Draw an empty rectangle.
272  * \note The bottom-right border of the rectangle is not drawn.
273  */
274 void gfx_DrawRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
275 {
276         /* Sort coords */
277         if (x1 > x2) SWAP(x1, x2);
278         if (y1 > y2) SWAP(y1, y2);
279
280         /* Draw rectangle */
281         gfx_DrawLine(bm, x1,   y1,   x2-1, y1);
282         gfx_DrawLine(bm, x2-1, y1,   x2-1, y2-1);
283         gfx_DrawLine(bm, x2-1, y2-1, x1,   y2-1);
284         gfx_DrawLine(bm, x1,   y2-1, x1,   y1);
285 }
286
287
288 /*!
289  * Clear a rectangular area.
290  * \note The bottom-right border of the rectangle is not drawn.
291  */
292 void gfx_ClearRect(Bitmap *bm, coord_t x1, coord_t y1, coord_t x2, coord_t y2)
293 {
294         coord_t x, y;
295
296         /* Sort coords */
297         if (x1 > x2) SWAP(x1, x2);
298         if (y1 > y2) SWAP(y1, y2);
299
300         /* Clip rect to bitmap bounds */
301         if (x1 < 0)             x1 = 0;
302         if (x2 < 0)             x2 = 0;
303         if (x1 > bm->width)     x1 = bm->width;
304         if (x2 > bm->width)     x2 = bm->width;
305         if (y1 < 0)             y1 = 0;
306         if (y2 < 0)             y2 = 0;
307         if (y1 > bm->width)     y1 = bm->width;
308         if (y2 > bm->width)     y2 = bm->width;
309
310         /* Draw rectangle */
311         for (x = x1; x < x2; x++)
312                 for (y = y1; y < y2; y++)
313                         BM_CLEAR(bm, x, y);
314 }
315
316
317 /*!
318  * Imposta un rettangolo di clipping per il disegno nella bitmap
319  */
320 void gfx_SetClipRect(Bitmap *bm, coord_t minx, coord_t miny, coord_t maxx, coord_t maxy)
321 {
322         ASSERT(minx < maxx);
323         ASSERT(miny < maxy);
324         ASSERT(miny >= 0);
325         ASSERT(minx >= 0);
326         ASSERT(maxx < bm->width);
327         ASSERT(maxy < bm->height);
328
329         bm->cr.xmin = minx;
330         bm->cr.ymin = miny;
331         bm->cr.xmax = maxx;
332         bm->cr.ymax = maxy;
333
334 /*      DB(kprintf("cr.xmin = %d, cr.ymin = %d, cr.xmax = %d, cr.ymax = %d\n",
335                 bm->cr.xMin, bm->cr.ymin, bm->cr.xmax, bm->cr.ymax);)
336 */
337 }
338
339
340 #if CONFIG_GFX_VCOORDS
341 /*!
342  * Imposta gli estremi del sistema di coordinate cartesiane rispetto
343  * al rettangolo di clipping della bitmap.
344  */
345 void gfx_SetViewRect(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
346 {
347         ASSERT(x1 != x2);
348         ASSERT(y1 != y2);
349
350         bm->orgX    = x1;
351         bm->orgY    = y1;
352         bm->scaleX  = (vcoord_t)(bm->cr.xmax - bm->cr.xmin) / (vcoord_t)(x2 - x1);   /* +1 */
353         bm->scaleY  = (vcoord_t)(bm->cr.ymax - bm->cr.ymin) / (vcoord_t)(y2 - y1);   /* +1 */
354
355 /*      DB(kprintf("orgX = %f, orgY = %f, scaleX = %f, scaleY = %f\n",
356                 bm->orgX, bm->orgY, bm->scaleX, bm->scaleY);)
357 */
358 }
359
360
361 /*!
362  * Transform a coordinate from the current reference system to a
363  * pixel offset within the bitmap.
364  */
365 coord_t gfx_TransformX(Bitmap *bm, vcoord_t x)
366 {
367         return bm->cr.xmin + (coord_t)((x - bm->orgX) * bm->scaleX);
368 }
369
370 /*!
371  * Transform a coordinate from the current reference system to a
372  * pixel offset within the bitmap.
373  */
374 coord_t gfx_TransformY(Bitmap *bm, vcoord_t y)
375 {
376         return bm->cr.ymin + (coord_t)((y - bm->orgY) * bm->scaleY);
377 }
378
379
380 /*!
381  * Draw a line from (x1;y1) to (x2;y2)
382  */
383 void gfx_VDrawLine(Bitmap *bm, vcoord_t x1, vcoord_t y1, vcoord_t x2, vcoord_t y2)
384 {
385         gfx_DrawLine(bm,
386                 gfx_TransformX(bm, x1), gfx_TransformY(bm, y1),
387                 gfx_TransformY(bm, x2), gfx_TransformY(bm, y2));
388 }
389 #endif /* CONFIG_GFX_VCOORDS */