LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 
29 #ifndef LSST_AFW_IMAGE_IMAGE_ALGORITHM_H
30 #define LSST_AFW_IMAGE_IMAGE_ALGORITHM_H
31 
32 #include <list>
33 #include <map>
34 #include <string>
35 #include <utility>
36 #include <functional>
37 
38 #include "boost/tr1/functional.hpp"
39 #include "boost/mpl/bool.hpp"
40 #include "boost/shared_ptr.hpp"
41 
42 #include "lsst/afw/image/lsstGil.h"
43 #include "lsst/afw/image/Utils.h"
45 #include "lsst/daf/base.h"
46 #include "lsst/daf/base/Citizen.h"
47 #include "lsst/pex/exceptions.h"
48 
49 namespace lsst { namespace afw { namespace image {
50 #if !defined(SWIG)
51 
54  template<typename ValT>
55  struct pixelOp0 : public std::tr1::function<ValT ()> {
56  virtual ~pixelOp0() {}
57  virtual ValT operator()() const = 0;
58  };
59 
63  template<typename ValT>
64  struct pixelOp1 : public std::tr1::function<ValT (ValT)> {
65  virtual ~pixelOp1() {}
66  virtual ValT operator()(ValT lhs) const = 0;
67  };
68 
72  template<typename ValT>
73  struct pixelOp1XY : public std::tr1::function<ValT (int, int, ValT)> {
74  virtual ~pixelOp1XY() {}
75  virtual ValT operator()(int x, int y, ValT lhs) const = 0;
76  };
77 
81  template<typename LhsT, typename RhsT>
82  struct pixelOp2 : public std::tr1::function<LhsT (LhsT, RhsT)>
83  {
84  virtual ~pixelOp2() {}
85  virtual LhsT operator()(LhsT lhs, RhsT rhs) const = 0;
86  };
87 
91  template<typename LhsT, typename RhsT>
92  struct pixelOp2XY : public std::tr1::function<LhsT (int, int, LhsT, RhsT)>
93  {
94  virtual ~pixelOp2XY() {}
95  virtual LhsT operator()(int x, int y, LhsT lhs, RhsT rhs) const = 0;
96  };
97 
98  /*******************************************************************************************************/
102  template<typename LhsT>
104  pixelOp0<LhsT> const& func
105  )
106  {
107  for (int y = 0; y != lhs.getHeight(); ++y) {
108  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
109  lhsPtr != lhsEnd; ++lhsPtr) {
110  *lhsPtr = func();
111  }
112  }
113  }
114 
118  template<typename LhsT>
120  pixelOp1<LhsT> const& func
121  )
122  {
123  for (int y = 0; y != lhs.getHeight(); ++y) {
124  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
125  lhsPtr != lhsEnd; ++lhsPtr) {
126  *lhsPtr = func(*lhsPtr);
127  }
128  }
129  }
130 
136  template<typename LhsT>
138  pixelOp1XY<LhsT> const& func
139  )
140  {
141  for (int y = 0; y != lhs.getHeight(); ++y) {
142  int x = lhs.getX0();
143  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
144  lhsPtr != lhsEnd; ++lhsPtr, ++x) {
145  *lhsPtr = func(x, y + lhs.getY0(), *lhsPtr);
146  }
147  }
148  }
149 
153  template<typename LhsT, typename RhsT>
155  Image<RhsT> const& rhs,
156  pixelOp1<RhsT> const& func
157  )
158  {
159  if (lhs.getDimensions() != rhs.getDimensions()) {
160  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
161  (boost::format("Images are of different size, %dx%d v %dx%d") %
162  lhs.getWidth() % lhs.getHeight() % rhs.getWidth() % rhs.getHeight()).str());
163  }
164 
165  for (int y = 0; y != lhs.getHeight(); ++y) {
166  typename Image<RhsT>::const_x_iterator rhsPtr = rhs.row_begin(y);
167 
168  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
169  lhsPtr != lhsEnd; ++rhsPtr, ++lhsPtr) {
170  *lhsPtr = func(*rhsPtr);
171  }
172  }
173  }
174 
178  template<typename LhsT, typename RhsT>
180  Image<RhsT> const& rhs,
181  pixelOp2<LhsT, RhsT> const& func
182  )
183  {
184  if (lhs.getDimensions() != rhs.getDimensions()) {
185  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
186  (boost::format("Images are of different size, %dx%d v %dx%d") %
187  lhs.getWidth() % lhs.getHeight() % rhs.getWidth() % rhs.getHeight()).str());
188  }
189 
190  for (int y = 0; y != lhs.getHeight(); ++y) {
191  typename Image<RhsT>::const_x_iterator rhsPtr = rhs.row_begin(y);
192 
193  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
194  lhsPtr != lhsEnd; ++rhsPtr, ++lhsPtr) {
195  *lhsPtr = func(*lhsPtr, *rhsPtr);
196  }
197  }
198  }
204  template<typename LhsT, typename RhsT>
206  Image<RhsT> const& rhs,
207  pixelOp2XY<LhsT, RhsT> const& func
208  )
209  {
210  if (lhs.getDimensions() != rhs.getDimensions()) {
211  throw LSST_EXCEPT(lsst::pex::exceptions::LengthError,
212  (boost::format("Images are of different size, %dx%d v %dx%d") %
213  lhs.getWidth() % lhs.getHeight() % rhs.getWidth() % rhs.getHeight()).str());
214  }
215 
216  for (int y = 0; y != lhs.getHeight(); ++y) {
217  typename Image<RhsT>::const_x_iterator rhsPtr = rhs.row_begin(y);
218  int x = lhs.getX0();
219  for (typename Image<LhsT>::x_iterator lhsPtr = lhs.row_begin(y), lhsEnd = lhs.row_end(y);
220  lhsPtr != lhsEnd; ++rhsPtr, ++lhsPtr, ++x) {
221  *lhsPtr = func(x, y + lhs.getY0(), *lhsPtr, *rhsPtr);
222  }
223  }
224  }
225 #endif
226 }}} // lsst::afw::image
227 
228 #endif
int y
x_iterator row_begin(int y) const
Definition: Image.h:319
x_iterator row_end(int y) const
Return an x_iterator to the end of the y&#39;th row.
Definition: Image.h:324
void for_each_pixel(Image< LhsT > &lhs, pixelOp0< LhsT > const &func)
virtual ValT operator()() const =0
Image utility functions.
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:237
A set of classes of general utility in connection with images.
_const_view_t::x_iterator const_x_iterator
A const iterator for traversing the pixels in a row.
Definition: Image.h:158
Types and classes to interface lsst::afw::image to boost::gil.
virtual ValT operator()(ValT lhs) const =0
int getY0() const
Definition: Image.h:255
int x
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
virtual ValT operator()(int x, int y, ValT lhs) const =0
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: Image.h:151
virtual LhsT operator()(LhsT lhs, RhsT rhs) const =0
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:239
virtual LhsT operator()(int x, int y, LhsT lhs, RhsT rhs) const =0
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: Image.h:298
A class to represent a 2-dimensional array of pixels.
Definition: PSF.h:43
Include files required for standard LSST Exception handling.
int getX0() const
Definition: Image.h:247