lsst.afw  14.0-59-g0c7cc230b
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 }
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());
122  explicit Mask(geom::Extent2I const& dimensions = geom::Extent2I(),
123  MaskPlaneDict const& planeDefs = MaskPlaneDict());
131  explicit Mask(geom::Extent2I const& dimensions, MaskPixelT initialValue,
132  MaskPlaneDict const& planeDefs = MaskPlaneDict());
139  explicit Mask(geom::Box2I const& bbox, MaskPlaneDict const& planeDefs = MaskPlaneDict());
147  explicit Mask(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  geom::Box2I const& bbox = geom::Box2I(), ImageOrigin origin = PARENT,
172  bool conformMasks = false);
173 
192  explicit Mask(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU,
195  geom::Box2I const& bbox = geom::Box2I(), ImageOrigin origin = PARENT,
196  bool conformMasks = false);
197 
213  explicit Mask(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet> metadata =
215  geom::Box2I const& bbox = geom::Box2I(), ImageOrigin origin = PARENT,
216  bool conformMasks = 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();
240  Mask(const Mask& src, const 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,
244  geom::Point2I const& xy0 = geom::Point2I());
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);
267  static MaskPixelT getPlaneBitMask(const std::vector<std::string>& names);
268 
270  Mask& operator^=(Mask const& rhs);
272  Mask& operator^=(MaskPixelT const rhs);
273 
280  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y);
287  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y) const;
295  bool operator()(int x, int y, int plane) const;
303  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y, CheckIndices const& check);
311  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y,
312  CheckIndices const& check) const;
321  bool operator()(int x, int y, int plane, CheckIndices const& check) const;
322 
330  void writeFits(std::string const& fileName, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
332  std::string const& mode = "w") const;
333 
341  void writeFits(fits::MemFileManager& manager,
344  std::string const& mode = "w") const;
345 
352  void writeFits(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
354 
363  void writeFits(
364  std::string const& filename,
366  std::string const& mode = "w",
368  ) const;
369 
378  void writeFits(
379  fits::MemFileManager& manager,
380  fits::ImageWriteOptions const& options,
381  std::string const& mode = "w",
383  ) const;
384 
392  void writeFits(
393  fits::Fits& fitsfile,
394  fits::ImageWriteOptions const& options,
396  ) const;
397 
398 
407  static Mask readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU) {
408  return Mask<MaskPixelT>(filename, hdu);
409  }
410 
419  static Mask readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU) {
420  return Mask<MaskPixelT>(manager, hdu);
421  }
422 
424  static std::string interpret(MaskPixelT value);
425  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
426 
427  // Mask Plane ops
428 
430  void clearAllMaskPlanes();
432  void clearMaskPlane(int plane);
436  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
443  static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr<lsst::daf::base::PropertySet const> metadata);
444  //
445  // Operations on the mask plane dictionary
446  //
448  static void clearMaskPlaneDict();
449  static int addMaskPlane(const std::string& name);
450  static void removeMaskPlane(const std::string& name);
460  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault = false);
461 
467  static int getMaskPlane(const std::string& name);
473  static MaskPixelT getPlaneBitMask(const std::string& name);
474 
475  static int getNumPlanesMax() { return 8 * sizeof(MaskPixelT); }
476  static int getNumPlanesUsed();
480  MaskPlaneDict const& getMaskPlaneDict() const;
482  void printMaskPlanes() const;
483 
487  static void addMaskPlanesToMetadata(std::shared_ptr<lsst::daf::base::PropertySet>);
488  //
489  // This one isn't static, it fixes up a given Mask's planes
504  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
505 
506 private:
507  // LSST_PERSIST_FORMATTER(lsst::afw::formatters::MaskFormatter)
508  std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
509 
510  static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
511  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
512  static const std::string maskPlanePrefix;
513 
522  static int addMaskPlane(std::string name, int plane);
523 
525  static int getMaskPlaneNoThrow(const std::string& name);
527  static MaskPixelT getBitMaskNoThrow(int plane);
533  static MaskPixelT getBitMask(int plane);
534 
538  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
539 
540  //
541  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
542  //
545 
551  void checkMaskDictionaries(Mask const& other);
552 };
553 
554 template <typename PixelT>
555 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
556 }
557 }
558 } // lsst::afw::image
559 
560 #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:220
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:65
Options for writing an image to FITS.
Definition: fits.h:220
std::string getAsString(int x, int y)
Definition: Mask.h:425
static Mask readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Read a Mask from a regular FITS file.
Definition: Mask.h:407
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:86
tuple options
void swap(Mask< PixelT > &a, Mask< PixelT > &b)
Definition: Mask.cc:424
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:298
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:106
Mask< MaskPT > type
Return the desired type.
Definition: Mask.h:94
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:84
detail::Mask_tag image_category
Definition: Mask.h:88
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:78
double x
static int getNumPlanesMax()
Definition: Mask.h:475
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:419
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18