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" 67 #include "boost/mpl/vector.hpp" 68 #include "boost/gil/gil_all.hpp" 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); }
111 bool empty()
const {
return _dict.empty(); }
113 void clear() { _dict.clear(); }
118 if (_dict.find(str) != _dict.end()) {
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) {}
174 for (MapWithHash::const_iterator ptr = begin(); ptr !=
end(); ++ptr) {
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) {
219 static bool first =
true;
222 setInitMaskBits(_defaultMaskDict);
227 return _defaultMaskDict;
231 _defaultMaskDict = newDefaultMaskDict;
233 return _defaultMaskDict;
236 void addDict(MapWithHash* dict) { _dicts[dict] = _dictCounter++; }
238 void eraseDict(MapWithHash* dict) { _dicts.erase(dict); }
242 addDict(_defaultMaskDict.get());
244 return _defaultMaskDict;
252 static DictState _state;
269 MapWithHash mwh(mpd);
271 _state.addDict(dict.
get());
278 return _state.setDefaultDict(dict);
284 _state.addDict(dict.
get());
303 int id = it->second + 1;
305 for (
int i = 0; i < id; ++i) {
306 MapWithHash::const_iterator
const it =
320 MapWithHash::const_iterator i = find(name);
322 return (i ==
end()) ? -1 : i->second;
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>
409 template <
typename MaskPixelT>
412 template <
typename MaskPixelT>
414 :
image::
ImageBase<MaskPixelT>(array, deep, xy0), _maskDict(detail::MaskDict::makeMaskDict()) {}
416 template <
typename PixelT>
421 swap(_maskDict, rhs._maskDict);
424 template <
typename PixelT>
429 template <
typename MaskPixelT>
437 template <
typename MaskPixelT>
442 template <
typename MaskPixelT>
449 #ifndef DOXYGEN // doc for this section is already in header 451 template <
typename MaskPixelT>
457 *
this =
Mask(fitsfile, metadata, bbox, origin, conformMasks);
460 template <
typename MaskPixelT>
467 *
this =
Mask(fitsfile, metadata, bbox, origin, conformMasks);
470 template <
typename MaskPixelT>
475 typedef boost::mpl::vector<unsigned char, unsigned short, short, std::int32_t> fits_mask_types;
481 fits_read_image<fits_mask_types>(fitsfile, *
this, *metadata,
bbox, origin);
499 template <
typename MaskPixelT>
507 template <
typename MaskPixelT>
515 template <
typename MaskPixelT>
521 template <
typename MaskPixelT>
532 template <
typename MaskPixelT>
543 template <
typename MaskPixelT>
550 std::make_shared<dafBase::PropertySet>();
552 fitsfile.
writeImage(*
this, options, useHeader);
558 struct addPlaneFunctor {
562 detail::MaskPlaneDict::const_iterator
const it =
564 dict->begin(), dict->end(),
566 std::placeholders::_1),
568 if (it != dict->end()) {
572 if (dict->find(
_name) == dict->end()) {
582 template <
typename MaskPixelT>
585 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
586 for (MaskPlaneDict::const_iterator iter = mpd.
begin(); iter != mpd.
end(); ++iter) {
587 if (value & getBitMask(iter->second)) {
588 if (result.
size() > 0) {
591 result += iter->first;
597 template <
typename MaskPixelT>
599 int id = getMaskPlaneNoThrow(name);
602 id = _maskPlaneDict()->getUnusedPlane();
609 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
612 _state.forEachMaskDict(addPlaneFunctor(name,
id));
617 template <
typename MaskPixelT>
622 str(boost::format(
"mask plane ID must be between 0 and %1%") % (
getNumPlanesMax() - 1)));
625 _maskPlaneDict()->add(name, planeId);
630 template <
typename MaskPixelT>
632 return _maskDict->getMaskPlaneDict();
635 template <
typename MaskPixelT>
639 str(boost::format(
"Plane %s doesn't exist in the default Mask") % name));
643 _maskPlaneDict()->erase(name);
646 template <
typename MaskPixelT>
655 _maskDict = _maskDict->clone();
658 _maskDict->erase(name);
665 template <
typename MaskPixelT>
667 return (planeId >= 0 && planeId <
getNumPlanesMax()) ? (1 << planeId) : 0;
670 template <
typename MaskPixelT>
672 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
674 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
675 if (planeId == i->second) {
676 MaskPixelT
const bitmask = getBitMaskNoThrow(planeId);
684 str(boost::format(
"Invalid mask plane ID: %d") % planeId));
687 template <
typename MaskPixelT>
689 int const plane = getMaskPlaneNoThrow(name);
693 str(boost::format(
"Invalid mask plane name: %s") % name));
699 template <
typename MaskPixelT>
704 template <
typename MaskPixelT>
709 template <
typename MaskPixelT>
711 MaskPixelT mpix = 0x0;
718 template <
typename MaskPixelT>
720 return _maskPlaneDict()->size();
723 template <
typename MaskPixelT>
725 _maskPlaneDict()->clear();
728 template <
typename MaskPixelT>
733 template <
typename MaskPixelT>
735 *
this &= ~getBitMask(planeId);
738 template <
typename MaskPixelT>
742 if (*_maskDict == *currentMD) {
750 MaskPixelT keepBitmask = 0;
751 MaskPixelT canonicalMask[
sizeof(MaskPixelT) * 8];
752 MaskPixelT currentMask[
sizeof(MaskPixelT) * 8];
755 for (MaskPlaneDict::const_iterator i = currentPlaneDict.
begin(); i != currentPlaneDict.
end(); i++) {
757 int const currentPlaneNumber = i->second;
758 int canonicalPlaneNumber = getMaskPlaneNoThrow(name);
760 if (canonicalPlaneNumber < 0) {
764 if (canonicalPlaneNumber == currentPlaneNumber) {
765 keepBitmask |= getBitMask(canonicalPlaneNumber);
767 canonicalMask[numReMap] = getBitMask(canonicalPlaneNumber);
768 currentMask[numReMap] = getBitMaskNoThrow(currentPlaneNumber);
775 for (
int r = 0; r != this->
getHeight(); ++r) {
778 MaskPixelT
const pixel = *ptr;
780 MaskPixelT newPixel = pixel & keepBitmask;
781 for (
int i = 0; i < numReMap; i++) {
782 if (pixel & currentMask[i]) newPixel |= canonicalMask[i];
794 template <
typename MaskPixelT>
799 template <
typename MaskPixelT>
805 template <
typename MaskPixelT>
810 template <
typename MaskPixelT>
816 template <
typename MaskPixelT>
822 template <
typename MaskPixelT>
828 template <
typename MaskPixelT>
830 if (*_maskDict != *other._maskDict) {
835 template <
typename MaskPixelT>
838 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l | val; });
842 template <
typename MaskPixelT>
844 checkMaskDictionaries(rhs);
848 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
852 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l | r; });
856 template <
typename MaskPixelT>
862 template <
typename MaskPixelT>
864 checkMaskDictionaries(rhs);
868 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
872 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l & r; });
876 template <
typename MaskPixelT>
879 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l ^ val; });
883 template <
typename MaskPixelT>
885 checkMaskDictionaries(rhs);
889 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
893 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l ^ r; });
897 template <
typename MaskPixelT>
899 MaskPixelT
const bitMask = getBitMask(planeId);
901 for (
int x = x0;
x <= x1;
x++) {
906 template <
typename MaskPixelT>
914 NameList paramNames = metadata->paramNames(
false);
915 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
916 if (i->compare(0, maskPlanePrefix.size(), maskPlanePrefix) == 0) {
917 metadata->remove(*i);
921 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
924 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
926 int const planeNumber = i->second;
928 if (planeName !=
"") {
929 metadata->add(maskPlanePrefix + planeName, planeNumber);
934 template <
typename MaskPixelT>
941 NameList paramNames = metadata->paramNames(
false);
942 int numPlanesUsed = 0;
945 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
946 if (i->compare(0, maskPlanePrefix.size(), maskPlanePrefix) == 0) {
949 int const planeId = metadata->getAsInt(*i);
951 MaskPlaneDict::const_iterator plane = newDict.
find(planeName);
952 if (plane != newDict.
end() && planeId != plane->second) {
955 for (MaskPlaneDict::const_iterator j = newDict.
begin(); j != newDict.
end(); ++j) {
956 if (planeId == j->second) {
958 str(boost::format(
"File specifies plane %s has same value (%d) as %s") %
959 planeName % planeId % j->first));
967 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
969 newDict[planeName] = planeId;
975 template <
typename MaskPixelT>
983 template <
typename MaskPixelT>
986 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.
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.
#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