lsst.afw  14.0-38-g278b05818
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);
238  Mask(const Mask& src, const geom::Box2I& bbox, ImageOrigin const origin = PARENT,
239  const bool deep = false);
240 
241  explicit Mask(ndarray::Array<MaskPixelT, 2, 1> const& array, bool deep = false,
242  geom::Point2I const& xy0 = geom::Point2I());
243 
244  void swap(Mask& rhs);
245  // Operators
246 
247  Mask& operator=(MaskPixelT const rhs);
248  Mask& operator=(const Mask& rhs);
249 
251  Mask& operator|=(Mask const& rhs);
253  Mask& operator|=(MaskPixelT const rhs);
254 
256  Mask& operator&=(Mask const& rhs);
258  Mask& operator&=(MaskPixelT const rhs);
264  static MaskPixelT getPlaneBitMask(const std::vector<std::string>& names);
265 
267  Mask& operator^=(Mask const& rhs);
269  Mask& operator^=(MaskPixelT const rhs);
270 
277  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y);
284  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y) const;
292  bool operator()(int x, int y, int plane) const;
300  typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y, CheckIndices const& check);
308  typename ImageBase<MaskPixelT>::PixelConstReference operator()(int x, int y,
309  CheckIndices const& check) const;
318  bool operator()(int x, int y, int plane, CheckIndices const& check) const;
319 
327  void writeFits(std::string const& fileName, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
329  std::string const& mode = "w") const;
330 
338  void writeFits(fits::MemFileManager& manager,
341  std::string const& mode = "w") const;
342 
349  void writeFits(fits::Fits& fitsfile, std::shared_ptr<lsst::daf::base::PropertySet const> metadata =
351 
360  void writeFits(
361  std::string const& filename,
363  std::string const& mode = "w",
365  ) const;
366 
375  void writeFits(
376  fits::MemFileManager& manager,
377  fits::ImageWriteOptions const& options,
378  std::string const& mode = "w",
380  ) const;
381 
389  void writeFits(
390  fits::Fits& fitsfile,
391  fits::ImageWriteOptions const& options,
393  ) const;
394 
395 
404  static Mask readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU) {
405  return Mask<MaskPixelT>(filename, hdu);
406  }
407 
416  static Mask readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU) {
417  return Mask<MaskPixelT>(manager, hdu);
418  }
419 
421  static std::string interpret(MaskPixelT value);
422  std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
423 
424  // Mask Plane ops
425 
427  void clearAllMaskPlanes();
429  void clearMaskPlane(int plane);
433  void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
440  static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr<lsst::daf::base::PropertySet const> metadata);
441  //
442  // Operations on the mask plane dictionary
443  //
445  static void clearMaskPlaneDict();
446  static int addMaskPlane(const std::string& name);
447  static void removeMaskPlane(const std::string& name);
457  void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault = false);
458 
464  static int getMaskPlane(const std::string& name);
470  static MaskPixelT getPlaneBitMask(const std::string& name);
471 
472  static int getNumPlanesMax() { return 8 * sizeof(MaskPixelT); }
473  static int getNumPlanesUsed();
477  MaskPlaneDict const& getMaskPlaneDict() const;
479  void printMaskPlanes() const;
480 
484  static void addMaskPlanesToMetadata(std::shared_ptr<lsst::daf::base::PropertySet>);
485  //
486  // This one isn't static, it fixes up a given Mask's planes
501  void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
502 
503 private:
504  // LSST_PERSIST_FORMATTER(lsst::afw::formatters::MaskFormatter)
505  std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
506 
507  static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
508  static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
509  static const std::string maskPlanePrefix;
510 
519  static int addMaskPlane(std::string name, int plane);
520 
522  static int getMaskPlaneNoThrow(const std::string& name);
524  static MaskPixelT getBitMaskNoThrow(int plane);
530  static MaskPixelT getBitMask(int plane);
531 
535  void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
536 
537  //
538  // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
539  //
542 
548  void checkMaskDictionaries(Mask const& other);
549 };
550 
551 template <typename PixelT>
552 void swap(Mask<PixelT>& a, Mask<PixelT>& b);
553 }
554 }
555 } // lsst::afw::image
556 
557 #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:202
std::string getAsString(int x, int y)
Definition: Mask.h:422
static Mask readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Read a Mask from a regular FITS file.
Definition: Mask.h:404
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:86
tuple options
void swap(Mask< PixelT > &a, Mask< PixelT > &b)
Definition: Mask.cc:419
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:280
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:102
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:472
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:416
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18