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
copyGoodPixels.cc
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 
30 #include <limits>
31 
32 #include "boost/format.hpp"
33 
34 #include "lsst/pex/exceptions.h"
35 #include "lsst/afw/geom.h"
37 
38 namespace pexExcept = lsst::pex::exceptions;
39 namespace afwGeom = lsst::afw::geom;
40 namespace afwImage = lsst::afw::image;
41 namespace coaddUtils = lsst::coadd::utils;
42 
43 namespace {
44  /*
45  * A boolean functor to test if a MaskedImage pixel is valid (mask & badPixel == 0)
46  */
47  struct CheckMask {
48  CheckMask(lsst::afw::image::MaskPixel badPixel) : _badPixel(badPixel) {}
49 
50  template<typename T>
51  bool operator()(T val) const {
52  return ((val.mask() & _badPixel) == 0) ? true : false;
53  }
54  private:
56  };
57 
58  /*
59  * A boolean functor to test if an Image pixel has known value (not NaN)
60  */
61  struct CheckKnownValue {
62  CheckKnownValue(lsst::afw::image::MaskPixel) {}
63 
64  template<typename T>
65  bool operator()(T val) const {
66  return !std::isnan(static_cast<float>(*val));
67  }
68  };
69 
70  /*
71  * Implementation of copyGoodPixels
72  *
73  * The template parameter isValidPixel is a functor with operator()
74  * which returns true if a given image pixel is valid.
75  * This allows us to support multiple image types including
76  * MaskedImage with a mask bit and Image with a check for NaN.
77  *
78  * @return overlapping bounding box, relative to parent image
79  */
80  template <typename ImageT, typename isValidPixel>
81  int copyGoodPixelsImpl(
82  ImageT &destImage,
83  ImageT const &srcImage,
84  lsst::afw::image::MaskPixel const badPixelMask
85  ) {
86  afwGeom::Box2I overlapBBox = destImage.getBBox();
87  overlapBBox.clip(srcImage.getBBox());
88  if (overlapBBox.isEmpty()) {
89  return 0;
90  }
91 
92  ImageT destView(destImage, overlapBBox, afwImage::PARENT, false);
93  ImageT srcView(srcImage, overlapBBox, afwImage::PARENT, false);
94 
95  isValidPixel const isValid(badPixelMask); // functor to check if a pixel is good
96  int numGoodPix = 0;
97  for (int y = 0, endY = srcView.getHeight(); y != endY; ++y) {
98  typename ImageT::const_x_iterator srcIter = srcView.row_begin(y);
99  typename ImageT::const_x_iterator const srcEndIter = srcView.row_end(y);
100  typename ImageT::x_iterator destIter = destView.row_begin(y);
101  for (; srcIter != srcEndIter; ++srcIter, ++destIter) {
102  if (isValid(srcIter)) {
103  *destIter = *srcIter;
104 // typename ImageT::SinglePixel pix = *srcIter;
105 // *destIter = pix;
106  ++numGoodPix;
107  }
108  }
109  }
110  return numGoodPix;
111  }
112 } // anonymous namespace
113 
114 template <typename ImagePixelT>
116  // spell out lsst:afw::image to make Doxygen happy
119 ) {
121  return copyGoodPixelsImpl<Image, CheckKnownValue>(destImage, srcImage, 0x0);
122 }
123 
124 template <typename ImagePixelT>
126  // spell out lsst:afw::image to make Doxygen happy
130  lsst::afw::image::VariancePixel> const &srcImage,
131  lsst::afw::image::MaskPixel const badPixelMask
132 ) {
134  return copyGoodPixelsImpl<Image, CheckMask>(destImage, srcImage, badPixelMask);
135 }
136 
137 // Explicit instantiations
138 
140 #define MASKEDIMAGE(IMAGEPIXEL) afwImage::MaskedImage<IMAGEPIXEL, \
141  afwImage::MaskPixel, afwImage::VariancePixel>
142 #define INSTANTIATE(IMAGEPIXEL) \
143  template int coaddUtils::copyGoodPixels<IMAGEPIXEL>( \
144  afwImage::Image<IMAGEPIXEL> &destImage, \
145  afwImage::Image<IMAGEPIXEL> const &srcImage \
146  ); \
147  \
148  template int coaddUtils::copyGoodPixels<IMAGEPIXEL>( \
149  MASKEDIMAGE(IMAGEPIXEL) &destImage, \
150  MASKEDIMAGE(IMAGEPIXEL) const &srcImage, \
151  afwImage::MaskPixel const badPixelMask \
152  );
153 
154 INSTANTIATE(double);
155 INSTANTIATE(float);
156 INSTANTIATE(int);
157 INSTANTIATE(boost::uint16_t);
int y
boost::uint16_t MaskPixel
An include file to include the header files for lsst::afw::geom.
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
int copyGoodPixels(lsst::afw::image::Image< ImagePixelT > &destImage, lsst::afw::image::Image< ImagePixelT > const &srcImage)
copy good pixels from one image to another
int isnan(T t)
Definition: ieee.h:110
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:77
float VariancePixel
! default type for Masks and MaskedImage Masks
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:166
#define INSTANTIATE(T)
ImageT val
Definition: CR.cc:154
Include files required for standard LSST Exception handling.