LSST Applications  21.0.0-147-g0e635eb1+1acddb5be5,22.0.0+052faf71bd,22.0.0+1ea9a8b2b2,22.0.0+6312710a6c,22.0.0+729191ecac,22.0.0+7589c3a021,22.0.0+9f079a9461,22.0.1-1-g7d6de66+b8044ec9de,22.0.1-1-g87000a6+536b1ee016,22.0.1-1-g8e32f31+6312710a6c,22.0.1-10-gd060f87+016f7cdc03,22.0.1-12-g9c3108e+df145f6f68,22.0.1-16-g314fa6d+c825727ab8,22.0.1-19-g93a5c75+d23f2fb6d8,22.0.1-19-gb93eaa13+aab3ef7709,22.0.1-2-g8ef0a89+b8044ec9de,22.0.1-2-g92698f7+9f079a9461,22.0.1-2-ga9b0f51+052faf71bd,22.0.1-2-gac51dbf+052faf71bd,22.0.1-2-gb66926d+6312710a6c,22.0.1-2-gcb770ba+09e3807989,22.0.1-20-g32debb5+b8044ec9de,22.0.1-23-gc2439a9a+fb0756638e,22.0.1-3-g496fd5d+09117f784f,22.0.1-3-g59f966b+1e6ba2c031,22.0.1-3-g849a1b8+f8b568069f,22.0.1-3-gaaec9c0+c5c846a8b1,22.0.1-32-g5ddfab5d3+60ce4897b0,22.0.1-4-g037fbe1+64e601228d,22.0.1-4-g8623105+b8044ec9de,22.0.1-5-g096abc9+d18c45d440,22.0.1-5-g15c806e+57f5c03693,22.0.1-7-gba73697+57f5c03693,master-g6e05de7fdc+c1283a92b8,master-g72cdda8301+729191ecac,w.2021.39
LSST Data Management Base Package
Image.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
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  * 2-D images
27  *
28  * This file contains the 2-d image support for LSST
29  */
30 #ifndef LSST_AFW_IMAGE_IMAGE_H
31 #define LSST_AFW_IMAGE_IMAGE_H
32 
33 #include <string>
34 
35 #include <climits>
36 #include <memory>
37 
38 #include "lsst/geom.h"
40 #include "lsst/afw/image/Mask.h"
41 #include "lsst/afw/math/Function.h"
43 #include "ndarray.h"
44 
45 namespace lsst {
46 namespace afw {
47 namespace image {
48 
50 template <typename PixelT>
51 class Image : public ImageBase<PixelT> {
52 public:
53  template <typename, typename, typename>
54  friend class MaskedImage;
55 
57 
59  template <typename ImagePT = PixelT>
63  };
64  template <typename OtherPixelT>
65  friend class Image; // needed by generalised copy constructors
66 
77  explicit Image(unsigned int width, unsigned int height, PixelT initialValue = 0);
87  explicit Image(lsst::geom::Extent2I const& dimensions = lsst::geom::Extent2I(), PixelT initialValue = 0);
94  explicit Image(lsst::geom::Box2I const& bbox, PixelT initialValue = 0);
95 
109  explicit Image(Image const& rhs, lsst::geom::Box2I const& bbox, ImageOrigin const origin = PARENT,
110  const bool deep = false);
120  Image(const Image& rhs, const bool deep = false);
121  Image(Image&& rhs);
122 
137  explicit Image(std::string const& fileName, int hdu = fits::DEFAULT_HDU,
141  bool allowUnsafe = false);
142 
157  explicit Image(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU,
161  bool allowUnsafe = false);
162 
174  explicit Image(fits::Fits& fitsfile,
178  bool allowUnsafe = false);
179 
180  // generalised copy constructor
181  template <typename OtherPixelT>
182  Image(Image<OtherPixelT> const& rhs, const bool deep) : image::ImageBase<PixelT>(rhs, deep) {}
183 
184  explicit Image(ndarray::Array<PixelT, 2, 1> const& array, bool deep = false,
186  : image::ImageBase<PixelT>(array, deep, xy0) {}
187 
188  ~Image() override = default;
189  //
190  // Assignment operators are not inherited
191  //
193  Image& operator=(const PixelT rhs);
203  Image& operator=(const Image& rhs);
204  Image& operator=(Image&& rhs);
205 
220  return Image(*this, bbox, origin, false);
221  }
222 
224  Image operator[](lsst::geom::Box2I const& bbox) const { return subset(bbox); }
225 
227 
235  void writeFits(std::string const& fileName,
238  std::string const& mode = "w") const;
239 
250  std::string const& mode = "w") const;
251 
260 
270  void writeFits(std::string const& filename, fits::ImageWriteOptions const& options,
271  std::string const& mode = "w",
273  std::shared_ptr<Mask<MaskPixel> const> mask = nullptr) const;
274 
285  std::string const& mode = "w",
287  std::shared_ptr<Mask<MaskPixel> const> mask = nullptr) const;
288 
297  void writeFits(fits::Fits& fitsfile, fits::ImageWriteOptions const& options,
299  std::shared_ptr<Mask<MaskPixel> const> mask = nullptr) const;
300 
309  static Image readFits(std::string const& filename, int hdu = fits::DEFAULT_HDU) {
310  return Image<PixelT>(filename, hdu);
311  }
312 
321  static Image readFits(fits::MemFileManager& manager, int hdu = fits::DEFAULT_HDU) {
322  return Image<PixelT>(manager, hdu);
323  }
324 
325  void swap(Image& rhs);
326  //
327  // Operators etc.
328  //
330  Image& operator+=(PixelT const rhs);
332  virtual Image& operator+=(Image<PixelT> const& rhs);
340  void scaledPlus(double const c, Image<PixelT> const& rhs);
342  Image& operator-=(PixelT const rhs);
344  Image& operator-=(Image<PixelT> const& rhs);
352  void scaledMinus(double const c, Image<PixelT> const& rhs);
354  Image& operator*=(PixelT const rhs);
356  Image& operator*=(Image<PixelT> const& rhs);
358  void scaledMultiplies(double const c, Image<PixelT> const& rhs);
364  Image& operator/=(PixelT const rhs);
366  Image& operator/=(Image<PixelT> const& rhs);
368  void scaledDivides(double const c, Image<PixelT> const& rhs);
369 
370  // In-place per-pixel sqrt(). Useful when handling variance planes.
371  void sqrt();
372 
373 protected:
375 };
376 
378 template <typename LhsPixelT, typename RhsPixelT>
379 Image<LhsPixelT>& operator+=(Image<LhsPixelT>& lhs, Image<RhsPixelT> const& rhs);
381 template <typename LhsPixelT, typename RhsPixelT>
382 Image<LhsPixelT>& operator-=(Image<LhsPixelT>& lhs, Image<RhsPixelT> const& rhs);
384 template <typename LhsPixelT, typename RhsPixelT>
385 Image<LhsPixelT>& operator*=(Image<LhsPixelT>& lhs, Image<RhsPixelT> const& rhs);
387 template <typename LhsPixelT, typename RhsPixelT>
388 Image<LhsPixelT>& operator/=(Image<LhsPixelT>& lhs, Image<RhsPixelT> const& rhs);
389 
390 template <typename PixelT>
391 void swap(Image<PixelT>& a, Image<PixelT>& b);
392 
396 template <typename PixelT>
398 public:
413  explicit DecoratedImage(const lsst::geom::Box2I& bbox);
430  DecoratedImage(DecoratedImage const& rhs, const bool deep = false);
441  explicit DecoratedImage(std::string const& fileName, const int hdu = fits::DEFAULT_HDU,
443  ImageOrigin const origin = PARENT, bool allowUnsafe = false);
444 
451 
453  void setMetadata(std::shared_ptr<lsst::daf::base::PropertySet> metadata) { _metadata = metadata; }
454 
456  int getWidth() const { return _image->getWidth(); }
458  int getHeight() const { return _image->getHeight(); }
459 
461  int getX0() const { return _image->getX0(); }
463  int getY0() const { return _image->getY0(); }
464 
466  const lsst::geom::Extent2I getDimensions() const { return _image->getDimensions(); }
467 
468  void swap(DecoratedImage& rhs);
469 
477  void writeFits(std::string const& fileName,
480  std::string const& mode = "w") const;
481 
490  void writeFits(std::string const& fileName, fits::ImageWriteOptions const& options,
493  std::string const& mode = "w") const;
494 
498  std::shared_ptr<Image<PixelT> const> getImage() const { return _image; }
499 
505  double getGain() const { return _gain; }
507  void setGain(double gain) { _gain = gain; }
508 
509 private:
512 
513  double _gain;
514 
515  void init();
516 };
517 
518 template <typename PixelT>
519 void swap(DecoratedImage<PixelT>& a, DecoratedImage<PixelT>& b);
520 
526 
530 template <typename T1, typename T2>
531 bool imagesOverlap(ImageBase<T1> const& image1, ImageBase<T2> const& image2);
532 
533 } // namespace image
534 } // namespace afw
535 } // namespace lsst
536 
537 #endif
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
table::Key< double > gain
Definition: Amplifier.cc:118
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:48
afw::table::Key< afw::table::Array< MaskPixelT > > mask
table::Key< int > b
table::Key< int > a
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:297
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:121
A container for an Image and its associated metadata.
Definition: Image.h:397
void writeFits(std::string const &fileName, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet const >(), std::string const &mode="w") const
Write a FITS file.
std::shared_ptr< Image< PixelT > const > getImage() const
Return a shared_ptr to the DecoratedImage's Image as const.
Definition: Image.h:498
DecoratedImage(const lsst::geom::Extent2I &dimensions=lsst::geom::Extent2I())
Create an image of the specified size.
const lsst::geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
Definition: Image.h:466
DecoratedImage & operator=(const DecoratedImage &image)
Assignment operator.
void swap(DecoratedImage &rhs)
double getGain() const
Return the DecoratedImage's gain.
Definition: Image.h:505
int getY0() const
Return the image's row-origin.
Definition: Image.h:463
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:458
std::shared_ptr< Image< PixelT > > getImage()
Return a shared_ptr to the DecoratedImage's Image.
Definition: Image.h:496
std::shared_ptr< lsst::daf::base::PropertySet > getMetadata() const
Definition: Image.h:452
void setMetadata(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Definition: Image.h:453
int getX0() const
Return the image's column-origin.
Definition: Image.h:461
void setGain(double gain)
Set the DecoratedImage's gain.
Definition: Image.h:507
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:456
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:102
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:51
void scaledPlus(double const c, Image< PixelT > const &rhs)
Add Image c*rhs to lhs.
Definition: Image.cc:488
Image & operator*=(PixelT const rhs)
Multiply lhs by scalar rhs.
Definition: Image.cc:546
void scaledMinus(double const c, Image< PixelT > const &rhs)
Subtract Image c*rhs from lhs.
Definition: Image.cc:520
Image & operator-=(PixelT const rhs)
Subtract scalar rhs from lhs.
Definition: Image.cc:501
Image(fits::Fits &fitsfile, std::shared_ptr< lsst::daf::base::PropertySet > metadata=std::shared_ptr< lsst::daf::base::PropertySet >(), lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Construct an Image from an already-open FITS object.
Image & operator=(const PixelT rhs)
Set the image's pixels to rhs.
Definition: Image.cc:338
Image subset(lsst::geom::Box2I const &bbox, ImageOrigin origin=PARENT) const
Return a subimage corresponding to the given box.
Definition: Image.h:219
Image(fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU, std::shared_ptr< lsst::daf::base::PropertySet > metadata=std::shared_ptr< lsst::daf::base::PropertySet >(), lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Construct an Image by reading a FITS image in memory.
Image operator[](lsst::geom::Box2I const &bbox) const
Return a subimage corresponding to the given box (interpreted as PARENT coordinates).
Definition: Image.h:224
void scaledMultiplies(double const c, Image< PixelT > const &rhs)
Multiply lhs by Image c*rhs (i.e. pixel-by-pixel multiplication)
Definition: Image.cc:565
Image & operator+=(PixelT const rhs)
Add scalar rhs to lhs.
Definition: Image.cc:456
static Image readFits(fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU)
Read an Image from a FITS RAM file.
Definition: Image.h:321
~Image() override=default
void swap(Image &rhs)
Definition: Image.cc:437
void writeFits(fits::MemFileManager &manager, fits::ImageWriteOptions const &options, std::string const &mode="w", std::shared_ptr< daf::base::PropertySet const > header=nullptr, std::shared_ptr< Mask< MaskPixel > const > mask=nullptr) const
Write an image to a FITS RAM file.
Image & operator/=(PixelT const rhs)
Divide lhs by scalar rhs.
Definition: Image.cc:578
Image(ndarray::Array< PixelT, 2, 1 > const &array, bool deep=false, lsst::geom::Point2I const &xy0=lsst::geom::Point2I())
Definition: Image.h:184
Image(Image< OtherPixelT > const &rhs, const bool deep)
Definition: Image.h:182
void writeFits(fits::MemFileManager &manager, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet const >(), std::string const &mode="w") const
Write an image to a FITS RAM file.
void writeFits(fits::Fits &fitsfile, fits::ImageWriteOptions const &options, std::shared_ptr< daf::base::PropertySet const > header=nullptr, std::shared_ptr< Mask< MaskPixel > const > mask=nullptr) const
Write an image to an open FITS file object.
void writeFits(std::string const &fileName, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet const >(), std::string const &mode="w") const
Write an image to a regular FITS file.
static Image readFits(std::string const &filename, int hdu=fits::DEFAULT_HDU)
Read an Image from a regular FITS file.
Definition: Image.h:309
void writeFits(fits::Fits &fitsfile, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet const >()) const
Write an image to an open FITS file object.
Image(std::string const &fileName, int hdu=fits::DEFAULT_HDU, std::shared_ptr< lsst::daf::base::PropertySet > metadata=std::shared_ptr< lsst::daf::base::PropertySet >(), lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Construct an Image by reading a regular FITS file.
void scaledDivides(double const c, Image< PixelT > const &rhs)
Divide lhs by Image c*rhs (i.e. pixel-by-pixel division)
Definition: Image.cc:613
void writeFits(std::string const &filename, fits::ImageWriteOptions const &options, std::string const &mode="w", std::shared_ptr< daf::base::PropertySet const > header=nullptr, std::shared_ptr< Mask< MaskPixel > const > mask=nullptr) const
Write an image to a regular FITS file.
friend class Image
Definition: Image.h:65
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
A Function taking two arguments.
Definition: Function.h:259
Class for storing generic metadata.
Definition: PropertySet.h:66
An integer coordinate rectangle.
Definition: Box.h:55
const int DEFAULT_HDU
Specify that the default HDU should be read.
Definition: fitsDefaults.h:18
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Image< LhsPixelT > & operator+=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Add lhs to Image rhs (i.e. pixel-by-pixel addition) where types are different.
Definition: Image.cc:659
Image< LhsPixelT > & operator-=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Subtract lhs from Image rhs (i.e. pixel-by-pixel subtraction) where types are different.
Definition: Image.cc:665
lsst::geom::Box2I bboxFromMetadata(daf::base::PropertySet &metadata)
Determine the image bounding box from its metadata (FITS header)
Definition: Image.cc:682
Image< LhsPixelT > & operator/=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Divide lhs by Image rhs (i.e. pixel-by-pixel division) where types are different.
Definition: Image.cc:677
Image< LhsPixelT > & operator*=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Multiply lhs by Image rhs (i.e. pixel-by-pixel multiplication) where types are different.
Definition: Image.cc:671
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:694
void swap(Image< PixelT > &a, Image< PixelT > &b)
Definition: Image.cc:444
A base class for image defects.
Options for writing an image to FITS.
Definition: fits.h:219
A templated class to return this classes' type (present in Image/Mask/MaskedImage)
Definition: Image.h:60