image.cpp
来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 591 行 · 第 1/2 页
CPP
591 行
/* Copyright 2005-2007 Adobe Systems Incorporated Use, modification and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). See http://opensource.adobe.com/gil for most recent version including documentation.*/// image_test.cpp : //#ifdef _MSC_VER//#pragma warning(disable : 4244) // conversion from 'gil::image<V,Alloc>::coord_t' to 'int', possible loss of data (visual studio compiler doesn't realize that the two types are the same)#pragma warning(disable : 4503) // decorated name length exceeded, name was truncated#endif#include <string>#include <vector>#include <ios>#include <iostream>#include <fstream>#include <map>#include <boost/lambda/lambda.hpp>#include <boost/lambda/bind.hpp>#include <boost/mpl/vector.hpp>#include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>#include <boost/crc.hpp>using namespace boost::gil;using namespace std;using namespace boost;extern rgb8c_planar_view_t sample_view;void error_if(bool condition);// When BOOST_GIL_GENERATE_REFERENCE_DATA is defined, the reference data is generated and saved.// When it is undefined, regression tests are checked against it//#define BOOST_GIL_GENERATE_REFERENCE_DATA////////////////////////////////////////////////////////// Some algorithms to use in testing///////////////////////////////////////////////////////template <typename GrayView, typename R>void gray_image_hist(const GrayView& img_view, R& hist) {// for_each_pixel(img_view,++lambda::var(hist)[lambda::_1]); for (typename GrayView::iterator it=img_view.begin(); it!=img_view.end(); ++it) ++hist[*it];}template <typename V, typename R>void get_hist(const V& img_view, R& hist) { gray_image_hist(color_converted_view<gray8_pixel_t>(img_view), hist);}// testing custom color conversiontemplate <typename C1, typename C2>struct my_color_converter_impl : public default_color_converter_impl<C1,C2> {};template <typename C1>struct my_color_converter_impl<C1,gray_t> { template <typename P1, typename P2> void operator()(const P1& src, P2& dst) const { default_color_converter_impl<C1,gray_t>()(src,dst); get_color(dst,gray_color_t())=channel_invert(get_color(dst,gray_color_t())); }};struct my_color_converter { template <typename SrcP,typename DstP> void operator()(const SrcP& src,DstP& dst) const { typedef typename color_space_type<SrcP>::type src_cs_t; typedef typename color_space_type<DstP>::type dst_cs_t; my_color_converter_impl<src_cs_t,dst_cs_t>()(src,dst); }};// Models a Unary Functiontemplate <typename P> // Models PixelValueConceptstruct mandelbrot_fn { typedef point2<std::ptrdiff_t> point_t; typedef mandelbrot_fn const_t; typedef P value_type; typedef value_type reference; typedef value_type const_reference; typedef point_t argument_type; typedef reference result_type; BOOST_STATIC_CONSTANT(bool, is_mutable=false); value_type _in_color,_out_color; point_t _img_size; static const int MAX_ITER=100; // max number of iterations mandelbrot_fn() {} mandelbrot_fn(const point_t& sz, const value_type& in_color, const value_type& out_color) : _in_color(in_color), _out_color(out_color), _img_size(sz) {} result_type operator()(const point_t& p) const { // normalize the coords to (-2..1, -1.5..1.5) // (actually make y -1.0..2 so it is asymmetric, so we can verify some view factory methods) double t=get_num_iter(point2<double>(p.x/(double)_img_size.x*3-2, p.y/(double)_img_size.y*3-1.0f));//1.5f)); t=pow(t,0.2); value_type ret; for (int k=0; k<num_channels<P>::value; ++k) ret[k]=(typename channel_type<value_type>::type)(_in_color[k]*t + _out_color[k]*(1-t)); return ret; }private: double get_num_iter(const point2<double>& p) const { point2<double> Z(0,0); for (int i=0; i<MAX_ITER; ++i) { Z = point2<double>(Z.x*Z.x - Z.y*Z.y + p.x, 2*Z.x*Z.y + p.y); if (Z.x*Z.x + Z.y*Z.y > 4) return i/(double)MAX_ITER; } return 0; }};template <typename T>void x_gradient(const T& src, const gray8s_view_t& dst) { for (int y=0; y<src.height(); ++y) { typename T::x_iterator src_it = src.row_begin(y); gray8s_view_t::x_iterator dst_it = dst.row_begin(y); for (int x=1; x<src.width()-1; ++x) dst_it[x] = (src_it[x+1] - src_it[x-1]) / 2; }}// A quick test whether a view is homogeneoustemplate <typename Pixel>struct pixel_is_homogeneous : public mpl::true_ {};template <typename P, typename C, typename L>struct pixel_is_homogeneous<packed_pixel<P,C,L> > : public mpl::false_ {};template <typename View>struct view_is_homogeneous : public pixel_is_homogeneous<typename View::value_type> {};////////////////////////////////////////////////////////// Tests image view transformations and algorithms///////////////////////////////////////////////////////class image_test {public: virtual void initialize() {} virtual void finalize() {} virtual ~image_test() {} void run();protected: virtual void check_view_impl(const rgb8c_view_t& view, const string& name)=0; template <typename View> void check_view(const View& img_view, const string& name) { rgb8_image_t rgb_img(img_view.dimensions()); copy_and_convert_pixels(img_view,view(rgb_img)); check_view_impl(const_view(rgb_img), name); }private: template <typename Img> void basic_test(const string& prefix); template <typename View> void view_transformations_test(const View& img_view, const string& prefix); template <typename View> void homogeneous_view_transformations_test(const View& img_view, const string& prefix, mpl::true_); template <typename View> void homogeneous_view_transformations_test(const View& img_view, const string& prefix, mpl::false_) {} template <typename View> void histogram_test(const View& img_view, const string& prefix); void virtual_view_test(); void packed_image_test(); void dynamic_image_test(); template <typename Img> void image_all_test(const string& prefix);};// testing image iterators, clone, fill, locators, color converttemplate <typename Img>void image_test::basic_test(const string& prefix) { typedef typename Img::view_t View; // make a 20x20 image Img img(typename View::point_t(20,20)); const View& img_view=view(img); // fill it with red rgb8_pixel_t red8(255,0,0), green8(0,255,0), blue8(0,0,255), white8(255,255,255); typename View::value_type red,green,blue,white; color_convert(red8,red); default_color_converter()(red8,red); red=color_convert_deref_fn<rgb8_ref_t,typename Img::view_t::value_type>()(red8); color_convert(green8,green); color_convert(blue8,blue); color_convert(white8,white); fill(img_view.begin(),img_view.end(),red); color_convert(red8,img_view[0]); // pointer to first pixel of second row typename View::reference rt=img_view.at(0,0)[img_view.width()]; typename View::x_iterator ptr=&rt; typename View::reference rt2=*(img_view.at(0,0)+img_view.width()); typename View::x_iterator ptr2=&rt2; error_if(ptr!=ptr2); error_if(img_view.x_at(0,0)+10!=10+img_view.x_at(0,0)); // draw a blue line along the diagonal typename View::xy_locator loc=img_view.xy_at(0,img_view.height()-1); for (int y=0; y<img_view.height(); ++y) { *loc=blue; ++loc.x(); loc.y()--; } // draw a green dotted line along the main diagonal with step of 3 loc=img_view.xy_at(img_view.width()-1,img_view.height()-1); while (loc.x()>=img_view.x_at(0,0)) { *loc=green; loc-=typename View::point_t(3,3); } // Clone and make every red pixel white Img imgWhite(img); for (typename View::iterator it=view(imgWhite).end(); (it-1)!=view(imgWhite).begin(); --it) { if (*(it-1)==red) *(it-1)=white; } check_view(img_view,prefix+"red_x"); check_view(view(imgWhite),prefix+"white_x");}template <typename View>void image_test::histogram_test(const View& img_view, const string& prefix) {// vector<int> histogram(255,0);// get_hist(cropped,histogram.begin()); unsigned char histogram[256]; fill(histogram,histogram+256,0); get_hist(img_view,histogram); gray8c_view_t hist_view=interleaved_view(256,1,(const gray8_pixel_t*)histogram,256); check_view(hist_view,prefix+"histogram");}template <typename View>void image_test::view_transformations_test(const View& img_view, const string& prefix) { check_view(img_view,prefix+"original"); check_view(subimage_view(img_view, iround(img_view.dimensions()/4), iround(img_view.dimensions()/2)),prefix+"cropped"); check_view(color_converted_view<gray8_pixel_t>(img_view),prefix+"gray8"); check_view(color_converted_view<gray8_pixel_t>(img_view,my_color_converter()),prefix+"my_gray8"); check_view(transposed_view(img_view),prefix+"transpose"); check_view(rotated180_view(img_view),prefix+"rot180"); check_view(rotated90cw_view(img_view),prefix+"90cw"); check_view(rotated90ccw_view(img_view),prefix+"90ccw"); check_view(flipped_up_down_view(img_view),prefix+"flipped_ud"); check_view(flipped_left_right_view(img_view),prefix+"flipped_lr"); check_view(subsampled_view(img_view,typename View::point_t(2,1)),prefix+"subsampled"); check_view(kth_channel_view<0>(img_view),prefix+"0th_k_channel"); homogeneous_view_transformations_test(img_view, prefix, view_is_homogeneous<View>());}template <typename View>void image_test::homogeneous_view_transformations_test(const View& img_view, const string& prefix, mpl::true_) { check_view(nth_channel_view(img_view,0),prefix+"0th_n_channel");}void image_test::virtual_view_test() { typedef mandelbrot_fn<rgb8_pixel_t> deref_t; typedef deref_t::point_t point_t; typedef virtual_2d_locator<deref_t,false> locator_t; typedef image_view<locator_t> my_virt_view_t; boost::function_requires<PixelLocatorConcept<locator_t> >(); gil_function_requires<StepIteratorConcept<locator_t::x_iterator> >(); point_t dims(200,200); my_virt_view_t mandel(dims, locator_t(point_t(0,0), point_t(1,1), deref_t(dims, rgb8_pixel_t(255,0,255), rgb8_pixel_t(0,255,0)))); gray8s_image_t img(dims); fill_pixels(view(img),0); // our x_gradient algorithm doesn't change the first & last column, so make sure they are 0 x_gradient(color_converted_view<gray8_pixel_t>(mandel), view(img)); check_view(color_converted_view<gray8_pixel_t>(const_view(img)), "mandelLuminosityGradient"); view_transformations_test(mandel,"virtual_"); histogram_test(mandel,"virtual_");}// Test alignment and packed imagesvoid image_test::packed_image_test() {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?