1 /*
2     Copyright 2005-2007 Adobe Systems Incorporated
3 
4     Use, modification and distribution are subject to the Boost Software License,
5     Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6     http://www.boost.org/LICENSE_1_0.txt).
7 
8     See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 /*************************************************************************************************/
11 
12 #ifndef GIL_DYNAMICIMAGE_ANY_IMAGE_HPP
13 #define GIL_DYNAMICIMAGE_ANY_IMAGE_HPP
14 
15 ////////////////////////////////////////////////////////////////////////////////////////
16 /// \file
17 /// \brief Support for run-time instantiated images and image views
18 /// \author Lubomir Bourdev and Hailin Jin \n
19 ///         Adobe Systems Incorporated
20 ///
21 ///
22 ////////////////////////////////////////////////////////////////////////////////////////
23 
24 #include "any_image_view.hpp"
25 #include "../../image.hpp"
26 
27 //#ifdef _MSC_VER
28 //#pragma warning(push)
29 //#pragma warning(disable : 4244)     // conversion from 'std::ptrdiff_t' to 'int', possible loss of data. even if we static-assert the two types are the same (on visual studio 8)
30 //#endif
31 
32 namespace boost { namespace gil {
33 
34 namespace detail {
35     template <typename T> struct get_view_t       { typedef typename T::view_t type; };
36     template <typename Images> struct images_get_views_t : public mpl::transform<Images, get_view_t<mpl::_1> > {};
37 
38     template <typename T> struct get_const_view_t { typedef typename T::const_view_t type; };
39     template <typename Images> struct images_get_const_views_t : public mpl::transform<Images, get_const_view_t<mpl::_1> > {};
40 
41     struct recreate_image_fnobj {
42         typedef void result_type;
43         const point2<std::ptrdiff_t>& _dimensions;
44         unsigned _alignment;
45 
recreate_image_fnobjboost::gil::detail::recreate_image_fnobj46         recreate_image_fnobj(const point2<std::ptrdiff_t>& dims, unsigned alignment) : _dimensions(dims), _alignment(alignment) {}
operator ()boost::gil::detail::recreate_image_fnobj47         template <typename Image> result_type operator()(Image& img) const { img.recreate(_dimensions,_alignment); }
48     };
49 
50     template <typename AnyView>  // Models AnyViewConcept
51     struct any_image_get_view {
52         typedef AnyView result_type;
operator ()boost::gil::detail::any_image_get_view53         template <typename Image> result_type operator()(      Image& img) const { return result_type(view(img)); }
54     };
55 
56     template <typename AnyConstView>  // Models AnyConstViewConcept
57     struct any_image_get_const_view {
58         typedef AnyConstView result_type;
operator ()boost::gil::detail::any_image_get_const_view59         template <typename Image> result_type operator()(const Image& img) const { return result_type(const_view(img)); }
60     };
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////////////
64 /// \ingroup ImageModel
65 /// \brief Represents a run-time specified image. Note it does NOT model ImageConcept
66 ///
67 /// Represents an image whose type (color space, layout, planar/interleaved organization, etc) can be specified at run time.
68 /// It is the runtime equivalent of \p image.
69 /// Some of the requirements of ImageConcept, such as the \p value_type typedef cannot be fulfilled, since the language does not allow runtime type specification.
70 /// Other requirements, such as access to the pixels, would be inefficient to provide. Thus \p any_image does not fully model ImageConcept.
71 /// In particular, its \p view and \p const_view methods return \p any_image_view, which does not fully model ImageViewConcept. See \p any_image_view for more.
72 ////////////////////////////////////////////////////////////////////////////////////////
73 template <typename ImageTypes>
74 class any_image : public variant<ImageTypes> {
75     typedef variant<ImageTypes> parent_t;
76 public:
77     typedef any_image_view<typename detail::images_get_const_views_t<ImageTypes>::type> const_view_t;
78     typedef any_image_view<typename detail::images_get_views_t<ImageTypes>::type>       view_t;
79     typedef std::ptrdiff_t x_coord_t;
80     typedef std::ptrdiff_t y_coord_t;
81     typedef point2<std::ptrdiff_t> point_t;
82 
any_image()83     any_image()                                                          : parent_t() {}
any_image(const T & obj)84     template <typename T> explicit any_image(const T& obj)               : parent_t(obj) {}
any_image(T & obj,bool do_swap)85     template <typename T> explicit any_image(T& obj, bool do_swap)       : parent_t(obj,do_swap) {}
any_image(const any_image & v)86     any_image(const any_image& v)                                        : parent_t((const parent_t&)v)    {}
87 
operator =(const T & obj)88     template <typename T> any_image& operator=(const T& obj)             { parent_t::operator=(obj); return *this; }
operator =(const any_image & v)89     any_image&                       operator=(const any_image& v)       { parent_t::operator=((const parent_t&)v); return *this;}
90 
recreate(const point_t & dims,unsigned alignment=1)91     void recreate(const point_t& dims, unsigned alignment=1)               { apply_operation(*this,detail::recreate_image_fnobj(dims,alignment)); }
recreate(x_coord_t width,y_coord_t height,unsigned alignment=1)92     void recreate(x_coord_t width, y_coord_t height, unsigned alignment=1) { recreate(point2<std::ptrdiff_t>(width,height),alignment); }
93 
num_channels() const94     std::size_t num_channels()  const { return apply_operation(*this, detail::any_type_get_num_channels()); }
dimensions() const95     point_t     dimensions()    const { return apply_operation(*this, detail::any_type_get_dimensions()); }
width() const96     x_coord_t   width()         const { return dimensions().x; }
height() const97     y_coord_t   height()        const { return dimensions().y; }
98 };
99 
100 ///@{
101 /// \name view, const_view
102 /// \brief Get an image view from a run-time instantiated image
103 
104 /// \ingroup ImageModel
105 
106 /// \brief Returns the non-constant-pixel view of any image. The returned view is any view.
107 template <typename Types>  GIL_FORCEINLINE // Models ImageVectorConcept
view(any_image<Types> & anyImage)108 typename any_image<Types>::view_t view(any_image<Types>& anyImage) {
109     return apply_operation(anyImage, detail::any_image_get_view<typename any_image<Types>::view_t>());
110 }
111 
112 /// \brief Returns the constant-pixel view of any image. The returned view is any view.
113 template <typename Types> GIL_FORCEINLINE // Models ImageVectorConcept
const_view(const any_image<Types> & anyImage)114 typename any_image<Types>::const_view_t const_view(const any_image<Types>& anyImage) {
115     return apply_operation(anyImage, detail::any_image_get_const_view<typename any_image<Types>::const_view_t>());
116 }
117 ///@}
118 
119 } }  // namespace boost::gil
120 
121 //#ifdef _MSC_VER
122 //#pragma warning(pop)
123 //#endif
124 
125 #endif
126