lsst.afw  16.0-12-gf08c31002
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.h"
44 #include "lsst/daf/base/Citizen.h"
45 #include "lsst/pex/exceptions.h"
46 #include "ndarray.h"
47 
48 namespace lsst {
49 namespace afw {
50 
51 namespace fits {
52 class Fits;
53 class MemFileManager;
54 struct ImageWriteOptions;
55 } // namespace fits
56 
57 namespace image {
58 namespace detail {
59 //
60 // Traits for image types
61 //
63 struct basic_tag {};
65 struct Image_tag : public basic_tag {};
67 template <typename ImageT>
68 struct image_traits {
69  typedef typename ImageT::image_category image_category;
70 };
71 //
72 std::string const wcsNameForXY0 = "A"; // the name of the WCS to use to save (X0, Y0) to FITS files; e.g. "A"
73 } // namespace detail
74 
76 class CheckIndices {
77 public:
78  explicit CheckIndices(bool check = true) : _check(check) {}
79  operator bool() const { return _check; }
80 
81 private:
82  bool _check;
83 };
84 
86 template <typename PixelT>
87 struct Reference {
88  typedef typename boost::gil::channel_traits<PixelT>::reference type;
89 };
91 template <typename PixelT>
93  typedef typename boost::gil::channel_traits<PixelT>::const_reference type;
94 };
95 
97 
99 //
100 // You are not expected to use this class directly in your own code; use one of the
101 // specialised subclasses
102 //
103 template <typename PixelT>
105 private:
106  typedef typename lsst::afw::image::detail::types_traits<PixelT>::view_t _view_t;
107  typedef typename lsst::afw::image::detail::types_traits<PixelT>::const_view_t _const_view_t;
108 
109  typedef ndarray::Manager Manager;
110 
111 public:
113 
115  typedef PixelT SinglePixel;
117  typedef PixelT Pixel;
123  typedef typename _view_t::xy_locator xy_locator;
125  typedef typename _view_t::xy_locator::const_t const_xy_locator;
127  typedef typename _view_t::iterator iterator;
129  typedef typename _const_view_t::iterator const_iterator;
131  typedef typename _view_t::reverse_iterator reverse_iterator;
133  typedef typename _const_view_t::reverse_iterator const_reverse_iterator;
135  typedef typename _view_t::x_iterator x_iterator;
139  typedef x_iterator fast_iterator;
141  typedef typename _view_t::x_iterator xy_x_iterator;
143  typedef typename _const_view_t::x_iterator const_x_iterator;
145  typedef typename _view_t::y_iterator y_iterator;
147  typedef typename _view_t::y_iterator xy_y_iterator;
149  typedef typename _const_view_t::y_iterator const_y_iterator;
151  typedef typename ndarray::Array<PixelT, 2, 1> Array;
153  typedef typename ndarray::Array<PixelT const, 2, 1> ConstArray;
154 
155  template <typename OtherPixelT>
156  friend class ImageBase; // needed by generalised copy constructors
157 
159  template <typename SinglePixelT>
160  static SinglePixel PixelCast(SinglePixelT rhs) {
161  return SinglePixel(rhs);
162  }
163  //
164  // DecoratedImage needs enough access to ImageBase to read data from disk; we might be able to design
165  // around this
166  //
167  template <typename>
168  friend class DecoratedImage;
169  template <typename, typename, typename>
170  friend class MaskedImage;
183  explicit ImageBase(const lsst::geom::Box2I& bbox);
194  ImageBase(const ImageBase& src, const bool deep = false);
210  explicit ImageBase(const ImageBase& src, const lsst::geom::Box2I& bbox, const ImageOrigin origin = PARENT,
211  const bool deep = false);
218  template <typename OtherPixelT>
219  ImageBase(const ImageBase<OtherPixelT>& rhs, const bool deep) : lsst::daf::base::Citizen(typeid(this)) {
220  if (!deep) {
222  "Only deep copies are permitted for ImageBases with different pixel types");
223  }
224 
225  ImageBase<PixelT> tmp(rhs.getBBox());
226  copy_and_convert_pixels(rhs._gilView, tmp._gilView); // from boost::gil
227 
228  using std::swap; // See Meyers, Effective C++, Item 25
229  ImageBase<PixelT>::swap(tmp); // See Meyers, Effective C++, Items 11 and 43
230  }
231 
241  explicit ImageBase(Array const& array, bool deep = false,
243 
244  virtual ~ImageBase() = default;
253  ImageBase& operator=(const ImageBase& rhs);
254  ImageBase& operator=(ImageBase&& rhs);
256  ImageBase& operator=(const PixelT rhs);
262  ImageBase& operator<<=(const ImageBase& rhs);
263 
275  void assign(ImageBase const& rhs, lsst::geom::Box2I const& bbox = lsst::geom::Box2I(),
276  ImageOrigin origin = PARENT);
277 
279 
283  PixelReference operator()(int x, int y);
286  PixelReference operator()(int x, int y, CheckIndices const&);
288  PixelConstReference operator()(int x, int y) const;
290  PixelConstReference operator()(int x, int y, CheckIndices const&) const;
291 
292  PixelConstReference get0(int x, int y) const { return operator()(x - getX0(), y - getY0()); }
293  PixelConstReference get0(int x, int y, CheckIndices const& check) const {
294  return operator()(x - getX0(), y - getY0(), check);
295  }
296  void set0(int x, int y, const PixelT v) { operator()(x - getX0(), y - getY0()) = v; }
297  void set0(int x, int y, const PixelT v, CheckIndices const& check) {
298  operator()(x - getX0(), y - getY0(), check) = v;
299  }
301 
303  PixelReference get(lsst::geom::Point2I const & index, ImageOrigin origin);
304 
306  PixelConstReference get(lsst::geom::Point2I const & index, ImageOrigin origin) const;
307 
309  PixelReference operator[](lsst::geom::Point2I const & index) {
310  return get(index, PARENT);
311  }
312 
314  PixelConstReference operator[](lsst::geom::Point2I const & index) const {
315  return get(index, PARENT);
316  }
317 
319  int getWidth() const { return _gilView.width(); }
321  int getHeight() const { return _gilView.height(); }
329  int getX0() const { return _origin.getX(); }
337  int getY0() const { return _origin.getY(); }
338 
346  lsst::geom::Point2I getXY0() const { return _origin; }
347 
354  double const pos,
355  lsst::afw::image::xOrY const xy
356  ) const {
357  double const fullIndex = pos - PixelZeroPos - (xy == X ? getX0() : getY0());
358  int const roundedIndex = static_cast<int>(fullIndex + 0.5);
359  double const residual = fullIndex - roundedIndex;
360  return std::pair<int, double>(roundedIndex, residual);
361  }
362 
372  inline double indexToPosition(double ind,
373  lsst::afw::image::xOrY const xy
374  ) const {
375  return ind + PixelZeroPos + (xy == X ? getX0() : getY0());
376  }
377 
379  lsst::geom::Extent2I getDimensions() const { return lsst::geom::Extent2I(getWidth(), getHeight()); }
380 
381  void swap(ImageBase& rhs);
382 
383  Array getArray();
384  ConstArray getArray() const;
385  //
386  // Iterators and Locators
387  //
393  iterator begin() const;
395  iterator end() const;
397  reverse_iterator rbegin() const;
399  reverse_iterator rend() const;
401  iterator at(int x, int y) const;
402 
410  fast_iterator begin(bool contiguous) const;
418  fast_iterator end(bool contiguous) const;
419 
424  x_iterator row_begin(int y) const { return _gilView.row_begin(y); }
425 
427  x_iterator row_end(int y) const { return _gilView.row_end(y); }
428 
430  x_iterator x_at(int x, int y) const { return _gilView.x_at(x, y); }
431 
436  y_iterator col_begin(int x) const { return _gilView.col_begin(x); }
437 
439  y_iterator col_end(int x) const { return _gilView.col_end(x); }
440 
442  y_iterator y_at(int x, int y) const { return _gilView.y_at(x, y); }
443 
448  xy_locator xy_at(int x, int y) const { return xy_locator(_gilView.xy_at(x, y)); }
457  void setXY0(lsst::geom::Point2I const origin) { _origin = origin; }
466  void setXY0(int const x0, int const y0) { setXY0(lsst::geom::Point2I(x0, y0)); }
467 
469  if (origin == PARENT) {
470  return lsst::geom::Box2I(_origin, getDimensions());
471  } else
472  return lsst::geom::Box2I(lsst::geom::Point2I(0, 0), getDimensions());
473  }
474 
475 private:
476  lsst::geom::Point2I _origin;
477  Manager::Ptr _manager;
478  _view_t _gilView;
479 
480  // oring of ImageBase in some larger image as returned to and manipulated
481  // by the user
482 
483 protected:
484  static _view_t _allocateView(lsst::geom::Extent2I const& dimensions, Manager::Ptr& manager);
485  static _view_t _makeSubView(lsst::geom::Extent2I const& dimensions, lsst::geom::Extent2I const& offset,
486  const _view_t& view);
487 
488  _view_t _getRawView() const { return _gilView; }
489 
490  inline bool isContiguous() const { return begin() + getWidth() * getHeight() == end(); }
491 };
492 
493 template <typename PixelT>
495 
496 // Inline template definitions
497 
498 template <typename PixelT>
500  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
501  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
502  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
503  this->_manager);
504 }
505 
506 template <typename PixelT>
508  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
509  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
510  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
511  this->_manager);
512 }
513 
514 } // namespace image
515 } // namespace afw
516 } // namespace lsst
517 
518 #endif
boost::gil::channel_traits< PixelT >::reference type
reference type
Definition: ImageBase.h:88
_const_view_t::reverse_iterator const_reverse_iterator
An STL compliant const reverse iterator.
Definition: ImageBase.h:133
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Definition: ImageBase.h:139
ConstReference< PixelT >::type PixelConstReference
A ConstReference to a PixelT.
Definition: ImageBase.h:121
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
Definition: ImageBase.h:131
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:353
_view_t::x_iterator xy_x_iterator
An iterator for traversing the pixels in a row, created from an xy_locator.
Definition: ImageBase.h:141
void set0(int x, int y, const PixelT v, CheckIndices const &check)
Definition: ImageBase.h:297
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Definition: Image.cc:262
detail::basic_tag image_category
trait class to identify type of image
Definition: ImageBase.h:112
T swap(T... args)
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:321
_const_view_t::x_iterator const_x_iterator
A const iterator for traversing the pixels in a row.
Definition: ImageBase.h:143
PixelT Pixel
A pixel in this ImageBase.
Definition: ImageBase.h:117
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:135
void setXY0(lsst::geom::Point2I const origin)
Set the ImageBase&#39;s origin.
Definition: ImageBase.h:457
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:329
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:104
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:424
Fits * fits
Definition: FitsWriter.cc:90
_const_view_t::iterator const_iterator
An STL compliant const iterator.
Definition: ImageBase.h:129
STL class.
_view_t::iterator iterator
An STL compliant iterator.
Definition: ImageBase.h:127
y_iterator y_at(int x, int y) const
Return an y_iterator to the point (x, y) in the image.
Definition: ImageBase.h:442
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
y_iterator col_begin(int x) const
Return an y_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:436
A base class for image defects.
int end
Definition: BoundedField.cc:99
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:468
void set0(int x, int y, const PixelT v)
Definition: ImageBase.h:296
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:309
A class to manipulate images, masks, and variance as a single object.
Definition: MaskedImage.h:81
_view_t::y_iterator y_iterator
An iterator for traversing the pixels in a column.
Definition: ImageBase.h:145
table::Key< int > b
traits class for image categories
Definition: ImageBase.h:68
A class used to request that array accesses be checked.
Definition: ImageBase.h:76
y_iterator col_end(int x) const
Return an y_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:439
double x
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:314
ndarray::Array< PixelT const, 2, 1 > ConstArray
An immutable ndarray representation of the image.
Definition: ImageBase.h:153
void setXY0(int const x0, int const y0)
Set the ImageBase&#39;s origin.
Definition: ImageBase.h:466
_view_t::xy_locator::const_t const_xy_locator
A const_xy_locator.
Definition: ImageBase.h:125
lsst::geom::Point2I getXY0() const
Return the image&#39;s origin.
Definition: ImageBase.h:346
#define LSST_EXCEPT(type,...)
Extent< int, 2 > Extent2I
int getY0() const
Return the image&#39;s row-origin.
Definition: ImageBase.h:337
_const_view_t::y_iterator const_y_iterator
A const iterator for traversing the pixels in a column.
Definition: ImageBase.h:149
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:319
Reference< PixelT >::type PixelReference
A Reference to a PixelT.
Definition: ImageBase.h:119
static SinglePixel PixelCast(SinglePixelT rhs)
Convert a type to our SinglePixel type.
Definition: ImageBase.h:160
x_iterator row_end(int y) const
Return an x_iterator to the end of the y&#39;th row.
Definition: ImageBase.h:427
x_iterator x_at(int x, int y) const
Return an x_iterator to the point (x, y) in the image.
Definition: ImageBase.h:430
table::Box2IKey bbox
table::Key< int > a
ImageT::image_category image_category
Definition: ImageBase.h:69
lsst::geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:379
PixelConstReference get0(int x, int y, CheckIndices const &check) const
Definition: ImageBase.h:293
_view_t _getRawView() const
Definition: ImageBase.h:488
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:123
ImageBase(const ImageBase< OtherPixelT > &rhs, const bool deep)
generalised copy constructor
Definition: ImageBase.h:219
ndarray::Array< PixelT, 2, 1 > Array
A mutable ndarray representation of the image.
Definition: ImageBase.h:151
xy_locator xy_at(int x, int y) const
Return an xy_locator at the point (x, y) in the image.
Definition: ImageBase.h:448
PixelConstReference get0(int x, int y) const
Definition: ImageBase.h:292
PixelT SinglePixel
A single Pixel of the same type as those in the ImageBase.
Definition: ImageBase.h:115
CheckIndices(bool check=true)
Definition: ImageBase.h:78
int y
Definition: SpanSet.cc:44
double indexToPosition(double ind, lsst::afw::image::xOrY const xy) const
Convert image index to image position.
Definition: ImageBase.h:372
std::string const wcsNameForXY0
Definition: ImageBase.h:72
boost::gil::channel_traits< PixelT >::const_reference type
const reference type
Definition: ImageBase.h:93
A container for an Image and its associated metadata.
Definition: Image.h:409
metafunction to extract reference type from PixelT
Definition: ImageBase.h:87
_view_t::y_iterator xy_y_iterator
An iterator for traversing the pixels in a row, created from an xy_locator.
Definition: ImageBase.h:147
metafunction to extract const reference type from PixelT
Definition: ImageBase.h:92