lsst.afw  13.0-91-g2a3e2ee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
25 /*
26  * LSST bitmasks
27  */
28 
29 #ifndef LSST_AFW_IMAGE_MASK_H
30 #define LSST_AFW_IMAGE_MASK_H
31 
32 #include <list>
33 #include <map>
34 #include <string>
35 
36 #include <memory>
37 
38 #include "lsst/base.h"
39 #include "lsst/daf/base/Citizen.h"
40 #include "lsst/daf/base/Persistable.h"
41 #include "lsst/daf/base/PropertySet.h"
42 #include "lsst/pex/exceptions.h"
44 #include "lsst/afw/image/Image.h"
46 
47 namespace lsst {
48 namespace afw {
49 namespace formatters {
50 template <typename>
51 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 
82 template <typename MaskPixelT = lsst::afw::image::MaskPixel>
83 class Mask : public ImageBase<MaskPixelT> {
84 public:
86 
88 
90  template <typename MaskPT = MaskPixelT>
93  typedef Mask<MaskPT> type;
94  };
95 
96  // Constructors
104  explicit Mask(unsigned int width, unsigned int height, MaskPlaneDict const& planeDefs = MaskPlaneDict());
113  explicit Mask(unsigned int width, unsigned int height, MaskPixelT initialValue,
114  MaskPlaneDict const& planeDefs = MaskPlaneDict());
121  explicit Mask(geom::Extent2I const& dimensions = geom::Extent2I(),
122  MaskPlaneDict const& planeDefs = MaskPlaneDict());
130  explicit Mask(geom::Extent2I const& dimensions, MaskPixelT initialValue,
131  MaskPlaneDict const& planeDefs = MaskPlaneDict());
138  explicit Mask(geom::Box2I const& bbox, MaskPlaneDict const& planeDefs = MaskPlaneDict());
146  explicit Mask(geom::Box2I const& bbox, MaskPixelT initialValue,
147  MaskPlaneDict const& planeDefs = MaskPlaneDict());
148 
167  explicit Mask(std::string const& fileName, int hdu = INT_MIN,
168  std::shared_ptr<lsst::daf::base::PropertySet> metadata =
169  std::shared_ptr<lsst::daf::base::PropertySet>(),
170  geom::Box2I const& bbox = geom::Box2I(), ImageOrigin origin = PARENT,
171  bool conformMasks = false);
172 
191  explicit Mask(fits::MemFileManager& manager, int hdu = INT_MIN,
192  std::shared_ptr<lsst::daf::base::PropertySet> metadata =
193  std::shared_ptr<lsst::daf::base::PropertySet>(),
194  geom::Box2I const& bbox = geom::Box2I(), ImageOrigin origin = PARENT,
195  bool conformMasks = false);
196 
212  explicit Mask(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet> metadata =
213  std::shared_ptr<lsst::daf::base::PropertySet>(),
214  geom::Box2I const& bbox = geom::Box2I(), ImageOrigin origin = PARENT,
215  bool conformMasks = false);
216 
217  // generalised copy constructor
218  template <typename OtherPixelT>
219  Mask(Mask<OtherPixelT> const& rhs, const bool deep)
220  : image::ImageBase<MaskPixelT>(rhs, deep), _maskDict(rhs._maskDict) {}
221 
228  Mask(const Mask& src, const bool deep = false);
237  Mask(const Mask& src, const geom::Box2I& bbox, ImageOrigin const origin = PARENT,
238  const bool deep = false);
239 
240  explicit Mask(ndarray::Array<MaskPixelT, 2, 1> const& array, bool deep = false,
241  geom::Point2I const& xy0 = geom::Point2I());
242 
243  void swap(Mask& rhs);
244  // Operators
245 
246  Mask& operator=(MaskPixelT const rhs);
247  Mask& operator=(const Mask& rhs);
248 
250  Mask& operator|=(Mask const& rhs);
252  Mask& operator|=(MaskPixelT const rhs);
253 
255  Mask& operator&=(Mask const& rhs);
257  Mask& operator&=(MaskPixelT const rhs);
263  static MaskPixelT getPlaneBitMask(const std::vector<std::string>& names);
264 
266  Mask& operator^=(Mask const& rhs);
268  Mask& operator^=(MaskPixelT const rhs);
269 
283  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y) const;
291  bool operator()(int x, int y, int plane) const;
299  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y, CheckIndices const& check);
308  CheckIndices const& check) const;
317  bool operator()(int x, int y, int plane, CheckIndices const& check) const;
318 
326  void writeFits(std::string const& fileName, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
327  std::shared_ptr<lsst::daf::base::PropertySet>(),
328  std::string const& mode = "w") const;
329 
337  void writeFits(fits::MemFileManager& manager,
338  std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
339  std::shared_ptr<lsst::daf::base::PropertySet>(),
340  std::string const& mode = "w") const;
341 
348  void writeFits(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
349  std::shared_ptr<lsst::daf::base::PropertySet const>()) const;
350 
358  static Mask readFits(std::string const& filename, int hdu = INT_MIN) {
359  return Mask<MaskPixelT>(filename, hdu);
360  }
361 
369  static Mask readFits(fits::MemFileManager& manager, int hdu = INT_MIN) {
370  return Mask<MaskPixelT>(manager, hdu);
371  }
372 
374  static std::string interpret(MaskPixelT value);
375  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
376 
377  // Mask Plane ops
378 
380  void clearAllMaskPlanes();
382  void clearMaskPlane(int plane);
386  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
393  static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr<lsst::daf::base::PropertySet const> metadata);
394  //
395  // Operations on the mask plane dictionary
396  //
398  static void clearMaskPlaneDict();
399  static int addMaskPlane(const std::string& name);
400  static void removeMaskPlane(const std::string& name);
410  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault = false);
411 
417  static int getMaskPlane(const std::string& name);
423  static MaskPixelT getPlaneBitMask(const std::string& name);
424 
425  static int getNumPlanesMax() { return 8 * sizeof(MaskPixelT); }
426  static int getNumPlanesUsed();
430  MaskPlaneDict const& getMaskPlaneDict() const;
432  void printMaskPlanes() const;
433 
437  static void addMaskPlanesToMetadata(std::shared_ptr<lsst::daf::base::PropertySet>);
438  //
439  // This one isn't static, it fixes up a given Mask's planes
454  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
455 
456 private:
457  // LSST_PERSIST_FORMATTER(lsst::afw::formatters::MaskFormatter)
458  std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
459 
460  static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
461  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
462  static const std::string maskPlanePrefix;
463 
472  static int addMaskPlane(std::string name, int plane);
473 
475  static int getMaskPlaneNoThrow(const std::string& name);
477  static MaskPixelT getBitMaskNoThrow(int plane);
483  static MaskPixelT getBitMask(int plane);
484 
488  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
489 
490  //
491  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
492  //
495 
501  void checkMaskDictionaries(Mask const& other);
502 };
503 
504 template <typename PixelT>
505 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
506 }
507 }
508 } // lsst::afw::image
509 
510 #endif // LSST_AFW_IMAGE_MASK_H
static std::string interpret(MaskPixelT value)
Interpret a mask value as a comma-separated list of mask plane names.
Definition: Mask.cc:552
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
static void clearMaskPlaneDict()
Reset the maskPlane dictionary.
Definition: Mask.cc:693
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Definition: Image.cc:227
static int getMaskPlane(const std::string &name)
Return the mask plane number corresponding to a plane name.
Definition: Mask.cc:657
Mask(Mask< OtherPixelT > const &rhs, const bool deep)
Definition: Mask.h:219
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:64
Mask & operator=(MaskPixelT const rhs)
Definition: Mask.cc:432
std::string getAsString(int x, int y)
Definition: Mask.h:375
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:85
static void addMaskPlanesToMetadata(std::shared_ptr< lsst::daf::base::PropertySet >)
Given a PropertySet, replace any existing MaskPlane assignments with the current ones.
Definition: Mask.cc:876
static int getNumPlanesUsed()
Definition: Mask.cc:688
static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr< lsst::daf::base::PropertySet const > metadata)
Given a PropertySet that contains the MaskPlane assignments, setup the MaskPlanes.
Definition: Mask.cc:904
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:199
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: Image.h:116
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:616
Mask< MaskPT > type
Return the desired type.
Definition: Mask.h:93
ImageBase< MaskPixelT >::PixelReference operator()(int x, int y)
get a reference to the specified pixel
Definition: Mask.cc:764
An integer coordinate rectangle.
Definition: Box.h:55
Mask & operator&=(Mask const &rhs)
AND a Mask into a Mask.
Definition: Mask.cc:832
Mask(unsigned int width, unsigned int height, MaskPlaneDict const &planeDefs=MaskPlaneDict())
Construct a Mask initialized to 0x0.
Definition: Mask.cc:355
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:83
detail::Mask_tag image_category
Definition: Mask.h:87
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:100
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:867
static Mask readFits(fits::MemFileManager &manager, int hdu=INT_MIN)
Read a Mask from a FITS RAM file.
Definition: Mask.h:369
table::Key< std::string > name
Definition: ApCorrMap.cc:71
A class used to request that array accesses be checked.
Definition: Image.h:88
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:679
double x
static Mask readFits(std::string const &filename, int hdu=INT_MIN)
Read a Mask from a regular FITS file.
Definition: Mask.h:358
static void removeMaskPlane(const std::string &name)
Definition: Mask.cc:605
static int getNumPlanesMax()
Definition: Mask.h:425
MaskPlaneDict const & getMaskPlaneDict() const
Return the Mask&#39;s maskPlaneDict.
Definition: Mask.cc:600
void writeFits(std::string const &fileName, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet >(), std::string const &mode="w") const
Write a mask to a regular FITS file.
afw::table::Key< afw::table::Array< ImagePixelT > > image
Mask & operator|=(Mask const &rhs)
OR a Mask into a Mask.
Definition: Mask.cc:812
void swap(Mask &rhs)
Definition: Mask.cc:411
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:708
Mask & operator^=(Mask const &rhs)
XOR a Mask into a Mask.
Definition: Mask.cc:853
A templated class to return this classes&#39; type (present in Image/Mask/MaskedImage) ...
Definition: Mask.h:91
void clearMaskPlane(int plane)
Clear the specified bit in all pixels.
Definition: Mask.cc:703
void printMaskPlanes() const
print the mask plane dictionary to std::cout
Definition: Mask.cc:945
void clearAllMaskPlanes()
Clear all the pixels.
Definition: Mask.cc:698
static int addMaskPlane(const std::string &name)
Definition: Mask.cc:567