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
Background.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2015 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 <https://www.lsstcorp.org/LegalNotices/>.
23  */
24 
32 #include <iostream>
33 #include <limits>
34 #include <vector>
35 #include <cmath>
36 #include "lsst/utils/ieee.h"
41 
42 namespace lsst {
43 namespace ex = pex::exceptions;
44 
45 namespace afw {
46 namespace math {
47 
58 template<typename ImageT>
59 Background::Background(ImageT const& img,
60  BackgroundControl const& bgCtrl
61  ) :
62  lsst::daf::base::Citizen(typeid(this)),
63  _imgBBox(img.getBBox()),
64  _bctrl(new BackgroundControl(bgCtrl)),
65  _asUsedInterpStyle(Interpolate::UNKNOWN),
66  _asUsedUndersampleStyle(THROW_EXCEPTION),
67  _xcen(0), _ycen(0), _xorig(0), _yorig(0), _xsize(0), _ysize(0)
68 {
69  if (_imgBBox.isEmpty()) {
70  throw LSST_EXCEPT(ex::InvalidParameterError, "Image contains no pixels");
71  }
72 
73  // Check that an int's large enough to hold the number of pixels
74  if (_imgBBox.getWidth()*static_cast<double>(_imgBBox.getHeight()) > std::numeric_limits<int>::max()) {
75  throw LSST_EXCEPT(lsst::pex::exceptions::OverflowError,
76  str(boost::format("Image %dx%d has more pixels than fit in an int (%d)")
77  % _imgBBox.getWidth() % _imgBBox.getHeight() % std::numeric_limits<int>::max()));
78  }
79 
81 }
82 
83 /************************************************************************************************************/
91  int const nx,
92  int const ny
93  ) :
94  lsst::daf::base::Citizen(typeid(this)),
95  _imgBBox(imageBBox),
96  _bctrl(new BackgroundControl(nx, ny)),
97  _asUsedInterpStyle(Interpolate::UNKNOWN),
98  _asUsedUndersampleStyle(THROW_EXCEPTION),
99  _xcen(0), _ycen(0), _xorig(0), _yorig(0), _xsize(0), _ysize(0)
100 {
101  if (_imgBBox.isEmpty()) {
102  throw LSST_EXCEPT(ex::InvalidParameterError, "Image contains no pixels");
103  }
104 
105  // Check that an int's large enough to hold the number of pixels
106  if (_imgBBox.getWidth()*static_cast<double>(_imgBBox.getHeight()) > std::numeric_limits<int>::max()) {
107  throw LSST_EXCEPT(lsst::pex::exceptions::OverflowError,
108  str(boost::format("Image %dx%d has more pixels than fit in an int (%d)")
109  % _imgBBox.getWidth() % _imgBBox.getHeight() % std::numeric_limits<int>::max()));
110  }
111 
113 }
114 
115 /************************************************************************************************************/
120 void
121 Background::_setCenOrigSize(int const width, int const height,
122  int const nxSample, int const nySample)
123 {
124  _xcen.resize( nxSample); _ycen.resize(nySample);
125  _xorig.resize(nxSample); _yorig.resize(nySample);
126  _xsize.resize(nxSample), _ysize.resize(nySample);
127 
128  // Compute the centers and origins for the cells
129  for (int iX = 0; iX < nxSample; ++iX) {
130  const int endx = std::min(((iX+1)*width + nxSample/2)/nxSample, width);
131  _xorig[iX] = (iX == 0) ? 0 : _xorig[iX-1] + _xsize[iX-1];
132  _xsize[iX] = endx - _xorig[iX];
133  _xcen [iX] = _xorig[iX] + (0.5 * _xsize[iX]) - 0.5;
134  }
135 
136  for (int iY = 0; iY < nySample; ++iY) {
137  const int endy = std::min(((iY+1)*height + nySample/2)/nySample, height);
138  _yorig[iY] = (iY == 0) ? 0 : _yorig[iY-1] + _ysize[iY-1];
139  _ysize[iY] = endy - _yorig[iY];
140  _ycen [iY] = _yorig[iY] + (0.5 * _ysize[iY]) - 0.5;
141  }
142 }
143 
144 /************************************************************************************************************/
148 UndersampleStyle stringToUndersampleStyle(std::string const &style) {
149  static std::map<std::string, UndersampleStyle> undersampleStrings;
150  if (undersampleStrings.size() == 0) {
151  undersampleStrings["THROW_EXCEPTION"] = THROW_EXCEPTION;
152  undersampleStrings["REDUCE_INTERP_ORDER"] = REDUCE_INTERP_ORDER;
153  undersampleStrings["INCREASE_NXNYSAMPLE"] = INCREASE_NXNYSAMPLE;
154  }
155 
156  if (undersampleStrings.find(style) == undersampleStrings.end()) {
157  throw LSST_EXCEPT(ex::InvalidParameterError, "Understample style not defined: "+style);
158  }
159  return undersampleStrings[style];
160 }
162 /*
163  * Explicit instantiations
164  *
165  */
166 #define INSTANTIATE_BACKGROUND(TYPE) \
167  template Background::Background(image::Image<TYPE> const& img, \
168  BackgroundControl const& bgCtrl); \
169  template Background::Background(image::MaskedImage<TYPE> const& img, \
170  BackgroundControl const& bgCtrl); \
171  template PTR(image::Image<TYPE>) Background::getImage<TYPE>(Interpolate::Style const, \
172  UndersampleStyle const) const;
173 
174 
175 INSTANTIATE_BACKGROUND(float)
176 
177 }}} // lsst::afw::math
UndersampleStyle stringToUndersampleStyle(std::string const &style)
Conversion function to switch a string to an UndersampleStyle.
Definition: Background.cc:148
std::vector< int > _ysize
y size ...
Definition: Background.h:340
Interpolate values for a set of x,y vector&lt;&gt;s.
Definition: Interpolate.h:37
std::vector< int > _xorig
x origin pix coords of sub images
Definition: Background.h:337
Background(ImageT const &img, BackgroundControl const &bgCtrl)
Constructor for Background.
Definition: Background.cc:59
std::vector< double > _ycen
y center ...
Definition: Background.h:336
Pass parameters to a Background object.
Definition: Background.h:61
An integer coordinate rectangle.
Definition: Box.h:53
int getHeight() const
Definition: Box.h:155
std::vector< int > _xsize
x size of sub images
Definition: Background.h:339
geom::Box2I _imgBBox
size and origin of input image
Definition: Background.h:330
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
std::vector< double > _xcen
x center pix coords of sub images
Definition: Background.h:335
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:166
void _setCenOrigSize(int const width, int const height, int const nxSample, int const nySample)
Definition: Background.cc:121
Compute Image Statistics.
Implementation of the Class MaskedImage.
std::vector< int > _yorig
y origin ...
Definition: Background.h:338
int getWidth() const
Definition: Box.h:154