-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcvcolor.cpp
More file actions
executable file
·2569 lines (2215 loc) · 102 KB
/
Copy pathcvcolor.cpp
File metadata and controls
executable file
·2569 lines (2215 loc) · 102 KB
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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
/********************************* COPYRIGHT NOTICE *******************************\
The function for RGB to Lab conversion is based on the MATLAB script
RGB2Lab.m translated by Mark Ruzon from C code by Yossi Rubner, 23 September 1997.
See the page [http://vision.stanford.edu/~ruzon/software/rgblab.html]
\**********************************************************************************/
/********************************* COPYRIGHT NOTICE *******************************\
Original code for Bayer->BGR/RGB conversion is provided by Dirk Schaefer
from MD-Mathematische Dienste GmbH. Below is the copyright notice:
IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
By downloading, copying, installing or using the software you agree
to this license. If you do not agree to this license, do not download,
install, copy or use the software.
Contributors License Agreement:
Copyright (c) 2002,
MD-Mathematische Dienste GmbH
Im Defdahl 5-10
44141 Dortmund
Germany
www.md-it.de
Redistribution and use in source and binary forms,
with or without modification, are permitted provided
that the following conditions are met:
Redistributions of source code must retain
the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
The name of Contributor may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
\**********************************************************************************/
/****************************************************************************************\
* Various 3/4-channel to 3/4-channel RGB transformations *
\****************************************************************************************/
#define CV_IMPL_BGRX2BGR( flavor, arrtype ) \
static CvStatus CV_STDCALL \
icvBGRx2BGR_##flavor##_CnC3R( const arrtype* src, int srcstep, \
arrtype* dst, int dststep, \
CvSize size, int src_cn, int blue_idx ) \
{ \
int i; \
\
srcstep /= sizeof(src[0]); \
dststep /= sizeof(dst[0]); \
srcstep -= size.width*src_cn; \
size.width *= 3; \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
for( i = 0; i < size.width; i += 3, src += src_cn ) \
{ \
arrtype t0=src[blue_idx], t1=src[1], t2=src[blue_idx^2]; \
dst[i] = t0; \
dst[i+1] = t1; \
dst[i+2] = t2; \
} \
} \
\
return CV_OK; \
}
#define CV_IMPL_BGR2BGRX( flavor, arrtype ) \
static CvStatus CV_STDCALL \
icvBGR2BGRx_##flavor##_C3C4R( const arrtype* src, int srcstep, \
arrtype* dst, int dststep, \
CvSize size, int blue_idx ) \
{ \
int i; \
\
srcstep /= sizeof(src[0]); \
dststep /= sizeof(dst[0]); \
srcstep -= size.width*3; \
size.width *= 4; \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
for( i = 0; i < size.width; i += 4, src += 3 ) \
{ \
arrtype t0=src[blue_idx], t1=src[1], t2=src[blue_idx^2]; \
dst[i] = t0; \
dst[i+1] = t1; \
dst[i+2] = t2; \
dst[i+3] = 0; \
} \
} \
\
return CV_OK; \
}
#define CV_IMPL_BGRA2RGBA( flavor, arrtype ) \
static CvStatus CV_STDCALL \
icvBGRA2RGBA_##flavor##_C4R( const arrtype* src, int srcstep, \
arrtype* dst, int dststep, CvSize size ) \
{ \
int i; \
\
srcstep /= sizeof(src[0]); \
dststep /= sizeof(dst[0]); \
size.width *= 4; \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
for( i = 0; i < size.width; i += 4 ) \
{ \
arrtype t0 = src[2], t1 = src[1], t2 = src[0], t3 = src[3]; \
dst[i] = t0; \
dst[i+1] = t1; \
dst[i+2] = t2; \
dst[i+3] = t3; \
} \
} \
\
return CV_OK; \
}
/****************************************************************************************\
* Transforming 16-bit (565 or 555) RGB to/from 24/32-bit (888[8]) RGB *
\****************************************************************************************/
static CvStatus
icvBGR5x52BGRx_8u_C2CnR( const uchar* src, int srcstep,
uchar* dst, int dststep,
CvSize size, int dst_cn,
int blue_idx, int green_bits )
{
int i;
assert( green_bits == 5 || green_bits == 6 );
dststep -= size.width*dst_cn;
for( ; size.height--; src += srcstep, dst += dststep )
{
if( green_bits == 6 )
for( i = 0; i < size.width; i++, dst += dst_cn )
{
unsigned t = ((const ushort*)src)[i];
dst[blue_idx] = (uchar)(t << 3);
dst[1] = (uchar)((t >> 3) & ~3);
dst[blue_idx ^ 2] = (uchar)((t >> 8) & ~7);
if( dst_cn == 4 )
dst[3] = 0;
}
else
for( i = 0; i < size.width; i++, dst += dst_cn )
{
unsigned t = ((const ushort*)src)[i];
dst[blue_idx] = (uchar)(t << 3);
dst[1] = (uchar)((t >> 2) & ~7);
dst[blue_idx ^ 2] = (uchar)((t >> 7) & ~7);
if( dst_cn == 4 )
dst[3] = 0;
}
}
return CV_OK;
}
static CvStatus
icvBGRx2BGR5x5_8u_CnC2R( const uchar* src, int srcstep,
uchar* dst, int dststep,
CvSize size, int src_cn,
int blue_idx, int green_bits )
{
int i;
srcstep -= size.width*src_cn;
for( ; size.height--; src += srcstep, dst += dststep )
{
if( green_bits == 6 )
for( i = 0; i < size.width; i++, src += src_cn )
{
int t = (src[blue_idx] >> 3)|((src[1]&~3) << 3)|((src[blue_idx^2]&~7) << 8);
((ushort*)dst)[i] = (ushort)t;
}
else
for( i = 0; i < size.width; i++, src += src_cn )
{
int t = (src[blue_idx] >> 3)|((src[1]&~7) << 2)|((src[blue_idx^2]&~7) << 7);
((ushort*)dst)[i] = (ushort)t;
}
}
return CV_OK;
}
/////////////////////////// IPP Color Conversion Functions //////////////////////////////
#define CV_IMPL_BGRx2ABC_IPP( flavor, arrtype ) \
static CvStatus \
icvBGRx2ABC_IPP_##flavor##_CnC3R( const arrtype* src, int srcstep, \
arrtype* dst, int dststep, CvSize size, int src_cn, \
int blue_idx, CvColorCvtFunc0 ipp_func ) \
{ \
int block_size = MIN(1 << 14, size.width); \
arrtype* buffer; \
int i, di, k; \
int do_copy = src_cn > 3 || blue_idx != 2 || src == dst; \
CvStatus status = CV_OK; \
\
if( !do_copy ) \
return ipp_func( src, srcstep, dst, dststep, size ); \
\
srcstep /= sizeof(src[0]); \
dststep /= sizeof(dst[0]); \
\
buffer = (arrtype*)cvStackAlloc( block_size*3*sizeof(buffer[0]) ); \
srcstep -= size.width*src_cn; \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
for( i = 0; i < size.width; i += block_size ) \
{ \
arrtype* dst1 = dst + i*3; \
di = MIN(block_size, size.width - i); \
\
for( k = 0; k < di*3; k += 3, src += src_cn ) \
{ \
arrtype b = src[blue_idx]; \
arrtype g = src[1]; \
arrtype r = src[blue_idx^2]; \
buffer[k] = r; \
buffer[k+1] = g; \
buffer[k+2] = b; \
} \
\
status = ipp_func( buffer, CV_STUB_STEP, \
dst1, CV_STUB_STEP, cvSize(di,1) ); \
if( status < 0 ) \
return status; \
} \
} \
\
return CV_OK; \
}
#ifdef HAVE_IPP
static CvStatus
icvBGRx2ABC_IPP_8u_CnC3R( const uchar* src, int srcstep,
uchar* dst, int dststep, CvSize size, int src_cn,
int blue_idx, CvColorCvtFunc0 ipp_func )
{
int block_size = MIN(1 << 14, size.width);
uchar* buffer;
int i, di, k;
int do_copy = src_cn > 3 || blue_idx != 2 || src == dst;
CvStatus status = CV_OK;
if( !do_copy )
return ipp_func( src, srcstep, dst, dststep, size );
srcstep /= sizeof(src[0]);
dststep /= sizeof(dst[0]);
buffer = (uchar*)cvStackAlloc( block_size*3*sizeof(buffer[0]) );
srcstep -= size.width*src_cn;
for( ; size.height--; src += srcstep, dst += dststep )
{
for( i = 0; i < size.width; i += block_size )
{
uchar* dst1 = dst + i*3;
di = MIN(block_size, size.width - i);
for( k = 0; k < di*3; k += 3, src += src_cn )
{
uchar b = src[blue_idx];
uchar g = src[1];
uchar r = src[blue_idx^2];
buffer[k] = r;
buffer[k+1] = g;
buffer[k+2] = b;
}
status = ipp_func( buffer, CV_STUB_STEP,
dst1, CV_STUB_STEP, cvSize(di,1) );
if( status < 0 )
return status;
}
}
return CV_OK;
}
//CV_IMPL_BGRx2ABC_IPP( 8u, uchar )
//CV_IMPL_BGRx2ABC_IPP( 16u, ushort )
CV_IMPL_BGRx2ABC_IPP( 32f, float )
#define CV_IMPL_ABC2BGRx_IPP( flavor, arrtype ) \
static CvStatus \
icvABC2BGRx_IPP_##flavor##_C3CnR( const arrtype* src, int srcstep, \
arrtype* dst, int dststep, CvSize size, int dst_cn, \
int blue_idx, CvColorCvtFunc0 ipp_func ) \
{ \
int block_size = MIN(1 << 10, size.width); \
arrtype* buffer; \
int i, di, k; \
int do_copy = dst_cn > 3 || blue_idx != 2 || src == dst; \
CvStatus status = CV_OK; \
\
if( !do_copy ) \
return ipp_func( src, srcstep, dst, dststep, size ); \
\
srcstep /= sizeof(src[0]); \
dststep /= sizeof(dst[0]); \
\
buffer = (arrtype*)cvStackAlloc( block_size*3*sizeof(buffer[0]) ); \
dststep -= size.width*dst_cn; \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
for( i = 0; i < size.width; i += block_size ) \
{ \
const arrtype* src1 = src + i*3; \
di = MIN(block_size, size.width - i); \
\
status = ipp_func( src1, CV_STUB_STEP, \
buffer, CV_STUB_STEP, cvSize(di,1) ); \
if( status < 0 ) \
return status; \
\
for( k = 0; k < di*3; k += 3, dst += dst_cn ) \
{ \
arrtype r = buffer[k]; \
arrtype g = buffer[k+1]; \
arrtype b = buffer[k+2]; \
dst[blue_idx] = b; \
dst[1] = g; \
dst[blue_idx^2] = r; \
if( dst_cn == 4 ) \
dst[3] = 0; \
} \
} \
} \
\
return CV_OK; \
}
CV_IMPL_ABC2BGRx_IPP( 8u, uchar )
//CV_IMPL_ABC2BGRx_IPP( 16u, ushort )
CV_IMPL_ABC2BGRx_IPP( 32f, float )
#endif
/////////////////////////////////////////////////////////////////////////////////////////
/****************************************************************************************\
* Color to/from Grayscale *
\****************************************************************************************/
#define fix(x,n) (int)((x)*(1 << (n)) + 0.5)
#define descale CV_DESCALE
#define cscGr_32f 0.299f
#define cscGg_32f 0.587f
#define cscGb_32f 0.114f
/* BGR/RGB -> Gray */
#define csc_shift 14
#define cscGr fix(cscGr_32f,csc_shift)
#define cscGg fix(cscGg_32f,csc_shift)
#define cscGb /*fix(cscGb_32f,csc_shift)*/ ((1 << csc_shift) - cscGr - cscGg)
#define CV_IMPL_GRAY2BGRX( flavor, arrtype ) \
static CvStatus \
icvGray2BGRx_##flavor##_C1CnR( const arrtype* src, int srcstep, \
arrtype* dst, int dststep, CvSize size, \
int dst_cn ) \
{ \
int i; \
srcstep /= sizeof(src[0]); \
dststep /= sizeof(src[0]); \
dststep -= size.width*dst_cn; \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
if( dst_cn == 3 ) \
for( i = 0; i < size.width; i++, dst += 3 ) \
dst[0] = dst[1] = dst[2] = src[i]; \
else \
for( i = 0; i < size.width; i++, dst += 4 ) \
{ \
dst[0] = dst[1] = dst[2] = src[i]; \
dst[3] = 0; \
} \
} \
\
return CV_OK; \
}
CV_IMPL_GRAY2BGRX( 8u, uchar )
CV_IMPL_GRAY2BGRX( 16u, ushort )
CV_IMPL_GRAY2BGRX( 32f, float )
static CvStatus
icvBGR5x52Gray_8u_C2C1R( const uchar* src, int srcstep,
uchar* dst, int dststep,
CvSize size, int green_bits )
{
int i;
assert( green_bits == 5 || green_bits == 6 );
for( ; size.height--; src += srcstep, dst += dststep )
{
if( green_bits == 6 )
for( i = 0; i < size.width; i++ )
{
int t = ((ushort*)src)[i];
t = ((t << 3) & 0xf8)*cscGb + ((t >> 3) & 0xfc)*cscGg +
((t >> 8) & 0xf8)*cscGr;
dst[i] = (uchar)CV_DESCALE(t,csc_shift);
}
else
for( i = 0; i < size.width; i++ )
{
int t = ((ushort*)src)[i];
t = ((t << 3) & 0xf8)*cscGb + ((t >> 2) & 0xf8)*cscGg +
((t >> 7) & 0xf8)*cscGr;
dst[i] = (uchar)CV_DESCALE(t,csc_shift);
}
}
return CV_OK;
}
static CvStatus
icvGray2BGR5x5_8u_C1C2R( const uchar* src, int srcstep,
uchar* dst, int dststep,
CvSize size, int green_bits )
{
int i;
assert( green_bits == 5 || green_bits == 6 );
for( ; size.height--; src += srcstep, dst += dststep )
{
if( green_bits == 6 )
for( i = 0; i < size.width; i++ )
{
int t = src[i];
((ushort*)dst)[i] = (ushort)((t >> 3)|((t & ~3) << 3)|((t & ~7) << 8));
}
else
for( i = 0; i < size.width; i++ )
{
int t = src[i] >> 3;
((ushort*)dst)[i] = (ushort)(t|(t << 5)|(t << 10));
}
}
return CV_OK;
}
static CvStatus
icvBGRx2Gray_8u_CnC1R( const uchar* src, int srcstep,
uchar* dst, int dststep, CvSize size,
int src_cn, int blue_idx )
{
int i;
srcstep -= size.width*src_cn;
if( size.width*size.height >= 1024 )
{
int* tab = (int*)cvStackAlloc( 256*3*sizeof(tab[0]) );
int r = 0, g = 0, b = (1 << (csc_shift-1));
for( i = 0; i < 256; i++ )
{
tab[i] = b;
tab[i+256] = g;
tab[i+512] = r;
g += cscGg;
if( !blue_idx )
b += cscGb, r += cscGr;
else
b += cscGr, r += cscGb;
}
for( ; size.height--; src += srcstep, dst += dststep )
{
for( i = 0; i < size.width; i++, src += src_cn )
{
int t0 = tab[src[0]] + tab[src[1] + 256] + tab[src[2] + 512];
dst[i] = (uchar)(t0 >> csc_shift);
}
}
}
else
{
for( ; size.height--; src += srcstep, dst += dststep )
{
for( i = 0; i < size.width; i++, src += src_cn )
{
int t0 = src[blue_idx]*cscGb + src[1]*cscGg + src[blue_idx^2]*cscGr;
dst[i] = (uchar)CV_DESCALE(t0, csc_shift);
}
}
}
return CV_OK;
}
static CvStatus
icvBGRx2Gray_16u_CnC1R( const ushort* src, int srcstep,
ushort* dst, int dststep, CvSize size,
int src_cn, int blue_idx )
{
int i;
int cb = cscGb, cr = cscGr;
srcstep /= sizeof(src[0]);
dststep /= sizeof(dst[0]);
srcstep -= size.width*src_cn;
if( blue_idx )
cb = cscGr, cr = cscGb;
for( ; size.height--; src += srcstep, dst += dststep )
for( i = 0; i < size.width; i++, src += src_cn )
dst[i] = (ushort)CV_DESCALE((unsigned)(src[0]*cb +
src[1]*cscGg + src[2]*cr), csc_shift);
return CV_OK;
}
static CvStatus
icvBGRx2Gray_32f_CnC1R( const float* src, int srcstep,
float* dst, int dststep, CvSize size,
int src_cn, int blue_idx )
{
int i;
float cb = cscGb_32f, cr = cscGr_32f;
if( blue_idx )
cb = cscGr_32f, cr = cscGb_32f;
srcstep /= sizeof(src[0]);
dststep /= sizeof(dst[0]);
srcstep -= size.width*src_cn;
for( ; size.height--; src += srcstep, dst += dststep )
for( i = 0; i < size.width; i++, src += src_cn )
dst[i] = src[0]*cb + src[1]*cscGg_32f + src[2]*cr;
return CV_OK;
}
/****************************************************************************************\
* RGB <-> YCrCb *
\****************************************************************************************/
/* BGR/RGB -> YCrCb */
#define yuvYr_32f cscGr_32f
#define yuvYg_32f cscGg_32f
#define yuvYb_32f cscGb_32f
#define yuvCr_32f 0.713f
#define yuvCb_32f 0.564f
#define yuv_shift 14
#define yuvYr fix(yuvYr_32f,yuv_shift)
#define yuvYg fix(yuvYg_32f,yuv_shift)
#define yuvYb fix(yuvYb_32f,yuv_shift)
#define yuvCr fix(yuvCr_32f,yuv_shift)
#define yuvCb fix(yuvCb_32f,yuv_shift)
#define yuv_descale(x) CV_DESCALE((x), yuv_shift)
#define yuv_prescale(x) ((x) << yuv_shift)
#define yuvRCr_32f 1.403f
#define yuvGCr_32f (-0.714f)
#define yuvGCb_32f (-0.344f)
#define yuvBCb_32f 1.773f
#define yuvRCr fix(yuvRCr_32f,yuv_shift)
#define yuvGCr (-fix(-yuvGCr_32f,yuv_shift))
#define yuvGCb (-fix(-yuvGCb_32f,yuv_shift))
#define yuvBCb fix(yuvBCb_32f,yuv_shift)
#define CV_IMPL_BGRx2YCrCb( flavor, arrtype, worktype, scale_macro, cast_macro, \
YUV_YB, YUV_YG, YUV_YR, YUV_CR, YUV_CB, YUV_Cx_BIAS ) \
static CvStatus \
icvBGRx2YCrCb_##flavor##_CnC3R( const arrtype* src, int srcstep, \
arrtype* dst, int dststep, CvSize size, int src_cn, int blue_idx ) \
{ \
int i; \
srcstep /= sizeof(src[0]); \
dststep /= sizeof(src[0]); \
srcstep -= size.width*src_cn; \
size.width *= 3; \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
for( i = 0; i < size.width; i += 3, src += src_cn ) \
{ \
worktype b = src[blue_idx], r = src[2^blue_idx], y; \
y = scale_macro(b*YUV_YB + src[1]*YUV_YG + r*YUV_YR); \
r = scale_macro((r - y)*YUV_CR) + YUV_Cx_BIAS; \
b = scale_macro((b - y)*YUV_CB) + YUV_Cx_BIAS; \
dst[i] = cast_macro(y); \
dst[i+1] = cast_macro(r); \
dst[i+2] = cast_macro(b); \
} \
} \
\
return CV_OK; \
}
CV_IMPL_BGRx2YCrCb( 8u, uchar, int, yuv_descale, CV_CAST_8U,
yuvYb, yuvYg, yuvYr, yuvCr, yuvCb, 128 )
CV_IMPL_BGRx2YCrCb( 16u, ushort, int, yuv_descale, CV_CAST_16U,
yuvYb, yuvYg, yuvYr, yuvCr, yuvCb, 32768 )
CV_IMPL_BGRx2YCrCb( 32f, float, float, CV_NOP, CV_NOP,
yuvYb_32f, yuvYg_32f, yuvYr_32f, yuvCr_32f, yuvCb_32f, 0.5f )
#define CV_IMPL_YCrCb2BGRx( flavor, arrtype, worktype, prescale_macro, \
scale_macro, cast_macro, YUV_BCb, YUV_GCr, YUV_GCb, YUV_RCr, YUV_Cx_BIAS)\
static CvStatus \
icvYCrCb2BGRx_##flavor##_C3CnR( const arrtype* src, int srcstep, \
arrtype* dst, int dststep, CvSize size, \
int dst_cn, int blue_idx ) \
{ \
int i; \
srcstep /= sizeof(src[0]); \
dststep /= sizeof(src[0]); \
dststep -= size.width*dst_cn; \
size.width *= 3; \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
for( i = 0; i < size.width; i += 3, dst += dst_cn ) \
{ \
worktype Y = prescale_macro(src[i]), \
Cr = src[i+1] - YUV_Cx_BIAS, \
Cb = src[i+2] - YUV_Cx_BIAS; \
worktype b, g, r; \
b = scale_macro( Y + YUV_BCb*Cb ); \
g = scale_macro( Y + YUV_GCr*Cr + YUV_GCb*Cb ); \
r = scale_macro( Y + YUV_RCr*Cr ); \
\
dst[blue_idx] = cast_macro(b); \
dst[1] = cast_macro(g); \
dst[blue_idx^2] = cast_macro(r); \
if( dst_cn == 4 ) \
dst[3] = 0; \
} \
} \
\
return CV_OK; \
}
CV_IMPL_YCrCb2BGRx( 8u, uchar, int, yuv_prescale, yuv_descale, CV_CAST_8U,
yuvBCb, yuvGCr, yuvGCb, yuvRCr, 128 )
CV_IMPL_YCrCb2BGRx( 16u, ushort, int, yuv_prescale, yuv_descale, CV_CAST_16U,
yuvBCb, yuvGCr, yuvGCb, yuvRCr, 32768 )
CV_IMPL_YCrCb2BGRx( 32f, float, float, CV_NOP, CV_NOP, CV_NOP,
yuvBCb_32f, yuvGCr_32f, yuvGCb_32f, yuvRCr_32f, 0.5f )
/****************************************************************************************\
* RGB <-> XYZ *
\****************************************************************************************/
#define xyzXr_32f 0.412453f
#define xyzXg_32f 0.357580f
#define xyzXb_32f 0.180423f
#define xyzYr_32f 0.212671f
#define xyzYg_32f 0.715160f
#define xyzYb_32f 0.072169f
#define xyzZr_32f 0.019334f
#define xyzZg_32f 0.119193f
#define xyzZb_32f 0.950227f
#define xyzRx_32f 3.240479f
#define xyzRy_32f (-1.53715f)
#define xyzRz_32f (-0.498535f)
#define xyzGx_32f (-0.969256f)
#define xyzGy_32f 1.875991f
#define xyzGz_32f 0.041556f
#define xyzBx_32f 0.055648f
#define xyzBy_32f (-0.204043f)
#define xyzBz_32f 1.057311f
#define xyz_shift 10
#define xyzXr_32s fix(xyzXr_32f, xyz_shift )
#define xyzXg_32s fix(xyzXg_32f, xyz_shift )
#define xyzXb_32s fix(xyzXb_32f, xyz_shift )
#define xyzYr_32s fix(xyzYr_32f, xyz_shift )
#define xyzYg_32s fix(xyzYg_32f, xyz_shift )
#define xyzYb_32s fix(xyzYb_32f, xyz_shift )
#define xyzZr_32s fix(xyzZr_32f, xyz_shift )
#define xyzZg_32s fix(xyzZg_32f, xyz_shift )
#define xyzZb_32s fix(xyzZb_32f, xyz_shift )
#define xyzRx_32s fix(3.240479f, xyz_shift )
#define xyzRy_32s -fix(1.53715f, xyz_shift )
#define xyzRz_32s -fix(0.498535f, xyz_shift )
#define xyzGx_32s -fix(0.969256f, xyz_shift )
#define xyzGy_32s fix(1.875991f, xyz_shift )
#define xyzGz_32s fix(0.041556f, xyz_shift )
#define xyzBx_32s fix(0.055648f, xyz_shift )
#define xyzBy_32s -fix(0.204043f, xyz_shift )
#define xyzBz_32s fix(1.057311f, xyz_shift )
#define xyz_descale(x) CV_DESCALE((x),xyz_shift)
#define CV_IMPL_BGRx2XYZ( flavor, arrtype, worktype, \
scale_macro, cast_macro, suffix ) \
static CvStatus \
icvBGRx2XYZ_##flavor##_CnC3R( const arrtype* src, int srcstep, \
arrtype* dst, int dststep, CvSize size, \
int src_cn, int blue_idx ) \
{ \
int i; \
worktype t, matrix[] = \
{ \
xyzXb##suffix, xyzXg##suffix, xyzXr##suffix, \
xyzYb##suffix, xyzYg##suffix, xyzYr##suffix, \
xyzZb##suffix, xyzZg##suffix, xyzZr##suffix \
}; \
\
srcstep /= sizeof(src[0]); \
dststep /= sizeof(dst[0]); \
srcstep -= size.width*src_cn; \
size.width *= 3; \
\
if( blue_idx ) \
{ \
CV_SWAP( matrix[0], matrix[2], t ); \
CV_SWAP( matrix[3], matrix[5], t ); \
CV_SWAP( matrix[6], matrix[8], t ); \
} \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
for( i = 0; i < size.width; i += 3, src += src_cn ) \
{ \
worktype x = scale_macro(src[0]*matrix[0] + \
src[1]*matrix[1] + src[2]*matrix[2]); \
worktype y = scale_macro(src[0]*matrix[3] + \
src[1]*matrix[4] + src[2]*matrix[5]); \
worktype z = scale_macro(src[0]*matrix[6] + \
src[1]*matrix[7] + src[2]*matrix[8]); \
\
dst[i] = (arrtype)(x); \
dst[i+1] = (arrtype)(y); \
dst[i+2] = cast_macro(z); /*sum of weights for z > 1*/ \
} \
} \
\
return CV_OK; \
}
CV_IMPL_BGRx2XYZ( 8u, uchar, int, xyz_descale, CV_CAST_8U, _32s )
CV_IMPL_BGRx2XYZ( 16u, ushort, int, xyz_descale, CV_CAST_16U, _32s )
CV_IMPL_BGRx2XYZ( 32f, float, float, CV_NOP, CV_NOP, _32f )
#define CV_IMPL_XYZ2BGRx( flavor, arrtype, worktype, scale_macro, \
cast_macro, suffix ) \
static CvStatus \
icvXYZ2BGRx_##flavor##_C3CnR( const arrtype* src, int srcstep, \
arrtype* dst, int dststep, CvSize size, \
int dst_cn, int blue_idx ) \
{ \
int i; \
worktype t, matrix[] = \
{ \
xyzBx##suffix, xyzBy##suffix, xyzBz##suffix, \
xyzGx##suffix, xyzGy##suffix, xyzGz##suffix, \
xyzRx##suffix, xyzRy##suffix, xyzRz##suffix \
}; \
\
srcstep /= sizeof(src[0]); \
dststep /= sizeof(dst[0]); \
dststep -= size.width*dst_cn; \
size.width *= 3; \
\
if( blue_idx ) \
{ \
CV_SWAP( matrix[0], matrix[6], t ); \
CV_SWAP( matrix[1], matrix[7], t ); \
CV_SWAP( matrix[2], matrix[8], t ); \
} \
\
for( ; size.height--; src += srcstep, dst += dststep ) \
{ \
for( i = 0; i < size.width; i += 3, dst += dst_cn ) \
{ \
worktype b = scale_macro(src[i]*matrix[0] + \
src[i+1]*matrix[1] + src[i+2]*matrix[2]); \
worktype g = scale_macro(src[i]*matrix[3] + \
src[i+1]*matrix[4] + src[i+2]*matrix[5]); \
worktype r = scale_macro(src[i]*matrix[6] + \
src[i+1]*matrix[7] + src[i+2]*matrix[8]); \
\
dst[0] = cast_macro(b); \
dst[1] = cast_macro(g); \
dst[2] = cast_macro(r); \
\
if( dst_cn == 4 ) \
dst[3] = 0; \
} \
} \
\
return CV_OK; \
}
CV_IMPL_XYZ2BGRx( 8u, uchar, int, xyz_descale, CV_CAST_8U, _32s )
CV_IMPL_XYZ2BGRx( 16u, ushort, int, xyz_descale, CV_CAST_16U, _32s )
CV_IMPL_XYZ2BGRx( 32f, float, float, CV_NOP, CV_NOP, _32f )
/****************************************************************************************\
* Non-linear Color Space Transformations *
\****************************************************************************************/
#ifndef HAVE_IPP
// driver color space conversion function for 8u arrays that uses 32f function
// with appropriate pre- and post-scaling.
static CvStatus
icvABC2BGRx_8u_C3CnR( const uchar* src, int srcstep, uchar* dst, int dststep,
CvSize size, int dst_cn, int blue_idx, CvColorCvtFunc2 cvtfunc_32f,
const float* pre_coeffs, int postscale )
{
int block_size = MIN(1 << 8, size.width);
float* buffer = (float*)cvStackAlloc( block_size*3*sizeof(buffer[0]) );
int i, di, k;
CvStatus status = CV_OK;
dststep -= size.width*dst_cn;
for( ; size.height--; src += srcstep, dst += dststep )
{
for( i = 0; i < size.width; i += block_size )
{
const uchar* src1 = src + i*3;
di = MIN(block_size, size.width - i);
for( k = 0; k < di*3; k += 3 )
{
float a = CV_8TO32F(src1[k])*pre_coeffs[0] + pre_coeffs[1];
float b = CV_8TO32F(src1[k+1])*pre_coeffs[2] + pre_coeffs[3];
float c = CV_8TO32F(src1[k+2])*pre_coeffs[4] + pre_coeffs[5];
buffer[k] = a;
buffer[k+1] = b;
buffer[k+2] = c;
}
status = cvtfunc_32f( buffer, 0, buffer, 0, cvSize(di,1), 3, blue_idx );
if( status < 0 )
return status;
if( postscale )
{
for( k = 0; k < di*3; k += 3, dst += dst_cn )
{
int b = cvRound(buffer[k]*255.);
int g = cvRound(buffer[k+1]*255.);
int r = cvRound(buffer[k+2]*255.);
dst[0] = CV_CAST_8U(b);
dst[1] = CV_CAST_8U(g);
dst[2] = CV_CAST_8U(r);
if( dst_cn == 4 )
dst[3] = 0;
}
}
else
{
for( k = 0; k < di*3; k += 3, dst += dst_cn )
{
int b = cvRound(buffer[k]);
int g = cvRound(buffer[k+1]);
int r = cvRound(buffer[k+2]);
dst[0] = CV_CAST_8U(b);
dst[1] = CV_CAST_8U(g);
dst[2] = CV_CAST_8U(r);
if( dst_cn == 4 )
dst[3] = 0;
}
}
}
}
return CV_OK;
}
// driver color space conversion function for 8u arrays that uses 32f function
// with appropriate pre- and post-scaling.
static CvStatus
icvBGRx2ABC_8u_CnC3R( const uchar* src, int srcstep, uchar* dst, int dststep,
CvSize size, int src_cn, int blue_idx, CvColorCvtFunc2 cvtfunc_32f,
int prescale, const float* post_coeffs )
{
int block_size = MIN(1 << 8, size.width);
float* buffer = (float*)cvStackAlloc( block_size*3*sizeof(buffer[0]) );
int i, di, k;
CvStatus status = CV_OK;
srcstep -= size.width*src_cn;
for( ; size.height--; src += srcstep, dst += dststep )
{
for( i = 0; i < size.width; i += block_size )
{
uchar* dst1 = dst + i*3;
di = MIN(block_size, size.width - i);
if( prescale )
{
for( k = 0; k < di*3; k += 3, src += src_cn )
{
float b = CV_8TO32F(src[0])*0.0039215686274509803f;
float g = CV_8TO32F(src[1])*0.0039215686274509803f;
float r = CV_8TO32F(src[2])*0.0039215686274509803f;