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
|
/*
Copyright (C) 2009 Red Hat, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef RED_BITMAP_UTILS_RGB16
#define PIXEL rgb16_pixel_t
#define FNAME(name) name##_rgb16
#define GET_r(pix) (((pix) >> 10) & 0x1f)
#define GET_g(pix) (((pix) >> 5) & 0x1f)
#define GET_b(pix) ((pix) & 0x1f)
#endif
#if defined(RED_BITMAP_UTILS_RGB24) || defined(RED_BITMAP_UTILS_RGB32)
#define GET_r(pix) ((pix).r)
#define GET_g(pix) ((pix).g)
#define GET_b(pix) ((pix).b)
#endif
#ifdef RED_BITMAP_UTILS_RGB24
#define PIXEL rgb24_pixel_t
#define FNAME(name) name##_rgb24
#endif
#ifdef RED_BITMAP_UTILS_RGB32
#define PIXEL rgb32_pixel_t
#define FNAME(name) name##_rgb32
#endif
#define SAME_PIXEL_WEIGHT 0.5
#define NOT_CONTRAST_PIXELS_WEIGHT -0.25
#define CONTRAST_PIXELS_WEIGHT 1.0
#ifndef RED_BITMAP_UTILS_RGB16
#define CONTRAST_TH 60
#else
#define CONTRAST_TH 8
#endif
#define SAMPLE_JUMP 15
#define SAME_PIXEL(p1, p2) (GET_r(p1) == GET_r(p2) && GET_g(p1) == GET_g(p2) && \
GET_b(p1) == GET_b(p2))
static const double FNAME(PIX_PAIR_SCORE)[] = {
SAME_PIXEL_WEIGHT,
CONTRAST_PIXELS_WEIGHT,
NOT_CONTRAST_PIXELS_WEIGHT,
};
// return 0 - equal, 1 - for contrast, 2 for no contrast (PIX_PAIR_SCORE is defined accordingly)
static inline int FNAME(pixelcmp)(PIXEL p1, PIXEL p2)
{
int diff = ABS(GET_r(p1) - GET_r(p2));
int equal;
if (diff >= CONTRAST_TH) {
return 1;
}
equal = !diff;
diff = ABS(GET_g(p1) - GET_g(p2));
if (diff >= CONTRAST_TH) {
return 1;
}
equal = equal && !diff;
diff = ABS(GET_b(p1) - GET_b(p2));
if (diff >= CONTRAST_TH) {
return 1;
}
equal = equal && !diff;
if (equal) {
return 0;
} else {
return 2;
}
}
static inline double FNAME(pixels_square_score)(PIXEL *line1, PIXEL *line2)
{
double ret = 0.0;
int all_ident = TRUE;
int cmp_res;
cmp_res = FNAME(pixelcmp)(*line1, line1[1]);
all_ident = all_ident && (!cmp_res);
ret += FNAME(PIX_PAIR_SCORE)[cmp_res];
cmp_res = FNAME(pixelcmp)(*line1, *line2);
all_ident = all_ident && (!cmp_res);
ret += FNAME(PIX_PAIR_SCORE)[cmp_res];
cmp_res = FNAME(pixelcmp)(*line1, line2[1]);
all_ident = all_ident && (!cmp_res);
ret += FNAME(PIX_PAIR_SCORE)[cmp_res];
// ignore squares where all pixels are identical
if (all_ident) {
ret -= (FNAME(PIX_PAIR_SCORE)[0]) * 3;
}
return ret;
}
static void FNAME(compute_lines_gradual_score)(PIXEL *lines, int width, int num_lines,
double *o_samples_sum_score, int *o_num_samples)
{
int jump = (SAMPLE_JUMP % width) ? SAMPLE_JUMP : SAMPLE_JUMP - 1;
PIXEL *cur_pix = lines + width / 2;
PIXEL *bottom_pix;
PIXEL *last_line = lines + (num_lines - 1) * width;
if ((width <= 1) || (num_lines <= 1)) {
*o_num_samples = 1;
*o_samples_sum_score = 1.0;
return;
}
*o_samples_sum_score = 0;
*o_num_samples = 0;
while (cur_pix < last_line) {
if ((cur_pix + 1 - lines) % width == 0) { // last pixel in the row
cur_pix--; // jump is bigger than 1 so we will not enter endless loop
}
bottom_pix = cur_pix + width;
(*o_samples_sum_score) += FNAME(pixels_square_score)(cur_pix, bottom_pix);
(*o_num_samples)++;
cur_pix += jump;
}
(*o_num_samples) *= 3;
}
#undef PIXEL
#undef FNAME
#undef SAME_PIXEL
#undef GET_r
#undef GET_g
#undef GET_b
#undef RED_BITMAP_UTILS_RGB16
#undef RED_BITMAP_UTILS_RGB24
#undef RED_BITMAP_UTILS_RGB32
#undef SAMPLE_JUMP
#undef CONTRAST_TH
#undef SAME_PIXEL_WEIGHT
#undef NOT_CONTRAST_PIXELS_WEIGHT
|