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>
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>
312 *
this = initialValue;
315 template <
typename PixelT>
317 *
this = initialValue;
320 template <
typename PixelT>
323 template <
typename PixelT>
326 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>
419 fits::Fits fitsfile(filename, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
420 writeFits(fitsfile, options, header,
mask);
423 template <
typename PixelT>
427 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
428 writeFits(fitsfile, options, header,
mask);
431 template <
typename PixelT>
440 template <
typename PixelT>
447 template <
typename PixelT>
453 template <
typename PixelT>
455 transform_pixels(_getRawView(), _getRawView(),
456 [](PixelT
const& l) -> PixelT {
return static_cast<PixelT
>(
std::sqrt(l)); });
459 template <
typename PixelT>
461 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l + rhs; });
465 template <
typename PixelT>
469 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
473 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
474 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l + r; });
478 template <
typename PixelT>
480 for (
int y = 0;
y != this->getHeight(); ++
y) {
485 *ptr +=
function(xPos, yPos);
491 template <
typename PixelT>
495 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
501 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l +
static_cast<PixelT
>(c * r); });
504 template <
typename PixelT>
506 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l - rhs; });
510 template <
typename PixelT>
514 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
518 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
519 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l - r; });
523 template <
typename PixelT>
527 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
533 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l -
static_cast<PixelT
>(c * r); });
536 template <
typename PixelT>
538 for (
int y = 0;
y != this->getHeight(); ++
y) {
543 *ptr -=
function(xPos, yPos);
549 template <
typename PixelT>
551 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l * rhs; });
555 template <
typename PixelT>
559 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
563 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
564 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l * r; });
568 template <
typename PixelT>
572 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
578 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l *
static_cast<PixelT
>(c * r); });
581 template <
typename PixelT>
583 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l / rhs; });
591 double const irhs = 1 / rhs;
598 float const irhs = 1 / rhs;
603 template <
typename PixelT>
607 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
611 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
612 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l / r; });
616 template <
typename PixelT>
620 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
626 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l /
static_cast<PixelT
>(c * r); });
633 template <
typename LhsPixelT,
typename RhsPixelT>
634 struct plusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
635 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs + rhs); }
638 template <
typename LhsPixelT,
typename RhsPixelT>
639 struct minusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
640 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs - rhs); }
643 template <
typename LhsPixelT,
typename RhsPixelT>
644 struct timesEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
645 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs * rhs); }
648 template <
typename LhsPixelT,
typename RhsPixelT>
649 struct divideEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
650 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs / rhs); }
654 template <
typename LhsPixelT,
typename RhsPixelT>
660 template <
typename LhsPixelT,
typename RhsPixelT>
666 template <
typename LhsPixelT,
typename RhsPixelT>
672 template <
typename LhsPixelT,
typename RhsPixelT>
680 if (metadata.
exists(
"ZNAXIS1") && metadata.
exists(
"ZNAXIS2")) {
689 template <
typename T1,
typename T2>
694 auto beg1Addr = arr1.front().begin();
695 auto end1Addr = arr1.back().end();
698 auto beg2Addr = arr2.front().begin();
699 auto end2Addr = arr2.back().end();
702 return ptrLess(beg1Addr, end2Addr) && ptrLess(beg2Addr, end1Addr);
709 #define INSTANTIATE_OPERATOR(OP_EQ, T) \ 710 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint16_t> const& rhs); \ 711 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<int> const& rhs); \ 712 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<float> const& rhs); \ 713 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<double> const& rhs); \ 714 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint64_t> const& rhs); 716 #define INSTANTIATE(T) \ 717 template class ImageBase<T>; \ 718 template class Image<T>; \ 719 INSTANTIATE_OPERATOR(+=, T); \ 720 INSTANTIATE_OPERATOR(-=, T); \ 721 INSTANTIATE_OPERATOR(*=, T); \ 722 INSTANTIATE_OPERATOR(/=, T) 724 #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.
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() ...
Extent2I const getDimensions() const
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.
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.
Point2I const getMin() const
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 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)
_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 operator*=(ExtentBase< int, N > &lhs, double rhs)
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
void operator/=(ExtentBase< int, N > &lhs, double rhs)
static _view_t _makeSubView(lsst::geom::Extent2I const &dimensions, lsst::geom::Extent2I const &offset, const _view_t &view)