LSSTApplications  17.0+124,17.0+14,17.0+73,18.0.0+37,18.0.0+80,18.0.0-4-g68ffd23+4,18.1.0-1-g0001055+12,18.1.0-1-g03d53ef+5,18.1.0-1-g1349e88+55,18.1.0-1-g2505f39+44,18.1.0-1-g5315e5e+4,18.1.0-1-g5e4b7ea+14,18.1.0-1-g7e8fceb+4,18.1.0-1-g85f8cd4+48,18.1.0-1-g8ff0b9f+4,18.1.0-1-ga2c679d+1,18.1.0-1-gd55f500+35,18.1.0-10-gb58edde+2,18.1.0-11-g0997b02+4,18.1.0-13-gfe4edf0b+12,18.1.0-14-g259bd21+21,18.1.0-19-gdb69f3f+2,18.1.0-2-g5f9922c+24,18.1.0-2-gd3b74e5+11,18.1.0-2-gfbf3545+32,18.1.0-26-g728bddb4+5,18.1.0-27-g6ff7ca9+2,18.1.0-3-g52aa583+25,18.1.0-3-g8ea57af+9,18.1.0-3-gb69f684+42,18.1.0-3-gfcaddf3+6,18.1.0-32-gd8786685a,18.1.0-4-gf3f9b77+6,18.1.0-5-g1dd662b+2,18.1.0-5-g6dbcb01+41,18.1.0-6-gae77429+3,18.1.0-7-g9d75d83+9,18.1.0-7-gae09a6d+30,18.1.0-9-gc381ef5+4,w.2019.45
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 
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"
40 #include "lsst/pex/exceptions.h"
43 #include "lsst/afw/fitsDefaults.h"
44 
45 namespace lsst {
46 namespace afw {
47 namespace image {
48 
49 namespace detail {
50 class MaskDict; // forward declaration
51 }
52 
53 // all masks will initially be instantiated with the same pixel type
54 namespace detail {
56 struct Mask_tag : public detail::basic_tag {};
57 
59 } // namespace detail
60 
76 template <typename MaskPixelT = lsst::afw::image::MaskPixel>
77 class Mask : public ImageBase<MaskPixelT> {
78 public:
80 
82 
84  template <typename MaskPT = MaskPixelT>
87  typedef Mask<MaskPT> type;
88  };
89 
90  // Constructors
98  explicit Mask(unsigned int width, unsigned int height, MaskPlaneDict const& planeDefs = MaskPlaneDict());
107  explicit Mask(unsigned int width, unsigned int height, MaskPixelT initialValue,
108  MaskPlaneDict const& planeDefs = MaskPlaneDict());
116  MaskPlaneDict const& planeDefs = MaskPlaneDict());
124  explicit Mask(lsst::geom::Extent2I const& dimensions, MaskPixelT initialValue,
125  MaskPlaneDict const& planeDefs = MaskPlaneDict());
132  explicit Mask(lsst::geom::Box2I const& bbox, MaskPlaneDict const& planeDefs = MaskPlaneDict());
140  explicit Mask(lsst::geom::Box2I const& bbox, MaskPixelT initialValue,
141  MaskPlaneDict const& planeDefs = MaskPlaneDict());
142 
163  explicit Mask(std::string const& fileName, int hdu = fits::DEFAULT_HDU,
166  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
167  bool conformMasks = false, bool allowUnsafe = false);
168 
189  explicit Mask(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU,
192  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
193  bool conformMasks = false, bool allowUnsafe = false);
194 
212  explicit Mask(fits::Fits& fitsfile,
215  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
216  bool conformMasks = false, bool allowUnsafe = false);
217 
218  // generalised copy constructor
219  template <typename OtherPixelT>
220  Mask(Mask<OtherPixelT> const& rhs, const bool deep)
221  : image::ImageBase<MaskPixelT>(rhs, deep), _maskDict(rhs._maskDict) {}
222 
229  Mask(const Mask& src, const bool deep = false);
230  Mask(Mask&& src);
231  ~Mask() override;
240  Mask(const Mask& src, const lsst::geom::Box2I& bbox, ImageOrigin const origin = PARENT,
241  const bool deep = false);
242 
243  explicit Mask(ndarray::Array<MaskPixelT, 2, 1> const& array, bool deep = false,
245 
246  void swap(Mask& rhs);
247  // Operators
248 
249  Mask& operator=(MaskPixelT const rhs);
250  Mask& operator=(const Mask& rhs);
251  Mask& operator=(Mask&& rhs);
252 
254  Mask& operator|=(Mask const& rhs);
256  Mask& operator|=(MaskPixelT const rhs);
257 
259  Mask& operator&=(Mask const& rhs);
261  Mask& operator&=(MaskPixelT const rhs);
262 
276  Mask subset(lsst::geom::Box2I const& bbox, ImageOrigin origin = PARENT) const {
277  return Mask(*this, bbox, origin, false);
278  }
279 
281  Mask operator[](lsst::geom::Box2I const& bbox) const { return subset(bbox); }
282 
284 
290  static MaskPixelT getPlaneBitMask(const std::vector<std::string>& names);
291 
293  Mask& operator^=(Mask const& rhs);
295  Mask& operator^=(MaskPixelT const rhs);
296 
303  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y);
310  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y) const;
318  bool operator()(int x, int y, int plane) const;
326  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y, CheckIndices const& check);
334  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y,
335  CheckIndices const& check) const;
344  bool operator()(int x, int y, int plane, CheckIndices const& check) const;
345 
353  void writeFits(std::string const& fileName,
356  std::string const& mode = "w") const;
357 
365  void writeFits(fits::MemFileManager& manager,
368  std::string const& mode = "w") const;
369 
376  void writeFits(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
378 
387  void writeFits(std::string const& filename, fits::ImageWriteOptions const& options,
388  std::string const& mode = "w",
389  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
390 
399  void writeFits(fits::MemFileManager& manager, fits::ImageWriteOptions const& options,
400  std::string const& mode = "w",
401  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
402 
410  void writeFits(fits::Fits& fitsfile, fits::ImageWriteOptions const& options,
411  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
412 
421  static Mask readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU) {
422  return Mask<MaskPixelT>(filename, hdu);
423  }
424 
433  static Mask readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU) {
434  return Mask<MaskPixelT>(manager, hdu);
435  }
436 
438  static std::string interpret(MaskPixelT value);
439  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
440 
441  // Mask Plane ops
442 
444  void clearAllMaskPlanes();
446  void clearMaskPlane(int plane);
450  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
457  static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr<lsst::daf::base::PropertySet const> metadata);
458  //
459  // Operations on the mask plane dictionary
460  //
462  static void clearMaskPlaneDict();
463  static int addMaskPlane(const std::string& name);
464  static void removeMaskPlane(const std::string& name);
474  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault = false);
475 
481  static int getMaskPlane(const std::string& name);
487  static MaskPixelT getPlaneBitMask(const std::string& name);
488 
489  static int getNumPlanesMax() { return 8 * sizeof(MaskPixelT); }
490  static int getNumPlanesUsed();
494  MaskPlaneDict const& getMaskPlaneDict() const;
496  void printMaskPlanes() const;
497 
501  static void addMaskPlanesToMetadata(std::shared_ptr<lsst::daf::base::PropertySet>);
502  //
503  // This one isn't static, it fixes up a given Mask's planes
518  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
519 
520 private:
521  friend class MaskFitsReader;
522 
523  std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
524 
525  static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
526  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
527  static const std::string maskPlanePrefix;
528 
537  static int addMaskPlane(std::string name, int plane);
538 
540  static int getMaskPlaneNoThrow(const std::string& name);
542  static MaskPixelT getBitMaskNoThrow(int plane);
548  static MaskPixelT getBitMask(int plane);
549 
553  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
554 
555  //
556  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
557  //
560 
566  void checkMaskDictionaries(Mask const& other);
567 };
568 
569 template <typename PixelT>
570 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
571 } // namespace image
572 } // namespace afw
573 } // namespace lsst
574 
575 #endif // LSST_AFW_IMAGE_MASK_H
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
A FITS reader class for Masks.
Mask(Mask< OtherPixelT > const &rhs, const bool deep)
Definition: Mask.h:220
table::Key< int > b
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:58
Options for writing an image to FITS.
Definition: fits.h:219
std::string getAsString(int x, int y)
Definition: Mask.h:439
static Mask readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Read a Mask from a regular FITS file.
Definition: Mask.h:421
int y
Definition: SpanSet.cc:49
table::Key< int > a
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:79
ItemVariant const * other
Definition: Schema.cc:56
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:102
Mask< MaskPT > type
Return the desired type.
Definition: Mask.h:87
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:281
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
detail::Mask_tag image_category
Definition: Mask.h:81
std::shared_ptr< RecordT > src
Definition: Match.cc:48
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
A class used to request that array accesses be checked.
Definition: ImageBase.h:74
double x
void swap(Image< PixelT > &a, Image< PixelT > &b)
Definition: Image.cc:456
static int getNumPlanesMax()
Definition: Mask.h:489
Basic LSST definitions.
Mask subset(lsst::geom::Box2I const &bbox, ImageOrigin origin=PARENT) const
Return a subimage corresponding to the given box.
Definition: Mask.h:276
A templated class to return this classes&#39; type (present in Image/Mask/MaskedImage) ...
Definition: Mask.h:85
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
static Mask readFits(fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU)
Read a Mask from a FITS RAM file.
Definition: Mask.h:433
An integer coordinate rectangle.
Definition: Box.h:55
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18