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" 50 template <
typename PixelT>
52 Manager::Ptr& manager) {
53 if (dimensions.getX() < 0 || dimensions.getY() < 0) {
55 str(boost::format(
"Both width and height must be non-negative: %d, %d") %
56 dimensions.getX() % dimensions.getY()));
60 str(boost::format(
"Image dimensions (%d x %d) too large; int overflow detected.") %
61 dimensions.getX() % dimensions.getY()));
64 ndarray::SimpleManager<PixelT>::allocate(dimensions.getX() * dimensions.getY());
66 return boost::gil::interleaved_view(dimensions.getX(), dimensions.getY(),
67 (
typename _view_t::value_type*)r.second,
68 dimensions.getX() *
sizeof(PixelT));
70 template <
typename PixelT>
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()) {
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())
82 return boost::gil::subimage_view(view, offset.getX(), offset.getY(), dimensions.getX(),
86 template <
typename PixelT>
88 : daf::base::Citizen(typeid(this)),
91 _gilView(_allocateView(dimensions, _manager)) {}
93 template <
typename PixelT>
95 : daf::base::Citizen(typeid(this)),
96 _origin(bbox.getMin()),
98 _gilView(_allocateView(bbox.getDimensions(), _manager)) {}
100 template <
typename PixelT>
104 : daf::base::Citizen(typeid(this)),
105 _origin(rhs._origin),
106 _manager(rhs._manager),
107 _gilView(rhs._gilView) {
115 template <
typename PixelT>
118 template <
typename PixelT>
123 : daf::base::Citizen(typeid(this)),
124 _origin((origin ==
PARENT) ? bbox.getMin() : rhs._origin + geom::
Extent2I(bbox.getMin())),
125 _manager(rhs._manager),
126 _gilView(_makeSubView(bbox.getDimensions(), _origin - rhs._origin, rhs._gilView)) {
134 template <
typename PixelT>
136 : daf::base::Citizen(typeid(this)),
138 _manager(array.getManager()),
139 _gilView(
boost::gil::interleaved_view(array.template getSize<1>(), array.template getSize<0>(),
140 (typename _view_t::value_type*)array.getData(),
141 array.template getStride<0>() * sizeof(PixelT))) {
148 template <
typename PixelT>
156 template <
typename PixelT>
161 template <
typename PixelT>
167 template <
typename PixelT>
172 (boost::format(
"Dimension mismatch: %dx%d v. %dx%d") % lhsDim.getX() %
177 copy_pixels(rhs._gilView, _gilView);
180 auto lhsGilView = _makeSubView(lhsDim, lhsOff, _gilView);
181 copy_pixels(rhs._gilView, lhsGilView);
185 template <
typename PixelT>
191 template <
typename PixelT>
194 if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
197 (boost::format(
"Index (%d, %d) is out of range [0--%d], [0--%d]") % x % y % (getWidth() - 1) %
206 template <
typename PixelT>
208 return _gilView(x, y)[0];
211 template <
typename PixelT>
214 if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
216 (boost::format(
"Index (%d, %d) is out of range [0--%d], [0--%d]") % x % y %
217 (this->getWidth() - 1) %
222 return _gilView(x, y)[0];
225 template <
typename PixelT>
229 swap(_manager, rhs._manager);
230 swap(_gilView, rhs._gilView);
231 swap(_origin, rhs._origin);
234 template <
typename PixelT>
242 template <
typename PixelT>
244 return _gilView.
begin();
247 template <
typename PixelT>
249 return _gilView.end();
252 template <
typename PixelT>
257 template <
typename PixelT>
259 return _gilView.
rend();
262 template <
typename PixelT>
264 return _gilView.
at(x, y);
267 template <
typename PixelT>
272 if (!this->isContiguous()) {
279 template <
typename PixelT>
284 if (!this->isContiguous()) {
288 return row_end(getHeight() - 1);
291 template <
typename PixelT>
293 fill_pixels(_gilView, rhs);
301 template <
typename PixelT>
304 *
this = initialValue;
307 template <
typename PixelT>
309 *
this = initialValue;
312 template <
typename PixelT>
314 *
this = initialValue;
317 template <
typename PixelT>
320 template <
typename PixelT>
323 template <
typename PixelT>
327 :
ImageBase<PixelT>(rhs, bbox, origin, deep) {}
329 template <
typename PixelT>
336 template <
typename PixelT>
343 template <
typename PixelT>
348 #ifndef DOXYGEN // doc for this section has been moved to header 350 template <
typename PixelT>
354 fits::Fits fitsfile(fileName,
"r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
357 *
this =
Image(fitsfile, metadata, bbox, origin);
366 template <
typename PixelT>
371 fits::Fits fitsfile(manager,
"r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
373 *
this =
Image(fitsfile, metadata, bbox, origin);
376 template <
typename PixelT>
380 typedef boost::mpl::vector<
unsigned char,
unsigned short, short, int,
unsigned int, float, double,
388 fits_read_image<fits_image_types>(fitsfile, *
this, *metadata,
bbox, origin);
391 template <
typename PixelT>
395 fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
396 writeFits(fitsfile, metadata_i);
399 template <
typename PixelT>
403 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
404 writeFits(fitsfile, metadata_i);
407 template <
typename PixelT>
413 template <
typename PixelT>
421 fits::Fits fitsfile(filename, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
422 writeFits(fitsfile, options, header,
mask);
425 template <
typename PixelT>
433 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
434 writeFits(fitsfile, options, header,
mask);
437 template <
typename PixelT>
450 template <
typename PixelT>
457 template <
typename PixelT>
463 template <
typename PixelT>
465 transform_pixels(_getRawView(), _getRawView(),
466 [](PixelT
const& l) -> PixelT {
return static_cast<PixelT
>(
std::sqrt(l)); });
469 template <
typename PixelT>
471 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l + rhs; });
475 template <
typename PixelT>
479 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
483 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
484 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l + r; });
488 template <
typename PixelT>
490 for (
int y = 0; y != this->getHeight(); ++y) {
495 *ptr +=
function(xPos, yPos);
501 template <
typename PixelT>
505 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
511 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l +
static_cast<PixelT
>(c * r); });
514 template <
typename PixelT>
516 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l - rhs; });
520 template <
typename PixelT>
524 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
528 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
529 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l - r; });
533 template <
typename PixelT>
537 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
543 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l -
static_cast<PixelT
>(c * r); });
546 template <
typename PixelT>
548 for (
int y = 0; y != this->getHeight(); ++y) {
553 *ptr -=
function(xPos, yPos);
559 template <
typename PixelT>
561 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l * rhs; });
565 template <
typename PixelT>
569 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
573 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
574 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l * r; });
578 template <
typename PixelT>
582 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
588 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l *
static_cast<PixelT
>(c * r); });
591 template <
typename PixelT>
593 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l / rhs; });
601 double const irhs = 1 / rhs;
608 float const irhs = 1 / rhs;
613 template <
typename PixelT>
617 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
621 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
622 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l / r; });
626 template <
typename PixelT>
630 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
636 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l /
static_cast<PixelT
>(c * r); });
643 template <
typename LhsPixelT,
typename RhsPixelT>
644 struct plusEq :
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 minusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
650 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs - rhs); }
653 template <
typename LhsPixelT,
typename RhsPixelT>
654 struct timesEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
655 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs * rhs); }
658 template <
typename LhsPixelT,
typename RhsPixelT>
659 struct divideEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
660 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs / rhs); }
664 template <
typename LhsPixelT,
typename RhsPixelT>
670 template <
typename LhsPixelT,
typename RhsPixelT>
676 template <
typename LhsPixelT,
typename RhsPixelT>
682 template <
typename LhsPixelT,
typename RhsPixelT>
691 if (metadata.
exists(
"ZNAXIS1") && metadata.
exists(
"ZNAXIS2")) {
704 #define INSTANTIATE_OPERATOR(OP_EQ, T) \ 705 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint16_t> const& rhs); \ 706 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<int> const& rhs); \ 707 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<float> const& rhs); \ 708 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<double> const& rhs); \ 709 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint64_t> const& rhs); 711 #define INSTANTIATE(T) \ 712 template class ImageBase<T>; \ 713 template class Image<T>; \ 714 INSTANTIATE_OPERATOR(+=, T); \ 715 INSTANTIATE_OPERATOR(-=, T); \ 716 INSTANTIATE_OPERATOR(*=, T); \ 717 INSTANTIATE_OPERATOR(/=, T) 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.
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 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