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>
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>
446 *
this =
Mask(fitsfile, metadata, bbox, origin, conformMasks);
449 template <
typename MaskPixelT>
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>
496 template <
typename MaskPixelT>
504 template <
typename MaskPixelT>
510 template <
typename MaskPixelT>
521 template <
typename MaskPixelT>
532 template <
typename MaskPixelT>
539 std::make_shared<dafBase::PropertySet>();
541 fitsfile.
writeImage(*
this, options, useHeader);
547 struct addPlaneFunctor {
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>
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();
598 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
601 _state.forEachMaskDict(addPlaneFunctor(name,
id));
606 template <
typename MaskPixelT>
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>
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>
661 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
663 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
664 if (planeId == i->second) {
665 MaskPixelT
const bitmask = getBitMaskNoThrow(planeId);
673 str(boost::format(
"Invalid mask plane ID: %d") % planeId));
676 template <
typename MaskPixelT>
678 int const plane = getMaskPlaneNoThrow(name);
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;
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++) {
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) {
824 template <
typename MaskPixelT>
827 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l | val; });
831 template <
typename MaskPixelT>
833 checkMaskDictionaries(rhs);
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);
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);
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>
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) {
915 int const planeNumber = i->second;
917 if (planeName !=
"") {
918 metadata->add(maskPlanePrefix + planeName, planeNumber);
923 template <
typename MaskPixelT>
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) {
938 int const planeId = metadata->getAsInt(*i);
940 MaskPlaneDict::const_iterator plane = newDict.
find(planeName);
941 if (plane != newDict.
end() && planeId != plane->second) {
944 for (MaskPlaneDict::const_iterator j = newDict.
begin(); j != newDict.
end(); ++j) {
945 if (planeId == j->second) {
947 str(boost::format(
"File specifies plane %s has same value (%d) as %s") %
948 planeName % planeId % j->first));
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.
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