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
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 "boost/cstdint.hpp"
38 #include "boost/shared_ptr.hpp"
39 
40 #include "lsst/base.h"
41 #include "lsst/daf/base/Citizen.h"
44 #include "lsst/pex/exceptions.h"
46 #include "lsst/afw/image/Image.h"
48 
49 namespace lsst {
50 namespace afw {
51  namespace formatters {
52  template<typename> class MaskFormatter;
53  }
54 namespace image {
55 
56 namespace detail {
57  class MaskDict; // forward declaration
58 }
59 
60 // all masks will initially be instantiated with the same pixel type
61 namespace detail {
63  struct Mask_tag : public detail::basic_tag { };
64 
65  typedef std::map<std::string, int> MaskPlaneDict;
66 }
67 
92 template<typename MaskPixelT=lsst::afw::image::MaskPixel>
93 class Mask : public ImageBase<MaskPixelT> {
94 public:
95  typedef boost::shared_ptr<Mask> Ptr;
96  typedef boost::shared_ptr<const Mask> ConstPtr;
98 
100 
101 #if !defined(SWIG)
102  template<typename MaskPT=MaskPixelT>
107  };
108 #endif
109 
110  // Constructors
111  explicit Mask(
112  unsigned int width, unsigned int height,
113  MaskPlaneDict const& planeDefs=MaskPlaneDict()
114  );
115  explicit Mask(
116  unsigned int width, unsigned int height,
117  MaskPixelT initialValue,
118  MaskPlaneDict const& planeDefs=MaskPlaneDict()
119  );
120  explicit Mask(
122  MaskPlaneDict const& planeDefs=MaskPlaneDict()
123  );
124  explicit Mask(
125  geom::Extent2I const & dimensions,
126  MaskPixelT initialValue,
127  MaskPlaneDict const& planeDefs=MaskPlaneDict()
128  );
129  explicit Mask(geom::Box2I const & bbox,
130  MaskPlaneDict const& planeDefs=MaskPlaneDict());
131  explicit Mask(geom::Box2I const & bbox, MaskPixelT initialValue,
132  MaskPlaneDict const& planeDefs=MaskPlaneDict());
133 
152  explicit Mask(
153  std::string const & fileName, int hdu=0,
155  geom::Box2I const & bbox=geom::Box2I(),
156  ImageOrigin origin=PARENT,
157  bool conformMasks=false
158  );
159 
178  explicit Mask(
179  fits::MemFileManager & manager, int hdu=0,
181  geom::Box2I const & bbox=geom::Box2I(),
182  ImageOrigin origin=PARENT,
183  bool conformMasks=false
184  );
185 
201  explicit Mask(
202  fits::Fits & fitsfile,
204  geom::Box2I const & bbox=geom::Box2I(),
205  ImageOrigin origin=PARENT,
206  bool conformMasks=false
207  );
208 
209  // generalised copy constructor
210  template<typename OtherPixelT>
211  Mask(Mask<OtherPixelT> const& rhs, const bool deep) :
212  image::ImageBase<MaskPixelT>(rhs, deep),
213  _maskDict(rhs._maskDict) {}
214 
215  Mask(const Mask& src, const bool deep=false);
216  Mask(
217  const Mask& src,
218  const geom::Box2I & bbox,
219  ImageOrigin const origin=PARENT,
220  const bool deep=false
221  );
222 
223  explicit Mask(ndarray::Array<MaskPixelT,2,1> const & array, bool deep=false,
224  geom::Point2I const & xy0=geom::Point2I());
225 
226  void swap(Mask& rhs);
227  // Operators
228 
229  Mask& operator=(MaskPixelT const rhs);
230  Mask& operator=(const Mask& rhs);
231 
232  void operator|=(Mask const& rhs);
233  void operator|=(MaskPixelT const rhs);
234 
235  void operator&=(Mask const& rhs);
236  void operator&=(MaskPixelT const rhs);
237  static MaskPixelT getPlaneBitMask(const std::vector<std::string> &names);
238 
239  void operator^=(Mask const& rhs);
240  void operator^=(MaskPixelT const rhs);
241 
243  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y) const;
244  bool operator()(int x, int y, int plane) const;
247  CheckIndices const&) const;
248  bool operator()(int x, int y, int plane, CheckIndices const&) const;
249 
257  void writeFits(
258  std::string const& fileName,
260  std::string const& mode="w"
261  ) const;
262 
270  void writeFits(
271  fits::MemFileManager & manager,
273  std::string const& mode="w"
274  ) const;
275 
282  void writeFits(
283  fits::Fits & fitsfile,
285  ) const;
286 
294  static Mask readFits(std::string const & filename, int hdu=0) {
295  return Mask<MaskPixelT>(filename, hdu);
296  }
297 
305  static Mask readFits(fits::MemFileManager & manager, int hdu=0) {
306  return Mask<MaskPixelT>(manager, hdu);
307  }
308 
310  static std::string interpret(MaskPixelT value);
311  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
312 
313  // Mask Plane ops
314 
315  void clearAllMaskPlanes();
316  void clearMaskPlane(int plane);
317  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
319  //
320  // Operations on the mask plane dictionary
321  //
322  static void clearMaskPlaneDict();
323  static int addMaskPlane(const std::string& name);
324  static void removeMaskPlane(const std::string& name);
325  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault=false);
326 
327  static int getMaskPlane(const std::string& name);
328  static MaskPixelT getPlaneBitMask(const std::string& name);
329 
330  static int getNumPlanesMax() { return 8*sizeof(MaskPixelT); }
331  static int getNumPlanesUsed();
332  MaskPlaneDict const& getMaskPlaneDict() const;
333  void printMaskPlanes() const;
334 
336  //
337  // This one isn't static, it fixes up a given Mask's planes
338  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
339 
340 private:
341  //LSST_PERSIST_FORMATTER(lsst::afw::formatters::MaskFormatter)
342  PTR(detail::MaskDict) _maskDict; // our bitplane dictionary
343 
344  static PTR(detail::MaskDict) _maskPlaneDict();
345  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
346  static const std::string maskPlanePrefix;
347 
348  static int addMaskPlane(std::string name, int plane);
349 
350  static int getMaskPlaneNoThrow(const std::string& name);
351  static MaskPixelT getBitMaskNoThrow(int plane);
352  static MaskPixelT getBitMask(int plane);
353 
354  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
355 
356  //
357  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
358  //
359  using ImageBase<MaskPixelT>::_getRawView;
360  using ImageBase<MaskPixelT>::swap;
361 
362  void checkMaskDictionaries(Mask const& other);
363 };
364 
365 template<typename PixelT>
366 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
367 
368 }}} // lsst::afw::image
369 
370 #endif // LSST_AFW_IMAGE_MASK_H
int y
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:1188
static void clearMaskPlaneDict()
Reset the maskPlane dictionary.
Definition: Mask.cc:881
ImageBase< MaskPixelT >::PixelReference operator()(int x, int y)
get a reference to the specified pixel
Definition: Mask.cc:977
boost::shared_ptr< const Mask > ConstPtr
Definition: Mask.h:96
#define PTR(...)
Definition: base.h:41
table::Key< std::string > name
Definition: ApCorrMap.cc:71
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
MaskPlaneDict const & getMaskPlaneDict() const
Definition: Mask.cc:745
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:65
#define CONST_PTR(...)
Definition: base.h:47
void operator^=(Mask const &rhs)
XOR a Mask into a Mask.
Definition: Mask.cc:1127
static Mask readFits(fits::MemFileManager &manager, int hdu=0)
Read a Mask from a FITS RAM file.
Definition: Mask.h:305
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:1155
static int getMaskPlane(const std::string &name)
Return the mask plane number corresponding to a plane name.
Definition: Mask.cc:825
static void removeMaskPlane(const std::string &name)
Definition: Mask.cc:751
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:194
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: Image.h:115
static int addMaskPlane(const std::string &name)
Definition: Mask.cc:700
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) ... (x1, y)
Definition: Mask.cc:1142
int const x0
Definition: saturated.cc:45
boost::shared_ptr< detail::MaskDict > _maskDict
Definition: Mask.h:342
boost::shared_ptr< Mask > Ptr
Definition: Mask.h:95
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
static MaskPixelT getBitMask(int plane)
Return the bitmask corresponding to plane ID.
Definition: Mask.cc:803
afw::table::PointKey< int > dimensions
void clearAllMaskPlanes()
Clear all the pixels.
Definition: Mask.cc:889
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
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:93
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:106
static std::string interpret(MaskPixelT value)
Interpret a mask value as a comma-separated list of mask plane names.
Definition: Mask.cc:684
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:97
static int getNumPlanesMax()
Definition: Mask.h:330
Support for 2-D images.
void _initializePlanes(MaskPlaneDict const &planeDefs)
Initialise mask planes; called by constructors.
Definition: Mask.cc:394
A class used to request that array accesses be checked.
Definition: Image.h:87
void swap(Mask &rhs)
Definition: Mask.cc:517
void operator|=(Mask const &rhs)
OR a Mask into a Mask.
Definition: Mask.cc:1081
Interface for Persistable base class.
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
int x
Mask & operator=(MaskPixelT const rhs)
Definition: Mask.cc:538
static const std::string maskPlanePrefix
Definition: Mask.h:346
A multidimensional strided array.
Definition: Array.h:47
void operator&=(Mask const &rhs)
AND a Mask into a Mask.
Definition: Mask.cc:1104
Mask(unsigned int width, unsigned int height, MaskPlaneDict const &planeDefs=MaskPlaneDict())
Construct a Mask initialized to 0x0.
Definition: Mask.cc:404
void checkMaskDictionaries(Mask const &other)
Definition: Mask.cc:1053
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.
void clearMaskPlane(int plane)
Clear the specified bit in all pixels.
Definition: Mask.cc:897
std::string getAsString(int x, int y)
Definition: Mask.h:311
Class for storing generic metadata.
Definition: PropertySet.h:82
static MaskPixelT getBitMaskNoThrow(int plane)
Return the bitmask corresponding to a plane ID, or 0 if invalid.
Definition: Mask.cc:793
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
Mask< MaskPT > type
Return the desired type.
Definition: Mask.h:106
afw::table::Key< double > b
static boost::shared_ptr< detail::MaskDict > _maskPlaneDict()
Definition: Mask.cc:1246
Interface for PropertySet class.
static int _setMaskPlaneDict(MaskPlaneDict const &mpd)
A templated class to return this classes&#39; type (present in Image/Mask/MaskedImage) ...
Definition: Mask.h:104
void printMaskPlanes() const
print the mask plane dictionary to std::cout
Definition: Mask.cc:1234
detail::Mask_tag image_category
Definition: Mask.h:99
Definition of default types for Masks and Variance Images.
static Mask readFits(std::string const &filename, int hdu=0)
Read a Mask from a regular FITS file.
Definition: Mask.h:294
Include files required for standard LSST Exception handling.
static int getNumPlanesUsed()
Reset the maskPlane dictionary.
Definition: Mask.cc:872
Interface for ImageFormatter class.
Mask(Mask< OtherPixelT > const &rhs, const bool deep)
Definition: Mask.h:211