lsst.afw  14.0-42-g99f9a3040+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/afw/geom/Extent.h"
40 #include "lsst/afw/geom/Box.h"
41 #include "lsst/afw/geom/Point.h"
42 #include "lsst/afw/image/lsstGil.h"
44 #include "lsst/afw/math/Function.h"
45 #include "lsst/daf/base.h"
46 #include "lsst/daf/base/Citizen.h"
47 #include "lsst/pex/exceptions.h"
48 #include "ndarray.h"
49 
50 namespace lsst {
51 namespace afw {
52 
53 namespace fits {
54 class Fits;
55 class MemFileManager;
56 struct ImageWriteOptions;
57 }
58 
59 namespace image {
60 namespace detail {
61 //
62 // Traits for image types
63 //
65 struct basic_tag {};
67 struct Image_tag : public basic_tag {};
69 template <typename ImageT>
70 struct image_traits {
71  typedef typename ImageT::image_category image_category;
72 };
73 //
74 std::string const wcsNameForXY0 = "A"; // the name of the WCS to use to save (X0, Y0) to FITS files; e.g. "A"
75 }
76 
78 class CheckIndices {
79 public:
80  explicit CheckIndices(bool check = true) : _check(check) {}
81  operator bool() const { return _check; }
82 
83 private:
84  bool _check;
85 };
86 
88 template <typename PixelT>
89 struct Reference {
90  typedef typename boost::gil::channel_traits<PixelT>::reference type;
91 };
93 template <typename PixelT>
95  typedef typename boost::gil::channel_traits<PixelT>::const_reference type;
96 };
97 
99 
101 //
102 // You are not expected to use this class directly in your own code; use one of the
103 // specialised subclasses
104 //
105 template <typename PixelT>
107 private:
108  typedef typename lsst::afw::image::detail::types_traits<PixelT>::view_t _view_t;
109  typedef typename lsst::afw::image::detail::types_traits<PixelT>::const_view_t _const_view_t;
110 
111  typedef ndarray::Manager Manager;
112 
113 public:
115 
117  typedef PixelT SinglePixel;
119  typedef PixelT Pixel;
125  typedef typename _view_t::xy_locator xy_locator;
127  typedef typename _view_t::xy_locator::const_t const_xy_locator;
129  typedef typename _view_t::iterator iterator;
131  typedef typename _const_view_t::iterator const_iterator;
133  typedef typename _view_t::reverse_iterator reverse_iterator;
135  typedef typename _const_view_t::reverse_iterator const_reverse_iterator;
137  typedef typename _view_t::x_iterator x_iterator;
141  typedef x_iterator fast_iterator;
143  typedef typename _view_t::x_iterator xy_x_iterator;
145  typedef typename _const_view_t::x_iterator const_x_iterator;
147  typedef typename _view_t::y_iterator y_iterator;
149  typedef typename _view_t::y_iterator xy_y_iterator;
151  typedef typename _const_view_t::y_iterator const_y_iterator;
153  typedef typename ndarray::Array<PixelT, 2, 1> Array;
155  typedef typename ndarray::Array<PixelT const, 2, 1> ConstArray;
156 
157  template <typename OtherPixelT>
158  friend class ImageBase; // needed by generalised copy constructors
159 
161  template <typename SinglePixelT>
162  static SinglePixel PixelCast(SinglePixelT rhs) {
163  return SinglePixel(rhs);
164  }
165  //
166  // DecoratedImage needs enough access to ImageBase to read data from disk; we might be able to design
167  // around this
168  //
169  template <typename>
170  friend class DecoratedImage;
171  template <typename, typename, typename>
172  friend class MaskedImage;
179  explicit ImageBase(const geom::Extent2I& dimensions = geom::Extent2I());
185  explicit ImageBase(const geom::Box2I& bbox);
196  ImageBase(const ImageBase& src, const bool deep = false);
197  ImageBase(ImageBase&& src);
212  explicit ImageBase(const ImageBase& src, const geom::Box2I& bbox, const ImageOrigin origin = PARENT,
213  const bool deep = false);
220  template <typename OtherPixelT>
221  ImageBase(const ImageBase<OtherPixelT>& rhs, const bool deep) : lsst::daf::base::Citizen(typeid(this)) {
222  if (!deep) {
224  "Only deep copies are permitted for ImageBases with different pixel types");
225  }
226 
227  ImageBase<PixelT> tmp(rhs.getBBox());
228  copy_and_convert_pixels(rhs._gilView, tmp._gilView); // from boost::gil
229 
230  using std::swap; // See Meyers, Effective C++, Item 25
231  ImageBase<PixelT>::swap(tmp); // See Meyers, Effective C++, Items 11 and 43
232  }
233 
243  explicit ImageBase(Array const& array, bool deep = false, geom::Point2I const& xy0 = geom::Point2I());
244 
245  virtual ~ImageBase() = default;
254  ImageBase& operator=(const ImageBase& rhs);
255  ImageBase& operator=(ImageBase&& rhs);
257  ImageBase& operator=(const PixelT rhs);
263  ImageBase& operator<<=(const ImageBase& rhs);
264 
276  void assign(ImageBase const& rhs, geom::Box2I const& bbox = geom::Box2I(), 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  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  geom::Extent2I getDimensions() const { return 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(geom::Point2I const origin) { _origin = origin; }
446  void setXY0(int const x0, int const y0) { setXY0(geom::Point2I(x0, y0)); }
447 
449  if (origin == PARENT) {
450  return geom::Box2I(_origin, getDimensions());
451  } else
452  return geom::Box2I(geom::Point2I(0, 0), getDimensions());
453  }
454 
455 private:
456  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(geom::Extent2I const& dimensions, Manager::Ptr& manager);
465  static _view_t _makeSubView(geom::Extent2I const& dimensions, 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 
477 // Inline template definitions
478 
479 template <typename PixelT>
481  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
482  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
483  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
484  this->_manager);
485 }
486 
487 template <typename PixelT>
489  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
490  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
491  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
492  this->_manager);
493 }
494 
495 
496 }
497 }
498 } // lsst::afw::image
499 
500 #endif
boost::gil::channel_traits< PixelT >::reference type
reference type
Definition: ImageBase.h:90
void setXY0(geom::Point2I const origin)
Set the ImageBase&#39;s origin.
Definition: ImageBase.h:437
_const_view_t::reverse_iterator const_reverse_iterator
An STL compliant const reverse iterator.
Definition: ImageBase.h:135
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Definition: ImageBase.h:141
ConstReference< PixelT >::type PixelConstReference
A ConstReference to a PixelT.
Definition: ImageBase.h:123
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
Definition: ImageBase.h:133
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:143
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:235
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:448
detail::basic_tag image_category
trait class to identify type of image
Definition: ImageBase.h:114
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:145
PixelT Pixel
A pixel in this ImageBase.
Definition: ImageBase.h:119
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:137
Extent< int, 2 > Extent2I
Definition: Extent.h:380
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:106
x_iterator row_begin(int y) const
Return an x_iterator to the start of the y&#39;th row.
Definition: ImageBase.h:404
_const_view_t::iterator const_iterator
An STL compliant const iterator.
Definition: ImageBase.h:131
An integer coordinate rectangle.
Definition: Box.h:55
STL class.
_view_t::iterator iterator
An STL compliant iterator.
Definition: ImageBase.h:129
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
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:359
A base class for image defects.
int end
void set0(int x, int y, const PixelT v)
Definition: ImageBase.h:293
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:147
table::Key< int > b
traits class for image categories
Definition: ImageBase.h:70
A class used to request that array accesses be checked.
Definition: ImageBase.h:78
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:155
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:127
#define LSST_EXCEPT(type,...)
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:151
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:121
static SinglePixel PixelCast(SinglePixelT rhs)
Convert a type to our SinglePixel type.
Definition: ImageBase.h:162
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:71
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:125
ImageBase(const ImageBase< OtherPixelT > &rhs, const bool deep)
generalised copy constructor
Definition: ImageBase.h:221
ndarray::Array< PixelT, 2, 1 > Array
A mutable ndarray representation of the image.
Definition: ImageBase.h:153
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:117
CheckIndices(bool check=true)
Definition: ImageBase.h:80
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:74
boost::gil::channel_traits< PixelT >::const_reference type
const reference type
Definition: ImageBase.h:95
A container for an Image and its associated metadata.
Definition: Image.h:394
metafunction to extract reference type from PixelT
Definition: ImageBase.h:89
_view_t::y_iterator xy_y_iterator
An iterator for traversing the pixels in a row, created from an xy_locator.
Definition: ImageBase.h:149
geom::Point2I getXY0() const
Return the image&#39;s origin.
Definition: ImageBase.h:326
metafunction to extract const reference type from PixelT
Definition: ImageBase.h:94