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>
120 : daf::base::Citizen(typeid(this)),
121 _origin((origin ==
PARENT) ? bbox.getMin() : rhs._origin + geom::
Extent2I(bbox.getMin())),
122 _manager(rhs._manager),
123 _gilView(_makeSubView(bbox.getDimensions(), _origin - rhs._origin, rhs._gilView)) {
131 template <
typename PixelT>
133 : daf::base::Citizen(typeid(this)),
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))) {
145 template <
typename PixelT>
153 template <
typename PixelT>
159 template <
typename PixelT>
164 (boost::format(
"Dimension mismatch: %dx%d v. %dx%d") % lhsDim.getX() %
169 copy_pixels(rhs._gilView, _gilView);
172 auto lhsGilView = _makeSubView(lhsDim, lhsOff, _gilView);
173 copy_pixels(rhs._gilView, lhsGilView);
177 template <
typename PixelT>
183 template <
typename PixelT>
186 if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
189 (boost::format(
"Index (%d, %d) is out of range [0--%d], [0--%d]") % x % y % (getWidth() - 1) %
198 template <
typename PixelT>
200 return _gilView(x, y)[0];
203 template <
typename PixelT>
206 if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
208 (boost::format(
"Index (%d, %d) is out of range [0--%d], [0--%d]") % x % y %
209 (this->getWidth() - 1) %
214 return _gilView(x, y)[0];
217 template <
typename PixelT>
221 swap(_manager, rhs._manager);
222 swap(_gilView, rhs._gilView);
223 swap(_origin, rhs._origin);
226 template <
typename PixelT>
234 template <
typename PixelT>
236 return _gilView.
begin();
239 template <
typename PixelT>
241 return _gilView.end();
244 template <
typename PixelT>
249 template <
typename PixelT>
251 return _gilView.
rend();
254 template <
typename PixelT>
256 return _gilView.
at(x, y);
259 template <
typename PixelT>
264 if (!this->isContiguous()) {
271 template <
typename PixelT>
276 if (!this->isContiguous()) {
280 return row_end(getHeight() - 1);
283 template <
typename PixelT>
285 fill_pixels(_gilView, rhs);
293 template <
typename PixelT>
296 *
this = initialValue;
299 template <
typename PixelT>
301 *
this = initialValue;
304 template <
typename PixelT>
306 *
this = initialValue;
309 template <
typename PixelT>
312 template <
typename PixelT>
316 :
ImageBase<PixelT>(rhs, bbox, origin, deep) {}
318 template <
typename PixelT>
325 template <
typename PixelT>
332 #ifndef DOXYGEN // doc for this section has been moved to header 334 template <
typename PixelT>
338 fits::Fits fitsfile(fileName,
"r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
341 *
this =
Image(fitsfile, metadata, bbox, origin);
350 template <
typename PixelT>
355 fits::Fits fitsfile(manager,
"r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
357 *
this =
Image(fitsfile, metadata, bbox, origin);
360 template <
typename PixelT>
364 typedef boost::mpl::vector<
unsigned char,
unsigned short, short, int,
unsigned int, float, double,
372 fits_read_image<fits_image_types>(fitsfile, *
this, *metadata,
bbox, origin);
375 template <
typename PixelT>
379 fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
380 writeFits(fitsfile, metadata_i);
383 template <
typename PixelT>
387 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
388 writeFits(fitsfile, metadata_i);
391 template <
typename PixelT>
397 template <
typename PixelT>
405 fits::Fits fitsfile(filename, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
406 writeFits(fitsfile, options, header,
mask);
409 template <
typename PixelT>
417 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
418 writeFits(fitsfile, options, header,
mask);
421 template <
typename PixelT>
434 template <
typename PixelT>
441 template <
typename PixelT>
447 template <
typename PixelT>
449 transform_pixels(_getRawView(), _getRawView(),
450 [](PixelT
const& l) -> PixelT {
return static_cast<PixelT
>(
std::sqrt(l)); });
453 template <
typename PixelT>
455 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l + rhs; });
459 template <
typename PixelT>
463 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
467 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
468 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l + r; });
472 template <
typename PixelT>
474 for (
int y = 0; y != this->getHeight(); ++y) {
479 *ptr +=
function(xPos, yPos);
485 template <
typename PixelT>
489 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
495 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l +
static_cast<PixelT
>(c * r); });
498 template <
typename PixelT>
500 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l - rhs; });
504 template <
typename PixelT>
508 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
512 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
513 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l - r; });
517 template <
typename PixelT>
521 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
527 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l -
static_cast<PixelT
>(c * r); });
530 template <
typename PixelT>
532 for (
int y = 0; y != this->getHeight(); ++y) {
537 *ptr -=
function(xPos, yPos);
543 template <
typename PixelT>
545 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l * rhs; });
549 template <
typename PixelT>
553 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
557 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
558 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l * r; });
562 template <
typename PixelT>
566 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
572 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l *
static_cast<PixelT
>(c * r); });
575 template <
typename PixelT>
577 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l / rhs; });
585 double const irhs = 1 / rhs;
592 float const irhs = 1 / rhs;
597 template <
typename PixelT>
601 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
605 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
606 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l / r; });
610 template <
typename PixelT>
614 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
620 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l /
static_cast<PixelT
>(c * r); });
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); }
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); }
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); }
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); }
648 template <
typename LhsPixelT,
typename RhsPixelT>
654 template <
typename LhsPixelT,
typename RhsPixelT>
660 template <
typename LhsPixelT,
typename RhsPixelT>
666 template <
typename LhsPixelT,
typename RhsPixelT>
675 if (metadata.
exists(
"ZNAXIS1") && metadata.
exists(
"ZNAXIS2")) {
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); 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) 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 tr1::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