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()));
58 if (dimensions.getX() != 0 && dimensions.getY() > std::numeric_limits<int>::max() / dimensions.getX()) {
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)),
98 _gilView(_allocateView(bbox.getDimensions(), _manager)) {}
100 template <
typename PixelT>
104 : daf::base::Citizen(typeid(this)),
106 _manager(rhs._manager),
107 _gilView(rhs._gilView) {
115 template <
typename PixelT>
120 : daf::base::Citizen(typeid(this)),
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);
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>
261 return _gilView.rbegin();
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,
354 fitsfile.setHdu(hdu);
356 *
this =
Image(fitsfile, metadata, bbox, origin);
359 if (fitsfile.getImageDim() == 0) {
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,
369 : ImageBase<PixelT>() {
371 fitsfile.setHdu(hdu);
372 *
this =
Image(fitsfile, metadata, bbox, origin);
375 template <
typename PixelT>
378 : ImageBase<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 {
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 {
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>
665 #define INSTANTIATE_OPERATOR(OP_EQ, T) \
666 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint16_t> const& rhs); \
667 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<int> const& rhs); \
668 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<float> const& rhs); \
669 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<double> const& rhs); \
670 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint64_t> const& rhs);
672 #define INSTANTIATE(T) \
673 template class ImageBase<T>; \
674 template class Image<T>; \
675 INSTANTIATE_OPERATOR(+=, T); \
676 INSTANTIATE_OPERATOR(-=, T); \
677 INSTANTIATE_OPERATOR(*=, T); \
678 INSTANTIATE_OPERATOR(/=, T)
Image & operator=(const PixelT rhs)
Set the image's pixels to rhs.
afw::table::PointKey< int > dimensions
iterator begin() const
Return an STL compliant iterator to the start of the image.
A functor class equivalent to tr1::function<LhsT (LhsT, RhsT)>, but with a virtual operator() ...
Image & operator*=(PixelT const rhs)
Multiply lhs by scalar rhs.
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.
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< PixelT > &a, ImageBase< PixelT > &b)
Image & operator/=(PixelT const rhs)
Divide lhs by scalar rhs.
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
void scaledDivides(double const c, Image< PixelT > const &rhs)
Divide lhs by Image c*rhs (i.e. pixel-by-pixel division)
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::Box2I getBBox(ImageOrigin origin=PARENT) const
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.
iterator at(int x, int y) const
Return an STL compliant iterator at the point (x, y)
An integer coordinate rectangle.
Image & operator-=(PixelT const rhs)
Subtract scalar rhs from lhs.
_view_t::iterator iterator
An STL compliant iterator.
An exception thrown when problems are found when reading or writing FITS files.
ImageBase & operator<<=(const ImageBase &rhs)
Set the lhs's pixel values to equal the rhs's.
ImageBase & operator=(const ImageBase &rhs)
Shallow assignment operator.
geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
void writeFits(std::string const &fileName, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet const >(), std::string const &mode="w") const
Write an image to a regular FITS file.
_view_t _getRawView() const
Image & operator+=(PixelT const rhs)
Add scalar rhs to lhs.
A class used to request that array accesses be checked.
int getHeight() const
Return the number of rows in the image.
void scaledPlus(double const c, Image< PixelT > const &rhs)
Add Image c*rhs to lhs.
bool isEmpty() const
Return true if the box contains no points.
void swap(ImageBase &rhs)
ndarray::Array< PixelT const, 2, 1 > ConstArray
An immutable ndarray representation of the image.
reverse_iterator rbegin() const
Return an STL compliant reverse iterator to the start of the image.
reverse_iterator rend() const
Return an STL compliant reverse iterator to the end of the image.
Image< LhsPixelT > & operator*=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Multiply lhs by Image rhs (i.e. pixel-by-pixel multiplication) where types are different.
void scaledMultiplies(double const c, Image< PixelT > const &rhs)
Multiply lhs by Image c*rhs (i.e. pixel-by-pixel multiplication)
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.
static _view_t _allocateView(geom::Extent2I const &dimensions, Manager::Ptr &manager)
Reference< PixelT >::type PixelReference
A Reference to a PixelT.
Image< LhsPixelT > & operator/=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Divide lhs by Image rhs (i.e. pixel-by-pixel division) where types are different. ...
std::shared_ptr< lsst::daf::base::PropertyList > createTrivialWcsAsPropertySet(std::string const &wcsName, int const x0=0, int const y0=0)
static _view_t _makeSubView(geom::Extent2I const &dimensions, geom::Extent2I const &offset, const _view_t &view)
Point2I const getMin() const
int getWidth() const
Return the number of columns in the image.
ndarray::Array< PixelT, 2, 1 > Array
A mutable ndarray representation of the image.
A class to represent a 2-dimensional array of pixels.
PixelReference operator()(int x, int y)
Return a reference to the pixel (x, y)
std::string const wcsNameForXY0
iterator end() const
Return an STL compliant iterator to the end of the image.
void scaledMinus(double const c, Image< PixelT > const &rhs)
Subtract Image c*rhs from lhs.
lsst::daf::base::PropertyList PropertyList