lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ImageAlgorithm.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * Support for functors over Image's pixels
27  */
28 #ifndef LSST_AFW_IMAGE_IMAGE_ALGORITHM_H
29 #define LSST_AFW_IMAGE_IMAGE_ALGORITHM_H
30 
31 #include <list>
32 #include <map>
33 #include <string>
34 #include <utility>
35 #include <functional>
36 
37 #include "boost/tr1/functional.hpp"
38 #include "boost/mpl/bool.hpp"
39 #include <memory>
40 
41 #include "lsst/afw/image/lsstGil.h"
43 #include "lsst/daf/base.h"
44 #include "lsst/daf/base/Citizen.h"
45 #include "lsst/pex/exceptions.h"
46 
47 namespace lsst {
48 namespace afw {
49 namespace image {
53 template <typename ValT>
54 struct pixelOp0 : public std::tr1::function<ValT()> {
55  virtual ~pixelOp0() {}
56  virtual ValT operator()() const = 0;
57 };
58 
62 template <typename ValT>
63 struct pixelOp1 : public std::tr1::function<ValT(ValT)> {
64  virtual ~pixelOp1() {}
65  virtual ValT operator()(ValT lhs) const = 0;
66 };
67 
71 template <typename ValT>
72 struct pixelOp1XY : public std::tr1::function<ValT(int, int, ValT)> {
73  virtual ~pixelOp1XY() {}
74  virtual ValT operator()(int x, int y, ValT lhs) const = 0;
75 };
76 
80 template <typename LhsT, typename RhsT>
81 struct pixelOp2 : public std::tr1::function<LhsT(LhsT, RhsT)> {
82  virtual ~pixelOp2() {}
83  virtual LhsT operator()(LhsT lhs, RhsT rhs) const = 0;
84 };
85 
89 template <typename LhsT, typename RhsT>
90 struct pixelOp2XY : public std::tr1::function<LhsT(int, int, LhsT, RhsT)> {
91  virtual ~pixelOp2XY() {}
92  virtual LhsT operator()(int x, int y, LhsT lhs, RhsT rhs) const = 0;
93 };
94 
98 template <typename LhsT>
100  pixelOp0<LhsT> const& func
101  ) {
102  for (int y = 0; y != lhs.getHeight(); ++y) {
103  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
104  lhsPtr != lhsEnd; ++lhsPtr) {
105  *lhsPtr = func();
106  }
107  }
108 }
109 
113 template <typename LhsT>
115  pixelOp1<LhsT> const& func
116  ) {
117  for (int y = 0; y != lhs.getHeight(); ++y) {
118  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
119  lhsPtr != lhsEnd; ++lhsPtr) {
120  *lhsPtr = func(*lhsPtr);
121  }
122  }
123 }
124 
130 template <typename LhsT>
132  pixelOp1XY<LhsT> const& func
133  ) {
134  for (int y = 0; y != lhs.getHeight(); ++y) {
135  int x = lhs.getX0();
136  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
137  lhsPtr != lhsEnd; ++lhsPtr, ++x) {
138  *lhsPtr = func(x, y + lhs.getY0(), *lhsPtr);
139  }
140  }
141 }
142 
146 template <typename LhsT, typename RhsT>
148  Image<RhsT> const& rhs,
149  pixelOp1<RhsT> const& func
150  ) {
151  if (lhs.getDimensions() != rhs.getDimensions()) {
152  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
153  (boost::format("Images are of different size, %dx%d v %dx%d") % lhs.getWidth() %
154  lhs.getHeight() % rhs.getWidth() % rhs.getHeight())
155  .str());
156  }
157 
158  for (int y = 0; y != lhs.getHeight(); ++y) {
159  typename Image<RhsT>::const_x_iterator rhsPtr = rhs.row_begin(y);
160 
161  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
162  lhsPtr != lhsEnd; ++rhsPtr, ++lhsPtr) {
163  *lhsPtr = func(*rhsPtr);
164  }
165  }
166 }
167 
171 template <typename LhsT, typename RhsT>
173  Image<RhsT> const& rhs,
174  pixelOp2<LhsT, RhsT> const& func
175  ) {
176  if (lhs.getDimensions() != rhs.getDimensions()) {
177  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
178  (boost::format("Images are of different size, %dx%d v %dx%d") % lhs.getWidth() %
179  lhs.getHeight() % rhs.getWidth() % rhs.getHeight())
180  .str());
181  }
182 
183  for (int y = 0; y != lhs.getHeight(); ++y) {
184  typename Image<RhsT>::const_x_iterator rhsPtr = rhs.row_begin(y);
185 
186  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
187  lhsPtr != lhsEnd; ++rhsPtr, ++lhsPtr) {
188  *lhsPtr = func(*lhsPtr, *rhsPtr);
189  }
190  }
191 }
197 template <typename LhsT, typename RhsT>
199  Image<RhsT> const& rhs,
200  pixelOp2XY<LhsT, RhsT> const& func
201  ) {
202  if (lhs.getDimensions() != rhs.getDimensions()) {
203  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
204  (boost::format("Images are of different size, %dx%d v %dx%d") % lhs.getWidth() %
205  lhs.getHeight() % rhs.getWidth() % rhs.getHeight())
206  .str());
207  }
208 
209  for (int y = 0; y != lhs.getHeight(); ++y) {
210  typename Image<RhsT>::const_x_iterator rhsPtr = rhs.row_begin(y);
211  int x = lhs.getX0();
212  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
213  lhsPtr != lhsEnd; ++rhsPtr, ++lhsPtr, ++x) {
214  *lhsPtr = func(x, y + lhs.getY0(), *lhsPtr, *rhsPtr);
215  }
216  }
217 }
218 }
219 }
220 } // lsst::afw::image
221 
222 #endif
A functor class equivalent to tr1::function&lt;LhsT (LhsT, RhsT)&gt;, but with a virtual operator() ...
A functor class equivalent to tr1::function&lt;ValT (int, int, ValT)&gt;, but with a virtual operator() ...
virtual LhsT operator()(int x, int y, LhsT lhs, RhsT rhs) const =0
_const_view_t::x_iterator const_x_iterator
A const iterator for traversing the pixels in a row.
Definition: Image.h:155
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: Image.h:147
void for_each_pixel(Image< LhsT > &lhs, pixelOp0< LhsT > const &func)
Set each pixel in an Image&lt;LhsT&gt; to func()
int getX0() const
Return the image&#39;s column-origin.
Definition: Image.h:317
A functor class equivalent to tr1::function&lt;LhsT (int, int, LhsT, RhsT)&gt;, but with a virtual operator...
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: Image.h:367
x_iterator row_end(int y) const
Return an x_iterator to the end of the y&#39;th row.
Definition: Image.h:415
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:309
A functor class equivalent to tr1::function&lt;ValT ()&gt;, but with a virtual operator() ...
double x
afw::table::Key< afw::table::Array< ImagePixelT > > image
A functor class equivalent to tr1::function&lt;ValT (ValT)&gt;, but with a virtual operator() ...
virtual ValT operator()(ValT lhs) const =0
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: Image.h:412
virtual ValT operator()(int x, int y, ValT lhs) const =0
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:307
virtual ValT operator()() const =0
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:486
virtual LhsT operator()(LhsT lhs, RhsT rhs) const =0
int getY0() const
Return the image&#39;s row-origin.
Definition: Image.h:325