lsst.afw  16.0-9-gdf7162dbc+1
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  //
278  // Operators etc.
279  //
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 
289  PixelConstReference get0(int x, int y) const { return operator()(x - getX0(), y - getY0()); }
290  PixelConstReference get0(int x, int y, CheckIndices const& check) const {
291  return operator()(x - getX0(), y - getY0(), check);
292  }
293  void set0(int x, int y, const PixelT v) { operator()(x - getX0(), y - getY0()) = v; }
294  void set0(int x, int y, const PixelT v, CheckIndices const& check) {
295  operator()(x - getX0(), y - getY0(), check) = v;
296  }
297 
299  int getWidth() const { return _gilView.width(); }
301  int getHeight() const { return _gilView.height(); }
309  int getX0() const { return _origin.getX(); }
317  int getY0() const { return _origin.getY(); }
318 
326  lsst::geom::Point2I getXY0() const { return _origin; }
327 
334  double const pos,
335  lsst::afw::image::xOrY const xy
336  ) const {
337  double const fullIndex = pos - PixelZeroPos - (xy == X ? getX0() : getY0());
338  int const roundedIndex = static_cast<int>(fullIndex + 0.5);
339  double const residual = fullIndex - roundedIndex;
340  return std::pair<int, double>(roundedIndex, residual);
341  }
342 
352  inline double indexToPosition(double ind,
353  lsst::afw::image::xOrY const xy
354  ) const {
355  return ind + PixelZeroPos + (xy == X ? getX0() : getY0());
356  }
357 
359  lsst::geom::Extent2I getDimensions() const { return lsst::geom::Extent2I(getWidth(), getHeight()); }
360 
361  void swap(ImageBase& rhs);
362 
363  Array getArray();
364  ConstArray getArray() const;
365  //
366  // Iterators and Locators
367  //
373  iterator begin() const;
375  iterator end() const;
377  reverse_iterator rbegin() const;
379  reverse_iterator rend() const;
381  iterator at(int x, int y) const;
382 
390  fast_iterator begin(bool contiguous) const;
398  fast_iterator end(bool contiguous) const;
399 
404  x_iterator row_begin(int y) const { return _gilView.row_begin(y); }
405 
407  x_iterator row_end(int y) const { return _gilView.row_end(y); }
408 
410  x_iterator x_at(int x, int y) const { return _gilView.x_at(x, y); }
411 
416  y_iterator col_begin(int x) const { return _gilView.col_begin(x); }
417 
419  y_iterator col_end(int x) const { return _gilView.col_end(x); }
420 
422  y_iterator y_at(int x, int y) const { return _gilView.y_at(x, y); }
423 
428  xy_locator xy_at(int x, int y) const { return xy_locator(_gilView.xy_at(x, y)); }
437  void setXY0(lsst::geom::Point2I const origin) { _origin = origin; }
446  void setXY0(int const x0, int const y0) { setXY0(lsst::geom::Point2I(x0, y0)); }
447 
449  if (origin == PARENT) {
450  return lsst::geom::Box2I(_origin, getDimensions());
451  } else
452  return lsst::geom::Box2I(lsst::geom::Point2I(0, 0), getDimensions());
453  }
454 
455 private:
456  lsst::geom::Point2I _origin;
457  Manager::Ptr _manager;
458  _view_t _gilView;
459 
460  // oring of ImageBase in some larger image as returned to and manipulated
461  // by the user
462 
463 protected:
464  static _view_t _allocateView(lsst::geom::Extent2I const& dimensions, Manager::Ptr& manager);
465  static _view_t _makeSubView(lsst::geom::Extent2I const& dimensions, lsst::geom::Extent2I const& offset,
466  const _view_t& view);
467 
468  _view_t _getRawView() const { return _gilView; }
469 
470  inline bool isContiguous() const { return begin() + getWidth() * getHeight() == end(); }
471 };
472 
473 template <typename PixelT>
475 
476 // Inline template definitions
477 
478 template <typename PixelT>
480  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
481  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
482  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
483  this->_manager);
484 }
485 
486 template <typename PixelT>
488  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
489  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
490  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
491  this->_manager);
492 }
493 
494 } // namespace image
495 } // namespace afw
496 } // namespace lsst
497 
498 #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:333
_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:294
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Definition: Image.cc:237
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:301
_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:437
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:309
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:404
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:422
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:416
A base class for image defects.
int end
Definition: BoundedField.cc:99
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:448
void set0(int x, int y, const PixelT v)
Definition: ImageBase.h:293
std::shared_ptr< RecordT > src
Definition: Match.cc:48
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:419
double x
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:446
_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:326
#define LSST_EXCEPT(type,...)
Extent< int, 2 > Extent2I
int getY0() const
Return the image&#39;s row-origin.
Definition: ImageBase.h:317
_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:299
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:407
x_iterator x_at(int x, int y) const
Return an x_iterator to the point (x, y) in the image.
Definition: ImageBase.h:410
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:359
PixelConstReference get0(int x, int y, CheckIndices const &check) const
Definition: ImageBase.h:290
_view_t _getRawView() const
Definition: ImageBase.h:468
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:428
PixelConstReference get0(int x, int y) const
Definition: ImageBase.h:289
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:352
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:385
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