lsst.afw  14.0-22-gc76603d1b
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"
42 #include "lsst/pex/exceptions.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 
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,
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,
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 =
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 
276  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y);
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);
307  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y,
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 =
328  std::string const& mode = "w") const;
329 
337  void writeFits(fits::MemFileManager& manager,
340  std::string const& mode = "w") const;
341 
348  void writeFits(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
350 
359  void writeFits(
360  std::string const& filename,
362  std::string const& mode = "w",
364  ) const;
365 
374  void writeFits(
375  fits::MemFileManager& manager,
376  fits::ImageWriteOptions const& options,
377  std::string const& mode = "w",
379  ) const;
380 
388  void writeFits(
389  fits::Fits& fitsfile,
390  fits::ImageWriteOptions const& options,
392  ) const;
393 
394 
402  static Mask readFits(std::string const& filename, int hdu = INT_MIN) {
403  return Mask<MaskPixelT>(filename, hdu);
404  }
405 
413  static Mask readFits(fits::MemFileManager& manager, int hdu = INT_MIN) {
414  return Mask<MaskPixelT>(manager, hdu);
415  }
416 
418  static std::string interpret(MaskPixelT value);
419  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
420 
421  // Mask Plane ops
422 
424  void clearAllMaskPlanes();
426  void clearMaskPlane(int plane);
430  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
437  static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr<lsst::daf::base::PropertySet const> metadata);
438  //
439  // Operations on the mask plane dictionary
440  //
442  static void clearMaskPlaneDict();
443  static int addMaskPlane(const std::string& name);
444  static void removeMaskPlane(const std::string& name);
454  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault = false);
455 
461  static int getMaskPlane(const std::string& name);
467  static MaskPixelT getPlaneBitMask(const std::string& name);
468 
469  static int getNumPlanesMax() { return 8 * sizeof(MaskPixelT); }
470  static int getNumPlanesUsed();
474  MaskPlaneDict const& getMaskPlaneDict() const;
476  void printMaskPlanes() const;
477 
481  static void addMaskPlanesToMetadata(std::shared_ptr<lsst::daf::base::PropertySet>);
482  //
483  // This one isn't static, it fixes up a given Mask's planes
498  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
499 
500 private:
501  // LSST_PERSIST_FORMATTER(lsst::afw::formatters::MaskFormatter)
502  std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
503 
504  static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
505  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
506  static const std::string maskPlanePrefix;
507 
516  static int addMaskPlane(std::string name, int plane);
517 
519  static int getMaskPlaneNoThrow(const std::string& name);
521  static MaskPixelT getBitMaskNoThrow(int plane);
527  static MaskPixelT getBitMask(int plane);
528 
532  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
533 
534  //
535  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
536  //
539 
545  void checkMaskDictionaries(Mask const& other);
546 };
547 
548 template <typename PixelT>
549 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
550 }
551 }
552 } // lsst::afw::image
553 
554 #endif // LSST_AFW_IMAGE_MASK_H
table::Key< std::string > name
Definition: ApCorrMap.cc:71
Mask(Mask< OtherPixelT > const &rhs, const bool deep)
Definition: Mask.h:219
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:64
Options for writing an image to FITS.
Definition: fits.h:201
std::string getAsString(int x, int y)
Definition: Mask.h:419
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:85
tuple options
void swap(Mask< PixelT > &a, Mask< PixelT > &b)
Definition: Mask.cc:419
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:279
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:106
Mask< MaskPT > type
Return the desired type.
Definition: Mask.h:93
An integer coordinate rectangle.
Definition: Box.h:55
STL class.
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
A base class for image defects.
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:101
static Mask readFits(fits::MemFileManager &manager, int hdu=INT_MIN)
Read a Mask from a FITS RAM file.
Definition: Mask.h:413
A class used to request that array accesses be checked.
Definition: ImageBase.h:78
double x
static Mask readFits(std::string const &filename, int hdu=INT_MIN)
Read a Mask from a regular FITS file.
Definition: Mask.h:402
static int getNumPlanesMax()
Definition: Mask.h:469
A templated class to return this classes&#39; type (present in Image/Mask/MaskedImage) ...
Definition: Mask.h:91