LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
binImage.cc
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2008, 2009, 2010 LSST Corporation.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <http://www.lsstcorp.org/LegalNotices/>.
21  */
22 
28 #include "lsst/pex/exceptions.h"
30 
31 namespace pexExcept = lsst::pex::exceptions;
32 namespace afwImage = lsst::afw::image;
33 
34 namespace lsst {
35 namespace afw {
36 namespace math {
37 
38 template<typename ImageT>
39 PTR(ImageT) binImage(ImageT const& in,
40  int const binsize,
41  lsst::afw::math::Property const flags
42  )
43 {
44  return binImage(in, binsize, binsize, flags);
45 }
46 
47 template<typename ImageT>
48 PTR(ImageT) binImage(ImageT const& in,
49  int const binX,
50  int const binY,
51  lsst::afw::math::Property const flags
52  )
53 {
54  if (flags != lsst::afw::math::MEAN) {
55  throw LSST_EXCEPT(pexExcept::InvalidParameterError,
56  (boost::format("Only afwMath::MEAN is supported, saw 0x%x") % flags).str());
57  }
58  if (binX <= 0 || binY <= 0) {
59  throw LSST_EXCEPT(pexExcept::DomainError,
60  (boost::format("Binning must be >= 0, saw %dx%d") % binX % binY).str());
61  }
62 
63  int const outWidth = in.getWidth()/binX;
64  int const outHeight = in.getHeight()/binY;
65 
66  typename ImageT::Ptr out = typename ImageT::Ptr(
67  new ImageT(geom::Extent2I(outWidth, outHeight))
68  );
69  out->setXY0(in.getXY0());
70  *out = typename ImageT::SinglePixel(0);
71 
72  for (int oy = 0, iy = 0; oy < out->getHeight(); ++oy) {
73  for (int i = 0; i != binY; ++i, ++iy) {
74  typename ImageT::x_iterator optr = out->row_begin(oy);
75  for (typename ImageT::x_iterator iptr = in.row_begin(iy), iend = iptr + binX*outWidth;
76  iptr < iend; ) {
77  typename ImageT::SinglePixel val = *iptr; ++iptr;
78  for (int j = 1; j != binX; ++j, ++iptr) {
79  val += *iptr;
80  }
81  *optr += val; ++optr;
82  }
83  }
84  for (typename ImageT::x_iterator ptr = out->row_begin(oy), end = out->row_end(oy); ptr != end; ++ptr) {
85  *ptr /= binX*binY;
86  }
87  }
88 
89  return out;
90 }
91 
92 /************************************************************************************************************/
93 //
94 // Explicit instantiations
95 //
97 #define INSTANTIATE(TYPE) \
98  template afwImage::Image<TYPE>::Ptr \
99  binImage(afwImage::Image<TYPE> const&, int, lsst::afw::math::Property const); \
100  template afwImage::Image<TYPE>::Ptr \
101  binImage(afwImage::Image<TYPE> const&, int, int, lsst::afw::math::Property const); \
102  template afwImage::MaskedImage<TYPE>::Ptr \
103  binImage(afwImage::MaskedImage<TYPE> const&, int, lsst::afw::math::Property const); \
104  template afwImage::MaskedImage<TYPE>::Ptr \
105  binImage(afwImage::MaskedImage<TYPE> const&, int, int, lsst::afw::math::Property const); \
106 
107 INSTANTIATE(boost::uint16_t)
108 INSTANTIATE(int)
109 INSTANTIATE(float)
110 INSTANTIATE(double)
112 
113 }}}
boost::shared_ptr< ImageT > binImage(ImageT const &inImage, int const binX, int const binY, lsst::afw::math::Property const flags=lsst::afw::math::MEAN)
Definition: binImage.cc:48
Include files required for standard LSST Exception handling.
#define INSTANTIATE(MATCH)
#define PTR(...)
Definition: base.h:41
bool val
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
estimate sample mean
Definition: Statistics.h:67
Property
control what is calculated
Definition: Statistics.h:63