LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
MaskedImage.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2016 AURA/LSST.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * Implementation for MaskedImage
27  */
28 #include <cstdint>
29 #include <typeinfo>
30 #include <sys/stat.h>
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 "lsst/log/Log.h"
37 #include "lsst/pex/exceptions.h"
38 
40 #include "lsst/afw/fits.h"
42 
43 namespace lsst {
44 namespace afw {
45 namespace image {
46 
47 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
49  MaskPlaneDict const& planeDict)
50  : _image(new Image(width, height)),
51  _mask(new Mask(width, height, planeDict)),
52  _variance(new Variance(width, height)) {
53  *_image = 0;
54  *_mask = 0x0;
55  *_variance = 0;
56 }
57 
58 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
60  MaskPlaneDict const& planeDict)
61  : _image(new Image(dimensions)),
62  _mask(new Mask(dimensions, planeDict)),
63  _variance(new Variance(dimensions)) {
64  *_image = 0;
65  *_mask = 0x0;
66  *_variance = 0;
67 }
68 
69 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
71  MaskPlaneDict const& planeDict)
72  : _image(new Image(bbox)), _mask(new Mask(bbox, planeDict)), _variance(new Variance(bbox)) {
73  *_image = 0;
74  *_mask = 0x0;
75  *_variance = 0;
76 }
77 
78 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
80  std::string const& fileName, std::shared_ptr<daf::base::PropertySet> metadata,
81  lsst::geom::Box2I const& bbox, ImageOrigin origin, bool conformMasks, bool needAllHdus,
84  std::shared_ptr<daf::base::PropertySet> varianceMetadata, bool allowUnsafe)
85  : _image(), _mask(), _variance() {
86  MaskedImageFitsReader reader(fileName);
87  *this = reader.read<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks, needAllHdus,
88  allowUnsafe);
89  if (metadata) {
90  metadata->combine(reader.readPrimaryMetadata());
91  }
92  if (imageMetadata) {
93  imageMetadata->combine(reader.readImageMetadata());
94  }
95  if (maskMetadata) {
96  maskMetadata->combine(reader.readMaskMetadata());
97  }
98  if (varianceMetadata) {
99  varianceMetadata->combine(reader.readVarianceMetadata());
100  }
101 }
102 
103 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
106  lsst::geom::Box2I const& bbox, ImageOrigin origin, bool conformMasks, bool needAllHdus,
109  std::shared_ptr<daf::base::PropertySet> varianceMetadata, bool allowUnsafe)
110  : _image(), _mask(), _variance() {
111  MaskedImageFitsReader reader(manager);
112  *this = reader.read<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks, needAllHdus,
113  allowUnsafe);
114  if (metadata) {
115  metadata->combine(reader.readPrimaryMetadata());
116  }
117  if (imageMetadata) {
118  imageMetadata->combine(reader.readImageMetadata());
119  }
120  if (maskMetadata) {
121  maskMetadata->combine(reader.readMaskMetadata());
122  }
123  if (varianceMetadata) {
124  varianceMetadata->combine(reader.readVarianceMetadata());
125  }
126 }
127 
128 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
131  ImageOrigin origin, bool conformMasks, bool needAllHdus,
134  std::shared_ptr<daf::base::PropertySet> varianceMetadata, bool allowUnsafe)
135  : _image(), _mask(), _variance() {
136  MaskedImageFitsReader reader(&fitsFile);
137  *this = reader.read<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks, needAllHdus,
138  allowUnsafe);
139  if (metadata) {
140  metadata->combine(reader.readPrimaryMetadata());
141  }
142  if (imageMetadata) {
143  imageMetadata->combine(reader.readImageMetadata());
144  }
145  if (maskMetadata) {
146  maskMetadata->combine(reader.readMaskMetadata());
147  }
148  if (varianceMetadata) {
149  varianceMetadata->combine(reader.readVarianceMetadata());
150  }
151 }
152 
153 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
156  : _image(image), _mask(mask), _variance(variance) {
157  conformSizes();
158 }
159 
160 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
162  : _image(rhs._image), _mask(rhs._mask), _variance(rhs._variance) {
163  if (deep) {
164  _image = std::shared_ptr<Image>(new Image(*rhs.getImage(), deep));
165  _mask = std::shared_ptr<Mask>(new Mask(*rhs.getMask(), deep));
166  _variance = std::shared_ptr<Variance>(new Variance(*rhs.getVariance(), deep));
167  }
168  conformSizes();
169 }
170 
171 // Delegate to copy-constructor for backwards compatibility
172 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
174  : MaskedImage(rhs, false) {}
175 
176 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
178  const lsst::geom::Box2I& bbox,
179  ImageOrigin const origin, bool deep
180 
181  )
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)
185  : static_cast<Variance*>(NULL)) {
186  conformSizes();
187 }
188 
189 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
191 operator=(MaskedImage const& rhs) = default;
192 
193 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
195 operator=(MaskedImage&& rhs) = default;
196 
197 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
199  using std::swap; // See Meyers, Effective C++, Item 25
200 
201  _image.swap(rhs._image);
202  _mask.swap(rhs._mask);
203  _variance.swap(rhs._variance);
204 }
205 
206 // Operators
207 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
209 operator=(MaskedImage::Pixel const& rhs) {
210  *_image = rhs.image();
211  *_mask = rhs.mask();
212  *_variance = rhs.variance();
213 
214  return *this;
215 }
216 
217 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
220  *_image = rhs.image();
221  *_mask = rhs.mask();
222  *_variance = rhs.variance();
223 
224  return *this;
225 }
226 
227 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
229  lsst::geom::Box2I const& bbox,
230  ImageOrigin origin) {
231  _image->assign(*rhs.getImage(), bbox, origin);
232  _mask->assign(*rhs.getMask(), bbox, origin);
233  _variance->assign(*rhs.getVariance(), bbox, origin);
234 }
235 
236 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
238 operator+=(MaskedImage const& rhs) {
239  *_image += *rhs.getImage();
240  *_mask |= *rhs.getMask();
241  *_variance += *rhs.getVariance();
242  return *this;
243 }
244 
245 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
247  MaskedImage const& rhs) {
248  (*_image).scaledPlus(c, *rhs.getImage());
249  *_mask |= *rhs.getMask();
250  (*_variance).scaledPlus(c * c, *rhs.getVariance());
251 }
252 
253 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
255 operator+=(ImagePixelT const rhs) {
256  *_image += rhs;
257  return *this;
258 }
259 
260 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
262 operator-=(MaskedImage const& rhs) {
263  *_image -= *rhs.getImage();
264  *_mask |= *rhs.getMask();
265  *_variance += *rhs.getVariance();
266  return *this;
267 }
268 
269 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
271  MaskedImage const& rhs) {
272  (*_image).scaledMinus(c, *rhs.getImage());
273  *_mask |= *rhs.getMask();
274  (*_variance).scaledPlus(c * c, *rhs.getVariance());
275 }
276 
277 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
279 operator-=(ImagePixelT const rhs) {
280  *_image -= rhs;
281  return *this;
282 }
283 
284 namespace {
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;
290  }
291 };
292 
295 template <typename ImagePixelT, typename VariancePixelT>
296 struct scaledProductVariance {
297  double _c;
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);
301  }
302 };
303 } // namespace
304 
305 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
307 operator*=(MaskedImage const& rhs) {
308  // Must do variance before we modify the image values
309  transform_pixels(_image->_getRawView(), // lhs
310  rhs._image->_getRawView(), // rhs,
311  _variance->_getRawView(), // Var(lhs),
312  rhs._variance->_getRawView(), // Var(rhs)
313  _variance->_getRawView(), // result
314  productVariance<ImagePixelT, VariancePixelT>());
315 
316  *_image *= *rhs.getImage();
317  *_mask |= *rhs.getMask();
318  return *this;
319 }
320 
321 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
323  MaskedImage const& rhs) {
324  // Must do variance before we modify the image values
325  transform_pixels(_image->_getRawView(), // lhs
326  rhs._image->_getRawView(), // rhs,
327  _variance->_getRawView(), // Var(lhs),
328  rhs._variance->_getRawView(), // Var(rhs)
329  _variance->_getRawView(), // result
330  scaledProductVariance<ImagePixelT, VariancePixelT>(c));
331 
332  (*_image).scaledMultiplies(c, *rhs.getImage());
333  *_mask |= *rhs.getMask();
334 }
335 
336 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
338 operator*=(ImagePixelT const rhs) {
339  *_image *= rhs;
340  *_variance *= rhs * rhs;
341  return *this;
342 }
343 
344 namespace {
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);
351  }
352 };
355 template <typename ImagePixelT, typename VariancePixelT>
356 struct scaledQuotientVariance {
357  double _c;
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);
362  }
363 };
364 } // namespace
365 
366 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
368 operator/=(MaskedImage const& rhs) {
369  // Must do variance before we modify the image values
370  transform_pixels(_image->_getRawView(), // lhs
371  rhs._image->_getRawView(), // rhs,
372  _variance->_getRawView(), // Var(lhs),
373  rhs._variance->_getRawView(), // Var(rhs)
374  _variance->_getRawView(), // result
375  quotientVariance<ImagePixelT, VariancePixelT>());
376 
377  *_image /= *rhs.getImage();
378  *_mask |= *rhs.getMask();
379  return *this;
380 }
381 
382 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
384  MaskedImage const& rhs) {
385  // Must do variance before we modify the image values
386  transform_pixels(_image->_getRawView(), // lhs
387  rhs._image->_getRawView(), // rhs,
388  _variance->_getRawView(), // Var(lhs),
389  rhs._variance->_getRawView(), // Var(rhs)
390  _variance->_getRawView(), // result
391  scaledQuotientVariance<ImagePixelT, VariancePixelT>(c));
392 
393  (*_image).scaledDivides(c, *rhs.getImage());
394  *_mask |= *rhs._mask;
395 }
396 
397 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
399 operator/=(ImagePixelT const rhs) {
400  *_image /= rhs;
401  *_variance /= rhs * rhs;
402  return *this;
403 }
404 
405 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
410  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
411  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
412  writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
413 }
414 
415 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
420  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
421  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
422  writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
423 }
424 
425 namespace {
426 
427 void processPlaneMetadata(std::shared_ptr<daf::base::PropertySet const> metadata,
428  std::shared_ptr<daf::base::PropertySet>& hdr, char const* exttype) {
429  if (metadata) {
430  hdr = metadata->deepCopy();
431  } else {
432  hdr.reset(new daf::base::PropertyList());
433  }
434  hdr->set("INHERIT", true);
435  hdr->set("EXTTYPE", exttype);
436 }
437 
438 } // namespace
439 
440 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
445  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
446  writeFits(fitsfile, fits::ImageWriteOptions(*_image), fits::ImageWriteOptions(*_mask),
447  fits::ImageWriteOptions(*_variance), metadata, imageMetadata, maskMetadata, varianceMetadata);
448 }
449 
450 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
452  std::string const& fileName, fits::ImageWriteOptions const& imageOptions,
453  fits::ImageWriteOptions const& maskOptions, fits::ImageWriteOptions const& varianceOptions,
457  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
458  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
459  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
460  varianceMetadata);
461 }
462 
463 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
465  fits::MemFileManager& manager, fits::ImageWriteOptions const& imageOptions,
466  fits::ImageWriteOptions const& maskOptions, fits::ImageWriteOptions const& varianceOptions,
470  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
471  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
472  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
473  varianceMetadata);
474 }
475 
476 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
478  fits::Fits& fitsfile, fits::ImageWriteOptions const& imageOptions,
479  fits::ImageWriteOptions const& maskOptions, fits::ImageWriteOptions const& varianceOptions,
483  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
485  if (metadata) {
486  header = metadata->deepCopy();
487  } else {
488  header = std::make_shared<daf::base::PropertyList>();
489  }
490 
491  if (fitsfile.countHdus() != 0) {
493  "MaskedImage::writeFits can only write to an empty file");
494  }
495  if (fitsfile.getHdu() < 1) {
496  // Don't ever write images to primary; instead we make an empty primary.
497  fitsfile.createEmpty();
498  } else {
499  fitsfile.setHdu(0);
500  }
501  fitsfile.writeMetadata(*header);
502 
503  processPlaneMetadata(imageMetadata, header, "IMAGE");
504  _image->writeFits(fitsfile, imageOptions, header, _mask);
505 
506  processPlaneMetadata(maskMetadata, header, "MASK");
507  _mask->writeFits(fitsfile, maskOptions, header);
508 
509  processPlaneMetadata(varianceMetadata, header, "VARIANCE");
510  _variance->writeFits(fitsfile, varianceOptions, header, _mask);
511 }
512 
513 // private function conformSizes() ensures that the Mask and Variance have the same dimensions
514 // as Image. If Mask and/or Variance have non-zero dimensions that conflict with the size of Image,
515 // a lsst::pex::exceptions::LengthError is thrown.
516 
517 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
519  if (!_mask || _mask->getWidth() == 0 || _mask->getHeight() == 0) {
520  _mask = MaskPtr(new Mask(_image->getBBox()));
521  *_mask = 0;
522  } else {
523  if (_mask->getDimensions() != _image->getDimensions()) {
524  throw LSST_EXCEPT(
526  (boost::format("Dimension mismatch: Image %dx%d v. Mask %dx%d") % _image->getWidth() %
527  _image->getHeight() % _mask->getWidth() % _mask->getHeight())
528  .str());
529  }
530  }
531 
532  if (!_variance || _variance->getWidth() == 0 || _variance->getHeight() == 0) {
533  _variance = VariancePtr(new Variance(_image->getBBox()));
534  *_variance = 0;
535  } else {
536  if (_variance->getDimensions() != _image->getDimensions()) {
537  throw LSST_EXCEPT(
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())
541  .str());
542  }
543  }
544 }
545 
546 //
547 // Iterators and locators
548 //
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());
554 #else
555  typename Image::iterator imageBegin = _image->begin();
556  typename Mask::iterator maskBegin = _mask->begin();
557  typename Variance::iterator varianceBegin = _variance->begin();
558 
559  return iterator(imageBegin, maskBegin, varianceBegin);
560 #endif
561 }
562 
563 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
566  typename Image::iterator imageEnd = getImage()->end();
567  typename Mask::iterator maskEnd = getMask()->end();
568  typename Variance::iterator varianceEnd = getVariance()->end();
569 
570  return iterator(imageEnd, maskEnd, varianceEnd);
571 }
572 
573 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
576  typename Image::iterator imageEnd = getImage()->at(x, y);
577  typename Mask::iterator maskEnd = getMask()->at(x, y);
578  typename Variance::iterator varianceEnd = getVariance()->at(x, y);
579 
580  return iterator(imageEnd, maskEnd, varianceEnd);
581 }
582 
583 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
586  typename Image::reverse_iterator imageBegin = _image->rbegin();
587  typename Mask::reverse_iterator maskBegin = _mask->rbegin();
588  typename Variance::reverse_iterator varianceBegin = _variance->rbegin();
589 
590  return reverse_iterator(imageBegin, maskBegin, varianceBegin);
591 }
592 
593 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
596  typename Image::reverse_iterator imageEnd = getImage()->rend();
597  typename Mask::reverse_iterator maskEnd = getMask()->rend();
598  typename Variance::reverse_iterator varianceEnd = getVariance()->rend();
599 
600  return reverse_iterator(imageEnd, maskEnd, varianceEnd);
601 }
602 
603 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
606  typename Image::x_iterator imageBegin = _image->row_begin(y);
607  typename Mask::x_iterator maskBegin = _mask->row_begin(y);
608  typename Variance::x_iterator varianceBegin = _variance->row_begin(y);
609 
610  return x_iterator(imageBegin, maskBegin, varianceBegin);
611 }
612 
613 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
616  typename Image::x_iterator imageEnd = getImage()->row_end(y);
617  typename Mask::x_iterator maskEnd = getMask()->row_end(y);
618  typename Variance::x_iterator varianceEnd = getVariance()->row_end(y);
619 
620  return x_iterator(imageEnd, maskEnd, varianceEnd);
621 }
622 
623 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
626  typename Image::y_iterator imageBegin = _image->col_begin(x);
627  typename Mask::y_iterator maskBegin = _mask->col_begin(x);
628  typename Variance::y_iterator varianceBegin = _variance->col_begin(x);
629 
630  return y_iterator(imageBegin, maskBegin, varianceBegin);
631 }
632 
633 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
636  typename Image::y_iterator imageEnd = getImage()->col_end(x);
637  typename Mask::y_iterator maskEnd = getMask()->col_end(x);
638  typename Variance::y_iterator varianceEnd = getVariance()->col_end(x);
639 
640  return y_iterator(imageEnd, maskEnd, varianceEnd);
641 }
642 
643 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
646  typename Image::fast_iterator imageBegin = _image->begin(contiguous);
647  typename Mask::fast_iterator maskBegin = _mask->begin(contiguous);
648  typename Variance::fast_iterator varianceBegin = _variance->begin(contiguous);
649 
650  return fast_iterator(imageBegin, maskBegin, varianceBegin);
651 }
652 
653 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
656  typename Image::fast_iterator imageEnd = getImage()->end(contiguous);
657  typename Mask::fast_iterator maskEnd = getMask()->end(contiguous);
658  typename Variance::fast_iterator varianceEnd = getVariance()->end(contiguous);
659 
660  return fast_iterator(imageEnd, maskEnd, varianceEnd);
661 }
662 
663 template <typename ImagePixelT1, typename ImagePixelT2>
666  return imagesOverlap(*image1.getImage(), *image2.getImage()) ||
667  imagesOverlap(*image1.getVariance(), *image2.getVariance()) ||
668  imagesOverlap(*image1.getMask(), *image2.getMask());
669 }
670 
671 //
672 // Explicit instantiations
673 //
674 #define INSTANTIATE2(ImagePixelT1, ImagePixelT2) \
675  template bool imagesOverlap<ImagePixelT1, ImagePixelT2>(MaskedImage<ImagePixelT1> const&, \
676  MaskedImage<ImagePixelT2> const&);
677 
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>;
683 
689 
691 INSTANTIATE2(int, int);
692 INSTANTIATE2(int, float);
693 INSTANTIATE2(int, double);
695 
697 INSTANTIATE2(float, int);
698 INSTANTIATE2(float, float);
699 INSTANTIATE2(float, double);
701 
703 INSTANTIATE2(double, int);
704 INSTANTIATE2(double, float);
705 INSTANTIATE2(double, double);
707 
713 
714 } // namespace image
715 } // namespace afw
716 } // namespace lsst
lsst::afw::image::pixel::Pixel::mask
MaskPixelT mask() const
Return the mask part of a Pixel.
Definition: Pixel.h:219
y
int y
Definition: SpanSet.cc:49
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
lsst::afw::image::MaskedImage::swap
void swap(MaskedImage &rhs)
Definition: MaskedImage.cc:198
lsst::afw::image::MaskedImage::scaledMultiplies
void scaledMultiplies(double const c, MaskedImage const &rhs)
Definition: MaskedImage.cc:322
lsst::afw::image::MaskedImage::row_begin
x_iterator row_begin(int y) const
Return an x_iterator to the start of the image.
Definition: MaskedImage.cc:605
lsst::afw::image::MaskedImageFitsReader::readPrimaryMetadata
std::shared_ptr< daf::base::PropertyList > readPrimaryMetadata()
Read the FITS header of one of the HDUs.
Definition: MaskedImageFitsReader.cc:100
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::image::MaskedImage::begin
iterator begin() const
Return an iterator to the start of the image.
Definition: MaskedImage.cc:551
lsst::afw::image::MaskedImage::MaskedImageIterator
An iterator to the MaskedImage.
Definition: MaskedImage.h:99
lsst::afw::image::Mask
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
lsst::afw::image::pixel::Pixel
A pixel of a MaskedImage.
Definition: Pixel.h:148
lsst::afw::image::MaskedImage::row_end
x_iterator row_end(int y) const
Return an x_iterator to the end of the image.
Definition: MaskedImage.cc:615
lsst::afw::image::pixel::SinglePixel
A single pixel of the same type as a MaskedImage.
Definition: Pixel.h:73
lsst::afw::fits::Fits
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
MaskedImage.h
lsst::afw::fits::Fits::writeMetadata
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
Definition: fits.cc:1095
lsst::afw::image::MaskedImage::operator=
MaskedImage & operator=(MaskedImage const &rhs)
Make the lhs use the rhs's pixels.
fits.h
lsst::afw
Definition: imageAlgorithm.dox:1
lsst::afw::image::MaskedImageFitsReader::readVarianceMetadata
std::shared_ptr< daf::base::PropertyList > readVarianceMetadata()
Read the FITS header of one of the HDUs.
Definition: MaskedImageFitsReader.cc:122
lsst::afw::image::ImageBase< ImagePixelT >::reverse_iterator
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
Definition: ImageBase.h:129
lsst::daf::base::PropertyList
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
lsst::afw::image::ImageBase::_getRawView
_view_t _getRawView() const
Definition: ImageBase.h:463
lsst::afw::image::ImageBase< ImagePixelT >::y_iterator
_view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
Definition: ImageBase.h:143
lsst::afw::image::MaskedImage::MaskedImage
MaskedImage(unsigned int width, unsigned int height, MaskPlaneDict const &planeDict=MaskPlaneDict())
Construct from a supplied dimensions.
Definition: MaskedImage.cc:48
lsst.pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::afw::fits::MemFileManager
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
mask
afw::table::Key< afw::table::Array< MaskPixelT > > mask
Definition: HeavyFootprint.cc:217
std::shared_ptr::reset
T reset(T... args)
lsst::afw::fits::ImageWriteOptions
Options for writing an image to FITS.
Definition: fits.h:219
lsst::afw::image::MaskedImage::col_end
y_iterator col_end(int x) const
Return an y_iterator to the end of the image.
Definition: MaskedImage.cc:635
lsst::afw::image::MaskedImage::Variance
lsst::afw::image::Image< VariancePixelT > Variance
Definition: MaskedImage.h:84
lsst::afw::image::pixel::Pixel::variance
VariancePixelT variance() const
Return the variance part of a Pixel.
Definition: Pixel.h:221
lsst::afw::image::MaskedImage
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
lsst::afw::image::MaskedImage::rbegin
reverse_iterator rbegin() const
Return a reverse_iterator to the start of the image.
Definition: MaskedImage.cc:585
lsst::afw::fits::Fits::AUTO_CHECK
@ AUTO_CHECK
Definition: fits.h:308
lsst::afw::fits::Fits::AUTO_CLOSE
@ AUTO_CLOSE
Definition: fits.h:307
x
double x
Definition: ChebyshevBoundedField.cc:277
MaskedImageFitsReader.h
lsst.pex::exceptions::LengthError
Reports attempts to exceed implementation-defined length limits for some classes.
Definition: Runtime.h:76
lsst::afw::fits::Fits::setHdu
void setHdu(int hdu, bool relative=false)
Set the current HDU.
Definition: fits.cc:513
lsst.pex::exceptions::LogicError
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
lsst::afw::image::MaskedImage::operator/=
MaskedImage & operator/=(ImagePixelT const rhs)
Definition: MaskedImage.cc:399
lsst::afw::image::MaskedImage::at
iterator at(int const x, int const y) const
Return an iterator at the point (x, y)
Definition: MaskedImage.cc:575
lsst::afw::image::MaskedImage::scaledPlus
void scaledPlus(double const c, MaskedImage const &rhs)
Add a scaled MaskedImage c*rhs to a MaskedImage.
Definition: MaskedImage.cc:246
std::uint16_t
dimensions
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
lsst::afw::image::MaskedImage::Mask
lsst::afw::image::Mask< MaskPixelT > Mask
Definition: MaskedImage.h:86
lsst::afw::image::MaskedImage::operator+=
MaskedImage & operator+=(ImagePixelT const rhs)
Add a scalar rhs to a MaskedImage.
Definition: MaskedImage.cc:255
std::map< std::string, int >
lsst::afw::image::MaskedImage::scaledMinus
void scaledMinus(double const c, MaskedImage const &rhs)
Subtract a scaled MaskedImage c*rhs from a MaskedImage.
Definition: MaskedImage.cc:270
lsst::afw::image::MaskedImageFitsReader::read
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.
Definition: MaskedImageFitsReader.cc:170
lsst::afw::image::pixel::SinglePixel::mask
MaskPixelT mask() const
Definition: Pixel.h:95
lsst::afw::image::ImageBase< Pixel >::x_iterator
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:133
lsst::afw::image::MaskedImage::end
iterator end() const
Return an iterator to the end of the image.
Definition: MaskedImage.cc:565
lsst::afw::image::MaskedImage< Pixel, lsst::afw::image::MaskPixel, lsst::afw::image::VariancePixel >::reverse_iterator
MaskedImageIterator< typename Image::reverse_iterator, typename Mask::reverse_iterator, typename Variance::reverse_iterator > reverse_iterator
Definition: MaskedImage.h:547
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::afw::fits::Fits::getHdu
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
Definition: fits.cc:507
lsst::afw::image::MaskedImage::getVariance
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
Definition: MaskedImage.h:1079
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
std::swap
T swap(T... args)
lsst::afw::image::MaskedImage::scaledDivides
void scaledDivides(double const c, MaskedImage const &rhs)
Definition: MaskedImage.cc:383
lsst::afw::image::MaskedImageFitsReader::readMaskMetadata
std::shared_ptr< daf::base::PropertyList > readMaskMetadata()
Read the FITS header of one of the HDUs.
Definition: MaskedImageFitsReader.cc:114
lsst::afw::image::pixel::SinglePixel::image
ImagePixelT image() const
Definition: Pixel.h:94
variance
afw::table::Key< afw::table::Array< VariancePixelT > > variance
Definition: HeavyFootprint.cc:218
lsst::afw::image::ImageBase< ImagePixelT >::fast_iterator
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Definition: ImageBase.h:137
lsst::afw::image::MaskedImageFitsReader::readImageMetadata
std::shared_ptr< daf::base::PropertyList > readImageMetadata()
Read the FITS header of one of the HDUs.
Definition: MaskedImageFitsReader.cc:106
lsst::afw::image::MaskedImage< Pixel, lsst::afw::image::MaskPixel, lsst::afw::image::VariancePixel >::fast_iterator
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Definition: MaskedImage.h:564
lsst::afw::image::MaskedImage< Pixel, lsst::afw::image::MaskPixel, lsst::afw::image::VariancePixel >::y_iterator
MaskedImageIterator< typename Image::y_iterator, typename Mask::y_iterator, typename Variance::y_iterator > y_iterator
An iterator to a column of a MaskedImage.
Definition: MaskedImage.h:568
lsst::afw::image::ImageBase< ImagePixelT >::iterator
_view_t::iterator iterator
An STL compliant iterator.
Definition: ImageBase.h:125
lsst::afw::fits::Fits::createEmpty
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
Definition: fits.cc:1240
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::afw::image::MaskedImage::getImage
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
Definition: MaskedImage.h:1046
INSTANTIATE2
#define INSTANTIATE2(ImagePixelT1, ImagePixelT2)
Definition: MaskedImage.cc:674
lsst::afw::image::pixel::SinglePixel::variance
VariancePixelT variance() const
Definition: Pixel.h:96
lsst::afw::image::MaskedImage::iterator
MaskedImageIterator< typename Image::iterator, typename Mask::iterator, typename Variance::iterator > iterator
Definition: MaskedImage.h:539
lsst::afw::image::ImageOrigin
ImageOrigin
Definition: ImageBase.h:94
lsst::afw::image::imagesOverlap
bool imagesOverlap(ImageBase< T1 > const &image1, ImageBase< T2 > const &image2)
Return true if the pixels for two images or masks overlap in memory.
Definition: Image.cc:700
lsst::afw::fits::Fits::countHdus
int countHdus()
Return the number of HDUs in the file.
Definition: fits.cc:534
lsst::afw::image::MaskedImageFitsReader
A FITS reader class for MaskedImages and their components.
Definition: MaskedImageFitsReader.h:41
lsst::afw::image::MaskedImage::writeFits
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.
Definition: MaskedImage.cc:406
lsst::afw::image::Image< ImagePixelT >
lsst::afw::image::MaskedImage::assign
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.
Definition: MaskedImage.cc:228
lsst::afw::image::MaskedImage::col_begin
y_iterator col_begin(int x) const
Return an y_iterator to the start of the image.
Definition: MaskedImage.cc:625
lsst::afw::image::MaskedImage::getMask
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage's mask.
Definition: MaskedImage.h:1058
lsst::afw::image::MaskedImage::operator*=
MaskedImage & operator*=(ImagePixelT const rhs)
Definition: MaskedImage.cc:338
lsst::geom::Extent< int, 2 >
lsst::afw::image::MaskedImage::operator-=
MaskedImage & operator-=(ImagePixelT const rhs)
Subtract a scalar rhs from a MaskedImage.
Definition: MaskedImage.cc:279
Log.h
LSST DM logging module built on log4cxx.
lsst::afw::image::MaskedImage::Image
lsst::afw::image::Image< ImagePixelT > Image
Definition: MaskedImage.h:85
_c
double _c
Definition: MaskedImage.cc:297
lsst::afw::image::MaskedImage< Pixel, lsst::afw::image::MaskPixel, lsst::afw::image::VariancePixel >::x_iterator
MaskedImageIterator< typename Image::x_iterator, typename Mask::x_iterator, typename Variance::x_iterator > x_iterator
An iterator to a row of a MaskedImage.
Definition: MaskedImage.h:556
lsst::afw::image::pixel::Pixel::image
ImagePixelT image() const
Return the image part of a Pixel.
Definition: Pixel.h:217
bbox
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
exceptions.h
lsst::afw::image::MaskedImage::rend
reverse_iterator rend() const
Return a reverse_iterator to the end of the image.
Definition: MaskedImage.cc:595