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 : _image(new
Image(width, height)),
51 _mask(new
Mask(width, height, planeDict)),
52 _variance(new
Variance(width, height)) {
58 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
61 : _image(new
Image(dimensions)),
62 _mask(new
Mask(dimensions, planeDict)),
63 _variance(new
Variance(dimensions)) {
69 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
72 : _image(new
Image(bbox)), _mask(new
Mask(bbox, planeDict)), _variance(new
Variance(bbox)) {
78 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
85 : _image(), _mask(), _variance() {
87 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
98 if (varianceMetadata) {
103 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
110 : _image(), _mask(), _variance() {
112 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
123 if (varianceMetadata) {
128 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
131 ImageOrigin origin,
bool conformMasks,
bool needAllHdus,
135 : _image(), _mask(), _variance() {
137 *
this = reader.
read<ImagePixelT, MaskPixelT, VariancePixelT>(
bbox, origin, conformMasks, needAllHdus,
148 if (varianceMetadata) {
153 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
156 : _image(image), _mask(mask), _variance(variance) {
160 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
162 : _image(rhs._image), _mask(rhs._mask), _variance(rhs._variance) {
172 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
176 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
182 : _image(new
Image(*rhs.getImage(), bbox, origin, deep)),
183 _mask(rhs._mask ? new
Mask(*rhs.getMask(), bbox, origin, deep) : static_cast<
Mask*>(NULL)),
184 _variance(rhs._variance ? new
Variance(*rhs.getVariance(), bbox, origin, deep)
189 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
193 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
197 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
201 _image.swap(rhs._image);
202 _mask.swap(rhs._mask);
203 _variance.swap(rhs._variance);
207 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
210 *_image = rhs.
image();
217 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
220 *_image = rhs.
image();
227 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
234 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
243 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
252 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
255 (*_image).scaledPlus(c, *rhs.
getImage());
257 (*_variance).scaledPlus(c * c, *rhs.
getVariance());
260 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
267 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
276 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
281 (*_variance).scaledPlus(c * c, *rhs.
getVariance());
284 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
293 template <
typename ImagePixelT,
typename VariancePixelT>
294 struct productVariance {
295 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
296 return lhs * lhs * varRhs + rhs * rhs * varLhs;
302 template <
typename ImagePixelT,
typename VariancePixelT>
303 struct scaledProductVariance {
305 scaledProductVariance(
double const c) : _c(c) {}
306 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
307 return _c * _c * (lhs * lhs * varRhs + rhs * rhs * varLhs);
312 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
316 transform_pixels(_image->_getRawView(),
317 rhs._image->_getRawView(),
318 _variance->_getRawView(),
319 rhs._variance->_getRawView(),
320 _variance->_getRawView(),
321 productVariance<ImagePixelT, VariancePixelT>());
328 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
332 transform_pixels(_image->_getRawView(),
333 rhs._image->_getRawView(),
334 _variance->_getRawView(),
335 rhs._variance->_getRawView(),
336 _variance->_getRawView(),
337 scaledProductVariance<ImagePixelT, VariancePixelT>(c));
339 (*_image).scaledMultiplies(c, *rhs.
getImage());
343 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
347 *_variance *= rhs * rhs;
353 template <
typename ImagePixelT,
typename VariancePixelT>
354 struct quotientVariance {
355 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
356 ImagePixelT
const rhs2 = rhs * rhs;
357 return (lhs * lhs * varRhs + rhs2 * varLhs) / (rhs2 * rhs2);
362 template <
typename ImagePixelT,
typename VariancePixelT>
363 struct scaledQuotientVariance {
365 scaledQuotientVariance(
double c) :
_c(c) {}
366 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
367 ImagePixelT
const rhs2 = rhs * rhs;
368 return (lhs * lhs * varRhs + rhs2 * varLhs) / (_c * _c * rhs2 * rhs2);
373 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
377 transform_pixels(_image->_getRawView(),
378 rhs._image->_getRawView(),
379 _variance->_getRawView(),
380 rhs._variance->_getRawView(),
381 _variance->_getRawView(),
382 quotientVariance<ImagePixelT, VariancePixelT>());
389 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
393 transform_pixels(_image->_getRawView(),
394 rhs._image->_getRawView(),
395 _variance->_getRawView(),
396 rhs._variance->_getRawView(),
397 _variance->_getRawView(),
398 scaledQuotientVariance<ImagePixelT, VariancePixelT>(c));
400 (*_image).scaledDivides(c, *rhs.
getImage());
401 *_mask |= *rhs._mask;
404 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
408 *_variance /= rhs * rhs;
412 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
418 fits::Fits fitsfile(fileName,
"w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
419 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
422 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
428 fits::Fits fitsfile(manager,
"w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
429 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
437 hdr = metadata->deepCopy();
441 hdr->set(
"INHERIT",
true);
442 hdr->set(
"EXTTYPE", exttype);
447 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
457 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
465 fits::Fits fitsfile(fileName,
"w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
466 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
470 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
478 fits::Fits fitsfile(manager,
"w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
479 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
483 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
493 header = metadata->deepCopy();
495 header = std::make_shared<daf::base::PropertyList>();
500 "MaskedImage::writeFits can only write to an empty file");
502 if (fitsfile.
getHdu() < 1) {
510 processPlaneMetadata(imageMetadata, header,
"IMAGE");
511 _image->writeFits(fitsfile, imageOptions, header, _mask);
513 processPlaneMetadata(maskMetadata, header,
"MASK");
514 _mask->writeFits(fitsfile, maskOptions, header);
516 processPlaneMetadata(varianceMetadata, header,
"VARIANCE");
517 _variance->writeFits(fitsfile, varianceOptions, header, _mask);
524 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
526 if (!_mask || _mask->getWidth() == 0 || _mask->getHeight() == 0) {
530 if (_mask->getDimensions() != _image->getDimensions()) {
533 (
boost::format(
"Dimension mismatch: Image %dx%d v. Mask %dx%d") % _image->getWidth() %
534 _image->getHeight() % _mask->getWidth() % _mask->getHeight())
539 if (!_variance || _variance->getWidth() == 0 || _variance->getHeight() == 0) {
543 if (_variance->getDimensions() != _image->getDimensions()) {
546 (
boost::format(
"Dimension mismatch: Image %dx%d v. Variance %dx%d") % _image->getWidth() %
547 _image->getHeight() % _variance->getWidth() % _variance->getHeight())
556 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
559 #if 0 // this doesn't compile; why? 560 return iterator(_image->begin(), _mask->begin(), _variance->begin());
566 return iterator(imageBegin, maskBegin, varianceBegin);
570 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
577 return iterator(imageEnd, maskEnd, varianceEnd);
580 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
587 return iterator(imageEnd, maskEnd, varianceEnd);
590 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
600 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
610 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
617 return x_iterator(imageBegin, maskBegin, varianceBegin);
620 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
627 return x_iterator(imageEnd, maskEnd, varianceEnd);
630 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
637 return y_iterator(imageBegin, maskBegin, varianceBegin);
640 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
647 return y_iterator(imageEnd, maskEnd, varianceEnd);
650 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
660 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
670 template <
typename ImagePixelT1,
typename ImagePixelT2>
681 #define INSTANTIATE2(ImagePixelT1, ImagePixelT2) \ 682 template bool imagesOverlap<ImagePixelT1, ImagePixelT2>(MaskedImage<ImagePixelT1> const&, \ 683 MaskedImage<ImagePixelT2> const&); 685 template class MaskedImage<std::uint16_t>;
686 template class MaskedImage<int>;
687 template class MaskedImage<float>;
688 template class MaskedImage<double>;
689 template class MaskedImage<std::uint64_t>;
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
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.
afw::table::PointKey< int > dimensions
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.
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.
#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.
void swap(CameraSys &a, CameraSys &b)
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.