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)),
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>
234 template <
typename PixelT>
236 return _gilView.begin();
239 template <
typename PixelT>
241 return _gilView.end();
244 template <
typename PixelT>
246 return _gilView.rbegin();
249 template <
typename PixelT>
251 return _gilView.rend();
254 template <
typename PixelT>
256 return _gilView.at(x, y);
259 template <
typename PixelT>
262 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Only contiguous == true makes sense");
264 if (!this->isContiguous()) {
265 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Image's pixels are not contiguous");
271 template <
typename PixelT>
274 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Only contiguous == true makes sense");
276 if (!this->isContiguous()) {
277 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Image's pixels are not contiguous");
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>
335 Image<PixelT>::Image(std::string
const& fileName,
int hdu, std::shared_ptr<daf::base::PropertySet> metadata,
339 fitsfile.setHdu(hdu);
341 *
this =
Image(fitsfile, metadata, bbox, origin);
344 if (fitsfile.getImageDim() == 0) {
345 LSST_EXCEPT_ADD(e, str(boost::format(
"HDU %d has NAXIS == 0") % hdu));
350 template <
typename PixelT>
352 std::shared_ptr<daf::base::PropertySet> metadata, geom::Box2I
const& bbox,
354 : ImageBase<PixelT>() {
356 fitsfile.setHdu(hdu);
357 *
this =
Image(fitsfile, metadata, bbox, origin);
360 template <
typename PixelT>
363 : ImageBase<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>
377 std::shared_ptr<daf::base::PropertySet const> metadata_i,
378 std::string
const& mode)
const {
380 writeFits(fitsfile, metadata_i);
383 template <
typename PixelT>
385 std::shared_ptr<daf::base::PropertySet const> metadata_i,
386 std::string
const& mode)
const {
388 writeFits(fitsfile, metadata_i);
391 template <
typename PixelT>
393 std::shared_ptr<daf::base::PropertySet const> metadata)
const {
394 fitsfile.writeImage(*
this, fits::ImageWriteOptions(*
this), metadata);
397 template <
typename PixelT>
399 std::string
const& filename,
400 fits::ImageWriteOptions
const& options,
401 std::string
const& mode,
402 std::shared_ptr<daf::base::PropertySet const> header,
403 std::shared_ptr<Mask<MaskPixel>
const>
mask
406 writeFits(fitsfile, options, header,
mask);
409 template <
typename PixelT>
411 fits::MemFileManager& manager,
412 fits::ImageWriteOptions
const& options,
413 std::string
const& mode,
414 std::shared_ptr<daf::base::PropertySet const> header,
415 std::shared_ptr<Mask<MaskPixel>
const>
mask
418 writeFits(fitsfile, options, header,
mask);
421 template <
typename PixelT>
423 fits::Fits& fitsfile,
424 fits::ImageWriteOptions
const& options,
425 std::shared_ptr<daf::base::PropertySet const> header,
426 std::shared_ptr<Mask<MaskPixel>
const>
mask
428 fitsfile.writeImage(*
this, options, header,
mask);
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>
462 throw LSST_EXCEPT(pex::exceptions::LengthError,
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>
488 throw LSST_EXCEPT(pex::exceptions::LengthError,
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>
507 throw LSST_EXCEPT(pex::exceptions::LengthError,
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>
520 throw LSST_EXCEPT(pex::exceptions::LengthError,
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>
552 throw LSST_EXCEPT(pex::exceptions::LengthError,
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>
565 throw LSST_EXCEPT(pex::exceptions::LengthError,
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>
600 throw LSST_EXCEPT(pex::exceptions::LengthError,
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>
613 throw LSST_EXCEPT(pex::exceptions::LengthError,
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")) {
676 dims =
geom::Extent2I(metadata.getAsInt(
"ZNAXIS1"), metadata.getAsInt(
"ZNAXIS2"));
678 dims =
geom::Extent2I(metadata.getAsInt(
"NAXIS1"), metadata.getAsInt(
"NAXIS2"));
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)
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.
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.
geom::Box2I bboxFromMetadata(daf::base::PropertySet &metadata)
Determine the image bounding box from its metadata (FITS header)
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)
reverse_iterator rbegin() const
Return an STL compliant reverse iterator to the start of the image.
void swap(Image< PixelT > &a, Image< PixelT > &b)
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.
lsst::daf::base::PropertySet PropertySet
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. ...
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.
geom::Point2I getImageXY0FromMetadata(std::string const &wcsName, lsst::daf::base::PropertySet *metadata)
#define INSTANTIATE(FROMSYS, TOSYS)
void scaledMinus(double const c, Image< PixelT > const &rhs)
Subtract Image c*rhs from lhs.
lsst::daf::base::PropertyList PropertyList