14 #ifndef GIL_SAMPLER_HPP 15 #define GIL_SAMPLER_HPP 17 #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp> 30 namespace boost {
namespace gil {
51 template <
typename DstP,
typename SrcView,
typename F>
53 point2<int> center(static_cast<int>(iround(p).x), static_cast<int>(iround(p).y));
54 if (center.x>=0 && center.y>=0 && center.x<src.width() && center.y<src.height()) {
55 result=src(center.x,center.y);
61 struct cast_channel_fn {
62 template <
typename SrcChannel,
typename DstChannel>
63 void operator()(
const SrcChannel& src, DstChannel& dst) {
64 typedef typename channel_traits<DstChannel>::value_type dst_value_t;
65 dst = dst_value_t(src);
69 template <
typename SrcPixel,
typename DstPixel>
70 void cast_pixel(
const SrcPixel& src, DstPixel& dst) {
71 static_for_each(src,dst,cast_channel_fn());
76 template <
typename Weight>
77 struct add_dst_mul_src_channel {
79 add_dst_mul_src_channel(Weight w) : _w(w) {}
81 template <
typename SrcChannel,
typename DstChannel>
82 void operator()(
const SrcChannel& src, DstChannel& dst)
const {
83 dst += DstChannel(src*_w);
88 template <
typename SrcP,
typename Weight,
typename DstP>
89 struct add_dst_mul_src {
90 void operator()(
const SrcP& src, Weight weight, DstP& dst)
const {
91 static_for_each(src,dst, add_dst_mul_src_channel<Weight>(weight));
106 template <
typename DstP,
typename SrcView,
typename F>
107 bool sample(
bilinear_sampler,
const SrcView& src,
const point2<F>& p, DstP& result) {
108 typedef typename SrcView::value_type SrcP;
109 point2<std::ptrdiff_t> p0(ifloor(p));
110 point2<F> frac(p.x-p0.x, p.y-p0.y);
111 if (p0.x < 0 || p0.y < 0 || p0.x>=src.width() || p0.y>=src.height())
return false;
113 pixel<F,devicen_layout_t<num_channels<SrcView>::value> > mp(0);
114 typename SrcView::xy_locator loc=src.xy_at(p0.x,p0.y);
116 if (p0.x+1<src.width()) {
117 if (p0.y+1<src.height()) {
119 detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.x)*(1-frac.y),mp);
120 detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.x *(1-frac.y),mp);
122 detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.x)* frac.y ,mp);
123 detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.x * frac.y ,mp);
126 detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.x),mp);
127 detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(loc.x()[1], frac.x ,mp);
130 if (p0.y+1<src.height()) {
132 detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, (1-frac.y),mp);
134 detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc, frac.y ,mp);
137 detail::add_dst_mul_src<SrcP,F,pixel<F,devicen_layout_t<num_channels<SrcView>::value> > >()(*loc,1,mp);
143 cast_pixel(mp,src_result);
145 color_convert(src_result, result);
A sampler that sets the destination pixel as the bilinear interpolation of the four closest pixels fr...
Structures for pixel-wise numeric operations /.
A sampler that sets the destination pixel to the closest one in the source. If outside the bounds...