]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/gil/test/legacy/performance.cpp
2 // Copyright 2005-2007 Adobe Systems Incorporated
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 #include <boost/gil.hpp>
10 #include <boost/core/lightweight_test.hpp>
16 // GIL performance test suite
19 // fill_pixels() on rgb8_image_t with rgb8_pixel_t
20 // fill_pixels() on rgb8_planar_image_t with rgb8_pixel_t
21 // fill_pixels() on rgb8_image_t with bgr8_pixel_t
22 // fill_pixels() on rgb8_planar_image_t with bgr8_pixel_t
23 // for_each_pixel() on rgb8_image_t
24 // for_each_pixel() on rgb8_planar_t
25 // copy_pixels() between rgb8_image_t and rgb8_image_t
26 // copy_pixels() between rgb8_image_t and bgr8_image_t
27 // copy_pixels() between rgb8_planar_image_t and rgb8_planar_image_t
28 // copy_pixels() between rgb8_image_t and rgb8_planar_image_t
29 // copy_pixels() between rgb8_planar_image_t and rgb8_image_t
30 // transform_pixels() between rgb8_image_t and rgb8_image_t
31 // transform_pixels() between rgb8_planar_image_t and rgb8_planar_image_t
32 // transform_pixels() between rgb8_planar_image_t and rgb8_image_t
33 // transform_pixels() between rgb8_image_t and rgb8_planar_image_t
35 using namespace boost::gil
;
37 // returns time in milliseconds per call
38 template <typename Op
>
39 double measure_time(Op op
, std::size_t num_loops
) {
40 clock_t begin
=clock();
41 for (std::size_t ii
=0; ii
<num_loops
; ++ii
) op();
42 return double(clock()-begin
)/double(num_loops
);
46 std::size_t width
=1000, height
=400;
48 // macros for standard GIL views
49 #define RGB_VIEW(T) image_view<memory_based_2d_locator<memory_based_step_iterator<pixel<T,rgb_layout_t>*>>>
50 #define BGR_VIEW(T) image_view<memory_based_2d_locator<memory_based_step_iterator<pixel<T,bgr_layout_t>*>>>
51 #define RGB_PLANAR_VIEW(T) image_view<memory_based_2d_locator<memory_based_step_iterator<planar_pixel_iterator<T*,rgb_t>>>>
53 template <typename View
, typename P
>
57 fill_gil_t(const View
& v_in
,const P
& p_in
) : _v(v_in
), _p(p_in
) {}
58 void operator()() const {fill_pixels(_v
,_p
);}
60 template <typename View
, typename P
> struct fill_nongil_t
;
61 template <typename T
, typename P
>
62 struct fill_nongil_t
<RGB_VIEW(T
), P
>
64 using View
= RGB_VIEW(T
);
67 fill_nongil_t(const View
& v_in
,const P
& p_in
) : _v(v_in
), _p(p_in
) {}
68 void operator()() const {
69 T
* first
=(T
*)_v
.row_begin(0);
70 T
* last
=first
+_v
.size()*3;
72 first
[0]=boost::gil::at_c
<0>(_p
);
73 first
[1]=boost::gil::at_c
<1>(_p
);
74 first
[2]=boost::gil::at_c
<2>(_p
);
79 template <typename T1
, typename T2
>
80 struct fill_nongil_t
<RGB_VIEW(T1
), pixel
<T2
,bgr_layout_t
>>
82 using View
= RGB_VIEW(T1
);
83 using P
= pixel
<T2
, bgr_layout_t
>;
86 fill_nongil_t(const View
& v_in
,const P
& p_in
) : _v(v_in
), _p(p_in
) {}
87 void operator()() const {
88 T1
* first
=(T1
*)_v
.row_begin(0);
89 T1
* last
=first
+_v
.size()*3;
91 first
[0]=boost::gil::at_c
<2>(_p
);
92 first
[1]=boost::gil::at_c
<1>(_p
);
93 first
[2]=boost::gil::at_c
<0>(_p
);
98 template <typename T1
, typename T2
>
99 struct fill_nongil_t
<RGB_PLANAR_VIEW(T1
), pixel
<T2
,rgb_layout_t
>>
101 using View
= RGB_PLANAR_VIEW(T1
);
102 using P
= pixel
<T2
, rgb_layout_t
>;
105 fill_nongil_t(const View
& v_in
,const P
& p_in
) : _v(v_in
), _p(p_in
) {}
106 void operator()() const {
107 std::size_t size
=_v
.size();
109 first
=(T1
*)boost::gil::at_c
<0>(_v
.row_begin(0));
110 std::fill(first
,first
+size
,boost::gil::at_c
<0>(_p
));
111 first
=(T1
*)boost::gil::at_c
<1>(_v
.row_begin(0));
112 std::fill(first
,first
+size
,boost::gil::at_c
<1>(_p
));
113 first
=(T1
*)boost::gil::at_c
<2>(_v
.row_begin(0));
114 std::fill(first
,first
+size
,boost::gil::at_c
<2>(_p
));
118 template <typename T1
, typename T2
>
119 struct fill_nongil_t
<RGB_PLANAR_VIEW(T1
), pixel
<T2
,bgr_layout_t
>>
121 using View
= RGB_PLANAR_VIEW(T1
);
122 using P
= pixel
<T2
,bgr_layout_t
>;
125 fill_nongil_t(const View
& v_in
,const P
& p_in
) : _v(v_in
), _p(p_in
) {}
126 void operator()() const {
127 std::size_t size
=_v
.size();
129 first
=(T1
*)boost::gil::at_c
<0>(_v
.row_begin(0));
130 std::fill(first
,first
+size
,boost::gil::at_c
<2>(_p
));
131 first
=(T1
*)boost::gil::at_c
<1>(_v
.row_begin(0));
132 std::fill(first
,first
+size
,boost::gil::at_c
<1>(_p
));
133 first
=(T1
*)boost::gil::at_c
<2>(_v
.row_begin(0));
134 std::fill(first
,first
+size
,boost::gil::at_c
<1>(_p
));
138 template <typename View
, typename P
>
139 void test_fill(std::size_t trials
) {
140 image
<typename
View::value_type
, is_planar
<View
>::value
> im(width
,height
);
141 std::cout
<< "GIL: "<< measure_time(fill_gil_t
<View
,P
>(view(im
),P()),trials
) << std::endl
;
142 std::cout
<< "Non-GIL: "<< measure_time(fill_nongil_t
<View
,P
>(view(im
),P()),trials
) << std::endl
;
145 template <typename T
>
147 void operator()(pixel
<T
,rgb_layout_t
>& p
) const {p
[0]=0;p
[1]=1;p
[2]=2;}
148 void operator()(const planar_pixel_reference
<T
&,rgb_t
>& p
) const {p
[0]=0;p
[1]=1;p
[2]=2;}
150 template <typename View
, typename F
>
151 struct for_each_gil_t
{
154 for_each_gil_t(const View
& v_in
,const F
& f_in
) : _v(v_in
), _f(f_in
) {}
155 void operator()() const {for_each_pixel(_v
,_f
);}
157 template <typename View
, typename F
> struct for_each_nongil_t
;
158 template <typename T
, typename T2
>
159 struct for_each_nongil_t
<RGB_VIEW(T
), rgb_fr_t
<T2
>>
161 using View
= RGB_VIEW(T
);
162 using F
= rgb_fr_t
<T2
>;
165 for_each_nongil_t(const View
& v_in
,const F
& f_in
) : _v(v_in
), _f(f_in
) {}
166 void operator()() const {
167 T
* first
=(T
*)_v
.row_begin(0);
168 T
* last
=first
+_v
.size()*3;
177 template <typename T1
, typename T2
>
178 struct for_each_nongil_t
<RGB_PLANAR_VIEW(T1
), rgb_fr_t
<T2
>>
180 using View
= RGB_PLANAR_VIEW(T1
);
181 using F
= rgb_fr_t
<T2
>;
184 for_each_nongil_t(const View
& v_in
,const F
& f_in
) : _v(v_in
), _f(f_in
) {}
185 void operator()() const {
186 T1
*first0
, *first1
, *first2
, *last0
;
187 first0
=(T1
*)boost::gil::at_c
<0>(_v
.row_begin(0));
188 first1
=(T1
*)boost::gil::at_c
<1>(_v
.row_begin(0));
189 first2
=(T1
*)boost::gil::at_c
<2>(_v
.row_begin(0));
190 last0
=first0
+_v
.size();
191 while(first0
!=last0
) {
199 template <typename View
, typename F
>
200 void test_for_each(std::size_t trials
) {
201 image
<typename
View::value_type
, is_planar
<View
>::value
> im(width
,height
);
202 std::cout
<< "GIL: "<<measure_time(for_each_gil_t
<View
,F
>(view(im
),F()),trials
) << std::endl
;
203 std::cout
<< "Non-GIL: "<<measure_time(for_each_nongil_t
<View
,F
>(view(im
),F()),trials
) << std::endl
;
207 template <typename View1
, typename View2
>
211 copy_gil_t(const View1
& v1_in
,const View2
& v2_in
) : _v1(v1_in
), _v2(v2_in
) {}
212 void operator()() const {copy_pixels(_v1
,_v2
);}
214 template <typename View1
, typename View2
> struct copy_nongil_t
;
215 template <typename T1
, typename T2
>
216 struct copy_nongil_t
<RGB_VIEW(T1
),RGB_VIEW(T2
)>
218 using View1
= RGB_VIEW(T1
);
219 using View2
= RGB_VIEW(T2
);
222 copy_nongil_t(const View1
& v1_in
,const View2
& v2_in
) : _v1(v1_in
), _v2(v2_in
) {}
223 void operator()() const {
224 T1
* first1
=(T1
*)_v1
.row_begin(0);
225 T1
* last1
=first1
+_v1
.size()*3;
226 T2
* first2
=(T2
*)_v2
.row_begin(0);
227 std::copy(first1
,last1
,first2
);
230 template <typename T1
, typename T2
>
231 struct copy_nongil_t
<RGB_VIEW(T1
),BGR_VIEW(T2
)>
233 using View1
= RGB_VIEW(T1
);
234 using View2
= BGR_VIEW(T2
);
237 copy_nongil_t(const View1
& v1_in
,const View2
& v2_in
) : _v1(v1_in
), _v2(v2_in
) {}
238 void operator()() const {
239 T1
* first1
=(T1
*)_v1
.row_begin(0);
240 T1
* last1
=first1
+_v1
.size()*3;
241 T2
* first2
=(T2
*)_v2
.row_begin(0);
242 while(first1
!=last1
) {
246 first1
+=3; first2
+=3;
250 template <typename T1
, typename T2
>
251 struct copy_nongil_t
<RGB_PLANAR_VIEW(T1
),RGB_PLANAR_VIEW(T2
)>
253 using View1
= RGB_PLANAR_VIEW(T1
);
254 using View2
= RGB_PLANAR_VIEW(T2
);
257 copy_nongil_t(const View1
& v1_in
,const View2
& v2_in
) : _v1(v1_in
), _v2(v2_in
) {}
258 void operator()() const {
259 std::size_t size
=_v1
.size();
260 T1
* first10
=(T1
*)boost::gil::at_c
<0>(_v1
.row_begin(0));
261 T1
* first11
=(T1
*)boost::gil::at_c
<1>(_v1
.row_begin(0));
262 T1
* first12
=(T1
*)boost::gil::at_c
<2>(_v1
.row_begin(0));
263 T2
* first20
=(T2
*)boost::gil::at_c
<0>(_v2
.row_begin(0));
264 T2
* first21
=(T2
*)boost::gil::at_c
<1>(_v2
.row_begin(0));
265 T2
* first22
=(T2
*)boost::gil::at_c
<2>(_v2
.row_begin(0));
266 std::copy(first10
,first10
+size
,first20
);
267 std::copy(first11
,first11
+size
,first21
);
268 std::copy(first12
,first12
+size
,first22
);
271 template <typename T1
, typename T2
>
272 struct copy_nongil_t
<RGB_VIEW(T1
),RGB_PLANAR_VIEW(T2
)>
274 using View1
= RGB_VIEW(T1
);
275 using View2
= RGB_PLANAR_VIEW(T2
);
278 copy_nongil_t(const View1
& v1_in
,const View2
& v2_in
) : _v1(v1_in
), _v2(v2_in
) {}
279 void operator()() const {
280 T1
* first
=(T1
*)_v1
.row_begin(0);
281 T1
* last
=first
+_v1
.size()*3;
282 T2
* first0
=(T2
*)boost::gil::at_c
<0>(_v2
.row_begin(0));
283 T2
* first1
=(T2
*)boost::gil::at_c
<1>(_v2
.row_begin(0));
284 T2
* first2
=(T2
*)boost::gil::at_c
<2>(_v2
.row_begin(0));
293 template <typename T1
, typename T2
>
294 struct copy_nongil_t
<RGB_PLANAR_VIEW(T1
),RGB_VIEW(T2
)>
296 using View1
= RGB_PLANAR_VIEW(T1
);
297 using View2
= RGB_VIEW(T2
);
300 copy_nongil_t(const View1
& v1_in
,const View2
& v2_in
) : _v1(v1_in
), _v2(v2_in
) {}
301 void operator()() const {
302 T1
* first
=(T1
*)_v2
.row_begin(0);
303 T1
* last
=first
+_v2
.size()*3;
304 T2
* first0
=(T2
*)boost::gil::at_c
<0>(_v1
.row_begin(0));
305 T2
* first1
=(T2
*)boost::gil::at_c
<1>(_v1
.row_begin(0));
306 T2
* first2
=(T2
*)boost::gil::at_c
<2>(_v1
.row_begin(0));
315 template <typename View1
, typename View2
>
316 void test_copy(std::size_t trials
) {
317 image
<typename
View1::value_type
, is_planar
<View1
>::value
> im1(width
,height
);
318 image
<typename
View2::value_type
, is_planar
<View2
>::value
> im2(width
,height
);
319 std::cout
<< "GIL: " <<measure_time(copy_gil_t
<View1
,View2
>(view(im1
),view(im2
)),trials
) << std::endl
;
320 std::cout
<< "Non-GIL: "<<measure_time(copy_nongil_t
<View1
,View2
>(view(im1
),view(im2
)),trials
) << std::endl
;
324 template <typename T
,typename Pixel
>
325 struct bgr_to_rgb_t
{
326 pixel
<T
,rgb_layout_t
> operator()(const Pixel
& p
) const {
327 return pixel
<T
,rgb_layout_t
>(T(get_color(p
,blue_t())*0.1f
),
328 T(get_color(p
,green_t())*0.2f
),
329 T(get_color(p
,red_t())*0.3f
));
332 template <typename View1
, typename View2
, typename F
>
333 struct transform_gil_t
{
337 transform_gil_t(const View1
& v1_in
,const View2
& v2_in
,const F
& f_in
) : _v1(v1_in
),_v2(v2_in
),_f(f_in
) {}
338 void operator()() const {transform_pixels(_v1
,_v2
,_f
);}
340 template <typename View1
, typename View2
, typename F
> struct transform_nongil_t
;
341 template <typename T1
, typename T2
, typename F
>
342 struct transform_nongil_t
<RGB_VIEW(T1
),RGB_VIEW(T2
),F
>
344 using View1
= RGB_VIEW(T1
);
345 using View2
= RGB_VIEW(T2
);
349 transform_nongil_t(const View1
& v1_in
,const View2
& v2_in
,const F
& f_in
) : _v1(v1_in
),_v2(v2_in
),_f(f_in
) {}
350 void operator()() const {
351 T1
* first1
=(T1
*)_v1
.row_begin(0);
352 T2
* first2
=(T1
*)_v2
.row_begin(0);
353 T1
* last1
=first1
+_v1
.size()*3;
354 while(first1
!=last1
) {
355 first2
[0]=T2(first1
[2]*0.1f
);
356 first2
[1]=T2(first1
[1]*0.2f
);
357 first2
[2]=T2(first1
[0]*0.3f
);
358 first1
+=3; first2
+=3;
362 template <typename T1
, typename T2
, typename F
>
363 struct transform_nongil_t
<RGB_PLANAR_VIEW(T1
),RGB_PLANAR_VIEW(T2
),F
>
365 using View1
= RGB_PLANAR_VIEW(T1
);
366 using View2
= RGB_PLANAR_VIEW(T2
);
370 transform_nongil_t(const View1
& v1_in
,const View2
& v2_in
,const F
& f_in
) : _v1(v1_in
),_v2(v2_in
),_f(f_in
) {}
371 void operator()() const {
372 T1
* first10
=(T1
*)boost::gil::at_c
<0>(_v1
.row_begin(0));
373 T1
* first11
=(T1
*)boost::gil::at_c
<1>(_v1
.row_begin(0));
374 T1
* first12
=(T1
*)boost::gil::at_c
<2>(_v1
.row_begin(0));
375 T1
* first20
=(T2
*)boost::gil::at_c
<0>(_v2
.row_begin(0));
376 T1
* first21
=(T2
*)boost::gil::at_c
<1>(_v2
.row_begin(0));
377 T1
* first22
=(T2
*)boost::gil::at_c
<2>(_v2
.row_begin(0));
378 T1
* last10
=first10
+_v1
.size();
379 while(first10
!=last10
) {
380 *first20
++=T2(*first12
++*0.1f
);
381 *first21
++=T2(*first11
++*0.2f
);
382 *first22
++=T2(*first10
++*0.3f
);
386 template <typename T1
, typename T2
, typename F
>
387 struct transform_nongil_t
<RGB_VIEW(T1
),RGB_PLANAR_VIEW(T2
),F
>
389 using View1
= RGB_VIEW(T1
);
390 using View2
= RGB_PLANAR_VIEW(T2
);
394 transform_nongil_t(const View1
& v1_in
,const View2
& v2_in
,const F
& f_in
) : _v1(v1_in
),_v2(v2_in
),_f(f_in
) {}
395 void operator()() const {
396 T1
* first1
=(T1
*)_v1
.row_begin(0);
397 T1
* last1
=first1
+_v1
.size()*3;
398 T1
* first20
=(T2
*)boost::gil::at_c
<0>(_v2
.row_begin(0));
399 T1
* first21
=(T2
*)boost::gil::at_c
<1>(_v2
.row_begin(0));
400 T1
* first22
=(T2
*)boost::gil::at_c
<2>(_v2
.row_begin(0));
401 while(first1
!=last1
) {
402 *first20
++=T2(first1
[2]*0.1f
);
403 *first21
++=T2(first1
[1]*0.2f
);
404 *first22
++=T2(first1
[0]*0.3f
);
409 template <typename T1
, typename T2
, typename F
>
410 struct transform_nongil_t
<RGB_PLANAR_VIEW(T1
),RGB_VIEW(T2
),F
>
412 using View1
= RGB_PLANAR_VIEW(T1
);
413 using View2
= RGB_VIEW(T2
);
417 transform_nongil_t(const View1
& v1_in
,const View2
& v2_in
,const F
& f_in
) : _v1(v1_in
),_v2(v2_in
),_f(f_in
) {}
418 void operator()() const {
419 T1
* first10
=(T1
*)boost::gil::at_c
<0>(_v1
.row_begin(0));
420 T1
* first11
=(T1
*)boost::gil::at_c
<1>(_v1
.row_begin(0));
421 T1
* first12
=(T1
*)boost::gil::at_c
<2>(_v1
.row_begin(0));
422 T2
* first2
=(T1
*)_v2
.row_begin(0);
423 T1
* last2
=first2
+_v1
.size()*3;
424 while(first2
!=last2
) {
425 first2
[0]=T2(*first12
++*0.1f
);
426 first2
[1]=T2(*first11
++*0.2f
);
427 first2
[2]=T2(*first10
++*0.3f
);
433 template <typename View1
, typename View2
, typename F
>
434 void test_transform(std::size_t trials
) {
435 image
<typename
View1::value_type
, is_planar
<View1
>::value
> im1(width
,height
);
436 image
<typename
View2::value_type
, is_planar
<View2
>::value
> im2(width
,height
);
437 //std::cout << "GIL: " <<measure_time(transform_gil_t<View1,View2,F>(view(im1),view(im2),F()),trials) << std::endl;
438 //std::cout << "Non-GIL: "<<measure_time(transform_nongil_t<View1,View2,F>(view(im1),view(im2),F()),trials) << std::endl;
441 void test_performance()
444 std::size_t num_trials
=1000;
446 std::size_t num_trials
=1;
450 std::cout
<<"test fill_pixels() on rgb8_image_t with rgb8_pixel_t"<<std::endl
;
451 test_fill
<rgb8_view_t
,rgb8_pixel_t
>(num_trials
);
452 std::cout
<<std::endl
;
454 std::cout
<<"test fill_pixels() on rgb8_planar_image_t with rgb8_pixel_t"<<std::endl
;
455 test_fill
<rgb8_planar_view_t
,rgb8_pixel_t
>(num_trials
);
456 std::cout
<<std::endl
;
458 std::cout
<<"test fill_pixels() on rgb8_image_t with bgr8_pixel_t"<<std::endl
;
459 test_fill
<rgb8_view_t
,bgr8_pixel_t
>(num_trials
);
460 std::cout
<<std::endl
;
462 std::cout
<<"test fill_pixels() on rgb8_planar_image_t with bgr8_pixel_t"<<std::endl
;
463 test_fill
<rgb8_planar_view_t
,bgr8_pixel_t
>(num_trials
);
464 std::cout
<<std::endl
;
467 std::cout
<<"test for_each_pixel() on rgb8_image_t"<<std::endl
;
468 test_for_each
<rgb8_view_t
,rgb_fr_t
<uint8_t> >(num_trials
);
469 std::cout
<<std::endl
;
471 std::cout
<<"test for_each_pixel() on rgb8_planar_image_t"<<std::endl
;
472 test_for_each
<rgb8_planar_view_t
,rgb_fr_t
<uint8_t> >(num_trials
);
473 std::cout
<<std::endl
;
476 std::cout
<<"test copy_pixels() between rgb8_image_t and rgb8_image_t"<<std::endl
;
477 test_copy
<rgb8_view_t
,rgb8_view_t
>(num_trials
);
478 std::cout
<<std::endl
;
480 std::cout
<<"test copy_pixels() between rgb8_image_t and bgr8_image_t"<<std::endl
;
481 test_copy
<rgb8_view_t
,bgr8_view_t
>(num_trials
);
482 std::cout
<<std::endl
;
484 std::cout
<<"test copy_pixels() between rgb8_planar_image_t and rgb8_planar_image_t"<<std::endl
;
485 test_copy
<rgb8_planar_view_t
,rgb8_planar_view_t
>(num_trials
);
486 std::cout
<<std::endl
;
488 std::cout
<<"test copy_pixels() between rgb8_image_t and rgb8_planar_image_t"<<std::endl
;
489 test_copy
<rgb8_view_t
,rgb8_planar_view_t
>(num_trials
);
490 std::cout
<<std::endl
;
492 std::cout
<<"test copy_pixels() between rgb8_planar_image_t and rgb8_image_t"<<std::endl
;
493 test_copy
<rgb8_planar_view_t
,rgb8_view_t
>(num_trials
);
494 std::cout
<<std::endl
;
497 std::cout
<<"test transform_pixels() between rgb8_image_t and rgb8_image_t"<<std::endl
;
498 test_transform
<rgb8_view_t
,rgb8_view_t
,bgr_to_rgb_t
<uint8_t,pixel
<uint8_t,rgb_layout_t
> > >(num_trials
);
499 std::cout
<<std::endl
;
501 std::cout
<<"test transform_pixels() between rgb8_planar_image_t and rgb8_planar_image_t"<<std::endl
;
502 test_transform
<rgb8_planar_view_t
,rgb8_planar_view_t
,bgr_to_rgb_t
<uint8_t,planar_pixel_reference
<uint8_t,rgb_t
> > >(num_trials
);
503 std::cout
<<std::endl
;
505 std::cout
<<"test transform_pixels() between rgb8_image_t and rgb8_planar_image_t"<<std::endl
;
506 test_transform
<rgb8_view_t
,rgb8_planar_view_t
,bgr_to_rgb_t
<uint8_t,pixel
<uint8_t,rgb_layout_t
> > >(num_trials
);
507 std::cout
<<std::endl
;
509 std::cout
<<"test transform_pixels() between rgb8_planar_image_t and rgb8_image_t"<<std::endl
;
510 test_transform
<rgb8_planar_view_t
,rgb8_view_t
,bgr_to_rgb_t
<uint8_t,planar_pixel_reference
<uint8_t,rgb_t
> > >(num_trials
);
511 std::cout
<<std::endl
;
519 return ::boost::report_errors();