metafunctions.rst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. Metafunctions
  2. =============
  3. .. contents::
  4. :local:
  5. :depth: 2
  6. Overview
  7. --------
  8. Flexibility comes at a price. GIL types can be very long and hard to read.
  9. To address this problem, GIL provides typedefs to refer to any standard image,
  10. pixel iterator, pixel locator, pixel reference or pixel value.
  11. They follow this pattern::
  12. *ColorSpace* + *BitDepth* + ["s|f"] + ["c"] + ["_planar"] + ["_step"] + *ClassType* + "_t"
  13. where *ColorSpace* also indicates the ordering of components.
  14. Examples are ``rgb``, ``bgr``, ``cmyk``, ``rgba``. *BitDepth* can be, for
  15. example, ``8``,``16``,``32``. By default the bits are unsigned integral type.
  16. Append ``s`` to the bit depth to indicate signed integral, or ``f`` to
  17. indicate floating point. ``c`` indicates object whose associated pixel
  18. reference is immutable. ``_planar`` indicates planar organization (as opposed
  19. to interleaved). ``_step`` indicates the type has a dynamic step and
  20. *ClassType* is ``_image`` (image, using a standard allocator), ``_view``
  21. (image view), ``_loc`` (pixel locator), ``_ptr`` (pixel iterator), ``_ref``
  22. (pixel reference), ``_pixel`` (pixel value).
  23. Here are examples:
  24. .. code-block:: cpp
  25. bgr8_image_t i; // 8-bit unsigned (unsigned char) interleaved BGR image
  26. cmyk16_pixel_t; x; // 16-bit unsigned (unsigned short) CMYK pixel value;
  27. cmyk16sc_planar_ref_t p(x); // const reference to a 16-bit signed integral (signed short) planar CMYK pixel x.
  28. rgb32f_planar_step_ptr_t ii; // step iterator to a floating point 32-bit (float) planar RGB pixel.
  29. Homogeneous memory-based images
  30. -------------------------------
  31. GIL provides the metafunctions that return the types of standard
  32. homogeneous memory-based GIL constructs given a channel type, a
  33. layout, and whether the construct is planar, has a step along the X
  34. direction, and is mutable:
  35. .. code-block:: cpp
  36. template <typename ChannelValue, typename Layout, bool IsPlanar=false, bool IsMutable=true>
  37. struct pixel_reference_type { typedef ... type; };
  38. template <typename Channel, typename Layout>
  39. struct pixel_value_type { typedef ... type; };
  40. template <typename ChannelValue, typename Layout, bool IsPlanar=false, bool IsStep=false, bool IsMutable=true>
  41. struct iterator_type { typedef ... type; };
  42. template <typename ChannelValue, typename Layout, bool IsPlanar=false, bool IsXStep=false, bool IsMutable=true>
  43. struct locator_type { typedef ... type; };
  44. template <typename ChannelValue, typename Layout, bool IsPlanar=false, bool IsXStep=false, bool IsMutable=true>
  45. struct view_type { typedef ... type; };
  46. template <typename ChannelValue, typename Layout, bool IsPlanar=false, typename Alloc=std::allocator<unsigned char> >
  47. struct image_type { typedef ... type; };
  48. template <typename BitField, typename ChannelBitSizeVector, typename Layout, typename Alloc=std::allocator<unsigned char> >
  49. struct packed_image_type { typedef ... type; };
  50. template <typename ChannelBitSizeVector, typename Layout, typename Alloc=std::allocator<unsigned char> >
  51. struct bit_aligned_image_type { typedef ... type; };
  52. Packed and bit-aligned images
  53. -----------------------------
  54. There are also helper metafunctions to construct packed and
  55. bit-aligned images with up to five channels:
  56. .. code-block:: cpp
  57. template <typename BitField, unsigned Size1,
  58. typename Layout, typename Alloc=std::allocator<unsigned char> >
  59. struct packed_image1_type { typedef ... type; };
  60. template <typename BitField, unsigned Size1, unsigned Size2,
  61. typename Layout, typename Alloc=std::allocator<unsigned char> >
  62. struct packed_image2_type { typedef ... type; };
  63. template <typename BitField, unsigned Size1, unsigned Size2, unsigned Size3,
  64. typename Layout, typename Alloc=std::allocator<unsigned char> >
  65. struct packed_image3_type { typedef ... type; };
  66. template <typename BitField, unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4,
  67. typename Layout, typename Alloc=std::allocator<unsigned char> >
  68. struct packed_image4_type { typedef ... type; };
  69. template <typename BitField, unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4, unsigned Size5,
  70. typename Layout, typename Alloc=std::allocator<unsigned char> >
  71. struct packed_image5_type { typedef ... type; };
  72. template <unsigned Size1,
  73. typename Layout, typename Alloc=std::allocator<unsigned char> >
  74. struct bit_aligned_image1_type { typedef ... type; };
  75. template <unsigned Size1, unsigned Size2,
  76. typename Layout, typename Alloc=std::allocator<unsigned char> >
  77. struct bit_aligned_image2_type { typedef ... type; };
  78. template <unsigned Size1, unsigned Size2, unsigned Size3,
  79. typename Layout, typename Alloc=std::allocator<unsigned char> >
  80. struct bit_aligned_image3_type { typedef ... type; };
  81. template <unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4,
  82. typename Layout, typename Alloc=std::allocator<unsigned char> >
  83. struct bit_aligned_image4_type { typedef ... type; };
  84. template <unsigned Size1, unsigned Size2, unsigned Size3, unsigned Size4, unsigned Size5,
  85. typename Layout, typename Alloc=std::allocator<unsigned char> >
  86. struct bit_aligned_image5_type { typedef ... type; };
  87. Iterators and views
  88. -------------------
  89. Here ``ChannelValue`` models ``ChannelValueConcept``. We don't need
  90. ``IsYStep`` because GIL's memory-based locator and view already allow
  91. the vertical step to be specified dynamically. Iterators and views can
  92. be constructed from a pixel type:
  93. .. code-block:: cpp
  94. template <typename Pixel, bool IsPlanar=false, bool IsStep=false, bool IsMutable=true>
  95. struct iterator_type_from_pixel { typedef ... type; };
  96. template <typename Pixel, bool IsPlanar=false, bool IsStepX=false, bool IsMutable=true>
  97. struct view_type_from_pixel { typedef ... type; };
  98. Using a heterogeneous pixel type will result in heterogeneous iterators and
  99. views. Types can also be constructed from horizontal iterator:
  100. .. code-block:: cpp
  101. template <typename XIterator>
  102. struct type_from_x_iterator
  103. {
  104. typedef ... step_iterator_t;
  105. typedef ... xy_locator_t;
  106. typedef ... view_t;
  107. };
  108. Pixel components
  109. ----------------
  110. You can get pixel-related types of any pixel-based GIL constructs (pixels,
  111. iterators, locators and views) using the following metafunctions provided by
  112. ``PixelBasedConcept``, ``HomogeneousPixelBasedConcept`` and metafunctions
  113. built on top of them:
  114. .. code-block:: cpp
  115. template <typename T> struct color_space_type { typedef ... type; };
  116. template <typename T> struct channel_mapping_type { typedef ... type; };
  117. template <typename T> struct is_planar { typedef ... type; };
  118. // Defined by homogeneous constructs
  119. template <typename T> struct channel_type { typedef ... type; };
  120. template <typename T> struct num_channels { typedef ... type; };
  121. Deriving and manipulating existing types
  122. ----------------------------------------
  123. There are metafunctions to construct the type of a construct from an existing
  124. type by changing one or more of its properties:
  125. .. code-block:: cpp
  126. template <typename PixelReference,
  127. typename ChannelValue, typename Layout, typename IsPlanar, typename IsMutable>
  128. struct derived_pixel_reference_type
  129. {
  130. typedef ... type; // Models PixelConcept
  131. };
  132. template <typename Iterator,
  133. typename ChannelValue, typename Layout, typename IsPlanar, typename IsStep, typename IsMutable>
  134. struct derived_iterator_type
  135. {
  136. typedef ... type; // Models PixelIteratorConcept
  137. };
  138. template <typename View,
  139. typename ChannelValue, typename Layout, typename IsPlanar, typename IsXStep, typename IsMutable>
  140. struct derived_view_type
  141. {
  142. typedef ... type; // Models ImageViewConcept
  143. };
  144. template <typename Image,
  145. typename ChannelValue, typename Layout, typename IsPlanar>
  146. struct derived_image_type
  147. {
  148. typedef ... type; // Models ImageConcept
  149. };
  150. You can replace one or more of its properties and use ``boost::use_default``
  151. for the rest. In this case ``IsPlanar``, ``IsStep`` and ``IsMutable`` are
  152. MPL boolean constants. For example, here is how to create the type of a view
  153. just like ``View``, but being grayscale and planar:
  154. .. code-block:: cpp
  155. using VT = typename derived_view_type<View, boost::use_default, gray_t, mpl::true_>::type;
  156. Type traits
  157. -----------
  158. These are metafunctions, some of which return integral types which can be
  159. evaluated like this:
  160. .. code-block:: cpp
  161. static_assert(is_planar<rgb8_planar_view_t>::value == true, "");
  162. GIL also supports type analysis metafunctions of the form:
  163. .. code-block:: cpp
  164. [pixel_reference/iterator/locator/view/image] + "_is_" + [basic/mutable/step]
  165. For example:
  166. .. code-block:: cpp
  167. if (view_is_mutable<View>::value)
  168. {
  169. ...
  170. }
  171. A *basic* GIL construct is a memory-based construct that uses the built-in GIL
  172. classes and does not have any function object to invoke upon dereferencing.
  173. For example, a simple planar or interleaved, step or non-step RGB image view
  174. is basic, but a color converted view or a virtual view is not.