File indexing completed on 2025-01-18 09:37:00
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_GIL_EXTENSION_NUMERIC_RESAMPLE_HPP
0009 #define BOOST_GIL_EXTENSION_NUMERIC_RESAMPLE_HPP
0010
0011 #include <boost/gil/extension/numeric/affine.hpp>
0012 #include <boost/gil/extension/dynamic_image/dynamic_image_all.hpp>
0013
0014 #include <algorithm>
0015 #include <functional>
0016
0017 namespace boost { namespace gil {
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 template <typename MapFn> struct mapping_traits {};
0029
0030
0031
0032
0033
0034 template <typename Sampler,
0035 typename SrcView,
0036 typename DstView,
0037 typename MapFn>
0038 void resample_pixels(const SrcView& src_view, const DstView& dst_view, const MapFn& dst_to_src, Sampler sampler=Sampler())
0039 {
0040 typename DstView::point_t dst_dims=dst_view.dimensions();
0041 typename DstView::point_t dst_p;
0042
0043 for (dst_p.y=0; dst_p.y<dst_dims.y; ++dst_p.y) {
0044 typename DstView::x_iterator xit = dst_view.row_begin(dst_p.y);
0045 for (dst_p.x=0; dst_p.x<dst_dims.x; ++dst_p.x) {
0046 sample(sampler, src_view, transform(dst_to_src, dst_p), xit[dst_p.x]);
0047 }
0048 }
0049 }
0050
0051
0052
0053
0054
0055
0056
0057 namespace detail {
0058 template <typename Sampler, typename MapFn>
0059 struct resample_pixels_fn : public binary_operation_obj<resample_pixels_fn<Sampler,MapFn> > {
0060 MapFn _dst_to_src;
0061 Sampler _sampler;
0062 resample_pixels_fn(const MapFn& dst_to_src, const Sampler& sampler) : _dst_to_src(dst_to_src), _sampler(sampler) {}
0063
0064 template <typename SrcView, typename DstView> BOOST_FORCEINLINE void apply_compatible(const SrcView& src, const DstView& dst) const {
0065 resample_pixels(src, dst, _dst_to_src, _sampler);
0066 }
0067 };
0068 }
0069
0070
0071
0072
0073 template <typename Sampler, typename ...Types1, typename V2, typename MapFn>
0074 void resample_pixels(const any_image_view<Types1...>& src, const V2& dst, const MapFn& dst_to_src, Sampler sampler=Sampler())
0075 {
0076 variant2::visit(std::bind(
0077 detail::resample_pixels_fn<Sampler, MapFn>(dst_to_src, sampler),
0078 std::placeholders::_1,
0079 dst), src);
0080 }
0081
0082
0083
0084
0085 template <typename Sampler, typename V1, typename ...Types2, typename MapFn>
0086 void resample_pixels(const V1& src, const any_image_view<Types2...>& dst, const MapFn& dst_to_src, Sampler sampler=Sampler())
0087 {
0088 using namespace std::placeholders;
0089 variant2::visit(std::bind(
0090 detail::resample_pixels_fn<Sampler, MapFn>(dst_to_src, sampler),
0091 src,
0092 std::placeholders::_1), dst);
0093 }
0094
0095
0096
0097
0098 template <typename Sampler, typename ...SrcTypes, typename ...DstTypes, typename MapFn>
0099 void resample_pixels(const any_image_view<SrcTypes...>& src, const any_image_view<DstTypes...>& dst, const MapFn& dst_to_src, Sampler sampler=Sampler()) {
0100 variant2::visit(detail::resample_pixels_fn<Sampler,MapFn>(dst_to_src,sampler), src, dst);
0101 }
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 template <typename Sampler, typename SrcMetaView, typename DstMetaView>
0113 void resample_subimage(const SrcMetaView& src, const DstMetaView& dst,
0114 double src_min_x, double src_min_y,
0115 double src_max_x, double src_max_y,
0116 double angle, const Sampler& sampler=Sampler()) {
0117 double src_width = std::max<double>(src_max_x - src_min_x - 1,1);
0118 double src_height = std::max<double>(src_max_y - src_min_y - 1,1);
0119 double dst_width = std::max<double>((double)(dst.width()-1),1);
0120 double dst_height = std::max<double>((double)(dst.height()-1),1);
0121
0122 matrix3x2<double> mat =
0123 matrix3x2<double>::get_translate(-dst_width/2.0, -dst_height/2.0) *
0124 matrix3x2<double>::get_scale(src_width / dst_width, src_height / dst_height)*
0125 matrix3x2<double>::get_rotate(-angle)*
0126 matrix3x2<double>::get_translate(src_min_x + src_width/2.0, src_min_y + src_height/2.0);
0127 resample_pixels(src,dst,mat,sampler);
0128 }
0129
0130
0131
0132
0133
0134
0135
0136 template <typename Sampler, typename SrcMetaView, typename DstMetaView>
0137 void resize_view(const SrcMetaView& src, const DstMetaView& dst, const Sampler& sampler=Sampler()) {
0138 resample_subimage(src,dst,0.0,0.0,(double)src.width(),(double)src.height(),0.0,sampler);
0139 }
0140
0141 } }
0142
0143 #endif