LSSTApplications  17.0+11,17.0+34,17.0+56,17.0+57,17.0+59,17.0+7,17.0-1-g377950a+33,17.0.1-1-g114240f+2,17.0.1-1-g4d4fbc4+28,17.0.1-1-g55520dc+49,17.0.1-1-g5f4ed7e+52,17.0.1-1-g6dd7d69+17,17.0.1-1-g8de6c91+11,17.0.1-1-gb9095d2+7,17.0.1-1-ge9fec5e+5,17.0.1-1-gf4e0155+55,17.0.1-1-gfc65f5f+50,17.0.1-1-gfc6fb1f+20,17.0.1-10-g87f9f3f+1,17.0.1-11-ge9de802+16,17.0.1-16-ga14f7d5c+4,17.0.1-17-gc79d625+1,17.0.1-17-gdae4c4a+8,17.0.1-2-g26618f5+29,17.0.1-2-g54f2ebc+9,17.0.1-2-gf403422+1,17.0.1-20-g2ca2f74+6,17.0.1-23-gf3eadeb7+1,17.0.1-3-g7e86b59+39,17.0.1-3-gb5ca14a,17.0.1-3-gd08d533+40,17.0.1-30-g596af8797,17.0.1-4-g59d126d+4,17.0.1-4-gc69c472+5,17.0.1-6-g5afd9b9+4,17.0.1-7-g35889ee+1,17.0.1-7-gc7c8782+18,17.0.1-9-gc4bbfb2+3,w.2019.22
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/daf/base/Citizen.h"
44 #include "lsst/pex/exceptions.h"
45 #include "ndarray.h"
46 
47 namespace lsst {
48 namespace afw {
49 
50 namespace fits {
51 class Fits;
52 class MemFileManager;
53 struct ImageWriteOptions;
54 } // namespace fits
55 
56 namespace image {
57 namespace detail {
58 //
59 // Traits for image types
60 //
62 struct basic_tag {};
64 struct Image_tag : public basic_tag {};
66 template <typename ImageT>
67 struct image_traits {
68  typedef typename ImageT::image_category image_category;
69 };
70 //
71 std::string const wcsNameForXY0 = "A"; // the name of the WCS to use to save (X0, Y0) to FITS files; e.g. "A"
72 } // namespace detail
73 
75 class CheckIndices {
76 public:
77  explicit CheckIndices(bool check = true) : _check(check) {}
78  operator bool() const { return _check; }
79 
80 private:
81  bool _check;
82 };
83 
85 template <typename PixelT>
86 struct Reference {
87  typedef typename boost::gil::channel_traits<PixelT>::reference type;
88 };
90 template <typename PixelT>
92  typedef typename boost::gil::channel_traits<PixelT>::const_reference type;
93 };
94 
96 
98 //
99 // You are not expected to use this class directly in your own code; use one of the
100 // specialised subclasses
101 //
102 template <typename PixelT>
104 private:
105  typedef typename lsst::afw::image::detail::types_traits<PixelT>::view_t _view_t;
106  typedef typename lsst::afw::image::detail::types_traits<PixelT>::const_view_t _const_view_t;
107 
108  typedef ndarray::Manager Manager;
109 
110 public:
112 
116  typedef PixelT Pixel;
122  typedef typename _view_t::xy_locator xy_locator;
124  typedef typename _view_t::xy_locator::const_t const_xy_locator;
126  typedef typename _view_t::iterator iterator;
128  typedef typename _const_view_t::iterator const_iterator;
130  typedef typename _view_t::reverse_iterator reverse_iterator;
132  typedef typename _const_view_t::reverse_iterator const_reverse_iterator;
134  typedef typename _view_t::x_iterator x_iterator;
138  typedef x_iterator fast_iterator;
140  typedef typename _view_t::x_iterator xy_x_iterator;
142  typedef typename _const_view_t::x_iterator const_x_iterator;
144  typedef typename _view_t::y_iterator y_iterator;
146  typedef typename _view_t::y_iterator xy_y_iterator;
148  typedef typename _const_view_t::y_iterator const_y_iterator;
150  typedef typename ndarray::Array<PixelT, 2, 1> Array;
152  typedef typename ndarray::Array<PixelT const, 2, 1> ConstArray;
153 
154  template <typename OtherPixelT>
155  friend class ImageBase; // needed by generalised copy constructors
156 
158  template <typename SinglePixelT>
159  static SinglePixel PixelCast(SinglePixelT rhs) {
160  return SinglePixel(rhs);
161  }
162  //
163  // DecoratedImage needs enough access to ImageBase to read data from disk; we might be able to design
164  // around this
165  //
166  template <typename>
167  friend class DecoratedImage;
168  template <typename, typename, typename>
169  friend class MaskedImage;
182  explicit ImageBase(const lsst::geom::Box2I& bbox);
193  ImageBase(const ImageBase& src, const bool deep = false);
209  explicit ImageBase(const ImageBase& src, const lsst::geom::Box2I& bbox, const ImageOrigin origin = PARENT,
210  const bool deep = false);
217  template <typename OtherPixelT>
218  ImageBase(const ImageBase<OtherPixelT>& rhs, const bool deep) : lsst::daf::base::Citizen(typeid(this)) {
219  if (!deep) {
221  "Only deep copies are permitted for ImageBases with different pixel types");
222  }
223 
224  ImageBase<PixelT> tmp(rhs.getBBox());
225  copy_and_convert_pixels(rhs._gilView, tmp._gilView); // from boost::gil
226 
227  using std::swap; // See Meyers, Effective C++, Item 25
228  ImageBase<PixelT>::swap(tmp); // See Meyers, Effective C++, Items 11 and 43
229  }
230 
240  explicit ImageBase(Array const& array, bool deep = false,
242 
243  virtual ~ImageBase() = default;
252  ImageBase& operator=(const ImageBase& rhs);
253  ImageBase& operator=(ImageBase&& rhs);
255  ImageBase& operator=(const PixelT rhs);
261  ImageBase& operator<<=(const ImageBase& rhs);
262 
274  void assign(ImageBase const& rhs, lsst::geom::Box2I const& bbox = lsst::geom::Box2I(),
275  ImageOrigin origin = PARENT);
276 
278 
282  PixelReference operator()(int x, int y);
285  PixelReference operator()(int x, int y, CheckIndices const&);
287  PixelConstReference operator()(int x, int y) const;
289  PixelConstReference operator()(int x, int y, CheckIndices const&) const;
290 
291  PixelConstReference get0(int x, int y) const { return operator()(x - getX0(), y - getY0()); }
292  PixelConstReference get0(int x, int y, CheckIndices const& check) const {
293  return operator()(x - getX0(), y - getY0(), check);
294  }
295  void set0(int x, int y, const PixelT v) { operator()(x - getX0(), y - getY0()) = v; }
296  void set0(int x, int y, const PixelT v, CheckIndices const& check) {
297  operator()(x - getX0(), y - getY0(), check) = v;
298  }
300 
302  PixelReference get(lsst::geom::Point2I const& index, ImageOrigin origin);
303 
305  PixelConstReference get(lsst::geom::Point2I const& index, ImageOrigin origin) const;
306 
308  PixelReference operator[](lsst::geom::Point2I const& index) { return get(index, PARENT); }
309 
311  PixelConstReference operator[](lsst::geom::Point2I const& index) const { return get(index, PARENT); }
312 
314  int getWidth() const { return _gilView.width(); }
316  int getHeight() const { return _gilView.height(); }
324  int getX0() const { return _origin.getX(); }
332  int getY0() const { return _origin.getY(); }
333 
341  lsst::geom::Point2I getXY0() const { return _origin; }
342 
349  double const pos,
350  lsst::afw::image::xOrY const xy
351  ) const {
352  double const fullIndex = pos - PixelZeroPos - (xy == X ? getX0() : getY0());
353  int const roundedIndex = static_cast<int>(fullIndex + 0.5);
354  double const residual = fullIndex - roundedIndex;
355  return std::pair<int, double>(roundedIndex, residual);
356  }
357 
367  inline double indexToPosition(double ind,
368  lsst::afw::image::xOrY const xy
369  ) const {
370  return ind + PixelZeroPos + (xy == X ? getX0() : getY0());
371  }
372 
374  lsst::geom::Extent2I getDimensions() const { return lsst::geom::Extent2I(getWidth(), getHeight()); }
375 
376  void swap(ImageBase& rhs);
377 
378  Array getArray();
379  ConstArray getArray() const;
380  //
381  // Iterators and Locators
382  //
388  iterator begin() const;
390  iterator end() const;
392  reverse_iterator rbegin() const;
394  reverse_iterator rend() const;
396  iterator at(int x, int y) const;
397 
405  fast_iterator begin(bool contiguous) const;
413  fast_iterator end(bool contiguous) const;
414 
419  x_iterator row_begin(int y) const { return _gilView.row_begin(y); }
420 
422  x_iterator row_end(int y) const { return _gilView.row_end(y); }
423 
425  x_iterator x_at(int x, int y) const { return _gilView.x_at(x, y); }
426 
431  y_iterator col_begin(int x) const { return _gilView.col_begin(x); }
432 
434  y_iterator col_end(int x) const { return _gilView.col_end(x); }
435 
437  y_iterator y_at(int x, int y) const { return _gilView.y_at(x, y); }
438 
443  xy_locator xy_at(int x, int y) const { return xy_locator(_gilView.xy_at(x, y)); }
452  void setXY0(lsst::geom::Point2I const origin) { _origin = origin; }
461  void setXY0(int const x0, int const y0) { setXY0(lsst::geom::Point2I(x0, y0)); }
462 
464  if (origin == PARENT) {
465  return lsst::geom::Box2I(_origin, getDimensions());
466  } else
467  return lsst::geom::Box2I(lsst::geom::Point2I(0, 0), getDimensions());
468  }
469 
470 private:
471  lsst::geom::Point2I _origin;
472  Manager::Ptr _manager;
473  _view_t _gilView;
474 
475  // oring of ImageBase in some larger image as returned to and manipulated
476  // by the user
477 
478 protected:
479  static _view_t _allocateView(lsst::geom::Extent2I const& dimensions, Manager::Ptr& manager);
480  static _view_t _makeSubView(lsst::geom::Extent2I const& dimensions, lsst::geom::Extent2I const& offset,
481  const _view_t& view);
482 
483  _view_t _getRawView() const { return _gilView; }
484 
485  inline bool isContiguous() const { return begin() + getWidth() * getHeight() == end(); }
486 };
487 
488 template <typename PixelT>
490 
491 // Inline template definitions
492 
493 template <typename PixelT>
495  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
496  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
497  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
498  this->_manager);
499 }
500 
501 template <typename PixelT>
503  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
504  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
505  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
506  this->_manager);
507 }
508 
509 } // namespace image
510 } // namespace afw
511 } // namespace lsst
512 
513 #endif
boost::gil::channel_traits< PixelT >::reference type
reference type
Definition: ImageBase.h:87
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
_const_view_t::reverse_iterator const_reverse_iterator
An STL compliant const reverse iterator.
Definition: ImageBase.h:132
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Definition: ImageBase.h:138
ConstReference< PixelT >::type PixelConstReference
A ConstReference to a PixelT.
Definition: ImageBase.h:120
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
Definition: ImageBase.h:130
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:348
_view_t::x_iterator xy_x_iterator
An iterator for traversing the pixels in a row, created from an xy_locator.
Definition: ImageBase.h:140
void set0(int x, int y, const PixelT v, CheckIndices const &check)
Definition: ImageBase.h:296
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Definition: Image.cc:267
detail::basic_tag image_category
trait class to identify type of image
Definition: ImageBase.h:111
T swap(T... args)
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:316
table::Key< int > b
_const_view_t::x_iterator const_x_iterator
A const iterator for traversing the pixels in a row.
Definition: ImageBase.h:142
PixelT Pixel
A pixel in this ImageBase.
Definition: ImageBase.h:116
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:134
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:452
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:324
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:103
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:419
Fits * fits
Definition: FitsWriter.cc:90
_const_view_t::iterator const_iterator
An STL compliant const iterator.
Definition: ImageBase.h:128
STL class.
_view_t::iterator iterator
An STL compliant iterator.
Definition: ImageBase.h:126
y_iterator y_at(int x, int y) const
Return an y_iterator to the point (x, y) in the image.
Definition: ImageBase.h:437
y_iterator col_begin(int x) const
Return an y_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:431
A base class for image defects.
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:463
void set0(int x, int y, const PixelT v)
Definition: ImageBase.h:295
PixelReference operator[](lsst::geom::Point2I const &index)
Return a reference to a single pixel in PARENT coordinates (with no bounds check).
Definition: ImageBase.h:308
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:74
_view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
Definition: ImageBase.h:144
traits class for image categories
Definition: ImageBase.h:67
A class used to request that array accesses be checked.
Definition: ImageBase.h:75
y_iterator col_end(int x) const
Return an y_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:434
table::Box2IKey bbox
Definition: Detector.cc:169
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:311
double x
ndarray::Array< PixelT const, 2, 1 > ConstArray
An immutable ndarray representation of the image.
Definition: ImageBase.h:152
std::shared_ptr< RecordT > src
Definition: Match.cc:48
void setXY0(int const x0, int const y0)
Set the ImageBase&#39;s origin.
Definition: ImageBase.h:461
_view_t::xy_locator::const_t const_xy_locator
A const_xy_locator.
Definition: ImageBase.h:124
lsst::geom::Point2I getXY0() const
Return the image&#39;s origin.
Definition: ImageBase.h:341
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Extent< int, 2 > Extent2I
Definition: Extent.h:397
int getY0() const
Return the image&#39;s row-origin.
Definition: ImageBase.h:332
_const_view_t::y_iterator const_y_iterator
A const iterator for traversing the pixels in a column.
Definition: ImageBase.h:148
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:314
Reference< PixelT >::type PixelReference
A Reference to a PixelT.
Definition: ImageBase.h:118
static SinglePixel PixelCast(SinglePixelT rhs)
Convert a type to our SinglePixel type.
Definition: ImageBase.h:159
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:422
x_iterator x_at(int x, int y) const
Return an x_iterator to the point (x, y) in the image.
Definition: ImageBase.h:425
ImageT::image_category image_category
Definition: ImageBase.h:68
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:55
lsst::geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:374
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
PixelConstReference get0(int x, int y, CheckIndices const &check) const
Definition: ImageBase.h:292
_view_t _getRawView() const
Definition: ImageBase.h:483
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:122
ImageBase(const ImageBase< OtherPixelT > &rhs, const bool deep)
generalised copy constructor
Definition: ImageBase.h:218
ndarray::Array< PixelT, 2, 1 > Array
A mutable ndarray representation of the image.
Definition: ImageBase.h:150
xy_locator xy_at(int x, int y) const
Return an xy_locator at the point (x, y) in the image.
Definition: ImageBase.h:443
An integer coordinate rectangle.
Definition: Box.h:54
PixelConstReference get0(int x, int y) const
Definition: ImageBase.h:291
PixelT SinglePixel
A single Pixel of the same type as those in the ImageBase.
Definition: ImageBase.h:114
CheckIndices(bool check=true)
Definition: ImageBase.h:77
double indexToPosition(double ind, lsst::afw::image::xOrY const xy) const
Convert image index to image position.
Definition: ImageBase.h:367
std::string const wcsNameForXY0
Definition: ImageBase.h:71
int end
boost::gil::channel_traits< PixelT >::const_reference type
const reference type
Definition: ImageBase.h:92
A container for an Image and its associated metadata.
Definition: Image.h:407
metafunction to extract reference type from PixelT
Definition: ImageBase.h:86
_view_t::y_iterator xy_y_iterator
An iterator for traversing the pixels in a row, created from an xy_locator.
Definition: ImageBase.h:146
metafunction to extract const reference type from PixelT
Definition: ImageBase.h:91