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" 66 #include "boost/mpl/vector.hpp" 67 #include "boost/gil/gil_all.hpp" 92 typedef detail::MaskPlaneDict::value_type value_type;
93 typedef detail::MaskPlaneDict::const_iterator const_iterator;
96 : _dict(dict), _hash(_calcHash()) {}
99 bool operator==(MapWithHash
const& rhs)
const {
return _hash == rhs._hash; }
101 const_iterator
begin()
const {
return _dict.begin(); }
102 const_iterator
end()
const {
return _dict.end(); }
103 const_iterator
find(detail::MaskPlaneDict::key_type
const&
name)
const {
return _dict.find(name); }
110 bool empty()
const {
return _dict.empty(); }
112 void clear() { _dict.clear(); }
117 if (_dict.find(str) != _dict.end()) {
134 for (const_iterator ptr =
begin(); ptr !=
end(); ++ptr) {
135 _hash = (_hash << 1) ^
136 boost::hash<std::string>()((*ptr).first + str(boost::format(
"%d") % ptr->second));
143 bool operator!=(MapWithHash
const& lhs, MapWithHash
const& rhs) {
return !(lhs == rhs); }
154 friend class ::lsst::afw::image::DictState;
158 MaskDict(MapWithHash
const* dict) : MapWithHash(*dict) {}
173 for (MapWithHash::const_iterator ptr = begin(); ptr !=
end(); ++ptr) {
197 _dicts[_defaultMaskDict.get()] = _dictCounter++;
201 _defaultMaskDict.reset();
203 for (HandleList::iterator ptr = _dicts.begin(); ptr != _dicts.end(); ++ptr) {
209 template <
typename FunctorT>
210 void forEachMaskDict(FunctorT func) {
211 for (HandleList::const_iterator ptr = _dicts.begin(); ptr != _dicts.end(); ++ptr) {
218 static bool first =
true;
221 setInitMaskBits(_defaultMaskDict);
226 return _defaultMaskDict;
230 _defaultMaskDict = newDefaultMaskDict;
232 return _defaultMaskDict;
235 void addDict(MapWithHash* dict) { _dicts[dict] = _dictCounter++; }
237 void eraseDict(MapWithHash* dict) { _dicts.erase(dict); }
241 addDict(_defaultMaskDict.get());
243 return _defaultMaskDict;
251 static DictState _state;
268 MapWithHash mwh(mpd);
270 _state.addDict(dict.
get());
277 return _state.setDefaultDict(dict);
283 _state.addDict(dict.
get());
302 int id = it->second + 1;
304 for (
int i = 0; i < id; ++i) {
305 MapWithHash::const_iterator
const it =
319 MapWithHash::const_iterator i = find(name);
321 return (i ==
end()) ? -1 : i->second;
334 dict->add(
"BAD", ++i);
335 dict->add(
"SAT", ++i);
336 dict->add(
"INTRP", ++i);
337 dict->add(
"CR", ++i);
338 dict->add(
"EDGE", ++i);
339 dict->add(
"DETECTED", ++i);
340 dict->add(
"DETECTED_NEGATIVE", ++i);
341 dict->add(
"SUSPECT", ++i);
342 dict->add(
"NO_DATA", ++i);
346 template <
typename MaskPixelT>
348 LOGL_DEBUG(
"afw.image.Mask",
"Number of mask planes: %d", getNumPlanesMax());
353 template <
typename MaskPixelT>
356 _initializePlanes(planeDefs);
360 template <
typename MaskPixelT>
364 _initializePlanes(planeDefs);
365 *
this = initialValue;
368 template <
typename MaskPixelT>
371 _initializePlanes(planeDefs);
375 template <
typename MaskPixelT>
379 _initializePlanes(planeDefs);
380 *
this = initialValue;
383 template <
typename MaskPixelT>
386 _initializePlanes(planeDefs);
390 template <
typename MaskPixelT>
393 _initializePlanes(planeDefs);
394 *
this = initialValue;
397 template <
typename MaskPixelT>
399 :
ImageBase<MaskPixelT>(rhs, bbox, origin, deep), _maskDict(rhs._maskDict) {}
401 template <
typename MaskPixelT>
403 :
ImageBase<MaskPixelT>(rhs, deep), _maskDict(rhs._maskDict) {}
405 template <
typename MaskPixelT>
408 template <
typename MaskPixelT>
411 template <
typename MaskPixelT>
413 :
image::
ImageBase<MaskPixelT>(array, deep, xy0), _maskDict(detail::MaskDict::makeMaskDict()) {}
415 template <
typename PixelT>
420 swap(_maskDict, rhs._maskDict);
423 template <
typename PixelT>
428 template <
typename MaskPixelT>
436 template <
typename MaskPixelT>
441 template <
typename MaskPixelT>
448 #ifndef DOXYGEN // doc for this section is already in header 450 template <
typename MaskPixelT>
456 *
this =
Mask(fitsfile, metadata, bbox, origin, conformMasks);
459 template <
typename MaskPixelT>
466 *
this =
Mask(fitsfile, metadata, bbox, origin, conformMasks);
469 template <
typename MaskPixelT>
474 typedef boost::mpl::vector<unsigned char, unsigned short, short, std::int32_t> fits_mask_types;
480 fits_read_image<fits_mask_types>(fitsfile, *
this, *metadata,
bbox, origin);
498 template <
typename MaskPixelT>
506 template <
typename MaskPixelT>
514 template <
typename MaskPixelT>
520 template <
typename MaskPixelT>
531 template <
typename MaskPixelT>
542 template <
typename MaskPixelT>
549 std::make_shared<dafBase::PropertySet>();
551 fitsfile.
writeImage(*
this, options, useHeader);
557 struct addPlaneFunctor {
561 detail::MaskPlaneDict::const_iterator
const it =
563 dict->begin(), dict->end(),
565 std::placeholders::_1),
567 if (it != dict->end()) {
571 if (dict->find(
_name) == dict->end()) {
581 template <
typename MaskPixelT>
584 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
585 for (MaskPlaneDict::const_iterator iter = mpd.
begin(); iter != mpd.
end(); ++iter) {
586 if (value & getBitMask(iter->second)) {
587 if (result.
size() > 0) {
590 result += iter->first;
596 template <
typename MaskPixelT>
598 int id = getMaskPlaneNoThrow(name);
601 id = _maskPlaneDict()->getUnusedPlane();
608 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
611 _state.forEachMaskDict(addPlaneFunctor(name,
id));
616 template <
typename MaskPixelT>
621 str(boost::format(
"mask plane ID must be between 0 and %1%") % (
getNumPlanesMax() - 1)));
624 _maskPlaneDict()->add(name, planeId);
629 template <
typename MaskPixelT>
631 return _maskDict->getMaskPlaneDict();
634 template <
typename MaskPixelT>
638 str(boost::format(
"Plane %s doesn't exist in the default Mask") % name));
642 _maskPlaneDict()->erase(name);
645 template <
typename MaskPixelT>
654 _maskDict = _maskDict->clone();
657 _maskDict->erase(name);
664 template <
typename MaskPixelT>
666 return (planeId >= 0 && planeId <
getNumPlanesMax()) ? (1 << planeId) : 0;
669 template <
typename MaskPixelT>
671 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
673 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
674 if (planeId == i->second) {
675 MaskPixelT
const bitmask = getBitMaskNoThrow(planeId);
683 str(boost::format(
"Invalid mask plane ID: %d") % planeId));
686 template <
typename MaskPixelT>
688 int const plane = getMaskPlaneNoThrow(name);
692 str(boost::format(
"Invalid mask plane name: %s") % name));
698 template <
typename MaskPixelT>
703 template <
typename MaskPixelT>
708 template <
typename MaskPixelT>
710 MaskPixelT mpix = 0x0;
717 template <
typename MaskPixelT>
719 return _maskPlaneDict()->size();
722 template <
typename MaskPixelT>
724 _maskPlaneDict()->clear();
727 template <
typename MaskPixelT>
732 template <
typename MaskPixelT>
734 *
this &= ~getBitMask(planeId);
737 template <
typename MaskPixelT>
741 if (*_maskDict == *currentMD) {
749 MaskPixelT keepBitmask = 0;
750 MaskPixelT canonicalMask[
sizeof(MaskPixelT) * 8];
751 MaskPixelT currentMask[
sizeof(MaskPixelT) * 8];
754 for (MaskPlaneDict::const_iterator i = currentPlaneDict.
begin(); i != currentPlaneDict.
end(); i++) {
756 int const currentPlaneNumber = i->second;
757 int canonicalPlaneNumber = getMaskPlaneNoThrow(name);
759 if (canonicalPlaneNumber < 0) {
763 if (canonicalPlaneNumber == currentPlaneNumber) {
764 keepBitmask |= getBitMask(canonicalPlaneNumber);
766 canonicalMask[numReMap] = getBitMask(canonicalPlaneNumber);
767 currentMask[numReMap] = getBitMaskNoThrow(currentPlaneNumber);
774 for (
int r = 0; r != this->
getHeight(); ++r) {
777 MaskPixelT
const pixel = *ptr;
779 MaskPixelT newPixel = pixel & keepBitmask;
780 for (
int i = 0; i < numReMap; i++) {
781 if (pixel & currentMask[i]) newPixel |= canonicalMask[i];
793 template <
typename MaskPixelT>
798 template <
typename MaskPixelT>
804 template <
typename MaskPixelT>
809 template <
typename MaskPixelT>
815 template <
typename MaskPixelT>
821 template <
typename MaskPixelT>
827 template <
typename MaskPixelT>
829 if (*_maskDict != *other._maskDict) {
834 template <
typename MaskPixelT>
837 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l | val; });
841 template <
typename MaskPixelT>
843 checkMaskDictionaries(rhs);
847 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
851 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l | r; });
855 template <
typename MaskPixelT>
861 template <
typename MaskPixelT>
863 checkMaskDictionaries(rhs);
867 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
871 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l & r; });
875 template <
typename MaskPixelT>
878 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l ^ val; });
882 template <
typename MaskPixelT>
884 checkMaskDictionaries(rhs);
888 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
892 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l ^ r; });
896 template <
typename MaskPixelT>
898 MaskPixelT
const bitMask = getBitMask(planeId);
900 for (
int x = x0;
x <= x1;
x++) {
905 template <
typename MaskPixelT>
913 NameList paramNames = metadata->paramNames(
false);
914 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
915 if (i->compare(0, maskPlanePrefix.size(), maskPlanePrefix) == 0) {
916 metadata->remove(*i);
920 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
923 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
925 int const planeNumber = i->second;
927 if (planeName !=
"") {
928 metadata->add(maskPlanePrefix + planeName, planeNumber);
933 template <
typename MaskPixelT>
940 NameList paramNames = metadata->paramNames(
false);
941 int numPlanesUsed = 0;
944 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
945 if (i->compare(0, maskPlanePrefix.size(), maskPlanePrefix) == 0) {
948 int const planeId = metadata->getAsInt(*i);
950 MaskPlaneDict::const_iterator plane = newDict.
find(planeName);
951 if (plane != newDict.
end() && planeId != plane->second) {
954 for (MaskPlaneDict::const_iterator j = newDict.
begin(); j != newDict.
end(); ++j) {
955 if (planeId == j->second) {
957 str(boost::format(
"File specifies plane %s has same value (%d) as %s") %
958 planeName % planeId % j->first));
966 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
968 newDict[planeName] = planeId;
974 template <
typename MaskPixelT>
982 template <
typename MaskPixelT>
985 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
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.
bool operator!=(CoordKey const &lhs, CoordKey const &rhs)
Compare CoordKeys for equality using the constituent Keys.
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()
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.
#define LOGL_DEBUG(logger, message...)
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()
static std::shared_ptr< MaskDict > incrDefaultVersion()
#define LSST_EXCEPT(type,...)
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