31 #pragma clang diagnostic push
32 #pragma clang diagnostic ignored "-Wunused-variable"
33 #pragma clang diagnostic pop
34 #include "boost/regex.hpp"
35 #include "boost/filesystem/path.hpp"
36 #include "boost/format.hpp"
48 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
51 : _image(new
Image(width, height)),
52 _mask(new
Mask(width, height, planeDict)),
53 _variance(new
Variance(width, height)) {
59 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
70 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
79 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
86 : _image(), _mask(), _variance() {
88 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
99 if (varianceMetadata) {
104 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
111 : _image(), _mask(), _variance() {
113 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
124 if (varianceMetadata) {
129 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
132 ImageOrigin origin,
bool conformMasks,
bool needAllHdus,
136 : _image(), _mask(), _variance() {
138 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
149 if (varianceMetadata) {
154 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
161 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
163 : _image(rhs._image), _mask(rhs._mask), _variance(rhs._variance) {
173 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
177 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
183 : _image(new
Image(*rhs.getImage(),
bbox, origin, deep)),
184 _mask(rhs._mask ? new
Mask(*rhs.getMask(),
bbox, origin, deep) : static_cast<
Mask*>(NULL)),
185 _variance(rhs._variance ? new
Variance(*rhs.getVariance(),
bbox, origin, deep)
190 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
194 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
198 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
202 _image.swap(rhs._image);
203 _mask.swap(rhs._mask);
204 _variance.swap(rhs._variance);
208 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
211 *_image = rhs.
image();
218 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
221 *_image = rhs.
image();
228 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
237 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
246 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
249 (*_image).scaledPlus(c, *rhs.
getImage());
251 (*_variance).scaledPlus(c * c, *rhs.
getVariance());
254 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
261 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
270 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
273 (*_image).scaledMinus(c, *rhs.
getImage());
275 (*_variance).scaledPlus(c * c, *rhs.
getVariance());
278 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
287 template <
typename ImagePixelT,
typename VariancePixelT>
288 struct productVariance {
289 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
290 return lhs * lhs * varRhs + rhs * rhs * varLhs;
296 template <
typename ImagePixelT,
typename VariancePixelT>
297 struct scaledProductVariance {
299 scaledProductVariance(
double const c) :
_c(c) {}
300 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
301 return _c *
_c * (lhs * lhs * varRhs + rhs * rhs * varLhs);
306 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
310 if (_image->getDimensions() != rhs._image->getDimensions()) {
312 boost::str(
boost::format(
"Images are of different size, %dx%d v %dx%d") %
313 _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
315 transform_pixels(_image->_getRawView(),
316 rhs._image->_getRawView(),
317 _variance->_getRawView(),
318 rhs._variance->_getRawView(),
319 _variance->_getRawView(),
320 productVariance<ImagePixelT, VariancePixelT>());
327 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
331 if (_image->getDimensions() != rhs._image->getDimensions()) {
333 boost::str(
boost::format(
"Images are of different size, %dx%d v %dx%d") %
334 _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
336 transform_pixels(_image->_getRawView(),
337 rhs._image->_getRawView(),
338 _variance->_getRawView(),
339 rhs._variance->_getRawView(),
340 _variance->_getRawView(),
341 scaledProductVariance<ImagePixelT, VariancePixelT>(c));
343 (*_image).scaledMultiplies(c, *rhs.
getImage());
347 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
351 *_variance *= rhs * rhs;
357 template <
typename ImagePixelT,
typename VariancePixelT>
358 struct quotientVariance {
359 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
360 ImagePixelT
const rhs2 = rhs * rhs;
361 return (lhs * lhs * varRhs + rhs2 * varLhs) / (rhs2 * rhs2);
366 template <
typename ImagePixelT,
typename VariancePixelT>
367 struct scaledQuotientVariance {
369 scaledQuotientVariance(
double c) :
_c(c) {}
370 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
371 ImagePixelT
const rhs2 = rhs * rhs;
372 return (lhs * lhs * varRhs + rhs2 * varLhs) / (
_c *
_c * rhs2 * rhs2);
377 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
381 if (_image->getDimensions() != rhs._image->getDimensions()) {
383 boost::str(
boost::format(
"Images are of different size, %dx%d v %dx%d") %
384 _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
386 transform_pixels(_image->_getRawView(),
387 rhs._image->_getRawView(),
388 _variance->_getRawView(),
389 rhs._variance->_getRawView(),
390 _variance->_getRawView(),
391 quotientVariance<ImagePixelT, VariancePixelT>());
398 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
402 if (_image->getDimensions() != rhs._image->getDimensions()) {
404 str(
boost::format(
"Images are of different size, %dx%d v %dx%d") %
405 _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
407 transform_pixels(_image->_getRawView(),
408 rhs._image->_getRawView(),
409 _variance->_getRawView(),
410 rhs._variance->_getRawView(),
411 _variance->_getRawView(),
412 scaledQuotientVariance<ImagePixelT, VariancePixelT>(c));
414 (*_image).scaledDivides(c, *rhs.
getImage());
415 *_mask |= *rhs._mask;
418 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
422 *_variance /= rhs * rhs;
426 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
433 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
436 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
443 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
451 hdr = metadata->deepCopy();
455 hdr->set(
"INHERIT",
true);
456 hdr->set(
"EXTTYPE", exttype);
461 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
471 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
480 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
484 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
493 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
497 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
507 header = metadata->deepCopy();
509 header = std::make_shared<daf::base::PropertyList>();
514 "MaskedImage::writeFits can only write to an empty file");
516 if (fitsfile.
getHdu() < 1) {
524 processPlaneMetadata(imageMetadata, header,
"IMAGE");
525 _image->writeFits(fitsfile, imageOptions, header, _mask);
527 processPlaneMetadata(maskMetadata, header,
"MASK");
528 _mask->writeFits(fitsfile, maskOptions, header);
530 processPlaneMetadata(varianceMetadata, header,
"VARIANCE");
531 _variance->writeFits(fitsfile, varianceOptions, header, _mask);
538 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
540 if (!_mask || _mask->getWidth() == 0 || _mask->getHeight() == 0) {
541 _mask = MaskPtr(
new Mask(_image->getBBox()));
544 if (_mask->getDimensions() != _image->getDimensions()) {
547 (
boost::format(
"Dimension mismatch: Image %dx%d v. Mask %dx%d") % _image->getWidth() %
548 _image->getHeight() % _mask->getWidth() % _mask->getHeight())
553 if (!_variance || _variance->getWidth() == 0 || _variance->getHeight() == 0) {
554 _variance = VariancePtr(
new Variance(_image->getBBox()));
557 if (_variance->getDimensions() != _image->getDimensions()) {
559 pex::exceptions::LengthError,
560 (
boost::format(
"Dimension mismatch: Image %dx%d v. Variance %dx%d") % _image->getWidth() %
561 _image->getHeight() % _variance->getWidth() % _variance->getHeight())
570 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
574 return iterator(_image->begin(), _mask->begin(), _variance->begin());
580 return iterator(imageBegin, maskBegin, varianceBegin);
584 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
591 return iterator(imageEnd, maskEnd, varianceEnd);
594 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
601 return iterator(imageEnd, maskEnd, varianceEnd);
604 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
614 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
624 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
631 return x_iterator(imageBegin, maskBegin, varianceBegin);
634 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
641 return x_iterator(imageEnd, maskEnd, varianceEnd);
644 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
651 return y_iterator(imageBegin, maskBegin, varianceBegin);
654 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
661 return y_iterator(imageEnd, maskEnd, varianceEnd);
664 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
674 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
684 template <
typename ImagePixelT1,
typename ImagePixelT2>
695 #define INSTANTIATE2(ImagePixelT1, ImagePixelT2) \
696 template bool imagesOverlap<ImagePixelT1, ImagePixelT2>(MaskedImage<ImagePixelT1> const&, \
697 MaskedImage<ImagePixelT2> const&);
699 template class MaskedImage<std::uint16_t>;
700 template class MaskedImage<int>;
701 template class MaskedImage<float>;
702 template class MaskedImage<double>;
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
afw::table::PointKey< int > dimensions
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.
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
_view_t::iterator iterator
An STL compliant iterator.
_view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
A class to represent a 2-dimensional array of pixels.
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.
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.
lsst::afw::image::Image< VariancePixelT > Variance
void scaledDivides(double const c, MaskedImage const &rhs)
MaskedImageIterator< typename Image::reverse_iterator, typename Mask::reverse_iterator, typename Variance::reverse_iterator > reverse_iterator
MaskedImageIterator< typename Image::y_iterator, typename Mask::y_iterator, typename Variance::y_iterator > y_iterator
An iterator to a column of a MaskedImage.
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.
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.
lsst::afw::image::Image< ImagePixelT > Image
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.
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.
lsst::afw::image::Mask< MaskPixelT > Mask
reverse_iterator rend() const
Return a reverse_iterator to the end of the image.
MaskedImageIterator< typename Image::iterator, typename Mask::iterator, typename Variance::iterator > iterator
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.
MaskedImageIterator< typename Image::x_iterator, typename Mask::x_iterator, typename Variance::x_iterator > x_iterator
An iterator to a row of a MaskedImage.
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.
An integer coordinate rectangle.
Reports attempts to exceed implementation-defined length limits for some classes.
Reports errors in the logical structure of the program.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
bool imagesOverlap(ImageBase< T1 > const &image1, ImageBase< T2 > const &image2)
Return true if the pixels for two images or masks overlap in memory.
def writeFits(filename, stamp_ims, metadata, type_name, write_mask, write_variance)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
A base class for image defects.
Options for writing an image to FITS.