LSSTApplications  18.1.0
LSSTDataManagementBasePackage
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/mpl/bool.hpp"
38 #include <memory>
39 
40 #include "lsst/afw/image/lsstGil.h"
42 #include "lsst/daf/base.h"
43 #include "lsst/daf/base/Citizen.h"
44 #include "lsst/pex/exceptions.h"
45 
46 namespace lsst {
47 namespace afw {
48 namespace image {
52 template <typename ValT>
53 struct pixelOp0 : public std::function<ValT()> {
54  virtual ~pixelOp0() {}
55  virtual ValT operator()() const = 0;
56 };
57 
61 template <typename ValT>
62 struct pixelOp1 : public std::function<ValT(ValT)> {
63  virtual ~pixelOp1() {}
64  virtual ValT operator()(ValT lhs) const = 0;
65 };
66 
70 template <typename ValT>
71 struct pixelOp1XY : public std::function<ValT(int, int, ValT)> {
72  virtual ~pixelOp1XY() {}
73  virtual ValT operator()(int x, int y, ValT lhs) const = 0;
74 };
75 
79 template <typename LhsT, typename RhsT>
80 struct pixelOp2 : public std::function<LhsT(LhsT, RhsT)> {
81  virtual ~pixelOp2() {}
82  virtual LhsT operator()(LhsT lhs, RhsT rhs) const = 0;
83 };
84 
88 template <typename LhsT, typename RhsT>
89 struct pixelOp2XY : public std::function<LhsT(int, int, LhsT, RhsT)> {
90  virtual ~pixelOp2XY() {}
91  virtual LhsT operator()(int x, int y, LhsT lhs, RhsT rhs) const = 0;
92 };
93 
97 template <typename LhsT>
99  pixelOp0<LhsT> const& func
100 ) {
101  for (int y = 0; y != lhs.getHeight(); ++y) {
102  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
103  lhsPtr != lhsEnd; ++lhsPtr) {
104  *lhsPtr = func();
105  }
106  }
107 }
108 
112 template <typename LhsT>
114  pixelOp1<LhsT> const& func
115 ) {
116  for (int y = 0; y != lhs.getHeight(); ++y) {
117  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
118  lhsPtr != lhsEnd; ++lhsPtr) {
119  *lhsPtr = func(*lhsPtr);
120  }
121  }
122 }
123 
129 template <typename LhsT>
131  pixelOp1XY<LhsT> const& func
132 ) {
133  for (int y = 0; y != lhs.getHeight(); ++y) {
134  int x = lhs.getX0();
135  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
136  lhsPtr != lhsEnd; ++lhsPtr, ++x) {
137  *lhsPtr = func(x, y + lhs.getY0(), *lhsPtr);
138  }
139  }
140 }
141 
145 template <typename LhsT, typename RhsT>
147  Image<RhsT> const& rhs,
148  pixelOp1<RhsT> const& func
149 ) {
150  if (lhs.getDimensions() != rhs.getDimensions()) {
152  (boost::format("Images are of different size, %dx%d v %dx%d") % lhs.getWidth() %
153  lhs.getHeight() % rhs.getWidth() % rhs.getHeight())
154  .str());
155  }
156 
157  for (int y = 0; y != lhs.getHeight(); ++y) {
158  typename Image<RhsT>::const_x_iterator rhsPtr = rhs.row_begin(y);
159 
160  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
161  lhsPtr != lhsEnd; ++rhsPtr, ++lhsPtr) {
162  *lhsPtr = func(*rhsPtr);
163  }
164  }
165 }
166 
170 template <typename LhsT, typename RhsT>
172  Image<RhsT> const& rhs,
173  pixelOp2<LhsT, RhsT> const& func
174 ) {
175  if (lhs.getDimensions() != rhs.getDimensions()) {
177  (boost::format("Images are of different size, %dx%d v %dx%d") % lhs.getWidth() %
178  lhs.getHeight() % rhs.getWidth() % rhs.getHeight())
179  .str());
180  }
181 
182  for (int y = 0; y != lhs.getHeight(); ++y) {
183  typename Image<RhsT>::const_x_iterator rhsPtr = rhs.row_begin(y);
184 
185  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
186  lhsPtr != lhsEnd; ++rhsPtr, ++lhsPtr) {
187  *lhsPtr = func(*lhsPtr, *rhsPtr);
188  }
189  }
190 }
196 template <typename LhsT, typename RhsT>
198  Image<RhsT> const& rhs,
199  pixelOp2XY<LhsT, RhsT> const& func
200 ) {
201  if (lhs.getDimensions() != rhs.getDimensions()) {
203  (boost::format("Images are of different size, %dx%d v %dx%d") % lhs.getWidth() %
204  lhs.getHeight() % rhs.getWidth() % rhs.getHeight())
205  .str());
206  }
207 
208  for (int y = 0; y != lhs.getHeight(); ++y) {
209  typename Image<RhsT>::const_x_iterator rhsPtr = rhs.row_begin(y);
210  int x = lhs.getX0();
211  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
212  lhsPtr != lhsEnd; ++rhsPtr, ++lhsPtr, ++x) {
213  *lhsPtr = func(x, y + lhs.getY0(), *lhsPtr, *rhsPtr);
214  }
215  }
216 }
217 } // namespace image
218 } // namespace afw
219 } // namespace lsst
220 
221 #endif
A functor class equivalent to std::function<LhsT (LhsT, RhsT)>, but with a virtual operator() ...
A functor class equivalent to std::function<ValT (int, int, ValT)>, but with a virtual operator() ...
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:316
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
_const_view_t::x_iterator const_x_iterator
A const iterator for traversing the pixels in a row.
Definition: ImageBase.h:142
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:134
int y
Definition: SpanSet.cc:49
void for_each_pixel(Image< LhsT > &lhs, pixelOp0< LhsT > const &func)
Set each pixel in an Image<LhsT> to func()
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:324
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:419
A base class for image defects.
A functor class equivalent to std::function<LhsT (int, int, LhsT, RhsT)>, but with a virtual operator...
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
A functor class equivalent to std::function<ValT ()>, but with a virtual operator() ...
double x
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
int getY0() const
Return the image&#39;s row-origin.
Definition: ImageBase.h:332
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:314
x_iterator row_end(int y) const
Return an x_iterator to the end of the y&#39;th row.
Definition: ImageBase.h:422
A functor class equivalent to std::function<ValT (ValT)>, but with a virtual operator() ...
lsst::geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:374
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
virtual ValT operator()() const =0
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:59