-
Notifications
You must be signed in to change notification settings - Fork 11
/
egi_color.c
1436 lines (1170 loc) · 41.4 KB
/
egi_color.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.
--- IMPORTANT NOTE ---
1. Visional illusions will mar the appearance of your GUI design.
2. Always Select correct colours to cover visual defects for your GUI!
--- COLOR SPACE ---
Gray scale: Monochrome
RGB: Red/Green/Blue, standard RGB, sRGB
YCbCr: Y/Cb/Cr(YUV),standard YCC
CMYK: Cyan/Magenta/Yellow/Key(black)
YCCK: Y/Cb/Cr/Key
bg_sRGB: big gamut RGB
bg_YCC: big gamut YCC
( https://www.color.org )
TODO: Using pointer params is better than just adding inline qualifier ?
EGI Color Functions
Journal:
2021-08-25:
1. Add egi_16bitColor_interplt4p().
2022-04-19:
1. Add HTML color name definition.
Midas Zhou
[email protected](Not in use since 2022_03_01)
-----------------------------------------------------------------------*/
#include "egi_color.h"
#include "egi_debug.h"
#include "egi_utils.h"
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
//#include <time.h>
#include <stdio.h>
#include <sys/time.h> /*gettimeofday*/
#include <stdint.h>
/*------------------------------------------------------------------------------------
Get a 16bit color value/alpha between two given colors/alphas by interpolation.
@color1, color2: The first and second input color value.
@alpha1, alpha2: The first and second input alpha value.
@f15_ratio: Ratio for interpolation, in fixed point type f15.
that is [0-1]*(2^15);
!!! A bigger ratio makes alpha more closer to color2/alpha2 !!!
@colro, alpha Pointer to pass output color and alpha.
Ignore if NULL.
-------------------------------------------------------------------------------------*/
inline void egi_16bitColor_interplt( EGI_16BIT_COLOR color1, EGI_16BIT_COLOR color2,
unsigned char alpha1, unsigned char alpha2,
int f15_ratio, EGI_16BIT_COLOR* color, unsigned char *alpha)
{
unsigned int R,G,B,A;
/* Interpolate color value */
if(color) {
R =((color1&0xF800)>>8)*((1U<<15)-f15_ratio); /* R */
R +=((color2&0xF800)>>8)*f15_ratio;
R >>= 15;
G =((color1&0x7E0)>>3)*((1U<<15)-f15_ratio); /* G */
G +=((color2&0x7E0)>>3)*f15_ratio;
G >>= 15;
B =((color1&0x1F)<<3)*((1U<<15)-f15_ratio); /* B */
B +=((color2&0x1F)<<3)*f15_ratio;
B >>= 15;
*color=COLOR_RGB_TO16BITS(R, G, B);
}
/* Interpolate alpha value */
if(alpha) {
A = alpha1*((1U<<15)-f15_ratio); /* R */
A += alpha2*f15_ratio;
A >>= 15;
*alpha=A;
}
}
/*--------------------------------------------------------------------------------
Interpolation within 4 pixels.
@color1-4: 16bit color values. ( 1,2 at upper side, 3,4 at lower side )
@alpah1-4: 8bit alpha values.
@f15_ratioX: Ratio for interpolation at X side, in fixed point type f15.
@f15_ratioY: Ratio for interpolation at Y sied, in fixed point type f15.
that is [0-1]*(2^15);
@colro, alpha Pointer to pass output color and alpha.
Ignore if NULL.
---------------------------------------------------------------------------------*/
inline void egi_16bitColor_interplt4p( EGI_16BIT_COLOR color1, EGI_16BIT_COLOR color2,
EGI_16BIT_COLOR color3, EGI_16BIT_COLOR color4,
unsigned char alpha1, unsigned char alpha2,
unsigned char alpha3, unsigned char alpha4,
int f15_ratioX, int f15_ratioY,
EGI_16BIT_COLOR* color, unsigned char *alpha )
{
EGI_16BIT_COLOR Cu, Cd; //Cl, Cr; /* interpolated color at 4 sides. */
EGI_8BIT_ALPHA Au, Ad; //Al, Ar; /* interpolated alpha at 4 sides. */
/* Get interpolation at upper and lower side. */
egi_16bitColor_interplt( color1, color2, alpha1, alpha2, f15_ratioX, &Cu, alpha==NULL?NULL:&Au);
egi_16bitColor_interplt( color3, color4, alpha1, alpha2, f15_ratioX, &Cd, alpha==NULL?NULL:&Ad);
/* OR: Get interpolation at left and right side. */
//egi_16bitColor_interplt( color1, color3, alpha1, alpha3, f15_ratioY, &Cl, &Al);
//egi_16bitColor_interplt( color2, color4, alpha2, alpha4, f15_ratioY, &Cr, &Ar);
/* Interpolate between Cu/Cd and Au/Ad */
egi_16bitColor_interplt( Cu, Cd, Au, Ad, f15_ratioY, color, alpha);
}
/*---------------------------------------------------------------------
To generate Xterm 256 colors as defined by ISO/IEC-6429.
code:
0-7: Same as 8_color, CSI: "ESC[30-37m"
8-15: Bright version of above colors. CSI: "ESC[90-97m"
16-231: 6x6x6=216 colors ( 16+36*r+6*g+b where 0<=r,g,b<=5 )
232-255: 24_grade grays
Return:
code<256 EGI_16BIT_COLOR
code>255 0 as BLACK
----------------------------------------------------------------------*/
EGI_16BIT_COLOR egi_256color_code(unsigned int code)
{
int nr,ng,nb;
/* 1. Code 0-7: 8 standard xterm color */
if( code<8 ) {
if(code==7) /* Silver(192,192,192) */
return COLOR_RGB_TO16BITS(192,192,192);
else /* 0-7: BLACK(0,0,0)
* Maroon(128,0,0), Green(0,128,0), Olive(128,128,0),
* Navy(0,0,128), Purple(128,0,128), Teal(0,128,128)
*/
return COLOR_RGB_TO16BITS((code&0x1)<<7, ((code>>1)&0x1)<<7, (code>>2)<<7); /* /2 %2 *128 */
}
/* 2. Code 8-15: 8 bright version standard xterm color */
else if(code<16) {
if(code==8) /* Grey(128,128,128) */
return COLOR_RGB_TO16BITS(128,128,128);
else /* 9-15: Red(255,0,0)
* Lime(0,255,0), Yellow(255,255,0), Blue(0,0,255),
* Fuchsia(255,0,255), Aqua(0,255,255), White(255,255,255)
*/
return COLOR_RGB_TO16BITS((code&0x1)*255, ((code>>1)&0x1)*255, ((code>>2)&0x1)*255); /* /2 %2 *256 */
}
/* 3. Code 16-231: 6x6x6=216 colors ( 16+36*r+6*g+b where 0<=r,g,b<=5 ) */
else if(code<232) {
nr=(code-16)/36;
ng=(code-16)%36/6;
nb=(code-16)%6;
return COLOR_RGB_TO16BITS(nr==0?0:(95+40*(nr-1)), ng==0?0:(95+40*(ng-1)),nb==0?0:(95+40*(nb-1)));
}
/* 4. Code 232-255: 24_grade grays, Grey(8,8,8) (18,18,18)...(238,238,238) */
else if( code<256 ) {
int val=8+(code-232)*10;
return COLOR_RGB_TO16BITS( val, val, val);
}
/* 5. CODE >255 , illegal. */
else
return 0;
}
/*------------------------------------------------------------------
Get average color value
@colors: array of color values
@n number of input colors
--------------------------------------------------------------------*/
inline EGI_16BIT_COLOR egi_16bitColor_avg(EGI_16BIT_COLOR *colors, int n)
{
int i;
unsigned int avgR,avgG,avgB;
/* to enusre array colors is not NULL */
avgR=avgG=avgB=0;
for(i=0; i<n; i++) {
avgR += ((colors[i]&0xF800)>>8);
avgG += ((colors[i]&0x7E0)>>3);
avgB += ((colors[i]&0x1F)<<3);
}
return COLOR_RGB_TO16BITS(avgR/n, avgG/n, avgB/n);
}
/*------------------------------------------------------------------
16bit color blend function
Note: Ignore back alpha value.
--------------------------------------------------------------------*/
inline EGI_16BIT_COLOR egi_16bitColor_blend(int front, int back, int alpha)
{
#if 0 /* Gamma correction, SLOW!!! */
float fa;
float gamma=2.2; //0.45; //2.2;
fa=(alpha+0.5)/256;
fa= pow(fa,1.0/gamma);
alpha=(unsigned char)(fa*256-0.5);
#else /* WARNING!!!! Must keep 0 alpha value unchanged, or BLACK bk color will appear !!!
a simple way to improve sharpness */
alpha = alpha*3/2;
if(alpha>255)
alpha=255;
//if(alpha<10)
// alpha=0;
#endif
return COLOR_16BITS_BLEND(front, back, alpha);
}
/*------------------------------------------------------------------------------
16bit color blend function
Note: Back alpha value also applied.
-------------------------------------------------------------------------------*/
inline EGI_16BIT_COLOR egi_16bitColor_blend2(EGI_16BIT_COLOR front, unsigned char falpha,
EGI_16BIT_COLOR back, unsigned char balpha )
{
/* applay front color if both 0 */
if(falpha==0 && balpha==0) {
return front;
}
/* blend them */
else {
return COLOR_RGB_TO16BITS (
( ((front&0xF800)>>8)*falpha + ((back&0xF800)>>8)*balpha) /(falpha+balpha),
( ((front&0x7E0)>>3)*falpha + ((back&0x7E0)>>3)*balpha) /(falpha+balpha),
( ((front&0x1F)<<3)*falpha + ((back&0x1F)<<3)*balpha) /(falpha+balpha)
);
}
}
/*-------------------------------------------------------------------------
Get a random 16bit color from Douglas.R.Jacobs' RGB Hex Triplet Color Chart
@rang: Expected color range:
0--all range
1--light color
2--medium range
3--deep color
with reference to http://www.jakesan.com
R: 0x00-0x33-0x66-0x99-0xcc-0xff
G: 0x00-0x33-0x66-0x99-0xcc-0xff
B: 0x00-0x33-0x66-0x99-0xcc-0xff
------------------------------------------------------------------------*/
inline EGI_16BIT_COLOR egi_color_random(enum egi_color_range range)
{
int i,j;
uint8_t color[3]; /*RGB*/
struct timeval tmval;
EGI_16BIT_COLOR retcolor;
gettimeofday(&tmval,NULL);
srand(tmval.tv_usec);
/* random number 0-5 */
for(i=0;i<3;i++)
{
#if 0 /* Step 0x33 */
j=(int)(6.0*rand()/(RAND_MAX+1.0));
color[i]= 0x33*j; /*i=0,1,2 -> R,G,B, j=0-5*/
#else /* Step 0x11 */
j=(int)(15.0*rand()/(RAND_MAX+1.0));
color[i]= 0x11*j; /*i=0,1,2 -> R,G,B, j=0-5*/
#endif
if( range > 0 && i==1) /* if not all color range */
{
/* to select G range, so as to select color range */
if( color[i] >= 0x33*(2*range-2) && color[i] <= 0x33*(2*range-1) )
{
EGI_PDEBUG(DBG_COLOR," ----------- color G =0X%02X\n",color[i]);
continue;
}
else /* retry */
{
i--;
continue;
}
}
}
retcolor=COLOR_RGB_TO16BITS(color[0],color[1],color[2]);
EGI_PDEBUG(DBG_COLOR,"egi random color GRAY: 0x%04X(16bits) / 0x%02X%02X%02X(24bits) \n",
retcolor,color[0],color[1],color[2]);
return retcolor;
}
/*-----------------------------------------------------------
Get a random color value with Min. Luma/brightness Y.
!!! TOo big Luma will cost much more time !!!
@rang: Expected color range:
0--all range
1--light color
2--medium range
3--deep color
@luman: Min. luminance of the color
-------------------------------------------------------------*/
EGI_16BIT_COLOR egi_color_random2(enum egi_color_range range, unsigned char luma)
{
EGI_16BIT_COLOR color;
/* Set LIMIT of input Luma
* Rmax=0xF8; Gmax=0xFC; Bmax=0xF8
* Max Luma=( 307*(31<<3)+604*(63<<2)+113*(31<<3) )>>10 = 250;
*/
if( luma > 220 ) {
printf("%s: Input luma is >220!\n",__func__);
}
if( luma > 250 )
luma=250;
do {
color=egi_color_random(range);
} while( egi_color_getY(color) < luma );
return color;
}
/*---------------------------------------------------
Get a random 16bit gray_color from Douglas.R.Jacobs'
RGB Hex Triplet Color Chart
rang: color range:
0--all range
1--light color
2--mid range
3--deep color
with reference to http://www.jakesan.com
R: 0x00-0x33-0x66-0x99-0xcc-0xff
G: 0x00-0x33-0x66-0x99-0xcc-0xff
B: 0x00-0x33-0x66-0x99-0xcc-0xff
---------------------------------------------------*/
EGI_16BIT_COLOR egi_colorGray_random(enum egi_color_range range)
{
int i;
uint8_t color; /*R=G=B*/
struct timeval tmval;
uint16_t ret;
gettimeofday(&tmval,NULL);
srand(tmval.tv_usec);
/* random number 0-5 */
for(;;)
{
i=(int)(15.0*rand()/(RAND_MAX+1.0));
color= 0x11*i;
if( range > 0 ) /* if not all color range */
{
/* to select R/G/B SAME range, so as to select color-GRAY range */
if( color>=0x11*5*(range-1) && color <= 0x11*(5*range-1) )
break;
else /* retry */
continue;
}
break;
}
ret=(EGI_16BIT_COLOR)COLOR_RGB_TO16BITS(color,color,color);
EGI_PDEBUG(DBG_COLOR,"egi random color GRAY: 0x%04X(16bits) / 0x%02X%02X%02X(24bits) \n",
ret,color,color,color);
return ret;
}
/*---------------------------------------------------------------------
Color Luminance/Brightness control, brightness to be increase/decreased
according to k.
@color Input color in 16bit
@k Deviation value
Note:
1. Permit Y<0, otherwise R,G,or B MAY never get to 0, when you need
to obtain a totally BLACK RGB(000) color in conversion, as of a check
point, say.
2. R,G,B each contributes differently to the luminance, with G the most and B the least !
--- RGB to YUV ---
Y=0.30R+0.59G+0.11B=(307R+604G+113G)>>10 [0-255]
U=0.493(B-Y)+128=( (505(B-Y))>>10 )+128 [0-255]
V=0.877(R-Y)+128=( (898(R-Y))>>10 )+128 [0-255]
--- YUV to RGB ---
R=Y+1.4075*(V-128)=Y+1.4075*V-1.4075*128
=(Y*4096 + 5765*V -737935)>>12
G=Y-0.3455*(U-128)-0.7169*(V-128)=Y+136-0.3455U-0.7169V
=(4096*Y+(136<<12)-1415*U-2936*V)>>12
B=Y+1.779*(U-128)=Y+1.779*U-1.779*128
=(Y*4096+7287*U-932708)>>12
---------------------------------------------------------------------*/
EGI_16BIT_COLOR egi_colorLuma_adjust(EGI_16BIT_COLOR color, int k)
{
int32_t R,G,B; /* !!! to be signed int32, same as YUV */
int32_t Y,U,V;
/* get 3*8bit R/G/B */
R = (color>>11)<<3;
G = (((color>>5)&(0b111111)))<<2;
B = (color&0b11111)<<3;
// printf(" color: 0x%02x, --- R:0x%02x -- G:0x%02x -- B:0x%02x \n",color,R,G,B);
// return (EGI_16BIT_COLOR)COLOR_RGB_TO16BITS(R,G,B);
/* convert RBG to YUV */
Y=(307*R+604*G+113*B)>>10;
U=((505*(B-Y))>>10)+128;
V=((898*(R-Y))>>10)+128;
//printf("R=%d, G=%d, B=%d ||| Y=%d, U=%d, V=%d \n",R,G,B,Y,U,V);
/* adjust Y, k>0 or k<0 */
Y += k; /* (k<<12); */
#if 0 /* HK2022-09-22 */
if(Y<0) {
Y=0;
// printf("------ Y=%d <0 -------\n",Y);
/* !! Let Y<0, otherwise R,G,or B MAY never get back to 0 when you need a totally BLACK */
// Y=0; /* DO NOT set to 0 when you need totally BLACK RBG */
}
else if(Y>255)
Y=255;
#endif
/* convert YUV back to RBG */
R=(Y*4096 + 5765*V -737935)>>12;
//printf("R'=0x%03x\n",R);
if(R<0)R=0;
else if(R>255)R=255;
G=((4096*Y-1415*U-2936*V)>>12)+136;
//printf("G'=0x%03x\n",G);
if(G<0)G=0;
else if(G>255)G=255;
B=(Y*4096+7287*U-932708)>>12;
//printf("B'=0x%03x\n",B);
if(B<0)B=0;
else if(B>255)B=255;
//printf(" Input color: 0x%02x, aft YUV adjust: R':0x%03x -- G':0x%03x -- B':0x%03x \n",color,R,G,B);
return (EGI_16BIT_COLOR)COLOR_RGB_TO16BITS(R,G,B);
}
/*----------------------------------------------
Convert YUYV to 24BIT RGB color
Y,U,V [0 255]
@src: Source of YUYV data.
@dest: Dest of RGB888 data
@w,h: Size of image blocks.
W,H MUST be multiples of 2.
@reverse: To reverse data.
Note:
1. The caller MUST ensure memory space for dest!
Return:
0 OK
<0 Fails
-----------------------------------------------*/
int egi_color_YUYV2RGB888(const unsigned char *src, unsigned char *dest, int w, int h, bool reverse)
{
int i,j;
unsigned char y1,y2,u,v;
int r1,g1,b1, r2,g2,b2;
if(src==NULL || dest==NULL)
return -1;
if(w<1 || h<1)
return -2;
for(i=0; i<h; i++) {
for(j=0; j<w; j+=2) { /* 2 pixels each time */
if(reverse) {
y1 =*(src + (((h-1-i)*w+j)<<1));
u =*(src + (((h-1-i)*w+j)<<1) +1);
y2 =*(src + (((h-1-i)*w+j)<<1) +2);
v =*(src + (((h-1-i)*w+j)<<1) +3);
}
else {
y1 =*(src + ((i*w+j)<<1));
u =*(src + ((i*w+j)<<1) +1);
y2 =*(src + ((i*w+j)<<1) +2);
v =*(src + ((i*w+j)<<1) +3);
}
r1 =(y1*4096 + 5765*v -737935)>>12;
g1 =((4096*y1-1415*u-2936*v)>>12)+136;
b1 =(y1*4096+7287*u-932708)>>12;
if(r1>255) r1=255; else if(r1<0) r1=0;
if(g1>255) g1=255; else if(g1<0) g1=0;
if(b1>255) b1=255; else if(b1<0) b1=0;
r2 =(y2*4096 + 5765*v -737935)>>12;
g2 =((4096*y2-1415*u-2936*v)>>12)+136;
b2 =(y2*4096+7287*u-932708)>>12;
if(r2>255) r2=255; else if(r2<0) r2=0;
if(g2>255) g2=255; else if(g2<0) g2=0;
if(b2>255) b2=255; else if(b2<0) b2=0;
*(dest + (i*w+j)*3) =(unsigned char)r1;
*(dest + (i*w+j)*3 +1) =(unsigned char)g1;
*(dest + (i*w+j)*3 +2) =(unsigned char)b1;
*(dest + (i*w+j)*3 +3) =(unsigned char)r2;
*(dest + (i*w+j)*3 +4) =(unsigned char)g2;
*(dest + (i*w+j)*3 +5) =(unsigned char)b2;
}
}
return 0;
}
/*----------------------------------------------
Convert YUYV to YUV
Y,U,V [0 255]
@src: Source of YUYV data.
@dest: Dest of YUV data
@w,h: Size of image blocks.
W,H MUST be multiples of 2.
@reverse: To reverse data.
Note:
1. The caller MUST ensure memory space for dest!
Return:
0 OK
<0 Fails
-----------------------------------------------*/
int egi_color_YUYV2YUV(const unsigned char *src, unsigned char *dest, int w, int h, bool reverse)
{
int i,j;
unsigned char y1,y2,u,v;
if(src==NULL || dest==NULL)
return -1;
if(w<1 || h<1)
return -2;
for(i=0; i<h; i++) {
for(j=0; j<w; j+=2) { /* 2 pixels each time */
if(reverse) {
y1 =*(src + (((h-1-i)*w+j)<<1));
u =*(src + (((h-1-i)*w+j)<<1) +1);
y2 =*(src + (((h-1-i)*w+j)<<1) +2);
v =*(src + (((h-1-i)*w+j)<<1) +3);
}
else {
y1 =*(src + ((i*w+j)<<1));
u =*(src + ((i*w+j)<<1) +1);
y2 =*(src + ((i*w+j)<<1) +2);
v =*(src + ((i*w+j)<<1) +3);
}
*(dest + (i*w+j)*3) =y1;
*(dest + (i*w+j)*3 +1) =u;
*(dest + (i*w+j)*3 +2) =v;
*(dest + (i*w+j)*3 +3) =y2;
*(dest + (i*w+j)*3 +4) =u;
*(dest + (i*w+j)*3 +5) =v;
}
}
return 0;
}
/*--------------------------------------------------
Get Y(Luma/Brightness) value from a 16BIT RGB color
as of YUV
Y=0.30R+0.59G+0.11B=(307R+604G+113B)>>10 [0 255]
---------------------------------------------------*/
unsigned char egi_color_getY(EGI_16BIT_COLOR color)
{
uint16_t R,G,B;
/* get 3*8bit R/G/B */
R = (color>>11)<<3;
G = (((color>>5)&(0b111111)))<<2;
B = (color&0b11111)<<3;
//printf(" color: 0x%02x, --- R:0x%02x -- G:0x%02x -- B:0x%02x \n",color,R,G,B);
/* convert to Y */
return (307*R+604*G+113*B)>>10;
}
/*--------------------------------------------------------------
Convert HSV to RGB
H--Hue [0 360] or X%360
S--Saturation [0 100%]*10000
V--Value [0 255]
Note: All vars to be float type will be more accurate!
---------------------------------------------------------------*/
EGI_16BIT_COLOR egi_color_HSV2RGB(const EGI_HSV_COLOR *hsv)
{
unsigned int h, hi,p,q,t,v; /* float type will be more accurate! */
float f;
EGI_16BIT_COLOR color;
if(hsv==NULL)
return WEGI_COLOR_BLACK;
/* Convert hsv->h to [0 360] */
if( hsv->h < 0)
h=360+(hsv->h%360);
else
h=hsv->h%360;
v=hsv->v;
hi=(h/60)%6;
f=h/60.0-hi;
p=v*(10000-hsv->s)/10000;
q=round(v*(10000-f*hsv->s)/10000);
t=round(v*(10000-(1-f)*hsv->s)/10000);
switch(hi) {
case 0:
color=COLOR_RGB_TO16BITS(v,t,p); break;
case 1:
color=COLOR_RGB_TO16BITS(q,v,p); break;
case 2:
color=COLOR_RGB_TO16BITS(p,v,t); break;
case 3:
color=COLOR_RGB_TO16BITS(p,q,v); break;
case 4:
color=COLOR_RGB_TO16BITS(t,p,v); break;
case 5:
color=COLOR_RGB_TO16BITS(v,p,q); break;
}
return color;
}
/*--------------------------------------------------------------
Convert RGB to HSV
H--Hue [0 360] or X%360
S--Saturation [0 100%]*10000
V--Value [0 255]
Note: All vars to be float type will be more accurate!
Return:
0 OK
<0 Fails
---------------------------------------------------------------*/
int egi_color_RGB2HSV(EGI_16BIT_COLOR color, EGI_HSV_COLOR *hsv)
{
uint8_t R,G,B;
uint8_t max,min;
if(hsv==NULL) return -1;
/* Get 3*8bit R/G/B */
R = (color>>11)<<3;
G = (((color>>5)&(0b111111)))<<2;
B = (color&0b11111)<<3;
/* Get max and min */
max=R;
if( max < G ) max=G;
if( max < B ) max=B;
min=R;
if( min > G ) min=G;
if( min > B ) min=B;
/* Cal. hue */
if( max==min )
hsv->h=0;
else if(max==R && G>=B)
hsv->h=60*(G-B)/(max-min)+0;
else if(max==R && G<B)
hsv->h=60*(G-B)/(max-min)+360;
else if(max==G)
hsv->h=60*(B-R)/(max-min)+120;
else if(max==B)
hsv->h=60*(R-G)/(max-min)+240;
/* Cal. satuarion */
if(max==0)
hsv->s=0;
else
hsv->s=10000*(max-min)/max;
/* Cal. value */
hsv->v=max;
return 0;
}
/*-------------------------------------------------------
Create an EGI_COLOR_BANDMAP holding 1 band, and set
its default color and len(>=1).
@color: Initial color for the map.
@len: Initial total length of the map.
Min 1.
Return:
An pointer OK
NULL Fails
-------------------------------------------------------*/
EGI_COLOR_BANDMAP *egi_colorBandMap_create(EGI_16BIT_COLOR color, unsigned int len)
{
EGI_COLOR_BANDMAP *map;
/* Limit len */
if(len<=0)len=1;
/* Calloc map */
map=calloc(1,sizeof(EGI_COLOR_BANDMAP));
if(map==NULL)
return NULL;
map->bands=calloc(COLORMAP_BANDS_GROW_SIZE, sizeof(EGI_COLOR_BAND));
if(map->bands==NULL) {
free(map);
return NULL;
}
/* Set default_color to the first band */
map->default_color=color;
map->bands[0].pos=0;
map->bands[0].len=len;
map->bands[0].color=color;
/* Set other params */
map->size=1; /* Init. one band */
map->capacity=COLORMAP_BANDS_GROW_SIZE;
return map;
}
/*------------------------------
Free an EGI_COLOR_BANDMAP.
-------------------------------*/
void egi_colorBandMap_free(EGI_COLOR_BANDMAP **map)
{
if(map==NULL || *map==NULL)
return;
free( (*map)->bands );
free( *map );
*map=NULL;
}
/* --------------------------------------
Allocate more sapce for map->bands.
Return:
0 OK
<0 Fails
----------------------------------------*/
int egi_colorBandMap_memGrowBands(EGI_COLOR_BANDMAP *map, unsigned int more_bands)
{
if(map==NULL || map->bands==NULL)
return -1;
printf("%s: grow mem. space from %u to %u (bands).\n", __func__, map->capacity, map->capacity+more_bands);
/* Check capacity and growsize */
if( egi_mem_grow( (void **)&map->bands, map->capacity*sizeof(EGI_COLOR_BAND), more_bands*sizeof(EGI_COLOR_BAND)) !=0 ) {
printf("%s: Fail to mem grow map->bands!\n", __func__);
return -2;
}
else {
map->capacity += more_bands;
}
return 0;
}
/*-----------------------------------------------------------------------
Pick color from the band map,
NOPE !!! XXXX --- The color is selected in the band map BEFORE pos
NOPE !!! This is differenct from egi_colorBandMap_get_bandIndex();
Note:
1. If map unavailbale, return 0 as black;
2. If map->size==0, then return map->default_color.
3. If position is out of range, then it returns the color of last band.
TODO: A fast way, binary search.
@map: An EGI_COLOR_BANDMAP pointer
@pos: Position of the band map.
----------------------------------------------------------------------*/
EGI_16BIT_COLOR egi_colorBandMap_pickColor(const EGI_COLOR_BANDMAP *map, unsigned int pos)
{
unsigned int i;
if(map==NULL || map->bands==NULL)
return 0;
/* If map->size<1 */
if(map->size==0) {
//return 0;
return map->default_color;
}
/* If pos==0 */
if(pos==0)
return map->bands[0].color;
/* Most frequent case for editor input: Pos is the last inserting position!! */
else if(pos == map->bands[map->size-1].pos+map->bands[map->size-1].len ) {
return map->bands[ map->size-1 ].color;
}
/* Pick color value */
for(i=0; i< map->size-1; i++) {
if( pos >= map->bands[i].pos && pos < map->bands[i+1].pos )
return map->bands[i].color;
}
/* pos at the last band, or out of range: Same color as the last band */
return map->bands[ map->size-1 ].color;
}
/*------------------------------------------------------------------------
Get index of map->bands[] accroding to pos.
!!! --- The Index is selected for the first band as pos>=bands[index].pos --- !!!
Note:
1. If map is unavailable, it returns 0!
2. !!! If pos out of range, it returns the last band index(map->size-1)
!!! Especially when map->size=0, it still returns index as 0!
TODO: A fast way. binary search!
@map: An EGI_COLOR_BANDMAP pointer
@pos: Position of the band map.
--------------------------------------------------------------------------*/
unsigned int egi_colorBandMap_get_bandIndex(const EGI_COLOR_BANDMAP *map, unsigned int pos)
{
unsigned int i;
if(map==NULL || map->bands==NULL)
return 0;
if(map->size==0)
return 0;
/* Most frequent case for editor input: Pos is the last inserting position!! */
else if(pos == map->bands[map->size-1].pos+map->bands[map->size-1].len ) {
return map->size-1;
}
for(i=0; i< map->size-1; i++) {
if( pos >= map->bands[i].pos && pos < map->bands[i+1].pos )
return i;
}
/* Any way, same as the last band! */
return map->size-1;
}
/*---------------------------------------------------------------------
Insert a color band into BANDMAP. !!! A recursive function !!!
Note:
0. Pos MUST be 0 if the map is empty with map->size==0.
1. If pos out of range, then it will fail!
!!! It's OK to insert just at bottom/end of the last band.
2. If the inserted band holds the same color as the located original band,
then just expand the original band.
3. All followed map->bans[].pos MUST be updated after insersion!
@map: An EGI_COLOR_BANDMAP pointer
@pos: Inserting position of the map, as start point of the new band.
@len: length of the inserted band.
@color: Color of the inserted band.
Return:
0 OK
<0 Fails, or out of range!
-------------------------------------------------------------------------*/
int egi_colorBandMap_insertBand(EGI_COLOR_BANDMAP *map, unsigned int pos, unsigned int len, EGI_16BIT_COLOR color)
{
unsigned int index; /* pos located in map->bands[index] */
unsigned int i;
if(map==NULL || map->bands==NULL)
return -1;
/* A. If insert to an empty map, then pos MUST be 0! */
if(map->size==0) {
if(pos!=0) {
printf("%s: Insert into an empty map, pos MUST be 0!\n", __func__);
return -2;
}
else {
printf("%s: Insert into an empty map!\n", __func__);
map->bands[0].pos=0;
map->bands[0].color=color;
map->bands[0].len=len;
map->size +=1;
return 0;
}
}
/* B. map->size>0: If out of range (BUT! it's OK to insert just at end of the band. == see following case: 4. ELSE IF ) */
if( pos > map->bands[map->size-1].pos + map->bands[map->size-1].len ) {
printf("%s: Insert pos is out of range!\n", __func__);
return -2;
}
/* Get index of band, as pos refers, (If pos out of range it will return the last band index, we ruled out the case as above.) */
index=egi_colorBandMap_get_bandIndex(map, pos);
/* 1. If just insert at the beginning of the map */
if(pos==0) {
printf("%s: Insert a band at pos=0!\n", __func__);
/* 1.1 If same color as the first band, merge with it. (map->size==0 ruled out) */
if( map->bands[0].color==color ) {
map->bands[0].len += len;
/* Update all followed bands */
for(i=1; i < map->size; i++)
map->bands[i].pos+=len;
return 0;
}
/* 1.2 Not same color, insert a new band then. */
else {
/* Grow bands mem */
if(map->capacity-map->size <1){
if( egi_colorBandMap_memGrowBands(map,COLORMAP_BANDS_GROW_SIZE)!=0 ) {
printf("%s: Fail to mem grow map->bands(case 1.2)!\n", __func__);
return -3;
}
}
/* Set aside bands[0] space for the new band */
/* NOTE: map->size == 0 is ruled out at very beginning. */
memmove( map->bands+1, map->bands+0, sizeof(EGI_COLOR_BAND)*map->size );
/* Increase map->size and assign new band */
map->size +=1;
map->bands[0].pos=0;
map->bands[0].len=len;
map->bands[0].color=color;
/* Update all followed bands */
for(i=1; i < map->size; i++)
map->bands[i].pos+=len;
return 0;
}
}
/* 2. If insert just at start pos of a band */
else if( map->bands[index].pos==pos ) {
// printf("%s: Insert at start of bands[%u]!\n", __func__, index);
/* 2.1 If same color as previous indexed band, merge with it. */
if( map->bands[index-1].color==color ) {
map->bands[index-1].len += len;
/* Update all pos of bands following bands[index-1] */