LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
Mask.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 
30 #ifndef LSST_AFW_IMAGE_MASK_H
31 #define LSST_AFW_IMAGE_MASK_H
32 
33 #include <list>
34 #include <map>
35 #include <string>
36 
37 #include <memory>
38 
39 #include "lsst/base.h"
40 #include "lsst/daf/base/Citizen.h"
43 #include "lsst/pex/exceptions.h"
45 #include "lsst/afw/image/Image.h"
47 
48 namespace lsst {
49 namespace afw {
50  namespace formatters {
51  template<typename> class MaskFormatter;
52  }
53 namespace image {
54 
55 namespace detail {
56  class MaskDict; // forward declaration
57 }
58 
59 // all masks will initially be instantiated with the same pixel type
60 namespace detail {
62  struct Mask_tag : public detail::basic_tag { };
63 
64  typedef std::map<std::string, int> MaskPlaneDict;
65 }
66 
91 template<typename MaskPixelT=lsst::afw::image::MaskPixel>
92 class Mask : public ImageBase<MaskPixelT> {
93 public:
94  typedef std::shared_ptr<Mask> Ptr;
95  typedef std::shared_ptr<const Mask> ConstPtr;
97 
99 
100 #if !defined(SWIG)
101  template<typename MaskPT=MaskPixelT>
106  };
107 #endif
108 
109  // Constructors
110  explicit Mask(
111  unsigned int width, unsigned int height,
112  MaskPlaneDict const& planeDefs=MaskPlaneDict()
113  );
114  explicit Mask(
115  unsigned int width, unsigned int height,
116  MaskPixelT initialValue,
117  MaskPlaneDict const& planeDefs=MaskPlaneDict()
118  );
119  explicit Mask(
121  MaskPlaneDict const& planeDefs=MaskPlaneDict()
122  );
123  explicit Mask(
124  geom::Extent2I const & dimensions,
125  MaskPixelT initialValue,
126  MaskPlaneDict const& planeDefs=MaskPlaneDict()
127  );
128  explicit Mask(geom::Box2I const & bbox,
129  MaskPlaneDict const& planeDefs=MaskPlaneDict());
130  explicit Mask(geom::Box2I const & bbox, MaskPixelT initialValue,
131  MaskPlaneDict const& planeDefs=MaskPlaneDict());
132 
151  explicit Mask(
152  std::string const & fileName, int hdu=0,
154  geom::Box2I const & bbox=geom::Box2I(),
155  ImageOrigin origin=PARENT,
156  bool conformMasks=false
157  );
158 
177  explicit Mask(
178  fits::MemFileManager & manager, int hdu=0,
180  geom::Box2I const & bbox=geom::Box2I(),
181  ImageOrigin origin=PARENT,
182  bool conformMasks=false
183  );
184 
200  explicit Mask(
201  fits::Fits & fitsfile,
203  geom::Box2I const & bbox=geom::Box2I(),
204  ImageOrigin origin=PARENT,
205  bool conformMasks=false
206  );
207 
208  // generalised copy constructor
209  template<typename OtherPixelT>
210  Mask(Mask<OtherPixelT> const& rhs, const bool deep) :
211  image::ImageBase<MaskPixelT>(rhs, deep),
212  _maskDict(rhs._maskDict) {}
213 
214  Mask(const Mask& src, const bool deep=false);
215  Mask(
216  const Mask& src,
217  const geom::Box2I & bbox,
218  ImageOrigin const origin=PARENT,
219  const bool deep=false
220  );
221 
222  explicit Mask(ndarray::Array<MaskPixelT,2,1> const & array, bool deep=false,
223  geom::Point2I const & xy0=geom::Point2I());
224 
225  void swap(Mask& rhs);
226  // Operators
227 
228  Mask& operator=(MaskPixelT const rhs);
229  Mask& operator=(const Mask& rhs);
230 
231  void operator|=(Mask const& rhs);
232  void operator|=(MaskPixelT const rhs);
233 
234  void operator&=(Mask const& rhs);
235  void operator&=(MaskPixelT const rhs);
236  static MaskPixelT getPlaneBitMask(const std::vector<std::string> &names);
237 
238  void operator^=(Mask const& rhs);
239  void operator^=(MaskPixelT const rhs);
240 
242  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y) const;
243  bool operator()(int x, int y, int plane) const;
246  CheckIndices const&) const;
247  bool operator()(int x, int y, int plane, CheckIndices const&) const;
248 
256  void writeFits(
257  std::string const& fileName,
259  std::string const& mode="w"
260  ) const;
261 
269  void writeFits(
270  fits::MemFileManager & manager,
272  std::string const& mode="w"
273  ) const;
274 
281  void writeFits(
282  fits::Fits & fitsfile,
284  ) const;
285 
293  static Mask readFits(std::string const & filename, int hdu=0) {
294  return Mask<MaskPixelT>(filename, hdu);
295  }
296 
304  static Mask readFits(fits::MemFileManager & manager, int hdu=0) {
305  return Mask<MaskPixelT>(manager, hdu);
306  }
307 
309  static std::string interpret(MaskPixelT value);
310  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
311 
312  // Mask Plane ops
313 
314  void clearAllMaskPlanes();
315  void clearMaskPlane(int plane);
316  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
318  //
319  // Operations on the mask plane dictionary
320  //
321  static void clearMaskPlaneDict();
322  static int addMaskPlane(const std::string& name);
323  static void removeMaskPlane(const std::string& name);
324  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault=false);
325 
326  static int getMaskPlane(const std::string& name);
327  static MaskPixelT getPlaneBitMask(const std::string& name);
328 
329  static int getNumPlanesMax() { return 8*sizeof(MaskPixelT); }
330  static int getNumPlanesUsed();
331  MaskPlaneDict const& getMaskPlaneDict() const;
332  void printMaskPlanes() const;
333 
335  //
336  // This one isn't static, it fixes up a given Mask's planes
337  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
338 
339 private:
340  //LSST_PERSIST_FORMATTER(lsst::afw::formatters::MaskFormatter)
341  PTR(detail::MaskDict) _maskDict; // our bitplane dictionary
342 
343  static PTR(detail::MaskDict) _maskPlaneDict();
344  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
345  static const std::string maskPlanePrefix;
346 
347  static int addMaskPlane(std::string name, int plane);
348 
349  static int getMaskPlaneNoThrow(const std::string& name);
350  static MaskPixelT getBitMaskNoThrow(int plane);
351  static MaskPixelT getBitMask(int plane);
352 
353  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
354 
355  //
356  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
357  //
358  using ImageBase<MaskPixelT>::_getRawView;
359  using ImageBase<MaskPixelT>::swap;
360 
361  void checkMaskDictionaries(Mask const& other);
362 };
363 
364 template<typename PixelT>
365 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
366 
367 }}} // lsst::afw::image
368 
369 #endif // LSST_AFW_IMAGE_MASK_H
int y
static std::string interpret(MaskPixelT value)
Interpret a mask value as a comma-separated list of mask plane names.
Definition: Mask.cc:684
void _initializePlanes(MaskPlaneDict const &planeDefs)
Initialise mask planes; called by constructors.
Definition: Mask.cc:393
static void clearMaskPlaneDict()
Reset the maskPlane dictionary.
Definition: Mask.cc:881
table::Key< std::string > name
Definition: ApCorrMap.cc:71
static MaskPixelT getBitMaskNoThrow(int plane)
Return the bitmask corresponding to a plane ID, or 0 if invalid.
Definition: Mask.cc:793
void writeFits(std::string const &fileName, boost::shared_ptr< lsst::daf::base::PropertySet const > metadata=boost::shared_ptr< lsst::daf::base::PropertySet >(), std::string const &mode="w") const
Write a mask to a regular FITS file.
static int getMaskPlane(const std::string &name)
Return the mask plane number corresponding to a plane name.
Definition: Mask.cc:825
Mask(Mask< OtherPixelT > const &rhs, const bool deep)
Definition: Mask.h:210
Include files required for standard LSST Exception handling.
void operator|=(Mask const &rhs)
OR a Mask into a Mask.
Definition: Mask.cc:1072
void operator&=(Mask const &rhs)
AND a Mask into a Mask.
Definition: Mask.cc:1097
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:64
Mask & operator=(MaskPixelT const rhs)
Definition: Mask.cc:537
std::string getAsString(int x, int y)
Definition: Mask.h:310
static int _setMaskPlaneDict(MaskPlaneDict const &mpd)
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:96
static int getNumPlanesUsed()
Reset the maskPlane dictionary.
Definition: Mask.cc:872
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:202
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: Image.h:114
void removeAndClearMaskPlane(const std::string &name, bool const removeFromDefault=false)
Clear all pixels of the specified mask and remove the plane from the mask plane dictionary; optionall...
Definition: Mask.cc:769
Mask< MaskPT > type
Return the desired type.
Definition: Mask.h:105
static int getMaskPlaneNoThrow(const std::string &name)
Return the mask plane number corresponding to a plane name, or -1 if not found.
Definition: Mask.cc:840
static const std::string maskPlanePrefix
Definition: Mask.h:345
int const x0
Definition: saturated.cc:45
ImageBase< MaskPixelT >::PixelReference operator()(int x, int y)
get a reference to the specified pixel
Definition: Mask.cc:977
An integer coordinate rectangle.
Definition: Box.h:53
void operator^=(Mask const &rhs)
XOR a Mask into a Mask.
Definition: Mask.cc:1122
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
Mask(unsigned int width, unsigned int height, MaskPlaneDict const &planeDefs=MaskPlaneDict())
Construct a Mask initialized to 0x0.
Definition: Mask.cc:403
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:42
void ImageT ImageT int float saturatedPixelValue int const width
Definition: saturated.cc:44
static MaskPixelT getBitMask(int plane)
Return the bitmask corresponding to plane ID.
Definition: Mask.cc:803
Interface for Persistable base class.
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:92
detail::Mask_tag image_category
Definition: Mask.h:98
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:106
void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y)
Set the bit specified by &quot;planeId&quot; for pixels (x0, y) ...
Definition: Mask.cc:1138
static MaskPlaneDict parseMaskPlaneMetadata(boost::shared_ptr< lsst::daf::base::PropertySet const >)
Given a PropertySet that contains the MaskPlane assignments, setup the MaskPlanes.
Definition: Mask.cc:1184
Support for 2-D images.
A class used to request that array accesses be checked.
Definition: Image.h:86
static MaskPixelT getPlaneBitMask(const std::vector< std::string > &names)
Return the bitmask corresponding to a vector of plane names OR&#39;d together.
Definition: Mask.cc:860
double x
void ImageT ImageT int float saturatedPixelValue int const height
Definition: saturated.cc:44
std::shared_ptr< Mask > Ptr
Definition: Mask.h:94
static void removeMaskPlane(const std::string &name)
Definition: Mask.cc:751
static void addMaskPlanesToMetadata(boost::shared_ptr< lsst::daf::base::PropertySet >)
Given a PropertySet, replace any existing MaskPlane assignments with the current ones.
Definition: Mask.cc:1151
static int getNumPlanesMax()
Definition: Mask.h:329
void checkMaskDictionaries(Mask const &other)
Definition: Mask.cc:1053
#define PTR(...)
Definition: base.h:41
MaskPlaneDict const & getMaskPlaneDict() const
Return the Mask&#39;s maskPlaneDict.
Definition: Mask.cc:745
Class for storing generic metadata.
Definition: PropertySet.h:82
static Mask readFits(fits::MemFileManager &manager, int hdu=0)
Read a Mask from a FITS RAM file.
Definition: Mask.h:304
afw::table::Key< double > b
Interface for PropertySet class.
void swap(Mask &rhs)
Definition: Mask.cc:516
void conformMaskPlanes(const MaskPlaneDict &masterPlaneDict)
Adjust this mask to conform to the standard Mask class&#39;s mask plane dictionary, adding any new mask p...
Definition: Mask.cc:913
static Mask readFits(std::string const &filename, int hdu=0)
Read a Mask from a regular FITS file.
Definition: Mask.h:293
A templated class to return this classes&#39; type (present in Image/Mask/MaskedImage) ...
Definition: Mask.h:103
#define CONST_PTR(...)
A shared pointer to a const object.
Definition: base.h:47
boost::shared_ptr< detail::MaskDict > _maskDict
Definition: Mask.h:341
void clearMaskPlane(int plane)
Clear the specified bit in all pixels.
Definition: Mask.cc:897
Basic LSST definitions.
void printMaskPlanes() const
print the mask plane dictionary to std::cout
Definition: Mask.cc:1230
Definition of default types for Masks and Variance Images.
void clearAllMaskPlanes()
Clear all the pixels.
Definition: Mask.cc:889
static int addMaskPlane(const std::string &name)
Definition: Mask.cc:700
std::shared_ptr< const Mask > ConstPtr
Definition: Mask.h:95
Interface for ImageFormatter class.
static boost::shared_ptr< detail::MaskDict > _maskPlaneDict()
Definition: Mask.cc:1242