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" 61 template <
typename MaskPixelT>
62 void Mask<MaskPixelT>::_initializePlanes(
MaskPlaneDict const& planeDefs) {
63 LOGL_DEBUG(
"afw.image.Mask",
"Number of mask planes: %d", getNumPlanesMax());
68 template <
typename MaskPixelT>
71 _initializePlanes(planeDefs);
75 template <
typename MaskPixelT>
79 _initializePlanes(planeDefs);
83 template <
typename MaskPixelT>
86 _initializePlanes(planeDefs);
90 template <
typename MaskPixelT>
94 _initializePlanes(planeDefs);
98 template <
typename MaskPixelT>
101 _initializePlanes(planeDefs);
105 template <
typename MaskPixelT>
108 _initializePlanes(planeDefs);
109 *
this = initialValue;
112 template <
typename MaskPixelT>
115 :
ImageBase<MaskPixelT>(rhs, bbox, origin, deep), _maskDict(rhs._maskDict) {}
117 template <
typename MaskPixelT>
119 :
ImageBase<MaskPixelT>(rhs, deep), _maskDict(rhs._maskDict) {}
121 template <
typename MaskPixelT>
124 template <
typename MaskPixelT>
127 template <
typename MaskPixelT>
130 :
image::
ImageBase<MaskPixelT>(array, deep, xy0), _maskDict(detail::MaskDict::getDefault()) {}
132 template <
typename PixelT>
137 swap(_maskDict, rhs._maskDict);
140 template <
typename PixelT>
145 template <
typename MaskPixelT>
153 template <
typename MaskPixelT>
158 template <
typename MaskPixelT>
165 #ifndef DOXYGEN // doc for this section is already in header 167 template <
typename MaskPixelT>
172 *
this = reader.
read<MaskPixelT>(
bbox, origin, conformMasks, allowUnsafe);
178 template <
typename MaskPixelT>
181 ImageOrigin origin,
bool conformMasks,
bool allowUnsafe)
184 *
this = reader.
read<MaskPixelT>(
bbox, origin, conformMasks, allowUnsafe);
190 template <
typename MaskPixelT>
196 *
this = reader.
read<MaskPixelT>(
bbox, origin, conformMasks, allowUnsafe);
202 template <
typename MaskPixelT>
210 template <
typename MaskPixelT>
218 template <
typename MaskPixelT>
224 template <
typename MaskPixelT>
232 template <
typename MaskPixelT>
240 template <
typename MaskPixelT>
244 header ? header->deepCopy() : std::make_shared<dafBase::PropertySet>();
246 fitsfile.
writeImage(*
this, options, useHeader);
251 template <
typename MaskPixelT>
254 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
256 if (value & getBitMask(
iter->second)) {
257 if (result.
size() > 0) {
260 result +=
iter->first;
266 template <
typename MaskPixelT>
268 int id = getMaskPlaneNoThrow(name);
271 id = _maskPlaneDict()->getUnusedPlane();
286 template <
typename MaskPixelT>
294 _maskPlaneDict()->add(name, planeId);
299 template <
typename MaskPixelT>
301 return _maskDict->getMaskPlaneDict();
304 template <
typename MaskPixelT>
308 str(
boost::format(
"Plane %s doesn't exist in the default Mask") % name));
312 _maskPlaneDict()->erase(name);
315 template <
typename MaskPixelT>
324 _maskDict = _maskDict->clone();
327 _maskDict->erase(name);
334 template <
typename MaskPixelT>
336 return (planeId >= 0 && planeId <
getNumPlanesMax()) ? (1 << planeId) : 0;
339 template <
typename MaskPixelT>
341 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
343 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
344 if (planeId == i->second) {
345 MaskPixelT
const bitmask = getBitMaskNoThrow(planeId);
356 template <
typename MaskPixelT>
358 int const plane = getMaskPlaneNoThrow(name);
368 template <
typename MaskPixelT>
370 return _maskPlaneDict()->getMaskPlane(name);
373 template <
typename MaskPixelT>
378 template <
typename MaskPixelT>
380 MaskPixelT mpix = 0x0;
387 template <
typename MaskPixelT>
389 return _maskPlaneDict()->size();
392 template <
typename MaskPixelT>
394 _maskPlaneDict()->clear();
397 template <
typename MaskPixelT>
402 template <
typename MaskPixelT>
404 *
this &= ~getBitMask(planeId);
407 template <
typename MaskPixelT>
411 if (*_maskDict == *currentMD) {
419 MaskPixelT keepBitmask = 0;
420 MaskPixelT canonicalMask[
sizeof(MaskPixelT) * 8];
421 MaskPixelT currentMask[
sizeof(MaskPixelT) * 8];
424 for (MaskPlaneDict::const_iterator i = currentPlaneDict.
begin(); i != currentPlaneDict.
end(); i++) {
426 int const currentPlaneNumber = i->second;
427 int canonicalPlaneNumber = getMaskPlaneNoThrow(name);
429 if (canonicalPlaneNumber < 0) {
433 if (canonicalPlaneNumber == currentPlaneNumber) {
434 keepBitmask |= getBitMask(canonicalPlaneNumber);
436 canonicalMask[numReMap] = getBitMask(canonicalPlaneNumber);
437 currentMask[numReMap] = getBitMaskNoThrow(currentPlaneNumber);
444 for (
int r = 0; r != this->
getHeight(); ++r) {
449 MaskPixelT newPixel = pixel & keepBitmask;
450 for (
int i = 0; i < numReMap; i++) {
451 if (pixel & currentMask[i]) newPixel |= canonicalMask[i];
463 template <
typename MaskPixelT>
468 template <
typename MaskPixelT>
474 template <
typename MaskPixelT>
479 template <
typename MaskPixelT>
485 template <
typename MaskPixelT>
491 template <
typename MaskPixelT>
497 template <
typename MaskPixelT>
499 if (*_maskDict != *other._maskDict) {
504 template <
typename MaskPixelT>
507 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l |
val; });
511 template <
typename MaskPixelT>
513 checkMaskDictionaries(rhs);
517 str(
boost::format(
"Images are of different size, %dx%d v %dx%d") %
521 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l | r; });
525 template <
typename MaskPixelT>
531 template <
typename MaskPixelT>
533 checkMaskDictionaries(rhs);
537 str(
boost::format(
"Images are of different size, %dx%d v %dx%d") %
541 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l & r; });
545 template <
typename MaskPixelT>
548 [&val](MaskPixelT
const& l) -> MaskPixelT {
return l ^
val; });
552 template <
typename MaskPixelT>
554 checkMaskDictionaries(rhs);
558 str(
boost::format(
"Images are of different size, %dx%d v %dx%d") %
562 [](MaskPixelT
const& l, MaskPixelT
const& r) -> MaskPixelT {
return l ^ r; });
566 template <
typename MaskPixelT>
568 MaskPixelT
const bitMask = getBitMask(planeId);
570 for (
int x = x0;
x <= x1;
x++) {
575 template <
typename MaskPixelT>
583 NameList paramNames = metadata->paramNames(
false);
584 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
585 if (i->compare(0, maskPlanePrefix.
size(), maskPlanePrefix) == 0) {
586 metadata->remove(*i);
590 MaskPlaneDict const& mpd = _maskPlaneDict()->getMaskPlaneDict();
593 for (MaskPlaneDict::const_iterator i = mpd.
begin(); i != mpd.
end(); ++i) {
595 int const planeNumber = i->second;
597 if (planeName !=
"") {
598 metadata->add(maskPlanePrefix + planeName, planeNumber);
603 template <
typename MaskPixelT>
610 NameList paramNames = metadata->paramNames(
false);
611 int numPlanesUsed = 0;
614 for (NameList::const_iterator i = paramNames.begin(); i != paramNames.end(); ++i) {
615 if (i->compare(0, maskPlanePrefix.
size(), maskPlanePrefix) == 0) {
618 int const planeId = metadata->getAsInt(*i);
620 MaskPlaneDict::const_iterator plane = newDict.
find(planeName);
621 if (plane != newDict.
end() && planeId != plane->second) {
624 for (MaskPlaneDict::const_iterator j = newDict.
begin(); j != newDict.
end(); ++j) {
625 if (planeId == j->second) {
627 str(
boost::format(
"File specifies plane %s has same value (%d) as %s") %
628 planeName % planeId % j->first));
638 newDict[planeName] = planeId;
644 template <
typename MaskPixelT>
652 template <
typename MaskPixelT>
655 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.
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
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
Reports attempts to exceed implementation-defined length limits for some classes. ...
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...)
Log a debug-level message using a varargs/printf style interface.
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.
LSST DM logging module built on log4cxx.
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)
MaskPlaneDict const & getMaskPlaneDict() const
Return the Mask's maskPlaneDict.
#define LSST_EXCEPT(type,...)
Create an exception with a given 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.
Reports invalid arguments.
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.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
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
An integer coordinate rectangle.
void clearAllMaskPlanes()
Clear all the pixels.
Reports when the result of an operation cannot be represented by the destination type.
static int addMaskPlane(const std::string &name)
PixelReference operator()(int x, int y)
Reports errors that are due to events beyond the control of the program.
void printMaskPlanes() const
print the mask plane dictionary to std::cout