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()) {
79 (boost::format(
"Box2I(Point2I(%d,%d),Extent2I(%d,%d)) doesn't fit in image %dx%d") %
80 offset.getX() % offset.getY() % dimensions.getX() % dimensions.getY() %
81 view.width() % view.height())
84 return boost::gil::subimage_view(view, offset.getX(), offset.getY(), dimensions.getX(),
88 template <
typename PixelT>
90 : daf::base::Citizen(typeid(this)),
93 _gilView(_allocateView(dimensions, _manager)) {}
95 template <
typename PixelT>
97 : daf::base::Citizen(typeid(this)),
98 _origin(bbox.getMin()),
100 _gilView(_allocateView(bbox.getDimensions(), _manager)) {}
102 template <
typename PixelT>
106 : daf::base::Citizen(typeid(this)),
107 _origin(rhs._origin),
108 _manager(rhs._manager),
109 _gilView(rhs._gilView) {
117 template <
typename PixelT>
120 template <
typename PixelT>
125 : daf::base::Citizen(typeid(this)),
126 _origin((origin ==
PARENT) ? bbox.getMin() : rhs._origin + geom::
Extent2I(bbox.getMin())),
127 _manager(rhs._manager),
128 _gilView(_makeSubView(bbox.getDimensions(), _origin - rhs._origin, rhs._gilView)) {
136 template <
typename PixelT>
138 : daf::base::Citizen(typeid(this)),
140 _manager(array.getManager()),
141 _gilView(
boost::gil::interleaved_view(array.template getSize<1>(), array.template getSize<0>(),
142 (typename _view_t::value_type*)array.getData(),
143 array.template getStride<0>() * sizeof(PixelT))) {
150 template <
typename PixelT>
158 template <
typename PixelT>
163 template <
typename PixelT>
169 template <
typename PixelT>
174 (boost::format(
"Dimension mismatch: %dx%d v. %dx%d") % lhsDim.getX() %
179 copy_pixels(rhs._gilView, _gilView);
182 auto lhsGilView = _makeSubView(lhsDim, lhsOff, _gilView);
183 copy_pixels(rhs._gilView, lhsGilView);
187 template <
typename PixelT>
193 template <
typename PixelT>
196 if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
199 (boost::format(
"Index (%d, %d) is out of range [0--%d], [0--%d]") % x % y % (getWidth() - 1) %
208 template <
typename PixelT>
210 return _gilView(x, y)[0];
213 template <
typename PixelT>
216 if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
218 (boost::format(
"Index (%d, %d) is out of range [0--%d], [0--%d]") % x % y %
219 (this->getWidth() - 1) %
224 return _gilView(x, y)[0];
227 template <
typename PixelT>
231 swap(_manager, rhs._manager);
232 swap(_gilView, rhs._gilView);
233 swap(_origin, rhs._origin);
236 template <
typename PixelT>
244 template <
typename PixelT>
246 return _gilView.
begin();
249 template <
typename PixelT>
251 return _gilView.end();
254 template <
typename PixelT>
259 template <
typename PixelT>
261 return _gilView.
rend();
264 template <
typename PixelT>
266 return _gilView.
at(x, y);
269 template <
typename PixelT>
274 if (!this->isContiguous()) {
281 template <
typename PixelT>
286 if (!this->isContiguous()) {
290 return row_end(getHeight() - 1);
293 template <
typename PixelT>
295 fill_pixels(_gilView, rhs);
303 template <
typename PixelT>
306 *
this = initialValue;
309 template <
typename PixelT>
311 *
this = initialValue;
314 template <
typename PixelT>
316 *
this = initialValue;
319 template <
typename PixelT>
322 template <
typename PixelT>
325 template <
typename PixelT>
329 :
ImageBase<PixelT>(rhs, bbox, origin, deep) {}
331 template <
typename PixelT>
338 template <
typename PixelT>
345 template <
typename PixelT>
350 #ifndef DOXYGEN // doc for this section has been moved to header 352 template <
typename PixelT>
356 fits::Fits fitsfile(fileName,
"r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
359 *
this =
Image(fitsfile, metadata, bbox, origin);
368 template <
typename PixelT>
373 fits::Fits fitsfile(manager,
"r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
375 *
this =
Image(fitsfile, metadata, bbox, origin);
378 template <
typename PixelT>
382 typedef boost::mpl::vector<
unsigned char,
unsigned short, short, int,
unsigned int, float, double,
390 fits_read_image<fits_image_types>(fitsfile, *
this, *metadata,
bbox, origin);
393 template <
typename PixelT>
397 fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
398 writeFits(fitsfile, metadata_i);
401 template <
typename PixelT>
405 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
406 writeFits(fitsfile, metadata_i);
409 template <
typename PixelT>
415 template <
typename PixelT>
423 fits::Fits fitsfile(filename, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
424 writeFits(fitsfile, options, header,
mask);
427 template <
typename PixelT>
435 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
436 writeFits(fitsfile, options, header,
mask);
439 template <
typename PixelT>
452 template <
typename PixelT>
459 template <
typename PixelT>
465 template <
typename PixelT>
467 transform_pixels(_getRawView(), _getRawView(),
468 [](PixelT
const& l) -> PixelT {
return static_cast<PixelT
>(
std::sqrt(l)); });
471 template <
typename PixelT>
473 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l + rhs; });
477 template <
typename PixelT>
481 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
485 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
486 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l + r; });
490 template <
typename PixelT>
492 for (
int y = 0;
y != this->getHeight(); ++
y) {
497 *ptr +=
function(xPos, yPos);
503 template <
typename PixelT>
507 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
513 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l +
static_cast<PixelT
>(c * r); });
516 template <
typename PixelT>
518 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l - rhs; });
522 template <
typename PixelT>
526 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
530 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
531 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l - r; });
535 template <
typename PixelT>
539 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
545 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l -
static_cast<PixelT
>(c * r); });
548 template <
typename PixelT>
550 for (
int y = 0;
y != this->getHeight(); ++
y) {
555 *ptr -=
function(xPos, yPos);
561 template <
typename PixelT>
563 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l * rhs; });
567 template <
typename PixelT>
571 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
575 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
576 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l * r; });
580 template <
typename PixelT>
584 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
590 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l *
static_cast<PixelT
>(c * r); });
593 template <
typename PixelT>
595 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l / rhs; });
603 double const irhs = 1 / rhs;
610 float const irhs = 1 / rhs;
615 template <
typename PixelT>
619 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
623 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
624 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l / r; });
628 template <
typename PixelT>
632 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
638 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l /
static_cast<PixelT
>(c * r); });
645 template <
typename LhsPixelT,
typename RhsPixelT>
646 struct plusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
647 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs + rhs); }
650 template <
typename LhsPixelT,
typename RhsPixelT>
651 struct minusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
652 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs - rhs); }
655 template <
typename LhsPixelT,
typename RhsPixelT>
656 struct timesEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
657 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs * rhs); }
660 template <
typename LhsPixelT,
typename RhsPixelT>
661 struct divideEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
662 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs / rhs); }
666 template <
typename LhsPixelT,
typename RhsPixelT>
672 template <
typename LhsPixelT,
typename RhsPixelT>
678 template <
typename LhsPixelT,
typename RhsPixelT>
684 template <
typename LhsPixelT,
typename RhsPixelT>
693 if (metadata.
exists(
"ZNAXIS1") && metadata.
exists(
"ZNAXIS2")) {
702 template <
typename T1,
typename T2>
707 auto beg1Addr = arr1.front().begin();
708 auto end1Addr = arr1.back().end();
711 auto beg2Addr = arr2.front().begin();
712 auto end2Addr = arr2.back().end();
715 return ptrLess(beg1Addr, end2Addr) && ptrLess(beg2Addr, end1Addr);
722 #define INSTANTIATE_OPERATOR(OP_EQ, T) \ 723 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint16_t> const& rhs); \ 724 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<int> const& rhs); \ 725 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<float> const& rhs); \ 726 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<double> const& rhs); \ 727 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint64_t> const& rhs); 729 #define INSTANTIATE(T) \ 730 template class ImageBase<T>; \ 731 template class Image<T>; \ 732 INSTANTIATE_OPERATOR(+=, T); \ 733 INSTANTIATE_OPERATOR(-=, T); \ 734 INSTANTIATE_OPERATOR(*=, T); \ 735 INSTANTIATE_OPERATOR(/=, T) 737 #define INSTANTIATE2(T1, T2) template bool imagesOverlap<T1, T2>(ImageBase<T1> const&, ImageBase<T2> const&); int getImageDim()
Return the number of dimensions in the current HDU.
Point2I const getMin() const
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() ...
bool isEmpty() const
Return true if the box contains no points.
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)
int getHeight() const
Return the number of rows in the image.
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 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.
geom::Point2I getImageXY0FromMetadata(daf::base::PropertySet &metadata, std::string const &wcsName, bool strip=false)
Extent< int, 2 > Extent2I
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)
An integer coordinate rectangle.
_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
geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
void operator*=(ExtentBase< int, N > &lhs, double rhs)
A base class for image defects.
Represent a 2-dimensional array of bitmask pixels.
Lifetime-management for memory that goes into FITS memory files.
#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.
geom::Box2I bboxFromMetadata(daf::base::PropertySet &metadata)
Determine the image bounding box from its metadata (FITS header)
void swap(ImageBase &rhs)
void setHdu(int hdu, bool relative=false)
Set the current HDU.
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
void swap(Image< PixelT > &a, Image< PixelT > &b)
#define LSST_EXCEPT(type,...)
reverse_iterator rend() const
Return an STL compliant reverse iterator to the end of the image.
Extent2I const getDimensions() const
int getWidth() const
Return the number of columns in the image.
static _view_t _allocateView(geom::Extent2I const &dimensions, Manager::Ptr &manager)
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)
Extent< double, N > & operator-=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
static _view_t _makeSubView(geom::Extent2I const &dimensions, geom::Extent2I const &offset, const _view_t &view)
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
_view_t _getRawView() const
#define LSST_EXCEPT_ADD(e, m)
ndarray::Array< MaskPixelT, 2, 1 > Array
A mutable ndarray representation of the image.
A class to represent a 2-dimensional array of pixels.
std::string const wcsNameForXY0