LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
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 
30 #include "boost/format.hpp"
31 #include "lsst/log/Log.h"
32 #include "lsst/pex/exceptions.h"
33 
35 #include "lsst/afw/fits.h"
37 
38 namespace lsst {
39 namespace afw {
40 namespace image {
41 
42 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
44  MaskPlaneDict const& planeDict)
45  : _image(new Image(width, height)),
46  _mask(new Mask(width, height, planeDict)),
47  _variance(new Variance(width, height)) {
48  *_image = 0;
49  *_mask = 0x0;
50  *_variance = 0;
51 }
52 
53 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
55  MaskPlaneDict const& planeDict)
56  : _image(new Image(dimensions)),
57  _mask(new Mask(dimensions, planeDict)),
58  _variance(new Variance(dimensions)) {
59  *_image = 0;
60  *_mask = 0x0;
61  *_variance = 0;
62 }
63 
64 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
66  MaskPlaneDict const& planeDict)
67  : _image(new Image(bbox)), _mask(new Mask(bbox, planeDict)), _variance(new Variance(bbox)) {
68  *_image = 0;
69  *_mask = 0x0;
70  *_variance = 0;
71 }
72 
73 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
75  std::string const& fileName, std::shared_ptr<daf::base::PropertySet> metadata,
76  lsst::geom::Box2I const& bbox, ImageOrigin origin, bool conformMasks, bool needAllHdus,
79  std::shared_ptr<daf::base::PropertySet> varianceMetadata, bool allowUnsafe)
80  : _image(), _mask(), _variance() {
81  MaskedImageFitsReader reader(fileName);
82  *this = reader.read<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks, needAllHdus,
83  allowUnsafe);
84  if (metadata) {
85  metadata->combine(reader.readPrimaryMetadata());
86  }
87  if (imageMetadata) {
88  imageMetadata->combine(reader.readImageMetadata());
89  }
90  if (maskMetadata) {
91  maskMetadata->combine(reader.readMaskMetadata());
92  }
93  if (varianceMetadata) {
94  varianceMetadata->combine(reader.readVarianceMetadata());
95  }
96 }
97 
98 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
101  lsst::geom::Box2I const& bbox, ImageOrigin origin, bool conformMasks, bool needAllHdus,
104  std::shared_ptr<daf::base::PropertySet> varianceMetadata, bool allowUnsafe)
105  : _image(), _mask(), _variance() {
106  MaskedImageFitsReader reader(manager);
107  *this = reader.read<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks, needAllHdus,
108  allowUnsafe);
109  if (metadata) {
110  metadata->combine(reader.readPrimaryMetadata());
111  }
112  if (imageMetadata) {
113  imageMetadata->combine(reader.readImageMetadata());
114  }
115  if (maskMetadata) {
116  maskMetadata->combine(reader.readMaskMetadata());
117  }
118  if (varianceMetadata) {
119  varianceMetadata->combine(reader.readVarianceMetadata());
120  }
121 }
122 
123 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
126  ImageOrigin origin, bool conformMasks, bool needAllHdus,
129  std::shared_ptr<daf::base::PropertySet> varianceMetadata, bool allowUnsafe)
130  : _image(), _mask(), _variance() {
131  MaskedImageFitsReader reader(&fitsFile);
132  *this = reader.read<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks, needAllHdus,
133  allowUnsafe);
134  if (metadata) {
135  metadata->combine(reader.readPrimaryMetadata());
136  }
137  if (imageMetadata) {
138  imageMetadata->combine(reader.readImageMetadata());
139  }
140  if (maskMetadata) {
141  maskMetadata->combine(reader.readMaskMetadata());
142  }
143  if (varianceMetadata) {
144  varianceMetadata->combine(reader.readVarianceMetadata());
145  }
146 }
147 
148 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
151  : _image(image), _mask(mask), _variance(variance) {
152  conformSizes();
153 }
154 
155 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
157  : _image(rhs._image), _mask(rhs._mask), _variance(rhs._variance) {
158  if (deep) {
159  _image = std::shared_ptr<Image>(new Image(*rhs.getImage(), deep));
160  _mask = std::shared_ptr<Mask>(new Mask(*rhs.getMask(), deep));
161  _variance = std::shared_ptr<Variance>(new Variance(*rhs.getVariance(), deep));
162  }
163  conformSizes();
164 }
165 
166 // Delegate to copy-constructor for backwards compatibility
167 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
169  : MaskedImage(rhs, false) {}
170 
171 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
173  const lsst::geom::Box2I& bbox,
174  ImageOrigin const origin, bool deep
175 
176  )
177  : _image(new Image(*rhs.getImage(), bbox, origin, deep)),
178  _mask(rhs._mask ? new Mask(*rhs.getMask(), bbox, origin, deep) : static_cast<Mask*>(nullptr)),
179  _variance(rhs._variance ? new Variance(*rhs.getVariance(), bbox, origin, deep)
180  : static_cast<Variance*>(nullptr)) {
181  conformSizes();
182 }
183 
184 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
186 operator=(MaskedImage const& rhs) = default;
187 
188 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
190 operator=(MaskedImage&& rhs) = default;
191 
192 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
194  using std::swap; // See Meyers, Effective C++, Item 25
195 
196  _image.swap(rhs._image);
197  _mask.swap(rhs._mask);
198  _variance.swap(rhs._variance);
199 }
200 
201 // Operators
202 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
204 operator=(MaskedImage::Pixel const& rhs) {
205  *_image = rhs.image();
206  *_mask = rhs.mask();
207  *_variance = rhs.variance();
208 
209  return *this;
210 }
211 
212 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
215  *_image = rhs.image();
216  *_mask = rhs.mask();
217  *_variance = rhs.variance();
218 
219  return *this;
220 }
221 
222 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
224  lsst::geom::Box2I const& bbox,
225  ImageOrigin origin) {
226  _image->assign(*rhs.getImage(), bbox, origin);
227  _mask->assign(*rhs.getMask(), bbox, origin);
228  _variance->assign(*rhs.getVariance(), bbox, origin);
229 }
230 
231 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
233 operator+=(MaskedImage const& rhs) {
234  *_image += *rhs.getImage();
235  *_mask |= *rhs.getMask();
236  *_variance += *rhs.getVariance();
237  return *this;
238 }
239 
240 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
242  MaskedImage const& rhs) {
243  (*_image).scaledPlus(c, *rhs.getImage());
244  *_mask |= *rhs.getMask();
245  (*_variance).scaledPlus(c * c, *rhs.getVariance());
246 }
247 
248 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
250 operator+=(ImagePixelT const rhs) {
251  *_image += rhs;
252  return *this;
253 }
254 
255 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
257 operator-=(MaskedImage const& rhs) {
258  *_image -= *rhs.getImage();
259  *_mask |= *rhs.getMask();
260  *_variance += *rhs.getVariance();
261  return *this;
262 }
263 
264 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
266  MaskedImage const& rhs) {
267  (*_image).scaledMinus(c, *rhs.getImage());
268  *_mask |= *rhs.getMask();
269  (*_variance).scaledPlus(c * c, *rhs.getVariance());
270 }
271 
272 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
274 operator-=(ImagePixelT const rhs) {
275  *_image -= rhs;
276  return *this;
277 }
278 
279 namespace {
281 template <typename ImagePixelT, typename VariancePixelT>
282 struct productVariance {
283  double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
284  return lhs * lhs * varRhs + rhs * rhs * varLhs;
285  }
286 };
287 
290 template <typename ImagePixelT, typename VariancePixelT>
291 struct scaledProductVariance {
292  double _c;
293  scaledProductVariance(double const c) : _c(c) {}
294  double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
295  return _c * _c * (lhs * lhs * varRhs + rhs * rhs * varLhs);
296  }
297 };
298 } // namespace
299 
300 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
302 operator*=(MaskedImage const& rhs) {
303  // Must do variance before we modify the image values
304  if (_image->getDimensions() != rhs._image->getDimensions()) {
306  boost::str(boost::format("Images are of different size, %dx%d v %dx%d") %
307  _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
308  }
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  if (_image->getDimensions() != rhs._image->getDimensions()) {
327  boost::str(boost::format("Images are of different size, %dx%d v %dx%d") %
328  _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
329  }
330  transform_pixels(_image->_getRawView(), // lhs
331  rhs._image->_getRawView(), // rhs,
332  _variance->_getRawView(), // Var(lhs),
333  rhs._variance->_getRawView(), // Var(rhs)
334  _variance->_getRawView(), // result
335  scaledProductVariance<ImagePixelT, VariancePixelT>(c));
336 
337  (*_image).scaledMultiplies(c, *rhs.getImage());
338  *_mask |= *rhs.getMask();
339 }
340 
341 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
343 operator*=(ImagePixelT const rhs) {
344  *_image *= rhs;
345  *_variance *= rhs * rhs;
346  return *this;
347 }
348 
349 namespace {
351 template <typename ImagePixelT, typename VariancePixelT>
352 struct quotientVariance {
353  double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
354  ImagePixelT const rhs2 = rhs * rhs;
355  return (lhs * lhs * varRhs + rhs2 * varLhs) / (rhs2 * rhs2);
356  }
357 };
360 template <typename ImagePixelT, typename VariancePixelT>
361 struct scaledQuotientVariance {
362  double _c;
363  scaledQuotientVariance(double c) : _c(c) {}
364  double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
365  ImagePixelT const rhs2 = rhs * rhs;
366  return (lhs * lhs * varRhs + rhs2 * varLhs) / (_c * _c * rhs2 * rhs2);
367  }
368 };
369 } // namespace
370 
371 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
373 operator/=(MaskedImage const& rhs) {
374  // Must do variance before we modify the image values
375  if (_image->getDimensions() != rhs._image->getDimensions()) {
377  boost::str(boost::format("Images are of different size, %dx%d v %dx%d") %
378  _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
379  }
380  transform_pixels(_image->_getRawView(), // lhs
381  rhs._image->_getRawView(), // rhs,
382  _variance->_getRawView(), // Var(lhs),
383  rhs._variance->_getRawView(), // Var(rhs)
384  _variance->_getRawView(), // result
385  quotientVariance<ImagePixelT, VariancePixelT>());
386 
387  *_image /= *rhs.getImage();
388  *_mask |= *rhs.getMask();
389  return *this;
390 }
391 
392 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
394  MaskedImage const& rhs) {
395  // Must do variance before we modify the image values
396  if (_image->getDimensions() != rhs._image->getDimensions()) {
398  str(boost::format("Images are of different size, %dx%d v %dx%d") %
399  _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
400  }
401  transform_pixels(_image->_getRawView(), // lhs
402  rhs._image->_getRawView(), // rhs,
403  _variance->_getRawView(), // Var(lhs),
404  rhs._variance->_getRawView(), // Var(rhs)
405  _variance->_getRawView(), // result
406  scaledQuotientVariance<ImagePixelT, VariancePixelT>(c));
407 
408  (*_image).scaledDivides(c, *rhs.getImage());
409  *_mask |= *rhs._mask;
410 }
411 
412 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
414 operator/=(ImagePixelT const rhs) {
415  *_image /= rhs;
416  *_variance /= rhs * rhs;
417  return *this;
418 }
419 
420 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
425  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
426  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
427  writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
428 }
429 
430 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
435  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
436  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
437  writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
438 }
439 
440 namespace {
441 
442 void processPlaneMetadata(std::shared_ptr<daf::base::PropertySet const> metadata,
443  std::shared_ptr<daf::base::PropertySet>& hdr, char const* exttype) {
444  if (metadata) {
445  hdr = metadata->deepCopy();
446  } else {
447  hdr.reset(new daf::base::PropertyList());
448  }
449  hdr->set("INHERIT", true);
450  hdr->set("EXTTYPE", exttype);
451  hdr->set("EXTNAME", exttype);
452 }
453 
454 } // namespace
455 
456 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
461  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
462  writeFits(fitsfile, fits::ImageWriteOptions(*_image), fits::ImageWriteOptions(*_mask),
463  fits::ImageWriteOptions(*_variance), metadata, imageMetadata, maskMetadata, varianceMetadata);
464 }
465 
466 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
468  std::string const& fileName, fits::ImageWriteOptions const& imageOptions,
469  fits::ImageWriteOptions const& maskOptions, fits::ImageWriteOptions const& varianceOptions,
473  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
474  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
475  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
476  varianceMetadata);
477 }
478 
479 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
481  fits::MemFileManager& manager, fits::ImageWriteOptions const& imageOptions,
482  fits::ImageWriteOptions const& maskOptions, fits::ImageWriteOptions const& varianceOptions,
486  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
487  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
488  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
489  varianceMetadata);
490 }
491 
492 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
494  fits::Fits& fitsfile, fits::ImageWriteOptions const& imageOptions,
495  fits::ImageWriteOptions const& maskOptions, fits::ImageWriteOptions const& varianceOptions,
499  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
501  if (metadata) {
502  header = metadata->deepCopy();
503  } else {
504  header = std::make_shared<daf::base::PropertyList>();
505  }
506 
507  if (fitsfile.countHdus() != 0) {
509  "MaskedImage::writeFits can only write to an empty file");
510  }
511  if (fitsfile.getHdu() < 1) {
512  // Don't ever write images to primary; instead we make an empty primary.
513  fitsfile.createEmpty();
514  } else {
515  fitsfile.setHdu(0);
516  }
517  fitsfile.writeMetadata(*header);
518 
519  processPlaneMetadata(imageMetadata, header, "IMAGE");
520  _image->writeFits(fitsfile, imageOptions, header, _mask);
521 
522  processPlaneMetadata(maskMetadata, header, "MASK");
523  _mask->writeFits(fitsfile, maskOptions, header);
524 
525  processPlaneMetadata(varianceMetadata, header, "VARIANCE");
526  _variance->writeFits(fitsfile, varianceOptions, header, _mask);
527 }
528 
529 // private function conformSizes() ensures that the Mask and Variance have the same dimensions
530 // as Image. If Mask and/or Variance have non-zero dimensions that conflict with the size of Image,
531 // a lsst::pex::exceptions::LengthError is thrown.
532 
533 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
535  if (!_mask || _mask->getWidth() == 0 || _mask->getHeight() == 0) {
536  _mask = MaskPtr(new Mask(_image->getBBox()));
537  *_mask = 0;
538  } else {
539  if (_mask->getDimensions() != _image->getDimensions()) {
540  throw LSST_EXCEPT(
542  (boost::format("Dimension mismatch: Image %dx%d v. Mask %dx%d") % _image->getWidth() %
543  _image->getHeight() % _mask->getWidth() % _mask->getHeight())
544  .str());
545  }
546  }
547 
548  if (!_variance || _variance->getWidth() == 0 || _variance->getHeight() == 0) {
549  _variance = VariancePtr(new Variance(_image->getBBox()));
550  *_variance = 0;
551  } else {
552  if (_variance->getDimensions() != _image->getDimensions()) {
553  throw LSST_EXCEPT(
554  pex::exceptions::LengthError,
555  (boost::format("Dimension mismatch: Image %dx%d v. Variance %dx%d") % _image->getWidth() %
556  _image->getHeight() % _variance->getWidth() % _variance->getHeight())
557  .str());
558  }
559  }
560 }
561 
562 //
563 // Iterators and locators
564 //
565 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
568 #if 0 // this doesn't compile; why?
569  return iterator(_image->begin(), _mask->begin(), _variance->begin());
570 #else
571  typename Image::iterator imageBegin = _image->begin();
572  typename Mask::iterator maskBegin = _mask->begin();
573  typename Variance::iterator varianceBegin = _variance->begin();
574 
575  return iterator(imageBegin, maskBegin, varianceBegin);
576 #endif
577 }
578 
579 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
582  typename Image::iterator imageEnd = getImage()->end();
583  typename Mask::iterator maskEnd = getMask()->end();
584  typename Variance::iterator varianceEnd = getVariance()->end();
585 
586  return iterator(imageEnd, maskEnd, varianceEnd);
587 }
588 
589 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
592  typename Image::iterator imageEnd = getImage()->at(x, y);
593  typename Mask::iterator maskEnd = getMask()->at(x, y);
594  typename Variance::iterator varianceEnd = getVariance()->at(x, y);
595 
596  return iterator(imageEnd, maskEnd, varianceEnd);
597 }
598 
599 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
602  typename Image::reverse_iterator imageBegin = _image->rbegin();
603  typename Mask::reverse_iterator maskBegin = _mask->rbegin();
604  typename Variance::reverse_iterator varianceBegin = _variance->rbegin();
605 
606  return reverse_iterator(imageBegin, maskBegin, varianceBegin);
607 }
608 
609 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
612  typename Image::reverse_iterator imageEnd = getImage()->rend();
613  typename Mask::reverse_iterator maskEnd = getMask()->rend();
614  typename Variance::reverse_iterator varianceEnd = getVariance()->rend();
615 
616  return reverse_iterator(imageEnd, maskEnd, varianceEnd);
617 }
618 
619 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
622  typename Image::x_iterator imageBegin = _image->row_begin(y);
623  typename Mask::x_iterator maskBegin = _mask->row_begin(y);
624  typename Variance::x_iterator varianceBegin = _variance->row_begin(y);
625 
626  return x_iterator(imageBegin, maskBegin, varianceBegin);
627 }
628 
629 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
632  typename Image::x_iterator imageEnd = getImage()->row_end(y);
633  typename Mask::x_iterator maskEnd = getMask()->row_end(y);
634  typename Variance::x_iterator varianceEnd = getVariance()->row_end(y);
635 
636  return x_iterator(imageEnd, maskEnd, varianceEnd);
637 }
638 
639 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
642  typename Image::y_iterator imageBegin = _image->col_begin(x);
643  typename Mask::y_iterator maskBegin = _mask->col_begin(x);
644  typename Variance::y_iterator varianceBegin = _variance->col_begin(x);
645 
646  return y_iterator(imageBegin, maskBegin, varianceBegin);
647 }
648 
649 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
652  typename Image::y_iterator imageEnd = getImage()->col_end(x);
653  typename Mask::y_iterator maskEnd = getMask()->col_end(x);
654  typename Variance::y_iterator varianceEnd = getVariance()->col_end(x);
655 
656  return y_iterator(imageEnd, maskEnd, varianceEnd);
657 }
658 
659 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
662  typename Image::fast_iterator imageBegin = _image->begin(contiguous);
663  typename Mask::fast_iterator maskBegin = _mask->begin(contiguous);
664  typename Variance::fast_iterator varianceBegin = _variance->begin(contiguous);
665 
666  return fast_iterator(imageBegin, maskBegin, varianceBegin);
667 }
668 
669 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
672  typename Image::fast_iterator imageEnd = getImage()->end(contiguous);
673  typename Mask::fast_iterator maskEnd = getMask()->end(contiguous);
674  typename Variance::fast_iterator varianceEnd = getVariance()->end(contiguous);
675 
676  return fast_iterator(imageEnd, maskEnd, varianceEnd);
677 }
678 
679 template <typename ImagePixelT1, typename ImagePixelT2>
682  return imagesOverlap(*image1.getImage(), *image2.getImage()) ||
683  imagesOverlap(*image1.getVariance(), *image2.getVariance()) ||
684  imagesOverlap(*image1.getMask(), *image2.getMask());
685 }
686 
687 //
688 // Explicit instantiations
689 //
690 #define INSTANTIATE2(ImagePixelT1, ImagePixelT2) \
691  template bool imagesOverlap<ImagePixelT1, ImagePixelT2>(MaskedImage<ImagePixelT1> const&, \
692  MaskedImage<ImagePixelT2> const&);
693 
694 template class MaskedImage<std::uint16_t>;
695 template class MaskedImage<int>;
696 template class MaskedImage<float>;
697 template class MaskedImage<double>;
698 template class MaskedImage<std::uint64_t>;
699 
705 
707 INSTANTIATE2(int, int);
708 INSTANTIATE2(int, float);
709 INSTANTIATE2(int, double);
711 
713 INSTANTIATE2(float, int);
714 INSTANTIATE2(float, float);
715 INSTANTIATE2(float, double);
717 
719 INSTANTIATE2(double, int);
720 INSTANTIATE2(double, float);
721 INSTANTIATE2(double, double);
723 
729 
730 } // namespace image
731 } // namespace afw
732 } // namespace lsst
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
double x
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:48
afw::table::Key< afw::table::Array< MaskPixelT > > mask
afw::table::Key< afw::table::Array< VariancePixelT > > variance
LSST DM logging module built on log4cxx.
double _c
Definition: MaskedImage.cc:292
#define INSTANTIATE2(ImagePixelT1, ImagePixelT2)
Definition: MaskedImage.cc:690
int y
Definition: SpanSet.cc:48
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
void createEmpty()
Create an empty image HDU with NAXIS=0 at the end of the file.
Definition: fits.cc:1240
void setHdu(int hdu, bool relative=false)
Set the current HDU.
Definition: fits.cc:513
int countHdus()
Return the number of HDUs in the file.
Definition: fits.cc:534
int getHdu()
Return the current HDU (0-indexed; 0 is the Primary HDU).
Definition: fits.cc:507
void writeMetadata(daf::base::PropertySet const &metadata)
Read a FITS header into a PropertySet or PropertyList.
Definition: fits.cc:1095
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
typename _view_t::iterator iterator
An STL compliant iterator.
Definition: ImageBase.h:125
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Definition: ImageBase.h:137
typename _view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
Definition: ImageBase.h:129
typename _view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:133
typename _view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
Definition: ImageBase.h:143
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:51
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
A FITS reader class for MaskedImages and their components.
std::shared_ptr< daf::base::PropertyList > readImageMetadata()
Read the FITS header of one of the HDUs.
std::shared_ptr< daf::base::PropertyList > readPrimaryMetadata()
Read the FITS header of one of the HDUs.
std::shared_ptr< daf::base::PropertyList > readMaskMetadata()
Read the FITS header of one of the HDUs.
std::shared_ptr< daf::base::PropertyList > readVarianceMetadata()
Read the FITS header of one of the HDUs.
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.
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
void scaledPlus(double const c, MaskedImage const &rhs)
Add a scaled MaskedImage c*rhs to a MaskedImage.
Definition: MaskedImage.cc:241
lsst::afw::image::Image< VariancePixelT > Variance
Definition: MaskedImage.h:84
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:421
MaskedImage(unsigned int width, unsigned int height, MaskPlaneDict const &planeDict=MaskPlaneDict())
Construct from a supplied dimensions.
Definition: MaskedImage.cc:43
void scaledDivides(double const c, MaskedImage const &rhs)
Definition: MaskedImage.cc:393
typename Mask< MaskPixelT >::MaskPlaneDict MaskPlaneDict
The Mask's MaskPlaneDict.
Definition: MaskedImage.h:82
iterator begin() const
Return an iterator to the start of the image.
Definition: MaskedImage.cc:567
reverse_iterator rbegin() const
Return a reverse_iterator to the start of the image.
Definition: MaskedImage.cc:601
lsst::afw::image::Mask< MaskPixelT > Mask
Definition: MaskedImage.h:86
y_iterator col_end(int x) const
Return an y_iterator to the end of the image.
Definition: MaskedImage.cc:651
void scaledMinus(double const c, MaskedImage const &rhs)
Subtract a scaled MaskedImage c*rhs from a MaskedImage.
Definition: MaskedImage.cc:265
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Definition: MaskedImage.h:546
MaskedImageIterator< typename Image::iterator, typename Mask::iterator, typename Variance::iterator > iterator
Definition: MaskedImage.h:529
MaskedImage & operator-=(ImagePixelT const rhs)
Subtract a scalar rhs from a MaskedImage.
Definition: MaskedImage.cc:274
x_iterator row_end(int y) const
Return an x_iterator to the end of the image.
Definition: MaskedImage.cc:631
void scaledMultiplies(double const c, MaskedImage const &rhs)
Definition: MaskedImage.cc:322
iterator end() const
Return an iterator to the end of the image.
Definition: MaskedImage.cc:581
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:223
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
Definition: MaskedImage.h:1051
MaskedImage & operator=(MaskedImage const &rhs)
Make the lhs use the rhs's pixels.
lsst::afw::image::Image< ImagePixelT > Image
Definition: MaskedImage.h:85
x_iterator row_begin(int y) const
Return an x_iterator to the start of the image.
Definition: MaskedImage.cc:621
void swap(MaskedImage &rhs)
Definition: MaskedImage.cc:193
y_iterator col_begin(int x) const
Return an y_iterator to the start of the image.
Definition: MaskedImage.cc:641
reverse_iterator rend() const
Return a reverse_iterator to the end of the image.
Definition: MaskedImage.cc:611
MaskedImage & operator+=(ImagePixelT const rhs)
Add a scalar rhs to a MaskedImage.
Definition: MaskedImage.cc:250
MaskedImage & operator*=(ImagePixelT const rhs)
Definition: MaskedImage.cc:343
iterator at(int const x, int const y) const
Return an iterator at the point (x, y)
Definition: MaskedImage.cc:591
MaskedImage & operator/=(ImagePixelT const rhs)
Definition: MaskedImage.cc:414
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage's mask.
Definition: MaskedImage.h:1030
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
Definition: MaskedImage.h:1018
A pixel of a MaskedImage.
Definition: Pixel.h:148
MaskPixelT mask() const
Return the mask part of a Pixel.
Definition: Pixel.h:219
ImagePixelT image() const
Return the image part of a Pixel.
Definition: Pixel.h:217
VariancePixelT variance() const
Return the variance part of a Pixel.
Definition: Pixel.h:221
A single pixel of the same type as a MaskedImage.
Definition: Pixel.h:73
VariancePixelT variance() const
Definition: Pixel.h:96
MaskPixelT mask() const
Definition: Pixel.h:95
ImagePixelT image() const
Definition: Pixel.h:94
Class for storing ordered metadata with comments.
Definition: PropertyList.h:68
An integer coordinate rectangle.
Definition: Box.h:55
Reports attempts to exceed implementation-defined length limits for some classes.
Definition: Runtime.h:76
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
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:692
def writeFits(filename, stamps, metadata, type_name, write_mask, write_variance, write_archive=False)
Definition: stamps.py:42
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.
T reset(T... args)
Options for writing an image to FITS.
Definition: fits.h:219
T swap(T... args)