LSST Applications  21.0.0+04719a4bac,21.0.0-1-ga51b5d4+4b710797af,21.0.0-1-gfc31b0f+3b24369756,21.0.0-10-g2408eff+50e97f2f47,21.0.0-10-g560fb7b+0803ad37c5,21.0.0-10-g5daeb2b+f9b8dc6d5a,21.0.0-10-g8d1d15d+77a6b82ebf,21.0.0-10-gcf60f90+c961be884d,21.0.0-11-g25eff31+7692554667,21.0.0-17-g6590b197+a14a01c114,21.0.0-2-g103fe59+b79afc2051,21.0.0-2-g1367e85+1003a3501c,21.0.0-2-g45278ab+04719a4bac,21.0.0-2-g5242d73+1003a3501c,21.0.0-2-g7f82c8f+c2a1919b98,21.0.0-2-g8f08a60+fd0b970de5,21.0.0-2-ga326454+c2a1919b98,21.0.0-2-gde069b7+ca45a81b40,21.0.0-2-gecfae73+afcaaec585,21.0.0-2-gfc62afb+1003a3501c,21.0.0-21-g5d80ea29e+5e3c9a3766,21.0.0-3-g357aad2+c67f36f878,21.0.0-3-g4be5c26+1003a3501c,21.0.0-3-g65f322c+02b1f88459,21.0.0-3-g7d9da8d+3b24369756,21.0.0-3-ge02ed75+a423c2ae7a,21.0.0-4-g591bb35+a423c2ae7a,21.0.0-4-g65b4814+0803ad37c5,21.0.0-4-g88306b8+199c7497e5,21.0.0-4-gccdca77+a631590478,21.0.0-4-ge8a399c+b923ff878e,21.0.0-5-gd00fb1e+d8b1e95daa,21.0.0-53-ge728e5d5+3cb64fea8e,21.0.0-6-g2d4f3f3+04719a4bac,21.0.0-7-g04766d7+8d320c19d5,21.0.0-7-g98eecf7+205433fbda,21.0.0-9-g39e06b5+a423c2ae7a,master-gac4afde19b+a423c2ae7a,w.2021.11
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 #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 "boost/format.hpp"
37 #include "lsst/log/Log.h"
38 #include "lsst/pex/exceptions.h"
39 
41 #include "lsst/afw/fits.h"
43 
44 namespace lsst {
45 namespace afw {
46 namespace image {
47 
48 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
50  MaskPlaneDict const& planeDict)
51  : _image(new Image(width, height)),
52  _mask(new Mask(width, height, planeDict)),
53  _variance(new Variance(width, height)) {
54  *_image = 0;
55  *_mask = 0x0;
56  *_variance = 0;
57 }
58 
59 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
61  MaskPlaneDict const& planeDict)
62  : _image(new Image(dimensions)),
63  _mask(new Mask(dimensions, planeDict)),
64  _variance(new Variance(dimensions)) {
65  *_image = 0;
66  *_mask = 0x0;
67  *_variance = 0;
68 }
69 
70 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
72  MaskPlaneDict const& planeDict)
73  : _image(new Image(bbox)), _mask(new Mask(bbox, planeDict)), _variance(new Variance(bbox)) {
74  *_image = 0;
75  *_mask = 0x0;
76  *_variance = 0;
77 }
78 
79 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
81  std::string const& fileName, std::shared_ptr<daf::base::PropertySet> metadata,
82  lsst::geom::Box2I const& bbox, ImageOrigin origin, bool conformMasks, bool needAllHdus,
85  std::shared_ptr<daf::base::PropertySet> varianceMetadata, bool allowUnsafe)
86  : _image(), _mask(), _variance() {
87  MaskedImageFitsReader reader(fileName);
88  *this = reader.read<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks, needAllHdus,
89  allowUnsafe);
90  if (metadata) {
91  metadata->combine(reader.readPrimaryMetadata());
92  }
93  if (imageMetadata) {
94  imageMetadata->combine(reader.readImageMetadata());
95  }
96  if (maskMetadata) {
97  maskMetadata->combine(reader.readMaskMetadata());
98  }
99  if (varianceMetadata) {
100  varianceMetadata->combine(reader.readVarianceMetadata());
101  }
102 }
103 
104 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
107  lsst::geom::Box2I const& bbox, ImageOrigin origin, bool conformMasks, bool needAllHdus,
110  std::shared_ptr<daf::base::PropertySet> varianceMetadata, bool allowUnsafe)
111  : _image(), _mask(), _variance() {
112  MaskedImageFitsReader reader(manager);
113  *this = reader.read<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks, needAllHdus,
114  allowUnsafe);
115  if (metadata) {
116  metadata->combine(reader.readPrimaryMetadata());
117  }
118  if (imageMetadata) {
119  imageMetadata->combine(reader.readImageMetadata());
120  }
121  if (maskMetadata) {
122  maskMetadata->combine(reader.readMaskMetadata());
123  }
124  if (varianceMetadata) {
125  varianceMetadata->combine(reader.readVarianceMetadata());
126  }
127 }
128 
129 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
132  ImageOrigin origin, bool conformMasks, bool needAllHdus,
135  std::shared_ptr<daf::base::PropertySet> varianceMetadata, bool allowUnsafe)
136  : _image(), _mask(), _variance() {
137  MaskedImageFitsReader reader(&fitsFile);
138  *this = reader.read<ImagePixelT, MaskPixelT, VariancePixelT>(bbox, origin, conformMasks, needAllHdus,
139  allowUnsafe);
140  if (metadata) {
141  metadata->combine(reader.readPrimaryMetadata());
142  }
143  if (imageMetadata) {
144  imageMetadata->combine(reader.readImageMetadata());
145  }
146  if (maskMetadata) {
147  maskMetadata->combine(reader.readMaskMetadata());
148  }
149  if (varianceMetadata) {
150  varianceMetadata->combine(reader.readVarianceMetadata());
151  }
152 }
153 
154 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
157  : _image(image), _mask(mask), _variance(variance) {
158  conformSizes();
159 }
160 
161 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
163  : _image(rhs._image), _mask(rhs._mask), _variance(rhs._variance) {
164  if (deep) {
165  _image = std::shared_ptr<Image>(new Image(*rhs.getImage(), deep));
166  _mask = std::shared_ptr<Mask>(new Mask(*rhs.getMask(), deep));
167  _variance = std::shared_ptr<Variance>(new Variance(*rhs.getVariance(), deep));
168  }
169  conformSizes();
170 }
171 
172 // Delegate to copy-constructor for backwards compatibility
173 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
175  : MaskedImage(rhs, false) {}
176 
177 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
179  const lsst::geom::Box2I& bbox,
180  ImageOrigin const origin, bool deep
181 
182  )
183  : _image(new Image(*rhs.getImage(), bbox, origin, deep)),
184  _mask(rhs._mask ? new Mask(*rhs.getMask(), bbox, origin, deep) : static_cast<Mask*>(NULL)),
185  _variance(rhs._variance ? new Variance(*rhs.getVariance(), bbox, origin, deep)
186  : static_cast<Variance*>(NULL)) {
187  conformSizes();
188 }
189 
190 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
192 operator=(MaskedImage const& rhs) = default;
193 
194 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
196 operator=(MaskedImage&& rhs) = default;
197 
198 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
200  using std::swap; // See Meyers, Effective C++, Item 25
201 
202  _image.swap(rhs._image);
203  _mask.swap(rhs._mask);
204  _variance.swap(rhs._variance);
205 }
206 
207 // Operators
208 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
210 operator=(MaskedImage::Pixel const& rhs) {
211  *_image = rhs.image();
212  *_mask = rhs.mask();
213  *_variance = rhs.variance();
214 
215  return *this;
216 }
217 
218 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
221  *_image = rhs.image();
222  *_mask = rhs.mask();
223  *_variance = rhs.variance();
224 
225  return *this;
226 }
227 
228 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
230  lsst::geom::Box2I const& bbox,
231  ImageOrigin origin) {
232  _image->assign(*rhs.getImage(), bbox, origin);
233  _mask->assign(*rhs.getMask(), bbox, origin);
234  _variance->assign(*rhs.getVariance(), bbox, origin);
235 }
236 
237 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
239 operator+=(MaskedImage const& rhs) {
240  *_image += *rhs.getImage();
241  *_mask |= *rhs.getMask();
242  *_variance += *rhs.getVariance();
243  return *this;
244 }
245 
246 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
248  MaskedImage const& rhs) {
249  (*_image).scaledPlus(c, *rhs.getImage());
250  *_mask |= *rhs.getMask();
251  (*_variance).scaledPlus(c * c, *rhs.getVariance());
252 }
253 
254 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
256 operator+=(ImagePixelT const rhs) {
257  *_image += rhs;
258  return *this;
259 }
260 
261 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
263 operator-=(MaskedImage const& rhs) {
264  *_image -= *rhs.getImage();
265  *_mask |= *rhs.getMask();
266  *_variance += *rhs.getVariance();
267  return *this;
268 }
269 
270 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
272  MaskedImage const& rhs) {
273  (*_image).scaledMinus(c, *rhs.getImage());
274  *_mask |= *rhs.getMask();
275  (*_variance).scaledPlus(c * c, *rhs.getVariance());
276 }
277 
278 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
280 operator-=(ImagePixelT const rhs) {
281  *_image -= rhs;
282  return *this;
283 }
284 
285 namespace {
287 template <typename ImagePixelT, typename VariancePixelT>
288 struct productVariance {
289  double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
290  return lhs * lhs * varRhs + rhs * rhs * varLhs;
291  }
292 };
293 
296 template <typename ImagePixelT, typename VariancePixelT>
297 struct scaledProductVariance {
298  double _c;
299  scaledProductVariance(double const c) : _c(c) {}
300  double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
301  return _c * _c * (lhs * lhs * varRhs + rhs * rhs * varLhs);
302  }
303 };
304 } // namespace
305 
306 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
308 operator*=(MaskedImage const& rhs) {
309  // Must do variance before we modify the image values
310  if (_image->getDimensions() != rhs._image->getDimensions()) {
312  boost::str(boost::format("Images are of different size, %dx%d v %dx%d") %
313  _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
314  }
315  transform_pixels(_image->_getRawView(), // lhs
316  rhs._image->_getRawView(), // rhs,
317  _variance->_getRawView(), // Var(lhs),
318  rhs._variance->_getRawView(), // Var(rhs)
319  _variance->_getRawView(), // result
320  productVariance<ImagePixelT, VariancePixelT>());
321 
322  *_image *= *rhs.getImage();
323  *_mask |= *rhs.getMask();
324  return *this;
325 }
326 
327 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
329  MaskedImage const& rhs) {
330  // Must do variance before we modify the image values
331  if (_image->getDimensions() != rhs._image->getDimensions()) {
333  boost::str(boost::format("Images are of different size, %dx%d v %dx%d") %
334  _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
335  }
336  transform_pixels(_image->_getRawView(), // lhs
337  rhs._image->_getRawView(), // rhs,
338  _variance->_getRawView(), // Var(lhs),
339  rhs._variance->_getRawView(), // Var(rhs)
340  _variance->_getRawView(), // result
341  scaledProductVariance<ImagePixelT, VariancePixelT>(c));
342 
343  (*_image).scaledMultiplies(c, *rhs.getImage());
344  *_mask |= *rhs.getMask();
345 }
346 
347 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
349 operator*=(ImagePixelT const rhs) {
350  *_image *= rhs;
351  *_variance *= rhs * rhs;
352  return *this;
353 }
354 
355 namespace {
357 template <typename ImagePixelT, typename VariancePixelT>
358 struct quotientVariance {
359  double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
360  ImagePixelT const rhs2 = rhs * rhs;
361  return (lhs * lhs * varRhs + rhs2 * varLhs) / (rhs2 * rhs2);
362  }
363 };
366 template <typename ImagePixelT, typename VariancePixelT>
367 struct scaledQuotientVariance {
368  double _c;
369  scaledQuotientVariance(double c) : _c(c) {}
370  double operator()(ImagePixelT lhs, ImagePixelT rhs, VariancePixelT varLhs, VariancePixelT varRhs) {
371  ImagePixelT const rhs2 = rhs * rhs;
372  return (lhs * lhs * varRhs + rhs2 * varLhs) / (_c * _c * rhs2 * rhs2);
373  }
374 };
375 } // namespace
376 
377 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
379 operator/=(MaskedImage const& rhs) {
380  // Must do variance before we modify the image values
381  if (_image->getDimensions() != rhs._image->getDimensions()) {
383  boost::str(boost::format("Images are of different size, %dx%d v %dx%d") %
384  _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
385  }
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  quotientVariance<ImagePixelT, VariancePixelT>());
392 
393  *_image /= *rhs.getImage();
394  *_mask |= *rhs.getMask();
395  return *this;
396 }
397 
398 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
400  MaskedImage const& rhs) {
401  // Must do variance before we modify the image values
402  if (_image->getDimensions() != rhs._image->getDimensions()) {
404  str(boost::format("Images are of different size, %dx%d v %dx%d") %
405  _image->getWidth() % _image->getHeight() % rhs._image->getWidth() % rhs._image->getHeight()));
406  }
407  transform_pixels(_image->_getRawView(), // lhs
408  rhs._image->_getRawView(), // rhs,
409  _variance->_getRawView(), // Var(lhs),
410  rhs._variance->_getRawView(), // Var(rhs)
411  _variance->_getRawView(), // result
412  scaledQuotientVariance<ImagePixelT, VariancePixelT>(c));
413 
414  (*_image).scaledDivides(c, *rhs.getImage());
415  *_mask |= *rhs._mask;
416 }
417 
418 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
420 operator/=(ImagePixelT const rhs) {
421  *_image /= rhs;
422  *_variance /= rhs * rhs;
423  return *this;
424 }
425 
426 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
431  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
432  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
433  writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
434 }
435 
436 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
441  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
442  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
443  writeFits(fitsfile, metadata, imageMetadata, maskMetadata, varianceMetadata);
444 }
445 
446 namespace {
447 
448 void processPlaneMetadata(std::shared_ptr<daf::base::PropertySet const> metadata,
449  std::shared_ptr<daf::base::PropertySet>& hdr, char const* exttype) {
450  if (metadata) {
451  hdr = metadata->deepCopy();
452  } else {
453  hdr.reset(new daf::base::PropertyList());
454  }
455  hdr->set("INHERIT", true);
456  hdr->set("EXTTYPE", exttype);
457 }
458 
459 } // namespace
460 
461 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
466  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
467  writeFits(fitsfile, fits::ImageWriteOptions(*_image), fits::ImageWriteOptions(*_mask),
468  fits::ImageWriteOptions(*_variance), metadata, imageMetadata, maskMetadata, varianceMetadata);
469 }
470 
471 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
473  std::string const& fileName, fits::ImageWriteOptions const& imageOptions,
474  fits::ImageWriteOptions const& maskOptions, fits::ImageWriteOptions const& varianceOptions,
478  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
479  fits::Fits fitsfile(fileName, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
480  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
481  varianceMetadata);
482 }
483 
484 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
486  fits::MemFileManager& manager, fits::ImageWriteOptions const& imageOptions,
487  fits::ImageWriteOptions const& maskOptions, fits::ImageWriteOptions const& varianceOptions,
491  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
492  fits::Fits fitsfile(manager, "w", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
493  writeFits(fitsfile, imageOptions, maskOptions, varianceOptions, metadata, imageMetadata, maskMetadata,
494  varianceMetadata);
495 }
496 
497 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
499  fits::Fits& fitsfile, fits::ImageWriteOptions const& imageOptions,
500  fits::ImageWriteOptions const& maskOptions, fits::ImageWriteOptions const& varianceOptions,
504  std::shared_ptr<daf::base::PropertySet const> varianceMetadata) const {
506  if (metadata) {
507  header = metadata->deepCopy();
508  } else {
509  header = std::make_shared<daf::base::PropertyList>();
510  }
511 
512  if (fitsfile.countHdus() != 0) {
514  "MaskedImage::writeFits can only write to an empty file");
515  }
516  if (fitsfile.getHdu() < 1) {
517  // Don't ever write images to primary; instead we make an empty primary.
518  fitsfile.createEmpty();
519  } else {
520  fitsfile.setHdu(0);
521  }
522  fitsfile.writeMetadata(*header);
523 
524  processPlaneMetadata(imageMetadata, header, "IMAGE");
525  _image->writeFits(fitsfile, imageOptions, header, _mask);
526 
527  processPlaneMetadata(maskMetadata, header, "MASK");
528  _mask->writeFits(fitsfile, maskOptions, header);
529 
530  processPlaneMetadata(varianceMetadata, header, "VARIANCE");
531  _variance->writeFits(fitsfile, varianceOptions, header, _mask);
532 }
533 
534 // private function conformSizes() ensures that the Mask and Variance have the same dimensions
535 // as Image. If Mask and/or Variance have non-zero dimensions that conflict with the size of Image,
536 // a lsst::pex::exceptions::LengthError is thrown.
537 
538 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
540  if (!_mask || _mask->getWidth() == 0 || _mask->getHeight() == 0) {
541  _mask = MaskPtr(new Mask(_image->getBBox()));
542  *_mask = 0;
543  } else {
544  if (_mask->getDimensions() != _image->getDimensions()) {
545  throw LSST_EXCEPT(
547  (boost::format("Dimension mismatch: Image %dx%d v. Mask %dx%d") % _image->getWidth() %
548  _image->getHeight() % _mask->getWidth() % _mask->getHeight())
549  .str());
550  }
551  }
552 
553  if (!_variance || _variance->getWidth() == 0 || _variance->getHeight() == 0) {
554  _variance = VariancePtr(new Variance(_image->getBBox()));
555  *_variance = 0;
556  } else {
557  if (_variance->getDimensions() != _image->getDimensions()) {
558  throw LSST_EXCEPT(
559  pex::exceptions::LengthError,
560  (boost::format("Dimension mismatch: Image %dx%d v. Variance %dx%d") % _image->getWidth() %
561  _image->getHeight() % _variance->getWidth() % _variance->getHeight())
562  .str());
563  }
564  }
565 }
566 
567 //
568 // Iterators and locators
569 //
570 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
573 #if 0 // this doesn't compile; why?
574  return iterator(_image->begin(), _mask->begin(), _variance->begin());
575 #else
576  typename Image::iterator imageBegin = _image->begin();
577  typename Mask::iterator maskBegin = _mask->begin();
578  typename Variance::iterator varianceBegin = _variance->begin();
579 
580  return iterator(imageBegin, maskBegin, varianceBegin);
581 #endif
582 }
583 
584 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
587  typename Image::iterator imageEnd = getImage()->end();
588  typename Mask::iterator maskEnd = getMask()->end();
589  typename Variance::iterator varianceEnd = getVariance()->end();
590 
591  return iterator(imageEnd, maskEnd, varianceEnd);
592 }
593 
594 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
597  typename Image::iterator imageEnd = getImage()->at(x, y);
598  typename Mask::iterator maskEnd = getMask()->at(x, y);
599  typename Variance::iterator varianceEnd = getVariance()->at(x, y);
600 
601  return iterator(imageEnd, maskEnd, varianceEnd);
602 }
603 
604 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
607  typename Image::reverse_iterator imageBegin = _image->rbegin();
608  typename Mask::reverse_iterator maskBegin = _mask->rbegin();
609  typename Variance::reverse_iterator varianceBegin = _variance->rbegin();
610 
611  return reverse_iterator(imageBegin, maskBegin, varianceBegin);
612 }
613 
614 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
617  typename Image::reverse_iterator imageEnd = getImage()->rend();
618  typename Mask::reverse_iterator maskEnd = getMask()->rend();
619  typename Variance::reverse_iterator varianceEnd = getVariance()->rend();
620 
621  return reverse_iterator(imageEnd, maskEnd, varianceEnd);
622 }
623 
624 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
627  typename Image::x_iterator imageBegin = _image->row_begin(y);
628  typename Mask::x_iterator maskBegin = _mask->row_begin(y);
629  typename Variance::x_iterator varianceBegin = _variance->row_begin(y);
630 
631  return x_iterator(imageBegin, maskBegin, varianceBegin);
632 }
633 
634 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
637  typename Image::x_iterator imageEnd = getImage()->row_end(y);
638  typename Mask::x_iterator maskEnd = getMask()->row_end(y);
639  typename Variance::x_iterator varianceEnd = getVariance()->row_end(y);
640 
641  return x_iterator(imageEnd, maskEnd, varianceEnd);
642 }
643 
644 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
647  typename Image::y_iterator imageBegin = _image->col_begin(x);
648  typename Mask::y_iterator maskBegin = _mask->col_begin(x);
649  typename Variance::y_iterator varianceBegin = _variance->col_begin(x);
650 
651  return y_iterator(imageBegin, maskBegin, varianceBegin);
652 }
653 
654 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
657  typename Image::y_iterator imageEnd = getImage()->col_end(x);
658  typename Mask::y_iterator maskEnd = getMask()->col_end(x);
659  typename Variance::y_iterator varianceEnd = getVariance()->col_end(x);
660 
661  return y_iterator(imageEnd, maskEnd, varianceEnd);
662 }
663 
664 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
667  typename Image::fast_iterator imageBegin = _image->begin(contiguous);
668  typename Mask::fast_iterator maskBegin = _mask->begin(contiguous);
669  typename Variance::fast_iterator varianceBegin = _variance->begin(contiguous);
670 
671  return fast_iterator(imageBegin, maskBegin, varianceBegin);
672 }
673 
674 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
677  typename Image::fast_iterator imageEnd = getImage()->end(contiguous);
678  typename Mask::fast_iterator maskEnd = getMask()->end(contiguous);
679  typename Variance::fast_iterator varianceEnd = getVariance()->end(contiguous);
680 
681  return fast_iterator(imageEnd, maskEnd, varianceEnd);
682 }
683 
684 template <typename ImagePixelT1, typename ImagePixelT2>
687  return imagesOverlap(*image1.getImage(), *image2.getImage()) ||
688  imagesOverlap(*image1.getVariance(), *image2.getVariance()) ||
689  imagesOverlap(*image1.getMask(), *image2.getMask());
690 }
691 
692 //
693 // Explicit instantiations
694 //
695 #define INSTANTIATE2(ImagePixelT1, ImagePixelT2) \
696  template bool imagesOverlap<ImagePixelT1, ImagePixelT2>(MaskedImage<ImagePixelT1> const&, \
697  MaskedImage<ImagePixelT2> const&);
698 
699 template class MaskedImage<std::uint16_t>;
700 template class MaskedImage<int>;
701 template class MaskedImage<float>;
702 template class MaskedImage<double>;
704 
710 
712 INSTANTIATE2(int, int);
713 INSTANTIATE2(int, float);
714 INSTANTIATE2(int, double);
716 
718 INSTANTIATE2(float, int);
719 INSTANTIATE2(float, float);
720 INSTANTIATE2(float, double);
722 
724 INSTANTIATE2(double, int);
725 INSTANTIATE2(double, float);
726 INSTANTIATE2(double, double);
728 
734 
735 } // namespace image
736 } // namespace afw
737 } // namespace lsst
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:49
afw::table::Key< afw::table::Array< MaskPixelT > > mask
afw::table::Key< afw::table::Array< VariancePixelT > > variance
LSST DM logging module built on log4cxx.
int y
Definition: SpanSet.cc:49
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
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
Definition: ImageBase.h:129
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Definition: ImageBase.h:137
_view_t::iterator iterator
An STL compliant iterator.
Definition: ImageBase.h:125
_view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
Definition: ImageBase.h:143
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:133
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:58
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:247
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:427
MaskedImage(unsigned int width, unsigned int height, MaskPlaneDict const &planeDict=MaskPlaneDict())
Construct from a supplied dimensions.
Definition: MaskedImage.cc:49
lsst::afw::image::Image< VariancePixelT > Variance
Definition: MaskedImage.h:84
void scaledDivides(double const c, MaskedImage const &rhs)
Definition: MaskedImage.cc:399
MaskedImageIterator< typename Image::reverse_iterator, typename Mask::reverse_iterator, typename Variance::reverse_iterator > reverse_iterator
Definition: MaskedImage.h:547
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
iterator begin() const
Return an iterator to the start of the image.
Definition: MaskedImage.cc:572
reverse_iterator rbegin() const
Return a reverse_iterator to the start of the image.
Definition: MaskedImage.cc:606
y_iterator col_end(int x) const
Return an y_iterator to the end of the image.
Definition: MaskedImage.cc:656
void scaledMinus(double const c, MaskedImage const &rhs)
Subtract a scaled MaskedImage c*rhs from a MaskedImage.
Definition: MaskedImage.cc:271
lsst::afw::image::Image< ImagePixelT > Image
Definition: MaskedImage.h:85
MaskedImage & operator-=(ImagePixelT const rhs)
Subtract a scalar rhs from a MaskedImage.
Definition: MaskedImage.cc:280
x_iterator row_end(int y) const
Return an x_iterator to the end of the image.
Definition: MaskedImage.cc:636
void scaledMultiplies(double const c, MaskedImage const &rhs)
Definition: MaskedImage.cc:328
iterator end() const
Return an iterator to the end of the image.
Definition: MaskedImage.cc:586
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:229
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
Definition: MaskedImage.h:1079
MaskedImage & operator=(MaskedImage const &rhs)
Make the lhs use the rhs's pixels.
x_iterator row_begin(int y) const
Return an x_iterator to the start of the image.
Definition: MaskedImage.cc:626
void swap(MaskedImage &rhs)
Definition: MaskedImage.cc:199
y_iterator col_begin(int x) const
Return an y_iterator to the start of the image.
Definition: MaskedImage.cc:646
lsst::afw::image::Mask< MaskPixelT > Mask
Definition: MaskedImage.h:86
reverse_iterator rend() const
Return a reverse_iterator to the end of the image.
Definition: MaskedImage.cc:616
MaskedImageIterator< typename Image::iterator, typename Mask::iterator, typename Variance::iterator > iterator
Definition: MaskedImage.h:539
MaskedImage & operator+=(ImagePixelT const rhs)
Add a scalar rhs to a MaskedImage.
Definition: MaskedImage.cc:256
MaskedImage & operator*=(ImagePixelT const rhs)
Definition: MaskedImage.cc:349
iterator at(int const x, int const y) const
Return an iterator at the point (x, y)
Definition: MaskedImage.cc:596
MaskedImage & operator/=(ImagePixelT const rhs)
Definition: MaskedImage.cc:420
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage's mask.
Definition: MaskedImage.h:1058
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
Definition: MaskedImage.h:1046
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
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:706
def writeFits(filename, stamp_ims, metadata, write_mask, write_variance)
Definition: stamps.py:38
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
A base class for image defects.
T reset(T... args)
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
double _c
Definition: MaskedImage.cc:298
#define INSTANTIATE2(ImagePixelT1, ImagePixelT2)
Definition: MaskedImage.cc:695
Options for writing an image to FITS.
Definition: fits.h:219
T swap(T... args)