LSSTApplications  18.1.0
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"
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 
164  explicit Mask(std::string const& fileName, int hdu = fits::DEFAULT_HDU,
167  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
168  bool conformMasks = false, bool allowUnsafe=false);
169 
190  explicit Mask(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU,
193  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
194  bool conformMasks = false, bool allowUnsafe=false);
195 
213  explicit Mask(fits::Fits& fitsfile,
216  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
217  bool conformMasks = false, bool allowUnsafe=false);
218 
219  // generalised copy constructor
220  template <typename OtherPixelT>
221  Mask(Mask<OtherPixelT> const& rhs, const bool deep)
222  : image::ImageBase<MaskPixelT>(rhs, deep), _maskDict(rhs._maskDict) {}
223 
230  Mask(const Mask& src, const bool deep = false);
231  Mask(Mask&& src);
232  ~Mask() override;
241  Mask(const Mask& src, const lsst::geom::Box2I& bbox, ImageOrigin const origin = PARENT,
242  const bool deep = false);
243 
244  explicit Mask(ndarray::Array<MaskPixelT, 2, 1> const& array, bool deep = false,
246 
247  void swap(Mask& rhs);
248  // Operators
249 
250  Mask& operator=(MaskPixelT const rhs);
251  Mask& operator=(const Mask& rhs);
252  Mask& operator=(Mask&& rhs);
253 
255  Mask& operator|=(Mask const& rhs);
257  Mask& operator|=(MaskPixelT const rhs);
258 
260  Mask& operator&=(Mask const& rhs);
262  Mask& operator&=(MaskPixelT const rhs);
263 
277  Mask subset(lsst::geom::Box2I const & bbox, ImageOrigin origin=PARENT) const {
278  return Mask(*this, bbox, origin, false);
279  }
280 
282  Mask operator[](lsst::geom::Box2I const & bbox) const {
283  return subset(bbox);
284  }
285 
287 
293  static MaskPixelT getPlaneBitMask(const std::vector<std::string>& names);
294 
296  Mask& operator^=(Mask const& rhs);
298  Mask& operator^=(MaskPixelT const rhs);
299 
306  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y);
313  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y) const;
321  bool operator()(int x, int y, int plane) const;
329  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y, CheckIndices const& check);
337  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y,
338  CheckIndices const& check) const;
347  bool operator()(int x, int y, int plane, CheckIndices const& check) const;
348 
356  void writeFits(std::string const& fileName,
359  std::string const& mode = "w") const;
360 
368  void writeFits(fits::MemFileManager& manager,
371  std::string const& mode = "w") const;
372 
379  void writeFits(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
381 
390  void writeFits(std::string const& filename, fits::ImageWriteOptions const& options,
391  std::string const& mode = "w",
392  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
393 
402  void writeFits(fits::MemFileManager& manager, fits::ImageWriteOptions const& options,
403  std::string const& mode = "w",
404  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
405 
413  void writeFits(fits::Fits& fitsfile, fits::ImageWriteOptions const& options,
414  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
415 
424  static Mask readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU) {
425  return Mask<MaskPixelT>(filename, hdu);
426  }
427 
436  static Mask readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU) {
437  return Mask<MaskPixelT>(manager, hdu);
438  }
439 
441  static std::string interpret(MaskPixelT value);
442  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
443 
444  // Mask Plane ops
445 
447  void clearAllMaskPlanes();
449  void clearMaskPlane(int plane);
453  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
460  static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr<lsst::daf::base::PropertySet const> metadata);
461  //
462  // Operations on the mask plane dictionary
463  //
465  static void clearMaskPlaneDict();
466  static int addMaskPlane(const std::string& name);
467  static void removeMaskPlane(const std::string& name);
477  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault = false);
478 
484  static int getMaskPlane(const std::string& name);
490  static MaskPixelT getPlaneBitMask(const std::string& name);
491 
492  static int getNumPlanesMax() { return 8 * sizeof(MaskPixelT); }
493  static int getNumPlanesUsed();
497  MaskPlaneDict const& getMaskPlaneDict() const;
499  void printMaskPlanes() const;
500 
504  static void addMaskPlanesToMetadata(std::shared_ptr<lsst::daf::base::PropertySet>);
505  //
506  // This one isn't static, it fixes up a given Mask's planes
521  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
522 
523 private:
524 
525  friend class MaskFitsReader;
526 
527  std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
528 
529  static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
530  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
531  static const std::string maskPlanePrefix;
532 
541  static int addMaskPlane(std::string name, int plane);
542 
544  static int getMaskPlaneNoThrow(const std::string& name);
546  static MaskPixelT getBitMaskNoThrow(int plane);
552  static MaskPixelT getBitMask(int plane);
553 
557  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
558 
559  //
560  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
561  //
564 
570  void checkMaskDictionaries(Mask const& other);
571 };
572 
573 template <typename PixelT>
574 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
575 } // namespace image
576 } // namespace afw
577 } // namespace lsst
578 
579 #endif // LSST_AFW_IMAGE_MASK_H
A FITS reader class for Masks.
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
Basic LSST definitions.
Mask(Mask< OtherPixelT > const &rhs, const bool deep)
Definition: Mask.h:221
table::Key< int > b
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:59
Options for writing an image to FITS.
Definition: fits.h:219
std::string getAsString(int x, int y)
Definition: Mask.h:442
static Mask readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Read a Mask from a regular FITS file.
Definition: Mask.h:424
int y
Definition: SpanSet.cc:49
table::Key< int > a
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:80
tuple options
Definition: lsstimport.py:47
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:103
Mask< MaskPT > type
Return the desired type.
Definition: Mask.h:88
STL class.
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:282
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:78
detail::Mask_tag image_category
Definition: Mask.h:82
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:75
table::Box2IKey bbox
Definition: Detector.cc:169
double x
std::shared_ptr< RecordT > src
Definition: Match.cc:48
void swap(Image< PixelT > &a, Image< PixelT > &b)
Definition: Image.cc:471
static int getNumPlanesMax()
Definition: Mask.h:492
ItemVariant const * other
Definition: Schema.cc:56
Mask subset(lsst::geom::Box2I const &bbox, ImageOrigin origin=PARENT) const
Return a subimage corresponding to the given box.
Definition: Mask.h:277
A templated class to return this classes&#39; type (present in Image/Mask/MaskedImage) ...
Definition: Mask.h:86
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:436
An integer coordinate rectangle.
Definition: Box.h:54
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18