lsst.afw  13.0-109-g62eb66483
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 //
232 // Iterators
233 //
234 template <typename PixelT>
236  return _gilView.begin();
237 }
238 
239 template <typename PixelT>
241  return _gilView.end();
242 }
243 
244 template <typename PixelT>
246  return _gilView.rbegin();
247 }
248 
249 template <typename PixelT>
251  return _gilView.rend();
252 }
253 
254 template <typename PixelT>
256  return _gilView.at(x, y);
257 }
258 
259 template <typename PixelT>
261  if (!contiguous) {
262  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Only contiguous == true makes sense");
263  }
264  if (!this->isContiguous()) {
265  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Image's pixels are not contiguous");
266  }
267 
268  return row_begin(0);
269 }
270 
271 template <typename PixelT>
273  if (!contiguous) {
274  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Only contiguous == true makes sense");
275  }
276  if (!this->isContiguous()) {
277  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Image's pixels are not contiguous");
278  }
279 
280  return row_end(getHeight() - 1);
281 }
282 
283 template <typename PixelT>
285  fill_pixels(_gilView, rhs);
286 
287  return *this;
288 }
289 
290 //
291 // On to Image itself. ctors, cctors, and operator=
292 //
293 template <typename PixelT>
294 Image<PixelT>::Image(unsigned int width, unsigned int height, PixelT initialValue)
295  : ImageBase<PixelT>(geom::ExtentI(width, height)) {
296  *this = initialValue;
297 }
298 
299 template <typename PixelT>
300 Image<PixelT>::Image(geom::Extent2I const& dimensions, PixelT initialValue) : ImageBase<PixelT>(dimensions) {
301  *this = initialValue;
302 }
303 
304 template <typename PixelT>
305 Image<PixelT>::Image(geom::Box2I const& bbox, PixelT initialValue) : ImageBase<PixelT>(bbox) {
306  *this = initialValue;
307 }
308 
309 template <typename PixelT>
310 Image<PixelT>::Image(Image const& rhs, bool const deep) : ImageBase<PixelT>(rhs, deep) {}
311 
312 template <typename PixelT>
313 Image<PixelT>::Image(Image const& rhs, geom::Box2I const& bbox, ImageOrigin const origin, bool const deep
314 
315  )
316  : ImageBase<PixelT>(rhs, bbox, origin, deep) {}
317 
318 template <typename PixelT>
320  this->ImageBase<PixelT>::operator=(rhs);
321 
322  return *this;
323 }
324 
325 template <typename PixelT>
327  this->ImageBase<PixelT>::operator=(rhs);
328 
329  return *this;
330 }
331 
332 #ifndef DOXYGEN // doc for this section has been moved to header
333 
334 template <typename PixelT>
335 Image<PixelT>::Image(std::string const& fileName, int hdu, std::shared_ptr<daf::base::PropertySet> metadata,
336  geom::Box2I const& bbox, ImageOrigin origin)
337  : ImageBase<PixelT>() {
338  fits::Fits fitsfile(fileName, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
339  fitsfile.setHdu(hdu);
340  try {
341  *this = Image(fitsfile, metadata, bbox, origin);
342  } catch (fits::FitsError& e) {
343  fitsfile.status = 0; // reset so we can read NAXIS
344  if (fitsfile.getImageDim() == 0) { // no pixels to read
345  LSST_EXCEPT_ADD(e, str(boost::format("HDU %d has NAXIS == 0") % hdu));
346  }
347  throw e;
348  }
349 }
350 template <typename PixelT>
351 Image<PixelT>::Image(fits::MemFileManager& manager, int const hdu,
352  std::shared_ptr<daf::base::PropertySet> metadata, geom::Box2I const& bbox,
353  ImageOrigin const origin)
354  : ImageBase<PixelT>() {
355  fits::Fits fitsfile(manager, "r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
356  fitsfile.setHdu(hdu);
357  *this = Image(fitsfile, metadata, bbox, origin);
358 }
359 
360 template <typename PixelT>
361 Image<PixelT>::Image(fits::Fits& fitsfile, std::shared_ptr<daf::base::PropertySet> metadata,
362  geom::Box2I const& bbox, ImageOrigin const origin)
363  : ImageBase<PixelT>() {
364  typedef boost::mpl::vector<unsigned char, unsigned short, short, int, unsigned int, float, double,
365  std::uint64_t>
366  fits_image_types;
367 
368  if (!metadata) {
369  metadata.reset(new daf::base::PropertyList());
370  }
371 
372  fits_read_image<fits_image_types>(fitsfile, *this, *metadata, bbox, origin);
373 }
374 
375 template <typename PixelT>
376 void Image<PixelT>::writeFits(std::string const& fileName,
377  std::shared_ptr<daf::base::PropertySet const> metadata_i,
378  std::string const& mode) const {
379  fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
380  writeFits(fitsfile, metadata_i);
381 }
382 
383 template <typename PixelT>
385  std::shared_ptr<daf::base::PropertySet const> metadata_i,
386  std::string const& mode) const {
387  fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
388  writeFits(fitsfile, metadata_i);
389 }
390 
391 template <typename PixelT>
392 void Image<PixelT>::writeFits(fits::Fits& fitsfile,
393  std::shared_ptr<daf::base::PropertySet const> metadata) const {
394  fitsfile.writeImage(*this, fits::ImageWriteOptions(*this), metadata);
395 }
396 
397 template <typename PixelT>
399  std::string const& filename,
400  fits::ImageWriteOptions const& options,
401  std::string const& mode,
402  std::shared_ptr<daf::base::PropertySet const> header,
403  std::shared_ptr<Mask<MaskPixel> const> mask
404 ) const {
405  fits::Fits fitsfile(filename, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
406  writeFits(fitsfile, options, header, mask);
407 }
408 
409 template <typename PixelT>
411  fits::MemFileManager& manager,
412  fits::ImageWriteOptions const& options,
413  std::string const& mode,
414  std::shared_ptr<daf::base::PropertySet const> header,
415  std::shared_ptr<Mask<MaskPixel> const> mask
416 ) const {
417  fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
418  writeFits(fitsfile, options, header, mask);
419 }
420 
421 template <typename PixelT>
423  fits::Fits& fitsfile,
424  fits::ImageWriteOptions const& options,
425  std::shared_ptr<daf::base::PropertySet const> header,
426  std::shared_ptr<Mask<MaskPixel> const> mask
427 ) const {
428  fitsfile.writeImage(*this, options, header, mask);
429 }
430 
431 
432 #endif // !DOXYGEN
433 
434 template <typename PixelT>
436  using std::swap; // See Meyers, Effective C++, Item 25
438  ; // no private variables to swap
439 }
440 
441 template <typename PixelT>
443  a.swap(b);
444 }
445 
446 // In-place, per-pixel, sqrt().
447 template <typename PixelT>
449  transform_pixels(_getRawView(), _getRawView(),
450  [](PixelT const& l) -> PixelT { return static_cast<PixelT>(std::sqrt(l)); });
451 }
452 
453 template <typename PixelT>
455  transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT const& l) -> PixelT { return l + rhs; });
456  return *this;
457 }
458 
459 template <typename PixelT>
461  if (this->getDimensions() != rhs.getDimensions()) {
462  throw LSST_EXCEPT(pex::exceptions::LengthError,
463  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
464  this->getHeight() % rhs.getWidth() % rhs.getHeight())
465  .str());
466  }
467  transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
468  [](PixelT const& l, PixelT const& r) -> PixelT { return l + r; });
469  return *this;
470 }
471 
472 template <typename PixelT>
474  for (int y = 0; y != this->getHeight(); ++y) {
475  double const yPos = this->indexToPosition(y, Y);
476  double xPos = this->indexToPosition(0, X);
477  for (typename Image<PixelT>::x_iterator ptr = this->row_begin(y), end = this->row_end(y); ptr != end;
478  ++ptr, ++xPos) {
479  *ptr += function(xPos, yPos);
480  }
481  }
482  return *this;
483 }
484 
485 template <typename PixelT>
486 void Image<PixelT>::scaledPlus(double const c, Image<PixelT> const& rhs) {
487  if (this->getDimensions() != rhs.getDimensions()) {
488  throw LSST_EXCEPT(pex::exceptions::LengthError,
489  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
490  this->getHeight() % rhs.getWidth() % rhs.getHeight())
491  .str());
492  }
493  transform_pixels(
494  _getRawView(), rhs._getRawView(), _getRawView(),
495  [&c](PixelT const& l, PixelT const& r) -> PixelT { return l + static_cast<PixelT>(c * r); });
496 }
497 
498 template <typename PixelT>
500  transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT const& l) -> PixelT { return l - rhs; });
501  return *this;
502 }
503 
504 template <typename PixelT>
506  if (this->getDimensions() != rhs.getDimensions()) {
507  throw LSST_EXCEPT(pex::exceptions::LengthError,
508  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
509  this->getHeight() % rhs.getWidth() % rhs.getHeight())
510  .str());
511  }
512  transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
513  [](PixelT const& l, PixelT const& r) -> PixelT { return l - r; });
514  return *this;
515 }
516 
517 template <typename PixelT>
518 void Image<PixelT>::scaledMinus(double const c, Image<PixelT> const& rhs) {
519  if (this->getDimensions() != rhs.getDimensions()) {
520  throw LSST_EXCEPT(pex::exceptions::LengthError,
521  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
522  this->getHeight() % rhs.getWidth() % rhs.getHeight())
523  .str());
524  }
525  transform_pixels(
526  _getRawView(), rhs._getRawView(), _getRawView(),
527  [&c](PixelT const& l, PixelT const& r) -> PixelT { return l - static_cast<PixelT>(c * r); });
528 }
529 
530 template <typename PixelT>
532  for (int y = 0; y != this->getHeight(); ++y) {
533  double const yPos = this->indexToPosition(y, Y);
534  double xPos = this->indexToPosition(0, X);
535  for (typename Image<PixelT>::x_iterator ptr = this->row_begin(y), end = this->row_end(y); ptr != end;
536  ++ptr, ++xPos) {
537  *ptr -= function(xPos, yPos);
538  }
539  }
540  return *this;
541 }
542 
543 template <typename PixelT>
545  transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT const& l) -> PixelT { return l * rhs; });
546  return *this;
547 }
548 
549 template <typename PixelT>
551  if (this->getDimensions() != rhs.getDimensions()) {
552  throw LSST_EXCEPT(pex::exceptions::LengthError,
553  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
554  this->getHeight() % rhs.getWidth() % rhs.getHeight())
555  .str());
556  }
557  transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
558  [](PixelT const& l, PixelT const& r) -> PixelT { return l * r; });
559  return *this;
560 }
561 
562 template <typename PixelT>
563 void Image<PixelT>::scaledMultiplies(double const c, Image<PixelT> const& rhs) {
564  if (this->getDimensions() != rhs.getDimensions()) {
565  throw LSST_EXCEPT(pex::exceptions::LengthError,
566  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
567  this->getHeight() % rhs.getWidth() % rhs.getHeight())
568  .str());
569  }
570  transform_pixels(
571  _getRawView(), rhs._getRawView(), _getRawView(),
572  [&c](PixelT const& l, PixelT const& r) -> PixelT { return l * static_cast<PixelT>(c * r); });
573 }
574 
575 template <typename PixelT>
577  transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT const& l) -> PixelT { return l / rhs; });
578  return *this;
579 }
580 //
581 // Specialize float and double for efficiency
582 //
583 template <>
585  double const irhs = 1 / rhs;
586  *this *= irhs;
587  return *this;
588 }
589 
590 template <>
592  float const irhs = 1 / rhs;
593  *this *= irhs;
594  return *this;
595 }
596 
597 template <typename PixelT>
599  if (this->getDimensions() != rhs.getDimensions()) {
600  throw LSST_EXCEPT(pex::exceptions::LengthError,
601  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
602  this->getHeight() % rhs.getWidth() % rhs.getHeight())
603  .str());
604  }
605  transform_pixels(_getRawView(), rhs._getRawView(), _getRawView(),
606  [](PixelT const& l, PixelT const& r) -> PixelT { return l / r; });
607  return *this;
608 }
609 
610 template <typename PixelT>
611 void Image<PixelT>::scaledDivides(double const c, Image<PixelT> const& rhs) {
612  if (this->getDimensions() != rhs.getDimensions()) {
613  throw LSST_EXCEPT(pex::exceptions::LengthError,
614  (boost::format("Images are of different size, %dx%d v %dx%d") % this->getWidth() %
615  this->getHeight() % rhs.getWidth() % rhs.getHeight())
616  .str());
617  }
618  transform_pixels(
619  _getRawView(), rhs._getRawView(), _getRawView(),
620  [&c](PixelT const& l, PixelT const& r) -> PixelT { return l / static_cast<PixelT>(c * r); });
621 }
622 
623 namespace {
624 /*
625  * Worker routine for manipulating images;
626  */
627 template <typename LhsPixelT, typename RhsPixelT>
628 struct plusEq : public pixelOp2<LhsPixelT, RhsPixelT> {
629  LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs) const { return static_cast<LhsPixelT>(lhs + rhs); }
630 };
631 
632 template <typename LhsPixelT, typename RhsPixelT>
633 struct minusEq : public pixelOp2<LhsPixelT, RhsPixelT> {
634  LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs) const { return static_cast<LhsPixelT>(lhs - rhs); }
635 };
636 
637 template <typename LhsPixelT, typename RhsPixelT>
638 struct timesEq : public pixelOp2<LhsPixelT, RhsPixelT> {
639  LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs) const { return static_cast<LhsPixelT>(lhs * rhs); }
640 };
641 
642 template <typename LhsPixelT, typename RhsPixelT>
643 struct divideEq : public pixelOp2<LhsPixelT, RhsPixelT> {
644  LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs) const { return static_cast<LhsPixelT>(lhs / rhs); }
645 };
646 }
647 
648 template <typename LhsPixelT, typename RhsPixelT>
650  for_each_pixel(lhs, rhs, plusEq<LhsPixelT, RhsPixelT>());
651  return lhs;
652 }
653 
654 template <typename LhsPixelT, typename RhsPixelT>
656  for_each_pixel(lhs, rhs, minusEq<LhsPixelT, RhsPixelT>());
657  return lhs;
658 }
659 
660 template <typename LhsPixelT, typename RhsPixelT>
662  for_each_pixel(lhs, rhs, timesEq<LhsPixelT, RhsPixelT>());
663  return lhs;
664 }
665 
666 template <typename LhsPixelT, typename RhsPixelT>
668  for_each_pixel(lhs, rhs, divideEq<LhsPixelT, RhsPixelT>());
669  return lhs;
670 }
671 
673 {
674  geom::Extent2I dims;
675  if (metadata.exists("ZNAXIS1") && metadata.exists("ZNAXIS2")) {
676  dims = geom::Extent2I(metadata.getAsInt("ZNAXIS1"), metadata.getAsInt("ZNAXIS2"));
677  } else {
678  dims = geom::Extent2I(metadata.getAsInt("NAXIS1"), metadata.getAsInt("NAXIS2"));
679  }
681  return geom::Box2I(xy0, dims);
682 }
683 
684 //
685 // Explicit instantiations
686 //
688 #define INSTANTIATE_OPERATOR(OP_EQ, T) \
689  template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint16_t> const& rhs); \
690  template Image<T>& operator OP_EQ(Image<T>& lhs, Image<int> const& rhs); \
691  template Image<T>& operator OP_EQ(Image<T>& lhs, Image<float> const& rhs); \
692  template Image<T>& operator OP_EQ(Image<T>& lhs, Image<double> const& rhs); \
693  template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint64_t> const& rhs);
694 
695 #define INSTANTIATE(T) \
696  template class ImageBase<T>; \
697  template class Image<T>; \
698  INSTANTIATE_OPERATOR(+=, T); \
699  INSTANTIATE_OPERATOR(-=, T); \
700  INSTANTIATE_OPERATOR(*=, T); \
701  INSTANTIATE_OPERATOR(/=, T)
702 
703 INSTANTIATE(std::uint16_t);
704 INSTANTIATE(int);
705 INSTANTIATE(float);
706 INSTANTIATE(double);
707 INSTANTIATE(std::uint64_t);
709 }
710 }
711 } // end lsst::afw::image
int getImageDim()
Return the number of dimensions in the current HDU.
Point2I const getMin() const
Definition: Box.h:123
void scaledPlus(OutImageT &outImage, double c1, InImageT const &inImage1, double c2, InImageT const &inImage2)
Compute the scaled sum of two images.
A functor class equivalent to tr1::function<LhsT (LhsT, RhsT)>, but with a virtual operator() ...
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:163
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
Definition: Span.h:37
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: ImageBase.h:133
afw::table::Key< afw::table::Array< MaskPixelT > > mask
void operator/=(ExtentBase< int, N > &lhs, double rhs)
Definition: Extent.h:397
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:299
Options for writing an image to FITS.
Definition: fits.h:201
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
Definition: ImageBase.h:137
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
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<LhsT> to func()
lsst::daf::base::PropertyList PropertyList
Definition: Wcs.cc:56
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition: fits.h:279
The base class for all image classed (Image, Mask, MaskedImage, ...)
Definition: ImageBase.h:106
A Function taking two arguments.
Definition: Function.h:266
An integer coordinate rectangle.
Definition: Box.h:55
lsst::daf::base::PropertySet PropertySet
Definition: Wcs.cc:55
_view_t::iterator iterator
An STL compliant iterator.
Definition: ImageBase.h:129
An exception thrown when problems are found when reading or writing FITS files.
Definition: fits.h:34
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
geom::Extent2I getDimensions() const
Return the image&#39;s size; useful for passing to constructors.
Definition: ImageBase.h:357
void operator*=(ExtentBase< int, N > &lhs, double rhs)
Definition: Extent.h:386
A base class for image defects.
Definition: cameraGeom.dox:3
int end
geom::Point2I getImageXY0FromMetadata(std::string const &wcsName, lsst::daf::base::PropertySet *metadata)
Definition: Wcs.cc:1096
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:83
Lifetime-management for memory that goes into FITS memory files.
Definition: fits.h:101
int max
Definition: BoundedField.cc:99
#define INSTANTIATE(FROMSYS, TOSYS)
Definition: Detector.cc:139
A class used to request that array accesses be checked.
Definition: ImageBase.h:78
geom::Box2I bboxFromMetadata(daf::base::PropertySet &metadata)
Determine the image bounding box from its metadata (FITS header)
Definition: Image.cc:672
double x
void swap(ImageBase &rhs)
Definition: Image.cc:218
void setHdu(int hdu, bool relative=false)
Set the current HDU.
Extent< int, 2 > ExtentI
Definition: Extent.h:338
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:413
void swap(Image< PixelT > &a, Image< PixelT > &b)
Definition: Image.cc:442
reverse_iterator rend() const
Return an STL compliant reverse iterator to the end of the image.
Definition: Image.cc:250
Extent2I const getDimensions() const
Definition: Box.h:153
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:297
static _view_t _allocateView(geom::Extent2I const &dimensions, Manager::Ptr &manager)
Definition: Image.cc:51
Reference< PixelT >::type PixelReference
A Reference to a PixelT.
Definition: ImageBase.h:121
reverse_iterator rbegin() const
Return an STL compliant reverse iterator to the start of the image.
Definition: Image.cc:245
iterator begin() const
Return an STL compliant iterator to the start of the image.
Definition: Image.cc:235
iterator at(int x, int y) const
Return an STL compliant iterator at the point (x, y)
Definition: Image.cc:255
Extent< double, N > & operator-=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
Definition: Extent.h:423
static _view_t _makeSubView(geom::Extent2I const &dimensions, geom::Extent2I const &offset, const _view_t &view)
Definition: Image.cc:71
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
Definition: fits.h:459
_view_t _getRawView() const
Definition: ImageBase.h:466
ndarray::Array< MaskPixelT, 2, 1 > Array
A mutable ndarray representation of the image.
Definition: ImageBase.h:153
void swap(Image &rhs)
Definition: Image.cc:435
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:68
std::string const wcsNameForXY0
Definition: ImageBase.h:74