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>
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>
279 (*_image).scaledMinus(c, *rhs.
getImage());
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(),
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(),
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(),
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(),
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>
419 writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
422 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
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>
466 writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
470 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
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) {
527 _mask = MaskPtr(
new Mask(_image->getBBox()));
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) {
540 _variance = VariancePtr(
new Variance(_image->getBBox()));
543 if (_variance->getDimensions() != _image->getDimensions()) {
545 pex::exceptions::LengthError,
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>
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>;