trim.im

filter: Statementstmt 69.2% 72 / 104 Branchbran 63.4% 118 / 186 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 65.5% 190 / 290 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« log.c ↑ Summary datatypes.c »
1 #define IMAGER_NO_CONTEXT
2 #include "imager.h"
3 #include "imageri.h"
4  
5 IMAGER_STATIC_INLINE int
6 163962 match_one_color(const i_sample_t *testme, const i_trim_colors_t *test) {
7 163962 ▶ if (test->c1.channel[0] > testme[0] || test->c2.channel[0] < testme[0])
Branch: 2 branches
BranchTrueFalseTotal
if (test->c1.channel[0] > testme[0] || test->c2.channel[0] < testme[0]) 163616 346 163962
if (test->c1.channel[0] > testme[0] || test->c2.channel[0] < testme[0]) 324 163292 163616
8 670 return 0;
9 163292 ▶ if (test->c1.channel[1] > testme[1] || test->c2.channel[1] < testme[1])
Branch: 2 branches
BranchTrueFalseTotal
if (test->c1.channel[1] > testme[1] || test->c2.channel[1] < testme[1]) 163180 112 163292
if (test->c1.channel[1] > testme[1] || test->c2.channel[1] < testme[1]) 92 163088 163180
10 204 return 0;
11 163088 ▶ if (test->c1.channel[2] > testme[2] || test->c2.channel[2] < testme[2])
Branch: 2 branches
BranchTrueFalseTotal
if (test->c1.channel[2] > testme[2] || test->c2.channel[2] < testme[2]) 163088 0 163088
if (test->c1.channel[2] > testme[2] || test->c2.channel[2] < testme[2]) 8 163080 163088
12 8 return 0;
13 163080 return 1;
14 }
15  
16 IMAGER_STATIC_INLINE int
17 0 match_one_fcolor(const i_fsample_t *testme, const i_trim_colors_t *test) {
18 0 ▶ if (test->cf1.channel[0] > testme[0] || test->cf2.channel[0] < testme[0])
Branch: 2 branches
BranchTrueFalseTotal
if (test->cf1.channel[0] > testme[0] || test->cf2.channel[0] < testme[0]) 0 0 0
if (test->cf1.channel[0] > testme[0] || test->cf2.channel[0] < testme[0]) 0 0 0
19 0 return 0;
20 0 ▶ if (test->cf1.channel[1] > testme[1] || test->cf2.channel[1] < testme[1])
Branch: 2 branches
BranchTrueFalseTotal
if (test->cf1.channel[1] > testme[1] || test->cf2.channel[1] < testme[1]) 0 0 0
if (test->cf1.channel[1] > testme[1] || test->cf2.channel[1] < testme[1]) 0 0 0
21 0 return 0;
22 0 ▶ if (test->cf1.channel[2] > testme[2] || test->cf2.channel[2] < testme[2])
Branch: 2 branches
BranchTrueFalseTotal
if (test->cf1.channel[2] > testme[2] || test->cf2.channel[2] < testme[2]) 0 0 0
if (test->cf1.channel[2] > testme[2] || test->cf2.channel[2] < testme[2]) 0 0 0
23 0 return 0;
24 0 return 1;
25 }
26  
27 IMAGER_STATIC_INLINE int
28 163134 match_any_color(const i_sample_t *testme, const i_trim_colors_t *tests, int count) {
29 int i;
30 164016 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 163962 54 164016
31 163962 ▶ if (match_one_color(testme, tests+i))
Branch: 1 branch
BranchTrueFalseTotal
if (match_one_color(testme, tests+i)) 163080 882 163962
32 163080 return 1;
33 }
34 54 return 0;
35 }
36  
37 IMAGER_STATIC_INLINE int
38 0 match_any_fcolor(const i_fsample_t *testme, const i_trim_colors_t *tests, int count) {
39 int i;
40 0 ▶ for (i = 0; i < count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) { 0 0 0
41 0 ▶ if (match_one_fcolor(testme, tests+i))
Branch: 1 branch
BranchTrueFalseTotal
if (match_one_fcolor(testme, tests+i)) 0 0 0
42 0 return 1;
43 }
44 0 return 0;
45 }
46  
47 #!define MATCH_ANY_COLOR match_any_color match_any_fcolor
48  
49 static const int gray_chans[4] = { 0, 0, 0, 1 };
50  
51 #define TEST_COLOR(s) \
52 ((color_count && MATCH_ANY_COLOR((s), colors, color_count)) || \
53 (check_alpha && (s)[3] <= work_threshold))
54  
55 15 static int
56 trim_rect_simple(i_img *im, double transp_threshold, int color_count,
57 const i_trim_colors_t *colors, i_img_dim *left, i_img_dim *top,
58 15 i_img_dim *right, i_img_dim *bottom) {
59 15 ▶ const int color_chans = i_img_color_channels(im);
Branch: 1 branch
BranchTrueFalseTotal
const int color_chans = i_img_color_channels(im); 0 15 15
60 15 const int *chans = color_chans == 1 ? gray_chans : NULL;
61 15 ▶ const int has_alpha = i_img_has_alpha(im);
Branch: 2 branches
BranchTrueFalseTotal
const int has_alpha = i_img_has_alpha(im); 8 7 15
const int has_alpha = i_img_has_alpha(im); 8 0 8
62 15 ▶ const int check_alpha = has_alpha && transp_threshold < 1.0;
Branch: 1 branch
BranchTrueFalseTotal
const int check_alpha = has_alpha && transp_threshold < 1.0; 8 7 15
63 const int chan_count = check_alpha ? 4 : 3;
64 i_img_dim x, y;
65 15 ▶#code im->bits <= 8
Branch: 1 branch
BranchTrueFalseTotal
#code im->bits <= 8 13 2 15
66 15 IM_SAMPLE_T *samps = mymalloc(sizeof(IM_SAMPLE_T) * im->xsize * chan_count);
67 #if IM_EIGHT_BIT
68 13 const IM_WORK_T work_threshold = floor(IM_SAMPLE_MAX * transp_threshold);
69 #else
70 2 const IM_WORK_T work_threshold = transp_threshold;
71 #endif
72  
73 /* scan down from top */
74 193 ▶ for (y = 0; y < im->ysize; ++y) {
Branch: 2 branches
BranchTrueFalseTotal
for (y = 0; y < im->ysize; ++y) { 157 0 157
for (y = 0; y < im->ysize; ++y) { 36 0 36
75 IM_SAMPLE_T *s;
76 193 IM_GSAMP(im, 0, im->xsize, y, samps, chans, chan_count);
77 38828 ▶ for (x = 0, s = samps; x < im->xsize; ++x, s += chan_count) {
Branch: 2 branches
BranchTrueFalseTotal
for (x = 0, s = samps; x < im->xsize; ++x, s += chan_count) { 29495 144 29639
for (x = 0, s = samps; x < im->xsize; ++x, s += chan_count) { 9155 34 9189
78 38650 ▶ if (!TEST_COLOR(s))
Branch: 8 branches
BranchTrueFalseTotal
if (!TEST_COLOR(s)) 20340 9155 29495
if (!TEST_COLOR(s)) 11 20329 20340
if (!TEST_COLOR(s)) 9159 7 9166
if (!TEST_COLOR(s)) 9153 6 9159
if (!TEST_COLOR(s)) 0 9155 9155
if (!TEST_COLOR(s)) 0 0 0
if (!TEST_COLOR(s)) 9155 0 9155
if (!TEST_COLOR(s)) 9153 2 9155
79 break;
80 }
81 193 ▶ if (x < im->xsize)
Branch: 2 branches
BranchTrueFalseTotal
if (x < im->xsize) 13 144 157
if (x < im->xsize) 2 34 36
82 15 break;
83 }
84 15 *top = y;
85 15 ▶ if (y < im->ysize) {
Branch: 2 branches
BranchTrueFalseTotal
if (y < im->ysize) { 13 0 13
if (y < im->ysize) { 2 0 2
86 /* scan from the bottom */
87 587 ▶ for (y = im->ysize-1; y >= 0; --y) {
Branch: 2 branches
BranchTrueFalseTotal
for (y = im->ysize-1; y >= 0; --y) { 405 0 405
for (y = im->ysize-1; y >= 0; --y) { 182 0 182
88 IM_SAMPLE_T *s;
89 587 IM_GSAMP(im, 0, im->xsize, y, samps, chans, chan_count);
90 127052 ▶ for (x = 0, s = samps; x < im->xsize; ++x, s += chan_count) {
Branch: 2 branches
BranchTrueFalseTotal
for (x = 0, s = samps; x < im->xsize; ++x, s += chan_count) { 82725 392 83117
for (x = 0, s = samps; x < im->xsize; ++x, s += chan_count) { 43755 180 43935
91 126480 ▶ if (!TEST_COLOR(s))
Branch: 8 branches
BranchTrueFalseTotal
if (!TEST_COLOR(s)) 38970 43755 82725
if (!TEST_COLOR(s)) 11 38959 38970
if (!TEST_COLOR(s)) 43759 7 43766
if (!TEST_COLOR(s)) 43753 6 43759
if (!TEST_COLOR(s)) 0 43755 43755
if (!TEST_COLOR(s)) 0 0 0
if (!TEST_COLOR(s)) 43755 0 43755
if (!TEST_COLOR(s)) 43753 2 43755
92 break;
93 }
94 587 ▶ if (x < im->xsize)
Branch: 2 branches
BranchTrueFalseTotal
if (x < im->xsize) 13 392 405
if (x < im->xsize) 2 180 182
95 15 break;
96 }
97 15 *bottom = im->ysize - y - 1;
98
99 /* we've trimmed top and bottom, now the sides */
100 15 *left = *right = im->xsize;
101 1805 ▶ for (y = *top; y < im->ysize - *bottom && (*left || *right); ++y) {
Branch: 6 branches
BranchTrueFalseTotal
for (y = *top; y < im->ysize - *bottom && (*left || *right); ++y) { 1404 13 1417
for (y = *top; y < im->ysize - *bottom && (*left || *right); ++y) { 1404 0 1404
for (y = *top; y < im->ysize - *bottom && (*left || *right); ++y) { 0 0 0
for (y = *top; y < im->ysize - *bottom && (*left || *right); ++y) { 386 2 388
for (y = *top; y < im->ysize - *bottom && (*left || *right); ++y) { 386 0 386
for (y = *top; y < im->ysize - *bottom && (*left || *right); ++y) { 0 0 0
102 IM_SAMPLE_T *s;
103 1790 IM_GSAMP(im, 0, im->xsize, y, samps, chans, chan_count);
104 71098 ▶ for (x = 0, s = samps; x < *left; ++x, s += chan_count) {
Branch: 2 branches
BranchTrueFalseTotal
for (x = 0, s = samps; x < *left; ++x, s += chan_count) { 45298 1387 46685
for (x = 0, s = samps; x < *left; ++x, s += chan_count) { 24029 384 24413
105 69327 ▶ if (!TEST_COLOR(s)) {
Branch: 8 branches
BranchTrueFalseTotal
if (!TEST_COLOR(s)) { 21269 24029 45298
if (!TEST_COLOR(s)) { 15 21254 21269
if (!TEST_COLOR(s)) { 24035 9 24044
if (!TEST_COLOR(s)) { 8 24027 24035
if (!TEST_COLOR(s)) { 0 24029 24029
if (!TEST_COLOR(s)) { 0 0 0
if (!TEST_COLOR(s)) { 24029 0 24029
if (!TEST_COLOR(s)) { 2 24027 24029
106 19 *left = x;
107 19 break;
108 }
109 }
110 1790 for (x = im->xsize - 1, s = samps + chan_count * im->xsize;
111 109134 ▶ x >= im->xsize - *right; --x, s -= chan_count) {
Branch: 2 branches
BranchTrueFalseTotal
x >= im->xsize - *right; --x, s -= chan_count) { 94960 1385 96345
x >= im->xsize - *right; --x, s -= chan_count) { 12405 384 12789
112 107365 ▶ if (!TEST_COLOR(s-chan_count)) {
Branch: 8 branches
BranchTrueFalseTotal
if (!TEST_COLOR(s-chan_count)) { 82555 12405 94960
if (!TEST_COLOR(s-chan_count)) { 17 82538 82555
if (!TEST_COLOR(s-chan_count)) { 12411 11 12422
if (!TEST_COLOR(s-chan_count)) { 8 12403 12411
if (!TEST_COLOR(s-chan_count)) { 0 12405 12405
if (!TEST_COLOR(s-chan_count)) { 0 0 0
if (!TEST_COLOR(s-chan_count)) { 12405 0 12405
if (!TEST_COLOR(s-chan_count)) { 2 12403 12405
113 21 *right = im->xsize - x - 1;
114 21 break;
115 }
116 }
117 }
118 }
119 else {
120 /* whole image can be trimmed */
121 0 *left = im->xsize;
122 0 *right = *bottom = 0;
123 }
124 15 myfree(samps);
125 #/code
126  
127 15 return 1;
128 }
129  
130 int
131 15 i_trim_rect(i_img *im, double transp_threshold, int color_count, const i_trim_colors_t *colors,
132 i_img_dim *left, i_img_dim *top, i_img_dim *right, i_img_dim *bottom) {
133 15 dIMCTXim(im);
134 15 i_trim_colors_t *tcolors = NULL;
135 i_trim_colors_t *tcolorp;
136 const i_trim_colors_t *colorp;
137 15 int tcolor_count = 0;
138 int result;
139  
140 15 ▶ if (color_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (color_count) { 11 4 15
141 11 tcolors = mymalloc(sizeof(i_trim_colors_t) * color_count);
142 11 tcolorp = tcolors;
143 /* convert 8-bit to float colors, or float colors to 8-bit depending on the
144 image type.
145 */
146 11 ▶ if (im->bits <= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (im->bits <= 8) { 11 0 11
147 int i, ch;
148 41 ▶ for (i = 0, colorp = colors; i < color_count; ++i, ++colorp) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0, colorp = colors; i < color_count; ++i, ++colorp) { 30 11 41
149 30 ▶ if (colorp->is_float) {
Branch: 1 branch
BranchTrueFalseTotal
if (colorp->is_float) { 0 30 30
150 0 ▶ for (ch = 0; ch < 3; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < 3; ++ch) { 0 0 0
151 0 tcolorp->c1.channel[ch] = ceil(colorp->cf1.channel[ch] * 255);
152 0 tcolorp->c2.channel[ch] = floor(colorp->cf2.channel[ch] * 255);
153 }
154 }
155 else {
156 30 *tcolorp = *colorp;
157 }
158 120 ▶ for (ch = 0; ch < 3; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < 3; ++ch) { 90 30 120
159 90 ▶ if (tcolorp->c1.channel[ch] > tcolorp->c2.channel[ch])
Branch: 1 branch
BranchTrueFalseTotal
if (tcolorp->c1.channel[ch] > tcolorp->c2.channel[ch]) 0 90 90
160 0 break;
161 }
162 30 ▶ if (ch == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch == 3) { 30 0 30
163 30 ++tcolorp, ++tcolor_count;
164 }
165 }
166 /* TODO optimize where the image is greyscale to remove color ranges that don't
167 overlap the greyscale line
168 */
169 }
170 else {
171 int i, ch;
172 0 ▶ for (i = 0, colorp = colors; i < color_count; ++i, ++colorp) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0, colorp = colors; i < color_count; ++i, ++colorp) { 0 0 0
173 0 ▶ if (!colorp->is_float) {
Branch: 1 branch
BranchTrueFalseTotal
if (!colorp->is_float) { 0 0 0
174 0 ▶ for (ch = 0; ch < 3; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < 3; ++ch) { 0 0 0
175 0 tcolorp->cf1.channel[ch] = colorp->c1.channel[ch] / 255.0;
176 0 tcolorp->cf2.channel[ch] = colorp->c2.channel[ch] / 255.0;
177 }
178 }
179 else {
180 0 *tcolorp = *colors;
181 }
182 0 ▶ for (ch = 0; ch < 3; ++ch) {
Branch: 1 branch
BranchTrueFalseTotal
for (ch = 0; ch < 3; ++ch) { 0 0 0
183 0 ▶ if (tcolorp->cf1.channel[ch] > tcolorp->cf2.channel[ch])
Branch: 1 branch
BranchTrueFalseTotal
if (tcolorp->cf1.channel[ch] > tcolorp->cf2.channel[ch]) 0 0 0
184 0 break;
185 }
186 0 ▶ if (ch == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch == 3) { 0 0 0
187 0 ++tcolorp, ++tcolor_count;
188 }
189 }
190 /* TODO optimize where the image is greyscale to remove color ranges that don't
191 overlap the greyscale line
192 */
193 }
194 }
195
196 15 i_clear_error();
197  
198 15 ▶ if (transp_threshold > 1.0 && tcolor_count == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (transp_threshold > 1.0 && tcolor_count == 0) { 0 15 15
if (transp_threshold > 1.0 && tcolor_count == 0) { 0 0 0
199 /* nothing to do */
200 0 *left = *top = *right = *bottom = 0;
201 0 result = 1;
202 }
203 else {
204 15 result = trim_rect_simple(im, transp_threshold, tcolor_count, tcolors, left, top,
205 right, bottom);
206 }
207  
208 15 myfree(tcolors);
209 15 return result;
210 }
« log.c ↑ Summary datatypes.c »