gil_concept.hpp 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef GIL_CONCEPT_H
  10. #define GIL_CONCEPT_H
  11. ////////////////////////////////////////////////////////////////////////////////////////
  12. /// \file
  13. /// \brief Concept check classes for GIL concepts
  14. /// \author Lubomir Bourdev and Hailin Jin \n
  15. /// Adobe Systems Incorporated
  16. /// \date 2005-2007 \n Last updated on February 12, 2007
  17. ///
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. #include <functional>
  20. #include "gil_config.hpp"
  21. #include <boost/type_traits.hpp>
  22. #include <boost/utility/enable_if.hpp>
  23. #include <boost/concept_check.hpp>
  24. #include <boost/iterator/iterator_concepts.hpp>
  25. #include <boost/mpl/and.hpp>
  26. #include <boost/mpl/size.hpp>
  27. namespace boost { namespace gil {
  28. #if defined(__GNUC__) && (__GNUC__ >= 4)
  29. #pragma GCC diagnostic push
  30. #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
  31. #endif
  32. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  33. #pragma warning(push)
  34. #pragma warning(disable:4510) //default constructor could not be generated
  35. #pragma warning(disable:4512) //assignment operator could not be generated
  36. #pragma warning(disable:4610) //can never be instantiated - user defined constructor required
  37. #endif
  38. template <typename T> struct channel_traits;
  39. template <typename P> struct is_pixel;
  40. template <typename dstT, typename srcT>
  41. typename channel_traits<dstT>::value_type channel_convert(const srcT& val);
  42. template <typename T> class point2;
  43. template <std::size_t K, typename T> const T& axis_value(const point2<T>& p);
  44. template <std::size_t K, typename T> T& axis_value( point2<T>& p);
  45. template <typename ColorBase, int K> struct kth_element_type;
  46. template <typename ColorBase, int K> struct kth_element_reference_type;
  47. template <typename ColorBase, int K> struct kth_element_const_reference_type;
  48. template <typename ColorBase, int K> struct kth_semantic_element_reference_type;
  49. template <typename ColorBase, int K> struct kth_semantic_element_const_reference_type;
  50. template <typename ColorBase> struct size;
  51. template <typename ColorBase> struct element_type;
  52. template <typename T> struct channel_type;
  53. template <typename T> struct color_space_type;
  54. template <typename T> struct channel_mapping_type;
  55. template <typename T> struct is_planar;
  56. template <typename T> struct num_channels;
  57. template <typename It> struct const_iterator_type;
  58. template <typename It> struct iterator_is_mutable;
  59. template <typename It> struct is_iterator_adaptor;
  60. template <typename It, typename NewBaseIt> struct iterator_adaptor_rebind;
  61. template <typename It> struct iterator_adaptor_get_base;
  62. // forward-declare at_c
  63. namespace detail { template <typename Element, typename Layout, int K> struct homogeneous_color_base; }
  64. template <int K, typename E, typename L, int N>
  65. typename add_reference<E>::type at_c( detail::homogeneous_color_base<E,L,N>& p);
  66. template <int K, typename E, typename L, int N>
  67. typename add_reference<typename add_const<E>::type>::type at_c(const detail::homogeneous_color_base<E,L,N>& p);
  68. template <typename P, typename C, typename L> struct packed_pixel;
  69. template <int K, typename P, typename C, typename L>
  70. typename kth_element_reference_type<packed_pixel<P,C,L>, K>::type
  71. at_c(packed_pixel<P,C,L>& p);
  72. template <int K, typename P, typename C, typename L>
  73. typename kth_element_const_reference_type<packed_pixel<P,C,L>,K>::type
  74. at_c(const packed_pixel<P,C,L>& p);
  75. template <typename B, typename C, typename L, bool M> struct bit_aligned_pixel_reference;
  76. template <int K, typename B, typename C, typename L, bool M> inline
  77. typename kth_element_reference_type<bit_aligned_pixel_reference<B,C,L,M>, K>::type
  78. at_c(const bit_aligned_pixel_reference<B,C,L,M>& p);
  79. // Forward-declare semantic_at_c
  80. template <int K, typename ColorBase>
  81. typename disable_if<is_const<ColorBase>,typename kth_semantic_element_reference_type<ColorBase,K>::type>::type semantic_at_c(ColorBase& p);
  82. template <int K, typename ColorBase>
  83. typename kth_semantic_element_const_reference_type<ColorBase,K>::type semantic_at_c(const ColorBase& p);
  84. template <typename T> struct dynamic_x_step_type;
  85. template <typename T> struct dynamic_y_step_type;
  86. template <typename T> struct transposed_type;
  87. namespace detail {
  88. template <typename T>
  89. void initialize_it(T&) {}
  90. } // namespace detail
  91. template <typename T>
  92. struct remove_const_and_reference : public remove_const<typename remove_reference<T>::type> {};
  93. #ifdef BOOST_GIL_USE_CONCEPT_CHECK
  94. #define GIL_CLASS_REQUIRE(type_var, ns, concept) BOOST_CLASS_REQUIRE(type_var, ns, concept);
  95. template <typename C> void gil_function_requires() { function_requires<C>(); }
  96. #else
  97. #define GIL_CLASS_REQUIRE(T,NS,C)
  98. template <typename C> void gil_function_requires() {}
  99. #endif
  100. /// \ingroup BasicConcepts
  101. /**
  102. \code
  103. auto concept DefaultConstructible<typename T> {
  104. T::T();
  105. };
  106. \endcode
  107. */
  108. template <typename T>
  109. struct DefaultConstructible {
  110. void constraints() {
  111. function_requires<boost::DefaultConstructibleConcept<T> >();
  112. }
  113. };
  114. /// \ingroup BasicConcepts
  115. /**
  116. \codeauto concept CopyConstructible<typename T> {
  117. T::T(T);
  118. T::~T();
  119. };
  120. \endcode
  121. */
  122. template <typename T>
  123. struct CopyConstructible {
  124. void constraints() {
  125. function_requires<boost::CopyConstructibleConcept<T> >();
  126. }
  127. };
  128. /// \ingroup BasicConcepts
  129. /**
  130. \code
  131. auto concept Assignable<typename T, typename U = T> {
  132. typename result_type;
  133. result_type operator=(T&, U);
  134. };
  135. \endcode
  136. */
  137. template <typename T>
  138. struct Assignable {
  139. void constraints() {
  140. function_requires<boost::AssignableConcept<T> >();
  141. }
  142. };
  143. /// \ingroup BasicConcepts
  144. /**
  145. \code
  146. auto concept EqualityComparable<typename T, typename U = T> {
  147. bool operator==(T x, T y);
  148. bool operator!=(T x, T y) { return !(x==y); }
  149. };
  150. \endcode
  151. */
  152. template <typename T>
  153. struct EqualityComparable {
  154. void constraints() {
  155. function_requires<boost::EqualityComparableConcept<T> >();
  156. }
  157. };
  158. /// \ingroup BasicConcepts
  159. /**
  160. \code
  161. concept SameType<typename T, typename U>;// unspecified
  162. \endcode
  163. */
  164. template <typename T, typename U>
  165. struct SameType {
  166. void constraints() {
  167. BOOST_STATIC_ASSERT((boost::is_same<T,U>::value_core));
  168. }
  169. };
  170. /// \ingroup BasicConcepts
  171. /**
  172. \code
  173. auto concept Swappable<typename T> {
  174. void swap(T&,T&);
  175. };
  176. \endcode
  177. */
  178. template <typename T>
  179. struct Swappable {
  180. void constraints() {
  181. using std::swap;
  182. swap(x,y);
  183. }
  184. T x,y;
  185. };
  186. /// \ingroup BasicConcepts
  187. /**
  188. \code
  189. auto concept Regular<typename T> : DefaultConstructible<T>, CopyConstructible<T>, EqualityComparable<T>,
  190. Assignable<T>, Swappable<T> {};
  191. \endcode
  192. */
  193. template <typename T>
  194. struct Regular {
  195. void constraints() {
  196. gil_function_requires< boost::DefaultConstructibleConcept<T> >();
  197. gil_function_requires< boost::CopyConstructibleConcept<T> >();
  198. gil_function_requires< boost::EqualityComparableConcept<T> >(); // ==, !=
  199. gil_function_requires< boost::AssignableConcept<T> >();
  200. gil_function_requires< Swappable<T> >();
  201. }
  202. };
  203. /// \ingroup BasicConcepts
  204. /**
  205. \code
  206. auto concept Metafunction<typename T> {
  207. typename type;
  208. };
  209. \endcode
  210. */
  211. template <typename T>
  212. struct Metafunction {
  213. void constraints() {
  214. typedef typename T::type type;
  215. }
  216. };
  217. ////////////////////////////////////////////////////////////////////////////////////////
  218. //
  219. // POINT CONCEPTS
  220. //
  221. ////////////////////////////////////////////////////////////////////////////////////////
  222. /// \brief N-dimensional point concept
  223. /// \ingroup PointConcept
  224. /**
  225. \code
  226. concept PointNDConcept<typename T> : Regular<T> {
  227. // the type of a coordinate along each axis
  228. template <size_t K> struct axis; where Metafunction<axis>;
  229. const size_t num_dimensions;
  230. // accessor/modifier of the value of each axis.
  231. template <size_t K> const typename axis<K>::type& T::axis_value() const;
  232. template <size_t K> typename axis<K>::type& T::axis_value();
  233. };
  234. \endcode
  235. */
  236. template <typename P>
  237. struct PointNDConcept {
  238. void constraints() {
  239. gil_function_requires< Regular<P> >();
  240. typedef typename P::value_type value_type;
  241. static const std::size_t N=P::num_dimensions; ignore_unused_variable_warning(N);
  242. typedef typename P::template axis<0>::coord_t FT;
  243. typedef typename P::template axis<N-1>::coord_t LT;
  244. FT ft=gil::axis_value<0>(point);
  245. axis_value<0>(point)=ft;
  246. LT lt=axis_value<N-1>(point);
  247. axis_value<N-1>(point)=lt;
  248. // value_type v=point[0]; ignore_unused_variable_warning(v);
  249. }
  250. P point;
  251. };
  252. /// \brief 2-dimensional point concept
  253. /// \ingroup PointConcept
  254. /**
  255. \code
  256. concept Point2DConcept<typename T> : PointNDConcept<T> {
  257. where num_dimensions == 2;
  258. where SameType<axis<0>::type, axis<1>::type>;
  259. typename value_type = axis<0>::type;
  260. const value_type& operator[](const T&, size_t i);
  261. value_type& operator[]( T&, size_t i);
  262. value_type x,y;
  263. };
  264. \endcode
  265. */
  266. template <typename P>
  267. struct Point2DConcept {
  268. void constraints() {
  269. gil_function_requires< PointNDConcept<P> >();
  270. BOOST_STATIC_ASSERT(P::num_dimensions == 2);
  271. point.x=point.y;
  272. point[0]=point[1];
  273. }
  274. P point;
  275. };
  276. ////////////////////////////////////////////////////////////////////////////////////////
  277. //
  278. // ITERATOR MUTABILITY CONCEPTS
  279. //
  280. // Taken from boost's concept_check.hpp. Isolating mutability to result in faster compile time
  281. //
  282. ////////////////////////////////////////////////////////////////////////////////////////
  283. namespace detail {
  284. template <class TT> // Preconditions: TT Models boost_concepts::ForwardTraversalConcept
  285. struct ForwardIteratorIsMutableConcept {
  286. void constraints() {
  287. *i++ = *i; // require postincrement and assignment
  288. }
  289. TT i;
  290. };
  291. template <class TT> // Preconditions: TT Models boost::BidirectionalIteratorConcept
  292. struct BidirectionalIteratorIsMutableConcept {
  293. void constraints() {
  294. gil_function_requires< ForwardIteratorIsMutableConcept<TT> >();
  295. *i-- = *i; // require postdecrement and assignment
  296. }
  297. TT i;
  298. };
  299. template <class TT> // Preconditions: TT Models boost_concepts::RandomAccessTraversalConcept
  300. struct RandomAccessIteratorIsMutableConcept {
  301. void constraints() {
  302. gil_function_requires< BidirectionalIteratorIsMutableConcept<TT> >();
  303. typename std::iterator_traits<TT>::difference_type n=0; ignore_unused_variable_warning(n);
  304. i[n] = *i; // require element access and assignment
  305. }
  306. TT i;
  307. };
  308. } // namespace detail
  309. ////////////////////////////////////////////////////////////////////////////////////////
  310. //
  311. // COLOR SPACE CONCEPTS
  312. //
  313. ////////////////////////////////////////////////////////////////////////////////////////
  314. /// \brief Color space type concept
  315. /// \ingroup ColorSpaceAndLayoutConcept
  316. /**
  317. \code
  318. concept ColorSpaceConcept<MPLRandomAccessSequence Cs> {
  319. // An MPL Random Access Sequence, whose elements are color tags
  320. };
  321. \endcode
  322. */
  323. template <typename Cs>
  324. struct ColorSpaceConcept {
  325. void constraints() {
  326. // An MPL Random Access Sequence, whose elements are color tags
  327. }
  328. };
  329. template <typename ColorSpace1, typename ColorSpace2> // Models ColorSpaceConcept
  330. struct color_spaces_are_compatible : public is_same<ColorSpace1,ColorSpace2> {};
  331. /// \brief Two color spaces are compatible if they are the same
  332. /// \ingroup ColorSpaceAndLayoutConcept
  333. /**
  334. \code
  335. concept ColorSpacesCompatibleConcept<ColorSpaceConcept Cs1, ColorSpaceConcept Cs2> {
  336. where SameType<Cs1,Cs2>;
  337. };
  338. \endcode
  339. */
  340. template <typename Cs1, typename Cs2>
  341. struct ColorSpacesCompatibleConcept {
  342. void constraints() {
  343. BOOST_STATIC_ASSERT((color_spaces_are_compatible<Cs1,Cs2>::value));
  344. }
  345. };
  346. /// \brief Channel mapping concept
  347. /// \ingroup ColorSpaceAndLayoutConcept
  348. /**
  349. \code
  350. concept ChannelMappingConcept<MPLRandomAccessSequence CM> {
  351. // An MPL Random Access Sequence, whose elements model MPLIntegralConstant representing a permutation
  352. };
  353. \endcode
  354. */
  355. template <typename CM>
  356. struct ChannelMappingConcept {
  357. void constraints() {
  358. // An MPL Random Access Sequence, whose elements model MPLIntegralConstant representing a permutation
  359. }
  360. };
  361. ////////////////////////////////////////////////////////////////////////////////////////
  362. ///
  363. /// Channel CONCEPTS
  364. ///
  365. ////////////////////////////////////////////////////////////////////////////////////////
  366. /// \ingroup ChannelConcept
  367. /// \brief A channel is the building block of a color. Color is defined as a mixture of primary colors and a channel defines the degree to which each primary color is used in the mixture.
  368. /**
  369. For example, in the RGB color space, using 8-bit unsigned channels, the color red is defined as [255 0 0], which means maximum of Red, and no Green and Blue.
  370. Built-in scalar types, such as \p int and \p float, are valid GIL channels. In more complex scenarios, channels may be represented as bit ranges or even individual bits.
  371. In such cases special classes are needed to represent the value and reference to a channel.
  372. Channels have a traits class, \p channel_traits, which defines their associated types as well as their operating ranges.
  373. \code
  374. concept ChannelConcept<typename T> : EqualityComparable<T> {
  375. typename value_type = T; // use channel_traits<T>::value_type to access it
  376. typename reference = T&; // use channel_traits<T>::reference to access it
  377. typename pointer = T*; // use channel_traits<T>::pointer to access it
  378. typename const_reference = const T&; // use channel_traits<T>::const_reference to access it
  379. typename const_pointer = const T*; // use channel_traits<T>::const_pointer to access it
  380. static const bool is_mutable; // use channel_traits<T>::is_mutable to access it
  381. static T min_value(); // use channel_traits<T>::min_value to access it
  382. static T max_value(); // use channel_traits<T>::min_value to access it
  383. };
  384. \endcode
  385. */
  386. template <typename T>
  387. struct ChannelConcept {
  388. void constraints() {
  389. gil_function_requires< boost::EqualityComparableConcept<T> >();
  390. typedef typename channel_traits<T>::value_type v;
  391. typedef typename channel_traits<T>::reference r;
  392. typedef typename channel_traits<T>::pointer p;
  393. typedef typename channel_traits<T>::const_reference cr;
  394. typedef typename channel_traits<T>::const_pointer cp;
  395. channel_traits<T>::min_value();
  396. channel_traits<T>::max_value();
  397. }
  398. T c;
  399. };
  400. namespace detail {
  401. // Preconditions: T models ChannelConcept
  402. template <typename T>
  403. struct ChannelIsMutableConcept {
  404. void constraints() {
  405. c1=c2;
  406. using std::swap;
  407. swap(c1,c2);
  408. }
  409. T c1;
  410. T c2;
  411. };
  412. }
  413. /// \brief A channel that allows for modifying its value
  414. /// \ingroup ChannelConcept
  415. /**
  416. \code
  417. concept MutableChannelConcept<ChannelConcept T> : Assignable<T>, Swappable<T> {};
  418. \endcode
  419. */
  420. template <typename T>
  421. struct MutableChannelConcept {
  422. void constraints() {
  423. gil_function_requires<ChannelConcept<T> >();
  424. gil_function_requires<detail::ChannelIsMutableConcept<T> >();
  425. }
  426. };
  427. /// \brief A channel that supports default construction.
  428. /// \ingroup ChannelConcept
  429. /**
  430. \code
  431. concept ChannelValueConcept<ChannelConcept T> : Regular<T> {};
  432. \endcode
  433. */
  434. template <typename T>
  435. struct ChannelValueConcept {
  436. void constraints() {
  437. gil_function_requires<ChannelConcept<T> >();
  438. gil_function_requires<Regular<T> >();
  439. }
  440. };
  441. /// \brief Predicate metafunction returning whether two channels are compatible
  442. /// \ingroup ChannelAlgorithm
  443. ///
  444. /// Channels are considered compatible if their value types (ignoring constness and references) are the same.
  445. /**
  446. Example:
  447. \code
  448. BOOST_STATIC_ASSERT((channels_are_compatible<uint8_t, const uint8_t&>::value));
  449. \endcode
  450. */
  451. template <typename T1, typename T2> // Models GIL Pixel
  452. struct channels_are_compatible
  453. : public is_same<typename channel_traits<T1>::value_type, typename channel_traits<T2>::value_type> {};
  454. /// \brief Channels are compatible if their associated value types (ignoring constness and references) are the same
  455. /// \ingroup ChannelConcept
  456. /**
  457. \code
  458. concept ChannelsCompatibleConcept<ChannelConcept T1, ChannelConcept T2> {
  459. where SameType<T1::value_type, T2::value_type>;
  460. };
  461. \endcode
  462. */
  463. template <typename T1, typename T2>
  464. struct ChannelsCompatibleConcept {
  465. void constraints() {
  466. BOOST_STATIC_ASSERT((channels_are_compatible<T1,T2>::value));
  467. }
  468. };
  469. /// \brief A channel is convertible to another one if the \p channel_convert algorithm is defined for the two channels
  470. ///
  471. /// Convertibility is non-symmetric and implies that one channel can be converted to another. Conversion is explicit and often lossy operation.
  472. /// \ingroup ChannelConcept
  473. /**
  474. \code
  475. concept ChannelConvertibleConcept<ChannelConcept SrcChannel, ChannelValueConcept DstChannel> {
  476. DstChannel channel_convert(const SrcChannel&);
  477. };
  478. \endcode
  479. */
  480. template <typename SrcChannel, typename DstChannel>
  481. struct ChannelConvertibleConcept {
  482. void constraints() {
  483. gil_function_requires<ChannelConcept<SrcChannel> >();
  484. gil_function_requires<MutableChannelConcept<DstChannel> >();
  485. dst=channel_convert<DstChannel,SrcChannel>(src); ignore_unused_variable_warning(dst);
  486. }
  487. SrcChannel src;
  488. DstChannel dst;
  489. };
  490. ////////////////////////////////////////////////////////////////////////////////////////
  491. ///
  492. /// COLOR BASE CONCEPTS
  493. ///
  494. ////////////////////////////////////////////////////////////////////////////////////////
  495. /// \ingroup ColorBaseConcept
  496. /// \brief A color base is a container of color elements (such as channels, channel references or channel pointers)
  497. /**
  498. The most common use of color base is in the implementation of a pixel, in which case the color
  499. elements are channel values. The color base concept, however, can be used in other scenarios. For example, a planar pixel has channels that are not
  500. contiguous in memory. Its reference is a proxy class that uses a color base whose elements are channel references. Its iterator uses a color base
  501. whose elements are channel iterators.
  502. A color base must have an associated layout (which consists of a color space, as well as an ordering of the channels).
  503. There are two ways to index the elements of a color base: A physical index corresponds to the way they are ordered in memory, and
  504. a semantic index corresponds to the way the elements are ordered in their color space.
  505. For example, in the RGB color space the elements are ordered as {red_t, green_t, blue_t}. For a color base with a BGR layout, the first element
  506. in physical ordering is the blue element, whereas the first semantic element is the red one.
  507. Models of \p ColorBaseConcept are required to provide the \p at_c<K>(ColorBase) function, which allows for accessing the elements based on their
  508. physical order. GIL provides a \p semantic_at_c<K>(ColorBase) function (described later) which can operate on any model of ColorBaseConcept and returns
  509. the corresponding semantic element.
  510. \code
  511. concept ColorBaseConcept<typename T> : CopyConstructible<T>, EqualityComparable<T> {
  512. // a GIL layout (the color space and element permutation)
  513. typename layout_t;
  514. // The type of K-th element
  515. template <int K> struct kth_element_type; where Metafunction<kth_element_type>;
  516. // The result of at_c
  517. template <int K> struct kth_element_const_reference_type; where Metafunction<kth_element_const_reference_type>;
  518. template <int K> kth_element_const_reference_type<T,K>::type at_c(T);
  519. // Copy-constructible and equality comparable with other compatible color bases
  520. template <ColorBaseConcept T2> where { ColorBasesCompatibleConcept<T,T2> }
  521. T::T(T2);
  522. template <ColorBaseConcept T2> where { ColorBasesCompatibleConcept<T,T2> }
  523. bool operator==(const T&, const T2&);
  524. template <ColorBaseConcept T2> where { ColorBasesCompatibleConcept<T,T2> }
  525. bool operator!=(const T&, const T2&);
  526. };
  527. \endcode
  528. */
  529. template <typename ColorBase>
  530. struct ColorBaseConcept {
  531. void constraints() {
  532. gil_function_requires< CopyConstructible<ColorBase> >();
  533. gil_function_requires< EqualityComparable<ColorBase> >();
  534. typedef typename ColorBase::layout_t::color_space_t color_space_t;
  535. gil_function_requires<ColorSpaceConcept<color_space_t> >();
  536. typedef typename ColorBase::layout_t::channel_mapping_t channel_mapping_t;
  537. // TODO: channel_mapping_t must be an MPL RandomAccessSequence
  538. static const std::size_t num_elements = size<ColorBase>::value;
  539. typedef typename kth_element_type<ColorBase,num_elements-1>::type TN;
  540. typedef typename kth_element_const_reference_type<ColorBase,num_elements-1>::type CR;
  541. CR cr=gil::at_c<num_elements-1>(cb); ignore_unused_variable_warning(cr);
  542. // functions that work for every pixel (no need to require them)
  543. semantic_at_c<0>(cb);
  544. semantic_at_c<num_elements-1>(cb);
  545. // also static_max(cb), static_min(cb), static_fill(cb,value), and all variations of static_for_each(), static_generate(), static_transform()
  546. }
  547. ColorBase cb;
  548. };
  549. /// \ingroup ColorBaseConcept
  550. /// \brief Color base which allows for modifying its elements
  551. /**
  552. \code
  553. concept MutableColorBaseConcept<ColorBaseConcept T> : Assignable<T>, Swappable<T> {
  554. template <int K> struct kth_element_reference_type; where Metafunction<kth_element_reference_type>;
  555. template <int K> kth_element_reference_type<kth_element_type<T,K>::type>::type at_c(T);
  556. template <ColorBaseConcept T2> where { ColorBasesCompatibleConcept<T,T2> }
  557. T& operator=(T&, const T2&);
  558. };
  559. \endcode
  560. */
  561. template <typename ColorBase>
  562. struct MutableColorBaseConcept {
  563. void constraints() {
  564. gil_function_requires< ColorBaseConcept<ColorBase> >();
  565. gil_function_requires< Assignable<ColorBase> >();
  566. gil_function_requires< Swappable<ColorBase> >();
  567. typedef typename kth_element_reference_type<ColorBase, 0>::type CR;
  568. CR r=gil::at_c<0>(cb);
  569. gil::at_c<0>(cb)=r;
  570. }
  571. ColorBase cb;
  572. };
  573. /// \ingroup ColorBaseConcept
  574. /// \brief Color base that also has a default-constructor. Refines Regular
  575. /**
  576. \code
  577. concept ColorBaseValueConcept<typename T> : MutableColorBaseConcept<T>, Regular<T> {
  578. };
  579. \endcode
  580. */
  581. template <typename ColorBase>
  582. struct ColorBaseValueConcept {
  583. void constraints() {
  584. gil_function_requires< MutableColorBaseConcept<ColorBase> >();
  585. gil_function_requires< Regular<ColorBase> >();
  586. }
  587. };
  588. /// \ingroup ColorBaseConcept
  589. /// \brief Color base whose elements all have the same type
  590. /**
  591. \code
  592. concept HomogeneousColorBaseConcept<ColorBaseConcept CB> {
  593. // For all K in [0 ... size<C1>::value-1):
  594. // where SameType<kth_element_type<CB,K>::type, kth_element_type<CB,K+1>::type>;
  595. kth_element_const_reference_type<CB,0>::type dynamic_at_c(const CB&, std::size_t n) const;
  596. };
  597. \endcode
  598. */
  599. template <typename ColorBase>
  600. struct HomogeneousColorBaseConcept {
  601. void constraints() {
  602. gil_function_requires< ColorBaseConcept<ColorBase> >();
  603. static const std::size_t num_elements = size<ColorBase>::value;
  604. typedef typename kth_element_type<ColorBase,0>::type T0;
  605. typedef typename kth_element_type<ColorBase,num_elements-1>::type TN;
  606. BOOST_STATIC_ASSERT((is_same<T0,TN>::value)); // better than nothing
  607. typedef typename kth_element_const_reference_type<ColorBase,0>::type CRef0;
  608. CRef0 e0=dynamic_at_c(cb,0);
  609. }
  610. ColorBase cb;
  611. };
  612. /// \ingroup ColorBaseConcept
  613. /// \brief Homogeneous color base that allows for modifying its elements
  614. /**
  615. \code
  616. concept MutableHomogeneousColorBaseConcept<ColorBaseConcept CB> : HomogeneousColorBaseConcept<CB> {
  617. kth_element_reference_type<CB,0>::type dynamic_at_c(CB&, std::size_t n);
  618. };
  619. \endcode
  620. */
  621. template <typename ColorBase>
  622. struct MutableHomogeneousColorBaseConcept {
  623. void constraints() {
  624. gil_function_requires< ColorBaseConcept<ColorBase> >();
  625. gil_function_requires< HomogeneousColorBaseConcept<ColorBase> >();
  626. typedef typename kth_element_reference_type<ColorBase, 0>::type R0;
  627. R0 x=dynamic_at_c(cb,0);
  628. dynamic_at_c(cb,0) = dynamic_at_c(cb,0);
  629. }
  630. ColorBase cb;
  631. };
  632. /// \ingroup ColorBaseConcept
  633. /// \brief Homogeneous color base that also has a default constructor. Refines Regular.
  634. /**
  635. \code
  636. concept HomogeneousColorBaseValueConcept<typename T> : MutableHomogeneousColorBaseConcept<T>, Regular<T> {
  637. };
  638. \endcode
  639. */
  640. template <typename ColorBase>
  641. struct HomogeneousColorBaseValueConcept {
  642. void constraints() {
  643. gil_function_requires< MutableHomogeneousColorBaseConcept<ColorBase> >();
  644. gil_function_requires< Regular<ColorBase> >();
  645. }
  646. };
  647. /// \ingroup ColorBaseConcept
  648. /// \brief Two color bases are compatible if they have the same color space and their elements are compatible, semantic-pairwise.
  649. /**
  650. \code
  651. concept ColorBasesCompatibleConcept<ColorBaseConcept C1, ColorBaseConcept C2> {
  652. where SameType<C1::layout_t::color_space_t, C2::layout_t::color_space_t>;
  653. // also, for all K in [0 ... size<C1>::value):
  654. // where Convertible<kth_semantic_element_type<C1,K>::type, kth_semantic_element_type<C2,K>::type>;
  655. // where Convertible<kth_semantic_element_type<C2,K>::type, kth_semantic_element_type<C1,K>::type>;
  656. };
  657. \endcode
  658. */
  659. template <typename ColorBase1, typename ColorBase2>
  660. struct ColorBasesCompatibleConcept {
  661. void constraints() {
  662. BOOST_STATIC_ASSERT((is_same<typename ColorBase1::layout_t::color_space_t,
  663. typename ColorBase2::layout_t::color_space_t>::value));
  664. // typedef typename kth_semantic_element_type<ColorBase1,0>::type e1;
  665. // typedef typename kth_semantic_element_type<ColorBase2,0>::type e2;
  666. // "e1 is convertible to e2"
  667. }
  668. };
  669. ////////////////////////////////////////////////////////////////////////////////////////
  670. ///
  671. /// PIXEL CONCEPTS
  672. ///
  673. ////////////////////////////////////////////////////////////////////////////////////////
  674. /// \brief Concept for all pixel-based GIL constructs, such as pixels, iterators, locators, views and images whose value type is a pixel
  675. /// \ingroup PixelBasedConcept
  676. /**
  677. \code
  678. concept PixelBasedConcept<typename T> {
  679. typename color_space_type<T>;
  680. where Metafunction<color_space_type<T> >;
  681. where ColorSpaceConcept<color_space_type<T>::type>;
  682. typename channel_mapping_type<T>;
  683. where Metafunction<channel_mapping_type<T> >;
  684. where ChannelMappingConcept<channel_mapping_type<T>::type>;
  685. typename is_planar<T>;
  686. where Metafunction<is_planar<T> >;
  687. where SameType<is_planar<T>::type, bool>;
  688. };
  689. \endcode
  690. */
  691. template <typename P>
  692. struct PixelBasedConcept {
  693. void constraints() {
  694. typedef typename color_space_type<P>::type color_space_t;
  695. gil_function_requires<ColorSpaceConcept<color_space_t> >();
  696. typedef typename channel_mapping_type<P>::type channel_mapping_t;
  697. gil_function_requires<ChannelMappingConcept<channel_mapping_t> >();
  698. static const bool planar = is_planar<P>::type::value; ignore_unused_variable_warning(planar);
  699. // This is not part of the concept, but should still work
  700. static const std::size_t nc = num_channels<P>::value;
  701. ignore_unused_variable_warning(nc);
  702. }
  703. };
  704. /// \brief Concept for homogeneous pixel-based GIL constructs
  705. /// \ingroup PixelBasedConcept
  706. /**
  707. \code
  708. concept HomogeneousPixelBasedConcept<PixelBasedConcept T> {
  709. typename channel_type<T>;
  710. where Metafunction<channel_type<T> >;
  711. where ChannelConcept<channel_type<T>::type>;
  712. };
  713. \endcode
  714. */
  715. template <typename P>
  716. struct HomogeneousPixelBasedConcept {
  717. void constraints() {
  718. gil_function_requires<PixelBasedConcept<P> >();
  719. typedef typename channel_type<P>::type channel_t;
  720. gil_function_requires<ChannelConcept<channel_t> >();
  721. }
  722. };
  723. /// \brief Pixel concept - A color base whose elements are channels
  724. /// \ingroup PixelConcept
  725. /**
  726. \code
  727. concept PixelConcept<typename P> : ColorBaseConcept<P>, PixelBasedConcept<P> {
  728. where is_pixel<P>::type::value==true;
  729. // where for each K [0..size<P>::value-1]:
  730. // ChannelConcept<kth_element_type<P,K> >;
  731. typename P::value_type; where PixelValueConcept<value_type>;
  732. typename P::reference; where PixelConcept<reference>;
  733. typename P::const_reference; where PixelConcept<const_reference>;
  734. static const bool P::is_mutable;
  735. template <PixelConcept P2> where { PixelConcept<P,P2> }
  736. P::P(P2);
  737. template <PixelConcept P2> where { PixelConcept<P,P2> }
  738. bool operator==(const P&, const P2&);
  739. template <PixelConcept P2> where { PixelConcept<P,P2> }
  740. bool operator!=(const P&, const P2&);
  741. };
  742. \endcode
  743. */
  744. template <typename P>
  745. struct PixelConcept {
  746. void constraints() {
  747. gil_function_requires<ColorBaseConcept<P> >();
  748. gil_function_requires<PixelBasedConcept<P> >();
  749. BOOST_STATIC_ASSERT((is_pixel<P>::value));
  750. static const bool is_mutable = P::is_mutable; ignore_unused_variable_warning(is_mutable);
  751. typedef typename P::value_type value_type;
  752. // gil_function_requires<PixelValueConcept<value_type> >();
  753. typedef typename P::reference reference;
  754. gil_function_requires<PixelConcept<typename remove_const_and_reference<reference>::type> >();
  755. typedef typename P::const_reference const_reference;
  756. gil_function_requires<PixelConcept<typename remove_const_and_reference<const_reference>::type> >();
  757. }
  758. };
  759. /// \brief Pixel concept that allows for changing its channels
  760. /// \ingroup PixelConcept
  761. /**
  762. \code
  763. concept MutablePixelConcept<PixelConcept P> : MutableColorBaseConcept<P> {
  764. where is_mutable==true;
  765. };
  766. \endcode
  767. */
  768. template <typename P>
  769. struct MutablePixelConcept {
  770. void constraints() {
  771. gil_function_requires<PixelConcept<P> >();
  772. BOOST_STATIC_ASSERT(P::is_mutable);
  773. }
  774. };
  775. /// \brief Homogeneous pixel concept
  776. /// \ingroup PixelConcept
  777. /**
  778. \code
  779. concept HomogeneousPixelConcept<PixelConcept P> : HomogeneousColorBaseConcept<P>, HomogeneousPixelBasedConcept<P> {
  780. P::template element_const_reference_type<P>::type operator[](P p, std::size_t i) const { return dynamic_at_c(p,i); }
  781. };
  782. \endcode
  783. */
  784. template <typename P>
  785. struct HomogeneousPixelConcept {
  786. void constraints() {
  787. gil_function_requires<PixelConcept<P> >();
  788. gil_function_requires<HomogeneousColorBaseConcept<P> >();
  789. gil_function_requires<HomogeneousPixelBasedConcept<P> >();
  790. p[0];
  791. }
  792. P p;
  793. };
  794. /// \brief Homogeneous pixel concept that allows for changing its channels
  795. /// \ingroup PixelConcept
  796. /**
  797. \code
  798. concept MutableHomogeneousPixelConcept<HomogeneousPixelConcept P> : MutableHomogeneousColorBaseConcept<P> {
  799. P::template element_reference_type<P>::type operator[](P p, std::size_t i) { return dynamic_at_c(p,i); }
  800. };
  801. \endcode
  802. */
  803. template <typename P>
  804. struct MutableHomogeneousPixelConcept {
  805. void constraints() {
  806. gil_function_requires<HomogeneousPixelConcept<P> >();
  807. gil_function_requires<MutableHomogeneousColorBaseConcept<P> >();
  808. p[0]=v;
  809. v=p[0];
  810. }
  811. typename P::template element_type<P>::type v;
  812. P p;
  813. };
  814. /// \brief Pixel concept that is a Regular type
  815. /// \ingroup PixelConcept
  816. /**
  817. \code
  818. concept PixelValueConcept<PixelConcept P> : Regular<P> {
  819. where SameType<value_type,P>;
  820. };
  821. \endcode
  822. */
  823. template <typename P>
  824. struct PixelValueConcept {
  825. void constraints() {
  826. gil_function_requires<PixelConcept<P> >();
  827. gil_function_requires<Regular<P> >();
  828. }
  829. };
  830. /// \brief Homogeneous pixel concept that is a Regular type
  831. /// \ingroup PixelConcept
  832. /**
  833. \code
  834. concept HomogeneousPixelValueConcept<HomogeneousPixelConcept P> : Regular<P> {
  835. where SameType<value_type,P>;
  836. };
  837. \endcode
  838. */
  839. template <typename P>
  840. struct HomogeneousPixelValueConcept {
  841. void constraints() {
  842. gil_function_requires<HomogeneousPixelConcept<P> >();
  843. gil_function_requires<Regular<P> >();
  844. BOOST_STATIC_ASSERT((is_same<P, typename P::value_type>::value));
  845. }
  846. };
  847. namespace detail {
  848. template <typename P1, typename P2, int K>
  849. struct channels_are_pairwise_compatible : public
  850. mpl::and_<channels_are_pairwise_compatible<P1,P2,K-1>,
  851. channels_are_compatible<typename kth_semantic_element_reference_type<P1,K>::type,
  852. typename kth_semantic_element_reference_type<P2,K>::type> > {};
  853. template <typename P1, typename P2>
  854. struct channels_are_pairwise_compatible<P1,P2,-1> : public mpl::true_ {};
  855. }
  856. /// \brief Returns whether two pixels are compatible
  857. ///
  858. /// Pixels are compatible if their channels and color space types are compatible. Compatible pixels can be assigned and copy constructed from one another.
  859. /// \ingroup PixelAlgorithm
  860. template <typename P1, typename P2> // Models GIL Pixel
  861. struct pixels_are_compatible
  862. : public mpl::and_<typename color_spaces_are_compatible<typename color_space_type<P1>::type,
  863. typename color_space_type<P2>::type>::type,
  864. detail::channels_are_pairwise_compatible<P1,P2,num_channels<P1>::value-1> > {};
  865. /// \brief Concept for pixel compatibility
  866. /// Pixels are compatible if their channels and color space types are compatible. Compatible pixels can be assigned and copy constructed from one another.
  867. /// \ingroup PixelConcept
  868. /**
  869. \code
  870. concept PixelsCompatibleConcept<PixelConcept P1, PixelConcept P2> : ColorBasesCompatibleConcept<P1,P2> {
  871. // where for each K [0..size<P1>::value):
  872. // ChannelsCompatibleConcept<kth_semantic_element_type<P1,K>::type, kth_semantic_element_type<P2,K>::type>;
  873. };
  874. \endcode
  875. */
  876. template <typename P1, typename P2> // precondition: P1 and P2 model PixelConcept
  877. struct PixelsCompatibleConcept {
  878. void constraints() {
  879. BOOST_STATIC_ASSERT((pixels_are_compatible<P1,P2>::value));
  880. }
  881. };
  882. /// \brief Pixel convertible concept
  883. ///
  884. /// Convertibility is non-symmetric and implies that one pixel can be converted to another, approximating the color. Conversion is explicit and sometimes lossy.
  885. /// \ingroup PixelConcept
  886. /**
  887. \code
  888. template <PixelConcept SrcPixel, MutablePixelConcept DstPixel>
  889. concept PixelConvertibleConcept {
  890. void color_convert(const SrcPixel&, DstPixel&);
  891. };
  892. \endcode
  893. */
  894. template <typename SrcP, typename DstP>
  895. struct PixelConvertibleConcept {
  896. void constraints() {
  897. gil_function_requires<PixelConcept<SrcP> >();
  898. gil_function_requires<MutablePixelConcept<DstP> >();
  899. color_convert(src,dst);
  900. }
  901. SrcP src;
  902. DstP dst;
  903. };
  904. ////////////////////////////////////////////////////////////////////////////////////////
  905. ///
  906. /// DEREFERENCE ADAPTOR CONCEPTS
  907. ///
  908. ////////////////////////////////////////////////////////////////////////////////////////
  909. /// \ingroup PixelDereferenceAdaptorConcept
  910. /// \brief Represents a unary function object that can be invoked upon dereferencing a pixel iterator.
  911. ///
  912. /// This can perform an arbitrary computation, such as color conversion or table lookup
  913. /**
  914. \code
  915. concept PixelDereferenceAdaptorConcept<boost::UnaryFunctionConcept D>
  916. : DefaultConstructibleConcept<D>, CopyConstructibleConcept<D>, AssignableConcept<D> {
  917. typename const_t; where PixelDereferenceAdaptorConcept<const_t>;
  918. typename value_type; where PixelValueConcept<value_type>;
  919. typename reference; // may be mutable
  920. typename const_reference; // must not be mutable
  921. static const bool D::is_mutable;
  922. where Convertible<value_type,result_type>;
  923. };
  924. \endcode
  925. */
  926. template <typename D>
  927. struct PixelDereferenceAdaptorConcept {
  928. void constraints() {
  929. gil_function_requires< boost::UnaryFunctionConcept<D,
  930. typename remove_const_and_reference<typename D::result_type>::type,
  931. typename D::argument_type> >();
  932. gil_function_requires< boost::DefaultConstructibleConcept<D> >();
  933. gil_function_requires< boost::CopyConstructibleConcept<D> >();
  934. gil_function_requires< boost::AssignableConcept<D> >();
  935. gil_function_requires<PixelConcept<typename remove_const_and_reference<typename D::result_type>::type> >();
  936. typedef typename D::const_t const_t;
  937. gil_function_requires<PixelDereferenceAdaptorConcept<const_t> >();
  938. typedef typename D::value_type value_type;
  939. gil_function_requires<PixelValueConcept<value_type> >();
  940. typedef typename D::reference reference; // == PixelConcept (if you remove const and reference)
  941. typedef typename D::const_reference const_reference; // == PixelConcept (if you remove const and reference)
  942. const bool is_mutable=D::is_mutable; ignore_unused_variable_warning(is_mutable);
  943. }
  944. D d;
  945. };
  946. template <typename P>
  947. struct PixelDereferenceAdaptorArchetype {
  948. typedef P argument_type;
  949. typedef P result_type;
  950. typedef PixelDereferenceAdaptorArchetype const_t;
  951. typedef typename remove_reference<P>::type value_type;
  952. typedef typename add_reference<P>::type reference;
  953. typedef reference const_reference;
  954. static const bool is_mutable=false;
  955. P operator()(P) const { throw; }
  956. };
  957. ////////////////////////////////////////////////////////////////////////////////////////
  958. ///
  959. /// Pixel ITERATOR CONCEPTS
  960. ///
  961. ////////////////////////////////////////////////////////////////////////////////////////
  962. /// \brief Concept for iterators, locators and views that can define a type just like the given iterator/locator/view, except it supports runtime specified step along the X navigation
  963. /// \ingroup PixelIteratorConcept
  964. /**
  965. \code
  966. concept HasDynamicXStepTypeConcept<typename T> {
  967. typename dynamic_x_step_type<T>;
  968. where Metafunction<dynamic_x_step_type<T> >;
  969. };
  970. \endcode
  971. */
  972. template <typename T>
  973. struct HasDynamicXStepTypeConcept {
  974. void constraints() {
  975. typedef typename dynamic_x_step_type<T>::type type;
  976. }
  977. };
  978. /// \brief Concept for locators and views that can define a type just like the given locator or view, except it supports runtime specified step along the Y navigation
  979. /// \ingroup PixelLocatorConcept
  980. /**
  981. \code
  982. concept HasDynamicYStepTypeConcept<typename T> {
  983. typename dynamic_y_step_type<T>;
  984. where Metafunction<dynamic_y_step_type<T> >;
  985. };
  986. \endcode
  987. */
  988. template <typename T>
  989. struct HasDynamicYStepTypeConcept {
  990. void constraints() {
  991. typedef typename dynamic_y_step_type<T>::type type;
  992. }
  993. };
  994. /// \brief Concept for locators and views that can define a type just like the given locator or view, except X and Y is swapped
  995. /// \ingroup PixelLocatorConcept
  996. /**
  997. \code
  998. concept HasTransposedTypeConcept<typename T> {
  999. typename transposed_type<T>;
  1000. where Metafunction<transposed_type<T> >;
  1001. };
  1002. \endcode
  1003. */
  1004. template <typename T>
  1005. struct HasTransposedTypeConcept {
  1006. void constraints() {
  1007. typedef typename transposed_type<T>::type type;
  1008. }
  1009. };
  1010. /// \defgroup PixelIteratorConceptPixelIterator PixelIteratorConcept
  1011. /// \ingroup PixelIteratorConcept
  1012. /// \brief STL iterator over pixels
  1013. /// \ingroup PixelIteratorConceptPixelIterator
  1014. /// \brief An STL random access traversal iterator over a model of PixelConcept.
  1015. /**
  1016. GIL's iterators must also provide the following metafunctions:
  1017. - \p const_iterator_type<Iterator>: Returns a read-only equivalent of \p Iterator
  1018. - \p iterator_is_mutable<Iterator>: Returns whether the given iterator is read-only or mutable
  1019. - \p is_iterator_adaptor<Iterator>: Returns whether the given iterator is an adaptor over another iterator. See IteratorAdaptorConcept for additional requirements of adaptors.
  1020. \code
  1021. concept PixelIteratorConcept<typename Iterator> : boost_concepts::RandomAccessTraversalConcept<Iterator>, PixelBasedConcept<Iterator> {
  1022. where PixelValueConcept<value_type>;
  1023. typename const_iterator_type<It>::type;
  1024. where PixelIteratorConcept<const_iterator_type<It>::type>;
  1025. static const bool iterator_is_mutable<It>::type::value;
  1026. static const bool is_iterator_adaptor<It>::type::value; // is it an iterator adaptor
  1027. };
  1028. \endcode
  1029. */
  1030. template <typename Iterator>
  1031. struct PixelIteratorConcept {
  1032. void constraints() {
  1033. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<Iterator> >();
  1034. gil_function_requires<PixelBasedConcept<Iterator> >();
  1035. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  1036. gil_function_requires<PixelValueConcept<value_type> >();
  1037. typedef typename const_iterator_type<Iterator>::type const_t;
  1038. static const bool is_mut = iterator_is_mutable<Iterator>::type::value; ignore_unused_variable_warning(is_mut);
  1039. const_t const_it(it); ignore_unused_variable_warning(const_it); // immutable iterator must be constructible from (possibly mutable) iterator
  1040. check_base(typename is_iterator_adaptor<Iterator>::type());
  1041. }
  1042. void check_base(mpl::false_) {}
  1043. void check_base(mpl::true_) {
  1044. typedef typename iterator_adaptor_get_base<Iterator>::type base_t;
  1045. gil_function_requires<PixelIteratorConcept<base_t> >();
  1046. }
  1047. Iterator it;
  1048. };
  1049. namespace detail {
  1050. template <typename Iterator> // Preconditions: Iterator Models PixelIteratorConcept
  1051. struct PixelIteratorIsMutableConcept {
  1052. void constraints() {
  1053. gil_function_requires<detail::RandomAccessIteratorIsMutableConcept<Iterator> >();
  1054. typedef typename remove_reference<typename std::iterator_traits<Iterator>::reference>::type ref;
  1055. typedef typename element_type<ref>::type channel_t;
  1056. gil_function_requires<detail::ChannelIsMutableConcept<channel_t> >();
  1057. }
  1058. };
  1059. }
  1060. /// \brief Pixel iterator that allows for changing its pixel
  1061. /// \ingroup PixelIteratorConceptPixelIterator
  1062. /**
  1063. \code
  1064. concept MutablePixelIteratorConcept<PixelIteratorConcept Iterator> : MutableRandomAccessIteratorConcept<Iterator> {};
  1065. \endcode
  1066. */
  1067. template <typename Iterator>
  1068. struct MutablePixelIteratorConcept {
  1069. void constraints() {
  1070. gil_function_requires<PixelIteratorConcept<Iterator> >();
  1071. gil_function_requires<detail::PixelIteratorIsMutableConcept<Iterator> >();
  1072. }
  1073. };
  1074. namespace detail {
  1075. // Iterators that can be used as the base of memory_based_step_iterator require some additional functions
  1076. template <typename Iterator> // Preconditions: Iterator Models boost_concepts::RandomAccessTraversalConcept
  1077. struct RandomAccessIteratorIsMemoryBasedConcept {
  1078. void constraints() {
  1079. std::ptrdiff_t bs=memunit_step(it); ignore_unused_variable_warning(bs);
  1080. it=memunit_advanced(it,3);
  1081. std::ptrdiff_t bd=memunit_distance(it,it); ignore_unused_variable_warning(bd);
  1082. memunit_advance(it,3);
  1083. // for performace you may also provide a customized implementation of memunit_advanced_ref
  1084. }
  1085. Iterator it;
  1086. };
  1087. }
  1088. /// \defgroup PixelIteratorConceptStepIterator StepIteratorConcept
  1089. /// \ingroup PixelIteratorConcept
  1090. /// \brief Iterator that advances by a specified step
  1091. /// \brief Concept of a random-access iterator that can be advanced in memory units (bytes or bits)
  1092. /// \ingroup PixelIteratorConceptStepIterator
  1093. /**
  1094. \code
  1095. concept MemoryBasedIteratorConcept<boost_concepts::RandomAccessTraversalConcept Iterator> {
  1096. typename byte_to_memunit<Iterator>; where metafunction<byte_to_memunit<Iterator> >;
  1097. std::ptrdiff_t memunit_step(const Iterator&);
  1098. std::ptrdiff_t memunit_distance(const Iterator& , const Iterator&);
  1099. void memunit_advance(Iterator&, std::ptrdiff_t diff);
  1100. Iterator memunit_advanced(const Iterator& p, std::ptrdiff_t diff) { Iterator tmp; memunit_advance(tmp,diff); return tmp; }
  1101. Iterator::reference memunit_advanced_ref(const Iterator& p, std::ptrdiff_t diff) { return *memunit_advanced(p,diff); }
  1102. };
  1103. \endcode
  1104. */
  1105. template <typename Iterator>
  1106. struct MemoryBasedIteratorConcept {
  1107. void constraints() {
  1108. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<Iterator> >();
  1109. gil_function_requires<detail::RandomAccessIteratorIsMemoryBasedConcept<Iterator> >();
  1110. }
  1111. };
  1112. /// \brief Step iterator concept
  1113. ///
  1114. /// Step iterators are iterators that have a set_step method
  1115. /// \ingroup PixelIteratorConceptStepIterator
  1116. /**
  1117. \code
  1118. concept StepIteratorConcept<boost_concepts::ForwardTraversalConcept Iterator> {
  1119. template <Integral D> void Iterator::set_step(D step);
  1120. };
  1121. \endcode
  1122. */
  1123. template <typename Iterator>
  1124. struct StepIteratorConcept {
  1125. void constraints() {
  1126. gil_function_requires<boost_concepts::ForwardTraversalConcept<Iterator> >();
  1127. it.set_step(0);
  1128. }
  1129. Iterator it;
  1130. };
  1131. /// \brief Step iterator that allows for modifying its current value
  1132. ///
  1133. /// \ingroup PixelIteratorConceptStepIterator
  1134. /**
  1135. \code
  1136. concept MutableStepIteratorConcept<Mutable_ForwardIteratorConcept Iterator> : StepIteratorConcept<Iterator> {};
  1137. \endcode
  1138. */
  1139. template <typename Iterator>
  1140. struct MutableStepIteratorConcept {
  1141. void constraints() {
  1142. gil_function_requires<StepIteratorConcept<Iterator> >();
  1143. gil_function_requires<detail::ForwardIteratorIsMutableConcept<Iterator> >();
  1144. }
  1145. };
  1146. /// \defgroup PixelIteratorConceptIteratorAdaptor IteratorAdaptorConcept
  1147. /// \ingroup PixelIteratorConcept
  1148. /// \brief Adaptor over another iterator
  1149. /// \ingroup PixelIteratorConceptIteratorAdaptor
  1150. /// \brief Iterator adaptor is a forward iterator adapting another forward iterator.
  1151. /**
  1152. In addition to GIL iterator requirements, GIL iterator adaptors must provide the following metafunctions:
  1153. - \p is_iterator_adaptor<Iterator>: Returns \p mpl::true_
  1154. - \p iterator_adaptor_get_base<Iterator>: Returns the base iterator type
  1155. - \p iterator_adaptor_rebind<Iterator,NewBase>: Replaces the base iterator with the new one
  1156. The adaptee can be obtained from the iterator via the "base()" method.
  1157. \code
  1158. concept IteratorAdaptorConcept<boost_concepts::ForwardTraversalConcept Iterator> {
  1159. where SameType<is_iterator_adaptor<Iterator>::type, mpl::true_>;
  1160. typename iterator_adaptor_get_base<Iterator>;
  1161. where Metafunction<iterator_adaptor_get_base<Iterator> >;
  1162. where boost_concepts::ForwardTraversalConcept<iterator_adaptor_get_base<Iterator>::type>;
  1163. typename another_iterator;
  1164. typename iterator_adaptor_rebind<Iterator,another_iterator>::type;
  1165. where boost_concepts::ForwardTraversalConcept<another_iterator>;
  1166. where IteratorAdaptorConcept<iterator_adaptor_rebind<Iterator,another_iterator>::type>;
  1167. const iterator_adaptor_get_base<Iterator>::type& Iterator::base() const;
  1168. };
  1169. \endcode
  1170. */
  1171. template <typename Iterator>
  1172. struct IteratorAdaptorConcept {
  1173. void constraints() {
  1174. gil_function_requires<boost_concepts::ForwardTraversalConcept<Iterator> >();
  1175. typedef typename iterator_adaptor_get_base<Iterator>::type base_t;
  1176. gil_function_requires<boost_concepts::ForwardTraversalConcept<base_t> >();
  1177. BOOST_STATIC_ASSERT(is_iterator_adaptor<Iterator>::value);
  1178. typedef typename iterator_adaptor_rebind<Iterator, void*>::type rebind_t;
  1179. base_t base=it.base(); ignore_unused_variable_warning(base);
  1180. }
  1181. Iterator it;
  1182. };
  1183. /// \brief Iterator adaptor that is mutable
  1184. /// \ingroup PixelIteratorConceptIteratorAdaptor
  1185. /**
  1186. \code
  1187. concept MutableIteratorAdaptorConcept<Mutable_ForwardIteratorConcept Iterator> : IteratorAdaptorConcept<Iterator> {};
  1188. \endcode
  1189. */
  1190. template <typename Iterator>
  1191. struct MutableIteratorAdaptorConcept {
  1192. void constraints() {
  1193. gil_function_requires<IteratorAdaptorConcept<Iterator> >();
  1194. gil_function_requires<detail::ForwardIteratorIsMutableConcept<Iterator> >();
  1195. }
  1196. };
  1197. ////////////////////////////////////////////////////////////////////////////////////////
  1198. ///
  1199. /// LOCATOR CONCEPTS
  1200. ///
  1201. ////////////////////////////////////////////////////////////////////////////////////////
  1202. /// \defgroup LocatorNDConcept RandomAccessNDLocatorConcept
  1203. /// \ingroup PixelLocatorConcept
  1204. /// \brief N-dimensional locator
  1205. /// \defgroup Locator2DConcept RandomAccess2DLocatorConcept
  1206. /// \ingroup PixelLocatorConcept
  1207. /// \brief 2-dimensional locator
  1208. /// \defgroup PixelLocator2DConcept PixelLocatorConcept
  1209. /// \ingroup PixelLocatorConcept
  1210. /// \brief 2-dimensional locator over pixel data
  1211. /// \ingroup LocatorNDConcept
  1212. /// \brief N-dimensional locator over immutable values
  1213. /**
  1214. \code
  1215. concept RandomAccessNDLocatorConcept<Regular Loc> {
  1216. typename value_type; // value over which the locator navigates
  1217. typename reference; // result of dereferencing
  1218. typename difference_type; where PointNDConcept<difference_type>; // return value of operator-.
  1219. typename const_t; // same as Loc, but operating over immutable values
  1220. typename cached_location_t; // type to store relative location (for efficient repeated access)
  1221. typename point_t = difference_type;
  1222. static const size_t num_dimensions; // dimensionality of the locator
  1223. where num_dimensions = point_t::num_dimensions;
  1224. // The difference_type and iterator type along each dimension. The iterators may only differ in
  1225. // difference_type. Their value_type must be the same as Loc::value_type
  1226. template <size_t D> struct axis {
  1227. typename coord_t = point_t::axis<D>::coord_t;
  1228. typename iterator; where RandomAccessTraversalConcept<iterator>; // iterator along D-th axis.
  1229. where iterator::value_type == value_type;
  1230. };
  1231. // Defines the type of a locator similar to this type, except it invokes Deref upon dereferencing
  1232. template <PixelDereferenceAdaptorConcept Deref> struct add_deref {
  1233. typename type; where RandomAccessNDLocatorConcept<type>;
  1234. static type make(const Loc& loc, const Deref& deref);
  1235. };
  1236. Loc& operator+=(Loc&, const difference_type&);
  1237. Loc& operator-=(Loc&, const difference_type&);
  1238. Loc operator+(const Loc&, const difference_type&);
  1239. Loc operator-(const Loc&, const difference_type&);
  1240. reference operator*(const Loc&);
  1241. reference operator[](const Loc&, const difference_type&);
  1242. // Storing relative location for faster repeated access and accessing it
  1243. cached_location_t Loc::cache_location(const difference_type&) const;
  1244. reference operator[](const Loc&,const cached_location_t&);
  1245. // Accessing iterators along a given dimension at the current location or at a given offset
  1246. template <size_t D> axis<D>::iterator& Loc::axis_iterator();
  1247. template <size_t D> axis<D>::iterator const& Loc::axis_iterator() const;
  1248. template <size_t D> axis<D>::iterator Loc::axis_iterator(const difference_type&) const;
  1249. };
  1250. \endcode
  1251. */
  1252. template <typename Loc>
  1253. struct RandomAccessNDLocatorConcept {
  1254. void constraints() {
  1255. gil_function_requires< Regular<Loc> >();
  1256. typedef typename Loc::value_type value_type;
  1257. typedef typename Loc::reference reference; // result of dereferencing
  1258. typedef typename Loc::difference_type difference_type; // result of operator-(pixel_locator, pixel_locator)
  1259. typedef typename Loc::cached_location_t cached_location_t; // type used to store relative location (to allow for more efficient repeated access)
  1260. typedef typename Loc::const_t const_t; // same as this type, but over const values
  1261. typedef typename Loc::point_t point_t; // same as difference_type
  1262. static const std::size_t N=Loc::num_dimensions; ignore_unused_variable_warning(N);
  1263. typedef typename Loc::template axis<0>::iterator first_it_type;
  1264. typedef typename Loc::template axis<N-1>::iterator last_it_type;
  1265. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<first_it_type> >();
  1266. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<last_it_type> >();
  1267. // point_t must be an N-dimensional point, each dimension of which must have the same type as difference_type of the corresponding iterator
  1268. gil_function_requires<PointNDConcept<point_t> >();
  1269. BOOST_STATIC_ASSERT(point_t::num_dimensions==N);
  1270. BOOST_STATIC_ASSERT((is_same<typename std::iterator_traits<first_it_type>::difference_type, typename point_t::template axis<0>::coord_t>::value));
  1271. BOOST_STATIC_ASSERT((is_same<typename std::iterator_traits<last_it_type>::difference_type, typename point_t::template axis<N-1>::coord_t>::value));
  1272. difference_type d;
  1273. loc+=d;
  1274. loc-=d;
  1275. loc=loc+d;
  1276. loc=loc-d;
  1277. reference r1=loc[d]; ignore_unused_variable_warning(r1);
  1278. reference r2=*loc; ignore_unused_variable_warning(r2);
  1279. cached_location_t cl=loc.cache_location(d); ignore_unused_variable_warning(cl);
  1280. reference r3=loc[d]; ignore_unused_variable_warning(r3);
  1281. first_it_type fi=loc.template axis_iterator<0>();
  1282. fi=loc.template axis_iterator<0>(d);
  1283. last_it_type li=loc.template axis_iterator<N-1>();
  1284. li=loc.template axis_iterator<N-1>(d);
  1285. typedef PixelDereferenceAdaptorArchetype<typename Loc::value_type> deref_t;
  1286. typedef typename Loc::template add_deref<deref_t>::type dtype;
  1287. //gil_function_requires<RandomAccessNDLocatorConcept<dtype> >(); // infinite recursion
  1288. }
  1289. Loc loc;
  1290. };
  1291. /// \ingroup Locator2DConcept
  1292. /// \brief 2-dimensional locator over immutable values
  1293. /**
  1294. \code
  1295. concept RandomAccess2DLocatorConcept<RandomAccessNDLocatorConcept Loc> {
  1296. where num_dimensions==2;
  1297. where Point2DConcept<point_t>;
  1298. typename x_iterator = axis<0>::iterator;
  1299. typename y_iterator = axis<1>::iterator;
  1300. typename x_coord_t = axis<0>::coord_t;
  1301. typename y_coord_t = axis<1>::coord_t;
  1302. // Only available to locators that have dynamic step in Y
  1303. //Loc::Loc(const Loc& loc, y_coord_t);
  1304. // Only available to locators that have dynamic step in X and Y
  1305. //Loc::Loc(const Loc& loc, x_coord_t, y_coord_t, bool transposed=false);
  1306. x_iterator& Loc::x();
  1307. x_iterator const& Loc::x() const;
  1308. y_iterator& Loc::y();
  1309. y_iterator const& Loc::y() const;
  1310. x_iterator Loc::x_at(const difference_type&) const;
  1311. y_iterator Loc::y_at(const difference_type&) const;
  1312. Loc Loc::xy_at(const difference_type&) const;
  1313. // x/y versions of all methods that can take difference type
  1314. x_iterator Loc::x_at(x_coord_t, y_coord_t) const;
  1315. y_iterator Loc::y_at(x_coord_t, y_coord_t) const;
  1316. Loc Loc::xy_at(x_coord_t, y_coord_t) const;
  1317. reference operator()(const Loc&, x_coord_t, y_coord_t);
  1318. cached_location_t Loc::cache_location(x_coord_t, y_coord_t) const;
  1319. bool Loc::is_1d_traversable(x_coord_t width) const;
  1320. y_coord_t Loc::y_distance_to(const Loc& loc2, x_coord_t x_diff) const;
  1321. };
  1322. \endcode
  1323. */
  1324. template <typename Loc>
  1325. struct RandomAccess2DLocatorConcept {
  1326. void constraints() {
  1327. gil_function_requires<RandomAccessNDLocatorConcept<Loc> >();
  1328. BOOST_STATIC_ASSERT(Loc::num_dimensions==2);
  1329. typedef typename dynamic_x_step_type<Loc>::type dynamic_x_step_t;
  1330. typedef typename dynamic_y_step_type<Loc>::type dynamic_y_step_t;
  1331. typedef typename transposed_type<Loc>::type transposed_t;
  1332. typedef typename Loc::cached_location_t cached_location_t;
  1333. gil_function_requires<Point2DConcept<typename Loc::point_t> >();
  1334. typedef typename Loc::x_iterator x_iterator;
  1335. typedef typename Loc::y_iterator y_iterator;
  1336. typedef typename Loc::x_coord_t x_coord_t;
  1337. typedef typename Loc::y_coord_t y_coord_t;
  1338. x_coord_t xd=0; ignore_unused_variable_warning(xd);
  1339. y_coord_t yd=0; ignore_unused_variable_warning(yd);
  1340. typename Loc::difference_type d;
  1341. typename Loc::reference r=loc(xd,yd); ignore_unused_variable_warning(r);
  1342. dynamic_x_step_t loc2(dynamic_x_step_t(), yd);
  1343. dynamic_x_step_t loc3(dynamic_x_step_t(), xd, yd);
  1344. typedef typename dynamic_y_step_type<typename dynamic_x_step_type<transposed_t>::type>::type dynamic_xy_step_transposed_t;
  1345. dynamic_xy_step_transposed_t loc4(loc, xd,yd,true);
  1346. bool is_contiguous=loc.is_1d_traversable(xd); ignore_unused_variable_warning(is_contiguous);
  1347. loc.y_distance_to(loc, xd);
  1348. loc=loc.xy_at(d);
  1349. loc=loc.xy_at(xd,yd);
  1350. x_iterator xit=loc.x_at(d);
  1351. xit=loc.x_at(xd,yd);
  1352. xit=loc.x();
  1353. y_iterator yit=loc.y_at(d);
  1354. yit=loc.y_at(xd,yd);
  1355. yit=loc.y();
  1356. cached_location_t cl=loc.cache_location(xd,yd); ignore_unused_variable_warning(cl);
  1357. }
  1358. Loc loc;
  1359. };
  1360. /// \ingroup PixelLocator2DConcept
  1361. /// \brief GIL's 2-dimensional locator over immutable GIL pixels
  1362. /**
  1363. \code
  1364. concept PixelLocatorConcept<RandomAccess2DLocatorConcept Loc> {
  1365. where PixelValueConcept<value_type>;
  1366. where PixelIteratorConcept<x_iterator>;
  1367. where PixelIteratorConcept<y_iterator>;
  1368. where x_coord_t == y_coord_t;
  1369. typename coord_t = x_coord_t;
  1370. };
  1371. \endcode
  1372. */
  1373. template <typename Loc>
  1374. struct PixelLocatorConcept {
  1375. void constraints() {
  1376. gil_function_requires< RandomAccess2DLocatorConcept<Loc> >();
  1377. gil_function_requires< PixelIteratorConcept<typename Loc::x_iterator> >();
  1378. gil_function_requires< PixelIteratorConcept<typename Loc::y_iterator> >();
  1379. typedef typename Loc::coord_t coord_t;
  1380. BOOST_STATIC_ASSERT((is_same<typename Loc::x_coord_t, typename Loc::y_coord_t>::value));
  1381. }
  1382. Loc loc;
  1383. };
  1384. namespace detail {
  1385. template <typename Loc> // preconditions: Loc Models RandomAccessNDLocatorConcept
  1386. struct RandomAccessNDLocatorIsMutableConcept {
  1387. void constraints() {
  1388. gil_function_requires<detail::RandomAccessIteratorIsMutableConcept<typename Loc::template axis<0>::iterator> >();
  1389. gil_function_requires<detail::RandomAccessIteratorIsMutableConcept<typename Loc::template axis<Loc::num_dimensions-1>::iterator> >();
  1390. typename Loc::difference_type d; initialize_it(d);
  1391. typename Loc::value_type v;initialize_it(v);
  1392. typename Loc::cached_location_t cl=loc.cache_location(d);
  1393. *loc=v;
  1394. loc[d]=v;
  1395. loc[cl]=v;
  1396. }
  1397. Loc loc;
  1398. };
  1399. template <typename Loc> // preconditions: Loc Models RandomAccess2DLocatorConcept
  1400. struct RandomAccess2DLocatorIsMutableConcept {
  1401. void constraints() {
  1402. gil_function_requires<detail::RandomAccessNDLocatorIsMutableConcept<Loc> >();
  1403. typename Loc::x_coord_t xd=0; ignore_unused_variable_warning(xd);
  1404. typename Loc::y_coord_t yd=0; ignore_unused_variable_warning(yd);
  1405. typename Loc::value_type v; initialize_it(v);
  1406. loc(xd,yd)=v;
  1407. }
  1408. Loc loc;
  1409. };
  1410. }
  1411. /// \ingroup LocatorNDConcept
  1412. /// \brief N-dimensional locator over mutable pixels
  1413. /**
  1414. \code
  1415. concept MutableRandomAccessNDLocatorConcept<RandomAccessNDLocatorConcept Loc> {
  1416. where Mutable<reference>;
  1417. };
  1418. \endcode
  1419. */
  1420. template <typename Loc>
  1421. struct MutableRandomAccessNDLocatorConcept {
  1422. void constraints() {
  1423. gil_function_requires<RandomAccessNDLocatorConcept<Loc> >();
  1424. gil_function_requires<detail::RandomAccessNDLocatorIsMutableConcept<Loc> >();
  1425. }
  1426. };
  1427. /// \ingroup Locator2DConcept
  1428. /// \brief 2-dimensional locator over mutable pixels
  1429. /**
  1430. \code
  1431. concept MutableRandomAccess2DLocatorConcept<RandomAccess2DLocatorConcept Loc> : MutableRandomAccessNDLocatorConcept<Loc> {};
  1432. \endcode
  1433. */
  1434. template <typename Loc>
  1435. struct MutableRandomAccess2DLocatorConcept {
  1436. void constraints() {
  1437. gil_function_requires< RandomAccess2DLocatorConcept<Loc> >();
  1438. gil_function_requires<detail::RandomAccess2DLocatorIsMutableConcept<Loc> >();
  1439. }
  1440. };
  1441. /// \ingroup PixelLocator2DConcept
  1442. /// \brief GIL's 2-dimensional locator over mutable GIL pixels
  1443. /**
  1444. \code
  1445. concept MutablePixelLocatorConcept<PixelLocatorConcept Loc> : MutableRandomAccess2DLocatorConcept<Loc> {};
  1446. \endcode
  1447. */
  1448. template <typename Loc>
  1449. struct MutablePixelLocatorConcept {
  1450. void constraints() {
  1451. gil_function_requires<PixelLocatorConcept<Loc> >();
  1452. gil_function_requires<detail::RandomAccess2DLocatorIsMutableConcept<Loc> >();
  1453. }
  1454. };
  1455. ////////////////////////////////////////////////////////////////////////////////////////
  1456. ///
  1457. /// IMAGE VIEW CONCEPTS
  1458. ///
  1459. ////////////////////////////////////////////////////////////////////////////////////////
  1460. /// \defgroup ImageViewNDConcept ImageViewNDLocatorConcept
  1461. /// \ingroup ImageViewConcept
  1462. /// \brief N-dimensional range
  1463. /// \defgroup ImageView2DConcept ImageView2DConcept
  1464. /// \ingroup ImageViewConcept
  1465. /// \brief 2-dimensional range
  1466. /// \defgroup PixelImageViewConcept ImageViewConcept
  1467. /// \ingroup ImageViewConcept
  1468. /// \brief 2-dimensional range over pixel data
  1469. /// \ingroup ImageViewNDConcept
  1470. /// \brief N-dimensional view over immutable values
  1471. /**
  1472. \code
  1473. concept RandomAccessNDImageViewConcept<Regular View> {
  1474. typename value_type;
  1475. typename reference; // result of dereferencing
  1476. typename difference_type; // result of operator-(iterator,iterator) (1-dimensional!)
  1477. typename const_t; where RandomAccessNDImageViewConcept<View>; // same as View, but over immutable values
  1478. typename point_t; where PointNDConcept<point_t>; // N-dimensional point
  1479. typename locator; where RandomAccessNDLocatorConcept<locator>; // N-dimensional locator.
  1480. typename iterator; where RandomAccessTraversalConcept<iterator>; // 1-dimensional iterator over all values
  1481. typename reverse_iterator; where RandomAccessTraversalConcept<reverse_iterator>;
  1482. typename size_type; // the return value of size()
  1483. // Equivalent to RandomAccessNDLocatorConcept::axis
  1484. template <size_t D> struct axis {
  1485. typename coord_t = point_t::axis<D>::coord_t;
  1486. typename iterator; where RandomAccessTraversalConcept<iterator>; // iterator along D-th axis.
  1487. where SameType<coord_t, iterator::difference_type>;
  1488. where SameType<iterator::value_type,value_type>;
  1489. };
  1490. // Defines the type of a view similar to this type, except it invokes Deref upon dereferencing
  1491. template <PixelDereferenceAdaptorConcept Deref> struct add_deref {
  1492. typename type; where RandomAccessNDImageViewConcept<type>;
  1493. static type make(const View& v, const Deref& deref);
  1494. };
  1495. static const size_t num_dimensions = point_t::num_dimensions;
  1496. // Create from a locator at the top-left corner and dimensions
  1497. View::View(const locator&, const point_type&);
  1498. size_type View::size() const; // total number of elements
  1499. reference operator[](View, const difference_type&) const; // 1-dimensional reference
  1500. iterator View::begin() const;
  1501. iterator View::end() const;
  1502. reverse_iterator View::rbegin() const;
  1503. reverse_iterator View::rend() const;
  1504. iterator View::at(const point_t&);
  1505. point_t View::dimensions() const; // number of elements along each dimension
  1506. bool View::is_1d_traversable() const; // can an iterator over the first dimension visit each value? I.e. are there gaps between values?
  1507. // iterator along a given dimension starting at a given point
  1508. template <size_t D> View::axis<D>::iterator View::axis_iterator(const point_t&) const;
  1509. reference operator()(View,const point_t&) const;
  1510. };
  1511. \endcode
  1512. */
  1513. template <typename View>
  1514. struct RandomAccessNDImageViewConcept {
  1515. void constraints() {
  1516. gil_function_requires< Regular<View> >();
  1517. typedef typename View::value_type value_type;
  1518. typedef typename View::reference reference; // result of dereferencing
  1519. typedef typename View::difference_type difference_type; // result of operator-(1d_iterator,1d_iterator)
  1520. typedef typename View::const_t const_t; // same as this type, but over const values
  1521. typedef typename View::point_t point_t; // N-dimensional point
  1522. typedef typename View::locator locator; // N-dimensional locator
  1523. typedef typename View::iterator iterator;
  1524. typedef typename View::reverse_iterator reverse_iterator;
  1525. typedef typename View::size_type size_type;
  1526. static const std::size_t N=View::num_dimensions;
  1527. gil_function_requires<RandomAccessNDLocatorConcept<locator> >();
  1528. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<iterator> >();
  1529. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<reverse_iterator> >();
  1530. typedef typename View::template axis<0>::iterator first_it_type;
  1531. typedef typename View::template axis<N-1>::iterator last_it_type;
  1532. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<first_it_type> >();
  1533. gil_function_requires<boost_concepts::RandomAccessTraversalConcept<last_it_type> >();
  1534. // BOOST_STATIC_ASSERT((typename std::iterator_traits<first_it_type>::difference_type, typename point_t::template axis<0>::coord_t>::value));
  1535. // BOOST_STATIC_ASSERT((typename std::iterator_traits< last_it_type>::difference_type, typename point_t::template axis<N-1>::coord_t>::value));
  1536. // point_t must be an N-dimensional point, each dimension of which must have the same type as difference_type of the corresponding iterator
  1537. gil_function_requires<PointNDConcept<point_t> >();
  1538. BOOST_STATIC_ASSERT(point_t::num_dimensions==N);
  1539. BOOST_STATIC_ASSERT((is_same<typename std::iterator_traits<first_it_type>::difference_type, typename point_t::template axis<0>::coord_t>::value));
  1540. BOOST_STATIC_ASSERT((is_same<typename std::iterator_traits<last_it_type>::difference_type, typename point_t::template axis<N-1>::coord_t>::value));
  1541. point_t p;
  1542. locator lc;
  1543. iterator it;
  1544. reverse_iterator rit;
  1545. difference_type d; detail::initialize_it(d); ignore_unused_variable_warning(d);
  1546. View(p,lc); // view must be constructible from a locator and a point
  1547. p=view.dimensions();
  1548. lc=view.pixels();
  1549. size_type sz=view.size(); ignore_unused_variable_warning(sz);
  1550. bool is_contiguous=view.is_1d_traversable(); ignore_unused_variable_warning(is_contiguous);
  1551. it=view.begin();
  1552. it=view.end();
  1553. rit=view.rbegin();
  1554. rit=view.rend();
  1555. reference r1=view[d]; ignore_unused_variable_warning(r1); // 1D access
  1556. reference r2=view(p); ignore_unused_variable_warning(r2); // 2D access
  1557. // get 1-D iterator of any dimension at a given pixel location
  1558. first_it_type fi=view.template axis_iterator<0>(p); ignore_unused_variable_warning(fi);
  1559. last_it_type li=view.template axis_iterator<N-1>(p); ignore_unused_variable_warning(li);
  1560. typedef PixelDereferenceAdaptorArchetype<typename View::value_type> deref_t;
  1561. typedef typename View::template add_deref<deref_t>::type dtype;
  1562. }
  1563. View view;
  1564. };
  1565. /// \ingroup ImageView2DConcept
  1566. /// \brief 2-dimensional view over immutable values
  1567. /**
  1568. \code
  1569. concept RandomAccess2DImageViewConcept<RandomAccessNDImageViewConcept View> {
  1570. where num_dimensions==2;
  1571. typename x_iterator = axis<0>::iterator;
  1572. typename y_iterator = axis<1>::iterator;
  1573. typename x_coord_t = axis<0>::coord_t;
  1574. typename y_coord_t = axis<1>::coord_t;
  1575. typename xy_locator = locator;
  1576. x_coord_t View::width() const;
  1577. y_coord_t View::height() const;
  1578. // X-navigation
  1579. x_iterator View::x_at(const point_t&) const;
  1580. x_iterator View::row_begin(y_coord_t) const;
  1581. x_iterator View::row_end (y_coord_t) const;
  1582. // Y-navigation
  1583. y_iterator View::y_at(const point_t&) const;
  1584. y_iterator View::col_begin(x_coord_t) const;
  1585. y_iterator View::col_end (x_coord_t) const;
  1586. // navigating in 2D
  1587. xy_locator View::xy_at(const point_t&) const;
  1588. // (x,y) versions of all methods taking point_t
  1589. View::View(x_coord_t,y_coord_t,const locator&);
  1590. iterator View::at(x_coord_t,y_coord_t) const;
  1591. reference operator()(View,x_coord_t,y_coord_t) const;
  1592. xy_locator View::xy_at(x_coord_t,y_coord_t) const;
  1593. x_iterator View::x_at(x_coord_t,y_coord_t) const;
  1594. y_iterator View::y_at(x_coord_t,y_coord_t) const;
  1595. };
  1596. \endcode
  1597. */
  1598. template <typename View>
  1599. struct RandomAccess2DImageViewConcept {
  1600. void constraints() {
  1601. gil_function_requires<RandomAccessNDImageViewConcept<View> >();
  1602. BOOST_STATIC_ASSERT(View::num_dimensions==2);
  1603. // TODO: This executes the requirements for RandomAccessNDLocatorConcept again. Fix it to improve compile time
  1604. gil_function_requires<RandomAccess2DLocatorConcept<typename View::locator> >();
  1605. typedef typename dynamic_x_step_type<View>::type dynamic_x_step_t;
  1606. typedef typename dynamic_y_step_type<View>::type dynamic_y_step_t;
  1607. typedef typename transposed_type<View>::type transposed_t;
  1608. typedef typename View::x_iterator x_iterator;
  1609. typedef typename View::y_iterator y_iterator;
  1610. typedef typename View::x_coord_t x_coord_t;
  1611. typedef typename View::y_coord_t y_coord_t;
  1612. typedef typename View::xy_locator xy_locator;
  1613. x_coord_t xd=0; ignore_unused_variable_warning(xd);
  1614. y_coord_t yd=0; ignore_unused_variable_warning(yd);
  1615. x_iterator xit;
  1616. y_iterator yit;
  1617. typename View::point_t d;
  1618. View(xd,yd,xy_locator()); // constructible with width, height, 2d_locator
  1619. xy_locator lc=view.xy_at(xd,yd);
  1620. lc=view.xy_at(d);
  1621. typename View::reference r=view(xd,yd); ignore_unused_variable_warning(r);
  1622. xd=view.width();
  1623. yd=view.height();
  1624. xit=view.x_at(d);
  1625. xit=view.x_at(xd,yd);
  1626. xit=view.row_begin(xd);
  1627. xit=view.row_end(xd);
  1628. yit=view.y_at(d);
  1629. yit=view.y_at(xd,yd);
  1630. yit=view.col_begin(xd);
  1631. yit=view.col_end(xd);
  1632. }
  1633. View view;
  1634. };
  1635. /// \ingroup PixelImageViewConcept
  1636. /// \brief GIL's 2-dimensional view over immutable GIL pixels
  1637. /**
  1638. \code
  1639. concept ImageViewConcept<RandomAccess2DImageViewConcept View> {
  1640. where PixelValueConcept<value_type>;
  1641. where PixelIteratorConcept<x_iterator>;
  1642. where PixelIteratorConcept<y_iterator>;
  1643. where x_coord_t == y_coord_t;
  1644. typename coord_t = x_coord_t;
  1645. std::size_t View::num_channels() const;
  1646. };
  1647. \endcode
  1648. */
  1649. template <typename View>
  1650. struct ImageViewConcept {
  1651. void constraints() {
  1652. gil_function_requires<RandomAccess2DImageViewConcept<View> >();
  1653. // TODO: This executes the requirements for RandomAccess2DLocatorConcept again. Fix it to improve compile time
  1654. gil_function_requires<PixelLocatorConcept<typename View::xy_locator> >();
  1655. BOOST_STATIC_ASSERT((is_same<typename View::x_coord_t, typename View::y_coord_t>::value));
  1656. typedef typename View::coord_t coord_t; // 1D difference type (same for all dimensions)
  1657. std::size_t num_chan = view.num_channels(); ignore_unused_variable_warning(num_chan);
  1658. }
  1659. View view;
  1660. };
  1661. namespace detail {
  1662. template <typename View> // Preconditions: View Models RandomAccessNDImageViewConcept
  1663. struct RandomAccessNDImageViewIsMutableConcept {
  1664. void constraints() {
  1665. gil_function_requires<detail::RandomAccessNDLocatorIsMutableConcept<typename View::locator> >();
  1666. gil_function_requires<detail::RandomAccessIteratorIsMutableConcept<typename View::iterator> >();
  1667. gil_function_requires<detail::RandomAccessIteratorIsMutableConcept<typename View::reverse_iterator> >();
  1668. gil_function_requires<detail::RandomAccessIteratorIsMutableConcept<typename View::template axis<0>::iterator> >();
  1669. gil_function_requires<detail::RandomAccessIteratorIsMutableConcept<typename View::template axis<View::num_dimensions-1>::iterator> >();
  1670. typename View::difference_type diff; initialize_it(diff); ignore_unused_variable_warning(diff);
  1671. typename View::point_t pt;
  1672. typename View::value_type v; initialize_it(v);
  1673. view[diff]=v;
  1674. view(pt)=v;
  1675. }
  1676. View view;
  1677. };
  1678. template <typename View> // preconditions: View Models RandomAccessNDImageViewConcept
  1679. struct RandomAccess2DImageViewIsMutableConcept {
  1680. void constraints() {
  1681. gil_function_requires<detail::RandomAccessNDImageViewIsMutableConcept<View> >();
  1682. typename View::x_coord_t xd=0; ignore_unused_variable_warning(xd);
  1683. typename View::y_coord_t yd=0; ignore_unused_variable_warning(yd);
  1684. typename View::value_type v; initialize_it(v);
  1685. view(xd,yd)=v;
  1686. }
  1687. View view;
  1688. };
  1689. template <typename View> // preconditions: View Models ImageViewConcept
  1690. struct PixelImageViewIsMutableConcept {
  1691. void constraints() {
  1692. gil_function_requires<detail::RandomAccess2DImageViewIsMutableConcept<View> >();
  1693. }
  1694. };
  1695. }
  1696. /// \ingroup ImageViewNDConcept
  1697. /// \brief N-dimensional view over mutable values
  1698. /**
  1699. \code
  1700. concept MutableRandomAccessNDImageViewConcept<RandomAccessNDImageViewConcept View> {
  1701. where Mutable<reference>;
  1702. };
  1703. \endcode
  1704. */
  1705. template <typename View>
  1706. struct MutableRandomAccessNDImageViewConcept {
  1707. void constraints() {
  1708. gil_function_requires<RandomAccessNDImageViewConcept<View> >();
  1709. gil_function_requires<detail::RandomAccessNDImageViewIsMutableConcept<View> >();
  1710. }
  1711. };
  1712. /// \ingroup ImageView2DConcept
  1713. /// \brief 2-dimensional view over mutable values
  1714. /**
  1715. \code
  1716. concept MutableRandomAccess2DImageViewConcept<RandomAccess2DImageViewConcept View> : MutableRandomAccessNDImageViewConcept<View> {};
  1717. \endcode
  1718. */
  1719. template <typename View>
  1720. struct MutableRandomAccess2DImageViewConcept {
  1721. void constraints() {
  1722. gil_function_requires<RandomAccess2DImageViewConcept<View> >();
  1723. gil_function_requires<detail::RandomAccess2DImageViewIsMutableConcept<View> >();
  1724. }
  1725. };
  1726. /// \ingroup PixelImageViewConcept
  1727. /// \brief GIL's 2-dimensional view over mutable GIL pixels
  1728. /**
  1729. \code
  1730. concept MutableImageViewConcept<ImageViewConcept View> : MutableRandomAccess2DImageViewConcept<View> {};
  1731. \endcode
  1732. */
  1733. template <typename View>
  1734. struct MutableImageViewConcept {
  1735. void constraints() {
  1736. gil_function_requires<ImageViewConcept<View> >();
  1737. gil_function_requires<detail::PixelImageViewIsMutableConcept<View> >();
  1738. }
  1739. };
  1740. /// \brief Returns whether two views are compatible
  1741. ///
  1742. /// Views are compatible if their pixels are compatible. Compatible views can be assigned and copy constructed from one another.
  1743. template <typename V1, typename V2> // Model ImageViewConcept
  1744. struct views_are_compatible : public pixels_are_compatible<typename V1::value_type, typename V2::value_type> {};
  1745. /// \brief Views are compatible if they have the same color spaces and compatible channel values. Constness and layout are not important for compatibility
  1746. /// \ingroup ImageViewConcept
  1747. /**
  1748. \code
  1749. concept ViewsCompatibleConcept<ImageViewConcept V1, ImageViewConcept V2> {
  1750. where PixelsCompatibleConcept<V1::value_type, P2::value_type>;
  1751. };
  1752. \endcode
  1753. */
  1754. template <typename V1, typename V2>
  1755. struct ViewsCompatibleConcept {
  1756. void constraints() {
  1757. BOOST_STATIC_ASSERT((views_are_compatible<V1,V2>::value));
  1758. }
  1759. };
  1760. ////////////////////////////////////////////////////////////////////////////////////////
  1761. ///
  1762. /// IMAGE CONCEPTS
  1763. ///
  1764. ////////////////////////////////////////////////////////////////////////////////////////
  1765. /// \ingroup ImageConcept
  1766. /// \brief N-dimensional container of values
  1767. /**
  1768. \code
  1769. concept RandomAccessNDImageConcept<typename Img> : Regular<Img> {
  1770. typename view_t; where MutableRandomAccessNDImageViewConcept<view_t>;
  1771. typename const_view_t = view_t::const_t;
  1772. typename point_t = view_t::point_t;
  1773. typename value_type = view_t::value_type;
  1774. typename allocator_type;
  1775. Img::Img(point_t dims, std::size_t alignment=1);
  1776. Img::Img(point_t dims, value_type fill_value, std::size_t alignment);
  1777. void Img::recreate(point_t new_dims, std::size_t alignment=1);
  1778. void Img::recreate(point_t new_dims, value_type fill_value, std::size_t alignment);
  1779. const point_t& Img::dimensions() const;
  1780. const const_view_t& const_view(const Img&);
  1781. const view_t& view(Img&);
  1782. };
  1783. \endcode
  1784. */
  1785. template <typename Img>
  1786. struct RandomAccessNDImageConcept {
  1787. void constraints() {
  1788. gil_function_requires<Regular<Img> >();
  1789. typedef typename Img::view_t view_t;
  1790. gil_function_requires<MutableRandomAccessNDImageViewConcept<view_t> >();
  1791. typedef typename Img::const_view_t const_view_t;
  1792. typedef typename Img::value_type pixel_t;
  1793. typedef typename Img::point_t point_t;
  1794. gil_function_requires<PointNDConcept<point_t> >();
  1795. const_view_t cv = const_view(img); ignore_unused_variable_warning(cv);
  1796. view_t v = view(img); ignore_unused_variable_warning(v);
  1797. pixel_t fill_value;
  1798. point_t pt=img.dimensions();
  1799. Img im1(pt);
  1800. Img im2(pt,1);
  1801. Img im3(pt,fill_value,1);
  1802. img.recreate(pt);
  1803. img.recreate(pt,1);
  1804. img.recreate(pt,fill_value,1);
  1805. }
  1806. Img img;
  1807. };
  1808. /// \ingroup ImageConcept
  1809. /// \brief 2-dimensional container of values
  1810. /**
  1811. \code
  1812. concept RandomAccess2DImageConcept<RandomAccessNDImageConcept Img> {
  1813. typename x_coord_t = const_view_t::x_coord_t;
  1814. typename y_coord_t = const_view_t::y_coord_t;
  1815. Img::Img(x_coord_t width, y_coord_t height, std::size_t alignment=1);
  1816. Img::Img(x_coord_t width, y_coord_t height, value_type fill_value, std::size_t alignment);
  1817. x_coord_t Img::width() const;
  1818. y_coord_t Img::height() const;
  1819. void Img::recreate(x_coord_t width, y_coord_t height, std::size_t alignment=1);
  1820. void Img::recreate(x_coord_t width, y_coord_t height, value_type fill_value, std::size_t alignment);
  1821. };
  1822. \endcode
  1823. */
  1824. template <typename Img>
  1825. struct RandomAccess2DImageConcept {
  1826. void constraints() {
  1827. gil_function_requires<RandomAccessNDImageConcept<Img> >();
  1828. typedef typename Img::x_coord_t x_coord_t;
  1829. typedef typename Img::y_coord_t y_coord_t;
  1830. typedef typename Img::value_type value_t;
  1831. gil_function_requires<MutableRandomAccess2DImageViewConcept<typename Img::view_t> >();
  1832. x_coord_t w=img.width();
  1833. y_coord_t h=img.height();
  1834. value_t fill_value;
  1835. Img im1(w,h);
  1836. Img im2(w,h,1);
  1837. Img im3(w,h,fill_value,1);
  1838. img.recreate(w,h);
  1839. img.recreate(w,h,1);
  1840. img.recreate(w,h,fill_value,1);
  1841. }
  1842. Img img;
  1843. };
  1844. /// \ingroup ImageConcept
  1845. /// \brief 2-dimensional image whose value type models PixelValueConcept
  1846. /**
  1847. \code
  1848. concept ImageConcept<RandomAccess2DImageConcept Img> {
  1849. where MutableImageViewConcept<view_t>;
  1850. typename coord_t = view_t::coord_t;
  1851. };
  1852. \endcode
  1853. */
  1854. template <typename Img>
  1855. struct ImageConcept {
  1856. void constraints() {
  1857. gil_function_requires<RandomAccess2DImageConcept<Img> >();
  1858. gil_function_requires<MutableImageViewConcept<typename Img::view_t> >();
  1859. typedef typename Img::coord_t coord_t;
  1860. BOOST_STATIC_ASSERT(num_channels<Img>::value == mpl::size<typename color_space_type<Img>::type>::value);
  1861. BOOST_STATIC_ASSERT((is_same<coord_t, typename Img::x_coord_t>::value));
  1862. BOOST_STATIC_ASSERT((is_same<coord_t, typename Img::y_coord_t>::value));
  1863. }
  1864. Img img;
  1865. };
  1866. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  1867. #pragma warning(pop)
  1868. #endif
  1869. #if defined(__GNUC__) && (__GNUC__ >= 4)
  1870. #pragma GCC diagnostic pop
  1871. #endif
  1872. } } // namespace boost::gil
  1873. #endif