29 #ifndef LSST_IMAGE_MASKEDIMAGE_H
30 #define LSST_IMAGE_MASKEDIMAGE_H
38 #include "boost/mpl/at.hpp"
39 #include "boost/iterator/zip_iterator.hpp"
40 #include "boost/tuple/tuple.hpp"
91 template <
typename ImagePT = ImagePixelT,
typename MaskPT = MaskPixelT,
typename VarPT = VariancePixelT>
98 template <
typename,
typename,
typename>
101 template <
typename,
typename,
typename>
104 template <
typename,
typename,
typename>
107 template <
typename,
typename,
typename>
116 template <
typename ImageIterator,
typename MaskIterator,
typename VarianceIterator,
117 template <
typename>
class Ref =
Reference>
119 using IMV_iterator_tuple = boost::tuple<ImageIterator, MaskIterator, VarianceIterator>;
125 using IMV_tuple =
typename boost::zip_iterator<IMV_iterator_tuple>::reference;
129 template <
typename,
typename,
typename>
136 VarianceIterator
const& var)
177 return &this->
_iter->template get<0>() - &rhs.
_iter->template get<0>();
181 return &this->
_iter->template get<0>() == &rhs.
_iter->template get<0>();
185 return &this->
_iter->template get<0>() != &rhs.
_iter->template get<0>();
189 return &this->
_iter->template get<0>() < &rhs.
_iter->template get<0>();
194 _iter->template get<2>()[0]);
203 typename boost::zip_iterator<IMV_iterator_tuple>
_iter;
207 template <
typename ImageIterator,
typename MaskIterator,
typename VarianceIterator>
225 template <
typename ImageIterator,
typename MaskIterator,
typename VarianceIterator>
228 typename detail::const_iterator_type<MaskIterator>::type,
229 typename detail::const_iterator_type<VarianceIterator>::type,
256 template <
typename ImageLocator,
typename MaskLocator,
typename VarianceLocator,
257 template <
typename>
class Ref =
Reference>
259 using IMVLocator =
typename boost::tuple<ImageLocator, MaskLocator, VarianceLocator>;
265 template <
template <
typename>
class X_OR_Y>
266 class _x_or_y_iterator {
272 X_OR_Y<ImageLocator>(_mil->_loc.template get<0>())() += di;
273 X_OR_Y<MaskLocator>(_mil->_loc.template get<1>())() += di;
274 X_OR_Y<VarianceLocator>(_mil->_loc.template get<2>())() += di;
278 _x_or_y_iterator& operator++() {
280 ++X_OR_Y<ImageLocator>(_mil->_loc.template get<0>())();
281 ++X_OR_Y<MaskLocator>(_mil->_loc.template get<1>())();
282 ++X_OR_Y<VarianceLocator>(_mil->_loc.template get<2>())();
286 bool operator==(_x_or_y_iterator
const& rhs) {
287 return X_OR_Y<ImageLocator>(_mil->_loc.template get<0>())() ==
288 X_OR_Y<ImageLocator>(rhs._mil->_loc.template get<0>())();
290 bool operator!=(_x_or_y_iterator
const& rhs) {
291 return X_OR_Y<ImageLocator>(_mil->_loc.template get<0>())() !=
292 X_OR_Y<ImageLocator>(rhs._mil->_loc.template get<0>())();
294 bool operator<(_x_or_y_iterator
const& rhs) {
295 return X_OR_Y<ImageLocator>(_mil->_loc.template get<0>())() <
296 X_OR_Y<ImageLocator>(rhs._mil->_loc.template get<0>())();
300 return Pixel((*(X_OR_Y<ImageLocator>(_mil->_loc.template get<0>())()))[0],
301 (*(X_OR_Y<MaskLocator>(_mil->_loc.template get<1>())()))[0],
302 (*(X_OR_Y<VarianceLocator>(_mil->_loc.template get<2>())()))[0]);
308 return (*(X_OR_Y<ImageLocator>(_mil->_loc.template get<0>())()))[0];
311 return (*(X_OR_Y<MaskLocator>(_mil->_loc.template get<1>())()))[0];
314 return (*(X_OR_Y<VarianceLocator>(_mil->_loc.template get<2>())()))[0];
322 template <
typename LocT>
324 using IterT =
typename LocT::x_iterator;
327 apply_x(LocT& loc) :
_loc(loc) {}
334 template <
typename LocT>
336 using IterT =
typename LocT::y_iterator;
339 apply_y(LocT& loc) :
_loc(loc) {}
347 using IMVCachedLocation =
typename boost::tuple<typename ImageLocator::cached_location_t, typename MaskLocator::cached_location_t, typename VarianceLocator::cached_location_t>;
357 template <
typename,
typename,
typename>
372 :
_loc(img, msk, var) {
378 return Pixel(
_loc.template get<0>().x()[0][0],
_loc.template get<1>().x()[0][0],
379 _loc.template get<2>().x()[0][0]);
385 _loc.template get<2>()(
x,
y)[0]);
391 return Pixel(
_loc.template get<0>()[cached_loc.
_imv.template get<0>()][0],
392 _loc.template get<1>()[cached_loc.
_imv.template get<1>()][0],
393 _loc.template get<2>()[cached_loc.
_imv.template get<2>()][0]);
414 using PixelTVec =
typename boost::mpl::vector<ImagePixelT, MaskPixelT, VariancePixelT>;
416 template <
typename N>
419 return _loc.template get<N::value>()[cached_loc.
_imv.template get<N::value>()][0];
422 template <
typename N>
424 return _loc.template get<N::value>()[0][0];
427 template <
typename N>
429 return _loc.template get<N::value>()(
x,
y)[0];
436 return apply_IMV<boost::mpl::int_<0>>(cached_loc);
445 return apply_IMV<boost::mpl::int_<1>>(cached_loc);
454 return apply_IMV<boost::mpl::int_<2>>(cached_loc);
460 return apply_IMV<boost::mpl::int_<2>>(
x,
y);
465 return _loc.template get<0>() == rhs.
_loc.template get<0>();
471 return _loc.template get<0>() < rhs.
_loc.template get<0>();
476 return operator+=(detail::difference_type(p.first, p.second));
481 _loc.template get<0>() += p;
482 _loc.template get<1>() += p;
483 _loc.template get<2>() += p;
497 template <
typename ImageLocator,
typename MaskLocator,
typename VarianceLocator>
507 template <
typename ImageLocator,
typename MaskLocator,
typename VarianceLocator>
510 typename detail::const_locator_type<MaskLocator>::type,
511 typename detail::const_locator_type<VarianceLocator>::type,
522 const_MaskLocator(
iter.
getLoc().template get<1>()),
523 const_VarianceLocator(
iter.
getLoc().template get<2>())) {
536 typedef const_MaskedImageIterator<typename Image::reverse_iterator,
570 explicit MaskedImage(
unsigned int width,
unsigned int height,
623 bool conformMasks =
false,
bool needAllHdus =
false,
628 bool allowUnsafe =
false);
651 bool conformMasks =
false,
bool needAllHdus =
false,
656 bool allowUnsafe =
false);
679 bool conformMasks =
false,
bool needAllHdus =
false,
684 bool allowUnsafe =
false);
705 bool const deep =
false);
711 template <
typename OtherPixelT>
716 : _image(), _mask(), _variance() {
719 "Only deep copies are permitted for MaskedImages with different pixel types");
1066 int getX0()
const {
return _image->getX0(); }
1074 int getY0()
const {
return _image->getY0(); }
1104 _image->setXY0(origin);
1108 _mask->setXY0(origin);
1112 _variance->setXY0(origin);
1124 return getImage()->indexToPosition(ind, xy);
1136 return getImage()->positionToIndex(pos, xy);
1191 return x_iterator(imageEnd, maskEnd, varianceEnd);
1210 return y_iterator(imageEnd, maskEnd, varianceEnd);
1225 return xy_locator(imageEnd, maskEnd, varianceEnd);
1229 void conformSizes();
1239 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
1251 template <
typename ImagePixelT1,
typename ImagePixelT2>
1252 bool imagesOverlap(MaskedImage<ImagePixelT1, MaskPixel, VariancePixel>
const& image1,
1253 MaskedImage<ImagePixelT2, MaskPixel, VariancePixel>
const& image2);
1273 template <
typename ImageT,
typename TagT>
1275 using type = ImageT;
1279 template <
typename ImageT>
1280 struct GetImage_<ImageT, typename
image::detail::MaskedImage_tag> {
1281 using type =
typename ImageT::Image;
1286 template <
typename ImageT>
1287 struct GetImage :
public GetImage_<ImageT, typename ImageT::image_category> {};
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
afw::table::PointKey< int > dimensions
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Lifetime-management for memory that goes into FITS memory files.
typename _view_t::xy_locator xy_locator
An xy_locator.
typename _view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
typename _view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
typename _view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
A class to represent a 2-dimensional array of pixels.
Represent a 2-dimensional array of bitmask pixels.
The base class for MaskedImageIterators (const and non-const)
bool operator==(MaskedImageIteratorBase const &rhs)
Return true if the lhs equals the rhs.
MaskedImageIteratorBase operator++(int)
Increment the iterator (postfix)
const Pixel operator*() const
Dereference the iterator, returning a const Pixel.
std::ptrdiff_t operator-(MaskedImageIteratorBase const &rhs)
Return the distance between two iterators.
bool operator!=(MaskedImageIteratorBase const &rhs)
Return true if the lhs doesn't equal the rhs.
boost::zip_iterator< IMV_iterator_tuple > _iter
MaskedImageIteratorBase & operator++()
Increment the iterator (prefix)
Pixel operator*()
Dereference the iterator, returning a Pixel.
Ref< MaskPixelT >::type mask()
Return (a reference to) the mask part of the Pixel pointed at by the iterator.
Ref< ImagePixelT >::type image()
Return (a reference to) the image part of the Pixel pointed at by the iterator.
MaskedImageIteratorBase & operator+=(std::ptrdiff_t delta)
Increment the iterator by delta
MaskedImageIteratorBase & operator-=(std::ptrdiff_t delta)
Decrement the iterator by delta
typename boost::zip_iterator< IMV_iterator_tuple >::reference IMV_tuple
The underlying iterator tuple.
MaskedImageIteratorBase(ImageIterator const &img, MaskIterator const &msk, VarianceIterator const &var)
Construct a MaskedImageIteratorBase from the image/mask/variance iterators.
Ref< VariancePixelT >::type variance()
Return (a reference to) the variance part of the Pixel pointed at by the iterator.
bool operator<(MaskedImageIteratorBase const &rhs)
Return true if the lhs is less than the rhs.
const IMV_iterator_tuple get_iterator_tuple() const
Return the underlying iterator tuple.
An iterator to the MaskedImage.
MaskedImageIterator(ImageIterator &img, MaskIterator &msk, VarianceIterator &var)
MaskedImageIterator operator+(std::ptrdiff_t delta)
Return a MaskedImageIterator that's delta beyond this.
A saved relative position, providing efficient access to neighbouring pixels.
cached_location_t(IMVLocator const &loc, int x, int y)
Create a cached_location_t that can be used to access pixels (x, y) away from loc
The base class for MaskedImageLocators (const and non-const)
Ref< typename boost::mpl::at< PixelTVec, N >::type >::type apply_IMV(int x, int y)
IMVLocator const & getLoc() const
typename boost::tuple< typename ImageLocator::cached_location_t, typename MaskLocator::cached_location_t, typename VarianceLocator::cached_location_t > IMVCachedLocation
MaskedImageLocatorBase & operator+=(pair2I const &p)
Increment the locator's x and y positions by p
Pixel operator*()
Dereference a locator, returning a Pixel.
Ref< VariancePixelT >::type variance(cached_location_t const &cached_loc)
Return a reference to the variance at the offset set when we created the cached_location_t
_x_or_y_iterator< apply_y > y_iterator
A y_iterator that provides a view of the xy_locator (i.e. advancing one advances the other)
Pixel operator[](cached_location_t const &cached_loc)
Dereference a locator, returning a Pixel offset by the amount set when we created the cached_location...
Ref< typename boost::mpl::at< PixelTVec, N >::type >::type apply_IMV(cached_location_t const &cached_loc)
MaskedImageLocatorBase & operator+=(detail::difference_type p)
Increment the locator's x and y positions by p
cached_location_t cache_location(int x, int y) const
Create a cached_location_t offset by (x, y) from locator.
Ref< MaskPixelT >::type mask(cached_location_t const &cached_loc)
Return a reference to the mask at the offset set when we created the cached_location_t
_x_or_y_iterator< apply_x > x_iterator
An x_iterator that provides a view of the xy_locator (i.e. advancing one advances the other)
Ref< ImagePixelT >::type image()
Return a reference to the image at the current position of the locator.
Ref< VariancePixelT >::type variance(int x, int y)
Return a reference to the variance offset by (x, y) from the current position of the locator.
Ref< MaskPixelT >::type mask(int x, int y)
Return a reference to the mask offset by (x, y) from the current position of the locator.
Pixel operator()(int x, int y)
Dereference a locator, returning a Pixel offset by (x, y) from the locator.
y_iterator y()
Return an iterator that can be used to move (or dereference) a locator.
bool operator==(MaskedImageLocatorBase const &rhs)
Return true iff two locators are equal.
Ref< MaskPixelT >::type mask()
Return a reference to the mask at the current position of the locator.
Ref< ImagePixelT >::type image(int x, int y)
Return a reference to the image offset by (x, y) from the current position of the locator.
Ref< VariancePixelT >::type variance()
Return a reference to the variance at the current position of the locator.
Ref< typename boost::mpl::at< PixelTVec, N >::type >::type apply_IMV()
bool operator!=(MaskedImageLocatorBase const &rhs)
Return true iff two locators are not equal.
typename boost::mpl::vector< ImagePixelT, MaskPixelT, VariancePixelT > PixelTVec
Ref< ImagePixelT >::type image(cached_location_t const &cached_loc)
Return a reference to the image at the offset set when we created the cached_location_t
bool operator<(MaskedImageLocatorBase const &rhs)
Return true iff lhs is less than rhs.
MaskedImageLocatorBase(ImageLocator const &img, MaskLocator const &msk, VarianceLocator const &var)
Construct a MaskedImageLocator from image/mask/variance locators.
x_iterator x()
Return an iterator that can be used to move (or dereference) a locator.
A locator for the MaskedImage.
MaskedImageLocator(ImageLocator &img, MaskLocator &msk, VarianceLocator &var)
An const iterator to the MaskedImage.
const_MaskedImageIterator & operator+(std::ptrdiff_t delta)
Return a const_MaskedImageIterator that's delta beyond this.
const_MaskedImageIterator(MaskedImageIterator< ImageIterator, MaskIterator, VarianceIterator > const &iter)
A const locator for the MaskedImage.
const_MaskedImageLocator(MaskedImageLocator< ImageLocator, MaskLocator, VarianceLocator > const &iter)
A class to manipulate images, masks, and variance as a single object.
void scaledPlus(double const c, MaskedImage const &rhs)
Add a scaled MaskedImage c*rhs to a MaskedImage.
lsst::afw::image::Image< VariancePixelT > Variance
MaskedImage operator[](lsst::geom::Box2I const &bbox) const
Return a subimage corresponding to the given box (interpreted as PARENT coordinates).
std::shared_ptr< image::Mask< MaskPixelT > > MaskPtr
shared pointer to the Mask
void writeFits(std::string const &fileName, std::shared_ptr< daf::base::PropertySet const > metadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > imageMetadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > maskMetadata=std::shared_ptr< daf::base::PropertySet const >(), std::shared_ptr< daf::base::PropertySet const > varianceMetadata=std::shared_ptr< daf::base::PropertySet const >()) const
Write a MaskedImage to a regular FITS file.
int getX0() const
Return the image's column-origin.
MaskedImage(unsigned int width, unsigned int height, MaskPlaneDict const &planeDict=MaskPlaneDict())
Construct from a supplied dimensions.
void setXY0(lsst::geom::Point2I const origin)
Set the MaskedImage's origin.
lsst::geom::Box2I getBBox(ImageOrigin const origin=PARENT) const
MaskedImage & operator*=(lsst::afw::image::Image< ImagePixelT > const &rhs)
void setVariance(Variance const &other)
Set the variance plane's pixel values to those of another Image.
void setXY0(int const x0, int const y0)
Set the MaskedImage's origin.
MaskedImage & operator/=(lsst::afw::image::Image< ImagePixelT > const &rhs)
static MaskedImage readFits(fits::MemFileManager &manager)
Read a MaskedImage from a FITS RAM file.
std::shared_ptr< image::Image< ImagePixelT > > ImagePtr
shared pointer to the Image
void scaledDivides(double const c, MaskedImage const &rhs)
typename MaskedImageLocator< typename Image::xy_locator, typename Mask::xy_locator, typename Variance::xy_locator >::y_iterator xy_y_iterator
an y_iterator associated with an xy_locator
MaskedImage(MaskedImage< OtherPixelT, MaskPixelT, VariancePixelT > const &rhs, const bool deep)
generalised copy constructor; defined here in the header so that the compiler can instantiate N(N-1)/...
MaskedImageIterator< typename Image::y_iterator, typename Mask::y_iterator, typename Variance::y_iterator > y_iterator
An iterator to a column of a MaskedImage.
int getHeight() const
Return the number of rows in the image.
typename Mask< MaskPixelT >::MaskPlaneDict MaskPlaneDict
The Mask's MaskPlaneDict.
MaskedImage & operator-=(lsst::afw::image::Image< ImagePixelT > const &rhs)
int getY0() const
Return the image's row-origin.
virtual ~MaskedImage()=default
MaskedImage & operator=(MaskedImage &&rhs)
lsst::afw::image::pixel::Pixel< ImagePixelT, MaskPixelT, VariancePixelT > Pixel
A Pixel in the MaskedImage.
iterator begin() const
Return an iterator to the start of the image.
MaskedImage & operator+=(lsst::afw::math::Function2< double > const &function)
reverse_iterator rbegin() const
Return a reverse_iterator to the start of the image.
MaskedImageIterator< typename Image::x_iterator, typename Mask::x_iterator, typename Variance::x_iterator > x_iterator
An iterator to a row of a MaskedImage.
void setMask(Mask const &other)
Set the mask plane's pixel values to those of another Mask.
lsst::afw::image::Mask< MaskPixelT > Mask
y_iterator col_end(int x) const
Return an y_iterator to the end of the image.
void scaledMinus(double const c, MaskedImage const &rhs)
Subtract a scaled MaskedImage c*rhs from a MaskedImage.
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
MaskedImageLocator< typename Image::xy_locator, typename Mask::xy_locator, typename Variance::xy_locator > xy_locator
A locator for a MaskedImage.
lsst::geom::Extent2I getDimensions() const
std::pair< int, double > positionToIndex(double const pos, lsst::afw::image::xOrY const xy) const
Convert image position to index (see Image::positionToIndex)
lsst::geom::Point2I getXY0() const
Return the image's origin.
MaskedImageIterator< typename Image::reverse_iterator, typename Mask::reverse_iterator, typename Variance::reverse_iterator > reverse_iterator
MaskedImageIterator< typename Image::iterator, typename Mask::iterator, typename Variance::iterator > iterator
xy_locator xy_at(int x, int y) const
Return an xy_locator at the point (x, y)
MaskedImage & operator-=(ImagePixelT const rhs)
Subtract a scalar rhs from a MaskedImage.
int getWidth() const
Return the number of columns in the image.
x_iterator row_end(int y) const
Return an x_iterator to the end of the image.
MaskedImage subset(lsst::geom::Box2I const &bbox, ImageOrigin origin=PARENT) const
Return a subimage corresponding to the given box.
void scaledMultiplies(double const c, MaskedImage const &rhs)
std::shared_ptr< image::Image< VariancePixelT > > VariancePtr
shared pointer to the variance Image
iterator end() const
Return an iterator to the end of the image.
typename MaskedImageLocator< typename Image::xy_locator, typename Mask::xy_locator, typename Variance::xy_locator >::x_iterator xy_x_iterator
an x_iterator associated with an xy_locator
void assign(MaskedImage const &rhs, lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
Copy pixels from another masked image to a specified subregion of this masked image.
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
MaskedImage & operator=(MaskedImage const &rhs)
Make the lhs use the rhs's pixels.
lsst::afw::image::Image< ImagePixelT > Image
x_iterator row_begin(int y) const
Return an x_iterator to the start of the image.
void swap(MaskedImage &rhs)
double indexToPosition(double ind, lsst::afw::image::xOrY const xy) const
Convert image index to image position (see Image::indexToPosition)
MaskedImage & operator+=(lsst::afw::image::Image< ImagePixelT > const &rhs)
void setImage(Image const &other)
Set the image plane's pixel values to those of another Image.
y_iterator col_begin(int x) const
Return an y_iterator to the start of the image.
reverse_iterator rend() const
Return a reverse_iterator to the end of the image.
static MaskedImage readFits(std::string const &filename)
Read a MaskedImage from a regular FITS file.
MaskedImage & operator+=(ImagePixelT const rhs)
Add a scalar rhs to a MaskedImage.
x_iterator x_at(int x, int y) const
Return an x_iterator at the point (x, y)
MaskedImage & operator*=(ImagePixelT const rhs)
MaskedImage & operator-=(lsst::afw::math::Function2< double > const &function)
iterator at(int const x, int const y) const
Return an iterator at the point (x, y)
MaskedImage & operator/=(ImagePixelT const rhs)
y_iterator y_at(int x, int y) const
Return an y_iterator at the point (x, y)
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage's mask.
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
A pixel of a MaskedImage.
A single pixel of the same type as a MaskedImage.
A Function taking two arguments.
An integer coordinate rectangle.
Reports invalid arguments.
std::string const fitsFile_RE
regexp to identify when MaskedImages should be written as MEFs
std::string const compressedFileNoMEF_RE
regexp to identify compressed files that we can't write MEFs to
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
float VariancePixel
default type for MaskedImage variance images
std::int32_t MaskPixel
default type for Masks and MaskedImage Masks
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > * makeMaskedImage(typename std::shared_ptr< Image< ImagePixelT >> image, typename std::shared_ptr< Mask< MaskPixelT >> mask=Mask< MaskPixelT >(), typename std::shared_ptr< Image< VariancePixelT >> variance=Image< VariancePixelT >())
A function to return a MaskedImage of the correct type (cf.
bool imagesOverlap(ImageBase< T1 > const &image1, ImageBase< T2 > const &image2)
Return true if the pixels for two images or masks overlap in memory.
A base class for image defects.
Options for writing an image to FITS.
A templated class to return this classes' type (present in Image/Mask/MaskedImage)
metafunction to extract reference type from PixelT
A traits class for MaskedImage.
A class used to identify classes that represent MaskedImage pixels.