Debian Codegraph @

src/filter/medians/ctmf.h

raw symbols SCIP
1 /*
2 * ctmf.c - Constant-time median filtering
3 * Copyright (C) 2006 Simon Perreault
4 *
5 * Reference: S. Perreault and P. Hébert, "Median Filtering in Constant Time",
6 * IEEE Transactions on Image Processing, September 2007.
7 *
8 * This program has been obtained from http://nomis80.org/ctmf.html. No patent
9 * covers this program, although it is subject to the following license:
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * Contact:
25 * Laboratoire de vision et systèmes numériques
26 * Pavillon Adrien-Pouliot
27 * Université Laval
28 * Sainte-Foy, Québec, Canada
29 * G1K 7P4
30 *
31 * [email protected]
32 */
33
34 /* Standard C includes */
35 #include <assert.h>
36 #include <math.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdint.h>
40
41 /* Intrinsic declarations */
42 #if defined(__SSE2__cxx . . $ `<built-in>:383:9`!) || defined(__MMX__cxx . . $ `<built-in>:387:9`!)
43 #if defined(__SSE2__cxx . . $ `<built-in>:383:9`!)
44 #include <emmintrin.h>
45 #elif defined(__MMX__)
46 #include <mmintrin.h>
47 #endif
48 #if defined(__GNUC__cxx . . $ `<built-in>:8:9`!)
49 #include <mm_malloc.h>
50 #elif defined(_MSC_VER)
51 #include <malloc.h>
52 #endif
53 #elif defined(__ALTIVEC__)
54 #include <altivec.h>
55 #endif
56
57 /* Compiler peculiarities */
58 #if defined(__GNUC__cxx . . $ `<built-in>:8:9`!)
59 #include <stdint.h>
60 #define inlinecxx . . $ `src/filter/medians/ctmf.h:60:9`! __inline__
61 #define aligncxx . . $ `src/filter/medians/ctmf.h:61:9`!(x) __attribute__ ((aligned (x)))
62 #elif defined(_MSC_VER)
63 #define inline __inline
64 #define align(x) __declspec(align(x))
65 #else
66 #define inline
67 #define align(x)
68 #endif
69
70 #ifndef MIN
71 #define MINcxx . . $ `src/filter/medians/ctmf.h:71:9`!(a,b) ((a) > (b) ? (b) : (a))
72 #endif
73
74 #ifndef MAX
75 #define MAXcxx . . $ `src/filter/medians/ctmf.h:75:9`!(a,b) ((a) < (b) ? (b) : (a))
76 #endif
77
78 /**
79 * This structure represents a two-tier histogram. The first tier (known as the
80 * "coarse" level) is 4 bit wide and the second tier (known as the "fine" level)
81 * is 8 bit wide. Pixels inserted in the fine level also get inserted into the
82 * coarse bucket designated by the 4 MSBs of the fine bucket value.
83 *
84 * The structure is aligned on 16 bytes, which is a prerequisite for SIMD
85 * instructions. Each bucket is 16 bit wide, which means that extra care must be
86 * taken to prevent overflow.
87 */
88 typedef structcxx . . $ $anonymous_type_24769c41e7714e6_0# aligncxx . . $ `src/filter/medians/ctmf.h:61:9`!(16)
89 {
90 uint16_tcxx . . $ uint16_t# coarsecxx . . $ $anonymous_type_24769c41e7714e6_0#coarse.[16];
91 uint16_tcxx . . $ uint16_t# finecxx . . $ $anonymous_type_24769c41e7714e6_0#fine.[16][16];
92 } Histogramcxx . . $ Histogram#;
93
94 /**
95 * HOP is short for Histogram OPeration. This macro makes an operation \a op on
96 * histogram \a h for pixel value \a x. It takes care of handling both levels.
97 */
98 #define HOPcxx . . $ `src/filter/medians/ctmf.h:98:9`!(h,x,op) \
99 h.coarse[x>>4] op; \
100 *((uint16_t*) h.fine + x) op;
101
102 #define COPcxx . . $ `src/filter/medians/ctmf.h:102:9`!(c,j,x,op) \
103 h_coarse[ 16*(n*c+j) + (x>>4) ] op; \
104 h_fine[ 16 * (n*(16*c+(x>>4)) + j) + (x & 0xF) ] op;
105
106 /**
107 * Adds histograms \a x and \a y and stores the result in \a y. Makes use of
108 * SSE2, MMX or Altivec, if available.
109 */
110 #if defined(__SSE2__cxx . . $ `<built-in>:383:9`!)
111 static inlinecxx . . $ `src/filter/medians/ctmf.h:60:9`! void histogram_addcxx . . $ histogram_add(538b84f8a11f5dea).( const uint16_tcxx . . $ uint16_t# x[16], uint16_tcxx . . $ uint16_t# y[16] )
112 {
113 *(__m128icxx . . $ __m128i#*) &y[0] = _mm_add_epi16cxx . . $ _mm_add_epi16(e79ab442e937dade).( *(__m128icxx . . $ __m128i#*) &y[0], *(__m128icxx . . $ __m128i#*) &x[0] );
114 *(__m128icxx . . $ __m128i#*) &y[8] = _mm_add_epi16cxx . . $ _mm_add_epi16(e79ab442e937dade).( *(__m128icxx . . $ __m128i#*) &y[8], *(__m128icxx . . $ __m128i#*) &x[8] );
115 }
116 #elif defined(__MMX__)
117 static inline void histogram_add( const uint16_t x[16], uint16_t y[16] )
118 {
119 *(__m64*) &y[0] = _mm_add_pi16( *(__m64*) &y[0], *(__m64*) &x[0] );
120 *(__m64*) &y[4] = _mm_add_pi16( *(__m64*) &y[4], *(__m64*) &x[4] );
121 *(__m64*) &y[8] = _mm_add_pi16( *(__m64*) &y[8], *(__m64*) &x[8] );
122 *(__m64*) &y[12] = _mm_add_pi16( *(__m64*) &y[12], *(__m64*) &x[12] );
123 }
124 #elif defined(__ALTIVEC__)
125 static inline void histogram_add( const uint16_t x[16], uint16_t y[16] )
126 {
127 *(vector unsigned short*) &y[0] = vec_add( *(vector unsigned short*) &y[0], *(vector unsigned short*) &x[0] );
128 *(vector unsigned short*) &y[8] = vec_add( *(vector unsigned short*) &y[8], *(vector unsigned short*) &x[8] );
129 }
130 #else
131 static inline void histogram_add( const uint16_t x[16], uint16_t y[16] )
132 {
133 int i;
134 for ( i = 0; i < 16; ++i ) {
135 y[i] += x[i];
136 }
137 }
138 #endif
139
140 /**
141 * Subtracts histogram \a x from \a y and stores the result in \a y. Makes use
142 * of SSE2, MMX or Altivec, if available.
143 */
144 #if defined(__SSE2__cxx . . $ `<built-in>:383:9`!)
145 static inlinecxx . . $ `src/filter/medians/ctmf.h:60:9`! void histogram_subcxx . . $ histogram_sub(538b84f8a11f5dea).( const uint16_tcxx . . $ uint16_t# x[16], uint16_tcxx . . $ uint16_t# y[16] )
146 {
147 *(__m128icxx . . $ __m128i#*) &y[0] = _mm_sub_epi16cxx . . $ _mm_sub_epi16(e79ab442e937dade).( *(__m128icxx . . $ __m128i#*) &y[0], *(__m128icxx . . $ __m128i#*) &x[0] );
148 *(__m128icxx . . $ __m128i#*) &y[8] = _mm_sub_epi16cxx . . $ _mm_sub_epi16(e79ab442e937dade).( *(__m128icxx . . $ __m128i#*) &y[8], *(__m128icxx . . $ __m128i#*) &x[8] );
149 }
150 #elif defined(__MMX__)
151 static inline void histogram_sub( const uint16_t x[16], uint16_t y[16] )
152 {
153 *(__m64*) &y[0] = _mm_sub_pi16( *(__m64*) &y[0], *(__m64*) &x[0] );
154 *(__m64*) &y[4] = _mm_sub_pi16( *(__m64*) &y[4], *(__m64*) &x[4] );
155 *(__m64*) &y[8] = _mm_sub_pi16( *(__m64*) &y[8], *(__m64*) &x[8] );
156 *(__m64*) &y[12] = _mm_sub_pi16( *(__m64*) &y[12], *(__m64*) &x[12] );
157 }
158 #elif defined(__ALTIVEC__)
159 static inline void histogram_sub( const uint16_t x[16], uint16_t y[16] )
160 {
161 *(vector unsigned short*) &y[0] = vec_sub( *(vector unsigned short*) &y[0], *(vector unsigned short*) &x[0] );
162 *(vector unsigned short*) &y[8] = vec_sub( *(vector unsigned short*) &y[8], *(vector unsigned short*) &x[8] );
163 }
164 #else
165 static inline void histogram_sub( const uint16_t x[16], uint16_t y[16] )
166 {
167 int i;
168 for ( i = 0; i < 16; ++i ) {
169 y[i] -= x[i];
170 }
171 }
172 #endif
173
174 static inlinecxx . . $ `src/filter/medians/ctmf.h:60:9`! void histogram_muladdcxx . . $ histogram_muladd(b3a5f9583ec2d65b).( const uint16_tcxx . . $ uint16_t# a, const uint16_tcxx . . $ uint16_t# x[16],
175 uint16_tcxx . . $ uint16_t# y[16] )
176 {
177 int i;
178 for ( i = 0; i < 16; ++i ) {
179 y[i] += a * x[i];
180 }
181 }
182
183 static void ctmf_helpercxx . . $ ctmf_helper(9235ecc91a7940e2).(
184 const unsigned char* const src, unsigned char* const dst,
185 const int width, const int height,
186 const int src_step, const int dst_step,
187 const int r, const int cn,
188 const int pad_left, const int pad_right
189 )
190 {
191 const int m = height, n = width;
192 int i, j, k, c;
193 const unsigned char *p, *q;
194
195 Histogramcxx . . $ Histogram# H[4];
196 uint16_tcxx . . $ uint16_t# *h_coarse, *h_fine, luc[4][16];
197
198 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!( src );
199 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!( dst );
200 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!( r >= 0 );
201 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!( width >= 2*r+1 );
202 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!( height >= 2*r+1 );
203 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!( src_step != 0 );
204 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!( dst_step != 0 );
205
206 /* SSE2 and MMX need aligned memory, provided by _mm_malloc(). */
207 #if defined(__SSE2__cxx . . $ `<built-in>:383:9`!) || defined(__MMX__cxx . . $ `<built-in>:387:9`!)
208 h_coarse = (uint16_tcxx . . $ uint16_t#*) _mm_malloccxx . . $ _mm_malloc(63a7bf2941bfcd7a).( 1 * 16 * n * cn * sizeof(uint16_tcxx . . $ uint16_t#), 16 );
209 h_fine = (uint16_tcxx . . $ uint16_t#*) _mm_malloccxx . . $ _mm_malloc(63a7bf2941bfcd7a).( 16 * 16 * n * cn * sizeof(uint16_tcxx . . $ uint16_t#), 16 );
210 memsetcxx . . $ memset(d1de1e4fb215f1c6).( h_coarse, 0, 1 * 16 * n * cn * sizeof(uint16_tcxx . . $ uint16_t#) );
211 memsetcxx . . $ memset(d1de1e4fb215f1c6).( h_fine, 0, 16 * 16 * n * cn * sizeof(uint16_tcxx . . $ uint16_t#) );
212 #else
213 h_coarse = (uint16_t*) calloc( 1 * 16 * n * cn, sizeof(uint16_t) );
214 h_fine = (uint16_t*) calloc( 16 * 16 * n * cn, sizeof(uint16_t) );
215 #endif
216
217 /* First row initialization */
218 for ( j = 0; j < n; ++j ) {
219 for ( c = 0; c < cn; ++c ) {
220 COPcxx . . $ `src/filter/medians/ctmf.h:102:9`!( c, j, src[cn*j+c], += r+1 );
221 }
222 }
223 for ( i = 0; i < r; ++i ) {
224 for ( j = 0; j < n; ++j ) {
225 for ( c = 0; c < cn; ++c ) {
226 COPcxx . . $ `src/filter/medians/ctmf.h:102:9`!( c, j, src[src_step*i+cn*j+c], ++ );
227 }
228 }
229 }
230
231 for ( i = 0; i < m; ++i ) {
232
233 /* Update column histograms for entire row. */
234 p = src + src_step * MAXcxx . . $ `src/filter/medians/ctmf.h:75:9`!( 0, i-r-1 );
235 q = p + cn * n;
236 for ( j = 0; p != q; ++j ) {
237 for ( c = 0; c < cn; ++c, ++p ) {
238 COPcxx . . $ `src/filter/medians/ctmf.h:102:9`!( c, j, *p, -- );
239 }
240 }
241
242 p = src + src_step * MINcxx . . $ `src/filter/medians/ctmf.h:71:9`!( m-1, i+r );
243 q = p + cn * n;
244 for ( j = 0; p != q; ++j ) {
245 for ( c = 0; c < cn; ++c, ++p ) {
246 COPcxx . . $ `src/filter/medians/ctmf.h:102:9`!( c, j, *p, ++ );
247 }
248 }
249
250 /* First column initialization */
251 memsetcxx . . $ memset(d1de1e4fb215f1c6).( H, 0, cn*sizeof(H[0]) );
252 memsetcxx . . $ memset(d1de1e4fb215f1c6).( luc, 0, cn*sizeof(luc[0]) );
253 if ( pad_left ) {
254 for ( c = 0; c < cn; ++c ) {
255 histogram_muladdcxx . . $ histogram_muladd(b3a5f9583ec2d65b).( r, &h_coarse[16*n*c], H[c].coarsecxx . . $ $anonymous_type_24769c41e7714e6_0#coarse. );
256 }
257 }
258 for ( j = 0; j < (pad_left ? r : 2*r); ++j ) {
259 for ( c = 0; c < cn; ++c ) {
260 histogram_addcxx . . $ histogram_add(538b84f8a11f5dea).( &h_coarse[16*(n*c+j)], H[c].coarsecxx . . $ $anonymous_type_24769c41e7714e6_0#coarse. );
261 }
262 }
263 for ( c = 0; c < cn; ++c ) {
264 for ( k = 0; k < 16; ++k ) {
265 histogram_muladdcxx . . $ histogram_muladd(b3a5f9583ec2d65b).( 2*r+1, &h_fine[16*n*(16*c+k)], &H[c].finecxx . . $ $anonymous_type_24769c41e7714e6_0#fine.[k][0] );
266 }
267 }
268
269 for ( j = pad_left ? 0 : r; j < (pad_right ? n : n-r); ++j ) {
270 for ( c = 0; c < cn; ++c ) {
271 const uint16_tcxx . . $ uint16_t# t = 2*r*r + 2*r;
272 uint16_tcxx . . $ uint16_t# sum = 0, *segment;
273 int b;
274
275 histogram_addcxx . . $ histogram_add(538b84f8a11f5dea).( &h_coarse[16*(n*c + MINcxx . . $ `src/filter/medians/ctmf.h:71:9`!(j+r,n-1))], H[c].coarsecxx . . $ $anonymous_type_24769c41e7714e6_0#coarse. );
276
277 /* Find median at coarse level */
278 for ( k = 0; k < 16 ; ++k ) {
279 sum += H[c].coarsecxx . . $ $anonymous_type_24769c41e7714e6_0#coarse.[k];
280 if ( sum > t ) {
281 sum -= H[c].coarsecxx . . $ $anonymous_type_24769c41e7714e6_0#coarse.[k];
282 break;
283 }
284 }
285 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!( k < 16 );
286
287 /* Update corresponding histogram segment */
288 if ( luc[c][k] <= j-r ) {
289 memsetcxx . . $ memset(d1de1e4fb215f1c6).( &H[c].finecxx . . $ $anonymous_type_24769c41e7714e6_0#fine.[k], 0, 16 * sizeof(uint16_tcxx . . $ uint16_t#) );
290 for ( luc[c][k] = j-r; luc[c][k] < MINcxx . . $ `src/filter/medians/ctmf.h:71:9`!(j+r+1,n); ++luc[c][k] ) {
291 histogram_addcxx . . $ histogram_add(538b84f8a11f5dea).( &h_fine[16*(n*(16*c+k)+luc[c][k])], H[c].finecxx . . $ $anonymous_type_24769c41e7714e6_0#fine.[k] );
292 }
293 if ( luc[c][k] < j+r+1 ) {
294 histogram_muladdcxx . . $ histogram_muladd(b3a5f9583ec2d65b).( j+r+1 - n, &h_fine[16*(n*(16*c+k)+(n-1))], &H[c].finecxx . . $ $anonymous_type_24769c41e7714e6_0#fine.[k][0] );
295 luc[c][k] = j+r+1;
296 }
297 }
298 else {
299 for ( ; luc[c][k] < j+r+1; ++luc[c][k] ) {
300 histogram_subcxx . . $ histogram_sub(538b84f8a11f5dea).( &h_fine[16*(n*(16*c+k)+MAXcxx . . $ `src/filter/medians/ctmf.h:75:9`!(luc[c][k]-2*r-1,0))], H[c].finecxx . . $ $anonymous_type_24769c41e7714e6_0#fine.[k] );
301 histogram_addcxx . . $ histogram_add(538b84f8a11f5dea).( &h_fine[16*(n*(16*c+k)+MINcxx . . $ `src/filter/medians/ctmf.h:71:9`!(luc[c][k],n-1))], H[c].finecxx . . $ $anonymous_type_24769c41e7714e6_0#fine.[k] );
302 }
303 }
304
305 histogram_subcxx . . $ histogram_sub(538b84f8a11f5dea).( &h_coarse[16*(n*c+MAXcxx . . $ `src/filter/medians/ctmf.h:75:9`!(j-r,0))], H[c].coarsecxx . . $ $anonymous_type_24769c41e7714e6_0#coarse. );
306
307 /* Find median in segment */
308 segment = H[c].finecxx . . $ $anonymous_type_24769c41e7714e6_0#fine.[k];
309 for ( b = 0; b < 16 ; ++b ) {
310 sum += segment[b];
311 if ( sum > t ) {
312 dst[dst_step*i+cn*j+c] = 16*k + b;
313 break;
314 }
315 }
316 assertcxx . . $ `<external>/4398592474888995393/assert.h:77:11`!( b < 16 );
317 }
318 }
319 }
320
321 #if defined(__SSE2__cxx . . $ `<built-in>:383:9`!) || defined(__MMX__cxx . . $ `<built-in>:387:9`!)
322 _mm_emptycxx . . $ _mm_empty(49f6e7a06ebc5aa8).();
323 _mm_freecxx . . $ _mm_free(5d22bdacc48458e8).(h_coarse);
324 _mm_freecxx . . $ _mm_free(5d22bdacc48458e8).(h_fine);
325 #else
326 free(h_coarse);
327 free(h_fine);
328 #endif
329 }
330
331 /**
332 * \brief Constant-time median filtering
333 *
334 * This function does a median filtering of an 8-bit image. The source image is
335 * processed as if it was padded with zeros. The median kernel is square with
336 * odd dimensions. Images of arbitrary size may be processed.
337 *
338 * To process multi-channel images, you must call this function multiple times,
339 * changing the source and destination addresses and steps such that each channel
340 * is processed as an independent single-channel image.
341 *
342 * Processing images of arbitrary bit depth is not supported.
343 *
344 * The computing time is O(1) per pixel, independent of the radius of the
345 * filter. The algorithm's initialization is O(r*width), but it is negligible.
346 * Memory usage is simple: it will be as big as the cache size, or smaller if
347 * the image is small. For efficiency, the histograms' bins are 16-bit wide.
348 * This may become too small and lead to overflow as \a r increases.
349 *
350 * \param src Source image data.
351 * \param dst Destination image data. Must be preallocated.
352 * \param width Image width, in pixels.
353 * \param height Image height, in pixels.
354 * \param src_step Distance between adjacent pixels on the same column in
355 * the source image, in bytes.
356 * \param dst_step Distance between adjacent pixels on the same column in
357 * the destination image, in bytes.
358 * \param r Median filter radius. The kernel will be a 2*r+1 by
359 * 2*r+1 square.
360 * \param cn Number of channels. For example, a grayscale image would
361 * have cn=1 while an RGB image would have cn=3.
362 * \param memsize Maximum amount of memory to use, in bytes. Set this to
363 * the size of the L2 cache, then vary it slightly and
364 * measure the processing time to find the optimal value.
365 * For example, a 512 kB L2 cache would have
366 * memsize=512*1024 initially.
367 */
368 void ctmfcxx . . $ ctmf(819d8c7cec1a7d9c).(
369 const unsigned char* const src, unsigned char* const dst,
370 const int width, const int height,
371 const int src_step, const int dst_step,
372 const int r, const int cn, const long unsigned int memsize
373 )
374 {
375 /*
376 * Processing the image in vertical stripes is an optimization made
377 * necessary by the limited size of the CPU cache. Each histogram is 544
378 * bytes big and therefore I can fit a limited number of them in the cache.
379 * That number may sometimes be smaller than the image width, which would be
380 * the number of histograms I would need without stripes.
381 *
382 * I need to keep histograms in the cache so that they are available
383 * quickly when processing a new row. Each row needs access to the previous
384 * row's histograms. If there are too many histograms to fit in the cache,
385 * thrashing to RAM happens.
386 *
387 * To solve this problem, I figure out the maximum number of histograms
388 * that can fit in cache. From this is determined the number of stripes in
389 * an image. The formulas below make the stripes all the same size and use
390 * as few stripes as possible.
391 *
392 * Note that each stripe causes an overlap on the neighboring stripes, as
393 * when mowing the lawn. That overlap is proportional to r. When the overlap
394 * is a significant size in comparison with the stripe size, then we are not
395 * O(1) anymore, but O(r). In fact, we have been O(r) all along, but the
396 * initialization term was neglected, as it has been (and rightly so) in B.
397 * Weiss, "Fast Median and Bilateral Filtering", SIGGRAPH, 2006. Processing
398 * by stripes only makes that initialization term bigger.
399 *
400 * Also, note that the leftmost and rightmost stripes don't need overlap.
401 * A flag is passed to ctmf_helper() so that it treats these cases as if the
402 * image was zero-padded.
403 */
404 int stripes = (int) ceil( (double) (width - 2*r) / (memsize / sizeof(Histogramcxx . . $ Histogram#) - 2*r) );
405 int stripe_size = (int) ceil( (double) ( width + stripes*2*r - 2*r ) / stripes );
406
407 int i;
408
409 for ( i = 0; i < width; i += stripe_size - 2*r ) {
410 int stripe = stripe_size;
411 /* Make sure that the filter kernel fits into one stripe. */
412 if ( i + stripe_size - 2*r >= width || width - (i + stripe_size - 2*r) < 2*r+1 ) {
413 stripe = width - i;
414 }
415
416 ctmf_helpercxx . . $ ctmf_helper(9235ecc91a7940e2).( src + cn*i, dst + cn*i, stripe, height, src_step, dst_step, r, cn,
417 i == 0, stripe == width - i );
418
419 if ( stripe == width - i ) {
420 break;
421 }
422 }
423 }