lsst.afw  15.0-33-g5c41f601f+4
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 #include "lsst/afw/fitsDefaults.h"
47 
48 namespace lsst {
49 namespace afw {
50 namespace formatters {
51 template <typename>
52 class MaskFormatter;
53 }
54 namespace image {
55 
56 namespace detail {
57 class MaskDict; // forward declaration
58 }
59 
60 // all masks will initially be instantiated with the same pixel type
61 namespace detail {
63 struct Mask_tag : public detail::basic_tag {};
64 
66 } // namespace detail
67 
83 template <typename MaskPixelT = lsst::afw::image::MaskPixel>
84 class Mask : public ImageBase<MaskPixelT> {
85 public:
87 
89 
91  template <typename MaskPT = MaskPixelT>
94  typedef Mask<MaskPT> type;
95  };
96 
97  // Constructors
105  explicit Mask(unsigned int width, unsigned int height, MaskPlaneDict const& planeDefs = MaskPlaneDict());
114  explicit Mask(unsigned int width, unsigned int height, MaskPixelT initialValue,
115  MaskPlaneDict const& planeDefs = MaskPlaneDict());
123  MaskPlaneDict const& planeDefs = MaskPlaneDict());
131  explicit Mask(lsst::geom::Extent2I const& dimensions, MaskPixelT initialValue,
132  MaskPlaneDict const& planeDefs = MaskPlaneDict());
139  explicit Mask(lsst::geom::Box2I const& bbox, MaskPlaneDict const& planeDefs = MaskPlaneDict());
147  explicit Mask(lsst::geom::Box2I const& bbox, MaskPixelT initialValue,
148  MaskPlaneDict const& planeDefs = MaskPlaneDict());
149 
168  explicit Mask(std::string const& fileName, int hdu = fits::DEFAULT_HDU,
171  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
172  bool conformMasks = false);
173 
192  explicit Mask(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU,
195  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
196  bool conformMasks = false);
197 
213  explicit Mask(fits::Fits& fitsfile,
216  lsst::geom::Box2I const& bbox = lsst::geom::Box2I(), ImageOrigin origin = PARENT,
217  bool conformMasks = 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();
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);
268  static MaskPixelT getPlaneBitMask(const std::vector<std::string>& names);
269 
271  Mask& operator^=(Mask const& rhs);
273  Mask& operator^=(MaskPixelT const rhs);
274 
281  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y);
288  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y) const;
296  bool operator()(int x, int y, int plane) const;
304  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y, CheckIndices const& check);
312  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y,
313  CheckIndices const& check) const;
322  bool operator()(int x, int y, int plane, CheckIndices const& check) const;
323 
331  void writeFits(std::string const& fileName,
334  std::string const& mode = "w") const;
335 
343  void writeFits(fits::MemFileManager& manager,
346  std::string const& mode = "w") const;
347 
354  void writeFits(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
356 
365  void writeFits(std::string const& filename, fits::ImageWriteOptions const& options,
366  std::string const& mode = "w",
367  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
368 
377  void writeFits(fits::MemFileManager& manager, fits::ImageWriteOptions const& options,
378  std::string const& mode = "w",
379  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
380 
388  void writeFits(fits::Fits& fitsfile, fits::ImageWriteOptions const& options,
389  std::shared_ptr<daf::base::PropertySet const> header = nullptr) const;
390 
399  static Mask readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU) {
400  return Mask<MaskPixelT>(filename, hdu);
401  }
402 
411  static Mask readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU) {
412  return Mask<MaskPixelT>(manager, hdu);
413  }
414 
416  static std::string interpret(MaskPixelT value);
417  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
418 
419  // Mask Plane ops
420 
422  void clearAllMaskPlanes();
424  void clearMaskPlane(int plane);
428  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
435  static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr<lsst::daf::base::PropertySet const> metadata);
436  //
437  // Operations on the mask plane dictionary
438  //
440  static void clearMaskPlaneDict();
441  static int addMaskPlane(const std::string& name);
442  static void removeMaskPlane(const std::string& name);
452  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault = false);
453 
459  static int getMaskPlane(const std::string& name);
465  static MaskPixelT getPlaneBitMask(const std::string& name);
466 
467  static int getNumPlanesMax() { return 8 * sizeof(MaskPixelT); }
468  static int getNumPlanesUsed();
472  MaskPlaneDict const& getMaskPlaneDict() const;
474  void printMaskPlanes() const;
475 
479  static void addMaskPlanesToMetadata(std::shared_ptr<lsst::daf::base::PropertySet>);
480  //
481  // This one isn't static, it fixes up a given Mask's planes
496  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
497 
498 private:
499  // LSST_PERSIST_FORMATTER(lsst::afw::formatters::MaskFormatter)
500  std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
501 
502  static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
503  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
504  static const std::string maskPlanePrefix;
505 
514  static int addMaskPlane(std::string name, int plane);
515 
517  static int getMaskPlaneNoThrow(const std::string& name);
519  static MaskPixelT getBitMaskNoThrow(int plane);
525  static MaskPixelT getBitMask(int plane);
526 
530  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
531 
532  //
533  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
534  //
537 
543  void checkMaskDictionaries(Mask const& other);
544 };
545 
546 template <typename PixelT>
547 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
548 } // namespace image
549 } // namespace afw
550 } // namespace lsst
551 
552 #endif // LSST_AFW_IMAGE_MASK_H
Mask(Mask< OtherPixelT > const &rhs, const bool deep)
Definition: Mask.h:221
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:65
Options for writing an image to FITS.
Definition: fits.h:218
std::string getAsString(int x, int y)
Definition: Mask.h:417
static Mask readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Read a Mask from a regular FITS file.
Definition: Mask.h:399
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:86
tuple options
ItemVariant const * other
Definition: Schema.cc:55
void swap(Mask< PixelT > &a, Mask< PixelT > &b)
Definition: Mask.cc:426
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:104
Mask< MaskPT > type
Return the desired type.
Definition: Mask.h:94
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:84
detail::Mask_tag image_category
Definition: Mask.h:88
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:76
double x
static int getNumPlanesMax()
Definition: Mask.h:467
table::Box2IKey bbox
table::Key< int > a
A templated class to return this classes&#39; type (present in Image/Mask/MaskedImage) ...
Definition: Mask.h:92
static Mask readFits(fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU)
Read a Mask from a FITS RAM file.
Definition: Mask.h:411
int y
Definition: SpanSet.cc:44
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18