lsst.afw  16.0-12-gf08c31002
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);
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  // LSST_PERSIST_FORMATTER(lsst::afw::formatters::MaskFormatter)
525  std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
526 
527  static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
528  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
529  static const std::string maskPlanePrefix;
530 
539  static int addMaskPlane(std::string name, int plane);
540 
542  static int getMaskPlaneNoThrow(const std::string& name);
544  static MaskPixelT getBitMaskNoThrow(int plane);
550  static MaskPixelT getBitMask(int plane);
551 
555  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
556 
557  //
558  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
559  //
562 
568  void checkMaskDictionaries(Mask const& other);
569 };
570 
571 template <typename PixelT>
572 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
573 } // namespace image
574 } // namespace afw
575 } // namespace lsst
576 
577 #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: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
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.
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: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:492
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:277
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:436
int y
Definition: SpanSet.cc:44
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18