47 #pragma clang diagnostic push 48 #pragma clang diagnostic ignored "-Wunused-variable" 49 #pragma clang diagnostic pop 50 #include "boost/format.hpp" 51 #include "boost/filesystem/path.hpp" 53 #include "boost/functional/hash.hpp" 55 #include "lsst/daf/base.h" 56 #include "lsst/daf/base/Citizen.h" 57 #include "lsst/pex/exceptions.h" 58 #include "lsst/log/Log.h" 67 #include "boost/mpl/vector.hpp" 68 #include "boost/gil/gil_all.hpp" 74 namespace pexExcept = lsst::pex::exceptions;
84 void setInitMaskBits(std::shared_ptr<detail::MaskDict> dict);
93 typedef detail::MaskPlaneDict::value_type value_type;
94 typedef detail::MaskPlaneDict::const_iterator const_iterator;
97 : _dict(dict), _hash(_calcHash()) {}
100 bool operator==(MapWithHash
const& rhs)
const {
return _hash == rhs._hash; }
102 const_iterator begin()
const {
return _dict.begin(); }
103 const_iterator
end()
const {
return _dict.end(); }
104 const_iterator find(detail::MaskPlaneDict::key_type
const&
name)
const {
return _dict.find(name); }
106 void add(std::string
const& str,
int val) {
111 bool empty()
const {
return _dict.empty(); }
113 void clear() { _dict.clear(); }
115 std::size_t size()
const {
return _dict.size(); }
117 void erase(std::string
const& str) {
118 if (_dict.find(str) != _dict.end()) {
126 std::size_t getHash()
const {
return _hash; }
133 std::size_t _calcHash() {
135 for (const_iterator ptr = begin(); ptr !=
end(); ++ptr) {
136 _hash = (_hash << 1) ^
137 boost::hash<std::string>()((*ptr).first + str(boost::format(
"%d") % ptr->second));
144 bool operator!=(MapWithHash
const& lhs, MapWithHash
const& rhs) {
return !(lhs == rhs); }
155 friend class ::lsst::afw::image::DictState;
159 MaskDict(MapWithHash
const* dict) : MapWithHash(*dict) {}
164 static std::shared_ptr<MaskDict>
setDefaultDict(std::shared_ptr<MaskDict> dict);
166 std::shared_ptr<MaskDict>
clone()
const;
174 for (MapWithHash::const_iterator ptr = begin(); ptr !=
end(); ++ptr) {
175 std::cout <<
"Plane " << ptr->second <<
" -> " << ptr->first << std::endl;
192 typedef std::map<MapWithHash*, int> HandleList;
198 _dicts[_defaultMaskDict.get()] = _dictCounter++;
202 _defaultMaskDict.reset();
204 for (HandleList::iterator ptr = _dicts.begin(); ptr != _dicts.end(); ++ptr) {
210 template <
typename FunctorT>
211 void forEachMaskDict(FunctorT func) {
212 for (HandleList::const_iterator ptr = _dicts.begin(); ptr != _dicts.end(); ++ptr) {
218 std::shared_ptr<detail::MaskDict> getDefaultDict() {
219 static bool first =
true;
222 setInitMaskBits(_defaultMaskDict);
227 return _defaultMaskDict;
230 std::shared_ptr<detail::MaskDict>
setDefaultDict(std::shared_ptr<detail::MaskDict> newDefaultMaskDict) {
231 _defaultMaskDict = newDefaultMaskDict;
233 return _defaultMaskDict;
236 void addDict(MapWithHash* dict) { _dicts[dict] = _dictCounter++; }
238 void eraseDict(MapWithHash* dict) { _dicts.erase(dict); }
241 _defaultMaskDict = std::shared_ptr<detail::MaskDict>(
new detail::MaskDict(*_defaultMaskDict.get()));
242 addDict(_defaultMaskDict.get());
244 return _defaultMaskDict;
247 std::shared_ptr<detail::MaskDict> _defaultMaskDict;
252 static DictState _state;
266 std::shared_ptr<MaskDict> dict = _state.getDefaultDict();
269 MapWithHash mwh(mpd);
270 dict = std::shared_ptr<MaskDict>(
new MaskDict(&mwh));
271 _state.addDict(dict.get());
278 return _state.setDefaultDict(dict);
282 std::shared_ptr<MaskDict> dict(
new MaskDict(*
this));
284 _state.addDict(dict.get());
298 MapWithHash::const_iterator
const it = std::max_element(
303 int id = it->second + 1;
305 for (
int i = 0; i < id; ++i) {
306 MapWithHash::const_iterator
const it =
309 std::bind(std::equal_to<int>(),
320 MapWithHash::const_iterator i = find(name);
322 return (i ==
end()) ? -1 : i->second;
333 void setInitMaskBits(std::shared_ptr<detail::MaskDict> dict) {
335 dict->add(
"BAD", ++i);
336 dict->add(
"SAT", ++i);
337 dict->add(
"INTRP", ++i);
338 dict->add(
"CR", ++i);
339 dict->add(
"EDGE", ++i);
340 dict->add(
"DETECTED", ++i);
341 dict->add(
"DETECTED_NEGATIVE", ++i);
342 dict->add(
"SUSPECT", ++i);
343 dict->add(
"NO_DATA", ++i);
347 template <
typename MaskPixelT>
349 LOGL_DEBUG(
"afw.image.Mask",
"Number of mask planes: %d", getNumPlanesMax());
354 template <
typename MaskPixelT>
357 _initializePlanes(planeDefs);
361 template <
typename MaskPixelT>
365 _initializePlanes(planeDefs);
366 *
this = initialValue;
369 template <
typename MaskPixelT>
372 _initializePlanes(planeDefs);
376 template <
typename MaskPixelT>
380 _initializePlanes(planeDefs);
381 *
this = initialValue;
384 template <
typename MaskPixelT>
387 _initializePlanes(planeDefs);
391 template <
typename MaskPixelT>
394 _initializePlanes(planeDefs);
395 *
this = initialValue;
398 template <
typename MaskPixelT>
400 :
ImageBase<MaskPixelT>(rhs, bbox, origin, deep), _maskDict(rhs._maskDict) {}
402 template <
typename MaskPixelT>
404 :
ImageBase<MaskPixelT>(rhs, deep), _maskDict(rhs._maskDict) {}
406 template <
typename MaskPixelT>
408 :
image::
ImageBase<MaskPixelT>(array, deep, xy0), _maskDict(detail::MaskDict::makeMaskDict()) {}
410 template <
typename PixelT>
415 swap(_maskDict, rhs._maskDict);
418 template <
typename PixelT>
423 template <
typename MaskPixelT>
431 template <
typename MaskPixelT>
438 #ifndef DOXYGEN // doc for this section is already in header 440 template <
typename MaskPixelT>
441 Mask<MaskPixelT>::Mask(std::string
const& fileName,
int hdu, std::shared_ptr<daf::base::PropertySet> metadata,
446 *
this =
Mask(fitsfile, metadata, bbox, origin, conformMasks);
449 template <
typename MaskPixelT>
451 std::shared_ptr<daf::base::PropertySet> metadata,
afw::geom::Box2I const& bbox,
456 *
this =
Mask(fitsfile, metadata, bbox, origin, conformMasks);
459 template <
typename MaskPixelT>
464 typedef boost::mpl::vector<unsigned char, unsigned short, short, std::int32_t> fits_mask_types;
470 fits_read_image<fits_mask_types>(fitsfile, *
this, *metadata, bbox, origin);
488 template <
typename MaskPixelT>
490 std::shared_ptr<lsst::daf::base::PropertySet const> metadata_i,
491 std::string
const& mode)
const {
496 template <
typename MaskPixelT>
498 std::shared_ptr<lsst::daf::base::PropertySet const> metadata_i,
499 std::string
const& mode)
const {
504 template <
typename MaskPixelT>
506 std::shared_ptr<lsst::daf::base::PropertySet const> metadata)
const {
510 template <
typename MaskPixelT>
512 std::string
const& filename,
514 std::string
const& mode,
515 std::shared_ptr<daf::base::PropertySet const> header
521 template <
typename MaskPixelT>
525 std::string
const& mode,
526 std::shared_ptr<daf::base::PropertySet const> header
532 template <
typename MaskPixelT>
536 std::shared_ptr<daf::base::PropertySet const> header
538 std::shared_ptr<daf::base::PropertySet> useHeader = header ? header->deepCopy() :
539 std::make_shared<dafBase::PropertySet>();
541 fitsfile.
writeImage(*
this, options, useHeader);
547 struct addPlaneFunctor {
548 addPlaneFunctor(std::string
const& name,
int id) :
_name(name),
_id(
id) {}
551 detail::MaskPlaneDict::const_iterator
const it =
553 dict->begin(), dict->end(),
555 std::placeholders::_1),
557 if (it != dict->end()) {
561 if (dict->find(
_name) == dict->end()) {
571 template <
typename MaskPixelT>
573 std::string result =
"";
574 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
575 for (MaskPlaneDict::const_iterator iter = mpd.begin(); iter != mpd.end(); ++iter) {
576 if (value & getBitMask(iter->second)) {
577 if (result.size() > 0) {
580 result += iter->first;
586 template <
typename MaskPixelT>
588 int id = getMaskPlaneNoThrow(name);
591 id = _maskPlaneDict()->getUnusedPlane();
597 throw LSST_EXCEPT(pexExcept::RuntimeError,
598 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
601 _state.forEachMaskDict(addPlaneFunctor(name,
id));
606 template <
typename MaskPixelT>
610 pexExcept::RangeError,
611 str(boost::format(
"mask plane ID must be between 0 and %1%") % (
getNumPlanesMax() - 1)));
614 _maskPlaneDict()->add(name, planeId);
619 template <
typename MaskPixelT>
621 return _maskDict->getMaskPlaneDict();
624 template <
typename MaskPixelT>
627 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
628 str(boost::format(
"Plane %s doesn't exist in the default Mask") % name));
632 _maskPlaneDict()->erase(name);
635 template <
typename MaskPixelT>
644 _maskDict = _maskDict->clone();
647 _maskDict->erase(name);
654 template <
typename MaskPixelT>
656 return (planeId >= 0 && planeId <
getNumPlanesMax()) ? (1 << planeId) : 0;
659 template <
typename MaskPixelT>
663 for (MaskPlaneDict::const_iterator i = mpd.begin(); i != mpd.end(); ++i) {
664 if (planeId == i->second) {
665 MaskPixelT
const bitmask = getBitMaskNoThrow(planeId);
672 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
673 str(boost::format(
"Invalid mask plane ID: %d") % planeId));
676 template <
typename MaskPixelT>
678 int const plane = getMaskPlaneNoThrow(name);
681 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
682 str(boost::format(
"Invalid mask plane name: %s") % name));
688 template <
typename MaskPixelT>
693 template <
typename MaskPixelT>
698 template <
typename MaskPixelT>
700 MaskPixelT mpix = 0x0;
701 for (std::vector<std::string>::const_iterator it = name.begin(); it != name.end(); ++it) {
707 template <
typename MaskPixelT>
709 return _maskPlaneDict()->size();
712 template <
typename MaskPixelT>
714 _maskPlaneDict()->clear();
717 template <
typename MaskPixelT>
722 template <
typename MaskPixelT>
724 *
this &= ~getBitMask(planeId);
727 template <
typename MaskPixelT>
731 if (*_maskDict == *currentMD) {
739 MaskPixelT keepBitmask = 0;
740 MaskPixelT canonicalMask[
sizeof(MaskPixelT) * 8];
741 MaskPixelT currentMask[
sizeof(MaskPixelT) * 8];
744 for (MaskPlaneDict::const_iterator i = currentPlaneDict.begin(); i != currentPlaneDict.end(); i++) {
745 std::string
const name = i->first;
746 int const currentPlaneNumber = i->second;
747 int canonicalPlaneNumber = getMaskPlaneNoThrow(name);
749 if (canonicalPlaneNumber < 0) {
753 if (canonicalPlaneNumber == currentPlaneNumber) {
754 keepBitmask |= getBitMask(canonicalPlaneNumber);
756 canonicalMask[numReMap] = getBitMask(canonicalPlaneNumber);
757 currentMask[numReMap] = getBitMaskNoThrow(currentPlaneNumber);
764 for (
int r = 0; r != this->
getHeight(); ++r) {
767 MaskPixelT
const pixel = *ptr;
769 MaskPixelT newPixel = pixel & keepBitmask;
770 for (
int i = 0; i < numReMap; i++) {
771 if (pixel & currentMask[i]) newPixel |= canonicalMask[i];
783 template <
typename MaskPixelT>
788 template <
typename MaskPixelT>
794 template <
typename MaskPixelT>
799 template <
typename MaskPixelT>
805 template <
typename MaskPixelT>
811 template <
typename MaskPixelT>
817 template <
typename MaskPixelT>
819 if (*_maskDict != *other._maskDict) {
820 throw LSST_EXCEPT(pexExcept::RuntimeError,
"Mask dictionaries do not match");
824 template <
typename MaskPixelT>
827 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l | val; });
831 template <
typename MaskPixelT>
833 checkMaskDictionaries(rhs);
836 throw LSST_EXCEPT(pexExcept::LengthError,
837 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
841 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l | r; });
845 template <
typename MaskPixelT>
851 template <
typename MaskPixelT>
853 checkMaskDictionaries(rhs);
856 throw LSST_EXCEPT(pexExcept::LengthError,
857 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
861 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l & r; });
865 template <
typename MaskPixelT>
868 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l ^ val; });
872 template <
typename MaskPixelT>
874 checkMaskDictionaries(rhs);
877 throw LSST_EXCEPT(pexExcept::LengthError,
878 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
882 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l ^ r; });
886 template <
typename MaskPixelT>
888 MaskPixelT
const bitMask = getBitMask(planeId);
890 for (
int x = x0;
x <= x1;
x++) {
895 template <
typename MaskPixelT>
898 throw LSST_EXCEPT(pexExcept::InvalidParameterError,
"Null std::shared_ptr<PropertySet>");
902 typedef std::vector<std::string> NameList;
903 NameList paramNames = metadata->paramNames(
false);
904 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
905 if (i->compare(0, maskPlanePrefix.size(), maskPlanePrefix) == 0) {
906 metadata->remove(*i);
910 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
913 for (MaskPlaneDict::const_iterator i = mpd.begin(); i != mpd.end(); ++i) {
914 std::string
const& planeName = i->first;
915 int const planeNumber = i->second;
917 if (planeName !=
"") {
918 metadata->add(maskPlanePrefix + planeName, planeNumber);
923 template <
typename MaskPixelT>
925 std::shared_ptr<dafBase::PropertySet const> metadata) {
929 typedef std::vector<std::string> NameList;
930 NameList paramNames = metadata->paramNames(
false);
931 int numPlanesUsed = 0;
934 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
935 if (i->compare(0, maskPlanePrefix.size(), maskPlanePrefix) == 0) {
937 std::string planeName = i->substr(maskPlanePrefix.size());
938 int const planeId = metadata->getAsInt(*i);
940 MaskPlaneDict::const_iterator plane = newDict.find(planeName);
941 if (plane != newDict.end() && planeId != plane->second) {
942 throw LSST_EXCEPT(pexExcept::RuntimeError,
"File specifies plane " + planeName +
" twice");
944 for (MaskPlaneDict::const_iterator j = newDict.begin(); j != newDict.end(); ++j) {
945 if (planeId == j->second) {
946 throw LSST_EXCEPT(pexExcept::RuntimeError,
947 str(boost::format(
"File specifies plane %s has same value (%d) as %s") %
948 planeName % planeId % j->first));
955 pexExcept::RuntimeError,
956 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
958 newDict[planeName] = planeId;
964 template <
typename MaskPixelT>
972 template <
typename MaskPixelT>
975 template <
typename MaskPixelT>
static std::string interpret(MaskPixelT value)
Interpret a mask value as a comma-separated list of mask plane names.
static void clearMaskPlaneDict()
Reset the maskPlane dictionary.
table::Key< std::string > name
bool operator!=(Coord const &lhs, Coord const &rhs)
Inequality; the complement of equality.
static int getMaskPlane(const std::string &name)
Return the mask plane number corresponding to a plane name.
std::string const & _name
int getHeight() const
Return the number of rows in the image.
std::map< std::string, int > MaskPlaneDict
Options for writing an image to FITS.
Mask & operator=(MaskPixelT const rhs)
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
detail::MaskPlaneDict MaskPlaneDict
bool operator==(CoordKey const &lhs, CoordKey const &rhs)
Compare CoordKeys for equality using the constituent Keys.
static void addMaskPlanesToMetadata(std::shared_ptr< lsst::daf::base::PropertySet >)
Given a PropertySet, replace any existing MaskPlane assignments with the current ones.
static int getNumPlanesUsed()
lsst::daf::base::PropertyList PropertyList
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
The base class for all image classed (Image, Mask, MaskedImage, ...)
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...
int getUnusedPlane() const
static MaskPlaneDict parseMaskPlaneMetadata(std::shared_ptr< lsst::daf::base::PropertySet const > metadata)
Given a PropertySet that contains the MaskPlane assignments, setup the MaskPlanes.
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y'th row.
ImageBase< MaskPixelT >::PixelReference operator()(int x, int y)
get a reference to the specified pixel
An integer coordinate rectangle.
iterator end() const
Return an STL compliant iterator to the end of the image.
Mask(unsigned int width, unsigned int height, MaskPlaneDict const &planeDefs=MaskPlaneDict())
Construct a Mask initialized to 0x0.
int getMaskPlane(const std::string &name) const
afw::table::PointKey< int > dimensions
geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
A base class for image defects.
Represent a 2-dimensional array of bitmask pixels.
Lifetime-management for memory that goes into FITS memory files.
void setMaskPlaneValues(const int plane, const int x0, const int x1, const int y)
Set the bit specified by "planeId" for pixels (x0, y) ...
A class used to request that array accesses be checked.
static MaskPixelT getPlaneBitMask(const std::vector< std::string > &names)
Return the bitmask corresponding to a vector of plane names OR'd together.
void swap(ImageBase &rhs)
MaskPlaneDict const & getMaskPlaneDict() const
Return the Mask's maskPlaneDict.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
static void listMaskDicts()
void swap(Image< PixelT > &a, Image< PixelT > &b)
static std::shared_ptr< MaskDict > incrDefaultVersion()
static void removeMaskPlane(const std::string &name)
static std::shared_ptr< MaskDict > makeMaskDict()
static int getNumPlanesMax()
Mask & operator &=(Mask const &rhs)
AND a Mask into a Mask.
int getWidth() const
Return the number of columns in the image.
std::shared_ptr< MaskDict > clone() const
Mask & operator|=(Mask const &rhs)
OR a Mask into a Mask.
x_iterator row_end(int y) const
Return an x_iterator to the end of the y'th row.
table::PointKey< int > pixel
static std::shared_ptr< MaskDict > setDefaultDict(std::shared_ptr< MaskDict > dict)
void conformMaskPlanes(const MaskPlaneDict &masterPlaneDict)
Adjust this mask to conform to the standard Mask class's mask plane dictionary, adding any new mask p...
void writeFits(std::string const &fileName, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet >(), std::string const &mode="w") const
Write a mask to a regular FITS file.
Mask & operator^=(Mask const &rhs)
XOR a Mask into a Mask.
void clearMaskPlane(int plane)
Clear the specified bit in all pixels.
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
_view_t _getRawView() const
void clearAllMaskPlanes()
Clear all the pixels.
static int addMaskPlane(const std::string &name)
PixelReference operator()(int x, int y)
Return a reference to the pixel (x, y)
void printMaskPlanes() const
print the mask plane dictionary to std::cout