-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathegi_fbgeom.c
7476 lines (6290 loc) · 206 KB
/
egi_fbgeom.c
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 program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
Referring to: http://blog.chinaunix.net/uid-22666248-id-285417.html
本文的copyright归[email protected] 所有,使用GPL发布,可以自由拷贝,转载。
但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。
博客:yuweixian4230.blog.chinaunix.net
Note:
1. Not thread safe.
2. TODO: For all lines drawing functions: if distance between two input points
is too big, it may take too long time! The Caller should avoid such case.
3. TODO: For all curve drawing functions: Current step/segment is too big, the
resutl of draw_line()/draw_wline() segment is NOT so smooth.
Call draw_dot() and apply anti_aliasing to improve it.
---TODO: To draw curve width by filled triangles! ---
Jurnal
2021-1-10:
1. Modify fbget_pixColor(): to pick color from working buffer.
2021-1-21/25:
1. draw_dot(): Use var. pix_color instead of fb_color to store blended color value.
fb_color is FB sys color value!
2. draw_line(): extend pixel/alpha as for anti_aliasing.
2021-2-9:
1. Add draw_triangle().
2021-2-25:
1. draw_wrect(), draw_wrect_nc(), draw_wrect()
Draw one additional 1_width line inside the rect...
2021-2-28:
1. Apply fb_dev->zbuff in draw_dot().
2021-3-3:
1. Add fbget_zbuff().
2021-3-12:
1. Apply fb_dev->pixcolor for virtual FBDEV.
2021-3-30:
1. draw_line(antialias): Comment 'dev->pixcolor_on=true' at start and
'dev->pixcolor_on=false' at end, for there's NO color in the func params.
Just let caller to set/reset it.
2. draw_dot(): if(virt_fb){}: Use pix_color instead of fb_color to store blended color value.
fb_color is FB sys color value!
2021-04-1:
1.draw_filled_spline(): save FBDEV.pixcolor_on, and restore it later.
2021-04-2:
1. draw_blend_filled_roundcorner_rect() draw_blend_filled_rect()
draw_blend_filled_annulus() draw_blend_filled_circle():
Save FBDEV.pixcolor_on, and restore it later.
2. draw_filled_spline(): fbset_color() replaced by fbset_color2().
draw_circle2() draw_filled_annulus2() draw_filled_circle2():
fbset_color() replaced by fbset_color2() to use pixcolor.
2021-05-19:
1. Add draw_blend_filled_triangle().
2021-05-22/23:
1. Add point_intriangle().
2. Add pxy_online(), point_online().
2021-07-26:
1. Add draw_line_antialias().
2. draw_line(): Wrap with draw_line_simple() and draw_line_antialias().
2021-08-08:
1. draw_filled_triangle(): Use draw_line_simple().
2021-08-12:
1. Add draw3D_line(), draw3D_simple_line(), dist_points(), dist3D_points()
2. draw_dot(): apply fb_dev->flipZ.
2021-08-27:
1. Add draw_filled_triangle2(). float param: x,y
2021-08-30:
1. Add draw_filled_triangle3(). int param: x,y
2021-09-01/02:
1. Improve draw_filled_triangle3( int x/y ).
2021-09-07:
1. draw_filled_triangle3(): Case_3: if the triangle degenerates into a line!
2021-09-14:
1. Modify clear_screen().
2021-09-24:
1. draw_dot(): Check if pixalpha==0 at first.
2021-11-8:
1. Add draw_filled_triangle_outline()
2021-11-9:
1. Add draw_filled_triangle4(), pixZ value applys.
2. Add draw_filled_triangle4(), pixZ value applys.
2021-11-11:
1. draw_dot(): fb_dev->zbuff_IgnoreEqual applys.
2022-03-04:
1. Add float_draw_dot()
2022-03-05:
1. Rename float_draw_dot() as fdraw_dot()
2. Add fdraw_line()
2022-03-06:
1. Add fdraw_circle()
2022-03-07:
1. draw_line(): calls fdraw_line() if fbdev->antialias_on.
2022-03-09:
1. draw_filled_circle(): outer circle for antialiasing effect.
2022-03-11:
1. draw_wline(): Call draw_filled_triangles() to draw thickness.
2022-03-12:
1. draw_filled_triangle(): improve accuracy by checking result of floorf()/ceilf()
2022-03-14:
1. draw_blend_filled_roundcorner_rect(): antialiasing for arcs.
2. fdraw_dot(): consider user geom pixalpha --- NOT GOOD FOR ANTIALIAS BLENDING.
2022-03-24:
1. Improve draw_wline_nc(), draw_wline()
2. draw_spline(): Call draw_pline() instead of draw_wline()
2022-04-26:
1. To restore FBDEV.pixcolor if FBDEV.pixcolor_on is ture, affect following funcitons:
draw_line_antialias() draw_blend_filled_roundcorner_rect()
draw_blend_filled_rect() draw_blend_filled_triangle()
draw_blend_filled_annulus() draw_blend_filled_circle() draw_filled_spline()
2022-06-22:
1. Add pxy_inbox2().
2022-08-29:
1. draw3D_line_simple(): improve with roundf().
2022-08-30:
1. Add pixelate_filled_triangle2()
2022-09-01:
1. pixelate_filled_triangle2(float **pXYZ): Use float type for pXYZ, to keep precision of pixZ.
Modified and appended by Midas-Zhou
[email protected](Not in use since 2022_03_01)
----------------------------------------------------------------------------*/
#include "egi_fbgeom.h"
#include "egi.h"
#include "egi_debug.h"
#include "egi_math.h"
#include "egi_color.h"
#include <unistd.h>
#include <string.h> /*memset*/
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <math.h>
#include <stdlib.h>
/* global variale */
EGI_BOX gv_fb_box;
/* Default color set.
* Note:
* 1. fb_color and fb_rgb are volatile! They may be changed by draw_dot() and other functions!
*
*/
static uint16_t fb_color=(30<<11)|(10<<5)|10; //r(5)g(6)b(5) /* For 16bpp, */
#ifdef LETS_NOTE
static uint32_t fb_rgb=0x0000ff; /* For 24/32 bpp */
#endif
/* default usleep time after each draw_point() */
static unsigned int fb_drawdot_usleep;
/* Ancillary functions */
static inline int dist_points(int x1, int y1, int x2, int y2)
{
return roundf(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
}
static inline int dist3D_points(int x1, int y1, int z1, int x2, int y2, int z2)
{
return roundf(sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1)));
}
/* Set drawing speed: 1 - 10, 0==10 */
void fbset_speed(unsigned int speed)
{
/* Set Limit */
if(speed==0) speed=10;
if(speed>10) speed=10;
fb_drawdot_usleep=2*(10-speed)*(10-speed);
}
/* For DEBUG : print out sys.fb_color and FBDEV.pixcolor and FBDEV.pixalpha */
void fbprint_fbcolor(void)
{
printf(" -- fb_color=%#04X -- \n", fb_color);
}
void fbprint_fbpix(FBDEV *dev)
{
printf("pixcolor_on:%s, pixcolor=%#04X; pixalpha_hold:%s, pixalpha=%d\n",
dev->pixcolor_on?"true":"false", dev->pixcolor,
dev->pixalpha_hold?"true":"false", dev->pixalpha );
}
/* set color for every dot */
inline void fbset_color(EGI_16BIT_COLOR color)
{
fb_color=color;
}
/* set color for every dot */
inline void fbset_color2(FBDEV *dev, EGI_16BIT_COLOR color)
{
if(dev->pixcolor_on) /* Set private pixcolor */
dev->pixcolor=color;
else /* Set public pixcolor */
fb_color=color;
}
/* Set and reset alpha value for pixels, which will be applied in draw_dot() */
inline void fbset_alpha(FBDEV *dev, EGI_8BIT_ALPHA alpha)
{
if(dev==NULL)return;
/* turn on FBDEV pixalpha_hold and set pixalpha */
dev->pixalpha_hold=true;
dev->pixalpha=alpha;
}
inline void fbreset_alpha(FBDEV *dev)
{
if(dev==NULL)return;
/* turn off FBDEV pixalpha_hold and reset pixalpha to 255 */
dev->pixalpha_hold=false;
dev->pixalpha=255;
}
/*--------------------------------------------
check if (px,py) in box(x1,y1,x2,y2)
return:
True: within the box or on the edge
False
Midas Zhou
---------------------------------------------*/
inline bool pxy_inbox(int px,int py, int x1, int y1,int x2, int y2)
{
int xl,xh,yl,yh;
if(x1>=x2){
xh=x1;xl=x2;
}
else {
xl=x1;xh=x2;
}
if(y1>=y2){
yh=y1;yl=y2;
}
else {
yh=y2;yl=y1;
}
#if 0
if( (px>=xl && px<=xh) && (py>=yl && py<=yh))
return true;
else
return false;
#else /* HK2022-06-22 */
if(px<xl || px>xh || py<yl || py>yh)
return false;
else
return true;
#endif
}
/*--------------------------------------------
check if (px,py) in box.
return:
True: within the box or on the edge
False
Midas Zhou
---------------------------------------------*/
inline bool pxy_inbox2(int px,int py, const EGI_BOX* box)
{
if(box==NULL)
return false;
return pxy_inbox( px, py,
box->startxy.x, box->startxy.y,
box->endxy.x, box->endxy.y
);
}
/*--------------------------------------------
check if (px,py) in on line(x1,y1,x2,y2).
Return:
True
False
Midas Zhou
---------------------------------------------*/
inline bool pxy_online(int px,int py, int x1, int y1,int x2, int y2)
{
MAT_VECTOR2D vt[3];
float s;
float len;
int xl,xh,yl,yh;
len= sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );
/* 1. If degenerated into a point */
if( len<1 ) {
//egi_dpstd("Too short a line, it's a point!\n");
len= sqrt( (px-x1)*(px-x1) + (py-y1)*(py-y1) );
return (len < 2.0);
}
/* 2. Check vector cross product, and distance=s/len */
vt[0]=(MAT_VECTOR2D){x1-px, y1-py}; /* x1-px */
vt[1]=(MAT_VECTOR2D){x2-px, y2-py}; /* x2-px */
s= VECTOR2D_CROSSPD(vt[0], vt[1]);
if( abs(s)/len > 1.0 )
return false;
/* 3. NOW: On the same line, but NOT necessary on the segment. */
/* 4. Check if on the segement */
if(x1>=x2){
xh=x1;xl=x2;
}
else {
xl=x1;xh=x2;
}
if(y1>=y2){
yh=y1;yl=y2;
}
else {
yh=y2;yl=y1;
}
return ( px>=xl && px<=xh) && (py>=yl && py<=yh); /* NOT '||', consider Vertical OR Horizontal line! */
}
/*--------------------------------------------
check if (px,py) in on segment defined by pts.
Return:
True
False
Midas Zhou
---------------------------------------------*/
inline bool point_online(const EGI_POINT *pxy, const EGI_POINT *pts)
{
if( pxy==NULL || pts==NULL)
return false;
return pxy_online( pxy->x, pxy->y,
pts->x, pts->y, (pts+1)->x, (pts+1)->y );
}
/*-----------------------------------------------
check if an EGI_POINT is in an EGI_BOX
return:
True: within the box or on the edge
False or Fails!
Midas Zhou
------------------------------------------------*/
inline bool point_inbox(const EGI_POINT *pxy, const EGI_BOX* box)
{
if(pxy==NULL || box==NULL)
return false;
return pxy_inbox( pxy->x, pxy->y,
box->startxy.x, box->startxy.y,
box->endxy.x, box->endxy.y
);
}
/*------------------------------------------------------------------------
check if an EGI_POINT is within a circle. If the point is just on the edge
of the circle, it deems as NOT within the circle.
@pxy: checking point.
@pc: Center point of the circle.
@r: Radius of the circle.
return:
True: the point is totally within the circle
False
Midas Zhou
-------------------------------------------------------------------------*/
inline bool point_incircle(const EGI_POINT *pxy, const EGI_POINT *pc, int r)
{
if(pxy==NULL || pc==NULL)
return false;
if( r*r > (pxy->x - pc->x)*(pxy->x - pc->x)+(pxy->y - pc->y)*(pxy->y - pc->y) )
return true;
else
return false;
}
/*------------------------------------------------------------------------
Check if an EGI_POINT is within a triangle.
(Barycentric Coordinate Method)
Reference: https://blackpawn.com/texts/pointinpoly/default.html
@pxy: checking point.
@tripts: Three points to define a triangle.
return:
True: the point is within the triangle.
False
Midas Zhou
-------------------------------------------------------------------------*/
inline bool point_intriangle(const EGI_POINT *pxy, const EGI_POINT *tripts)
{
MAT_VECTOR2D vt[4];
// float s;
float dp00, dp01, dp02, dp11, dp12;
float denom;
float u,v;
// float len;
if(pxy==NULL || tripts==NULL)
return false;
vt[0] = VECTOR2D_SUB(tripts[2], tripts[0]);
vt[1] = VECTOR2D_SUB(tripts[1], tripts[0]);
vt[2] = VECTOR2D_SUB(pxy[0], tripts[0]); /* <---- pxy -> ptn[0] ------- */
vt[3]= VECTOR2D_SUB(tripts[2], tripts[1]);
dp00=VECTOR2D_DOTPD(vt[0], vt[0]);
dp01=VECTOR2D_DOTPD(vt[0], vt[1]);
dp02=VECTOR2D_DOTPD(vt[0], vt[2]);
dp11=VECTOR2D_DOTPD(vt[1], vt[1]);
dp12=VECTOR2D_DOTPD(vt[1], vt[2]);
denom=dp00*dp11 - dp01*dp01;
/* If degenrated into a line. */
if( abs(denom) < 1 ) {
//egi_dpstd("denom<1, It's a line! \n");
/* Case: vt[3]~=0! */
if(VECTOR2D_MOD( vt[3])<1 )
return point_online(pxy, tripts); /* Line: tripts[0], tripts[1] */
/* Case: vt[0]~=0! || vt[1]~=0! */
else
return point_online(pxy, tripts+1); /* Line: tripts[1], tripts[2] */
}
u=(dp11*dp02 - dp01*dp12)/denom;
v=(dp00*dp12 - dp01*dp02)/denom;
return (u>=0) && (v>=0) && ( u+v <= 1 );
}
/*---------------------------------------------------------------
Check whether the box is totally WITHIN the container.
If there is an overlap of any sides, it is deemed as NOT within!
@inbox: checking box
@container: containing box
Return:
True: the box is totally WITHIN the container.
False: NOT totally WITHIN the container.
or fails!
Midas Zhou
----------------------------------------------------------------*/
bool box_inbox(EGI_BOX* inbox, EGI_BOX* container)
{
int xcu,xcd; /*u-max, d-min*/
int xiu,xid;
int ycu,ycd;
int yiu,yid;
if( inbox==NULL || container==NULL )
return false;
/* 1. get Max. and Min X coord of the container */
if( container->startxy.x > container->endxy.x ) {
xcu=container->startxy.x;
xcd=container->endxy.x;
}
else {
xcu=container->endxy.x;
xcd=container->startxy.x;
}
/* 2. get Max. and Min X coord of the inbox */
if( inbox->startxy.x > inbox->endxy.x ) {
xiu=inbox->startxy.x;
xid=inbox->endxy.x;
}
else {
xiu=inbox->endxy.x;
xid=inbox->startxy.x;
}
/* 3. compare X coord */
if( !(xcd<xid) || !(xcu>xiu) )
return false;
/* 4. get Max. and Min Y coord of the container */
if( container->startxy.y > container->endxy.y ) {
ycu=container->startxy.y;
ycd=container->endxy.y;
}
else {
ycu=container->endxy.y;
ycd=container->startxy.y;
}
/* 5. get Max. and Min Y coord of the inbox */
if( inbox->startxy.y > inbox->endxy.y ) {
yiu=inbox->startxy.y;
yid=inbox->endxy.y;
}
else {
yiu=inbox->endxy.y;
yid=inbox->startxy.y;
}
/* 6. compare Y coord */
if( !(ycd<yid) || !(ycu>yiu) )
return false;
return true;
}
/*--------------------------------------------------------------------
Check whether the box is totally out of the container.
If there is an overlap of any sides, it is deemed as NOT totally out!
@inbox: checking box
@container: containing box
Return:
True: the box is totally out of the container.
False: NOT totally out of the container.
or fails!
Midas Zhou
--------------------------------------------------------------------*/
bool box_outbox(EGI_BOX* inbox, EGI_BOX* container)
{
int xcu,xcd; /*u-max, d-min*/
int xiu,xid;
int ycu,ycd;
int yiu,yid;
if( inbox==NULL || container==NULL )
return false;
/* 1. get Max. and Min X coord of the container */
if( container->startxy.x > container->endxy.x ) {
xcu=container->startxy.x;
xcd=container->endxy.x;
}
else {
xcu=container->endxy.x;
xcd=container->startxy.x;
}
/* 2. get Max. and Min X coord of the inbox */
if( inbox->startxy.x > inbox->endxy.x ) {
xiu=inbox->startxy.x;
xid=inbox->endxy.x;
}
else {
xiu=inbox->endxy.x;
xid=inbox->startxy.x;
}
/* 3. compare X coord */
if( !( xcd>xiu || xcu<xid ) )
return false;
/* 4. get Max. and Min Y coord of the container */
if( container->startxy.y > container->endxy.y ) {
ycu=container->startxy.y;
ycd=container->endxy.y;
}
else {
ycu=container->endxy.y;
ycd=container->startxy.y;
}
/* 5. get Max. and Min Y coord of the inbox */
if( inbox->startxy.y > inbox->endxy.y ) {
yiu=inbox->startxy.y;
yid=inbox->endxy.y;
}
else {
yiu=inbox->endxy.y;
yid=inbox->startxy.y;
}
/* 6. compare Y coord */
if( !( ycd>yiu || ycu<yid ) )
return false;
return true;
}
/*---------------------------------------------
clear screen with given color
BUG:
!!!!call egi_colorxxxx_randmon() error
Midas
---------------------------------------------*/
void clear_screen(FBDEV *fb_dev, uint16_t color)
{
int bytes_per_pixel=fb_dev->vinfo.bits_per_pixel/8;
long int location=0;
/* If map_bk allocated, ENABLE_BACK_BUFFER is defined */
if( fb_dev->map_bk ) {
for(location=0; location < (fb_dev->screensize/bytes_per_pixel); location++)
*((uint16_t*)(fb_dev->map_bk+location*bytes_per_pixel))=color;
}
/* Only map_fb is available */
else {
for(location=0; location < (fb_dev->screensize/bytes_per_pixel); location++)
*((uint16_t*)(fb_dev->map_fb+location*bytes_per_pixel))=color;
}
}
/*----------- Oboselete!!! Replaced by fb_clear_backBuff() -----------
clear fb_dev->map_bk with given color
Midas
--------------------------------------------------------------------*/
void fbclear_bkBuff(FBDEV *fb_dev, uint16_t color)
{
int bytes_per_pixel=fb_dev->vinfo.bits_per_pixel/8;
long int location=0;
for(location=0; location < (fb_dev->screensize/bytes_per_pixel); location++)
*((uint16_t*)(fb_dev->map_bk+location*bytes_per_pixel))=color;
}
/*-------------------------------------------------------------------
Return indicated pixel color value in EGI_16BIT_COLOR.
--- FOR 16BIT COLOR FB ---
Note:
1. If fb_dev->lumadelt is NOT zero, then picked color is NOT same
as original written color.
@fb_dev: FB under consideration
@x,y: Pixel coordinate value. under FB.pos_rotate coord!
Return:
Ok 16bit color value for the pixel with given coordinates(x,y).
Fail 0 as for black
Midas Zhou
-------------------------------------------------------------------*/
EGI_16BIT_COLOR fbget_pixColor(FBDEV *fb_dev, int x, int y)
{
if(fb_dev==NULL || fb_dev->map_fb==NULL)
return 0;
int fx=0, fy=0; /* FB mem space coordinate */
int xres=fb_dev->vinfo.xres;
int yres=fb_dev->vinfo.yres;
/* check FB.pos_rotate: Map x,y to fx,fy under default FB coord.
* Note: Here xres/yres is default/HW_set FB x/y resolustion!
*/
switch(fb_dev->pos_rotate) {
case 0: /* FB default position */
fx=x;
fy=y;
break;
case 1: /* Clockwise 90 deg */
fx=(xres-1)-y;
fy=x;
break;
case 2: /* Clockwise 180 deg */
fx=(xres-1)-x;
fy=(yres-1)-y;
break;
case 3: /* Clockwise 270 deg */
fx=y;
fy=(yres-1)-x;
break;
}
/* Check mapped FX FY in default FB */
if( fx < 0 || fx > xres-1 ) return 0;
if( fy < 0 || fy > yres-1 ) return 0;
/* Pick pix color from working buffer */
if(fb_dev->map_bk)
return *(EGI_16BIT_COLOR *)(fb_dev->map_bk+(fy*xres+fx)*sizeof(EGI_16BIT_COLOR));
/* Pick from FB mmap */
else
return *(EGI_16BIT_COLOR *)(fb_dev->map_fb+(fy*xres+fx)*sizeof(EGI_16BIT_COLOR));
}
/*-------------------------------------------------------------------
Return zbuff value for the given point (x,y) in FBDEV.
@fb_dev: FBDEV.
@x,y: Pixel coordinate value. under FB.pos_rotate coord!
Return:
0 Fails or out of range. ( 0 as for bkground layer.)
>=0 Ok.
Midas Zhou
-------------------------------------------------------------------*/
int fbget_zbuff(FBDEV *fb_dev, int x, int y)
{
if(fb_dev==NULL || fb_dev->map_fb==NULL)
return 0;
int fx=0, fy=0; /* FB mem space coordinate */
int xres=fb_dev->vinfo.xres;
int yres=fb_dev->vinfo.yres;
long int pixoff;
/* check FB.pos_rotate: Map x,y to fx,fy under default FB coord.
* Note: Here xres/yres is default/HW_set FB x/y resolustion!
*/
switch(fb_dev->pos_rotate) {
case 0: /* FB default position */
fx=x;
fy=y;
break;
case 1: /* Clockwise 90 deg */
fx=(xres-1)-y;
fy=x;
break;
case 2: /* Clockwise 180 deg */
fx=(xres-1)-x;
fy=(yres-1)-y;
break;
case 3: /* Clockwise 270 deg */
fx=y;
fy=(yres-1)-x;
break;
}
/* Check mapped FX FY in default FB */
if( fx < 0 || fx > xres-1 ) return 0;
if( fy < 0 || fy > yres-1 ) return 0;
/* Get zbuff */
pixoff=fy*xres+fx;
return fb_dev->zbuff[pixoff];
}
/*------------------------------------------------------------------
Assign color value to a pixel in framebuffer.
Note:
1. The pixel color value will be system color fb_color, or as its
private color of FBDEV.pixcolor.
2. The caller shall do boudary check first, to ensure that coordinates
(x,y) is within screen size range. or just rule it out.
TEST:
1. test luma adjustment FOR_16BITS_COLOR_FBDEV only.
Midas Zhou
Return:
0 OK
<0 Fails
-1 get out of FB mem.(ifndef FB_DOTOUT_ROLLBACK)
-------------------------------------------------------------------*/
int draw_dot(FBDEV *fb_dev,int x,int y)
{
EGI_IMGBUF *virt_fb;
unsigned char *map;
int fx=0;
int fy=0;
long int location=0;
long int pixoff;
#ifdef LETS_NOTE
unsigned char* pARGB=NULL; /* For LETS_NOTE */
#endif
int xres;
int yres;
FBPIX fpix;
int sumalpha;
EGI_16BIT_COLOR pix_color;
/* Check input data */
if(fb_dev==NULL)
return -2;
/* Check pixalpha if 0 */
if(fb_dev->pixalpha == 0) {
/* Do NOT change fb_dev->zbuff[pixoff] */
/* Reset alpha to 255 as default */
if(fb_dev->pixalpha_hold==false)
fb_dev->pixalpha=255;
return 0;
}
/* <<<<<< FB BUFFER SELECT >>>>>> */
#if defined(ENABLE_BACK_BUFFER) || defined(LETS_NOTE)
map=fb_dev->map_bk; /* write to back buffer */
#else
map=fb_dev->map_fb; /* write directly to FB map */;
#endif
/* set xres and yres */
virt_fb=fb_dev->virt_fb;
if(virt_fb) { /* for virtual FB */
xres=virt_fb->width;
yres=virt_fb->height;
}
else { /* for FB */
xres=fb_dev->vinfo.xres;
yres=fb_dev->vinfo.yres;
}
/* Check FB.pos_xres, pos_yres */
//if(fb_dev->pos_rotate) {
if( x<0 || x>fb_dev->pos_xres-1)
return -1;
if( y<0 || y>fb_dev->pos_yres-1)
return -1;
//}
/* Check FB.pos_rotate
* IF 90 Deg rotated: Y maps to (xres-1)-FB.X, X maps to FB.Y
* Note: Here xres/yres is default/HW_set FB x/y resolustion!
*/
switch(fb_dev->pos_rotate) {
case 0: /* FB default position */
fx=x;
fy=y;
break;
case 1: /* Clockwise 90 deg */
fx=(xres-1)-y;
fy=x;
break;
case 2: /* Clockwise 180 deg */
fx=(xres-1)-x;
fy=(yres-1)-y;
break;
case 3: /* Clockwise 270 deg */
fx=y;
fy=(yres-1)-x;
break;
}
#ifdef FB_DOTOUT_ROLLBACK
/* map to LCD(X,Y) or IMGBUF if virt_fb */
if(fx>xres-1)
fx=fx%xres;
else if(fx<0)
{
fx=xres-(-fx)%xres;
fx=fx%xres; /* here fx=1-240 */
}
if( fy > yres-1) {
fy=fy%yres;
}
else if(fy<0) {
fy=yres-(-fy)%yres;
fy=fy%yres; /* here fy=1-320 */
}
#else /* NO ROLLBACK */
/* ignore out_ranged points */
if( fx>(xres-1) || fx<0) {
return -1;
}
if( fy>(yres-1) || fy<0 ) {
return -1;
}
#endif
/* Check Z if zbuff_on */
if( fb_dev->zbuff_on ) {
pixoff=fy*xres+fx;
/* flipZ==ture */
if( fb_dev->flipZ ) {
//fb_dev->pixz = -fb_dev->pixz;
/* Zbuff > -pixZ */
if( fb_dev->zbuff[pixoff] > -fb_dev->pixz )
return 0;
/* Zbuff == -pixZ */
else if( fb_dev->zbuff[pixoff] == -fb_dev->pixz && fb_dev->zbuff_IgnoreEqual )
return 0;
/* Zbuff <(=) -pixZ */
else
fb_dev->zbuff[pixoff]= -fb_dev->pixz;
}
/* flipZ==false */
else {
/* Zbuff > pixZ */
if( fb_dev->zbuff[pixoff] > fb_dev->pixz )
return 0;
/* Zbuff==pixZ */
else if( fb_dev->zbuff[pixoff] == fb_dev->pixz && fb_dev->zbuff_IgnoreEqual )
return 0;
/* Zbuff <(=) pixZ */
else
fb_dev->zbuff[pixoff]=fb_dev->pixz;
}
}
/* ---------------- ( for virtual FB :: for 16bit color only NOW!!! ) -------------- */
if(virt_fb) {
/* (in pixles): data location of the point pixel */
location=fx+fy*virt_fb->width;
/* check if no space left for a 16bit pixel */
if( location < 0 || location > fb_dev->screensize - 1 ) /* screensize of imgbuf,
* in pixels! */
{
printf("WARNING: point location out of EGI_IMGBUF mem.!\n");
return -1;
}
/* blend pixel color
* TODO: Also apply background alpha value if virt_fb->alpha available ????
* fb_color=egi_16bitColor_blend2( fb_color, fb_dev->pixalpha,
* virt_fb->imgbuf[location], virt_fb->alpha[location]);
*/
if(fb_dev->pixalpha==255) { /* if 100% front color */
if(fb_dev->pixcolor_on)
virt_fb->imgbuf[location]=fb_dev->pixcolor;
else
virt_fb->imgbuf[location]=fb_color;
}
/* otherwise, blend with original color, back alpha value ignored!!! */
else if( fb_dev->pixalpha!=0 ) {
if(fb_dev->pixcolor_on) {
/* NOTE: back color alpha value all deemed as 255, */
// XXX fb_color=COLOR_16BITS_BLEND( fb_dev->pixcolor, /* Front color */
pix_color=COLOR_16BITS_BLEND( fb_dev->pixcolor, /* Front color */
virt_fb->imgbuf[location], /* Back color */
fb_dev->pixalpha ); /* Alpha value */
virt_fb->imgbuf[location]=pix_color;
}
else {
/* NOTE: back color alpha value all deemed as 255, */
pix_color=COLOR_16BITS_BLEND( fb_color, /* Front color */
virt_fb->imgbuf[location], /* Back color */
fb_dev->pixalpha ); /* Alpha value */
virt_fb->imgbuf[location]=pix_color;
}
}
/* if VIRT FB has alpha data. TODO: Apply egi_16bitColor_blend2() */
if(virt_fb->alpha) {
/* sum up alpha value */
sumalpha=virt_fb->alpha[location]+fb_dev->pixalpha;
if( sumalpha > 255 ) sumalpha=255;
virt_fb->alpha[location]=sumalpha;
}
/* reset alpha to 255 as default, at last!!! */
if(fb_dev->pixalpha_hold==false)
fb_dev->pixalpha=255;
}
/* ------------------------ ( for real FB ) ---------------------- */
else {
#ifdef LETS_NOTE /* --------- FOR 32BITS COLOR (ARGB) FBDEV ------------ */
/*(in bytes:) data location of the point pixel */
location=(fx+fb_dev->vinfo.xoffset)*(fb_dev->vinfo.bits_per_pixel>>3)+
(fy+fb_dev->vinfo.yoffset)*fb_dev->finfo.line_length;
/* NOT necessary ??? check if no space left for a 16bit_pixel in FB mem */
if( location<0 || location > (fb_dev->screensize-sizeof(uint32_t)) ) /* screensize in bytes! */
{
//printf("WARNING: point location out of fb mem.!\n");
return -1;
}
/* push old data to FB FILO */
if(fb_dev->filo_on && fb_dev->pixalpha>0 )
{
fpix.position=location; /* pixel to bytes, !!! FAINT !!! */
pARGB=map+location;
fpix.argb=*(uint32_t *)pARGB;
/* FB alpha value no more use ? */