30#include "boost/format.hpp"
42template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
45 : _image(new
Image(width, height)),
46 _mask(new
Mask(width, height, planeDict)),
47 _variance(new
Variance(width, height)) {
53template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
56 : _image(new
Image(dimensions)),
57 _mask(new
Mask(dimensions, planeDict)),
58 _variance(new
Variance(dimensions)) {
64template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
73template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
80 : _image(), _mask(), _variance() {
82 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
93 if (varianceMetadata) {
98template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
105 : _image(), _mask(), _variance() {
107 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
118 if (varianceMetadata) {
123template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
126 ImageOrigin origin,
bool conformMasks,
bool needAllHdus,
130 : _image(), _mask(), _variance() {
132 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
143 if (varianceMetadata) {
148template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
156template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
158 : _image(rhs._image), _mask(rhs._mask), _variance(rhs._variance) {
168template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
172template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
178 : _image(new
Image(*rhs.getImage(),
bbox, origin, deep)),
179 _mask(rhs._mask ? new
Mask(*rhs.getMask(),
bbox, origin, deep) : static_cast<
Mask*>(nullptr)),
180 _variance(rhs._variance ? new
Variance(*rhs.getVariance(),
bbox, origin, deep)
181 : static_cast<
Variance*>(nullptr)) {
185template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
189template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
193template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
197 _image.swap(rhs._image);
198 _mask.swap(rhs._mask);
199 _variance.swap(rhs._variance);
203template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
206 *_image = rhs.
image();
213template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
216 *_image = rhs.
image();
223template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
232template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
241template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
244 (*_image).scaledPlus(c, *rhs.
getImage());
246 (*_variance).scaledPlus(c * c, *rhs.
getVariance());
249template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
256template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
265template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
268 (*_image).scaledMinus(c, *rhs.
getImage());
270 (*_variance).scaledPlus(c * c, *rhs.
getVariance());
273template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
282template <
typename ImagePixelT,
typename VariancePixelT>
283struct productVariance {
284 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
285 return lhs * lhs * varRhs + rhs * rhs * varLhs;
291template <
typename ImagePixelT,
typename VariancePixelT>
292struct scaledProductVariance {
294 scaledProductVariance(
double const c) :
_c(c) {}
295 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
296 return _c *
_c * (lhs * lhs * varRhs + rhs * rhs * varLhs);
301template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
305 if (_image->getDimensions() != rhs._image->
getDimensions()) {
307 boost::str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
308 _image->getWidth() % _image->getHeight() % rhs._image->
getWidth() % rhs._image->
getHeight()));
310 transform_pixels(_image->_getRawView(),
312 _variance->_getRawView(),
313 rhs._variance->_getRawView(),
314 _variance->_getRawView(),
315 productVariance<ImagePixelT, VariancePixelT>());
322template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
326 if (_image->getDimensions() != rhs._image->
getDimensions()) {
328 boost::str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
329 _image->getWidth() % _image->getHeight() % rhs._image->
getWidth() % rhs._image->
getHeight()));
331 transform_pixels(_image->_getRawView(),
333 _variance->_getRawView(),
334 rhs._variance->_getRawView(),
335 _variance->_getRawView(),
336 scaledProductVariance<ImagePixelT, VariancePixelT>(c));
338 (*_image).scaledMultiplies(c, *rhs.
getImage());
342template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
346 *_variance *= rhs * rhs;
352template <
typename ImagePixelT,
typename VariancePixelT>
353struct quotientVariance {
354 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
355 ImagePixelT
const rhs2 = rhs * rhs;
356 return (lhs * lhs * varRhs + rhs2 * varLhs) / (rhs2 * rhs2);
361template <
typename ImagePixelT,
typename VariancePixelT>
362struct scaledQuotientVariance {
364 scaledQuotientVariance(
double c) :
_c(c) {}
365 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
366 ImagePixelT
const rhs2 = rhs * rhs;
367 return (lhs * lhs * varRhs + rhs2 * varLhs) / (
_c *
_c * rhs2 * rhs2);
372template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
376 if (_image->getDimensions() != rhs._image->
getDimensions()) {
378 boost::str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
379 _image->getWidth() % _image->getHeight() % rhs._image->
getWidth() % rhs._image->
getHeight()));
381 transform_pixels(_image->_getRawView(),
383 _variance->_getRawView(),
384 rhs._variance->_getRawView(),
385 _variance->_getRawView(),
386 quotientVariance<ImagePixelT, VariancePixelT>());
393template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
397 if (_image->getDimensions() != rhs._image->
getDimensions()) {
399 str(boost::format(
"Images are of different size, %dx%d v %dx%d") %
400 _image->getWidth() % _image->getHeight() % rhs._image->
getWidth() % rhs._image->
getHeight()));
402 transform_pixels(_image->_getRawView(),
404 _variance->_getRawView(),
405 rhs._variance->_getRawView(),
406 _variance->_getRawView(),
407 scaledQuotientVariance<ImagePixelT, VariancePixelT>(c));
409 (*_image).scaledDivides(c, *rhs.
getImage());
410 *_mask |= *rhs._mask;
413template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
417 *_variance /= rhs * rhs;
421template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
428 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
431template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
438 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
450 hdr->set(
"INHERIT",
true);
451 hdr->set(
"EXTTYPE", exttype);
452 hdr->set(
"EXTNAME", exttype);
457template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
467template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
476 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
480template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
489 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
493template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
503 header = metadata->deepCopy();
505 header = std::make_shared<daf::base::PropertyList>();
510 "MaskedImage::writeFits can only write to an empty file");
512 if (fitsfile.
getHdu() < 1) {
520 processPlaneMetadata(imageMetadata.
get(), header,
"IMAGE");
521 _image->writeFits(fitsfile, imageOptions, header.
get(), _mask.get());
523 processPlaneMetadata(maskMetadata.
get(), header,
"MASK");
524 _mask->writeFits(fitsfile, maskOptions, header.
get());
526 processPlaneMetadata(varianceMetadata.
get(), header,
"VARIANCE");
527 _variance->writeFits(fitsfile, varianceOptions, header.
get(), _mask.get());
534template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
536 if (!_mask || _mask->getWidth() == 0 || _mask->getHeight() == 0) {
537 _mask = MaskPtr(
new Mask(_image->getBBox()));
540 if (_mask->getDimensions() != _image->getDimensions()) {
543 (boost::format(
"Dimension mismatch: Image %dx%d v. Mask %dx%d") % _image->getWidth() %
544 _image->getHeight() % _mask->getWidth() % _mask->getHeight())
549 if (!_variance || _variance->getWidth() == 0 || _variance->getHeight() == 0) {
550 _variance = VariancePtr(
new Variance(_image->getBBox()));
553 if (_variance->getDimensions() != _image->getDimensions()) {
555 pex::exceptions::LengthError,
556 (boost::format(
"Dimension mismatch: Image %dx%d v. Variance %dx%d") % _image->getWidth() %
557 _image->getHeight() % _variance->getWidth() % _variance->getHeight())
566template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
570 return iterator(_image->begin(), _mask->begin(), _variance->begin());
576 return iterator(imageBegin, maskBegin, varianceBegin);
580template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
587 return iterator(imageEnd, maskEnd, varianceEnd);
590template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
597 return iterator(imageEnd, maskEnd, varianceEnd);
600template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
610template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
620template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
627 return x_iterator(imageBegin, maskBegin, varianceBegin);
630template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
637 return x_iterator(imageEnd, maskEnd, varianceEnd);
640template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
647 return y_iterator(imageBegin, maskBegin, varianceBegin);
650template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
657 return y_iterator(imageEnd, maskEnd, varianceEnd);
660template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
670template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
680template <
typename ImagePixelT1,
typename ImagePixelT2>
691#define INSTANTIATE2(ImagePixelT1, ImagePixelT2) \
692 template bool imagesOverlap<ImagePixelT1, ImagePixelT2>(MaskedImage<ImagePixelT1> const&, \
693 MaskedImage<ImagePixelT2> const&);
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
LSST DM logging module built on log4cxx.
#define INSTANTIATE2(ImagePixelT1, ImagePixelT2)
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
int countHdus()
Return the number of HDUs in the file.
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
Lifetime-management for memory that goes into FITS memory files.
typename _view_t::iterator iterator
An STL compliant iterator.
int getWidth() const
Return the number of columns in the image.
lsst::geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
typename _view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
int getHeight() const
Return the number of rows in the image.
typename _view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
_view_t _getRawView() const
typename _view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
Represent a 2-dimensional array of bitmask pixels.
An iterator to the MaskedImage.
A FITS reader class for MaskedImages and their components.
std::shared_ptr< daf::base::PropertyList > readImageMetadata()
Read the FITS header of one of the HDUs.
std::shared_ptr< daf::base::PropertyList > readPrimaryMetadata()
Read the FITS header of one of the HDUs.
std::shared_ptr< daf::base::PropertyList > readMaskMetadata()
Read the FITS header of one of the HDUs.
std::shared_ptr< daf::base::PropertyList > readVarianceMetadata()
Read the FITS header of one of the HDUs.
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > read(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool conformMasks=false, bool needAllHdus=false, bool allowUnsafe=false)
Read the full MaskedImage.
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
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.
MaskedImage(unsigned int width, unsigned int height, MaskPlaneDict const &planeDict=MaskPlaneDict())
Construct from a supplied dimensions.
void scaledDivides(double const c, MaskedImage const &rhs)
typename Mask< MaskPixelT >::MaskPlaneDict MaskPlaneDict
The Mask's MaskPlaneDict.
iterator begin() const
Return an iterator to the start of the image.
reverse_iterator rbegin() const
Return a reverse_iterator to the start of the image.
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.
MaskedImageIterator< typename Image::iterator, typename Mask::iterator, typename Variance::iterator > iterator
MaskedImage & operator-=(ImagePixelT const rhs)
Subtract a scalar rhs from a MaskedImage.
x_iterator row_end(int y) const
Return an x_iterator to the end of the image.
void scaledMultiplies(double const c, MaskedImage const &rhs)
iterator end() const
Return an iterator to the end of the image.
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)
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.
MaskedImage & operator+=(ImagePixelT const rhs)
Add a scalar rhs to a MaskedImage.
MaskedImage & operator*=(ImagePixelT const rhs)
iterator at(int const x, int const y) const
Return an iterator at the point (x, y)
MaskedImage & operator/=(ImagePixelT const rhs)
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.
MaskPixelT mask() const
Return the mask part of a Pixel.
ImagePixelT image() const
Return the image part of a Pixel.
VariancePixelT variance() const
Return the variance part of a Pixel.
A single pixel of the same type as a MaskedImage.
VariancePixelT variance() const
ImagePixelT image() const
Class for storing ordered metadata with comments.
Class for storing generic metadata.
virtual std::shared_ptr< PropertySet > deepCopy() const
Make a deep copy of the PropertySet and all of its contents.
An integer coordinate rectangle.
Reports attempts to exceed implementation-defined length limits for some classes.
Reports errors in the logical structure of the program.
bool imagesOverlap(ImageBase< T1 > const &image1, ImageBase< T2 > const &image2)
Return true if the pixels for two images or masks overlap in memory.
Options for writing an image to FITS.
g2d::python::Image< bool > Mask