/home/liu/actions-runner/_work/ccv/ccv/lib/ccv_image_processing.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "ccv.h" |
2 | | #include "ccv_internal.h" |
3 | | |
4 | | static void _ccv_rgb_to_yuv(ccv_dense_matrix_t* a, ccv_dense_matrix_t* b) |
5 | 0 | { |
6 | 0 | unsigned char* a_ptr = a->data.u8; |
7 | 0 | unsigned char* b_ptr = b->data.u8; |
8 | 0 | int i, j; |
9 | 0 | #define for_block(_for_get, _for_set_b, _for_get_b) \ |
10 | 0 | for (i = 0; i < a->rows; i++) \ |
11 | 0 | { \ |
12 | 0 | for (j = 0; j < a->cols; j++) \ |
13 | 0 | { \ |
14 | 0 | _for_set_b(b_ptr, j * 3, (_for_get(a_ptr, j * 3) * 1225 + _for_get(a_ptr, j * 3 + 1) * 2404 + _for_get(a_ptr, j * 3 + 2) * 467) / 4096); \ |
15 | 0 | _for_set_b(b_ptr, j * 3 + 1, (_for_get(a_ptr, j * 3 + 2) - _for_get_b(b_ptr, j * 3)) * 2015 / 4096 + 128); \ |
16 | 0 | _for_set_b(b_ptr, j * 3 + 2, (_for_get(a_ptr, j * 3) - _for_get_b(b_ptr, j * 3)) * 3592 / 4096 + 128); \ |
17 | 0 | } \ |
18 | 0 | a_ptr += a->step; \ |
19 | 0 | b_ptr += b->step; \ |
20 | 0 | } |
21 | 0 | ccv_matrix_getter(a->type, ccv_matrix_setter_getter, b->type, for_block); |
22 | 0 | #undef for_block |
23 | 0 | } |
24 | | |
25 | | void ccv_color_transform(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type, int flag) |
26 | 0 | { |
27 | 0 | ccv_declare_derived_signature(sig, a->sig != 0, ccv_sign_with_format(64, "ccv_color_transform(%d)", flag), a->sig, CCV_EOF_SIGN); |
28 | 0 | assert(flag == CCV_RGB_TO_YUV); |
29 | 0 | switch (flag) |
30 | 0 | { |
31 | 0 | case CCV_RGB_TO_YUV: |
32 | 0 | assert(CCV_GET_CHANNEL(a->type) == CCV_C3); |
33 | 0 | type = (type == 0) ? CCV_GET_DATA_TYPE(a->type) | CCV_C3 : CCV_GET_DATA_TYPE(type) | CCV_C3; |
34 | 0 | break; |
35 | 0 | } |
36 | 0 | ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, a->rows, a->cols, CCV_ALL_DATA_TYPE | CCV_C3, type, sig); |
37 | 0 | ccv_object_return_if_cached(, db); |
38 | 0 | switch (flag) |
39 | 0 | { |
40 | 0 | case CCV_RGB_TO_YUV: |
41 | 0 | _ccv_rgb_to_yuv(a, db); |
42 | 0 | break; |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | | void ccv_saturation(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type, double ds) |
47 | 3 | { |
48 | 3 | assert(CCV_GET_CHANNEL(a->type) == CCV_C3); // only works in RGB space |
49 | 3 | ccv_declare_derived_signature(sig, a->sig != 0, ccv_sign_with_format(64, "ccv_saturation(%la)", ds), a->sig, CCV_EOF_SIGN); |
50 | 3 | type = (type == 0) ? CCV_GET_DATA_TYPE(a->type) | CCV_GET_CHANNEL(a->type) : CCV_GET_DATA_TYPE0 (type) | 0 CCV_GET_CHANNEL0 (a->type); |
51 | 3 | ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, a->rows, a->cols, CCV_ALL_DATA_TYPE | CCV_GET_CHANNEL(a->type), type, sig); |
52 | 3 | ccv_object_return_if_cached(, db); |
53 | 3 | int i, j; |
54 | 3 | unsigned char* aptr = a->data.u8; |
55 | 3 | unsigned char* bptr = db->data.u8; |
56 | 3 | #define for_block(_for_get, _for_set) \ |
57 | 1.39k | for (i = 0; 3 i < a->rows; i++1.38k ) \ |
58 | 1.38k | { \ |
59 | 1.01M | for (j = 0; j < a->cols; j++1.01M ) \ |
60 | 1.01M | { \ |
61 | 1.01M | double gs = _for_get(aptr, j * 3) * 0.299 + _for_get(aptr, j * 3 + 1) * 0.587 + _for_get(aptr, j * 3 + 2) * 0.114; \ |
62 | 1.01M | _for_set(bptr, j * 3, (_for_get(aptr, j * 3) - gs) * ds + gs); \ |
63 | 1.01M | _for_set(bptr, j * 3 + 1, (_for_get(aptr, j * 3 + 1) - gs) * ds + gs); \ |
64 | 1.01M | _for_set(bptr, j * 3 + 2, (_for_get(aptr, j * 3 + 2) - gs) * ds + gs); \ |
65 | 1.01M | } \ |
66 | 1.38k | aptr += a->step; \ |
67 | 1.38k | bptr += db->step; \ |
68 | 1.38k | } |
69 | 3 | ccv_matrix_getter(a->type, ccv_matrix_setter, db->type, for_block); |
70 | 3 | #undef for_block |
71 | 3 | } |
72 | | |
73 | | void ccv_contrast(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type, double ds) |
74 | 3 | { |
75 | 3 | ccv_declare_derived_signature(sig, a->sig != 0, ccv_sign_with_format(64, "ccv_contrast(%la)", ds), a->sig, CCV_EOF_SIGN); |
76 | 3 | type = (type == 0) ? CCV_GET_DATA_TYPE(a->type) | CCV_GET_CHANNEL(a->type) : CCV_GET_DATA_TYPE0 (type) | 0 CCV_GET_CHANNEL0 (a->type); |
77 | 3 | ccv_dense_matrix_t* db = *b = ccv_dense_matrix_renew(*b, a->rows, a->cols, CCV_ALL_DATA_TYPE | CCV_GET_CHANNEL(a->type), type, sig); |
78 | 3 | ccv_object_return_if_cached(, db); |
79 | 3 | int i, j, k, ch = CCV_GET_CHANNEL(a->type); |
80 | 3 | double* ms = (double*)alloca(sizeof(double) * ch); |
81 | 3 | memset(ms, 0, sizeof(double) * ch); |
82 | 3 | unsigned char* aptr = a->data.u8; |
83 | 3 | #define for_block(_, _for_get) \ |
84 | 1.39k | for (i = 0; 3 i < a->rows; i++1.38k ) \ |
85 | 1.38k | { \ |
86 | 1.01M | for (j = 0; j < a->cols; j++1.01M ) \ |
87 | 4.05M | for (k = 0; 1.01M k < ch; k++3.04M ) \ |
88 | 3.04M | ms[k] += _for_get1.38k (aptr, j * ch + k); \ |
89 | 1.38k | aptr += a->step; \ |
90 | 1.38k | } |
91 | 3 | ccv_matrix_getter(a->type, for_block); |
92 | 3 | #undef for_block |
93 | 12 | for (i = 0; i < ch; i++9 ) |
94 | 9 | ms[i] = ms[i] / (a->rows * a->cols); |
95 | 3 | aptr = a->data.u8; |
96 | 3 | unsigned char* bptr = db->data.u8; |
97 | 3 | if (CCV_GET_DATA_TYPE(a->type) == CCV_8U && CCV_GET_DATA_TYPE2 (db->type) == CCV_8U2 ) // specialize for 8U type |
98 | 2 | { |
99 | 2 | unsigned char* us = (unsigned char*)alloca(sizeof(unsigned char) * ch * 256); |
100 | 514 | for (i = 0; i < 256; i++512 ) |
101 | 2.04k | for (j = 0; 512 j < ch; j++1.53k ) |
102 | 1.53k | us[i * ch + j] = ccv_clamp((i - ms[j]) * ds + ms[j], 0, 255); |
103 | 1.20k | for (i = 0; i < a->rows; i++1.20k ) |
104 | 1.20k | { |
105 | 961k | for (j = 0; j < a->cols; j++960k ) |
106 | 3.84M | for (k = 0; 960k k < ch; k++2.88M ) |
107 | 2.88M | bptr[j * ch + k] = us[(aptr[j * ch + k]) * ch + k]; |
108 | 1.20k | aptr += a->step; |
109 | 1.20k | bptr += db->step; |
110 | 1.20k | } |
111 | 2 | } else { |
112 | 1 | #define for_block(_for_get, _for_set) \ |
113 | 189 | for (i = 0; 1 i < a->rows; i++188 ) \ |
114 | 188 | { \ |
115 | 53.5k | for (j = 0; j < a->cols; j++53.3k ) \ |
116 | 213k | for (k = 0; 53.3k k < ch; k++160k ) \ |
117 | 53.3k | _for_set188 (bptr, j * ch + k, (_for_get(aptr, j * ch + k) - ms[k]) * ds + ms[k]); \ |
118 | 188 | aptr += a->step; \ |
119 | 188 | bptr += db->step; \ |
120 | 188 | } |
121 | 1 | ccv_matrix_getter(a->type, ccv_matrix_setter, db->type, for_block); |
122 | 1 | #undef for_block |
123 | 1 | } |
124 | 3 | } |