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>
69 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
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>
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>
236 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
245 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
248 (*_image).scaledPlus(c, *rhs.
getImage());
250 (*_variance).scaledPlus(c * c, *rhs.
getVariance());
253 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
260 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
269 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
272 (*_image).scaledMinus(c, *rhs.
getImage());
274 (*_variance).scaledPlus(c * c, *rhs.
getVariance());
277 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
286 template <
typename ImagePixelT,
typename VariancePixelT>
287 struct productVariance {
288 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
289 return lhs * lhs * varRhs + rhs * rhs * varLhs;
295 template <
typename ImagePixelT,
typename VariancePixelT>
296 struct scaledProductVariance {
298 scaledProductVariance(
double const c) :
_c(c) {}
299 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
300 return _c *
_c * (lhs * lhs * varRhs + rhs * rhs * varLhs);
305 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
309 transform_pixels(_image->_getRawView(),
311 _variance->_getRawView(),
312 rhs._variance->_getRawView(),
313 _variance->_getRawView(),
314 productVariance<ImagePixelT, VariancePixelT>());
321 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
325 transform_pixels(_image->_getRawView(),
327 _variance->_getRawView(),
328 rhs._variance->_getRawView(),
329 _variance->_getRawView(),
330 scaledProductVariance<ImagePixelT, VariancePixelT>(c));
332 (*_image).scaledMultiplies(c, *rhs.
getImage());
336 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
340 *_variance *= rhs * rhs;
346 template <
typename ImagePixelT,
typename VariancePixelT>
347 struct quotientVariance {
348 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
349 ImagePixelT
const rhs2 = rhs * rhs;
350 return (lhs * lhs * varRhs + rhs2 * varLhs) / (rhs2 * rhs2);
355 template <
typename ImagePixelT,
typename VariancePixelT>
356 struct scaledQuotientVariance {
358 scaledQuotientVariance(
double c) :
_c(c) {}
359 double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
360 ImagePixelT
const rhs2 = rhs * rhs;
361 return (lhs * lhs * varRhs + rhs2 * varLhs) / (
_c *
_c * rhs2 * rhs2);
366 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
370 transform_pixels(_image->_getRawView(),
372 _variance->_getRawView(),
373 rhs._variance->_getRawView(),
374 _variance->_getRawView(),
375 quotientVariance<ImagePixelT, VariancePixelT>());
382 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
386 transform_pixels(_image->_getRawView(),
388 _variance->_getRawView(),
389 rhs._variance->_getRawView(),
390 _variance->_getRawView(),
391 scaledQuotientVariance<ImagePixelT, VariancePixelT>(c));
393 (*_image).scaledDivides(c, *rhs.
getImage());
394 *_mask |= *rhs._mask;
397 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
401 *_variance /= rhs * rhs;
405 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
412 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
415 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
422 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
430 hdr = metadata->deepCopy();
434 hdr->set(
"INHERIT",
true);
435 hdr->set(
"EXTTYPE", exttype);
440 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
450 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
459 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
463 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
472 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
476 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
486 header = metadata->deepCopy();
488 header = std::make_shared<daf::base::PropertyList>();
493 "MaskedImage::writeFits can only write to an empty file");
495 if (fitsfile.
getHdu() < 1) {
503 processPlaneMetadata(imageMetadata, header,
"IMAGE");
504 _image->writeFits(fitsfile, imageOptions, header, _mask);
506 processPlaneMetadata(maskMetadata, header,
"MASK");
507 _mask->writeFits(fitsfile, maskOptions, header);
509 processPlaneMetadata(varianceMetadata, header,
"VARIANCE");
510 _variance->writeFits(fitsfile, varianceOptions, header, _mask);
517 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
519 if (!_mask || _mask->getWidth() == 0 || _mask->getHeight() == 0) {
520 _mask = MaskPtr(
new Mask(_image->getBBox()));
523 if (_mask->getDimensions() != _image->getDimensions()) {
526 (
boost::format(
"Dimension mismatch: Image %dx%d v. Mask %dx%d") % _image->getWidth() %
527 _image->getHeight() % _mask->getWidth() % _mask->getHeight())
532 if (!_variance || _variance->getWidth() == 0 || _variance->getHeight() == 0) {
533 _variance = VariancePtr(
new Variance(_image->getBBox()));
536 if (_variance->getDimensions() != _image->getDimensions()) {
538 pex::exceptions::LengthError,
539 (
boost::format(
"Dimension mismatch: Image %dx%d v. Variance %dx%d") % _image->getWidth() %
540 _image->getHeight() % _variance->getWidth() % _variance->getHeight())
549 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
552 #if 0 // this doesn't compile; why?
553 return iterator(_image->begin(), _mask->begin(), _variance->begin());
559 return iterator(imageBegin, maskBegin, varianceBegin);
563 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
570 return iterator(imageEnd, maskEnd, varianceEnd);
573 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
580 return iterator(imageEnd, maskEnd, varianceEnd);
583 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
593 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
603 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
610 return x_iterator(imageBegin, maskBegin, varianceBegin);
613 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
620 return x_iterator(imageEnd, maskEnd, varianceEnd);
623 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
630 return y_iterator(imageBegin, maskBegin, varianceBegin);
633 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
640 return y_iterator(imageEnd, maskEnd, varianceEnd);
643 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
653 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
663 template <
typename ImagePixelT1,
typename ImagePixelT2>
674 #define INSTANTIATE2(ImagePixelT1, ImagePixelT2) \
675 template bool imagesOverlap<ImagePixelT1, ImagePixelT2>(MaskedImage<ImagePixelT1> const&, \
676 MaskedImage<ImagePixelT2> const&);
678 template class MaskedImage<std::uint16_t>;
679 template class MaskedImage<int>;
680 template class MaskedImage<float>;
681 template class MaskedImage<double>;
682 template class MaskedImage<std::uint64_t>;