00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef GIL_IMAGE_VIEW_FACTORY_HPP
00010 #define GIL_IMAGE_VIEW_FACTORY_HPP
00011
00021
00023 #include <cassert>
00024 #include "gil_config.hpp"
00025 #include "image.hpp"
00026 #include "color_convert.hpp"
00027
00028 ADOBE_GIL_NAMESPACE_BEGIN
00029
00033
00035
00037 template <typename IT>
00038 inline typename type_from_x_iterator<IT>::view_t interleaved_view(int width, int height, IT pixels, std::ptrdiff_t rowsize_in_bytes) {
00039 return typename type_from_x_iterator<IT>::view_t(width, height, pixels, rowsize_in_bytes);
00040 }
00041
00043 template <typename IC>
00044 inline typename type_from_x_iterator<planar_ptr<IC,rgb_t> >::view_t planar_rgb_view(int width, int height, IC r, IC g, IC b, std::ptrdiff_t rowsize_in_bytes) {
00045 return typename type_from_x_iterator<planar_ptr<IC,rgb_t> >::view_t(width, height, planar_ptr<IC,rgb_t>(r,g,b), rowsize_in_bytes);
00046 }
00047
00049 template <typename IC>
00050 inline typename type_from_x_iterator<planar_ptr<IC,rgba_t> >::view_t planar_rgba_view(int width, int height, IC r, IC g, IC b, IC a, std::ptrdiff_t rowsize_in_bytes) {
00051 return typename type_from_x_iterator<planar_ptr<IC,rgba_t> >::view_t(width, height, planar_ptr<IC,rgba_t>(r,g,b,a), rowsize_in_bytes);
00052 }
00053
00055 template <typename IC>
00056 inline typename type_from_x_iterator<planar_ptr<IC,cmyk_t> >::view_t planar_cmyk_view(int width, int height, IC c, IC m, IC y, IC k, std::ptrdiff_t rowsize_in_bytes) {
00057 return typename type_from_x_iterator<planar_ptr<IC,cmyk_t> >::view_t(width, height, planar_ptr<IC,cmyk_t>(c,m,y,k), rowsize_in_bytes);
00058 }
00059
00061 template <typename IC>
00062 inline typename type_from_x_iterator<planar_ptr<IC,lab_t> >::view_t planar_lab_view(int width, int height, IC l, IC a, IC b, std::ptrdiff_t rowsize_in_bytes) {
00063 return typename type_from_x_iterator<planar_ptr<IC,lab_t> >::view_t(width, height, planar_ptr<IC,lab_t>(l,a,b), rowsize_in_bytes);
00064 }
00065
00067 template <typename IC>
00068 inline typename type_from_x_iterator<planar_ptr<IC,hsb_t> >::view_t planar_hsb_view(int width, int height, IC h, IC s, IC b, std::ptrdiff_t rowsize_in_bytes) {
00069 return typename type_from_x_iterator<planar_ptr<IC,hsb_t> >::view_t(width, height, planar_ptr<IC,hsb_t>(h,s,b), rowsize_in_bytes);
00070 }
00071
00073
00074 namespace detail {
00075 template <typename SRC_VIEW, typename DST_P, typename SRC_P>
00076 struct _color_convert_view_type {
00077 private:
00078 typedef dereference_iterator_adaptor<typename SRC_VIEW::x_iterator, color_convert_deref_fn<DST_P> > x_iterator;
00079 public:
00080 typedef typename type_from_x_iterator<x_iterator>::view_t type;
00081 };
00082
00083 template <typename SRC_VIEW, typename DST_P>
00084 struct _color_convert_view_type<SRC_VIEW,DST_P,DST_P> {
00085 typedef SRC_VIEW type;
00086 };
00087 }
00088
00092
00095 template <typename SRC_VIEW, typename DST_P>
00096 struct color_convert_view_type : public detail::_color_convert_view_type<SRC_VIEW,DST_P, typename SRC_VIEW::value_type> {
00097 GIL_CLASS_REQUIRE(SRC_VIEW, GIL, ImageViewConcept);
00098 GIL_CLASS_REQUIRE(DST_P, GIL, MutablePixelConcept);
00099 };
00100
00102
00104 template <typename DST_P, typename VIEW>
00105 inline typename color_convert_view_type<VIEW,DST_P>::type color_converted_view(const VIEW& src) {
00106 return typename color_convert_view_type<VIEW,DST_P>::type(src);
00107 }
00108
00110 template <typename VIEW>
00111 inline VIEW flipped_up_down_view(const VIEW& src) {
00112 return VIEW(src.dimensions(),src.row_begin(src.height()-1),-src.row_bytes());
00113 }
00114
00116 template <typename VIEW>
00117 inline typename VIEW::dynamic_step_t flipped_left_right_view(const VIEW& src) {
00118 return typename VIEW::dynamic_step_t(src.dimensions(),make_step_iterator(src.x_at(src.width()-1,0),-src.pix_bytestep()),src.row_bytes());
00119 }
00120
00122 template <typename VIEW>
00123 inline typename VIEW::dynamic_step_t transposed_view(const VIEW& src) {
00124 return typename VIEW::dynamic_step_t(src.height(),src.width(),make_step_iterator(src.x_at(0,0),src.row_bytes()),src.pix_bytestep());
00125 }
00126
00128 template <typename VIEW>
00129 inline typename VIEW::dynamic_step_t rotated90cw_view(const VIEW& src) {
00130 return typename VIEW::dynamic_step_t(src.height(),src.width(),make_step_iterator(src.x_at(0,src.height()-1),-src.row_bytes()),src.pix_bytestep());
00131 }
00132
00134 template <typename VIEW>
00135 inline typename VIEW::dynamic_step_t rotated90ccw_view(const VIEW& src) {
00136 return typename VIEW::dynamic_step_t(src.height(),src.width(),make_step_iterator(src.x_at(src.width()-1,0),src.row_bytes()),-src.pix_bytestep());
00137 }
00138
00140 template <typename VIEW>
00141 inline typename VIEW::dynamic_step_t rotated180_view(const VIEW& src) {
00142 return typename VIEW::dynamic_step_t(src.dimensions(),make_step_iterator(src.x_at(src.width()-1,src.height()-1),-src.pix_bytestep()),-src.row_bytes());
00143 }
00144
00146 template <typename VIEW>
00147 inline VIEW subimage_view(const VIEW& src, const typename VIEW::point_t& topleft, const typename VIEW::point_t& dimensions) {
00148 return VIEW(dimensions,src.x_at(topleft),src.row_bytes());
00149 }
00150 template <typename VIEW>
00151 inline VIEW subimage_view(const VIEW& src, int xMin, int yMin, int width, int height) {
00152 return VIEW(width,height,src.x_at(xMin,yMin),src.row_bytes());
00153 }
00154
00156 template <typename VIEW>
00157 inline typename VIEW::dynamic_step_t subsampled_view(const VIEW& src, typename VIEW::coord_t xStep, typename VIEW::coord_t yStep) {
00158 assert(xStep>0 && yStep>0);
00159 return typename VIEW::dynamic_step_t((src.width()+(xStep-1))/xStep,(src.height()+(yStep-1))/yStep,
00160 make_step_iterator(src.x_at(0,0),xStep*src.pix_bytestep()),src.row_bytes()*yStep);
00161 }
00162
00164 template <typename VIEW>
00165 inline typename VIEW::dynamic_step_t subsampled_view(const VIEW& src, const typename VIEW::point_t& step) {
00166 return subsampled_view(src,step.x,step.y);
00167 }
00168
00171
00175 template <typename VIEW>
00176 struct nth_channel_view_type {
00177 private:
00178 GIL_CLASS_REQUIRE(VIEW, GIL, ImageViewConcept);
00179 typedef typename VIEW::x_iterator src_x_iterator;
00180
00181
00182
00183 BOOST_STATIC_CONSTANT(bool, adjacent=
00184 !iterator_is_step<src_x_iterator>::value &&
00185 (pixel_iterator_traits<src_x_iterator>::is_planar ||
00186 pixel_iterator_traits< src_x_iterator >::color_space_t::num_channels==1));
00187 public:
00188 typedef typename view_type<typename VIEW::channel_t, gray_t, false, !adjacent, view_is_mutable<VIEW>::value>::type type;
00189 };
00190
00191
00192 namespace detail {
00193
00194
00195 template <typename VIEW, bool CHANNELS_TOGETHER>
00196 struct __nth_channel_view {
00197 static typename nth_channel_view_type<VIEW>::type apply(const VIEW& src, int n) {
00198 typedef typename nth_channel_view_type<VIEW>::type::x_iterator IT;
00199 typedef typename pixel_iterator_traits<IT>::base_t IT_BASE;
00200 IT sit(IT_BASE(&(src(0,0)[n])),src.pix_bytestep());
00201 return typename nth_channel_view_type<VIEW>::type(src.dimensions(),sit, src.row_bytes());
00202 }
00203 };
00204
00205
00206 template <typename VIEW>
00207 struct __nth_channel_view<VIEW,true> {
00208 static typename nth_channel_view_type<VIEW>::type apply(const VIEW& src, int n) {
00209 typedef typename nth_channel_view_type<VIEW>::type::x_iterator IT;
00210 return interleaved_view(src.width(),src.height(),(IT)&(src(0,0)[n]), src.row_bytes());
00211 }
00212 };
00213 }
00214
00216 template <typename VIEW>
00217 typename nth_channel_view_type<VIEW>::type nth_channel_view(const VIEW& src, int n) {
00218
00219 BOOST_STATIC_ASSERT((pixel_iterator_traits<typename VIEW::x_iterator>::pixel_data_is_real));
00220 return detail::__nth_channel_view<VIEW,!view_is_step<typename nth_channel_view_type<VIEW>::type >::value>::apply(src,n);
00221 }
00223
00224 ADOBE_GIL_NAMESPACE_END
00225
00226 #endif