lsst.afw  16.0-43-gaa65a4573+1
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"
41 #include "lsst/pex/exceptions.h"
44 #include "lsst/afw/fitsDefaults.h"
45 
46 namespace lsst {
47 namespace afw {
48 namespace image {
49 
50 namespace detail {
51 class MaskDict; // forward declaration
52 }
53 
54 // all masks will initially be instantiated with the same pixel type
55 namespace detail {
57 struct Mask_tag : public detail::basic_tag {};
58 
60 } // namespace detail
61 
77 template <typename MaskPixelT = lsst::afw::image::MaskPixel>
78 class Mask : public ImageBase<MaskPixelT> {
79 public:
81 
83 
85  template <typename MaskPT = MaskPixelT>
88  typedef Mask<MaskPT> type;
89  };
90 
91  // Constructors
99  explicit Mask(unsigned int width, unsigned int height, MaskPlaneDict const& planeDefs = MaskPlaneDict());
108  explicit Mask(unsigned int width, unsigned int height, MaskPixelT initialValue,
109  MaskPlaneDict const& planeDefs = MaskPlaneDict());
117  MaskPlaneDict const& planeDefs = MaskPlaneDict());
125  explicit Mask(lsst::geom::Extent2I const& dimensions, MaskPixelT initialValue,
126  MaskPlaneDict const& planeDefs = MaskPlaneDict());
133  explicit Mask(lsst::geom::Box2I const& bbox, MaskPlaneDict const& planeDefs = MaskPlaneDict());
141  explicit Mask(lsst::geom::Box2I const& bbox, MaskPixelT initialValue,
142  MaskPlaneDict const& planeDefs = MaskPlaneDict());
143 
162  explicit Mask(std::string const& fileName, int hdu = fits::DEFAULT_HDU,
165  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
166  bool conformMasks = false);
167 
186  explicit Mask(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU,
189  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
190  bool conformMasks = false);
191 
207  explicit Mask(fits::Fits& fitsfile,
210  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
211  bool conformMasks = false);
212 
213  // generalised copy constructor
214  template <typename OtherPixelT>
215  Mask(Mask<OtherPixelT> const& rhs, const bool deep)
216  : image::ImageBase<MaskPixelT>(rhs, deep), _maskDict(rhs._maskDict) {}
217 
224  Mask(const Mask& src, const bool deep = false);
225  Mask(Mask&& src);
226  ~Mask() override;
235  Mask(const Mask& src, const lsst::geom::Box2I& bbox, ImageOrigin const origin = PARENT,
236  const bool deep = false);
237 
238  explicit Mask(ndarray::Array<MaskPixelT, 2, 1> const& array, bool deep = false,
240 
241  void swap(Mask& rhs);
242  // Operators
243 
244  Mask& operator=(MaskPixelT const rhs);
245  Mask& operator=(const Mask& rhs);
246  Mask& operator=(Mask&& rhs);
247 
249  Mask& operator|=(Mask const& rhs);
251  Mask& operator|=(MaskPixelT const rhs);
252 
254  Mask& operator&=(Mask const& rhs);
256  Mask& operator&=(MaskPixelT const rhs);
257 
271  Mask subset(lsst::geom::Box2I const & bbox, ImageOrigin origin=PARENT) const {
272  return Mask(*this, bbox, origin, false);
273  }
274 
276  Mask operator[](lsst::geom::Box2I const & bbox) const {
277  return subset(bbox);
278  }
279 
281 
287  static MaskPixelT getPlaneBitMask(const std::vector<std::string>& names);
288 
290  Mask& operator^=(Mask const& rhs);
292  Mask& operator^=(MaskPixelT const rhs);
293 
300  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y);
307  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y) const;
315  bool operator()(int x, int y, int plane) const;
323  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y, CheckIndices const& check);
331  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y,
332  CheckIndices const& check) const;
341  bool operator()(int x, int y, int plane, CheckIndices const& check) const;
342 
350  void writeFits(std::string const& fileName,
353  std::string const& mode = "w") const;
354 
362  void writeFits(fits::MemFileManager& manager,
365  std::string const& mode = "w") const;
366 
373  void writeFits(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
375 
384  void writeFits(std::string const& filename, fits::ImageWriteOptions const& options,
385  std::string const& mode = "w",
386  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
387 
396  void writeFits(fits::MemFileManager& manager, fits::ImageWriteOptions const& options,
397  std::string const& mode = "w",
398  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
399 
407  void writeFits(fits::Fits& fitsfile, fits::ImageWriteOptions const& options,
408  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
409 
418  static Mask readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU) {
419  return Mask<MaskPixelT>(filename, hdu);
420  }
421 
430  static Mask readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU) {
431  return Mask<MaskPixelT>(manager, hdu);
432  }
433 
435  static std::string interpret(MaskPixelT value);
436  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
437 
438  // Mask Plane ops
439 
441  void clearAllMaskPlanes();
443  void clearMaskPlane(int plane);
447  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
454  static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr<lsst::daf::base::PropertySet const> metadata);
455  //
456  // Operations on the mask plane dictionary
457  //
459  static void clearMaskPlaneDict();
460  static int addMaskPlane(const std::string& name);
461  static void removeMaskPlane(const std::string& name);
471  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault = false);
472 
478  static int getMaskPlane(const std::string& name);
484  static MaskPixelT getPlaneBitMask(const std::string& name);
485 
486  static int getNumPlanesMax() { return 8 * sizeof(MaskPixelT); }
487  static int getNumPlanesUsed();
491  MaskPlaneDict const& getMaskPlaneDict() const;
493  void printMaskPlanes() const;
494 
498  static void addMaskPlanesToMetadata(std::shared_ptr<lsst::daf::base::PropertySet>);
499  //
500  // This one isn't static, it fixes up a given Mask's planes
515  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
516 
517 private:
518  std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
519 
520  static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
521  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
522  static const std::string maskPlanePrefix;
523 
532  static int addMaskPlane(std::string name, int plane);
533 
535  static int getMaskPlaneNoThrow(const std::string& name);
537  static MaskPixelT getBitMaskNoThrow(int plane);
543  static MaskPixelT getBitMask(int plane);
544 
548  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
549 
550  //
551  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
552  //
555 
561  void checkMaskDictionaries(Mask const& other);
562 };
563 
564 template <typename PixelT>
565 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
566 } // namespace image
567 } // namespace afw
568 } // namespace lsst
569 
570 #endif // LSST_AFW_IMAGE_MASK_H
Mask(Mask< OtherPixelT > const &rhs, const bool deep)
Definition: Mask.h:215
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:59
Options for writing an image to FITS.
Definition: fits.h:218
std::string getAsString(int x, int y)
Definition: Mask.h:436
static Mask readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Read a Mask from a regular FITS file.
Definition: Mask.h:418
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:80
tuple options
ItemVariant const * other
Definition: Schema.cc:55
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:296
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:103
Mask< MaskPT > type
Return the desired type.
Definition: Mask.h:88
STL class.
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
A base class for image defects.
Mask operator[](lsst::geom::Box2I const &bbox) const
Return a subimage corresponding to the given box (interpreted as PARENT coordinates).
Definition: Mask.h:276
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:78
detail::Mask_tag image_category
Definition: Mask.h:82
std::shared_ptr< RecordT > src
Definition: Match.cc:48
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:120
table::Key< int > b
A class used to request that array accesses be checked.
Definition: ImageBase.h:75
double x
void swap(Image< PixelT > &a, Image< PixelT > &b)
Definition: Image.cc:473
static int getNumPlanesMax()
Definition: Mask.h:486
table::Box2IKey bbox
table::Key< int > a
Mask subset(lsst::geom::Box2I const &bbox, ImageOrigin origin=PARENT) const
Return a subimage corresponding to the given box.
Definition: Mask.h:271
A templated class to return this classes&#39; type (present in Image/Mask/MaskedImage) ...
Definition: Mask.h:86
static Mask readFits(fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU)
Read a Mask from a FITS RAM file.
Definition: Mask.h:430
int y
Definition: SpanSet.cc:49
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18