30 #include "boost/format.hpp"
31 #include "boost/gil.hpp"
44 template <
typename PixelT>
46 Manager::Ptr& manager) {
49 str(
boost::format(
"Both width and height must be non-negative: %d, %d") %
54 str(
boost::format(
"Image dimensions (%d x %d) too large; int overflow detected.") %
61 (
typename _view_t::value_type*)r.second,
64 template <
typename PixelT>
67 const _view_t& view) {
68 if (offset.getX() < 0 || offset.getY() < 0 || offset.getX() +
dimensions.getX() > view.width() ||
69 offset.getY() +
dimensions.getY() > view.height()) {
73 "Box2I(Point2I(%d,%d),lsst::geom::Extent2I(%d,%d)) doesn't fit in image %dx%d") %
79 && view.width() == 0 && view.height() == 0) {
83 return boost::gil::subimage_view(view, offset.getX(), offset.getY(),
dimensions.getX(),
88 template <
typename PixelT>
90 : _origin(0, 0), _manager(), _gilView(_allocateView(
dimensions, _manager)) {}
92 template <
typename PixelT>
94 : _origin(
bbox.getMin()), _manager(), _gilView(_allocateView(
bbox.getDimensions(), _manager)) {}
96 template <
typename PixelT>
100 : _origin(rhs._origin), _manager(rhs._manager), _gilView(rhs._gilView) {
108 template <
typename PixelT>
111 template <
typename PixelT>
117 _manager(rhs._manager),
118 _gilView(_makeSubView(
bbox.getDimensions(), _origin - rhs._origin, rhs._gilView)) {
126 template <
typename PixelT>
129 _manager(array.getManager()),
130 _gilView(
boost::gil::interleaved_view(array.template
getSize<1>(), array.template
getSize<0>(),
131 (typename _view_t::value_type*)array.getData(),
132 array.template getStride<0>() * sizeof(
PixelT))) {
139 template <
typename PixelT>
147 template <
typename PixelT>
152 template <
typename PixelT>
154 auto lhsDim =
bbox.isEmpty() ? getDimensions() :
bbox.getDimensions();
157 (
boost::format(
"Dimension mismatch: %dx%d v. %dx%d") % lhsDim.getX() %
161 if (
bbox.isEmpty()) {
162 copy_pixels(rhs._gilView, _gilView);
165 auto lhsGilView = _makeSubView(lhsDim, lhsOff, _gilView);
166 copy_pixels(rhs._gilView, lhsGilView);
170 template <
typename PixelT>
176 template <
typename PixelT>
179 if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
182 (getWidth() - 1) % (getHeight() - 1))
190 template <
typename PixelT>
192 return _gilView(
x,
y)[0];
195 template <
typename PixelT>
198 if (check && (x < 0 || x >= getWidth() || y < 0 || y >= getHeight())) {
200 (
boost::format(
"Index (%d, %d) is out of range [0--%d], [0--%d]") %
x %
y %
201 (this->getWidth() - 1) % (this->getHeight() - 1))
205 return _gilView(
x,
y)[0];
208 template <
typename PixelT>
211 int x = index.getX();
212 int y = index.getY();
217 return _gilView(
x,
y)[0];
220 template <
typename PixelT>
223 int x = index.getX();
224 int y = index.getY();
229 return _gilView(
x,
y)[0];
232 template <
typename PixelT>
236 swap(_manager, rhs._manager);
237 swap(_gilView, rhs._gilView);
238 swap(_origin, rhs._origin);
241 template <
typename PixelT>
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>
279 if (!this->isContiguous()) {
286 template <
typename PixelT>
291 if (!this->isContiguous()) {
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>
317 *
this = initialValue;
320 template <
typename PixelT>
322 *
this = initialValue;
325 template <
typename PixelT>
328 template <
typename PixelT>
331 template <
typename PixelT>
336 template <
typename PixelT>
343 template <
typename PixelT>
350 template <
typename PixelT>
357 template <
typename PixelT>
367 template <
typename PixelT>
374 metadata->combine(reader.readMetadata());
378 template <
typename PixelT>
388 template <
typename PixelT>
396 template <
typename PixelT>
404 template <
typename PixelT>
410 template <
typename PixelT>
418 template <
typename PixelT>
426 template <
typename PixelT>
430 fitsfile.writeImage(*
this, options, header,
mask);
435 template <
typename PixelT>
441 template <
typename PixelT>
447 template <
typename PixelT>
449 transform_pixels(_getRawView(), _getRawView(),
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(),
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() %
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(),
517 template <
typename PixelT>
521 (
boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
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(),
562 template <
typename PixelT>
566 (
boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
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(),
610 template <
typename PixelT>
614 (
boost::format(
"Images are of different size, %dx%d v %dx%d") % this->getWidth() %
627 template <
typename LhsPixelT,
typename RhsPixelT>
628 struct plusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
629 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const override {
630 return static_cast<LhsPixelT
>(lhs + rhs);
634 template <
typename LhsPixelT,
typename RhsPixelT>
635 struct minusEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
636 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const override {
637 return static_cast<LhsPixelT
>(lhs - rhs);
641 template <
typename LhsPixelT,
typename RhsPixelT>
642 struct timesEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
643 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const override {
644 return static_cast<LhsPixelT
>(lhs * rhs);
648 template <
typename LhsPixelT,
typename RhsPixelT>
649 struct divideEq :
public pixelOp2<LhsPixelT, RhsPixelT> {
650 LhsPixelT operator()(LhsPixelT lhs, RhsPixelT rhs)
const override {
651 return static_cast<LhsPixelT
>(lhs / rhs);
656 template <
typename LhsPixelT,
typename RhsPixelT>
662 template <
typename LhsPixelT,
typename RhsPixelT>
668 template <
typename LhsPixelT,
typename RhsPixelT>
674 template <
typename LhsPixelT,
typename RhsPixelT>
682 if (metadata.
exists(
"ZNAXIS1") && metadata.
exists(
"ZNAXIS2")) {
691 template <
typename T1,
typename T2>
702 auto beg1Addr = arr1.front().begin();
703 auto end1Addr = arr1.back().end();
706 auto beg2Addr = arr2.front().begin();
707 auto end2Addr = arr2.back().end();
710 return ptrLess(beg1Addr, end2Addr) && ptrLess(beg2Addr, end1Addr);
717 #define INSTANTIATE_OPERATOR(OP_EQ, T) \
718 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint16_t> const& rhs); \
719 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<int> const& rhs); \
720 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<float> const& rhs); \
721 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<double> const& rhs); \
722 template Image<T>& operator OP_EQ(Image<T>& lhs, Image<std::uint64_t> const& rhs);
724 #define INSTANTIATE(T) \
725 template class ImageBase<T>; \
726 template class Image<T>; \
727 INSTANTIATE_OPERATOR(+=, T); \
728 INSTANTIATE_OPERATOR(-=, T); \
729 INSTANTIATE_OPERATOR(*=, T); \
730 INSTANTIATE_OPERATOR(/=, T)
732 #define INSTANTIATE2(T1, T2) template bool imagesOverlap<T1, T2>(ImageBase<T1> const&, ImageBase<T2> const&);
#define INSTANTIATE(FROMSYS, TOSYS)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
afw::table::PointKey< int > dimensions
#define INSTANTIATE2(ImagePixelT1, ImagePixelT2)
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
void writeImage(ndarray::Array< T const, N, C > const &array)
Write an ndarray::Array to a FITS image HDU.
Lifetime-management for memory that goes into FITS memory files.
A class used to request that array accesses be checked.
std::shared_ptr< daf::base::PropertyList > readMetadata()
Read the image's FITS header.
The base class for all image classed (Image, Mask, MaskedImage, ...)
iterator end() const
Return an STL compliant iterator to the end of the image.
iterator begin() const
Return an STL compliant iterator to the start of the image.
static _view_t _allocateView(lsst::geom::Extent2I const &dimensions, Manager::Ptr &manager)
typename Reference< PixelT >::type PixelReference
A Reference to a PixelT.
typename _view_t::iterator iterator
An STL compliant iterator.
PixelReference operator()(int x, int y)
Return a reference to the pixel (x, y) in LOCAL coordinates.
static _view_t _makeSubView(lsst::geom::Extent2I const &dimensions, lsst::geom::Extent2I const &offset, const _view_t &view)
int getWidth() const
Return the number of columns in the image.
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
int getArea() const
Return the area of the image.
lsst::geom::Extent2I getDimensions() const
Return the image's size; useful for passing to constructors.
typename ndarray::Array< PixelT, 2, 1 > Array
A mutable ndarray representation of the image.
void assign(ImageBase const &rhs, lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT)
Copy pixels from another image to a specified subregion of this image.
x_iterator fast_iterator
A fast STL compliant iterator for contiguous images N.b.
typename _view_t::reverse_iterator reverse_iterator
An STL compliant reverse iterator.
int getHeight() const
Return the number of rows in the image.
ImageBase & operator=(const ImageBase &rhs)
Shallow assignment operator.
iterator at(int x, int y) const
Return an STL compliant iterator at the point (x, y)
typename _view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
reverse_iterator rbegin() const
Return an STL compliant reverse iterator to the start of the image.
_view_t _getRawView() const
PixelReference get(lsst::geom::Point2I const &index, ImageOrigin origin)
Return a reference to a single pixel (with no bounds check).
void swap(ImageBase &rhs)
typename ConstReference< PixelT >::type PixelConstReference
A ConstReference to a PixelT.
reverse_iterator rend() const
Return an STL compliant reverse iterator to the end of the image.
A FITS reader class for regular Images.
Image< PixelT > read(lsst::geom::Box2I const &bbox=lsst::geom::Box2I(), ImageOrigin origin=PARENT, bool allowUnsafe=false)
Read the Image.
A class to represent a 2-dimensional array of pixels.
Image & operator*=(PixelT const rhs)
Multiply lhs by scalar rhs.
Image & operator-=(PixelT const rhs)
Subtract scalar rhs from lhs.
void scaledPlus(PixelT const c, Image< PixelT > const &rhs)
Add Image c*rhs to lhs.
Image & operator=(const PixelT rhs)
Set the image's pixels to rhs.
Image & operator+=(PixelT const rhs)
Add scalar rhs to lhs.
void scaledMinus(PixelT const c, Image< PixelT > const &rhs)
Subtract Image c*rhs from lhs.
Image & operator/=(PixelT const rhs)
Divide lhs by scalar rhs.
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.
void scaledDivides(PixelT const c, Image< PixelT > const &rhs)
Divide lhs by Image c*rhs (i.e. pixel-by-pixel division)
void scaledMultiplies(PixelT const c, Image< PixelT > const &rhs)
Multiply lhs by Image c*rhs (i.e. pixel-by-pixel multiplication)
A Function taking two arguments.
Class for storing generic metadata.
int getAsInt(std::string const &name) const
Get the last value for a bool/char/short/int property name (possibly hierarchical).
bool exists(std::string const &name) const
Determine if a name (possibly hierarchical) exists.
An integer coordinate rectangle.
Reports attempts to exceed implementation-defined length limits for some classes.
Reports errors that are due to events beyond the control of the program.
lsst::geom::Point2I getImageXY0FromMetadata(daf::base::PropertySet &metadata, std::string const &wcsName, bool strip=false)
std::string const wcsNameForXY0
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
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< LhsPixelT > & operator-=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Subtract lhs from Image rhs (i.e. pixel-by-pixel subtraction) where types are different.
void for_each_pixel(Image< LhsT > &lhs, pixelOp0< LhsT > const &func)
Set each pixel in an Image<LhsT> to func()
lsst::geom::Box2I bboxFromMetadata(daf::base::PropertySet &metadata)
Determine the image bounding box from its metadata (FITS header)
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.
double indexToPosition(double ind)
Convert image index to image position.
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.
bool imagesOverlap(ImageBase< T1 > const &image1, ImageBase< T2 > const &image2)
Return true if the pixels for two images or masks overlap in memory.
void swap(Image< PixelT > &a, Image< PixelT > &b)
Extent< int, 2 > Extent2I
def writeFits(filename, stamps, metadata, type_name, write_mask, write_variance, write_archive=False)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
A base class for image defects.
Options for writing an image to FITS.
A functor class equivalent to std::function<LhsT (LhsT, RhsT)>, but with a virtual operator()