lsst.afw  14.0-35-g29d3a87a7
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);
211  explicit ImageBase(const ImageBase& src, const geom::Box2I& bbox, const ImageOrigin origin = PARENT,
212  const bool deep = false);
219  template <typename OtherPixelT>
220  ImageBase(const ImageBase<OtherPixelT>& rhs, const bool deep) : lsst::daf::base::Citizen(typeid(this)) {
221  if (!deep) {
223  "Only deep copies are permitted for ImageBases with different pixel types");
224  }
225 
226  ImageBase<PixelT> tmp(rhs.getBBox());
227  copy_and_convert_pixels(rhs._gilView, tmp._gilView); // from boost::gil
228 
229  using std::swap; // See Meyers, Effective C++, Item 25
230  ImageBase<PixelT>::swap(tmp); // See Meyers, Effective C++, Items 11 and 43
231  }
232 
242  explicit ImageBase(Array const& array, bool deep = false, geom::Point2I const& xy0 = geom::Point2I());
243 
244  virtual ~ImageBase() {}
253  ImageBase& operator=(const ImageBase& rhs);
255  ImageBase& operator=(const PixelT rhs);
261  ImageBase& operator<<=(const ImageBase& rhs);
262 
274  void assign(ImageBase const& rhs, geom::Box2I const& bbox = geom::Box2I(), ImageOrigin origin = PARENT);
275  //
276  // Operators etc.
277  //
279  PixelReference operator()(int x, int y);
281  PixelReference operator()(int x, int y, CheckIndices const&);
283  PixelConstReference operator()(int x, int y) const;
285  PixelConstReference operator()(int x, int y, CheckIndices const&) const;
286 
287  PixelConstReference get0(int x, int y) const { return operator()(x - getX0(), y - getY0()); }
288  PixelConstReference get0(int x, int y, CheckIndices const& check) const {
289  return operator()(x - getX0(), y - getY0(), check);
290  }
291  void set0(int x, int y, const PixelT v) { operator()(x - getX0(), y - getY0()) = v; }
292  void set0(int x, int y, const PixelT v, CheckIndices const& check) {
293  operator()(x - getX0(), y - getY0(), check) = v;
294  }
295 
297  int getWidth() const { return _gilView.width(); }
299  int getHeight() const { return _gilView.height(); }
307  int getX0() const { return _origin.getX(); }
315  int getY0() const { return _origin.getY(); }
316 
324  geom::Point2I getXY0() const { return _origin; }
325 
332  double const pos,
333  lsst::afw::image::xOrY const xy
334  ) const {
335  double const fullIndex = pos - PixelZeroPos - (xy == X ? getX0() : getY0());
336  int const roundedIndex = static_cast<int>(fullIndex + 0.5);
337  double const residual = fullIndex - roundedIndex;
338  return std::pair<int, double>(roundedIndex, residual);
339  }
340 
350  inline double indexToPosition(double ind,
351  lsst::afw::image::xOrY const xy
352  ) const {
353  return ind + PixelZeroPos + (xy == X ? getX0() : getY0());
354  }
355 
357  geom::Extent2I getDimensions() const { return geom::Extent2I(getWidth(), getHeight()); }
358 
359  void swap(ImageBase& rhs);
360 
361  Array getArray();
362  ConstArray getArray() const;
363  //
364  // Iterators and Locators
365  //
371  iterator begin() const;
373  iterator end() const;
375  reverse_iterator rbegin() const;
377  reverse_iterator rend() const;
379  iterator at(int x, int y) const;
380 
388  fast_iterator begin(bool contiguous) const;
396  fast_iterator end(bool contiguous) const;
397 
402  x_iterator row_begin(int y) const { return _gilView.row_begin(y); }
403 
405  x_iterator row_end(int y) const { return _gilView.row_end(y); }
406 
408  x_iterator x_at(int x, int y) const { return _gilView.x_at(x, y); }
409 
414  y_iterator col_begin(int x) const { return _gilView.col_begin(x); }
415 
417  y_iterator col_end(int x) const { return _gilView.col_end(x); }
418 
420  y_iterator y_at(int x, int y) const { return _gilView.y_at(x, y); }
421 
426  xy_locator xy_at(int x, int y) const { return xy_locator(_gilView.xy_at(x, y)); }
435  void setXY0(geom::Point2I const origin) { _origin = origin; }
444  void setXY0(int const x0, int const y0) { setXY0(geom::Point2I(x0, y0)); }
445 
447  if (origin == PARENT) {
448  return geom::Box2I(_origin, getDimensions());
449  } else
450  return geom::Box2I(geom::Point2I(0, 0), getDimensions());
451  }
452 
453 private:
454  geom::Point2I _origin;
455  Manager::Ptr _manager;
456  _view_t _gilView;
457 
458  // oring of ImageBase in some larger image as returned to and manipulated
459  // by the user
460 
461 protected:
462  static _view_t _allocateView(geom::Extent2I const& dimensions, Manager::Ptr& manager);
463  static _view_t _makeSubView(geom::Extent2I const& dimensions, geom::Extent2I const& offset,
464  const _view_t& view);
465 
466  _view_t _getRawView() const { return _gilView; }
467 
468  inline bool isContiguous() const { return begin() + getWidth() * getHeight() == end(); }
469 };
470 
471 template <typename PixelT>
473 
474 
475 // Inline template definitions
476 
477 template <typename PixelT>
479  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
480  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
481  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
482  this->_manager);
483 }
484 
485 template <typename PixelT>
487  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
488  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
489  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
490  this->_manager);
491 }
492 
493 
494 }
495 }
496 } // lsst::afw::image
497 
498 #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:435
_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:331
_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:292
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Definition: Image.cc:227
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: ImageBase.h:446
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:299
_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:341
int getX0() const
Return the image&#39;s column-origin.
Definition: ImageBase.h:307
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:402
_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:420
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:414
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:357
A base class for image defects.
int end
void set0(int x, int y, const PixelT v)
Definition: ImageBase.h:291
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:417
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:444
_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:315
_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:297
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:405
x_iterator x_at(int x, int y) const
Return an x_iterator to the point (x, y) in the image.
Definition: ImageBase.h:408
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:288
_view_t _getRawView() const
Definition: ImageBase.h:466
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:220
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:426
PixelConstReference get0(int x, int y) const
Definition: ImageBase.h:287
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:350
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:392
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:324
metafunction to extract const reference type from PixelT
Definition: ImageBase.h:94