LSST Applications g0b6bd0c080+a72a5dd7e6,g1182afd7b4+2a019aa3bb,g17e5ecfddb+2b8207f7de,g1d67935e3f+06cf436103,g38293774b4+ac198e9f13,g396055baef+6a2097e274,g3b44f30a73+6611e0205b,g480783c3b1+98f8679e14,g48ccf36440+89c08d0516,g4b93dc025c+98f8679e14,g5c4744a4d9+a302e8c7f0,g613e996a0d+e1c447f2e0,g6c8d09e9e7+25247a063c,g7271f0639c+98f8679e14,g7a9cd813b8+124095ede6,g9d27549199+a302e8c7f0,ga1cf026fa3+ac198e9f13,ga32aa97882+7403ac30ac,ga786bb30fb+7a139211af,gaa63f70f4e+9994eb9896,gabf319e997+ade567573c,gba47b54d5d+94dc90c3ea,gbec6a3398f+06cf436103,gc6308e37c7+07dd123edb,gc655b1545f+ade567573c,gcc9029db3c+ab229f5caf,gd01420fc67+06cf436103,gd877ba84e5+06cf436103,gdb4cecd868+6f279b5b48,ge2d134c3d5+cc4dbb2e3f,ge448b5faa6+86d1ceac1d,gecc7e12556+98f8679e14,gf3ee170dca+25247a063c,gf4ac96e456+ade567573c,gf9f5ea5b4d+ac198e9f13,gff490e6085+8c2580be5c,w.2022.27
LSST Data Management Base Package
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"
40#include "lsst/pex/exceptions.h"
44
45namespace lsst {
46namespace afw {
47namespace image {
48
49namespace detail {
50class MaskDict; // forward declaration
51}
52
53// all masks will initially be instantiated with the same pixel type
54namespace detail {
56struct Mask_tag : public detail::basic_tag {};
57
59} // namespace detail
60
76template <typename MaskPixelT = lsst::afw::image::MaskPixel>
77class Mask : public ImageBase<MaskPixelT> {
78public:
80
82
84 template <typename MaskPT = MaskPixelT>
88 };
89
90 // Constructors
98 explicit Mask(unsigned int width, unsigned int height, MaskPlaneDict const& planeDefs = MaskPlaneDict());
107 explicit Mask(unsigned int width, unsigned int height, MaskPixelT initialValue,
108 MaskPlaneDict const& planeDefs = MaskPlaneDict());
116 MaskPlaneDict const& planeDefs = MaskPlaneDict());
124 explicit Mask(lsst::geom::Extent2I const& dimensions, MaskPixelT initialValue,
125 MaskPlaneDict const& planeDefs = MaskPlaneDict());
132 explicit Mask(lsst::geom::Box2I const& bbox, MaskPlaneDict const& planeDefs = MaskPlaneDict());
140 explicit Mask(lsst::geom::Box2I const& bbox, MaskPixelT initialValue,
141 MaskPlaneDict const& planeDefs = MaskPlaneDict());
142
163 explicit Mask(std::string const& fileName, int hdu = fits::DEFAULT_HDU,
167 bool conformMasks = false, bool allowUnsafe = false);
168
189 explicit Mask(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU,
193 bool conformMasks = false, bool allowUnsafe = false);
194
212 explicit Mask(fits::Fits& fitsfile,
216 bool conformMasks = false, bool allowUnsafe = 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() override;
240 Mask(const Mask& src, const lsst::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,
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);
262
277 return Mask(*this, bbox, origin, false);
278 }
279
281 Mask operator[](lsst::geom::Box2I const& bbox) const { return subset(bbox); }
282
284
290 static MaskPixelT getPlaneBitMask(const std::vector<std::string>& names);
291
293 Mask& operator^=(Mask const& rhs);
295 Mask& operator^=(MaskPixelT const rhs);
296
318 bool operator()(int x, int y, int plane) const;
326 typename ImageBase<MaskPixelT>::PixelReference operator()(int x, int y, CheckIndices const& check);
335 CheckIndices const& check) const;
344 bool operator()(int x, int y, int plane, CheckIndices const& check) const;
345
347
354 void writeFits(std::string const& fileName,
355 daf::base::PropertySet const * metadata = nullptr,
356 std::string const& mode = "w") const;
357 [[deprecated("Replaced by a non-shared_ptr overload. Will be removed after v25.")]]
358 void writeFits(std::string const& fileName,
360 std::string const& mode = "w") const;
362
364
372 daf::base::PropertySet const * metadata = nullptr,
373 std::string const& mode = "w") const;
374 [[deprecated("Replaced by a non-shared_ptr overload. Will be removed after v25.")]]
377 std::string const& mode = "w") const;
379
381
387 void writeFits(fits::Fits& fitsfile, daf::base::PropertySet const * metadata = nullptr) const;
388 [[deprecated("Replaced by a non-shared_ptr overload. Will be removed after v25.")]]
389 void writeFits(fits::Fits& fitsfile,
392
394
402 void writeFits(std::string const& filename, fits::ImageWriteOptions const& options,
403 std::string const& mode = "w",
404 daf::base::PropertySet const * header = nullptr) const;
405 [[deprecated("Replaced by a non-shared_ptr overload. Will be removed after v25.")]]
406 void writeFits(std::string const& filename, fits::ImageWriteOptions const& options,
407 std::string const& mode,
410
412
421 std::string const& mode = "w",
422 daf::base::PropertySet const * header = nullptr) const;
423 [[deprecated("Replaced by a non-shared_ptr overload. Will be removed after v25.")]]
425 std::string const& mode,
428
430
437 void writeFits(fits::Fits& fitsfile, fits::ImageWriteOptions const& options,
438 daf::base::PropertySet const * header = nullptr) const;
439 [[deprecated("Replaced by a non-shared_ptr overload. Will be removed after v25.")]]
440 void writeFits(fits::Fits& fitsfile, fits::ImageWriteOptions const& options,
443
452 static Mask readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU) {
453 return Mask<MaskPixelT>(filename, hdu);
454 }
455
465 return Mask<MaskPixelT>(manager, hdu);
466 }
467
469 static std::string interpret(MaskPixelT value);
470 std::string getAsString(int x, int y) { return interpret((*this)(x, y)); }
471
472 // Mask Plane ops
473
475 void clearAllMaskPlanes();
477 void clearMaskPlane(int plane);
481 void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y);
489 //
490 // Operations on the mask plane dictionary
491 //
493 static void clearMaskPlaneDict();
494 static int addMaskPlane(const std::string& name);
495 static void removeMaskPlane(const std::string& name);
505 void removeAndClearMaskPlane(const std::string& name, bool const removeFromDefault = false);
506
512 static int getMaskPlane(const std::string& name);
518 static MaskPixelT getPlaneBitMask(const std::string& name);
519
520 static int getNumPlanesMax() { return 8 * sizeof(MaskPixelT); }
521 static int getNumPlanesUsed();
525 MaskPlaneDict const& getMaskPlaneDict() const;
527 void printMaskPlanes() const;
528
533 //
534 // This one isn't static, it fixes up a given Mask's planes
549 void conformMaskPlanes(const MaskPlaneDict& masterPlaneDict);
550
551private:
552 friend class MaskFitsReader;
553
554 std::shared_ptr<detail::MaskDict> _maskDict; // our bitplane dictionary
555
556 static std::shared_ptr<detail::MaskDict> _maskPlaneDict();
557 static int _setMaskPlaneDict(MaskPlaneDict const& mpd);
558 static const std::string maskPlanePrefix;
559
568 static int addMaskPlane(std::string name, int plane);
569
571 static int getMaskPlaneNoThrow(const std::string& name);
573 static MaskPixelT getBitMaskNoThrow(int plane);
579 static MaskPixelT getBitMask(int plane);
580
584 void _initializePlanes(MaskPlaneDict const& planeDefs); // called by ctors
585
586 //
587 // Make names in templatized base class visible (Meyers, Effective C++, Item 43)
588 //
591
597 void checkMaskDictionaries(Mask const& other);
598};
599
600template <typename PixelT>
602} // namespace image
603} // namespace afw
604} // namespace lsst
605
606#endif // LSST_AFW_IMAGE_MASK_H
table::Key< std::string > name
Definition: Amplifier.cc:116
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
double x
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:48
std::shared_ptr< RecordT > src
Definition: Match.cc:48
int y
Definition: SpanSet.cc:48
table::Key< int > b
table::Key< int > a
Basic LSST definitions.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
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:74
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:102
typename Reference< PixelT >::type PixelReference
A Reference to a PixelT.
Definition: ImageBase.h:117
typename ConstReference< PixelT >::type PixelConstReference
A ConstReference to a PixelT.
Definition: ImageBase.h:119
A FITS reader class for Masks.
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
void swap(Mask &rhs)
Definition: Mask.cc:126
void writeFits(std::string const &fileName, std::shared_ptr< daf::base::PropertySet const > metadata, std::string const &mode="w") const
void writeFits(fits::MemFileManager &manager, fits::ImageWriteOptions const &options, std::string const &mode="w", daf::base::PropertySet const *header=nullptr) const
Write a mask to a FITS RAM file.
void writeFits(fits::MemFileManager &manager, std::shared_ptr< daf::base::PropertySet const > metadata, std::string const &mode="w") const
Mask & operator=(MaskPixelT const rhs)
Definition: Mask.cc:152
void printMaskPlanes() const
print the mask plane dictionary to std::cout
Definition: Mask.cc:678
static Mask readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Read a Mask from a regular FITS file.
Definition: Mask.h:452
static int getMaskPlane(const std::string &name)
Return the mask plane number corresponding to a plane name.
Definition: Mask.cc:390
detail::MaskPlaneDict MaskPlaneDict
Definition: Mask.h:79
static std::string interpret(MaskPixelT value)
Interpret a mask value as a comma-separated list of mask plane names.
Definition: Mask.cc:285
static void removeMaskPlane(const std::string &name)
Definition: Mask.cc:338
void removeAndClearMaskPlane(const std::string &name, bool const removeFromDefault=false)
Clear all pixels of the specified mask and remove the plane from the mask plane dictionary; optionall...
Definition: Mask.cc:349
Mask(Mask< OtherPixelT > const &rhs, const bool deep)
Definition: Mask.h:220
static Mask readFits(fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU)
Read a Mask from a FITS RAM file.
Definition: Mask.h:464
static void clearMaskPlaneDict()
Reset the maskPlane dictionary.
Definition: Mask.cc:426
void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y)
Set the bit specified by "planeId" for pixels (x0, y) ... (x1, y)
Definition: Mask.cc:600
static void addMaskPlanesToMetadata(std::shared_ptr< lsst::daf::base::PropertySet >)
Given a PropertySet, replace any existing MaskPlane assignments with the current ones.
Definition: Mask.cc:609
Mask subset(lsst::geom::Box2I const &bbox, ImageOrigin origin=PARENT) const
Return a subimage corresponding to the given box.
Definition: Mask.h:276
ImageBase< MaskPixelT >::PixelReference operator()(int x, int y)
get a reference to the specified pixel
Definition: Mask.cc:497
Mask & operator^=(Mask const &rhs)
XOR a Mask into a Mask.
Definition: Mask.cc:586
void writeFits(fits::Fits &fitsfile, std::shared_ptr< daf::base::PropertySet const > metadata) const
static int addMaskPlane(const std::string &name)
Definition: Mask.cc:300
Mask(std::string const &fileName, int hdu=fits::DEFAULT_HDU, std::shared_ptr< lsst::daf::base::PropertySet > metadata=std::shared_ptr< lsst::daf::base::PropertySet >(), lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
Construct a Mask by reading a regular FITS file.
void writeFits(std::string const &filename, fits::ImageWriteOptions const &options, std::string const &mode="w", daf::base::PropertySet const *header=nullptr) const
Write a mask to a regular FITS file.
void writeFits(fits::Fits &fitsfile, fits::ImageWriteOptions const &options, std::shared_ptr< daf::base::PropertySet const > header) const
void writeFits(fits::MemFileManager &manager, fits::ImageWriteOptions const &options, std::string const &mode, std::shared_ptr< daf::base::PropertySet const > header) const
static MaskPixelT getPlaneBitMask(const std::vector< std::string > &names)
Return the bitmask corresponding to a vector of plane names OR'd together.
Definition: Mask.cc:412
Mask(fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU, std::shared_ptr< lsst::daf::base::PropertySet > metadata=std::shared_ptr< lsst::daf::base::PropertySet >(), lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
Construct a Mask by reading a FITS image in memory.
Mask(unsigned int width, unsigned int height, MaskPlaneDict const &planeDefs=MaskPlaneDict())
Construct a Mask initialized to 0x0.
Definition: Mask.cc:62
std::string getAsString(int x, int y)
Definition: Mask.h:470
Mask(fits::Fits &fitsfile, std::shared_ptr< lsst::daf::base::PropertySet > metadata=std::shared_ptr< lsst::daf::base::PropertySet >(), lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
Construct a Mask from an already-open FITS object.
void writeFits(fits::MemFileManager &manager, daf::base::PropertySet const *metadata=nullptr, std::string const &mode="w") const
Write a mask to a FITS RAM file.
MaskPlaneDict const & getMaskPlaneDict() const
Return the Mask's maskPlaneDict.
Definition: Mask.cc:333
void conformMaskPlanes(const MaskPlaneDict &masterPlaneDict)
Adjust this mask to conform to the standard Mask class's mask plane dictionary, adding any new mask p...
Definition: Mask.cc:441
void writeFits(std::string const &filename, fits::ImageWriteOptions const &options, std::string const &mode, std::shared_ptr< daf::base::PropertySet const > header) const
Mask & operator|=(Mask const &rhs)
OR a Mask into a Mask.
Definition: Mask.cc:545
Mask operator[](lsst::geom::Box2I const &bbox) const
Return a subimage corresponding to the given box (interpreted as PARENT coordinates).
Definition: Mask.h:281
static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr< lsst::daf::base::PropertySet const > metadata)
Given a PropertySet that contains the MaskPlane assignments, setup the MaskPlanes.
Definition: Mask.cc:637
void clearAllMaskPlanes()
Clear all the pixels.
Definition: Mask.cc:431
static int getNumPlanesUsed()
Definition: Mask.cc:421
void writeFits(fits::Fits &fitsfile, daf::base::PropertySet const *metadata=nullptr) const
Write a mask to an open FITS file object.
void clearMaskPlane(int plane)
Clear the specified bit in all pixels.
Definition: Mask.cc:436
static int getNumPlanesMax()
Definition: Mask.h:520
void writeFits(std::string const &fileName, daf::base::PropertySet const *metadata=nullptr, std::string const &mode="w") const
Write a mask to a regular FITS file.
void writeFits(fits::Fits &fitsfile, fits::ImageWriteOptions const &options, daf::base::PropertySet const *header=nullptr) const
Write a mask to an open FITS file object.
Mask & operator&=(Mask const &rhs)
AND a Mask into a Mask.
Definition: Mask.cc:565
Class for storing generic metadata.
Definition: PropertySet.h:66
An integer coordinate rectangle.
Definition: Box.h:55
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
std::map< std::string, int > MaskPlaneDict
Definition: Mask.h:58
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
void swap(Image< PixelT > &a, Image< PixelT > &b)
Definition: Image.cc:483
A base class for image defects.
Options for writing an image to FITS.
Definition: fits.h:219
A templated class to return this classes' type (present in Image/Mask/MaskedImage)
Definition: Mask.h:85