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" 38 #include "lsst/pex/exceptions.h" 50 template <
typename PixelT>
52 Manager::Ptr& manager) {
53 if (dimensions.getX() < 0 || dimensions.getY() < 0) {
54 throw LSST_EXCEPT(pex::exceptions::LengthError,
55 str(boost::format(
"Both width and height must be non-negative: %d, %d") %
56 dimensions.getX() % dimensions.getY()));
59 throw LSST_EXCEPT(pex::exceptions::LengthError,
60 str(boost::format(
"Image dimensions (%d x %d) too large; int overflow detected.") %
61 dimensions.getX() % dimensions.getY()));
63 std::pair<Manager::Ptr, PixelT*> r =
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()) {
76 throw LSST_EXCEPT(pex::exceptions::LengthError,
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>
163 throw LSST_EXCEPT(pex::exceptions::LengthError,
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())) {
188 pex::exceptions::LengthError,
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())) {
207 throw LSST_EXCEPT(pex::exceptions::LengthError,
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>
231 template <
typename PixelT>
233 int rowStride =
reinterpret_cast<PixelT*
>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
234 return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
235 ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
239 template <
typename PixelT>
241 int rowStride =
reinterpret_cast<PixelT*
>(row_begin(1)) - reinterpret_cast<PixelT*>(row_begin(0));
242 return ndarray::external(reinterpret_cast<PixelT*>(row_begin(0)),
243 ndarray::makeVector(getHeight(), getWidth()), ndarray::makeVector(rowStride, 1),
249 template <
typename PixelT>
251 return _gilView.
begin();
254 template <
typename PixelT>
256 return _gilView.end();
259 template <
typename PixelT>
264 template <
typename PixelT>
266 return _gilView.
rend();
269 template <
typename PixelT>
271 return _gilView.
at(x, y);
274 template <
typename PixelT>
277 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Only contiguous == true makes sense");
279 if (!this->isContiguous()) {
280 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Image's pixels are not contiguous");
286 template <
typename PixelT>
289 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Only contiguous == true makes sense");
291 if (!this->isContiguous()) {
292 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Image's pixels are not contiguous");
295 return row_end(getHeight() - 1);
298 template <
typename PixelT>
300 fill_pixels(_gilView, rhs);
308 template <
typename PixelT>
311 *
this = initialValue;
314 template <
typename PixelT>
316 *
this = initialValue;
319 template <
typename PixelT>
321 *
this = initialValue;
324 template <
typename PixelT>
327 template <
typename PixelT>
331 :
ImageBase<PixelT>(rhs, bbox, origin, deep) {}
333 template <
typename PixelT>
340 template <
typename PixelT>
347 #ifndef DOXYGEN // doc for this section has been moved to header 349 template <
typename PixelT>
350 Image<PixelT>::Image(std::string
const& fileName,
int hdu, std::shared_ptr<daf::base::PropertySet> metadata,
353 fits::Fits fitsfile(fileName,
"r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
356 *
this =
Image(fitsfile, metadata, bbox, origin);
360 LSST_EXCEPT_ADD(e, str(boost::format(
"HDU %d has NAXIS == 0") % hdu));
365 template <
typename PixelT>
367 std::shared_ptr<daf::base::PropertySet> metadata,
geom::Box2I const& bbox,
370 fits::Fits fitsfile(manager,
"r", fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
372 *
this =
Image(fitsfile, metadata, bbox, origin);
375 template <
typename PixelT>
379 typedef boost::mpl::vector<
unsigned char,
unsigned short, short, int,
unsigned int, float, double,
387 fits_read_image<fits_image_types>(fitsfile, *
this, *metadata, bbox, origin);
390 template <
typename PixelT>
392 std::shared_ptr<daf::base::PropertySet const> metadata_i,
393 std::string
const& mode)
const {
394 fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
395 writeFits(fitsfile, metadata_i);
398 template <
typename PixelT>
400 std::shared_ptr<daf::base::PropertySet const> metadata_i,
401 std::string
const& mode)
const {
402 fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
403 writeFits(fitsfile, metadata_i);
406 template <
typename PixelT>
408 std::shared_ptr<daf::base::PropertySet const> metadata_i)
const {
409 std::shared_ptr<daf::base::PropertySet> metadata;
410 std::shared_ptr<daf::base::PropertySet> wcsAMetadata =
413 metadata = metadata_i->deepCopy();
414 metadata->combine(wcsAMetadata);
416 metadata = wcsAMetadata;
423 template <
typename PixelT>
430 template <
typename PixelT>
436 template <
typename PixelT>
438 transform_pixels(_getRawView(), _getRawView(),
439 [](PixelT
const& l) -> PixelT {
return static_cast<PixelT
>(std::sqrt(l)); });
442 template <
typename PixelT>
444 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l + rhs; });
448 template <
typename PixelT>
451 throw LSST_EXCEPT(pex::exceptions::LengthError,
452 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
456 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
457 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l + r; });
461 template <
typename PixelT>
463 for (
int y = 0; y != this->getHeight(); ++y) {
468 *ptr +=
function(xPos, yPos);
474 template <
typename PixelT>
477 throw LSST_EXCEPT(pex::exceptions::LengthError,
478 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
484 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l +
static_cast<PixelT
>(c * r); });
487 template <
typename PixelT>
489 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l - rhs; });
493 template <
typename PixelT>
496 throw LSST_EXCEPT(pex::exceptions::LengthError,
497 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
501 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
502 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l - r; });
506 template <
typename PixelT>
509 throw LSST_EXCEPT(pex::exceptions::LengthError,
510 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
516 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l -
static_cast<PixelT
>(c * r); });
519 template <
typename PixelT>
521 for (
int y = 0; y != this->getHeight(); ++y) {
526 *ptr -=
function(xPos, yPos);
532 template <
typename PixelT>
534 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l * rhs; });
538 template <
typename PixelT>
541 throw LSST_EXCEPT(pex::exceptions::LengthError,
542 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
546 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
547 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l * r; });
551 template <
typename PixelT>
554 throw LSST_EXCEPT(pex::exceptions::LengthError,
555 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
561 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l *
static_cast<PixelT
>(c * r); });
564 template <
typename PixelT>
566 transform_pixels(_getRawView(), _getRawView(), [&rhs](PixelT
const& l) -> PixelT {
return l / rhs; });
574 double const irhs = 1 / rhs;
581 float const irhs = 1 / rhs;
586 template <
typename PixelT>
589 throw LSST_EXCEPT(pex::exceptions::LengthError,
590 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
594 transform_pixels(_getRawView(), rhs.
_getRawView(), _getRawView(),
595 [](PixelT
const& l, PixelT
const& r) -> PixelT {
return l / r; });
599 template <
typename PixelT>
602 throw LSST_EXCEPT(pex::exceptions::LengthError,
603 (boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
609 [&c](PixelT
const& l, PixelT
const& r) -> PixelT {
return l /
static_cast<PixelT
>(c * r); });
616 template <
typename LhsPixelT,
typename RhsPixelT>
617 struct plusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
618 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs + rhs); }
621 template <
typename LhsPixelT,
typename RhsPixelT>
622 struct minusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
623 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs - rhs); }
626 template <
typename LhsPixelT,
typename RhsPixelT>
627 struct timesEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
628 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs * rhs); }
631 template <
typename LhsPixelT,
typename RhsPixelT>
632 struct divideEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
633 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const {
return static_cast<LhsPixelT
>(lhs / rhs); }
637 template <
typename LhsPixelT,
typename RhsPixelT>
643 template <
typename LhsPixelT,
typename RhsPixelT>
649 template <
typename LhsPixelT,
typename RhsPixelT>
655 template <
typename LhsPixelT,
typename RhsPixelT>
663 geom::Extent2I dims{metadata.getAsInt(
"NAXIS1"), metadata.getAsInt(
"NAXIS2")};
672 #define INSTANTIATE_OPERATOR(OP_EQ, T) \ 673 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint16_t> const& rhs); \ 674 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<int> const& rhs); \ 675 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<float> const& rhs); \ 676 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<double> const& rhs); \ 677 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint64_t> const& rhs); 679 #define INSTANTIATE(T) \ 680 template class ImageBase<T>; \ 681 template class Image<T>; \ 682 INSTANTIATE_OPERATOR(+=, T); \ 683 INSTANTIATE_OPERATOR(-=, T); \ 684 INSTANTIATE_OPERATOR(*=, T); \ 685 INSTANTIATE_OPERATOR(/=, T) int getImageDim()
Return the number of dimensions in the current HDU.
Point2I const getMin() const
afw::table::PointKey< int > dimensions
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 swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
void operator/=(ExtentBase< int, N > &lhs, double rhs)
int getHeight() const
Return the number of rows in the image.
_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.
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.
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.
geom::Point2I getImageXY0FromMetadata(std::string const &wcsName, lsst::daf::base::PropertySet *metadata)
std::shared_ptr< lsst::daf::base::PropertyList > createTrivialWcsAsPropertySet(std::string const &wcsName, int const x0=0, int const y0=0)
Lifetime-management for memory that goes into FITS memory files.
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 fits_write_image(fits::Fits &fitsfile, const ImageT &image, std::shared_ptr< daf::base::PropertySet const > metadata=std::shared_ptr< daf::base::PropertySet const >())
void swap(ImageBase &rhs)
void setHdu(int hdu, bool relative=false)
Set the current HDU.
ndarray::Array< PixelT const, 2, 1 > ConstArray
An immutable ndarray representation of the image.
Extent< double, N > & operator+=(Extent< double, N > &lhs, Extent< int, N > const &rhs)
reverse_iterator rend() const
Return an STL compliant reverse iterator to the end of the image.
Extent2I const getDimensions() const
lsst::daf::base::PropertySet PropertySet
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)
_view_t _getRawView() const
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
lsst::daf::base::PropertyList PropertyList