This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathximadsp.cpp
3767 lines (3388 loc) · 110 KB
/
ximadsp.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
// xImaDsp.cpp : DSP functions
/* 07/08/2001 v1.00 - Davide Pizzolato - www.xdp.it
* CxImage version 7.0.1 07/Jan/2011
*/
#include "ximage.h"
#include "ximaiter.h"
#if CXIMAGE_SUPPORT_DSP
////////////////////////////////////////////////////////////////////////////////
/**
* Converts the image to B&W.
* The OptimalThreshold() function can be used for calculating the optimal threshold.
* \param level: the lightness threshold.
* \return true if everything is ok
*/
bool CxImage::Threshold(uint8_t level)
{
if (!pDib) return false;
if (head.biBitCount == 1) return true;
GrayScale();
CxImage tmp(head.biWidth,head.biHeight,1);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
for (int32_t y=0;y<head.biHeight;y++){
info.nProgress = (int32_t)(100*y/head.biHeight);
if (info.nEscape) break;
for (int32_t x=0;x<head.biWidth;x++){
if (BlindGetPixelIndex(x,y)>level)
tmp.BlindSetPixelIndex(x,y,1);
else
tmp.BlindSetPixelIndex(x,y,0);
}
}
tmp.SetPaletteColor(0,0,0,0);
tmp.SetPaletteColor(1,255,255,255);
Transfer(tmp);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Converts the image to B&W, using a threshold mask
* \param pThresholdMask: the lightness threshold mask.
* the pThresholdMask image must be grayscale with same with and height of the current image
* \return true if everything is ok
*/
bool CxImage::Threshold(CxImage* pThresholdMask)
{
if (!pDib) return false;
if (head.biBitCount == 1) return true;
if (!pThresholdMask) return false;
if (!pThresholdMask->IsValid() ||
!pThresholdMask->IsGrayScale() ||
pThresholdMask->GetWidth() != GetWidth() ||
pThresholdMask->GetHeight() != GetHeight()){
strcpy(info.szLastError,"invalid ThresholdMask");
return false;
}
GrayScale();
CxImage tmp(head.biWidth,head.biHeight,1);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
for (int32_t y=0;y<head.biHeight;y++){
info.nProgress = (int32_t)(100*y/head.biHeight);
if (info.nEscape) break;
for (int32_t x=0;x<head.biWidth;x++){
if (BlindGetPixelIndex(x,y)>pThresholdMask->BlindGetPixelIndex(x,y))
tmp.BlindSetPixelIndex(x,y,1);
else
tmp.BlindSetPixelIndex(x,y,0);
}
}
tmp.SetPaletteColor(0,0,0,0);
tmp.SetPaletteColor(1,255,255,255);
Transfer(tmp);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Filters only the pixels with a lightness less (or more) than the threshold level,
* and preserves the colors for the unfiltered pixels.
* \param level = the lightness threshold.
* \param bDirection = false: filter dark pixels, true: filter light pixels
* \param nBkgndColor = filtered pixels are set to nBkgndColor color
* \param bSetAlpha = if true, sets also the alpha component for the filtered pixels, with nBkgndColor.rgbReserved
* \return true if everything is ok
* \author [DP], [wangsongtao]
*/
////////////////////////////////////////////////////////////////////////////////
bool CxImage::Threshold2(uint8_t level, bool bDirection, RGBQUAD nBkgndColor, bool bSetAlpha)
{
if (!pDib) return false;
if (head.biBitCount == 1) return true;
CxImage tmp(*this, true, false, false);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
tmp.GrayScale();
int32_t xmin,xmax,ymin,ymax;
if (pSelection){
xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
} else {
xmin = ymin = 0;
xmax = head.biWidth; ymax=head.biHeight;
}
for(int32_t y=ymin; y<ymax; y++){
info.nProgress = (int32_t)(100*y/head.biHeight);
if (info.nEscape) break;
for(int32_t x=xmin; x<xmax; x++){
#if CXIMAGE_SUPPORT_SELECTION
if (BlindSelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
{
uint8_t i = tmp.BlindGetPixelIndex(x,y);
if (!bDirection && i<level) BlindSetPixelColor(x,y,nBkgndColor,bSetAlpha);
if (bDirection && i>=level) BlindSetPixelColor(x,y,nBkgndColor,bSetAlpha);
}
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract RGB channels from the image. Each channel is an 8 bit grayscale image.
* \param r,g,b: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitRGB(CxImage* r,CxImage* g,CxImage* b)
{
if (!pDib) return false;
if (r==NULL && g==NULL && b==NULL) return false;
CxImage tmpr(head.biWidth,head.biHeight,8);
CxImage tmpg(head.biWidth,head.biHeight,8);
CxImage tmpb(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(int32_t y=0; y<head.biHeight; y++){
for(int32_t x=0; x<head.biWidth; x++){
color = BlindGetPixelColor(x,y);
if (r) tmpr.BlindSetPixelIndex(x,y,color.rgbRed);
if (g) tmpg.BlindSetPixelIndex(x,y,color.rgbGreen);
if (b) tmpb.BlindSetPixelIndex(x,y,color.rgbBlue);
}
}
if (r) tmpr.SetGrayPalette();
if (g) tmpg.SetGrayPalette();
if (b) tmpb.SetGrayPalette();
/*for(int32_t j=0; j<256; j++){
uint8_t i=(uint8_t)j;
if (r) tmpr.SetPaletteColor(i,i,0,0);
if (g) tmpg.SetPaletteColor(i,0,i,0);
if (b) tmpb.SetPaletteColor(i,0,0,i);
}*/
if (r) r->Transfer(tmpr);
if (g) g->Transfer(tmpg);
if (b) b->Transfer(tmpb);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract CMYK channels from the image. Each channel is an 8 bit grayscale image.
* \param c,m,y,k: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitCMYK(CxImage* c,CxImage* m,CxImage* y,CxImage* k)
{
if (!pDib) return false;
if (c==NULL && m==NULL && y==NULL && k==NULL) return false;
CxImage tmpc(head.biWidth,head.biHeight,8);
CxImage tmpm(head.biWidth,head.biHeight,8);
CxImage tmpy(head.biWidth,head.biHeight,8);
CxImage tmpk(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(int32_t yy=0; yy<head.biHeight; yy++){
for(int32_t xx=0; xx<head.biWidth; xx++){
color = BlindGetPixelColor(xx,yy);
if (c) tmpc.BlindSetPixelIndex(xx,yy,(uint8_t)(255-color.rgbRed));
if (m) tmpm.BlindSetPixelIndex(xx,yy,(uint8_t)(255-color.rgbGreen));
if (y) tmpy.BlindSetPixelIndex(xx,yy,(uint8_t)(255-color.rgbBlue));
if (k) tmpk.BlindSetPixelIndex(xx,yy,(uint8_t)RGB2GRAY(color.rgbRed,color.rgbGreen,color.rgbBlue));
}
}
if (c) tmpc.SetGrayPalette();
if (m) tmpm.SetGrayPalette();
if (y) tmpy.SetGrayPalette();
if (k) tmpk.SetGrayPalette();
if (c) c->Transfer(tmpc);
if (m) m->Transfer(tmpm);
if (y) y->Transfer(tmpy);
if (k) k->Transfer(tmpk);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract YUV channels from the image. Each channel is an 8 bit grayscale image.
* \param y,u,v: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitYUV(CxImage* y,CxImage* u,CxImage* v)
{
if (!pDib) return false;
if (y==NULL && u==NULL && v==NULL) return false;
CxImage tmpy(head.biWidth,head.biHeight,8);
CxImage tmpu(head.biWidth,head.biHeight,8);
CxImage tmpv(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(int32_t yy=0; yy<head.biHeight; yy++){
for(int32_t x=0; x<head.biWidth; x++){
color = RGBtoYUV(BlindGetPixelColor(x,yy));
if (y) tmpy.BlindSetPixelIndex(x,yy,color.rgbRed);
if (u) tmpu.BlindSetPixelIndex(x,yy,color.rgbGreen);
if (v) tmpv.BlindSetPixelIndex(x,yy,color.rgbBlue);
}
}
if (y) tmpy.SetGrayPalette();
if (u) tmpu.SetGrayPalette();
if (v) tmpv.SetGrayPalette();
if (y) y->Transfer(tmpy);
if (u) u->Transfer(tmpu);
if (v) v->Transfer(tmpv);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract YIQ channels from the image. Each channel is an 8 bit grayscale image.
* \param y,i,q: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitYIQ(CxImage* y,CxImage* i,CxImage* q)
{
if (!pDib) return false;
if (y==NULL && i==NULL && q==NULL) return false;
CxImage tmpy(head.biWidth,head.biHeight,8);
CxImage tmpi(head.biWidth,head.biHeight,8);
CxImage tmpq(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(int32_t yy=0; yy<head.biHeight; yy++){
for(int32_t x=0; x<head.biWidth; x++){
color = RGBtoYIQ(BlindGetPixelColor(x,yy));
if (y) tmpy.BlindSetPixelIndex(x,yy,color.rgbRed);
if (i) tmpi.BlindSetPixelIndex(x,yy,color.rgbGreen);
if (q) tmpq.BlindSetPixelIndex(x,yy,color.rgbBlue);
}
}
if (y) tmpy.SetGrayPalette();
if (i) tmpi.SetGrayPalette();
if (q) tmpq.SetGrayPalette();
if (y) y->Transfer(tmpy);
if (i) i->Transfer(tmpi);
if (q) q->Transfer(tmpq);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract XYZ channels from the image. Each channel is an 8 bit grayscale image.
* \param x,y,z: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitXYZ(CxImage* x,CxImage* y,CxImage* z)
{
if (!pDib) return false;
if (x==NULL && y==NULL && z==NULL) return false;
CxImage tmpx(head.biWidth,head.biHeight,8);
CxImage tmpy(head.biWidth,head.biHeight,8);
CxImage tmpz(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(int32_t yy=0; yy<head.biHeight; yy++){
for(int32_t xx=0; xx<head.biWidth; xx++){
color = RGBtoXYZ(BlindGetPixelColor(xx,yy));
if (x) tmpx.BlindSetPixelIndex(xx,yy,color.rgbRed);
if (y) tmpy.BlindSetPixelIndex(xx,yy,color.rgbGreen);
if (z) tmpz.BlindSetPixelIndex(xx,yy,color.rgbBlue);
}
}
if (x) tmpx.SetGrayPalette();
if (y) tmpy.SetGrayPalette();
if (z) tmpz.SetGrayPalette();
if (x) x->Transfer(tmpx);
if (y) y->Transfer(tmpy);
if (z) z->Transfer(tmpz);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Extract HSL channels from the image. Each channel is an 8 bit grayscale image.
* \param h,s,l: pointers to CxImage objects, to store the splited channels
* \return true if everything is ok
*/
bool CxImage::SplitHSL(CxImage* h,CxImage* s,CxImage* l)
{
if (!pDib) return false;
if (h==NULL && s==NULL && l==NULL) return false;
CxImage tmph(head.biWidth,head.biHeight,8);
CxImage tmps(head.biWidth,head.biHeight,8);
CxImage tmpl(head.biWidth,head.biHeight,8);
RGBQUAD color;
for(int32_t y=0; y<head.biHeight; y++){
for(int32_t x=0; x<head.biWidth; x++){
color = RGBtoHSL(BlindGetPixelColor(x,y));
if (h) tmph.BlindSetPixelIndex(x,y,color.rgbRed);
if (s) tmps.BlindSetPixelIndex(x,y,color.rgbGreen);
if (l) tmpl.BlindSetPixelIndex(x,y,color.rgbBlue);
}
}
if (h) tmph.SetGrayPalette();
if (s) tmps.SetGrayPalette();
if (l) tmpl.SetGrayPalette();
/* pseudo-color generator for hue channel (visual debug)
if (h) for(int32_t j=0; j<256; j++){
uint8_t i=(uint8_t)j;
RGBQUAD hsl={120,240,i,0};
tmph.SetPaletteColor(i,HSLtoRGB(hsl));
}*/
if (h) h->Transfer(tmph);
if (s) s->Transfer(tmps);
if (l) l->Transfer(tmpl);
return true;
}
////////////////////////////////////////////////////////////////////////////////
#define HSLMAX 255 /* H,L, and S vary over 0-HSLMAX */
#define RGBMAX 255 /* R,G, and B vary over 0-RGBMAX */
/* HSLMAX BEST IF DIVISIBLE BY 6 */
/* RGBMAX, HSLMAX must each fit in a uint8_t. */
/* Hue is undefined if Saturation is 0 (grey-scale) */
/* This value determines where the Hue scrollbar is */
/* initially set for achromatic colors */
#define HSLUNDEFINED (HSLMAX*2/3)
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::RGBtoHSL(RGBQUAD lRGBColor)
{
uint8_t R,G,B; /* input RGB values */
uint8_t H,L,S; /* output HSL values */
uint8_t cMax,cMin; /* max and min RGB values */
uint16_t Rdelta,Gdelta,Bdelta; /* intermediate value: % of spread from max*/
R = lRGBColor.rgbRed; /* get R, G, and B out of uint32_t */
G = lRGBColor.rgbGreen;
B = lRGBColor.rgbBlue;
cMax = max( max(R,G), B); /* calculate lightness */
cMin = min( min(R,G), B);
L = (uint8_t)((((cMax+cMin)*HSLMAX)+RGBMAX)/(2*RGBMAX));
if (cMax==cMin){ /* r=g=b --> achromatic case */
S = 0; /* saturation */
H = HSLUNDEFINED; /* hue */
} else { /* chromatic case */
if (L <= (HSLMAX/2)) /* saturation */
S = (uint8_t)((((cMax-cMin)*HSLMAX)+((cMax+cMin)/2))/(cMax+cMin));
else
S = (uint8_t)((((cMax-cMin)*HSLMAX)+((2*RGBMAX-cMax-cMin)/2))/(2*RGBMAX-cMax-cMin));
/* hue */
Rdelta = (uint16_t)((((cMax-R)*(HSLMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
Gdelta = (uint16_t)((((cMax-G)*(HSLMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
Bdelta = (uint16_t)((((cMax-B)*(HSLMAX/6)) + ((cMax-cMin)/2) ) / (cMax-cMin));
if (R == cMax)
H = (uint8_t)(Bdelta - Gdelta);
else if (G == cMax)
H = (uint8_t)((HSLMAX/3) + Rdelta - Bdelta);
else /* B == cMax */
H = (uint8_t)(((2*HSLMAX)/3) + Gdelta - Rdelta);
// if (H < 0) H += HSLMAX; //always false
if (H > HSLMAX) H -= HSLMAX;
}
RGBQUAD hsl={L,S,H,0};
return hsl;
}
////////////////////////////////////////////////////////////////////////////////
float CxImage::HueToRGB(float n1,float n2, float hue)
{
//<F. Livraghi> fixed implementation for HSL2RGB routine
float rValue;
if (hue > 360)
hue = hue - 360;
else if (hue < 0)
hue = hue + 360;
if (hue < 60)
rValue = n1 + (n2-n1)*hue/60.0f;
else if (hue < 180)
rValue = n2;
else if (hue < 240)
rValue = n1+(n2-n1)*(240-hue)/60;
else
rValue = n1;
return rValue;
}
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::HSLtoRGB(COLORREF cHSLColor)
{
return HSLtoRGB(RGBtoRGBQUAD(cHSLColor));
}
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::HSLtoRGB(RGBQUAD lHSLColor)
{
//<F. Livraghi> fixed implementation for HSL2RGB routine
float h,s,l;
float m1,m2;
uint8_t r,g,b;
h = (float)lHSLColor.rgbRed * 360.0f/255.0f;
s = (float)lHSLColor.rgbGreen/255.0f;
l = (float)lHSLColor.rgbBlue/255.0f;
if (l <= 0.5) m2 = l * (1+s);
else m2 = l + s - l*s;
m1 = 2 * l - m2;
if (s == 0) {
r=g=b=(uint8_t)(l*255.0f);
} else {
r = (uint8_t)(HueToRGB(m1,m2,h+120) * 255.0f);
g = (uint8_t)(HueToRGB(m1,m2,h) * 255.0f);
b = (uint8_t)(HueToRGB(m1,m2,h-120) * 255.0f);
}
RGBQUAD rgb = {b,g,r,0};
return rgb;
}
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::YUVtoRGB(RGBQUAD lYUVColor)
{
int32_t U,V,R,G,B;
float Y = lYUVColor.rgbRed;
U = lYUVColor.rgbGreen - 128;
V = lYUVColor.rgbBlue - 128;
// R = (int32_t)(1.164 * Y + 2.018 * U);
// G = (int32_t)(1.164 * Y - 0.813 * V - 0.391 * U);
// B = (int32_t)(1.164 * Y + 1.596 * V);
R = (int32_t)( Y + 1.403f * V);
G = (int32_t)( Y - 0.344f * U - 0.714f * V);
B = (int32_t)( Y + 1.770f * U);
R= min(255,max(0,R));
G= min(255,max(0,G));
B= min(255,max(0,B));
RGBQUAD rgb={(uint8_t)B,(uint8_t)G,(uint8_t)R,0};
return rgb;
}
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::RGBtoYUV(RGBQUAD lRGBColor)
{
int32_t Y,U,V,R,G,B;
R = lRGBColor.rgbRed;
G = lRGBColor.rgbGreen;
B = lRGBColor.rgbBlue;
// Y = (int32_t)( 0.257 * R + 0.504 * G + 0.098 * B);
// U = (int32_t)( 0.439 * R - 0.368 * G - 0.071 * B + 128);
// V = (int32_t)(-0.148 * R - 0.291 * G + 0.439 * B + 128);
Y = (int32_t)(0.299f * R + 0.587f * G + 0.114f * B);
U = (int32_t)((B-Y) * 0.565f + 128);
V = (int32_t)((R-Y) * 0.713f + 128);
Y= min(255,max(0,Y));
U= min(255,max(0,U));
V= min(255,max(0,V));
RGBQUAD yuv={(uint8_t)V,(uint8_t)U,(uint8_t)Y,0};
return yuv;
}
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::YIQtoRGB(RGBQUAD lYIQColor)
{
int32_t I,Q,R,G,B;
float Y = lYIQColor.rgbRed;
I = lYIQColor.rgbGreen - 128;
Q = lYIQColor.rgbBlue - 128;
R = (int32_t)( Y + 0.956f * I + 0.621f * Q);
G = (int32_t)( Y - 0.273f * I - 0.647f * Q);
B = (int32_t)( Y - 1.104f * I + 1.701f * Q);
R= min(255,max(0,R));
G= min(255,max(0,G));
B= min(255,max(0,B));
RGBQUAD rgb={(uint8_t)B,(uint8_t)G,(uint8_t)R,0};
return rgb;
}
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::RGBtoYIQ(RGBQUAD lRGBColor)
{
int32_t Y,I,Q,R,G,B;
R = lRGBColor.rgbRed;
G = lRGBColor.rgbGreen;
B = lRGBColor.rgbBlue;
Y = (int32_t)( 0.2992f * R + 0.5868f * G + 0.1140f * B);
I = (int32_t)( 0.5960f * R - 0.2742f * G - 0.3219f * B + 128);
Q = (int32_t)( 0.2109f * R - 0.5229f * G + 0.3120f * B + 128);
Y= min(255,max(0,Y));
I= min(255,max(0,I));
Q= min(255,max(0,Q));
RGBQUAD yiq={(uint8_t)Q,(uint8_t)I,(uint8_t)Y,0};
return yiq;
}
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::XYZtoRGB(RGBQUAD lXYZColor)
{
int32_t X,Y,Z,R,G,B;
X = lXYZColor.rgbRed;
Y = lXYZColor.rgbGreen;
Z = lXYZColor.rgbBlue;
double k=1.088751;
R = (int32_t)( 3.240479f * X - 1.537150f * Y - 0.498535f * Z * k);
G = (int32_t)( -0.969256f * X + 1.875992f * Y + 0.041556f * Z * k);
B = (int32_t)( 0.055648f * X - 0.204043f * Y + 1.057311f * Z * k);
R= min(255,max(0,R));
G= min(255,max(0,G));
B= min(255,max(0,B));
RGBQUAD rgb={(uint8_t)B,(uint8_t)G,(uint8_t)R,0};
return rgb;
}
////////////////////////////////////////////////////////////////////////////////
RGBQUAD CxImage::RGBtoXYZ(RGBQUAD lRGBColor)
{
int32_t X,Y,Z,R,G,B;
R = lRGBColor.rgbRed;
G = lRGBColor.rgbGreen;
B = lRGBColor.rgbBlue;
X = (int32_t)( 0.412453f * R + 0.357580f * G + 0.180423f * B);
Y = (int32_t)( 0.212671f * R + 0.715160f * G + 0.072169f * B);
Z = (int32_t)((0.019334f * R + 0.119193f * G + 0.950227f * B)*0.918483657f);
//X= min(255,max(0,X));
//Y= min(255,max(0,Y));
//Z= min(255,max(0,Z));
RGBQUAD xyz={(uint8_t)Z,(uint8_t)Y,(uint8_t)X,0};
return xyz;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Generates a "rainbow" palette with saturated colors
* \param correction: 1 generates a single hue spectrum. 0.75 is nice for scientific applications.
*/
void CxImage::HuePalette(float correction)
{
if (head.biClrUsed==0) return;
for(uint32_t j=0; j<head.biClrUsed; j++){
uint8_t i=(uint8_t)(j*correction*(255/(head.biClrUsed-1)));
RGBQUAD hsl={120,240,i,0};
SetPaletteColor((uint8_t)j,HSLtoRGB(hsl));
}
}
////////////////////////////////////////////////////////////////////////////////
/**
* Replaces the original hue and saturation values.
* \param hue: hue
* \param sat: saturation
* \param blend: can be from 0 (no effect) to 1 (full effect)
* \return true if everything is ok
*/
bool CxImage::Colorize(uint8_t hue, uint8_t sat, float blend)
{
if (!pDib) return false;
if (blend < 0.0f) blend = 0.0f;
if (blend > 1.0f) blend = 1.0f;
int32_t a0 = (int32_t)(256*blend);
int32_t a1 = 256 - a0;
bool bFullBlend = false;
if (blend > 0.999f) bFullBlend = true;
RGBQUAD color,hsl;
if (head.biClrUsed==0){
int32_t xmin,xmax,ymin,ymax;
if (pSelection){
xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
} else {
xmin = ymin = 0;
xmax = head.biWidth; ymax=head.biHeight;
}
for(int32_t y=ymin; y<ymax; y++){
info.nProgress = (int32_t)(100*(y-ymin)/(ymax-ymin));
if (info.nEscape) break;
for(int32_t x=xmin; x<xmax; x++){
#if CXIMAGE_SUPPORT_SELECTION
if (BlindSelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
{
if (bFullBlend){
color = RGBtoHSL(BlindGetPixelColor(x,y));
color.rgbRed=hue;
color.rgbGreen=sat;
BlindSetPixelColor(x,y,HSLtoRGB(color));
} else {
color = BlindGetPixelColor(x,y);
hsl.rgbRed=hue;
hsl.rgbGreen=sat;
hsl.rgbBlue = (uint8_t)RGB2GRAY(color.rgbRed,color.rgbGreen,color.rgbBlue);
hsl = HSLtoRGB(hsl);
//BlendPixelColor(x,y,hsl,blend);
//color.rgbRed = (uint8_t)(hsl.rgbRed * blend + color.rgbRed * (1.0f - blend));
//color.rgbBlue = (uint8_t)(hsl.rgbBlue * blend + color.rgbBlue * (1.0f - blend));
//color.rgbGreen = (uint8_t)(hsl.rgbGreen * blend + color.rgbGreen * (1.0f - blend));
color.rgbRed = (uint8_t)((hsl.rgbRed * a0 + color.rgbRed * a1)>>8);
color.rgbBlue = (uint8_t)((hsl.rgbBlue * a0 + color.rgbBlue * a1)>>8);
color.rgbGreen = (uint8_t)((hsl.rgbGreen * a0 + color.rgbGreen * a1)>>8);
BlindSetPixelColor(x,y,color);
}
}
}
}
} else {
for(uint32_t j=0; j<head.biClrUsed; j++){
if (bFullBlend){
color = RGBtoHSL(GetPaletteColor((uint8_t)j));
color.rgbRed=hue;
color.rgbGreen=sat;
SetPaletteColor((uint8_t)j,HSLtoRGB(color));
} else {
color = GetPaletteColor((uint8_t)j);
hsl.rgbRed=hue;
hsl.rgbGreen=sat;
hsl.rgbBlue = (uint8_t)RGB2GRAY(color.rgbRed,color.rgbGreen,color.rgbBlue);
hsl = HSLtoRGB(hsl);
color.rgbRed = (uint8_t)(hsl.rgbRed * blend + color.rgbRed * (1.0f - blend));
color.rgbBlue = (uint8_t)(hsl.rgbBlue * blend + color.rgbBlue * (1.0f - blend));
color.rgbGreen = (uint8_t)(hsl.rgbGreen * blend + color.rgbGreen * (1.0f - blend));
SetPaletteColor((uint8_t)j,color);
}
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Changes the brightness and the contrast of the image.
* \param brightness: can be from -255 to 255, if brightness is negative, the image becomes dark.
* \param contrast: can be from -100 to 100, the neutral value is 0.
* \return true if everything is ok
*/
bool CxImage::Light(int32_t brightness, int32_t contrast)
{
if (!pDib) return false;
float c=(100 + contrast)/100.0f;
brightness+=128;
uint8_t cTable[256]; //<nipper>
for (int32_t i=0;i<256;i++) {
cTable[i] = (uint8_t)max(0,min(255,(int32_t)((i-128)*c + brightness + 0.5f)));
}
return Lut(cTable);
}
////////////////////////////////////////////////////////////////////////////////
/**
* \return mean lightness of the image. Useful with Threshold() and Light()
*/
float CxImage::Mean()
{
if (!pDib) return 0;
CxImage tmp(*this,true);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
tmp.GrayScale();
float sum=0;
int32_t xmin,xmax,ymin,ymax;
if (pSelection){
xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
} else {
xmin = ymin = 0;
xmax = head.biWidth; ymax=head.biHeight;
}
if (xmin==xmax || ymin==ymax) return (float)0.0;
uint8_t *iSrc=tmp.info.pImage;
iSrc += tmp.info.dwEffWidth*ymin; // necessary for selections <Admir Hodzic>
for(int32_t y=ymin; y<ymax; y++){
info.nProgress = (int32_t)(100*(y-ymin)/(ymax-ymin)); //<zhanghk><Anatoly Ivasyuk>
for(int32_t x=xmin; x<xmax; x++){
sum+=iSrc[x];
}
iSrc+=tmp.info.dwEffWidth;
}
return sum/(xmax-xmin)/(ymax-ymin);
}
////////////////////////////////////////////////////////////////////////////////
/**
* 2D linear filter
* \param kernel: convolving matrix, in row format.
* \param Ksize: size of the kernel.
* \param Kfactor: normalization constant.
* \param Koffset: bias.
* \verbatim Example: the "soften" filter uses this kernel:
1 1 1
1 8 1
1 1 1
the function needs: kernel={1,1,1,1,8,1,1,1,1}; Ksize=3; Kfactor=16; Koffset=0; \endverbatim
* \return true if everything is ok
*/
bool CxImage::Filter(int32_t* kernel, int32_t Ksize, int32_t Kfactor, int32_t Koffset)
{
if (!pDib) return false;
int32_t k2 = Ksize/2;
int32_t kmax= Ksize-k2;
int32_t r,g,b,i;
int32_t ksumcur,ksumtot;
RGBQUAD c;
CxImage tmp(*this);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
int32_t xmin,xmax,ymin,ymax;
if (pSelection){
xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
} else {
xmin = ymin = 0;
xmax = head.biWidth; ymax=head.biHeight;
}
ksumtot = 0;
for(int32_t j=-k2;j<kmax;j++){
for(int32_t k=-k2;k<kmax;k++){
ksumtot += kernel[(j+k2)+Ksize*(k+k2)];
}
}
if ((head.biBitCount==8) && IsGrayScale())
{
uint8_t* cPtr;
uint8_t* cPtr2;
int32_t iCount;
int32_t iY, iY2, iY1;
cPtr = info.pImage;
cPtr2 = (uint8_t *)tmp.info.pImage;
for(int32_t y=ymin; y<ymax; y++){
info.nProgress = (int32_t)(100*(y-ymin)/(ymax-ymin));
if (info.nEscape) break;
iY1 = y*info.dwEffWidth+xmin;
for(int32_t x=xmin; x<xmax; x++, iY1++){
#if CXIMAGE_SUPPORT_SELECTION
if (BlindSelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
{
b=ksumcur=0;
iCount = 0;
iY2 = ((y-k2)*info.dwEffWidth);
for(int32_t j=-k2;j<kmax;j++, iY2+=info.dwEffWidth)
{
if (0>(y+j) || (y+j)>=head.biHeight) continue;
iY = iY2+x;
for(int32_t k=-k2;k<kmax;k++, iCount++)
{
if (0>(x+k) || (x+k)>=head.biWidth) continue;
i=kernel[iCount];
b += cPtr[iY+k] * i;
ksumcur += i;
}
}
if (Kfactor==0 || ksumcur==0){
cPtr2[iY1] = (uint8_t)min(255, max(0,(int32_t)(b + Koffset)));
} else if (ksumtot == ksumcur) {
cPtr2[iY1] = (uint8_t)min(255, max(0,(int32_t)(b/Kfactor + Koffset)));
} else {
cPtr2[iY1] = (uint8_t)min(255, max(0,(int32_t)((b*ksumtot)/(ksumcur*Kfactor) + Koffset)));
}
}
}
}
}
else
{
for(int32_t y=ymin; y<ymax; y++){
info.nProgress = (int32_t)(100*(y-ymin)/(ymax-ymin));
if (info.nEscape) break;
for(int32_t x=xmin; x<xmax; x++){
#if CXIMAGE_SUPPORT_SELECTION
if (BlindSelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
{
r=b=g=ksumcur=0;
for(int32_t j=-k2;j<kmax;j++){
for(int32_t k=-k2;k<kmax;k++){
if (!IsInside(x+j,y+k)) continue;
c = BlindGetPixelColor(x+j,y+k);
i = kernel[(j+k2)+Ksize*(k+k2)];
r += c.rgbRed * i;
g += c.rgbGreen * i;
b += c.rgbBlue * i;
ksumcur += i;
}
}
if (Kfactor==0 || ksumcur==0){
c.rgbRed = (uint8_t)min(255, max(0,(int32_t)(r + Koffset)));
c.rgbGreen = (uint8_t)min(255, max(0,(int32_t)(g + Koffset)));
c.rgbBlue = (uint8_t)min(255, max(0,(int32_t)(b + Koffset)));
} else if (ksumtot == ksumcur) {
c.rgbRed = (uint8_t)min(255, max(0,(int32_t)(r/Kfactor + Koffset)));
c.rgbGreen = (uint8_t)min(255, max(0,(int32_t)(g/Kfactor + Koffset)));
c.rgbBlue = (uint8_t)min(255, max(0,(int32_t)(b/Kfactor + Koffset)));
} else {
c.rgbRed = (uint8_t)min(255, max(0,(int32_t)((r*ksumtot)/(ksumcur*Kfactor) + Koffset)));
c.rgbGreen = (uint8_t)min(255, max(0,(int32_t)((g*ksumtot)/(ksumcur*Kfactor) + Koffset)));
c.rgbBlue = (uint8_t)min(255, max(0,(int32_t)((b*ksumtot)/(ksumcur*Kfactor) + Koffset)));
}
tmp.BlindSetPixelColor(x,y,c);
}
}
}
}
Transfer(tmp);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Enhance the dark areas of the image
* \param Ksize: size of the kernel.
* \return true if everything is ok
*/
bool CxImage::Erode(int32_t Ksize)
{
if (!pDib) return false;
int32_t k2 = Ksize/2;
int32_t kmax= Ksize-k2;
uint8_t r,g,b;
RGBQUAD c;
CxImage tmp(*this);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
int32_t xmin,xmax,ymin,ymax;
if (pSelection){
xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
} else {
xmin = ymin = 0;
xmax = head.biWidth; ymax=head.biHeight;
}
for(int32_t y=ymin; y<ymax; y++){
info.nProgress = (int32_t)(100*(y-ymin)/(ymax-ymin));
if (info.nEscape) break;
for(int32_t x=xmin; x<xmax; x++){
#if CXIMAGE_SUPPORT_SELECTION
if (BlindSelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
{
r=b=g=255;
for(int32_t j=-k2;j<kmax;j++){
for(int32_t k=-k2;k<kmax;k++){
if (!IsInside(x+j,y+k)) continue;
c = BlindGetPixelColor(x+j,y+k);
if (c.rgbRed < r) r=c.rgbRed;
if (c.rgbGreen < g) g=c.rgbGreen;
if (c.rgbBlue < b) b=c.rgbBlue;
}
}
c.rgbRed = r;
c.rgbGreen = g;
c.rgbBlue = b;
tmp.BlindSetPixelColor(x,y,c);
}
}
}
Transfer(tmp);
return true;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Enhance the light areas of the image
* \param Ksize: size of the kernel.
* \return true if everything is ok
*/
bool CxImage::Dilate(int32_t Ksize)
{
if (!pDib) return false;
int32_t k2 = Ksize/2;
int32_t kmax= Ksize-k2;
uint8_t r,g,b;
RGBQUAD c;
CxImage tmp(*this);
if (!tmp.IsValid()){
strcpy(info.szLastError,tmp.GetLastError());
return false;
}
int32_t xmin,xmax,ymin,ymax;
if (pSelection){
xmin = info.rSelectionBox.left; xmax = info.rSelectionBox.right;
ymin = info.rSelectionBox.bottom; ymax = info.rSelectionBox.top;
} else {
xmin = ymin = 0;
xmax = head.biWidth; ymax=head.biHeight;
}
for(int32_t y=ymin; y<ymax; y++){
info.nProgress = (int32_t)(100*(y-ymin)/(ymax-ymin));
if (info.nEscape) break;
for(int32_t x=xmin; x<xmax; x++){
#if CXIMAGE_SUPPORT_SELECTION
if (BlindSelectionIsInside(x,y))
#endif //CXIMAGE_SUPPORT_SELECTION
{
r=b=g=0;
for(int32_t j=-k2;j<kmax;j++){
for(int32_t k=-k2;k<kmax;k++){
if (!IsInside(x+j,y+k)) continue;
c = BlindGetPixelColor(x+j,y+k);
if (c.rgbRed > r) r=c.rgbRed;
if (c.rgbGreen > g) g=c.rgbGreen;
if (c.rgbBlue > b) b=c.rgbBlue;
}
}
c.rgbRed = r;
c.rgbGreen = g;
c.rgbBlue = b;
tmp.BlindSetPixelColor(x,y,c);
}
}
}
Transfer(tmp);
return true;
}
////////////////////////////////////////////////////////////////////////////////