/
uVGA_gfx.cpp
1370 lines (1144 loc) · 24.1 KB
/
uVGA_gfx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of uVGA library.
uVGA library is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
uVGA library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with uVGA library. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2017 Eric PREVOTEAU
Original Author: Eric PREVOTEAU <digital.or@gmail.com>
*/
#include "uVGA.h"
#define dump(v) {Serial.print(#v ":"); Serial.println(v);}
#define NO_DMA_GFX
#define FAST_HLINE
// clip X to inside horizontal range
inline int uVGA::clip_x(int x)
{
if(x < 0)
return 0;
if(x >= fb_width)
return fb_width - 1;
return x;
}
// clip Y to inside vertical range
inline int uVGA::clip_y(int y)
{
if(y < 0)
return 0;
if(y >= fb_height)
return fb_height - 1;
return y;
}
// wait for GFX dma to become free
inline void uVGA::wait_idle_gfx_dma()
{
#ifndef NO_DMA_GFX
while(edma->ERQ & (1 << gfx_dma_num));
#endif
}
// clear screen with an optional color
void uVGA::clear(int color)
{
uVGA::fillRect(0, 0, fb_width - 1, fb_height - 1, color);
}
// draw a single pixel. If the pixel is out of screen, it is not displayed
void uVGA::drawPixel(int x, int y, int color)
{
// pixel outside of visible area ?
if( (clip_x(x) != x)
|| (clip_y(y) != y)
)
return;
wait_idle_gfx_dma();
drawPixelFast(x, y, color);
}
// draw a single pixel WITHOUT performing any clipping test
inline void uVGA::drawPixelFast(int x, int y, int color)
{
frame_buffer[y * fb_row_stride + x] = color;
}
int uVGA::getPixel(int x, int y)
{
return _getPixel(x, y);
}
inline int uVGA::_getPixel(int x, int y)
{
// pixel outside of visible area ?
if( (clip_x(x) != x)
|| (clip_y(y) != y)
)
return 0;
wait_idle_gfx_dma();
return getPixelFast(x, y);
}
inline int uVGA::getPixelFast(int x, int y)
{
return *(frame_buffer + y * fb_row_stride + x);
}
// draw a horizontal line pixel with clipping
void uVGA::drawHLine(int y, int x1, int x2, int color)
{
int nx1;
int nx2;
// line out of screen ?
if(clip_y(y) != y)
return;
nx1 = clip_x(x1);
nx2 = clip_x(x2);
wait_idle_gfx_dma();
if(x1 <= x2)
drawHLineFast(y, nx1, nx2, color);
else
drawHLineFast(y, nx2, nx1, color);
}
// draw a horizontal line pixel WITHOUT performing any clipping test
// x1 always <= x2
inline void uVGA::drawHLineFast(int y, int x1, int x2, int color)
{
#ifdef NO_DMA_GFX
uint8_t *ptr = frame_buffer + y * fb_row_stride + x1;
#ifdef FAST_HLINE
int nb = x2 - x1 + 1;
// copy until address becomes a multiple of 4
while( (((uint32_t)ptr) & 3) && (nb > 0))
{
*ptr++ = color;
nb--;
}
if(nb)
{
uint32_t c = color & 0xFF;
c = c | (c << 8);
c = c | (c << 16);
while( nb >= 4)
{
*((uint32_t*)ptr) = c;
ptr += 4;
nb -= 4;
}
while(nb > 0)
{
*ptr++ = color;
nb--;
}
}
#else
while(x1 <= x2)
{
*ptr++ = color;
x1++;
}
#endif
#else
gfx_dma_color[0] = color;
gfx_dma->SADDR = gfx_dma_color;
gfx_dma->SOFF = 0;
gfx_dma->ATTR = DMA_TCD_ATTR_SSIZE(DMA_TCD_ATTR_SIZE_8BIT) | DMA_TCD_ATTR_DSIZE(DMA_TCD_ATTR_SIZE_8BIT);
gfx_dma->NBYTES = x2 - x1 + 1;
gfx_dma->SLAST = 0;
gfx_dma->DADDR = frame_buffer + y * fb_row_stride + x1;
gfx_dma->DOFF = 1;
gfx_dma->CITER = 1;
gfx_dma->BITER = 1;
gfx_dma->CSR = DMA_TCD_CSR_DREQ;
edma->SERQ = gfx_dma_num;
#endif
}
// draw a vertical line pixel with clipping
void uVGA::drawVLine(int x, int y1, int y2, int color)
{
int ny1;
int ny2;
// line out of screen ?
if(clip_x(x) != x)
return;
ny1 = clip_y(y1);
ny2 = clip_y(y2);
wait_idle_gfx_dma();
if(y1 <= y2)
drawVLineFast(x, ny1, ny2, color);
else
drawVLineFast(x, ny2, ny1, color);
}
// draw a vertical line pixel WITHOUT performing any clipping test
// y1 always <= y2
inline void uVGA::drawVLineFast(int x, int y1, int y2, int color)
{
#ifdef NO_DMA_GFX
uint8_t *ptr = frame_buffer + y1 * fb_row_stride + x;
while(y1 <= y2)
{
*ptr = color;
ptr += fb_row_stride;
y1++;
}
#else
gfx_dma_color[0] = color;
gfx_dma->SADDR = gfx_dma_color;
gfx_dma->SOFF = 0;
gfx_dma->ATTR = DMA_TCD_ATTR_SSIZE(DMA_TCD_ATTR_SIZE_8BIT) | DMA_TCD_ATTR_DSIZE(DMA_TCD_ATTR_SIZE_8BIT);
gfx_dma->NBYTES = y2 - y1 + 1;
gfx_dma->SLAST = 0;
gfx_dma->DADDR = frame_buffer + y1 * fb_row_stride + x;
gfx_dma->DOFF = fb_row_stride;
gfx_dma->CITER = 1;
gfx_dma->BITER = 1;
gfx_dma->CSR = DMA_TCD_CSR_DREQ;
edma->SERQ = gfx_dma_num;
#endif
}
void uVGA::fillRect(int x0, int y0, int x1, int y1, int color)
{
/*
int width;
int height;
*/
int t;
x0 = clip_x(x0);
y0 = clip_y(y0);
x1 = clip_x(x1);
y1 = clip_y(y1);
// increase speed if the rectangle is a single pixel, horizontal or vertical line
if( (x0 == x1) )
{
if(y0 == y1)
{
wait_idle_gfx_dma();
return drawPixelFast(x0, y0, color);
}
else
{
wait_idle_gfx_dma();
if(y0 < y1)
return drawVLineFast(x0, y0, y1, color);
else
return drawVLineFast(x0, y1, y0, color);
}
}
else if(y0 == y1)
{
wait_idle_gfx_dma();
if(x0 < x1)
return drawHLineFast(y0, x0, x1, color);
else
return drawHLineFast(y0, x1, x0, color);
}
if( x0 > x1 )
{
t = x0;
x0 = x1;
x1 = t;
}
if( y0 > y1 )
{
t = y0;
y0 = y1;
y1 = t;
}
while(y0 <= y1)
{
wait_idle_gfx_dma();
drawHLineFast(y0, x0, x1, color);
y0++;
}
/*
width = abs(x1 - x0);
height = abs(y1 - y0);
gfx_dma_color = color;
wait_idle_gfx_dma();
// lets program DMA to perform the task and free the CPU
// source is a single pixel of the given color
gfx_dma->SADDR = &gfx_dma_color;
gfx_dma->SOFF = 0; // stay on this pixel
gfx_dma->ATTR_SRC = 0 // source data size = 1 byte
gfx_dma->NBYTES_MLOFFYES = DMA_TCD_NBYTES_DMLOE | // at end of minor loop, adjust destination address (offset)
DMA_TCD_NBYTES_MLOFFYES_MLOFF(fb_row_stride - width) | // adjustment is nb bytes in frame buffer line - copied "color" bytes
DMA_TCD_NBYTES_MLOFFYES_NBYTES(width) ; // each minor loop transfert copy a line of the rectangle (width byte)
gfx_dma->SLAST = 0; // not pointer correction at end of major loop
gfx_dma->DADDR = frame_buffer + fb_row_stride * y0 + x0; // destination is pixel (x0,y0) in the frame buffer
gfx_dma->DOFF = 1; // after each
gfx_dma->ATTR_DST = 0; // destination data size = 1 byte
gfx_dma->CITER = width * height; // major loop should copy the whole rectangle surface
gfx_dma->DLASTSGA = 0; // no scatter/gather mode
gfx_dma->CSR = 0;
gfx_dma->BITER = gfx_dma->CITER;
nécessite CR[EMLM]=1
est-ce que ça ne pertubera pas le DMA vidéo ???
mettre DMA_DCHPRIx[ECP] = 1
*/
}
// bitmap format must be the same as modeline.img_color_mode
void uVGA::drawBitmap(int16_t x_pos, int16_t y_pos, uint8_t *bitmap, int16_t bitmap_width, int16_t bitmap_height)
{
int fx;
int fy;
int fw;
int fh;
int bx;
int by;
int off_x, off_y;
uint8_t *bitmap_ptr;
fx = clip_x(x_pos);
// X position outside of image (right of image)
if(fx < x_pos)
return;
// compute the number of pixels to skip at the beginning of each bitmap line and the number of pixel per line to copy
if(fx > x_pos)
{
bx = fx - x_pos;
fw = bitmap_width - bx;
// X position outside of image (right of image)
if(fw <= 0)
return;
}
else
{
bx = 0;
if( (fx + bitmap_width) >= fb_width)
{
fw = fb_width - fx;
}
else
{
fw = bitmap_width;
}
}
fy = clip_y(y_pos);
// Y position outside of image (bottom of image)
if(fy < y_pos)
return;
// compute the number of lines to skip at the beginning of bitmap and the number of lines to copy
if(fy > y_pos)
{
by = fy - y_pos;
fh = bitmap_height - by;
// Y position outside of image (right of image)
if(fh <= 0)
return;
}
else
{
by = 0;
if( (fy + bitmap_height) >= fb_height)
{
fh = fb_height - fy;
}
else
{
fh = bitmap_height;
}
}
// here, (fx,fy) is the destination position in the image
// (bx,by) is the position in the bitmap
// (fw,fh) is the size to copy
wait_idle_gfx_dma();
for(off_y = 0; off_y < fh; off_y++)
{
bitmap_ptr = bitmap + (by + off_y) * bitmap_width + bx;
for(off_x = 0; off_x < fw; off_x++)
{
drawPixelFast( fx + off_x,
fy + off_y,
*bitmap_ptr++
);
}
}
}
#define SWAP(x,y) { (x)=(x)^(y); (y)=(x)^(y); (x)=(x)^(y); }
// Fill a triangle - Bresenham method
// Original from http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
void uVGA::fillTri(int x1, int y1, int x2, int y2, int x3, int y3, int color)
{
int t1x, t2x, y, minx, maxx, t1xp, t2xp;
bool changed1 = false;
bool changed2 = false;
int signx1, signx2, dx1, dy1, dx2, dy2;
int e1, e2;
// Sort vertices
if (y1 > y2)
{
SWAP(y1, y2);
SWAP(x1, x2);
}
if (y1 > y3)
{
SWAP(y1, y3);
SWAP(x1, x3);
}
if (y2 > y3)
{
SWAP(y2, y3);
SWAP(x2, x3);
}
t1x = t2x = x1;
y = y1; // Starting points
dx1 = x2 - x1;
//delta_and_sign(x0, x1, &delta_x, &sign_x);
if(dx1 < 0)
{
dx1 = -dx1;
signx1 = -1;
}
else
{
signx1 = 1;
}
dy1 = y2 - y1;
dx2 = x3 - x1;
if(dx2 < 0)
{
dx2 = -dx2;
signx2 = -1;
}
else
{
signx2 = 1;
}
dy2 = y3 - y1;
if (dy1 > dx1) // swap values
{
SWAP(dx1, dy1);
changed1 = true;
}
if (dy2 > dx2) // swap values
{
SWAP(dy2, dx2);
changed2 = true;
}
e2 = dx2 >> 1;
// Flat top, just process the second half
if(y1 == y2)
goto next;
e1 = dx1 >> 1;
for (int i = 0; i < dx1;)
{
t1xp = 0;
t2xp = 0;
if(t1x < t2x)
{
minx = t1x;
maxx = t2x;
}
else
{
minx = t2x;
maxx = t1x;
}
// process first line until y value is about to change
while(i < dx1)
{
i++;
e1 += dy1;
while (e1 >= dx1)
{
e1 -= dx1;
if (changed1)
t1xp = signx1; //t1x += signx1;
else
goto next1;
}
if (changed1)
break;
else
t1x += signx1;
}
// Move line
next1:
// process second line until y value is about to change
while (1)
{
e2 += dy2;
while (e2 >= dx2)
{
e2 -= dx2;
if (changed2)
t2xp = signx2; //t2x += signx2;
else
goto next2;
}
if (changed2)
break;
else
t2x += signx2;
}
next2:
if(minx > t1x)
minx = t1x;
if(minx > t2x)
minx = t2x;
if(maxx < t1x)
maxx = t1x;
if(maxx < t2x)
maxx = t2x;
drawHLine(y, minx, maxx, color);
// Now increase y
if(!changed1)
t1x += signx1;
t1x += t1xp;
if(!changed2)
t2x += signx2;
t2x += t2xp;
y += 1;
if(y == y2)
break;
}
next:
// Second half
dx1 = x3 - x2;
if(dx1 < 0)
{
dx1 = -dx1;
signx1 = -1;
}
else
{
signx1 = 1;
}
dy1 = y3 - y2;
t1x = x2;
if (dy1 > dx1) // swap values
{
SWAP(dy1, dx1);
changed1 = true;
}
else
{
changed1 = false;
}
e1 = dx1 >> 1;
for (int i = 0; i <= dx1; i++)
{
t1xp = 0;
t2xp = 0;
if(t1x < t2x)
{
minx = t1x;
maxx = t2x;
}
else
{
minx = t2x;
maxx = t1x;
}
// process first line until y value is about to change
while(i < dx1)
{
e1 += dy1;
while (e1 >= dx1)
{
e1 -= dx1;
if (changed1)
{
t1xp = signx1; //t1x += signx1;
break;
}
else
goto next3;
}
if (changed1)
break;
else
t1x += signx1;
if(i < dx1)
i++;
}
next3:
// process second line until y value is about to change
while (t2x != x3)
{
e2 += dy2;
while (e2 >= dx2)
{
e2 -= dx2;
if(changed2)
t2xp = signx2;
else
goto next4;
}
if (changed2)
break;
else
t2x += signx2;
}
next4:
if(minx > t1x)
minx = t1x;
if(minx > t2x)
minx = t2x;
if(maxx < t1x)
maxx = t1x;
if(maxx < t2x)
maxx = t2x;
drawHLine(y, minx, maxx, color);
// Now increase y
if(!changed1)
t1x += signx1;
t1x += t1xp;
if(!changed2)
t2x += signx2;
t2x += t2xp;
y += 1;
if(y > y3)
return;
}
}
// all drawing algorithms taken from http://members.chello.at/~easyfilter/bresenham.html
inline void uVGA::delta_and_sign(int v1, int v2, int *delta, int *sign)
{
if(v2 > v1)
{
*delta = v2 - v1;
*sign = 1;
}
else if(v2 < v1)
{
*delta = v1 - v2;
*sign = -1;
}
else
{
*delta = 0;
*sign = 0;
}
}
// draw a line with or without its last pixel
void uVGA::drawLine(int x0, int y0, int x1, int y1, int color, bool no_last_pixel)
{
int delta_x;
int sign_x;
int delta_y;
int sign_y;
int err;
int err2;
if(x0 == x1)
{
if(y0 == y1)
drawPixel(x0, y0, color);
else
drawVLine(x0, y0, y1, color);
return;
}
else if(y0 == y1)
{
drawHLine(y0, x0, x1, color);
}
delta_and_sign(x0, x1, &delta_x, &sign_x);
delta_and_sign(y0, y1, &delta_y, &sign_y);
err= delta_x - delta_y;
do
{
drawPixel(x0, y0, color);
err2 = 2 * err;
if(err2 > -delta_y)
{
err -= delta_y;
x0 += sign_x;
}
else if(err2 < delta_x)
{
err += delta_x;
y0 += sign_y;
}
} while ( (x0 != x1) || (y0 != y1) );
// plot last line pixel;
if(!no_last_pixel)
drawPixel(x1, y1, color);
}
// draw a triangle
void uVGA::drawTri(int x0, int y0, int x1, int y1, int x2, int y2, int color)
{
drawLinex(x0, y0, x1, y1, color);
drawLinex(x1, y1, x2, y2, color);
drawLinex(x2, y2, x0, y0, color);
}
// draw a rectangle
void uVGA::drawRect(int x0, int y0, int x1, int y1, int color)
{
drawHLine(y0, x0, x1, color);
drawHLine(y1, x0, x1, color);
drawVLine(x0, y0, y1, color);
drawVLine(x1, y0, y1, color);
}
void uVGA::drawCircle(int xm, int ym, int r, int color)
{
int x = -r;
int y = 0;
int err = 2-2*r; /* II. Quadrant */
do
{
drawPixel(xm-x, ym+y, color); /* I. Quadrant */
drawPixel(xm-y, ym-x, color); /* II. Quadrant */
drawPixel(xm+x, ym-y, color); /* III. Quadrant */
drawPixel(xm+y, ym+x, color); /* IV. Quadrant */
r = err;
if (r <= y)
{
y++;
err += y * 2 + 1; /* e_xy+e_y < 0 */
}
if ( (r > x) || (err > y))
{
x++;
err += x * 2 + 1; /* e_xy+e_x > 0 or no 2nd y-step */
}
} while (x < 0);
}
// Yes, it is weird, it is easier to draw a fill circle than a simple circle
void uVGA::fillCircle(int xm, int ym, int r, int color)
{
int x, y;
for (y = -r; y <= r; y++)
{
for (x = -r; x <= r; x++)
{
if ((x * x) + (y * y) <= (r * r))
{
drawHLine(ym + y, xm + x, xm - x, color);
if(y != 0)
drawHLine(ym - y, xm + x, xm - x, color);
break;
}
}
}
}
// x0, y0 = center of the ellipse
// x1, y1 = upper right edge of the bounding box of the ellipse
void uVGA::drawEllipse(int _x0, int _y0, int _x1, int _y1, int color)
{
int half_height = abs(_y0 - _y1) / 2;
int half_width = abs(_x0 - _x1) / 2;
int x0 = (_x0 < _x1 ? _x0 : _x1);
int y0 = (_y0 < _y1 ? _y0 : _y1) + 2 * half_height;
int x1 = x0 + half_width * 2;
int y1 = y0 - half_height * 2;
int a = abs(x1-x0);
int b = abs(y1-y0);
int b1 = b & 1; /* values of diameter */
long dx = 4 * (1 - a) * b * b;
long dy = 4 * (b1 + 1) * a * a; /* error increment */
long err = dx + dy + b1 * a * a;
long e2; /* error of 1.step */
// if x1,y1 is not the correct edge, try... something
if(x0 > x1)
{
x0 = x1;
x1 += a;
}
if(y0 > y1)
{
y0 = y1;
}
y0 += (b + 1) / 2;
y1 = y0 - b1; /* starting pixel */
a *= 8*a;
b1 = 8*b*b;
do {
drawPixel(x1, y0, color); /* I. Quadrant */
drawPixel(x0, y0, color); /* II. Quadrant */
drawPixel(x0, y1, color); /* III. Quadrant */
drawPixel(x1, y1, color); /* IV. Quadrant */
e2 = 2*err;
if(e2 <= dy)
{
y0++;
y1--;
dy += a;
err += dy;
} /* y step */
if((e2 >= dx) || (2*err > dy))
{
x0++;
x1--;
dx += b1;
err += dx;
} /* x step */
} while (x0 <= x1);
while ((y0 - y1) < b)
{ /* too early stop of flat ellipses a=1 */
drawPixel(x0 - 1, y0, color); /* -> finish tip of ellipse */
drawPixel(x1 + 1, y0++, color);
drawPixel(x0 - 1, y1, color);
drawPixel(x1 + 1, y1--, color);
}
}
void uVGA::fillEllipse(int x0, int y0, int x1, int y1, int color)
{
int x;
int y;
int half_height = abs(y0 - y1) / 2;
int half_width = abs(x0 - x1) / 2;
int center_x = (x0 < x1 ? x0 : x1) + half_width;
int center_y = (y0 < y1 ? y0 : y1) + half_height;
for(y = -half_height; y <= 0; y++)
{
for(x = -half_width; x <= 0; x++)
{
if( (x * x * half_height * half_height + y * y * half_width * half_width) <= (half_height * half_height * half_width * half_width))
{
drawHLine(center_y + y, center_x + x, center_x - x, color);
if(y != 0)
drawHLine(center_y - y, center_x + x, center_x - x, color);
break;
}
}
}
}
// copy area s_x,s_y of w*h pixels to destination d_x,d_y
void uVGA::copy(int s_x, int s_y, int d_x, int d_y, int w, int h)
{
int c_s_x;
int c_s_y;
int c_d_x;
int c_d_y;
int c_w;
int c_h;
int error;
int sxpos;
int sypos;
int dxpos;
int dypos;
int dx;
int dy;
int off_x;
int off_y;
// nothing to copy ?
if((w <= 0) || (h <= 0))
return;
c_s_x = s_x;
c_s_y = s_y;
c_d_x = d_x;
c_d_y = d_y;
c_w = w;
c_h = h;
// let's do some clipping to avoid overflow
// 1) adjust position and size of source area according to screen size
if(c_s_x < 0)
{
c_w += c_s_x;
c_d_x -= c_s_x;
c_s_x = 0;
}
if(c_s_y < 0)
{
c_h += c_s_y;
c_d_x -= c_s_y;
c_s_y = 0;
}
error = c_s_x + c_w;
if(error >= fb_width)
{