35 #pragma clang diagnostic push 36 #pragma clang diagnostic ignored "-Wunused-variable" 37 #pragma clang diagnostic pop 38 #include "boost/format.hpp" 39 #include "boost/filesystem/path.hpp" 41 #include "boost/functional/hash.hpp" 55 #include "boost/mpl/vector.hpp" 56 #include "boost/gil/gil_all.hpp" 71 template <
typename MaskPixelT>
72 void Mask<MaskPixelT>::_initializePlanes(
MaskPlaneDict const& planeDefs) {
73 LOGL_DEBUG(
"afw.image.Mask",
"Number of mask planes: %d", getNumPlanesMax());
78 template <
typename MaskPixelT>
81 _initializePlanes(planeDefs);
85 template <
typename MaskPixelT>
89 _initializePlanes(planeDefs);
93 template <
typename MaskPixelT>
96 _initializePlanes(planeDefs);
100 template <
typename MaskPixelT>
104 _initializePlanes(planeDefs);
105 *
this = initialValue;
108 template <
typename MaskPixelT>
111 _initializePlanes(planeDefs);
115 template <
typename MaskPixelT>
118 _initializePlanes(planeDefs);
119 *
this = initialValue;
122 template <
typename MaskPixelT>
125 :
ImageBase<MaskPixelT>(rhs, bbox, origin, deep), _maskDict(rhs._maskDict) {}
127 template <
typename MaskPixelT>
129 :
ImageBase<MaskPixelT>(rhs, deep), _maskDict(rhs._maskDict) {}
131 template <
typename MaskPixelT>
134 template <
typename MaskPixelT>
137 template <
typename MaskPixelT>
140 :
image::
ImageBase<MaskPixelT>(array, deep, xy0), _maskDict(detail::MaskDict::getDefault()) {}
142 template <
typename PixelT>
147 swap(_maskDict, rhs._maskDict);
150 template <
typename PixelT>
155 template <
typename MaskPixelT>
163 template <
typename MaskPixelT>
168 template <
typename MaskPixelT>
175 #ifndef DOXYGEN // doc for this section is already in header 177 template <
typename MaskPixelT>
183 *
this =
Mask(fitsfile, metadata, bbox, origin, conformMasks);
186 template <
typename MaskPixelT>
193 *
this =
Mask(fitsfile, metadata, bbox, origin, conformMasks);
196 template <
typename MaskPixelT>
201 typedef boost::mpl::vector<unsigned char, unsigned short, short, std::int32_t> fits_mask_types;
207 fits_read_image<fits_mask_types>(fitsfile, *
this, *metadata,
bbox, origin);
226 template <
typename MaskPixelT>
234 template <
typename MaskPixelT>
242 template <
typename MaskPixelT>
248 template <
typename MaskPixelT>
256 template <
typename MaskPixelT>
264 template <
typename MaskPixelT>
268 header ? header->deepCopy() : std::make_shared<dafBase::PropertySet>();
270 fitsfile.
writeImage(*
this, options, useHeader);
276 template <
typename MaskPixelT>
279 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
280 for (MaskPlaneDict::const_iterator iter = mpd.
begin(); iter != mpd.
end(); ++iter) {
281 if (value & getBitMask(iter->second)) {
282 if (result.
size() > 0) {
285 result += iter->first;
291 template <
typename MaskPixelT>
293 int id = getMaskPlaneNoThrow(name);
296 id = _maskPlaneDict()->getUnusedPlane();
303 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
311 template <
typename MaskPixelT>
316 str(boost::format(
"mask plane ID must be between 0 and %1%") % (
getNumPlanesMax() - 1)));
319 _maskPlaneDict()->add(name, planeId);
324 template <
typename MaskPixelT>
326 return _maskDict->getMaskPlaneDict();
329 template <
typename MaskPixelT>
333 str(boost::format(
"Plane %s doesn't exist in the default Mask") % name));
337 _maskPlaneDict()->erase(name);
340 template <
typename MaskPixelT>
349 _maskDict = _maskDict->clone();
352 _maskDict->erase(name);
359 template <
typename MaskPixelT>
361 return (planeId >= 0 && planeId <
getNumPlanesMax()) ? (1 << planeId) : 0;
364 template <
typename MaskPixelT>
366 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
368 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
369 if (planeId == i->second) {
370 MaskPixelT
const bitmask = getBitMaskNoThrow(planeId);
378 str(boost::format(
"Invalid mask plane ID: %d") % planeId));
381 template <
typename MaskPixelT>
383 int const plane = getMaskPlaneNoThrow(name);
387 str(boost::format(
"Invalid mask plane name: %s") % name));
393 template <
typename MaskPixelT>
395 return _maskPlaneDict()->getMaskPlane(name);
398 template <
typename MaskPixelT>
403 template <
typename MaskPixelT>
405 MaskPixelT mpix = 0x0;
412 template <
typename MaskPixelT>
414 return _maskPlaneDict()->size();
417 template <
typename MaskPixelT>
419 _maskPlaneDict()->clear();
422 template <
typename MaskPixelT>
427 template <
typename MaskPixelT>
429 *
this &= ~getBitMask(planeId);
432 template <
typename MaskPixelT>
436 if (*_maskDict == *currentMD) {
444 MaskPixelT keepBitmask = 0;
445 MaskPixelT canonicalMask[
sizeof(MaskPixelT) * 8];
446 MaskPixelT currentMask[
sizeof(MaskPixelT) * 8];
449 for (MaskPlaneDict::const_iterator i = currentPlaneDict.
begin(); i != currentPlaneDict.
end(); i++) {
451 int const currentPlaneNumber = i->second;
452 int canonicalPlaneNumber = getMaskPlaneNoThrow(name);
454 if (canonicalPlaneNumber < 0) {
458 if (canonicalPlaneNumber == currentPlaneNumber) {
459 keepBitmask |= getBitMask(canonicalPlaneNumber);
461 canonicalMask[numReMap] = getBitMask(canonicalPlaneNumber);
462 currentMask[numReMap] = getBitMaskNoThrow(currentPlaneNumber);
469 for (
int r = 0; r != this->
getHeight(); ++r) {
472 MaskPixelT
const pixel = *ptr;
474 MaskPixelT newPixel = pixel & keepBitmask;
475 for (
int i = 0; i < numReMap; i++) {
476 if (pixel & currentMask[i]) newPixel |= canonicalMask[i];
488 template <
typename MaskPixelT>
493 template <
typename MaskPixelT>
499 template <
typename MaskPixelT>
504 template <
typename MaskPixelT>
510 template <
typename MaskPixelT>
516 template <
typename MaskPixelT>
522 template <
typename MaskPixelT>
524 if (*_maskDict != *other._maskDict) {
529 template <
typename MaskPixelT>
532 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l | val; });
536 template <
typename MaskPixelT>
538 checkMaskDictionaries(rhs);
542 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
546 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l | r; });
550 template <
typename MaskPixelT>
556 template <
typename MaskPixelT>
558 checkMaskDictionaries(rhs);
562 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
566 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l & r; });
570 template <
typename MaskPixelT>
573 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l ^ val; });
577 template <
typename MaskPixelT>
579 checkMaskDictionaries(rhs);
583 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
587 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l ^ r; });
591 template <
typename MaskPixelT>
593 MaskPixelT
const bitMask = getBitMask(planeId);
595 for (
int x = x0;
x <= x1;
x++) {
600 template <
typename MaskPixelT>
608 NameList paramNames = metadata->paramNames(
false);
609 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
610 if (i->compare(0, maskPlanePrefix.
size(), maskPlanePrefix) == 0) {
611 metadata->remove(*i);
615 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
618 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
620 int const planeNumber = i->second;
622 if (planeName !=
"") {
623 metadata->add(maskPlanePrefix + planeName, planeNumber);
628 template <
typename MaskPixelT>
635 NameList paramNames = metadata->paramNames(
false);
636 int numPlanesUsed = 0;
639 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
640 if (i->compare(0, maskPlanePrefix.
size(), maskPlanePrefix) == 0) {
643 int const planeId = metadata->getAsInt(*i);
645 MaskPlaneDict::const_iterator plane = newDict.
find(planeName);
646 if (plane != newDict.
end() && planeId != plane->second) {
649 for (MaskPlaneDict::const_iterator j = newDict.
begin(); j != newDict.
end(); ++j) {
650 if (planeId == j->second) {
652 str(boost::format(
"File specifies plane %s has same value (%d) as %s") %
653 planeName % planeId % j->first));
661 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
663 newDict[planeName] = planeId;
669 template <
typename MaskPixelT>
677 template <
typename MaskPixelT>
680 template <
typename MaskPixelT>
static std::shared_ptr< MaskDict > getDefault()
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.
static void setDefault(std::shared_ptr< MaskDict > dict)
static int getMaskPlane(const std::string &name)
Return the mask plane number corresponding to a plane 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.
static std::shared_ptr< MaskDict > detachDefault()
ItemVariant const * other
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...
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
static void addAllMasksPlane(std::string const &name, int bitId)
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.
afw::table::PointKey< int > dimensions
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)
std::unique_ptr< SchemaItem< U > > result
MaskPlaneDict const & getMaskPlaneDict() const
Return the Mask's maskPlaneDict.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
#define LSST_EXCEPT(type,...)
static void removeMaskPlane(const std::string &name)
static int getNumPlanesMax()
static std::shared_ptr< MaskDict > copyOrGetDefault(MaskPlaneDict const &dict)
Mask & operator &=(Mask const &rhs)
AND a Mask into a Mask.
int getWidth() const
Return the number of columns in the image.
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
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.
lsst::geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
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)
void printMaskPlanes() const
print the mask plane dictionary to std::cout