31 #include <type_traits> 32 #include "boost/mpl/vector.hpp" 33 #pragma clang diagnostic push 34 #pragma clang diagnostic ignored "-Wunused-variable" 35 #pragma clang diagnostic pop 36 #include "boost/format.hpp" 37 #include "boost/filesystem/path.hpp" 38 #include "boost/gil/gil_all.hpp" 52 template <
typename PixelT>
54 Manager::Ptr& manager) {
55 if (dimensions.getX() < 0 || dimensions.getY() < 0) {
57 str(boost::format(
"Both width and height must be non-negative: %d, %d") %
58 dimensions.getX() % dimensions.getY()));
62 str(boost::format(
"Image dimensions (%d x %d) too large; int overflow detected.") %
63 dimensions.getX() % dimensions.getY()));
66 ndarray::SimpleManager<PixelT>::allocate(dimensions.getX() * dimensions.getY());
68 return boost::gil::interleaved_view(dimensions.getX(), dimensions.getY(),
69 (
typename _view_t::value_type*)r.second,
70 dimensions.getX() *
sizeof(PixelT));
72 template <
typename PixelT>
75 const _view_t& view) {
76 if (offset.getX() < 0 || offset.getY() < 0 || offset.getX() + dimensions.getX() > view.width() ||
77 offset.getY() + dimensions.getY() > view.height()) {
81 "Box2I(Point2I(%d,%d),lsst::geom::Extent2I(%d,%d)) doesn't fit in image %dx%d") %
82 offset.getX() % offset.getY() % dimensions.getX() % dimensions.getY() % view.width() %
86 return boost::gil::subimage_view(view, offset.getX(), offset.getY(), dimensions.getX(),
90 template <
typename PixelT>
92 : daf::base::Citizen(typeid(this)),
95 _gilView(_allocateView(dimensions, _manager)) {}
97 template <
typename PixelT>
99 : daf::base::Citizen(typeid(this)),
100 _origin(bbox.getMin()),
102 _gilView(_allocateView(bbox.getDimensions(), _manager)) {}
104 template <
typename PixelT>
108 : daf::base::Citizen(typeid(this)),
109 _origin(rhs._origin),
110 _manager(rhs._manager),
111 _gilView(rhs._gilView) {
119 template <
typename PixelT>
122 template <
typename PixelT>
127 : daf::base::Citizen(typeid(this)),
128 _origin((origin ==
PARENT) ? bbox.getMin() : rhs._origin +
lsst::geom::
Extent2I(bbox.getMin())),
129 _manager(rhs._manager),
130 _gilView(_makeSubView(bbox.getDimensions(), _origin - rhs._origin, rhs._gilView)) {
138 template <
typename PixelT>
140 : daf::base::Citizen(typeid(this)),
142 _manager(array.getManager()),
143 _gilView(
boost::gil::interleaved_view(array.template getSize<1>(), array.template getSize<0>(),
144 (typename _view_t::value_type*)array.getData(),
145 array.template getStride<0>() * sizeof(PixelT))) {
152 template <
typename PixelT>
160 template <
typename PixelT>
165 template <
typename PixelT>
171 template <
typename PixelT>
176 (boost::format(
"Dimension mismatch: %dx%d v. %dx%d") % lhsDim.getX() %
181 copy_pixels(rhs._gilView, _gilView);
184 auto lhsGilView = _makeSubView(lhsDim, lhsOff, _gilView);
185 copy_pixels(rhs._gilView, lhsGilView);
189 template <
typename PixelT>
195 template <
typename PixelT>
198 if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
200 (boost::format(
"Index (%d, %d) is out of range [0--%d], [0--%d]") % x % y %
201 (getWidth() - 1) % (getHeight() - 1))
209 template <
typename PixelT>
211 return _gilView(x, y)[0];
214 template <
typename PixelT>
217 if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
219 (boost::format(
"Index (%d, %d) is out of range [0--%d], [0--%d]") % x % y %
220 (this->getWidth() - 1) % (this->getHeight() - 1))
224 return _gilView(x, y)[0];
227 template <
typename PixelT>
230 int x = index.getX();
231 int y = index.getY();
236 return _gilView(x, y)[0];
239 template <
typename PixelT>
242 int x = index.getX();
243 int y = index.getY();
248 return _gilView(x, y)[0];
252 template <
typename PixelT>
256 swap(_manager, rhs._manager);
257 swap(_gilView, rhs._gilView);
258 swap(_origin, rhs._origin);
261 template <
typename PixelT>
269 template <
typename PixelT>
271 return _gilView.
begin();
274 template <
typename PixelT>
276 return _gilView.end();
279 template <
typename PixelT>
284 template <
typename PixelT>
286 return _gilView.
rend();
289 template <
typename PixelT>
291 return _gilView.
at(x, y);
294 template <
typename PixelT>
299 if (!this->isContiguous()) {
306 template <
typename PixelT>
311 if (!this->isContiguous()) {
315 return row_end(getHeight() - 1);
318 template <
typename PixelT>
320 fill_pixels(_gilView, rhs);
328 template <
typename PixelT>
331 *
this = initialValue;
334 template <
typename PixelT>
337 *
this = initialValue;
340 template <
typename PixelT>
342 *
this = initialValue;
345 template <
typename PixelT>
348 template <
typename PixelT>
351 template <
typename PixelT>
354 :
ImageBase<PixelT>(rhs, bbox, origin, deep) {}
356 template <
typename PixelT>
363 template <
typename PixelT>
370 template <
typename PixelT>
375 #ifndef DOXYGEN // doc for this section has been moved to header 377 template <
typename PixelT>
381 fits::Fits fitsfile(fileName,
"r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
384 *
this =
Image(fitsfile, metadata, bbox, origin);
393 template <
typename PixelT>
398 fits::Fits fitsfile(manager,
"r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
400 *
this =
Image(fitsfile, metadata, bbox, origin);
403 template <
typename PixelT>
407 typedef boost::mpl::vector<
unsigned char,
unsigned short, short, int,
unsigned int, float, double,
415 fits_read_image<fits_image_types>(fitsfile, *
this, *metadata,
bbox, origin);
418 template <
typename PixelT>
422 fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
423 writeFits(fitsfile, metadata_i);
426 template <
typename PixelT>
430 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
431 writeFits(fitsfile, metadata_i);
434 template <
typename PixelT>
440 template <
typename PixelT>
444 fits::Fits fitsfile(filename, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
445 writeFits(fitsfile, options, header,
mask);
448 template <
typename PixelT>
452 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
453 writeFits(fitsfile, options, header,
mask);
456 template <
typename PixelT>
465 template <
typename PixelT>
472 template <
typename PixelT>
478 template <
typename PixelT>
480 transform_pixels(_getRawView(), _getRawView(),
481 [](PixelT
const& l) -> PixelT {
return static_cast<PixelT
>(
std::sqrt(l)); });
484 template <
typename PixelT>
486 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l + rhs; });
490 template <
typename PixelT>
494 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
498 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
499 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l + r; });
503 template <
typename PixelT>
505 for (
int y = 0;
y != this->getHeight(); ++
y) {
510 *ptr +=
function(xPos, yPos);
516 template <
typename PixelT>
520 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
526 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l +
static_cast<PixelT
>(c * r); });
529 template <
typename PixelT>
531 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l - rhs; });
535 template <
typename PixelT>
539 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
543 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
544 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l - r; });
548 template <
typename PixelT>
552 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
558 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l -
static_cast<PixelT
>(c * r); });
561 template <
typename PixelT>
563 for (
int y = 0;
y != this->getHeight(); ++
y) {
568 *ptr -=
function(xPos, yPos);
574 template <
typename PixelT>
576 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l * rhs; });
580 template <
typename PixelT>
584 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
588 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
589 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l * r; });
593 template <
typename PixelT>
597 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
603 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l *
static_cast<PixelT
>(c * r); });
606 template <
typename PixelT>
608 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l / rhs; });
616 double const irhs = 1 / rhs;
623 float const irhs = 1 / rhs;
628 template <
typename PixelT>
632 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
636 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
637 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l / r; });
641 template <
typename PixelT>
645 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
651 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l /
static_cast<PixelT
>(c * r); });
658 template <
typename LhsPixelT,
typename RhsPixelT>
659 struct plusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
660 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const override {
661 return static_cast<LhsPixelT
>(lhs + rhs);
665 template <
typename LhsPixelT,
typename RhsPixelT>
666 struct minusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
667 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const override {
668 return static_cast<LhsPixelT
>(lhs - rhs);
672 template <
typename LhsPixelT,
typename RhsPixelT>
673 struct timesEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
674 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const override {
675 return static_cast<LhsPixelT
>(lhs * rhs);
679 template <
typename LhsPixelT,
typename RhsPixelT>
680 struct divideEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
681 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const override {
682 return static_cast<LhsPixelT
>(lhs / rhs);
687 template <
typename LhsPixelT,
typename RhsPixelT>
693 template <
typename LhsPixelT,
typename RhsPixelT>
699 template <
typename LhsPixelT,
typename RhsPixelT>
705 template <
typename LhsPixelT,
typename RhsPixelT>
713 if (metadata.
exists(
"ZNAXIS1") && metadata.
exists(
"ZNAXIS2")) {
722 template <
typename T1,
typename T2>
727 auto beg1Addr = arr1.front().begin();
728 auto end1Addr = arr1.back().end();
731 auto beg2Addr = arr2.front().begin();
732 auto end2Addr = arr2.back().end();
735 return ptrLess(beg1Addr, end2Addr) && ptrLess(beg2Addr, end1Addr);
742 #define INSTANTIATE_OPERATOR(OP_EQ, T) \ 743 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint16_t> const& rhs); \ 744 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<int> const& rhs); \ 745 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<float> const& rhs); \ 746 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<double> const& rhs); \ 747 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint64_t> const& rhs); 749 #define INSTANTIATE(T) \ 750 template class ImageBase<T>; \ 751 template class Image<T>; \ 752 INSTANTIATE_OPERATOR(+=, T); \ 753 INSTANTIATE_OPERATOR(-=, T); \ 754 INSTANTIATE_OPERATOR(*=, T); \ 755 INSTANTIATE_OPERATOR(/=, T) 757 #define INSTANTIATE2(T1, T2) template bool imagesOverlap<T1, T2>(ImageBase<T1> const&, ImageBase<T2> const&); Extent2I const getDimensions() const noexcept
int getImageDim()
Return the number of dimensions in the current HDU.
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 std::function<LhsT (LhsT, RhsT)>, but with a virtual operator() ...
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
ConstReference< PixelT >::type PixelConstReference
A ConstReference to a PixelT.
double indexToPosition(double ind)
Convert image index to image position.
_view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
void operator/=(ExtentBase< int, N > &lhs, double rhs) noexcept
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.
int getHeight() const
Return the number of rows in the image.
void operator*=(ExtentBase< int, N > &lhs, double rhs) noexcept
bool isEmpty() const noexcept
Options for writing an image to FITS.
int getAsInt(std::string const &name) const
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
void for_each_pixel(Image< LhsT > &lhs, pixelOp0< LhsT > const &func)
Set each pixel in an Image<LhsT> to func()
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
The base class for all image classed (Image, Mask, MaskedImage, ...)
A Function taking two arguments.
#define INSTANTIATE2(ImagePixelT1, ImagePixelT2)
Point2I const getMin() const noexcept
_view_t::iterator iterator
An STL compliant iterator.
An exception thrown when problems are found when reading or writing FITS files.
afw::table::PointKey< int > dimensions
A base class for image defects.
Represent a 2-dimensional array of bitmask pixels.
Lifetime-management for memory that goes into FITS memory files.
void assign(ImageBase const &rhs, lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
Copy pixels from another image to a specified subregion of this image.
#define INSTANTIATE(FROMSYS, TOSYS)
bool imagesOverlap(ImageBase< T1 > const &image1, ImageBase< T2 > const &image2)
Return true if the pixels for two images or masks overlap in memory.
bool exists(std::string const &name) const
A class used to request that array accesses be checked.
void swap(ImageBase &rhs)
void setHdu(int hdu, bool relative=false)
Set the current HDU.
void swap(Image< PixelT > &a, Image< PixelT > &b)
#define LSST_EXCEPT(type,...)
Extent< int, 2 > Extent2I
reverse_iterator rend() const
Return an STL compliant reverse iterator to the end of the image.
lsst::geom::Point2I getImageXY0FromMetadata(daf::base::PropertySet &metadata, std::string const &wcsName, bool strip=false)
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.
int getWidth() const
Return the number of columns in the image.
Reference< PixelT >::type PixelReference
A Reference to a PixelT.
reverse_iterator rbegin() const
Return an STL compliant reverse iterator to the start of the image.
iterator begin() const
Return an STL compliant iterator to the start of the image.
iterator at(int x, int y) const
Return an STL compliant iterator at the point (x, y)
lsst::geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
_view_t _getRawView() const
static _view_t _allocateView(lsst::geom::Extent2I const &dimensions, Manager::Ptr &manager)
#define LSST_EXCEPT_ADD(e, m)
ndarray::Array< MaskPixelT, 2, 1 > Array
A mutable ndarray representation of the image.
lsst::geom::Box2I bboxFromMetadata(daf::base::PropertySet &metadata)
Determine the image bounding box from its metadata (FITS header)
A class to represent a 2-dimensional array of pixels.
std::string const wcsNameForXY0
static _view_t _makeSubView(lsst::geom::Extent2I const &dimensions, lsst::geom::Extent2I const &offset, const _view_t &view)