LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
LSSTDataManagementBasePackage
ImageBase.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  * Support for 2-D images
27  *
28  * This file contains the basic 2-d image support for LSST
29  */
30 #ifndef LSST_AFW_IMAGE_IMAGEBASE_H
31 #define LSST_AFW_IMAGE_IMAGEBASE_H
32 
33 #include <string>
34 #include <utility>
35 #include <functional>
36 
37 #include <memory>
38 
39 #include "lsst/geom.h"
40 #include "lsst/afw/image/lsstGil.h"
42 #include "lsst/afw/math/Function.h"
43 #include "lsst/pex/exceptions.h"
44 #include "ndarray.h"
45 
46 namespace lsst {
47 namespace afw {
48 
49 namespace fits {
50 class Fits;
51 class MemFileManager;
52 struct ImageWriteOptions;
53 } // namespace fits
54 
55 namespace image {
56 namespace detail {
57 //
58 // Traits for image types
59 //
61 struct basic_tag {};
63 struct Image_tag : public basic_tag {};
65 template <typename ImageT>
66 struct image_traits {
67  typedef typename ImageT::image_category image_category;
68 };
69 //
70 std::string const wcsNameForXY0 = "A"; // the name of the WCS to use to save (X0, Y0) to FITS files; e.g. "A"
71 } // namespace detail
72 
74 class CheckIndices {
75 public:
76  explicit CheckIndices(bool check = true) : _check(check) {}
77  operator bool() const { return _check; }
78 
79 private:
80  bool _check;
81 };
82 
84 template <typename PixelT>
85 struct Reference {
86  typedef typename boost::gil::channel_traits<PixelT>::reference type;
87 };
89 template <typename PixelT>
91  typedef typename boost::gil::channel_traits<PixelT>::const_reference type;
92 };
93 
95 
97 //
98 // You are not expected to use this class directly in your own code; use one of the
99 // specialised subclasses
100 //
101 template <typename PixelT>
102 class ImageBase {
103 private:
104  typedef typename lsst::afw::image::detail::types_traits<PixelT>::view_t _view_t;
105  typedef typename lsst::afw::image::detail::types_traits<PixelT>::const_view_t _const_view_t;
106 
107  typedef ndarray::Manager Manager;
108 
109 public:
111 
115  typedef PixelT Pixel;
121  typedef typename _view_t::xy_locator xy_locator;
123  typedef typename _view_t::xy_locator::const_t const_xy_locator;
125  typedef typename _view_t::iterator iterator;
127  typedef typename _const_view_t::iterator const_iterator;
129  typedef typename _view_t::reverse_iterator reverse_iterator;
131  typedef typename _const_view_t::reverse_iterator const_reverse_iterator;
133  typedef typename _view_t::x_iterator x_iterator;
137  typedef x_iterator fast_iterator;
139  typedef typename _view_t::x_iterator xy_x_iterator;
141  typedef typename _const_view_t::x_iterator const_x_iterator;
143  typedef typename _view_t::y_iterator y_iterator;
145  typedef typename _view_t::y_iterator xy_y_iterator;
147  typedef typename _const_view_t::y_iterator const_y_iterator;
149  typedef typename ndarray::Array<PixelT, 2, 1> Array;
151  typedef typename ndarray::Array<PixelT const, 2, 1> ConstArray;
152 
153  template <typename OtherPixelT>
154  friend class ImageBase; // needed by generalised copy constructors
155 
157  template <typename SinglePixelT>
158  static SinglePixel PixelCast(SinglePixelT rhs) {
159  return SinglePixel(rhs);
160  }
161  //
162  // DecoratedImage needs enough access to ImageBase to read data from disk; we might be able to design
163  // around this
164  //
165  template <typename>
166  friend class DecoratedImage;
167  template <typename, typename, typename>
168  friend class MaskedImage;
181  explicit ImageBase(const lsst::geom::Box2I& bbox);
192  ImageBase(const ImageBase& src, const bool deep = false);
208  explicit ImageBase(const ImageBase& src, const lsst::geom::Box2I& bbox, const ImageOrigin origin = PARENT,
209  const bool deep = false);
216  template <typename OtherPixelT>
217  ImageBase(const ImageBase<OtherPixelT>& rhs, const bool deep) {
218  if (!deep) {
220  "Only deep copies are permitted for ImageBases with different pixel types");
221  }
222 
223  ImageBase<PixelT> tmp(rhs.getBBox());
224  copy_and_convert_pixels(rhs._gilView, tmp._gilView); // from boost::gil
225 
226  using std::swap; // See Meyers, Effective C++, Item 25
227  ImageBase<PixelT>::swap(tmp); // See Meyers, Effective C++, Items 11 and 43
228  }
229 
239  explicit ImageBase(Array const& array, bool deep = false,
241 
242  virtual ~ImageBase() = default;
251  ImageBase& operator=(const ImageBase& rhs);
252  ImageBase& operator=(ImageBase&& rhs);
254  ImageBase& operator=(const PixelT rhs);
260  [[deprecated("Use `assign` instead. To be removed after 20.0.0.")]] // DM-22276
261  ImageBase&
262  operator<<=(const ImageBase& rhs);
263 
275  void assign(ImageBase const& rhs, lsst::geom::Box2I const& bbox = lsst::geom::Box2I(),
276  ImageOrigin origin = PARENT);
277 
278  // TODO: should deprecate these, but the replacement should take two ints
279  // rather than a Point2I, to keep C++ loops simple
281  PixelReference operator()(int x, int y);
283  PixelReference operator()(int x, int y, CheckIndices const&);
285  PixelConstReference operator()(int x, int y) const;
287  PixelConstReference operator()(int x, int y, CheckIndices const&) const;
288 
290 
294  [[deprecated("Use `operator[Point2I(x, y)]` instead. To be removed after 20.0.0.")]] // DM-22276
295  PixelConstReference
296  get0(int x, int y) const {
297  return operator()(x - getX0(), y - getY0());
298  }
299  [
300  [deprecated("No replacement; `operator[Point2I(x, y)]` provides unchecked lookup. To be "
301  "removed after 20.0.0.")]] // DM-22276
302  PixelConstReference
303  get0(int x, int y, CheckIndices const& check) const {
304  return operator()(x - getX0(), y - getY0(), check);
305  }
306  [[deprecated("Use `operator[Point2I(x, y)]` instead. To be removed after 20.0.0.")]] // DM-22276
307  void
308  set0(int x, int y, const PixelT v) {
309  operator()(x - getX0(), y - getY0()) = v;
310  }
311  [
312  [deprecated("No replacement; `operator[Point2I(x, y)]` provides unchecked lookup. To be "
313  "removed after 20.0.0.")]] // DM-22276
314  void
315  set0(int x, int y, const PixelT v, CheckIndices const& check) {
316  operator()(x - getX0(), y - getY0(), check) = v;
317  }
319 
321  PixelReference get(lsst::geom::Point2I const& index, ImageOrigin origin);
322 
324  PixelConstReference get(lsst::geom::Point2I const& index, ImageOrigin origin) const;
325 
327  PixelReference operator[](lsst::geom::Point2I const& index) { return get(index, PARENT); }
328 
330  PixelConstReference operator[](lsst::geom::Point2I const& index) const { return get(index, PARENT); }
331 
333  int getWidth() const { return _gilView.width(); }
335  int getHeight() const { return _gilView.height(); }
343  int getX0() const { return _origin.getX(); }
351  int getY0() const { return _origin.getY(); }
352 
360  lsst::geom::Point2I getXY0() const { return _origin; }
361 
368  double const pos,
369  lsst::afw::image::xOrY const xy
370  ) const {
371  double const fullIndex = pos - PixelZeroPos - (xy == X ? getX0() : getY0());
372  int const roundedIndex = static_cast<int>(fullIndex + 0.5);
373  double const residual = fullIndex - roundedIndex;
374  return std::pair<int, double>(roundedIndex, residual);
375  }
376 
386  inline double indexToPosition(double ind,
387  lsst::afw::image::xOrY const xy
388  ) const {
389  return ind + PixelZeroPos + (xy == X ? getX0() : getY0());
390  }
391 
393  lsst::geom::Extent2I getDimensions() const { return lsst::geom::Extent2I(getWidth(), getHeight()); }
394 
395  void swap(ImageBase& rhs);
396 
397  Array getArray();
398  ConstArray getArray() const;
399  //
400  // Iterators and Locators
401  //
407  iterator begin() const;
409  iterator end() const;
411  reverse_iterator rbegin() const;
413  reverse_iterator rend() const;
415  iterator at(int x, int y) const;
416 
424  fast_iterator begin(bool contiguous) const;
432  fast_iterator end(bool contiguous) const;
433 
438  x_iterator row_begin(int y) const { return _gilView.row_begin(y); }
439 
441  x_iterator row_end(int y) const { return _gilView.row_end(y); }
442 
444  x_iterator x_at(int x, int y) const { return _gilView.x_at(x, y); }
445 
450  y_iterator col_begin(int x) const { return _gilView.col_begin(x); }
451 
453  y_iterator col_end(int x) const { return _gilView.col_end(x); }
454 
456  y_iterator y_at(int x, int y) const { return _gilView.y_at(x, y); }
457 
462  xy_locator xy_at(int x, int y) const { return xy_locator(_gilView.xy_at(x, y)); }
471  void setXY0(lsst::geom::Point2I const origin) { _origin = origin; }
480  void setXY0(int const x0, int const y0) { setXY0(lsst::geom::Point2I(x0, y0)); }
481 
483  if (origin == PARENT) {
484  return lsst::geom::Box2I(_origin, getDimensions());
485  } else
486  return lsst::geom::Box2I(lsst::geom::Point2I(0, 0), getDimensions());
487  }
488 
489 private:
490  lsst::geom::Point2I _origin;
491  Manager::Ptr _manager;
492  _view_t _gilView;
493 
494  // oring of ImageBase in some larger image as returned to and manipulated
495  // by the user
496 
497 protected:
498  static _view_t _allocateView(lsst::geom::Extent2I const& dimensions, Manager::Ptr& manager);
499  static _view_t _makeSubView(lsst::geom::Extent2I const& dimensions, lsst::geom::Extent2I const& offset,
500  const _view_t& view);
501 
502  _view_t _getRawView() const { return _gilView; }
503 
504  inline bool isContiguous() const { return begin() + getWidth() * getHeight() == end(); }
505 };
506 
507 template <typename PixelT>
509 
510 // Inline template definitions
511 
512 template <typename PixelT>
514  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
515  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
516  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
517  this->_manager);
518 }
519 
520 template <typename PixelT>
522  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
523  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
524  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
525  this->_manager);
526 }
527 
528 } // namespace image
529 } // namespace afw
530 } // namespace lsst
531 
532 #endif
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
boost::gil::channel_traits< PixelT >::reference type
reference type
Definition: ImageBase.h:86
_const_view_t::reverse_iterator const_reverse_iterator
An STL compliant const reverse iterator.
Definition: ImageBase.h:131
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Definition: ImageBase.h:137
ConstReference< PixelT >::type PixelConstReference
A ConstReference to a PixelT.
Definition: ImageBase.h:119
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
Definition: ImageBase.h:129
std::pair< int, double > positionToIndex(double const pos, lsst::afw::image::xOrY const xy) const
Convert image position to index (nearest integer and fractional parts)
Definition: ImageBase.h:367
_view_t::x_iterator xy_x_iterator
An iterator for traversing the pixels in a row, created from an xy_locator.
Definition: ImageBase.h:139
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Definition: Image.cc:255
detail::basic_tag image_category
trait class to identify type of image
Definition: ImageBase.h:110
T swap(T... args)
instead To be removed after PixelConstReference get0(int x, int y) const
Definition: ImageBase.h:296
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:335
table::Key< int > b
provides unchecked lookup To be removed after PixelConstReference get0(int x, int y, CheckIndices const &check) const
Definition: ImageBase.h:303
_const_view_t::x_iterator const_x_iterator
A const iterator for traversing the pixels in a row.
Definition: ImageBase.h:141
PixelT Pixel
A pixel in this ImageBase.
Definition: ImageBase.h:115
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:133
int y
Definition: SpanSet.cc:49
table::Key< int > a
void setXY0(lsst::geom::Point2I const origin)
Set the ImageBase&#39;s origin.
Definition: ImageBase.h:471
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:343
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:102
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:438
Fits * fits
Definition: FitsWriter.cc:90
_const_view_t::iterator const_iterator
An STL compliant const iterator.
Definition: ImageBase.h:127
STL class.
_view_t::iterator iterator
An STL compliant iterator.
Definition: ImageBase.h:125
y_iterator y_at(int x, int y) const
Return an y_iterator to the point (x, y) in the image.
Definition: ImageBase.h:456
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
y_iterator col_begin(int x) const
Return an y_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:450
A base class for image defects.
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:482
std::shared_ptr< RecordT > src
Definition: Match.cc:48
PixelReference operator[](lsst::geom::Point2I const &index)
Return a reference to a single pixel in PARENT coordinates (with no bounds check).
Definition: ImageBase.h:327
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:73
_view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
Definition: ImageBase.h:143
traits class for image categories
Definition: ImageBase.h:66
A class used to request that array accesses be checked.
Definition: ImageBase.h:74
y_iterator col_end(int x) const
Return an y_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:453
instead To be removed after void set0(int x, int y, const PixelT v)
Definition: ImageBase.h:308
PixelConstReference operator[](lsst::geom::Point2I const &index) const
Return a reference to a single pixel in PARENT coordinates (with no bounds check).
Definition: ImageBase.h:330
double x
ndarray::Array< PixelT const, 2, 1 > ConstArray
An immutable ndarray representation of the image.
Definition: ImageBase.h:151
void setXY0(int const x0, int const y0)
Set the ImageBase&#39;s origin.
Definition: ImageBase.h:480
_view_t::xy_locator::const_t const_xy_locator
A const_xy_locator.
Definition: ImageBase.h:123
lsst::geom::Point2I getXY0() const
Return the image&#39;s origin.
Definition: ImageBase.h:360
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Extent< int, 2 > Extent2I
Definition: Extent.h:397
provides unchecked lookup To be removed after void set0(int x, int y, const PixelT v, CheckIndices const &check)
Definition: ImageBase.h:315
int getY0() const
Return the image&#39;s row-origin.
Definition: ImageBase.h:351
_const_view_t::y_iterator const_y_iterator
A const iterator for traversing the pixels in a column.
Definition: ImageBase.h:147
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:333
Reference< PixelT >::type PixelReference
A Reference to a PixelT.
Definition: ImageBase.h:117
static SinglePixel PixelCast(SinglePixelT rhs)
Convert a type to our SinglePixel type.
Definition: ImageBase.h:158
Reports invalid arguments.
Definition: Runtime.h:66
x_iterator row_end(int y) const
Return an x_iterator to the end of the y&#39;th row.
Definition: ImageBase.h:441
x_iterator x_at(int x, int y) const
Return an x_iterator to the point (x, y) in the image.
Definition: ImageBase.h:444
ImageT::image_category image_category
Definition: ImageBase.h:67
lsst::geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:393
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
_view_t _getRawView() const
Definition: ImageBase.h:502
const double PixelZeroPos
position of center of pixel 0
Definition: ImageUtils.h:44
_view_t::xy_locator xy_locator
An xy_locator.
Definition: ImageBase.h:121
ImageBase(const ImageBase< OtherPixelT > &rhs, const bool deep)
generalised copy constructor
Definition: ImageBase.h:217
ndarray::Array< PixelT, 2, 1 > Array
A mutable ndarray representation of the image.
Definition: ImageBase.h:149
xy_locator xy_at(int x, int y) const
Return an xy_locator at the point (x, y) in the image.
Definition: ImageBase.h:462
An integer coordinate rectangle.
Definition: Box.h:55
PixelT SinglePixel
A single Pixel of the same type as those in the ImageBase.
Definition: ImageBase.h:113
CheckIndices(bool check=true)
Definition: ImageBase.h:76
double indexToPosition(double ind, lsst::afw::image::xOrY const xy) const
Convert image index to image position.
Definition: ImageBase.h:386
std::string const wcsNameForXY0
Definition: ImageBase.h:70
int end
boost::gil::channel_traits< PixelT >::const_reference type
const reference type
Definition: ImageBase.h:91
A container for an Image and its associated metadata.
Definition: Image.h:404
metafunction to extract reference type from PixelT
Definition: ImageBase.h:85
_view_t::y_iterator xy_y_iterator
An iterator for traversing the pixels in a row, created from an xy_locator.
Definition: ImageBase.h:145
metafunction to extract const reference type from PixelT
Definition: ImageBase.h:90