lsst.afw  13.0-92-g8ecf155+1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Image.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2016 AURA/LSST.
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  * Implementation for ImageBase and Image
27  */
28 #include <cstdint>
29 #include <iostream>
30 #include "boost/mpl/vector.hpp"
31 #pragma clang diagnostic push
32 #pragma clang diagnostic ignored "-Wunused-variable"
33 #pragma clang diagnostic pop
34 #include "boost/format.hpp"
35 #include "boost/filesystem/path.hpp"
36 #include "boost/gil/gil_all.hpp"
37 
38 #include "lsst/pex/exceptions.h"
39 #include "lsst/afw/image/Image.h"
41 #include "lsst/afw/image/Wcs.h"
42 #include "lsst/afw/fits.h"
45 
46 namespace lsst {
47 namespace afw {
48 namespace image {
49 
50 template <typename PixelT>
51 typename ImageBase<PixelT>::_view_t ImageBase<PixelT>::_allocateView(geom::Extent2I const& dimensions,
52  Manager::Ptr& manager) {
53  if (dimensions.getX() < 0 || dimensions.getY() < 0) {
54  throw LSST_EXCEPT(pex::exceptions::LengthError,
55  str(boost::format("Both width and height must be non-negative: %d, %d") %
56  dimensions.getX() % dimensions.getY()));
57  }
58  if (dimensions.getX() != 0 && dimensions.getY() > std::numeric_limits<int>::max() / dimensions.getX()) {
59  throw LSST_EXCEPT(pex::exceptions::LengthError,
60  str(boost::format("Image dimensions (%d x %d) too large; int overflow detected.") %
61  dimensions.getX() % dimensions.getY()));
62  }
63  std::pair<Manager::Ptr, PixelT*> r =
64  ndarray::SimpleManager<PixelT>::allocate(dimensions.getX() * dimensions.getY());
65  manager = r.first;
66  return boost::gil::interleaved_view(dimensions.getX(), dimensions.getY(),
67  (typename _view_t::value_type*)r.second,
68  dimensions.getX() * sizeof(PixelT));
69 }
70 template <typename PixelT>
71 typename ImageBase<PixelT>::_view_t ImageBase<PixelT>::_makeSubView(geom::Extent2I const& dimensions,
72  geom::Extent2I const& offset,
73  const _view_t& view) {
74  if (offset.getX() < 0 || offset.getY() < 0 || offset.getX() + dimensions.getX() > view.width() ||
75  offset.getY() + dimensions.getY() > view.height()) {
76  throw LSST_EXCEPT(pex::exceptions::LengthError,
77  (boost::format("Box2I(Point2I(%d,%d),Extent2I(%d,%d)) doesn't fit in image %dx%d") %
78  offset.getX() % offset.getY() % dimensions.getX() % dimensions.getY() %
79  view.width() % view.height())
80  .str());
81  }
82  return boost::gil::subimage_view(view, offset.getX(), offset.getY(), dimensions.getX(),
83  dimensions.getY());
84 }
85 
86 template <typename PixelT>
88  : daf::base::Citizen(typeid(this)),
89  _origin(0, 0),
90  _manager(),
91  _gilView(_allocateView(dimensions, _manager)) {}
92 
93 template <typename PixelT>
95  : daf::base::Citizen(typeid(this)),
96  _origin(bbox.getMin()),
97  _manager(),
98  _gilView(_allocateView(bbox.getDimensions(), _manager)) {}
99 
100 template <typename PixelT>
101 ImageBase<PixelT>::ImageBase(ImageBase const& rhs, bool const deep
102 
103  )
104  : daf::base::Citizen(typeid(this)),
105  _origin(rhs._origin),
106  _manager(rhs._manager),
107  _gilView(rhs._gilView) {
108  if (deep) {
109  ImageBase tmp(getBBox());
110  tmp.assign(*this); // now copy the pixels
111  swap(tmp);
112  }
113 }
114 
115 template <typename PixelT>
116 ImageBase<PixelT>::ImageBase(ImageBase const& rhs, geom::Box2I const& bbox, ImageOrigin const origin,
117  bool const deep
118 
119  )
120  : daf::base::Citizen(typeid(this)),
121  _origin((origin == PARENT) ? bbox.getMin() : rhs._origin + geom::Extent2I(bbox.getMin())),
122  _manager(rhs._manager), // reference counted pointer, don't copy pixels
123  _gilView(_makeSubView(bbox.getDimensions(), _origin - rhs._origin, rhs._gilView)) {
124  if (deep) {
125  ImageBase tmp(getBBox());
126  tmp.assign(*this); // now copy the pixels
127  swap(tmp);
128  }
129 }
130 
131 template <typename PixelT>
132 ImageBase<PixelT>::ImageBase(Array const& array, bool deep, geom::Point2I const& xy0)
133  : daf::base::Citizen(typeid(this)),
134  _origin(xy0),
135  _manager(array.getManager()),
136  _gilView(boost::gil::interleaved_view(array.template getSize<1>(), array.template getSize<0>(),
137  (typename _view_t::value_type*)array.getData(),
138  array.template getStride<0>() * sizeof(PixelT))) {
139  if (deep) {
140  ImageBase tmp(*this, true);
141  swap(tmp);
142  }
143 }
144 
145 template <typename PixelT>
147  ImageBase tmp(rhs);
148  swap(tmp); // See Meyers, Effective C++, Item 11
149 
150  return *this;
151 }
152 
153 template <typename PixelT>
155  assign(rhs);
156  return *this;
157 }
158 
159 template <typename PixelT>
160 void ImageBase<PixelT>::assign(ImageBase const& rhs, geom::Box2I const& bbox, ImageOrigin origin) {
161  auto lhsDim = bbox.isEmpty() ? getDimensions() : bbox.getDimensions();
162  if (lhsDim != rhs.getDimensions()) {
163  throw LSST_EXCEPT(pex::exceptions::LengthError,
164  (boost::format("Dimension mismatch: %dx%d v. %dx%d") % lhsDim.getX() %
165  lhsDim.getY() % rhs.getWidth() % rhs.getHeight())
166  .str());
167  }
168  if (bbox.isEmpty()) {
169  copy_pixels(rhs._gilView, _gilView);
170  } else {
171  auto lhsOff = (origin == PARENT) ? bbox.getMin() - _origin : geom::Extent2I(bbox.getMin());
172  auto lhsGilView = _makeSubView(lhsDim, lhsOff, _gilView);
173  copy_pixels(rhs._gilView, lhsGilView);
174  }
175 }
176 
177 template <typename PixelT>
179  return const_cast<typename ImageBase<PixelT>::PixelReference>(
180  static_cast<typename ImageBase<PixelT>::PixelConstReference>(_gilView(x, y)[0]));
181 }
182 
183 template <typename PixelT>
185  CheckIndices const& check) {
186  if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
187  throw LSST_EXCEPT(
188  pex::exceptions::LengthError,
189  (boost::format("Index (%d, %d) is out of range [0--%d], [0--%d]") % x % y % (getWidth() - 1) %
190  (getHeight() -
191  1)).str());
192  }
193 
194  return const_cast<typename ImageBase<PixelT>::PixelReference>(
195  static_cast<typename ImageBase<PixelT>::PixelConstReference>(_gilView(x, y)[0]));
196 }
197 
198 template <typename PixelT>
200  return _gilView(x, y)[0];
201 }
202 
203 template <typename PixelT>
205  int x, int y, CheckIndices const& check) const {
206  if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
207  throw LSST_EXCEPT(pex::exceptions::LengthError,
208  (boost::format("Index (%d, %d) is out of range [0--%d], [0--%d]") % x % y %
209  (this->getWidth() - 1) %
210  (this->getHeight() -
211  1)).str());
212  }
213 
214  return _gilView(x, y)[0];
215 }
216 
217 template <typename PixelT>
219  using std::swap; // See Meyers, Effective C++, Item 25
220 
221  swap(_manager, rhs._manager); // just swapping the pointers
222  swap(_gilView, rhs._gilView);
223  swap(_origin, rhs._origin);
224 }
225 
226 template <typename PixelT>
228  a.swap(b);
229 }
230 
231 template <typename PixelT>
233  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
234  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
235  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
236  this->_manager);
237 }
238 
239 template <typename PixelT>
241  int rowStride = reinterpret_cast<PixelT*>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
242  return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
243  ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
244  this->_manager);
245 }
246 //
247 // Iterators
248 //
249 template <typename PixelT>
251  return _gilView.begin();
252 }
253 
254 template <typename PixelT>
256  return _gilView.end();
257 }
258 
259 template <typename PixelT>
261  return _gilView.rbegin();
262 }
263 
264 template <typename PixelT>
266  return _gilView.rend();
267 }
268 
269 template <typename PixelT>
271  return _gilView.at(x, y);
272 }
273 
274 template <typename PixelT>
276  if (!contiguous) {
277  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Only contiguous == true makes sense");
278  }
279  if (!this->isContiguous()) {
280  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Image's pixels are not contiguous");
281  }
282 
283  return row_begin(0);
284 }
285 
286 template <typename PixelT>
288  if (!contiguous) {
289  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Only contiguous == true makes sense");
290  }
291  if (!this->isContiguous()) {
292  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Image's pixels are not contiguous");
293  }
294 
295  return row_end(getHeight() - 1);
296 }
297 
298 template <typename PixelT>
300  fill_pixels(_gilView, rhs);
301 
302  return *this;
303 }
304 
305 //
306 // On to Image itself. ctors, cctors, and operator=
307 //
308 template <typename PixelT>
309 Image<PixelT>::Image(unsigned int width, unsigned int height, PixelT initialValue)
310  : ImageBase<PixelT>(geom::ExtentI(width, height)) {
311  *this = initialValue;
312 }
313 
314 template <typename PixelT>
315 Image<PixelT>::Image(geom::Extent2I const& dimensions, PixelT initialValue) : ImageBase<PixelT>(dimensions) {
316  *this = initialValue;
317 }
318 
319 template <typename PixelT>
320 Image<PixelT>::Image(geom::Box2I const& bbox, PixelT initialValue) : ImageBase<PixelT>(bbox) {
321  *this = initialValue;
322 }
323 
324 template <typename PixelT>
325 Image<PixelT>::Image(Image const& rhs, bool const deep) : ImageBase<PixelT>(rhs, deep) {}
326 
327 template <typename PixelT>
328 Image<PixelT>::Image(Image const& rhs, geom::Box2I const& bbox, ImageOrigin const origin, bool const deep
329 
330  )
331  : ImageBase<PixelT>(rhs, bbox, origin, deep) {}
332 
333 template <typename PixelT>
335  this->ImageBase<PixelT>::operator=(rhs);
336 
337  return *this;
338 }
339 
340 template <typename PixelT>
342  this->ImageBase<PixelT>::operator=(rhs);
343 
344  return *this;
345 }
346 
347 #ifndef DOXYGEN // doc for this section has been moved to header
348 
349 template <typename PixelT>
350 Image<PixelT>::Image(std::string const& fileName, int hdu, std::shared_ptr<daf::base::PropertySet> metadata,
351  geom::Box2I const& bbox, ImageOrigin origin)
352  : ImageBase<PixelT>() {
353  fits::Fits fitsfile(fileName, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
354  fitsfile.setHdu(hdu);
355  try {
356  *this = Image(fitsfile, metadata, bbox, origin);
357  } catch (fits::FitsError& e) {
358  fitsfile.status = 0; // reset so we can read NAXIS
359  if (fitsfile.getImageDim() == 0) { // no pixels to read
360  LSST_EXCEPT_ADD(e, str(boost::format("HDU %d has NAXIS == 0") % hdu));
361  }
362  throw e;
363  }
364 }
365 template <typename PixelT>
366 Image<PixelT>::Image(fits::MemFileManager& manager, int const hdu,
367  std::shared_ptr<daf::base::PropertySet> metadata, geom::Box2I const& bbox,
368  ImageOrigin const origin)
369  : ImageBase<PixelT>() {
370  fits::Fits fitsfile(manager, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
371  fitsfile.setHdu(hdu);
372  *this = Image(fitsfile, metadata, bbox, origin);
373 }
374 
375 template <typename PixelT>
376 Image<PixelT>::Image(fits::Fits& fitsfile, std::shared_ptr<daf::base::PropertySet> metadata,
377  geom::Box2I const& bbox, ImageOrigin const origin)
378  : ImageBase<PixelT>() {
379  typedef boost::mpl::vector<unsigned char, unsigned short, short, int, unsigned int, float, double,
380  std::uint64_t>
381  fits_image_types;
382 
383  if (!metadata) {
384  metadata.reset(new daf::base::PropertyList());
385  }
386 
387  fits_read_image<fits_image_types>(fitsfile, *this, *metadata, bbox, origin);
388 }
389 
390 template <typename PixelT>
391 void Image<PixelT>::writeFits(std::string const& fileName,
392  std::shared_ptr<daf::base::PropertySet const> metadata_i,
393  std::string const& mode) const {
394  fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
395  writeFits(fitsfile, metadata_i);
396 }
397 
398 template <typename PixelT>
399 void Image<PixelT>::writeFits(fits::MemFileManager& manager,
400  std::shared_ptr<daf::base::PropertySet const> metadata_i,
401  std::string const& mode) const {
402  fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
403  writeFits(fitsfile, metadata_i);
404 }
405 
406 template <typename PixelT>
407 void Image<PixelT>::writeFits(fits::Fits& fitsfile,
408  std::shared_ptr<daf::base::PropertySet const> metadata_i) const {
409  std::shared_ptr<daf::base::PropertySet> metadata;
410  std::shared_ptr<daf::base::PropertySet> wcsAMetadata =
411  detail::createTrivialWcsAsPropertySet(detail::wcsNameForXY0, this->getX0(), this->getY0());
412  if (metadata_i) {
413  metadata = metadata_i->deepCopy();
414  metadata->combine(wcsAMetadata);
415  } else {
416  metadata = wcsAMetadata;
417  }
418  fits_write_image(fitsfile, *this, metadata);
419 }
420 
421 #endif // !DOXYGEN
422 
423 template <typename PixelT>
425  using std::swap; // See Meyers, Effective C++, Item 25
427  ; // no private variables to swap
428 }
429 
430 template <typename PixelT>
432  a.swap(b);
433 }
434 
435 // In-place, per-pixel, sqrt().
436 template <typename PixelT>
438  transform_pixels(_getRawView(), _getRawView(),
439  [](PixelT const& l) -> PixelT { return static_cast<PixelT>(std::sqrt(l)); });
440 }
441 
442 template <typename PixelT>
444  transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT const& l) -> PixelT { return l + rhs; });
445  return *this;
446 }
447 
448 template <typename PixelT>
450  if (this->getDimensions() != rhs.getDimensions()) {
451  throw LSST_EXCEPT(pex::exceptions::LengthError,
452  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
453  this->getHeight() % rhs.getWidth() % rhs.getHeight())
454  .str());
455  }
456  transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
457  [](PixelT const& l, PixelT const& r) -> PixelT { return l + r; });
458  return *this;
459 }
460 
461 template <typename PixelT>
463  for (int y = 0; y != this->getHeight(); ++y) {
464  double const yPos = this->indexToPosition(y, Y);
465  double xPos = this->indexToPosition(0, X);
466  for (typename Image<PixelT>::x_iterator ptr = this->row_begin(y), end = this->row_end(y); ptr != end;
467  ++ptr, ++xPos) {
468  *ptr += function(xPos, yPos);
469  }
470  }
471  return *this;
472 }
473 
474 template <typename PixelT>
475 void Image<PixelT>::scaledPlus(double const c, Image<PixelT> const& rhs) {
476  if (this->getDimensions() != rhs.getDimensions()) {
477  throw LSST_EXCEPT(pex::exceptions::LengthError,
478  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
479  this->getHeight() % rhs.getWidth() % rhs.getHeight())
480  .str());
481  }
482  transform_pixels(
483  _getRawView(), rhs._getRawView(), _getRawView(),
484  [&c](PixelT const& l, PixelT const& r) -> PixelT { return l + static_cast<PixelT>(c * r); });
485 }
486 
487 template <typename PixelT>
489  transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT const& l) -> PixelT { return l - rhs; });
490  return *this;
491 }
492 
493 template <typename PixelT>
495  if (this->getDimensions() != rhs.getDimensions()) {
496  throw LSST_EXCEPT(pex::exceptions::LengthError,
497  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
498  this->getHeight() % rhs.getWidth() % rhs.getHeight())
499  .str());
500  }
501  transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
502  [](PixelT const& l, PixelT const& r) -> PixelT { return l - r; });
503  return *this;
504 }
505 
506 template <typename PixelT>
507 void Image<PixelT>::scaledMinus(double const c, Image<PixelT> const& rhs) {
508  if (this->getDimensions() != rhs.getDimensions()) {
509  throw LSST_EXCEPT(pex::exceptions::LengthError,
510  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
511  this->getHeight() % rhs.getWidth() % rhs.getHeight())
512  .str());
513  }
514  transform_pixels(
515  _getRawView(), rhs._getRawView(), _getRawView(),
516  [&c](PixelT const& l, PixelT const& r) -> PixelT { return l - static_cast<PixelT>(c * r); });
517 }
518 
519 template <typename PixelT>
521  for (int y = 0; y != this->getHeight(); ++y) {
522  double const yPos = this->indexToPosition(y, Y);
523  double xPos = this->indexToPosition(0, X);
524  for (typename Image<PixelT>::x_iterator ptr = this->row_begin(y), end = this->row_end(y); ptr != end;
525  ++ptr, ++xPos) {
526  *ptr -= function(xPos, yPos);
527  }
528  }
529  return *this;
530 }
531 
532 template <typename PixelT>
534  transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT const& l) -> PixelT { return l * rhs; });
535  return *this;
536 }
537 
538 template <typename PixelT>
540  if (this->getDimensions() != rhs.getDimensions()) {
541  throw LSST_EXCEPT(pex::exceptions::LengthError,
542  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
543  this->getHeight() % rhs.getWidth() % rhs.getHeight())
544  .str());
545  }
546  transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
547  [](PixelT const& l, PixelT const& r) -> PixelT { return l * r; });
548  return *this;
549 }
550 
551 template <typename PixelT>
552 void Image<PixelT>::scaledMultiplies(double const c, Image<PixelT> const& rhs) {
553  if (this->getDimensions() != rhs.getDimensions()) {
554  throw LSST_EXCEPT(pex::exceptions::LengthError,
555  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
556  this->getHeight() % rhs.getWidth() % rhs.getHeight())
557  .str());
558  }
559  transform_pixels(
560  _getRawView(), rhs._getRawView(), _getRawView(),
561  [&c](PixelT const& l, PixelT const& r) -> PixelT { return l * static_cast<PixelT>(c * r); });
562 }
563 
564 template <typename PixelT>
566  transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT const& l) -> PixelT { return l / rhs; });
567  return *this;
568 }
569 //
570 // Specialize float and double for efficiency
571 //
572 template <>
574  double const irhs = 1 / rhs;
575  *this *= irhs;
576  return *this;
577 }
578 
579 template <>
581  float const irhs = 1 / rhs;
582  *this *= irhs;
583  return *this;
584 }
585 
586 template <typename PixelT>
588  if (this->getDimensions() != rhs.getDimensions()) {
589  throw LSST_EXCEPT(pex::exceptions::LengthError,
590  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
591  this->getHeight() % rhs.getWidth() % rhs.getHeight())
592  .str());
593  }
594  transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
595  [](PixelT const& l, PixelT const& r) -> PixelT { return l / r; });
596  return *this;
597 }
598 
599 template <typename PixelT>
600 void Image<PixelT>::scaledDivides(double const c, Image<PixelT> const& rhs) {
601  if (this->getDimensions() != rhs.getDimensions()) {
602  throw LSST_EXCEPT(pex::exceptions::LengthError,
603  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
604  this->getHeight() % rhs.getWidth() % rhs.getHeight())
605  .str());
606  }
607  transform_pixels(
608  _getRawView(), rhs._getRawView(), _getRawView(),
609  [&c](PixelT const& l, PixelT const& r) -> PixelT { return l / static_cast<PixelT>(c * r); });
610 }
611 
612 namespace {
613 /*
614  * Worker routine for manipulating images;
615  */
616 template <typename LhsPixelT, typename RhsPixelT>
617 struct plusEq : public pixelOp2<LhsPixelT, RhsPixelT> {
618  LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs) const { return static_cast<LhsPixelT>(lhs + rhs); }
619 };
620 
621 template <typename LhsPixelT, typename RhsPixelT>
622 struct minusEq : public pixelOp2<LhsPixelT, RhsPixelT> {
623  LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs) const { return static_cast<LhsPixelT>(lhs - rhs); }
624 };
625 
626 template <typename LhsPixelT, typename RhsPixelT>
627 struct timesEq : public pixelOp2<LhsPixelT, RhsPixelT> {
628  LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs) const { return static_cast<LhsPixelT>(lhs * rhs); }
629 };
630 
631 template <typename LhsPixelT, typename RhsPixelT>
632 struct divideEq : public pixelOp2<LhsPixelT, RhsPixelT> {
633  LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs) const { return static_cast<LhsPixelT>(lhs / rhs); }
634 };
635 }
636 
637 template <typename LhsPixelT, typename RhsPixelT>
639  for_each_pixel(lhs, rhs, plusEq<LhsPixelT, RhsPixelT>());
640  return lhs;
641 }
642 
643 template <typename LhsPixelT, typename RhsPixelT>
645  for_each_pixel(lhs, rhs, minusEq<LhsPixelT, RhsPixelT>());
646  return lhs;
647 }
648 
649 template <typename LhsPixelT, typename RhsPixelT>
651  for_each_pixel(lhs, rhs, timesEq<LhsPixelT, RhsPixelT>());
652  return lhs;
653 }
654 
655 template <typename LhsPixelT, typename RhsPixelT>
657  for_each_pixel(lhs, rhs, divideEq<LhsPixelT, RhsPixelT>());
658  return lhs;
659 }
660 
662 {
663  geom::Extent2I dims{metadata.getAsInt("NAXIS1"), metadata.getAsInt("NAXIS2")};
665  return geom::Box2I(xy0, dims);
666 }
667 
668 //
669 // Explicit instantiations
670 //
672 #define INSTANTIATE_OPERATOR(OP_EQ, T) \
673  template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint16_t> const& rhs); \
674  template Image<T>& operator OP_EQ(Image<T>& lhs, Image<int> const& rhs); \
675  template Image<T>& operator OP_EQ(Image<T>& lhs, Image<float> const& rhs); \
676  template Image<T>& operator OP_EQ(Image<T>& lhs, Image<double> const& rhs); \
677  template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint64_t> const& rhs);
678 
679 #define INSTANTIATE(T) \
680  template class ImageBase<T>; \
681  template class Image<T>; \
682  INSTANTIATE_OPERATOR(+=, T); \
683  INSTANTIATE_OPERATOR(-=, T); \
684  INSTANTIATE_OPERATOR(*=, T); \
685  INSTANTIATE_OPERATOR(/=, T)
686 
687 INSTANTIATE(std::uint16_t);
688 INSTANTIATE(int);
689 INSTANTIATE(float);
690 INSTANTIATE(double);
691 INSTANTIATE(std::uint64_t);
693 }
694 }
695 } // end lsst::afw::image
Image & operator=(const PixelT rhs)
Set the image&#39;s pixels to rhs.
Definition: Image.cc:334
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
iterator begin() const
Return an STL compliant iterator to the start of the image.
Definition: Image.cc:250
A functor class equivalent to tr1::function&lt;LhsT (LhsT, RhsT)&gt;, but with a virtual operator() ...
Image & operator*=(PixelT const rhs)
Multiply lhs by scalar rhs.
Definition: Image.cc:533
Extent2I const getDimensions() const
Definition: Box.h:153
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
Definition: Image.h:151
ConstReference< PixelT >::type PixelConstReference
A ConstReference to a PixelT.
Definition: Image.h:133
double indexToPosition(double ind)
Convert image index to image position.
Definition: ImageUtils.h:55
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
Definition: Image.h:143
Image< LhsPixelT > & operator+=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Add lhs to Image rhs (i.e. pixel-by-pixel addition) where types are different.
Definition: Image.cc:638
void fits_write_image(fits::Fits &fitsfile, const ImageT &image, std::shared_ptr< daf::base::PropertySet const > metadata=std::shared_ptr< daf::base::PropertySet const >())
Definition: fits_io.h:99
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Definition: Image.cc:227
Image & operator/=(PixelT const rhs)
Divide lhs by scalar rhs.
Definition: Image.cc:565
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: Image.h:147
void scaledDivides(double const c, Image< PixelT > const &rhs)
Divide lhs by Image c*rhs (i.e. pixel-by-pixel division)
Definition: Image.cc:600
#define INSTANTIATE(TYPE)
void assign(ImageBase const &rhs, geom::Box2I const &bbox=geom::Box2I(), ImageOrigin origin=PARENT)
Copy pixels from another image to a specified subregion of this image.
Definition: Image.cc:160
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Definition: Image.h:456
Extent< int, 2 > Extent2I
Definition: Extent.h:341
void for_each_pixel(Image< LhsT > &lhs, pixelOp0< LhsT > const &func)
Set each pixel in an Image&lt;LhsT&gt; to func()
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:199
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: Image.h:116
A Function taking two arguments.
Definition: Function.h:266
iterator at(int x, int y) const
Return an STL compliant iterator at the point (x, y)
Definition: Image.cc:270
An integer coordinate rectangle.
Definition: Box.h:55
Image & operator-=(PixelT const rhs)
Subtract scalar rhs from lhs.
Definition: Image.cc:488
_view_t::iterator iterator
An STL compliant iterator.
Definition: Image.h:139
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:33
ImageBase & operator<<=(const ImageBase &rhs)
Set the lhs&#39;s pixel values to equal the rhs&#39;s.
Definition: Image.cc:154
ImageBase & operator=(const ImageBase &rhs)
Shallow assignment operator.
Definition: Image.cc:146
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: Image.h:367
void writeFits(std::string const &fileName, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet const >(), std::string const &mode="w") const
Write an image to a regular FITS file.
_view_t _getRawView() const
Definition: Image.h:476
Image & operator+=(PixelT const rhs)
Add scalar rhs to lhs.
Definition: Image.cc:443
int max
Definition: BoundedField.cc:99
friend class ImageBase
Definition: Image.h:168
A class used to request that array accesses be checked.
Definition: Image.h:88
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:309
geom::Box2I bboxFromMetadata(daf::base::PropertySet &metadata)
Determine the image bounding box from its metadata (FITS header)
Definition: Image.cc:661
void scaledPlus(double const c, Image< PixelT > const &rhs)
Add Image c*rhs to lhs.
Definition: Image.cc:475
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:163
void swap(ImageBase &rhs)
Definition: Image.cc:218
double x
Extent< int, 2 > ExtentI
Definition: Extent.h:338
ndarray::Array< PixelT const, 2, 1 > ConstArray
An immutable ndarray representation of the image.
Definition: Image.h:165
reverse_iterator rbegin() const
Return an STL compliant reverse iterator to the start of the image.
Definition: Image.cc:260
reverse_iterator rend() const
Return an STL compliant reverse iterator to the end of the image.
Definition: Image.cc:265
Image< LhsPixelT > & operator*=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Multiply lhs by Image rhs (i.e. pixel-by-pixel multiplication) where types are different.
Definition: Image.cc:650
void scaledMultiplies(double const c, Image< PixelT > const &rhs)
Multiply lhs by Image c*rhs (i.e. pixel-by-pixel multiplication)
Definition: Image.cc:552
Image< LhsPixelT > & operator-=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Subtract lhs from Image rhs (i.e. pixel-by-pixel subtraction) where types are different.
Definition: Image.cc:644
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
static _view_t _allocateView(geom::Extent2I const &dimensions, Manager::Ptr &manager)
Definition: Image.cc:51
afw::table::Key< afw::table::Array< ImagePixelT > > image
Reference< PixelT >::type PixelReference
A Reference to a PixelT.
Definition: Image.h:131
ImageOrigin _origin
Definition: fits_io_mpl.h:85
Image< LhsPixelT > & operator/=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Divide lhs by Image rhs (i.e. pixel-by-pixel division) where types are different. ...
Definition: Image.cc:656
std::shared_ptr< lsst::daf::base::PropertyList > createTrivialWcsAsPropertySet(std::string const &wcsName, int const x0=0, int const y0=0)
Definition: Wcs.cc:1069
static _view_t _makeSubView(geom::Extent2I const &dimensions, geom::Extent2I const &offset, const _view_t &view)
Definition: Image.cc:71
Point2I const getMin() const
Definition: Box.h:123
friend class Image
Definition: Image.h:500
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:307
ndarray::Array< PixelT, 2, 1 > Array
A mutable ndarray representation of the image.
Definition: Image.h:163
void swap(Image &rhs)
Definition: Image.cc:424
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:486
PixelReference operator()(int x, int y)
Return a reference to the pixel (x, y)
Definition: Image.cc:178
std::string const wcsNameForXY0
Definition: Image.h:84
int end
iterator end() const
Return an STL compliant iterator to the end of the image.
Definition: Image.cc:255
geom::Point2I getImageXY0FromMetadata(std::string const &wcsName, lsst::daf::base::PropertySet *metadata)
Definition: Wcs.cc:1096
void scaledMinus(double const c, Image< PixelT > const &rhs)
Subtract Image c*rhs from lhs.
Definition: Image.cc:507
lsst::daf::base::PropertyList PropertyList
Definition: Wcs.cc:56