blob: 2fcbdb032c167f661aa2d9c81091639fd378ad72 [file] [log] [blame]
<!-- Copyright 2008 Lubomir Bourdev and Hailin Jin
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
-->
<!--
Copyright 2005-2007 Adobe Systems Incorporated
Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
or a copy at http://stlab.adobe.com/licenses.html)
Some files are held under additional license.
Please see "http://stlab.adobe.com/licenses.html" for more information.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<TITLE>Generic Image Library: iterator_from_2d.hpp Source File</TITLE>
<META HTTP-EQUIV="content-type" CONTENT="text/html;charset=ISO-8859-1"/>
<LINK TYPE="text/css" REL="stylesheet" HREF="adobe_source.css"/>
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0" style='width: 100%; margin: 0; padding: 0'><tr>
<td width="100%" valign="top" style='padding-left: 10px; padding-right: 10px; padding-bottom: 10px'>
<div class="qindex"><a class="qindex" href="index.html">Modules</a>
| <a class="qindex" href="classes.html">Alphabetical List</a>
| <a class="qindex" href="annotated.html">Class List</a>
| <a class="qindex" href="dirs.html">Directories</a>
| <a class="qindex" href="files.html">File List</a>
| <a class="qindex" href="../index.html">GIL Home Page</a>
</div>
<!-- End Header -->
<!-- Generated by Doxygen 1.5.6 -->
<div class="navpath"><a class="el" href="g_i_l_0076.html">boost</a>&nbsp;&raquo&nbsp;<a class="el" href="g_i_l_0079.html">gil</a>
</div>
<div class="contents">
<h1>iterator_from_2d.hpp</h1><a href="g_i_l_0230.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> Copyright 2005-2007 Adobe Systems Incorporated</span>
<a name="l00003"></a>00003 <span class="comment"> </span>
<a name="l00004"></a>00004 <span class="comment"> Use, modification and distribution are subject to the Boost Software License,</span>
<a name="l00005"></a>00005 <span class="comment"> Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at</span>
<a name="l00006"></a>00006 <span class="comment"> http://www.boost.org/LICENSE_1_0.txt).</span>
<a name="l00007"></a>00007 <span class="comment"></span>
<a name="l00008"></a>00008 <span class="comment"> See http://opensource.adobe.com/gil for most recent version including documentation.</span>
<a name="l00009"></a>00009 <span class="comment">*/</span>
<a name="l00010"></a>00010 <span class="comment">/*************************************************************************************************/</span>
<a name="l00011"></a>00011
<a name="l00012"></a>00012 <span class="preprocessor">#ifndef GIL_ITERATOR_FROM_2D_H</span>
<a name="l00013"></a>00013 <span class="preprocessor"></span><span class="preprocessor">#define GIL_ITERATOR_FROM_2D_H</span>
<a name="l00014"></a>00014 <span class="preprocessor"></span>
<a name="l00023"></a>00023
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;cassert&gt;</span>
<a name="l00025"></a>00025 <span class="preprocessor">#include &lt;boost/iterator/iterator_facade.hpp&gt;</span>
<a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="g_i_l_0092.html" title="Concept check classes for GIL concepts.">gil_concept.hpp</a>"</span>
<a name="l00027"></a>00027 <span class="preprocessor">#include "<a class="code" href="g_i_l_0094.html" title="GIL configuration file.">gil_config.hpp</a>"</span>
<a name="l00028"></a>00028 <span class="preprocessor">#include "<a class="code" href="g_i_l_0243.html" title="pixel iterator support">pixel_iterator.hpp</a>"</span>
<a name="l00029"></a>00029 <span class="preprocessor">#include "<a class="code" href="locator_8hpp.html" title="pixel 2D locator">locator.hpp</a>"</span>
<a name="l00030"></a>00030
<a name="l00031"></a>00031 <span class="keyword">namespace </span>boost { <span class="keyword">namespace </span>gil {
<a name="l00032"></a>00032
<a name="l00038"></a>00038
<a name="l00039"></a>00039
<a name="l00043"></a>00043
<a name="l00044"></a>00044
<a name="l00050"></a>00050
<a name="l00051"></a>00051 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Loc2&gt; <span class="comment">// Models PixelLocatorConcept</span>
<a name="l00052"></a><a class="code" href="g_i_l_0042.html">00052</a> <span class="keyword">class </span><a class="code" href="g_i_l_0042.html" title="Provides 1D random-access navigation to the pixels of the image. Models: PixelIteratorConcept...">iterator_from_2d</a> : <span class="keyword">public</span> iterator_facade&lt;iterator_from_2d&lt;Loc2&gt;,
<a name="l00053"></a>00053 typename Loc2::value_type,
<a name="l00054"></a>00054 random_access_traversal_tag,
<a name="l00055"></a>00055 typename Loc2::reference,
<a name="l00056"></a>00056 typename Loc2::coord_t&gt; {
<a name="l00057"></a>00057 GIL_CLASS_REQUIRE(Loc2, boost::gil, <a class="code" href="g_i_l_0354.html" title="GIL&amp;#39;s 2-dimensional locator over immutable GIL pixels.">PixelLocatorConcept</a>)
<a name="l00058"></a>00058 <span class="keyword">public</span>:
<a name="l00059"></a>00059 <span class="keyword">typedef</span> iterator_facade&lt;iterator_from_2d&lt;Loc2&gt;,
<a name="l00060"></a>00060 <span class="keyword">typename</span> Loc2::value_type,
<a name="l00061"></a>00061 random_access_traversal_tag,
<a name="l00062"></a>00062 <span class="keyword">typename</span> Loc2::reference,
<a name="l00063"></a>00063 <span class="keyword">typename</span> Loc2::coord_t&gt; parent_t;
<a name="l00064"></a>00064 <span class="keyword">typedef</span> <span class="keyword">typename</span> parent_t::reference reference;
<a name="l00065"></a>00065 <span class="keyword">typedef</span> <span class="keyword">typename</span> parent_t::difference_type difference_type;
<a name="l00066"></a>00066 <span class="keyword">typedef</span> <span class="keyword">typename</span> Loc2::x_iterator x_iterator;
<a name="l00067"></a>00067 <span class="keyword">typedef</span> <span class="keyword">typename</span> Loc2::point_t point_t;
<a name="l00068"></a>00068
<a name="l00069"></a>00069 std::ptrdiff_t width()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> _width; } <span class="comment">// number of pixels per image row</span>
<a name="l00070"></a>00070 std::ptrdiff_t x_pos()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> _coords.<a class="code" href="g_i_l_0060.html#9a4f74af87a76a4c3dcb729cb0e68f8d">x</a>; } <span class="comment">// current x position</span>
<a name="l00071"></a>00071 std::ptrdiff_t y_pos()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> _coords.<a class="code" href="g_i_l_0060.html#1cb2b5ea04251d543e49356ef54eb853">y</a>; } <span class="comment">// current y position</span>
<a name="l00072"></a>00072
<a name="l00075"></a><a class="code" href="g_i_l_0042.html#de1279c4ed222b6efb0867c038765ea1">00075</a> reference <a class="code" href="g_i_l_0042.html#de1279c4ed222b6efb0867c038765ea1">operator[]</a>(difference_type d)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> *(*<span class="keyword">this</span>+d); }
<a name="l00076"></a>00076
<a name="l00077"></a>00077 <span class="keywordtype">bool</span> is_1d_traversable()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> _p.is_1d_traversable(width()); } <span class="comment">// is there no gap at the end of each row?</span>
<a name="l00078"></a>00078 x_iterator&amp; x() { <span class="keywordflow">return</span> _p.x(); }
<a name="l00079"></a>00079
<a name="l00080"></a>00080 iterator_from_2d(){}
<a name="l00081"></a>00081 iterator_from_2d(<span class="keyword">const</span> Loc2&amp; p, std::ptrdiff_t width, std::ptrdiff_t x=0, std::ptrdiff_t y=0) : _coords(x,y), _width(width), _p(p) {}
<a name="l00082"></a>00082 iterator_from_2d(<span class="keyword">const</span> iterator_from_2d&amp; pit) : _coords(pit._coords), _width(pit._width), _p(pit._p) {}
<a name="l00083"></a>00083 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Loc&gt; iterator_from_2d(<span class="keyword">const</span> iterator_from_2d&lt;Loc&gt;&amp; pit) : _coords(pit._coords), _width(pit._width), _p(pit._p) {}
<a name="l00084"></a>00084
<a name="l00085"></a>00085 <span class="keyword">private</span>:
<a name="l00086"></a>00086 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Loc&gt; <span class="keyword">friend</span> <span class="keyword">class </span>iterator_from_2d;
<a name="l00087"></a>00087 <span class="keyword">friend</span> <span class="keyword">class </span>boost::iterator_core_access;
<a name="l00088"></a>00088 reference dereference()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> *_p; }
<a name="l00089"></a>00089 <span class="keywordtype">void</span> increment() {
<a name="l00090"></a>00090 ++_coords.x;
<a name="l00091"></a>00091 ++_p.x();
<a name="l00092"></a>00092 <span class="keywordflow">if</span> (_coords.x&gt;=_width) {
<a name="l00093"></a>00093 _coords.x=0;
<a name="l00094"></a>00094 ++_coords.y;
<a name="l00095"></a>00095 _p+=point_t(-_width,1);
<a name="l00096"></a>00096 }
<a name="l00097"></a>00097 }
<a name="l00098"></a>00098 <span class="keywordtype">void</span> decrement() {
<a name="l00099"></a>00099 --_coords.x;
<a name="l00100"></a>00100 --_p.x();
<a name="l00101"></a>00101 <span class="keywordflow">if</span> (_coords.x&lt;0) {
<a name="l00102"></a>00102 _coords.x=_width-1;
<a name="l00103"></a>00103 --_coords.y;
<a name="l00104"></a>00104 _p+=point_t(_width,-1);
<a name="l00105"></a>00105 }
<a name="l00106"></a>00106 }
<a name="l00107"></a>00107
<a name="l00108"></a>00108 GIL_FORCEINLINE <span class="keywordtype">void</span> advance(difference_type d) {
<a name="l00109"></a>00109 <span class="keywordflow">if</span> (_width==0) <span class="keywordflow">return</span>; <span class="comment">// unfortunately we need to check for that. Default-constructed images have width of 0 and the code below will throw if executed.</span>
<a name="l00110"></a>00110 point_t delta;
<a name="l00111"></a>00111 <span class="keywordflow">if</span> (_coords.x+d&gt;=0) { <span class="comment">// not going back to a previous row?</span>
<a name="l00112"></a>00112 delta.x=(_coords.x+(std::ptrdiff_t)d)%_width - _coords.x;
<a name="l00113"></a>00113 delta.y=(_coords.x+(std::ptrdiff_t)d)/_width;
<a name="l00114"></a>00114 } <span class="keywordflow">else</span> {
<a name="l00115"></a>00115 delta.x=(_coords.x+(std::ptrdiff_t)d*(1-_width))%_width -_coords.x;
<a name="l00116"></a>00116 delta.y=-(_width-_coords.x-(std::ptrdiff_t)d-1)/_width;
<a name="l00117"></a>00117 }
<a name="l00118"></a>00118 _p+=delta;
<a name="l00119"></a>00119 _coords.x+=delta.x;
<a name="l00120"></a>00120 _coords.y+=delta.y;
<a name="l00121"></a>00121 }
<a name="l00122"></a>00122
<a name="l00123"></a>00123 difference_type distance_to(<span class="keyword">const</span> iterator_from_2d&amp; it)<span class="keyword"> const </span>{
<a name="l00124"></a>00124 <span class="keywordflow">if</span> (_width==0) <span class="keywordflow">return</span> 0;
<a name="l00125"></a>00125 <span class="keywordflow">return</span> (it.y_pos()-_coords.y)*_width + (it.x_pos()-_coords.x);
<a name="l00126"></a>00126 }
<a name="l00127"></a>00127
<a name="l00128"></a>00128 <span class="keywordtype">bool</span> <a class="code" href="g_i_l_0214.html#g4ed3cf71cb0fbf435d1389df8567a4e5" title="std::equal(I1,I1,I2) with I1 and I2 being a iterator_from_2d">equal</a>(<span class="keyword">const</span> iterator_from_2d&amp; it)<span class="keyword"> const </span>{
<a name="l00129"></a>00129 assert(_width==it.width()); <span class="comment">// they must belong to the same image</span>
<a name="l00130"></a>00130 <span class="keywordflow">return</span> _coords==it._coords &amp;&amp; _p==it._p;
<a name="l00131"></a>00131 }
<a name="l00132"></a>00132
<a name="l00133"></a>00133 point2&lt;std::ptrdiff_t&gt; _coords;
<a name="l00134"></a>00134 std::ptrdiff_t _width;
<a name="l00135"></a>00135 Loc2 _p;
<a name="l00136"></a>00136 };
<a name="l00137"></a>00137
<a name="l00138"></a>00138 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Loc&gt; <span class="comment">// Models PixelLocatorConcept</span>
<a name="l00139"></a>00139 <span class="keyword">struct </span>const_iterator_type&lt;iterator_from_2d&lt;Loc&gt; &gt; {
<a name="l00140"></a>00140 <span class="keyword">typedef</span> iterator_from_2d&lt;typename Loc::const_t&gt; type;
<a name="l00141"></a>00141 };
<a name="l00142"></a>00142
<a name="l00143"></a>00143 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Loc&gt; <span class="comment">// Models PixelLocatorConcept</span>
<a name="l00144"></a>00144 <span class="keyword">struct </span>iterator_is_mutable&lt;iterator_from_2d&lt;Loc&gt; &gt; : <span class="keyword">public</span> iterator_is_mutable&lt;typename Loc::x_iterator&gt; {};
<a name="l00145"></a>00145
<a name="l00146"></a>00146
<a name="l00148"></a>00148 <span class="comment">// HasDynamicXStepTypeConcept</span>
<a name="l00150"></a>00150 <span class="comment"></span>
<a name="l00151"></a>00151 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Loc&gt;
<a name="l00152"></a>00152 <span class="keyword">struct </span>dynamic_x_step_type&lt;iterator_from_2d&lt;Loc&gt; &gt; {
<a name="l00153"></a>00153 <span class="keyword">typedef</span> iterator_from_2d&lt;typename dynamic_x_step_type&lt;Loc&gt;::type&gt; type;
<a name="l00154"></a>00154 };
<a name="l00155"></a>00155
<a name="l00156"></a>00156
<a name="l00158"></a>00158 <span class="comment">// PixelBasedConcept</span>
<a name="l00160"></a>00160 <span class="comment"></span>
<a name="l00161"></a>00161 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Loc&gt; <span class="comment">// Models PixelLocatorConcept</span>
<a name="l00162"></a>00162 <span class="keyword">struct </span>color_space_type&lt;iterator_from_2d&lt;Loc&gt; &gt; : <span class="keyword">public</span> color_space_type&lt;Loc&gt; {};
<a name="l00163"></a>00163
<a name="l00164"></a>00164 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Loc&gt; <span class="comment">// Models PixelLocatorConcept</span>
<a name="l00165"></a>00165 <span class="keyword">struct </span>channel_mapping_type&lt;iterator_from_2d&lt;Loc&gt; &gt; : <span class="keyword">public</span> channel_mapping_type&lt;Loc&gt; {};
<a name="l00166"></a>00166
<a name="l00167"></a>00167 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Loc&gt; <span class="comment">// Models PixelLocatorConcept</span>
<a name="l00168"></a>00168 <span class="keyword">struct </span>is_planar&lt;iterator_from_2d&lt;Loc&gt; &gt; : <span class="keyword">public</span> is_planar&lt;Loc&gt; {};
<a name="l00169"></a>00169
<a name="l00170"></a>00170 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> Loc&gt; <span class="comment">// Models PixelLocatorConcept</span>
<a name="l00171"></a>00171 <span class="keyword">struct </span>channel_type&lt;iterator_from_2d&lt;Loc&gt; &gt; : <span class="keyword">public</span> channel_type&lt;Loc&gt; {};
<a name="l00172"></a>00172
<a name="l00173"></a>00173 } } <span class="comment">// namespace boost::gil</span>
<a name="l00174"></a>00174
<a name="l00175"></a>00175 <span class="preprocessor">#endif</span>
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Generated on Sat May 2 13:50:14 2009 for Generic Image Library by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>