33 #include "boost/format.hpp"
35 #include "lsst/pex/exceptions.h"
36 #include "lsst/afw/geom.h"
39 namespace pexExcept = lsst::pex::exceptions;
40 namespace afwGeom = lsst::afw::geom;
41 namespace afwImage = lsst::afw::image;
42 namespace coaddUtils = lsst::coadd::utils;
49 CheckMask(lsst::afw::image::MaskPixel badPixel) : _badPixel(badPixel) {}
52 bool operator()(T val)
const {
53 return ((val.mask() & _badPixel) == 0) ?
true :
false;
56 lsst::afw::image::MaskPixel _badPixel;
62 struct CheckKnownValue {
63 CheckKnownValue(lsst::afw::image::MaskPixel) {}
66 bool operator()(T val)
const {
67 return !std::isnan(static_cast<float>(*val));
81 template <
typename CoaddT,
typename WeightPixelT,
typename isVal
idPixel>
82 static lsst::afw::geom::Box2I addToCoaddImpl(
84 lsst::afw::image::Image<WeightPixelT> &weightMap,
86 lsst::afw::image::MaskPixel
const badPixelMask,
89 typedef typename afwImage::Image<WeightPixelT> WeightMapT;
91 if (coadd.getBBox() != weightMap.getBBox()) {
92 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
93 (boost::format(
"coadd and weightMap parent bboxes differ: %s != %s") %
94 coadd.getBBox() % weightMap.getBBox()).str());
97 afwGeom::Box2I overlapBBox = coadd.getBBox();
98 overlapBBox.clip(image.getBBox());
99 if (overlapBBox.isEmpty()) {
103 CoaddT coaddView(coadd, overlapBBox, afwImage::PARENT,
false);
104 WeightMapT weightMapView(weightMap, overlapBBox, afwImage::PARENT,
false);
105 CoaddT imageView(image, overlapBBox, afwImage::PARENT,
false);
107 isValidPixel
const isValid(badPixelMask);
108 for (
int y = 0, endY = imageView.getHeight(); y != endY; ++y) {
109 typename CoaddT::const_x_iterator imageIter = imageView.row_begin(y);
110 typename CoaddT::const_x_iterator
const imageEndIter = imageView.row_end(y);
111 typename CoaddT::x_iterator coaddIter = coaddView.row_begin(y);
112 typename WeightMapT::x_iterator weightMapIter = weightMapView.row_begin(y);
113 for (; imageIter != imageEndIter; ++imageIter, ++coaddIter, ++weightMapIter) {
114 if (isValid(imageIter)) {
115 typename CoaddT::SinglePixel pix = *imageIter;
116 pix *=
typename CoaddT::Pixel(weight);
118 *weightMapIter += weight;
126 template <
typename CoaddPixelT,
typename WeightPixelT>
129 lsst::afw::image::Image<CoaddPixelT> &coadd,
130 lsst::afw::image::Image<WeightPixelT> &weightMap,
131 lsst::afw::image::Image<CoaddPixelT>
const &image,
134 typedef lsst::afw::image::Image<CoaddPixelT> Image;
135 return addToCoaddImpl<Image, WeightPixelT, CheckKnownValue>(coadd, weightMap, image, 0x0, weight);
138 template <
typename CoaddPixelT,
typename WeightPixelT>
141 lsst::afw::image::MaskedImage<CoaddPixelT, lsst::afw::image::MaskPixel,
142 lsst::afw::image::VariancePixel> &coadd,
143 lsst::afw::image::Image<WeightPixelT> &weightMap,
144 lsst::afw::image::MaskedImage<CoaddPixelT, lsst::afw::image::MaskPixel,
145 lsst::afw::image::VariancePixel>
const &maskedImage,
146 lsst::afw::image::MaskPixel
const badPixelMask,
149 typedef lsst::afw::image::MaskedImage<CoaddPixelT> Image;
150 return addToCoaddImpl<Image,WeightPixelT,CheckMask>(coadd, weightMap, maskedImage, badPixelMask, weight);
156 #define MASKEDIMAGE(IMAGEPIXEL) afwImage::MaskedImage<IMAGEPIXEL, \
157 afwImage::MaskPixel, afwImage::VariancePixel>
158 #define INSTANTIATE(COADDPIXEL, WEIGHTPIXEL) \
159 template lsst::afw::geom::Box2I coaddUtils::addToCoadd<COADDPIXEL, WEIGHTPIXEL>( \
160 afwImage::Image<COADDPIXEL> &coadd, \
161 afwImage::Image<WEIGHTPIXEL> &weightMap, \
162 afwImage::Image<COADDPIXEL> const &image, \
166 template lsst::afw::geom::Box2I coaddUtils::addToCoadd<COADDPIXEL, WEIGHTPIXEL>( \
167 MASKEDIMAGE(COADDPIXEL) &coadd, \
168 afwImage::Image<WEIGHTPIXEL> &weightMap, \
169 MASKEDIMAGE(COADDPIXEL) const &image, \
170 afwImage::MaskPixel const badPixelMask, \
174 INSTANTIATE(
double,
double);
175 INSTANTIATE(
double,
float);
176 INSTANTIATE(
double,
int);
177 INSTANTIATE(
double, std::uint16_t);
178 INSTANTIATE(
float,
double);
179 INSTANTIATE(
float,
float);
180 INSTANTIATE(
float,
int);
181 INSTANTIATE(
float, std::uint16_t);
lsst::afw::geom::Box2I addToCoadd(lsst::afw::image::Image< CoaddPixelT > &coadd, lsst::afw::image::Image< WeightPixelT > &weightMap, lsst::afw::image::Image< CoaddPixelT > const &image, WeightPixelT weight)
add good pixels from an image to a coadd and associated weight map