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" 64 template <
typename MaskPixelT>
65 void Mask<MaskPixelT>::_initializePlanes(
MaskPlaneDict const& planeDefs) {
66 LOGL_DEBUG(
"afw.image.Mask",
"Number of mask planes: %d", getNumPlanesMax());
71 template <
typename MaskPixelT>
74 _initializePlanes(planeDefs);
78 template <
typename MaskPixelT>
82 _initializePlanes(planeDefs);
86 template <
typename MaskPixelT>
89 _initializePlanes(planeDefs);
93 template <
typename MaskPixelT>
97 _initializePlanes(planeDefs);
101 template <
typename MaskPixelT>
104 _initializePlanes(planeDefs);
108 template <
typename MaskPixelT>
111 _initializePlanes(planeDefs);
112 *
this = initialValue;
115 template <
typename MaskPixelT>
118 :
ImageBase<MaskPixelT>(rhs, bbox, origin, deep), _maskDict(rhs._maskDict) {}
120 template <
typename MaskPixelT>
122 :
ImageBase<MaskPixelT>(rhs, deep), _maskDict(rhs._maskDict) {}
124 template <
typename MaskPixelT>
127 template <
typename MaskPixelT>
130 template <
typename MaskPixelT>
133 :
image::
ImageBase<MaskPixelT>(array, deep, xy0), _maskDict(detail::MaskDict::getDefault()) {}
135 template <
typename PixelT>
140 swap(_maskDict, rhs._maskDict);
143 template <
typename PixelT>
148 template <
typename MaskPixelT>
156 template <
typename MaskPixelT>
161 template <
typename MaskPixelT>
168 #ifndef DOXYGEN // doc for this section is already in header 170 template <
typename MaskPixelT>
175 *
this = reader.
read<MaskPixelT>(
bbox, origin, conformMasks, allowUnsafe);
181 template <
typename MaskPixelT>
184 ImageOrigin origin,
bool conformMasks,
bool allowUnsafe)
187 *
this = reader.
read<MaskPixelT>(
bbox, origin, conformMasks, allowUnsafe);
193 template <
typename MaskPixelT>
199 *
this = reader.
read<MaskPixelT>(
bbox, origin, conformMasks, allowUnsafe);
205 template <
typename MaskPixelT>
213 template <
typename MaskPixelT>
221 template <
typename MaskPixelT>
227 template <
typename MaskPixelT>
235 template <
typename MaskPixelT>
243 template <
typename MaskPixelT>
247 header ? header->deepCopy() : std::make_shared<dafBase::PropertySet>();
249 fitsfile.
writeImage(*
this, options, useHeader);
255 template <
typename MaskPixelT>
258 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
259 for (MaskPlaneDict::const_iterator iter = mpd.
begin(); iter != mpd.
end(); ++iter) {
260 if (value & getBitMask(iter->second)) {
261 if (result.
size() > 0) {
264 result += iter->first;
270 template <
typename MaskPixelT>
272 int id = getMaskPlaneNoThrow(name);
275 id = _maskPlaneDict()->getUnusedPlane();
282 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
290 template <
typename MaskPixelT>
295 str(boost::format(
"mask plane ID must be between 0 and %1%") % (
getNumPlanesMax() - 1)));
298 _maskPlaneDict()->add(name, planeId);
303 template <
typename MaskPixelT>
305 return _maskDict->getMaskPlaneDict();
308 template <
typename MaskPixelT>
312 str(boost::format(
"Plane %s doesn't exist in the default Mask") % name));
316 _maskPlaneDict()->erase(name);
319 template <
typename MaskPixelT>
328 _maskDict = _maskDict->clone();
331 _maskDict->erase(name);
338 template <
typename MaskPixelT>
340 return (planeId >= 0 && planeId <
getNumPlanesMax()) ? (1 << planeId) : 0;
343 template <
typename MaskPixelT>
345 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
347 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
348 if (planeId == i->second) {
349 MaskPixelT
const bitmask = getBitMaskNoThrow(planeId);
357 str(boost::format(
"Invalid mask plane ID: %d") % planeId));
360 template <
typename MaskPixelT>
362 int const plane = getMaskPlaneNoThrow(name);
366 str(boost::format(
"Invalid mask plane name: %s") % name));
372 template <
typename MaskPixelT>
374 return _maskPlaneDict()->getMaskPlane(name);
377 template <
typename MaskPixelT>
382 template <
typename MaskPixelT>
384 MaskPixelT mpix = 0x0;
391 template <
typename MaskPixelT>
393 return _maskPlaneDict()->size();
396 template <
typename MaskPixelT>
398 _maskPlaneDict()->clear();
401 template <
typename MaskPixelT>
406 template <
typename MaskPixelT>
408 *
this &= ~getBitMask(planeId);
411 template <
typename MaskPixelT>
415 if (*_maskDict == *currentMD) {
423 MaskPixelT keepBitmask = 0;
424 MaskPixelT canonicalMask[
sizeof(MaskPixelT) * 8];
425 MaskPixelT currentMask[
sizeof(MaskPixelT) * 8];
428 for (MaskPlaneDict::const_iterator i = currentPlaneDict.
begin(); i != currentPlaneDict.
end(); i++) {
430 int const currentPlaneNumber = i->second;
431 int canonicalPlaneNumber = getMaskPlaneNoThrow(name);
433 if (canonicalPlaneNumber < 0) {
437 if (canonicalPlaneNumber == currentPlaneNumber) {
438 keepBitmask |= getBitMask(canonicalPlaneNumber);
440 canonicalMask[numReMap] = getBitMask(canonicalPlaneNumber);
441 currentMask[numReMap] = getBitMaskNoThrow(currentPlaneNumber);
448 for (
int r = 0; r != this->
getHeight(); ++r) {
451 MaskPixelT
const pixel = *ptr;
453 MaskPixelT newPixel = pixel & keepBitmask;
454 for (
int i = 0; i < numReMap; i++) {
455 if (pixel & currentMask[i]) newPixel |= canonicalMask[i];
467 template <
typename MaskPixelT>
472 template <
typename MaskPixelT>
478 template <
typename MaskPixelT>
483 template <
typename MaskPixelT>
489 template <
typename MaskPixelT>
495 template <
typename MaskPixelT>
501 template <
typename MaskPixelT>
503 if (*_maskDict != *other._maskDict) {
508 template <
typename MaskPixelT>
511 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l | val; });
515 template <
typename MaskPixelT>
517 checkMaskDictionaries(rhs);
521 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
525 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l | r; });
529 template <
typename MaskPixelT>
535 template <
typename MaskPixelT>
537 checkMaskDictionaries(rhs);
541 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
545 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l & r; });
549 template <
typename MaskPixelT>
552 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l ^ val; });
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>
572 MaskPixelT
const bitMask = getBitMask(planeId);
574 for (
int x = x0;
x <= x1;
x++) {
579 template <
typename MaskPixelT>
587 NameList paramNames = metadata->paramNames(
false);
588 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
589 if (i->compare(0, maskPlanePrefix.
size(), maskPlanePrefix) == 0) {
590 metadata->remove(*i);
594 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
597 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
599 int const planeNumber = i->second;
601 if (planeName !=
"") {
602 metadata->add(maskPlanePrefix + planeName, planeNumber);
607 template <
typename MaskPixelT>
614 NameList paramNames = metadata->paramNames(
false);
615 int numPlanesUsed = 0;
618 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
619 if (i->compare(0, maskPlanePrefix.
size(), maskPlanePrefix) == 0) {
622 int const planeId = metadata->getAsInt(*i);
624 MaskPlaneDict::const_iterator plane = newDict.
find(planeName);
625 if (plane != newDict.
end() && planeId != plane->second) {
628 for (MaskPlaneDict::const_iterator j = newDict.
begin(); j != newDict.
end(); ++j) {
629 if (planeId == j->second) {
631 str(boost::format(
"File specifies plane %s has same value (%d) as %s") %
632 planeName % planeId % j->first));
640 str(boost::format(
"Max number of planes (%1%) already used") %
getNumPlanesMax()));
642 newDict[planeName] = planeId;
648 template <
typename MaskPixelT>
656 template <
typename MaskPixelT>
659 template <
typename MaskPixelT>
Mask< PixelT > read(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool allowUnsafe=false)
Read the Mask.
static std::shared_ptr< MaskDict > getDefault()
A FITS reader class for Masks.
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.
std::shared_ptr< daf::base::PropertyList > readMetadata()
Read the image's FITS header.
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.
#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