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" 47 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
50 : daf::
base::Citizen(typeid(this)),
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>
62 : daf::
base::Citizen(typeid(this)),
63 _image(new
Image(dimensions)),
64 _mask(new
Mask(dimensions, planeDict)),
65 _variance(new
Variance(dimensions)) {
71 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
74 : daf::
base::Citizen(typeid(this)),
75 _image(new
Image(bbox)),
76 _mask(new
Mask(bbox, planeDict)),
83 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
90 : daf::
base::Citizen(typeid(this)), _image(), _mask(), _variance() {
92 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
103 if (varianceMetadata) {
108 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
116 : daf::
base::Citizen(typeid(this)), _image(), _mask(), _variance() {
118 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
129 if (varianceMetadata) {
134 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
137 ImageOrigin origin,
bool conformMasks,
bool needAllHdus,
142 : daf::
base::Citizen(typeid(this)), _image(), _mask(), _variance() {
144 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
155 if (varianceMetadata) {
160 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
163 : daf::
base::Citizen(typeid(this)), _image(image), _mask(mask), _variance(variance) {
167 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
169 : daf::
base::Citizen(typeid(this)), _image(rhs._image), _mask(rhs._mask), _variance(rhs._variance) {
179 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
183 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
189 : daf::
base::Citizen(typeid(this)),
190 _image(new
Image(*rhs.getImage(), bbox, origin, deep)),
191 _mask(rhs._mask ? new
Mask(*rhs.getMask(), bbox, origin, deep) : static_cast<
Mask*>(NULL)),
192 _variance(rhs._variance ? new
Variance(*rhs.getVariance(), bbox, origin, deep)
197 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
201 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
205 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
209 _image.swap(rhs._image);
210 _mask.swap(rhs._mask);
211 _variance.swap(rhs._variance);
215 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
218 *_image = rhs.
image();
225 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
228 *_image = rhs.
image();
235 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
242 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
251 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
260 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
263 (*_image).scaledPlus(c, *rhs.
getImage());
265 (*_variance).scaledPlus(c * c, *rhs.
getVariance());
268 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
275 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
284 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
289 (*_variance).scaledPlus(c * c, *rhs.
getVariance());
292 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
301 template <
typename ImagePixelT,
typename VariancePixelT>
302 struct productVariance {
303 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
304 return lhs * lhs * varRhs + rhs * rhs * varLhs;
310 template <
typename ImagePixelT,
typename VariancePixelT>
311 struct scaledProductVariance {
313 scaledProductVariance(
double const c) : _c(c) {}
314 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
315 return _c * _c * (lhs * lhs * varRhs + rhs * rhs * varLhs);
320 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
324 transform_pixels(_image->_getRawView(),
325 rhs._image->_getRawView(),
326 _variance->_getRawView(),
327 rhs._variance->_getRawView(),
328 _variance->_getRawView(),
329 productVariance<ImagePixelT, VariancePixelT>());
336 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
340 transform_pixels(_image->_getRawView(),
341 rhs._image->_getRawView(),
342 _variance->_getRawView(),
343 rhs._variance->_getRawView(),
344 _variance->_getRawView(),
345 scaledProductVariance<ImagePixelT, VariancePixelT>(c));
347 (*_image).scaledMultiplies(c, *rhs.
getImage());
351 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
355 *_variance *= rhs * rhs;
361 template <
typename ImagePixelT,
typename VariancePixelT>
362 struct quotientVariance {
363 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
364 ImagePixelT
const rhs2 = rhs * rhs;
365 return (lhs * lhs * varRhs + rhs2 * varLhs) / (rhs2 * rhs2);
370 template <
typename ImagePixelT,
typename VariancePixelT>
371 struct scaledQuotientVariance {
373 scaledQuotientVariance(
double c) :
_c(c) {}
374 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
375 ImagePixelT
const rhs2 = rhs * rhs;
376 return (lhs * lhs * varRhs + rhs2 * varLhs) / (_c * _c * rhs2 * rhs2);
381 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
385 transform_pixels(_image->_getRawView(),
386 rhs._image->_getRawView(),
387 _variance->_getRawView(),
388 rhs._variance->_getRawView(),
389 _variance->_getRawView(),
390 quotientVariance<ImagePixelT, VariancePixelT>());
397 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
401 transform_pixels(_image->_getRawView(),
402 rhs._image->_getRawView(),
403 _variance->_getRawView(),
404 rhs._variance->_getRawView(),
405 _variance->_getRawView(),
406 scaledQuotientVariance<ImagePixelT, VariancePixelT>(c));
408 (*_image).scaledDivides(c, *rhs.
getImage());
409 *_mask |= *rhs._mask;
412 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
416 *_variance /= rhs * rhs;
420 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
426 fits::Fits fitsfile(fileName,
"w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
427 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
430 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
436 fits::Fits fitsfile(manager,
"w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
437 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
445 hdr = metadata->deepCopy();
449 hdr->set(
"INHERIT",
true);
450 hdr->set(
"EXTTYPE", exttype);
455 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
465 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
473 fits::Fits fitsfile(fileName,
"w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
474 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
478 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
486 fits::Fits fitsfile(manager,
"w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
487 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
491 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
501 header = metadata->deepCopy();
503 header = std::make_shared<daf::base::PropertyList>();
508 "MaskedImage::writeFits can only write to an empty file");
510 if (fitsfile.
getHdu() < 1) {
518 processPlaneMetadata(imageMetadata, header,
"IMAGE");
519 _image->writeFits(fitsfile, imageOptions, header, _mask);
521 processPlaneMetadata(maskMetadata, header,
"MASK");
522 _mask->writeFits(fitsfile, maskOptions, header);
524 processPlaneMetadata(varianceMetadata, header,
"VARIANCE");
525 _variance->writeFits(fitsfile, varianceOptions, header, _mask);
532 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
534 if (!_mask || _mask->getWidth() == 0 || _mask->getHeight() == 0) {
538 if (_mask->getDimensions() != _image->getDimensions()) {
541 (
boost::format(
"Dimension mismatch: Image %dx%d v. Mask %dx%d") % _image->getWidth() %
542 _image->getHeight() % _mask->getWidth() % _mask->getHeight())
547 if (!_variance || _variance->getWidth() == 0 || _variance->getHeight() == 0) {
551 if (_variance->getDimensions() != _image->getDimensions()) {
554 (
boost::format(
"Dimension mismatch: Image %dx%d v. Variance %dx%d") % _image->getWidth() %
555 _image->getHeight() % _variance->getWidth() % _variance->getHeight())
564 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
567 #if 0 // this doesn't compile; why? 568 return iterator(_image->begin(), _mask->begin(), _variance->begin());
574 return iterator(imageBegin, maskBegin, varianceBegin);
578 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
585 return iterator(imageEnd, maskEnd, varianceEnd);
588 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
595 return iterator(imageEnd, maskEnd, varianceEnd);
598 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
608 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
618 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
625 return x_iterator(imageBegin, maskBegin, varianceBegin);
628 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
635 return x_iterator(imageEnd, maskEnd, varianceEnd);
638 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
645 return y_iterator(imageBegin, maskBegin, varianceBegin);
648 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
655 return y_iterator(imageEnd, maskEnd, varianceEnd);
658 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
668 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
678 template <
typename ImagePixelT1,
typename ImagePixelT2>
689 #define INSTANTIATE2(ImagePixelT1, ImagePixelT2) \ 690 template bool imagesOverlap<ImagePixelT1, ImagePixelT2>(MaskedImage<ImagePixelT1> const&, \ 691 MaskedImage<ImagePixelT2> const&); 693 template class MaskedImage<std::uint16_t>;
694 template class MaskedImage<int>;
695 template class MaskedImage<float>;
696 template class MaskedImage<double>;
697 template class MaskedImage<std::uint64_t>;
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
afw::table::PointKey< int > dimensions
std::shared_ptr< daf::base::PropertyList > readVarianceMetadata()
Read the FITS header of one of the HDUs.
void scaledPlus(OutImageT &outImage, double c1, InImageT const &inImage1, double c2, InImageT const &inImage2)
Compute the scaled sum of two images.
VariancePixelT variance() const
Return the variance part of a Pixel.
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
Image< LhsPixelT > & operator+=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Add lhs to Image rhs (i.e. pixel-by-pixel addition) where types are different.
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Class for storing ordered metadata with comments.
iterator at(int const x, int const y) const
Return an iterator at the point (x, y)
VariancePixelT variance() const
Reports attempts to exceed implementation-defined length limits for some classes. ...
Options for writing an image to FITS.
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
std::shared_ptr< daf::base::PropertyList > readMaskMetadata()
Read the FITS header of one of the HDUs.
MaskedImageIterator< typename Image::x_iterator, typename Mask::x_iterator, typename Variance::x_iterator > x_iterator
An iterator to a row of a MaskedImage.
A single pixel of the same type as a MaskedImage.
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
MaskedImage(unsigned int width, unsigned int height, MaskPlaneDict const &planeDict=MaskPlaneDict())
Construct from a supplied dimensions.
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
MaskedImageIterator< typename Image::iterator, typename Mask::iterator, typename Variance::iterator > iterator
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
LSST DM logging module built on log4cxx.
An iterator to the MaskedImage.
_view_t::iterator iterator
An STL compliant iterator.
reverse_iterator rend() const
Return a reverse_iterator to the end of the image.
ImagePixelT image() const
A base class for image defects.
Represent a 2-dimensional array of bitmask pixels.
MaskedImageIterator< typename Image::y_iterator, typename Mask::y_iterator, typename Variance::y_iterator > y_iterator
An iterator to a column of a MaskedImage.
Lifetime-management for memory that goes into FITS memory files.
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
MaskPixelT mask() const
Return the mask part of a Pixel.
A class to manipulate images, masks, and variance as a single object.
_view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
ImagePixelT image() const
Return the image part of a Pixel.
std::shared_ptr< daf::base::PropertyList > readPrimaryMetadata()
Read the FITS header of one of the HDUs.
MaskedImageIterator< typename Image::reverse_iterator, typename Mask::reverse_iterator, typename Variance::reverse_iterator > reverse_iterator
bool imagesOverlap(ImageBase< T1 > const &image1, ImageBase< T2 > const &image2)
Return true if the pixels for two images or masks overlap in memory.
Reports errors in the logical structure of the program.
void setHdu(int hdu, bool relative=false)
Set the current HDU.
x_iterator row_end(int y) const
Return an x_iterator to the end of the image.
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.
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage's mask.
void swap(Image< PixelT > &a, Image< PixelT > &b)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
A pixel of a MaskedImage.
Image< LhsPixelT > & operator*=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Multiply lhs by Image rhs (i.e. pixel-by-pixel multiplication) where types are different.
Image< LhsPixelT > & operator-=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Subtract lhs from Image rhs (i.e. pixel-by-pixel subtraction) where types are different.
y_iterator col_end(int x) const
Return an y_iterator to the end of the image.
A FITS reader class for MaskedImages and their components.
Image< LhsPixelT > & operator/=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Divide lhs by Image rhs (i.e. pixel-by-pixel division) where types are different. ...
std::shared_ptr< daf::base::PropertyList > readImageMetadata()
Read the FITS header of one of the HDUs.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
An integer coordinate rectangle.
#define INSTANTIATE2(ImagePixelT1, ImagePixelT2)
void scaledMinus(double const c, MaskedImage const &rhs)
Subtract a scaled MaskedImage c*rhs from a MaskedImage.
int countHdus()
Return the number of HDUs in the file.