Debian Codegraph @

trim.im

raw
1 #define IMAGER_NO_CONTEXT
2 #include "imager.h"
3 #include "imageri.h"
4
5 IMAGER_STATIC_INLINE int
6 match_one_color(const i_sample_t *testme, const i_trim_colors_t *test) {
7 if (test->c1.channel[0] > testme[0] || test->c2.channel[0] < testme[0])
8 return 0;
9 if (test->c1.channel[1] > testme[1] || test->c2.channel[1] < testme[1])
10 return 0;
11 if (test->c1.channel[2] > testme[2] || test->c2.channel[2] < testme[2])
12 return 0;
13 return 1;
14 }
15
16 IMAGER_STATIC_INLINE int
17 match_one_fcolor(const i_fsample_t *testme, const i_trim_colors_t *test) {
18 if (test->cf1.channel[0] > testme[0] || test->cf2.channel[0] < testme[0])
19 return 0;
20 if (test->cf1.channel[1] > testme[1] || test->cf2.channel[1] < testme[1])
21 return 0;
22 if (test->cf1.channel[2] > testme[2] || test->cf2.channel[2] < testme[2])
23 return 0;
24 return 1;
25 }
26
27 IMAGER_STATIC_INLINE int
28 match_any_color(const i_sample_t *testme, const i_trim_colors_t *tests, int count) {
29 int i;
30 for (i = 0; i < count; ++i) {
31 if (match_one_color(testme, tests+i))
32 return 1;
33 }
34 return 0;
35 }
36
37 IMAGER_STATIC_INLINE int
38 match_any_fcolor(const i_fsample_t *testme, const i_trim_colors_t *tests, int count) {
39 int i;
40 for (i = 0; i < count; ++i) {
41 if (match_one_fcolor(testme, tests+i))
42 return 1;
43 }
44 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 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 i_img_dim *right, i_img_dim *bottom) {
59 const int color_chans = i_img_color_channels(im);
60 const int *chans = color_chans == 1 ? gray_chans : NULL;
61 const int has_alpha = i_img_has_alpha(im);
62 const int check_alpha = has_alpha && transp_threshold < 1.0;
63 const int chan_count = check_alpha ? 4 : 3;
64 i_img_dim x, y;
65 #code im->bits <= 8
66 IM_SAMPLE_T *samps = mymalloc(sizeof(IM_SAMPLE_T) * im->xsize * chan_count);
67 #if IM_EIGHT_BIT
68 const IM_WORK_T work_threshold = floor(IM_SAMPLE_MAX * transp_threshold);
69 #else
70 const IM_WORK_T work_threshold = transp_threshold;
71 #endif
72
73 /* scan down from top */
74 for (y = 0; y < im->ysize; ++y) {
75 IM_SAMPLE_T *s;
76 IM_GSAMP(im, 0, im->xsize, y, samps, chans, chan_count);
77 for (x = 0, s = samps; x < im->xsize; ++x, s += chan_count) {
78 if (!TEST_COLOR(s))
79 break;
80 }
81 if (x < im->xsize)
82 break;
83 }
84 *top = y;
85 if (y < im->ysize) {
86 /* scan from the bottom */
87 for (y = im->ysize-1; y >= 0; --y) {
88 IM_SAMPLE_T *s;
89 IM_GSAMP(im, 0, im->xsize, y, samps, chans, chan_count);
90 for (x = 0, s = samps; x < im->xsize; ++x, s += chan_count) {
91 if (!TEST_COLOR(s))
92 break;
93 }
94 if (x < im->xsize)
95 break;
96 }
97 *bottom = im->ysize - y - 1;
98
99 /* we've trimmed top and bottom, now the sides */
100 *left = *right = im->xsize;
101 for (y = *top; y < im->ysize - *bottom && (*left || *right); ++y) {
102 IM_SAMPLE_T *s;
103 IM_GSAMP(im, 0, im->xsize, y, samps, chans, chan_count);
104 for (x = 0, s = samps; x < *left; ++x, s += chan_count) {
105 if (!TEST_COLOR(s)) {
106 *left = x;
107 break;
108 }
109 }
110 for (x = im->xsize - 1, s = samps + chan_count * im->xsize;
111 x >= im->xsize - *right; --x, s -= chan_count) {
112 if (!TEST_COLOR(s-chan_count)) {
113 *right = im->xsize - x - 1;
114 break;
115 }
116 }
117 }
118 }
119 else {
120 /* whole image can be trimmed */
121 *left = im->xsize;
122 *right = *bottom = 0;
123 }
124 myfree(samps);
125 #/code
126
127 return 1;
128 }
129
130 int
131 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 dIMCTXim(im);
134 i_trim_colors_t *tcolors = NULL;
135 i_trim_colors_t *tcolorp;
136 const i_trim_colors_t *colorp;
137 int tcolor_count = 0;
138 int result;
139
140 if (color_count) {
141 tcolors = mymalloc(sizeof(i_trim_colors_t) * color_count);
142 tcolorp = tcolors;
143 /* convert 8-bit to float colors, or float colors to 8-bit depending on the
144 image type.
145 */
146 if (im->bits <= 8) {
147 int i, ch;
148 for (i = 0, colorp = colors; i < color_count; ++i, ++colorp) {
149 if (colorp->is_float) {
150 for (ch = 0; ch < 3; ++ch) {
151 tcolorp->c1.channel[ch] = ceil(colorp->cf1.channel[ch] * 255);
152 tcolorp->c2.channel[ch] = floor(colorp->cf2.channel[ch] * 255);
153 }
154 }
155 else {
156 *tcolorp = *colorp;
157 }
158 for (ch = 0; ch < 3; ++ch) {
159 if (tcolorp->c1.channel[ch] > tcolorp->c2.channel[ch])
160 break;
161 }
162 if (ch == 3) {
163 ++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 for (i = 0, colorp = colors; i < color_count; ++i, ++colorp) {
173 if (!colorp->is_float) {
174 for (ch = 0; ch < 3; ++ch) {
175 tcolorp->cf1.channel[ch] = colorp->c1.channel[ch] / 255.0;
176 tcolorp->cf2.channel[ch] = colorp->c2.channel[ch] / 255.0;
177 }
178 }
179 else {
180 *tcolorp = *colors;
181 }
182 for (ch = 0; ch < 3; ++ch) {
183 if (tcolorp->cf1.channel[ch] > tcolorp->cf2.channel[ch])
184 break;
185 }
186 if (ch == 3) {
187 ++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 i_clear_error();
197
198 if (transp_threshold > 1.0 && tcolor_count == 0) {
199 /* nothing to do */
200 *left = *top = *right = *bottom = 0;
201 result = 1;
202 }
203 else {
204 result = trim_rect_simple(im, transp_threshold, tcolor_count, tcolors, left, top,
205 right, bottom);
206 }
207
208 myfree(tcolors);
209 return result;
210 }