24#include "pybind11/pybind11.h"
25#include "pybind11/stl.h"
27#include "ndarray/pybind11.h"
36using namespace pybind11::literals;
44template <
typename PixelT>
45using PyImageBase = py::class_<ImageBase<PixelT>, std::shared_ptr<ImageBase<PixelT>>>;
47template <
typename PixelT>
48using PyImage = py::class_<Image<PixelT>, std::shared_ptr<Image<PixelT>>,
ImageBase<PixelT>>;
50template <
typename PixelT>
51using PyDecoratedImage = py::class_<DecoratedImage<PixelT>, std::shared_ptr<DecoratedImage<PixelT>>>;
53template <
typename MaskPixelT>
64template <
typename FromPixelT,
typename ToPixelT>
65static void declareCastConstructor(PyImage<ToPixelT> &cls) {
69template <
typename PixelT>
70static void declareImageBase(lsst::cpputils::python::WrapperCollection &wrappers, std::string
const &suffix) {
72 wrappers.
wrapType(PyImageBase<PixelT>(wrappers.
module, (
"ImageBase" + suffix).c_str()), [](
auto &mod,
74 cls.def(py::init<lsst::geom::Extent2I const &>(),
"dimensions"_a = lsst::geom::Extent2I());
75 cls.def(py::init<ImageBase<PixelT> const &, bool>(),
"src"_a,
"deep"_a = false);
76 cls.def(py::init<ImageBase<PixelT> const &, lsst::geom::Box2I const &, ImageOrigin, bool>(),
"src"_a,
77 "bbox"_a,
"origin"_a = PARENT,
"deep"_a = false);
78 cls.def(py::init<Array const &, bool, lsst::geom::Point2I const &>(),
"array"_a,
"deep"_a = false,
79 "xy0"_a = lsst::geom::Point2I());
81 cls.def(
"assign", &ImageBase<PixelT>::assign,
"rhs"_a,
"bbox"_a = lsst::geom::Box2I(),
85 cls.def(
"getWidth", &ImageBase<PixelT>::getWidth);
86 cls.def(
"getHeight", &ImageBase<PixelT>::getHeight);
87 cls.def(
"getX0", &ImageBase<PixelT>::getX0);
88 cls.def(
"getY0", &ImageBase<PixelT>::getY0);
89 cls.def(
"getXY0", &ImageBase<PixelT>::getXY0);
90 cls.def(
"positionToIndex", &ImageBase<PixelT>::positionToIndex,
"position"_a,
"xOrY"_a);
91 cls.def(
"indexToPosition", &ImageBase<PixelT>::indexToPosition,
"index"_a,
"xOrY"_a);
92 cls.def(
"getDimensions", &ImageBase<PixelT>::getDimensions);
93 cls.def(
"getArray", (Array(ImageBase<PixelT>::*)()) & ImageBase<PixelT>::getArray);
94 cls.def_property(
"array", (Array(ImageBase<PixelT>::*)()) & ImageBase<PixelT>::getArray,
95 [](ImageBase<PixelT> &self, ndarray::Array<PixelT const, 2, 0> const &array) {
96 if (array.isEmpty()) {
97 throw py::type_error(
"Image array may not be None.");
101 if (array.shallow() != self.getArray().shallow()) {
102 self.getArray().deep() = array;
117 img.get(index, origin) = val;
119 "index"_a,
"value"_a,
"origin"_a);
124 return img.get(index, origin);
126 "index"_a,
"origin"_a);
130template <
typename MaskPixelT>
131static void declareMask(lsst::cpputils::python::WrapperCollection &wrappers, std::string
const &suffix) {
132 wrappers.
wrapType(PyMask<MaskPixelT>(wrappers.
module, (
"Mask" + suffix).c_str()), [](
auto &mod,
135 cls.def(py::init<unsigned int, unsigned int, typename Mask<MaskPixelT>::MaskPlaneDict const &>(),
136 "width"_a,
"height"_a,
"planeDefs"_a = typename Mask<MaskPixelT>::MaskPlaneDict());
137 cls.def(py::init<unsigned int, unsigned int, MaskPixelT,
138 typename Mask<MaskPixelT>::MaskPlaneDict const &>(),
139 "width"_a,
"height"_a,
"initialValue"_a,
140 "planeDefs"_a = typename Mask<MaskPixelT>::MaskPlaneDict());
141 cls.def(py::init<lsst::geom::Extent2I const &, typename Mask<MaskPixelT>::MaskPlaneDict const &>(),
142 "dimensions"_a = lsst::geom::Extent2I(),
143 "planeDefs"_a = typename Mask<MaskPixelT>::MaskPlaneDict());
144 cls.def(py::init<lsst::geom::Extent2I const &, MaskPixelT,
145 typename Mask<MaskPixelT>::MaskPlaneDict const &>(),
146 "dimensions"_a = lsst::geom::Extent2I(),
"initialValue"_a,
147 "planeDefs"_a = typename Mask<MaskPixelT>::MaskPlaneDict());
148 cls.def(py::init<lsst::geom::Box2I const &, typename Mask<MaskPixelT>::MaskPlaneDict const &>(),
149 "bbox"_a,
"planeDefs"_a = typename Mask<MaskPixelT>::MaskPlaneDict());
150 cls.def(py::init<lsst::geom::Box2I const &, MaskPixelT,
151 typename Mask<MaskPixelT>::MaskPlaneDict const &>(),
152 "bbox"_a,
"initialValue"_a,
"planeDefs"_a = typename Mask<MaskPixelT>::MaskPlaneDict());
153 cls.def(py::init<const Mask<MaskPixelT> &, const bool>(),
"src"_a,
"deep"_a = false);
154 cls.def(py::init<const Mask<MaskPixelT> &, const lsst::geom::Box2I &, ImageOrigin const,
156 "src"_a,
"bbox"_a,
"origin"_a = PARENT,
"deep"_a = false);
157 cls.def(py::init<ndarray::Array<MaskPixelT, 2, 1> const &, bool, lsst::geom::Point2I const &>(),
158 "array"_a,
"deep"_a = false,
"xy0"_a = lsst::geom::Point2I());
159 cls.def(py::init<std::string const &, int, std::shared_ptr<lsst::daf::base::PropertySet>,
160 lsst::geom::Box2I const &, ImageOrigin, bool, bool>(),
161 "fileName"_a,
"hdu"_a = fits::DEFAULT_HDU,
"metadata"_a = nullptr,
162 "bbox"_a = lsst::geom::Box2I(),
"origin"_a = PARENT,
"conformMasks"_a = false,
163 "allowUnsafe"_a = false);
164 cls.def(py::init<fits::MemFileManager &, int, std::shared_ptr<lsst::daf::base::PropertySet>,
165 lsst::geom::Box2I const &, ImageOrigin, bool, bool>(),
166 "manager"_a,
"hdu"_a = fits::DEFAULT_HDU,
"metadata"_a = nullptr,
167 "bbox"_a = lsst::geom::Box2I(),
"origin"_a = PARENT,
"conformMasks"_a = false,
168 "allowUnsafe"_a = false);
169 cls.def(py::init<fits::Fits &, std::shared_ptr<lsst::daf::base::PropertySet>,
170 lsst::geom::Box2I const &, ImageOrigin, bool, bool>(),
171 "fitsFile"_a,
"metadata"_a = nullptr,
"bbox"_a = lsst::geom::Box2I(),
"origin"_a = PARENT,
172 "conformMasks"_a = false,
"allowUnsafe"_a = false);
175 cls.def(
"__ior__", [](Mask<MaskPixelT> &self, Mask<MaskPixelT> &other) { return self |= other; });
176 cls.def(
"__ior__", [](
Mask<MaskPixelT> &self, MaskPixelT
const other) {
return self |= other; });
177 cls.def(
"__ior__", [](
Mask<MaskPixelT> &self,
int other) {
return self |= other; });
179 cls.def(
"__iand__", [](
Mask<MaskPixelT> &self, MaskPixelT
const other) {
return self &= other; });
180 cls.def(
"__iand__", [](
Mask<MaskPixelT> &self,
int other) {
return self &= other; });
182 cls.def(
"__ixor__", [](
Mask<MaskPixelT> &self, MaskPixelT
const other) {
return self ^= other; });
183 cls.def(
"__ixor__", [](
Mask<MaskPixelT> &self,
int other) {
return self ^= other; });
189 daf::base::PropertySet
const *,
190 std::string
const &)
const) &
192 "fileName"_a,
"metadata"_a =
nullptr,
"mode"_a =
"w");
195 daf::base::PropertySet
const *,
196 std::string
const &)
const) &
198 "manager"_a,
"metadata"_a =
nullptr,
"mode"_a =
"w");
203 "fitsfile"_a,
"metadata"_a =
nullptr);
205 (
void (
Mask<MaskPixelT>::*)(std::string
const &, fits::ImageWriteOptions
const &,
207 daf::base::PropertySet
const *)
const) &
209 "filename"_a,
"options"_a,
"mode"_a =
"w",
210 "header"_a =
nullptr);
212 (
void (
Mask<MaskPixelT>::*)(fits::MemFileManager &, fits::ImageWriteOptions
const &,
214 daf::base::PropertySet
const *)
const) &
216 "manager"_a,
"options"_a,
"mode"_a =
"w",
217 "header"_a =
nullptr);
220 daf::base::PropertySet
const *)
const) &
222 "fits"_a,
"options"_a,
"header"_a =
nullptr);
225 cls.def_static(
"readFits",
238 "removeFromDefault"_a =
false);
240 cls.def_static(
"getPlaneBitMask",
242 cls.def_static(
"getPlaneBitMask",
254template <
typename PixelT>
255static PyImage<PixelT> declareImage(lsst::cpputils::python::WrapperCollection &wrappers,
256 const std::string &suffix) {
257 return wrappers.
wrapType(PyImage<PixelT>(wrappers.
module, (
"Image" + suffix).c_str()), [](
auto &mod,
260 cls.def(py::init<unsigned int, unsigned int, PixelT>(),
"width"_a,
"height"_a,
"intialValue"_a = 0);
261 cls.def(py::init<lsst::geom::Extent2I const &, PixelT>(),
"dimensions"_a = lsst::geom::Extent2I(),
262 "initialValue"_a = 0);
263 cls.def(py::init<lsst::geom::Box2I const &, PixelT>(),
"bbox"_a,
"initialValue"_a = 0);
264 cls.def(py::init<Image<PixelT> const &, lsst::geom::Box2I const &, ImageOrigin const, const bool>(),
265 "rhs"_a,
"bbox"_a,
"origin"_a = PARENT,
"deep"_a = false);
266 cls.def(py::init<ndarray::Array<PixelT, 2, 1> const &, bool, lsst::geom::Point2I const &>(),
267 "array"_a,
"deep"_a = false,
"xy0"_a = lsst::geom::Point2I());
268 cls.def(py::init<std::string const &, int, std::shared_ptr<daf::base::PropertySet>,
269 lsst::geom::Box2I const &, ImageOrigin, bool>(),
270 "fileName"_a,
"hdu"_a = fits::DEFAULT_HDU,
"metadata"_a = nullptr,
271 "bbox"_a = lsst::geom::Box2I(),
"origin"_a = PARENT,
"allowUnsafe"_a = false);
272 cls.def(py::init<fits::MemFileManager &, int, std::shared_ptr<daf::base::PropertySet>,
273 lsst::geom::Box2I const &, ImageOrigin, bool>(),
274 "manager"_a,
"hdu"_a = fits::DEFAULT_HDU,
"metadata"_a = nullptr,
275 "bbox"_a = lsst::geom::Box2I(),
"origin"_a = PARENT,
"allowUnsafe"_a = false);
276 cls.def(py::init<fits::Fits &, std::shared_ptr<daf::base::PropertySet>, lsst::geom::Box2I const &,
277 ImageOrigin, bool>(),
278 "fitsFile"_a,
"metadata"_a = nullptr,
"bbox"_a = lsst::geom::Box2I(),
"origin"_a = PARENT,
279 "allowUnsafe"_a = false);
282 cls.def(
"__iadd__", [](Image<PixelT> &self, PixelT const &other) { return self += other; });
284 cls.def(
"__iadd__", [](
Image<PixelT> &self, lsst::afw::math::Function2<double>
const &other) {
285 return self += other;
287 cls.def(
"__isub__", [](
Image<PixelT> &self, PixelT
const &other) {
return self -= other; });
289 cls.def(
"__isub__", [](
Image<PixelT> &self, lsst::afw::math::Function2<double>
const &other) {
290 return self -= other;
292 cls.def(
"__imul__", [](
Image<PixelT> &self, PixelT
const &other) {
return self *= other; });
294 cls.def(
"__itruediv__", [](
Image<PixelT> &self, PixelT
const &other) {
return self /= other; });
295 cls.def(
"__itruediv__",
299 cls.def(
"scaledPlus", &Image<PixelT>::scaledPlus);
300 cls.def(
"scaledMinus", &Image<PixelT>::scaledMinus);
301 cls.def(
"scaledMultiplies", &Image<PixelT>::scaledMultiplies);
302 cls.def(
"scaledDivides", &Image<PixelT>::scaledDivides);
304 cls.def(
"subset", &Image<PixelT>::subset,
"bbox"_a,
"origin"_a = PARENT);
307 (
void (
Image<PixelT>::*)(std::string
const &, daf::base::PropertySet
const *,
308 std::string
const &)
const) &
309 Image<PixelT>::writeFits,
310 "fileName"_a,
"metadata"_a =
nullptr,
"mode"_a =
"w");
313 daf::base::PropertySet
const *, std::string
const &)
315 Image<PixelT>::writeFits,
316 "manager"_a,
"metadata"_a =
nullptr,
"mode"_a =
"w");
319 Image<PixelT>::writeFits,
320 "fitsfile"_a,
"metadata"_a =
nullptr);
323 std::string
const &, daf::base::PropertySet
const *,
325 Image<PixelT>::writeFits,
326 "filename"_a,
"options"_a,
"mode"_a =
"w",
327 "header"_a =
nullptr,
331 std::string
const &, daf::base::PropertySet
const *,
333 Image<PixelT>::writeFits,
334 "manager"_a,
"options"_a,
"mode"_a =
"w",
335 "header"_a =
nullptr,
339 daf::base::PropertySet
const *,
341 Image<PixelT>::writeFits,
342 "fits"_a,
"options"_a,
"header"_a =
nullptr,
345 cls.def_static(
"readFits", (
Image<PixelT>(*)(std::string
const &,
int))Image<PixelT>::readFits,
349 cls.def(
"sqrt", &Image<PixelT>::sqrt);
353template <
typename PixelT>
354static void declareDecoratedImage(lsst::cpputils::python::WrapperCollection &wrappers,
355 std::string
const &suffix) {
357 PyDecoratedImage<PixelT>(wrappers.
module, (
"DecoratedImage" + suffix).c_str()),
358 [](
auto &mod,
auto &cls) {
359 cls.def(py::init<const lsst::geom::Extent2I &>(),
"dimensions"_a = lsst::geom::Extent2I());
360 cls.def(py::init<const lsst::geom::Box2I &>(),
"bbox"_a);
361 cls.def(py::init<std::shared_ptr<Image<PixelT>>>(),
"rhs"_a);
362 cls.def(py::init<DecoratedImage<PixelT> const &, const bool>(),
"rhs"_a,
"deep"_a = false);
363 cls.def(py::init<std::string const &, const int, lsst::geom::Box2I const &, ImageOrigin const,
365 "fileName"_a,
"hdu"_a = fits::DEFAULT_HDU,
"bbox"_a = lsst::geom::Box2I(),
366 "origin"_a = PARENT,
"allowUnsafe"_a = false);
368 cls.def(
"getMetadata", &DecoratedImage<PixelT>::getMetadata);
369 cls.def(
"setMetadata", &DecoratedImage<PixelT>::setMetadata);
370 cls.def(
"getWidth", &DecoratedImage<PixelT>::getWidth);
371 cls.def(
"getHeight", &DecoratedImage<PixelT>::getHeight);
372 cls.def(
"getX0", &DecoratedImage<PixelT>::getX0);
373 cls.def(
"getY0", &DecoratedImage<PixelT>::getY0);
374 cls.def(
"getDimensions", &DecoratedImage<PixelT>::getDimensions);
375 cls.def(
"swap", &DecoratedImage<PixelT>::swap);
377 py::overload_cast<std::string const &, daf::base::PropertySet const *,
378 std::string const &>(&DecoratedImage<PixelT>::writeFits,
380 "filename"_a,
"metadata"_a = nullptr,
383 py::overload_cast<std::string const &, fits::ImageWriteOptions const &,
384 daf::base::PropertySet const *, std::string const &>(
385 &DecoratedImage<PixelT>::writeFits, py::const_),
386 "filename"_a,
"options"_a,
"metadata"_a = nullptr,
388 cls.def(
"getImage", py::overload_cast<>(&DecoratedImage<PixelT>::getImage));
389 cls.def_property_readonly(
"image", py::overload_cast<>(&DecoratedImage<PixelT>::getImage));
390 cls.def(
"getGain", &DecoratedImage<PixelT>::getGain);
391 cls.def(
"setGain", &DecoratedImage<PixelT>::setGain);
396template <
typename PixelT>
397static void addImageSliceOperators(
401 [](
Image<PixelT> const &self, ImageSlice<PixelT>
const &other) {
return self + other; },
405 [](
Image<PixelT> const &self, ImageSlice<PixelT>
const &other) {
return self - other; },
409 [](
Image<PixelT> const &self, ImageSlice<PixelT>
const &other) {
return self * other; },
413 [](
Image<PixelT> const &self, ImageSlice<PixelT>
const &other) {
return self / other; },
415 cls.def(
"__iadd__", [](
Image<PixelT> &self, ImageSlice<PixelT>
const &other) {
419 cls.def(
"__isub__", [](
Image<PixelT> &self, ImageSlice<PixelT>
const &other) {
423 cls.def(
"__imul__", [](
Image<PixelT> &self, ImageSlice<PixelT>
const &other) {
427 cls.def(
"__itruediv__", [](
Image<PixelT> &self, ImageSlice<PixelT>
const &other) {
433template <
typename PixelT,
typename PyClass>
434static void addGeneralizedCopyConstructors(PyClass &cls) {
435 cls.def(py::init<
Image<int> const &,
const bool>(),
"rhs"_a,
"deep"_a =
false);
436 cls.def(py::init<Image<float>
const &,
const bool>(),
"rhs"_a,
"deep"_a =
false);
437 cls.def(py::init<Image<double>
const &,
const bool>(),
"rhs"_a,
"deep"_a =
false);
442 cls.def(
"convertF", [](
Image<PixelT> const &self) {
return Image<float>(self,
true); });
443 cls.def(
"convertD", [](
Image<PixelT> const &self) {
return Image<double>(self,
true); });
447 cls.def(
"convertFloat", [](
Image<PixelT> const &self) {
return Image<float>(self,
true); });
448 cls.def(
"convertDouble", [](
Image<PixelT> const &self) {
return Image<double>(self,
true); });
454 wrappers.
wrapType(py::enum_<ImageOrigin>(wrappers.
module,
"ImageOrigin"), [](
auto &mod,
auto &enm) {
455 enm.value(
"PARENT", ImageOrigin::PARENT);
456 enm.value(
"LOCAL", ImageOrigin::LOCAL);
460 declareImageBase<int>(wrappers,
"I");
461 declareImageBase<float>(wrappers,
"F");
462 declareImageBase<double>(wrappers,
"D");
463 declareImageBase<std::uint16_t>(wrappers,
"U");
464 declareImageBase<std::uint64_t>(wrappers,
"L");
467 declareMask<MaskPixel>(wrappers,
"X");
469 auto clsImageI = declareImage<int>(wrappers,
"I");
470 auto clsImageF = declareImage<float>(wrappers,
"F");
471 auto clsImageD = declareImage<double>(wrappers,
"D");
472 auto clsImageU = declareImage<std::uint16_t>(wrappers,
"U");
473 auto clsImageL = declareImage<std::uint64_t>(wrappers,
"L");
476 addGeneralizedCopyConstructors<int>(clsImageI);
477 addGeneralizedCopyConstructors<float>(clsImageF);
478 addGeneralizedCopyConstructors<double>(clsImageD);
479 addGeneralizedCopyConstructors<std::uint16_t>(clsImageU);
480 addGeneralizedCopyConstructors<std::uint64_t>(clsImageL);
483 addImageSliceOperators<float>(clsImageF);
484 addImageSliceOperators<double>(clsImageD);
486 declareDecoratedImage<int>(wrappers,
"I");
487 declareDecoratedImage<float>(wrappers,
"F");
488 declareDecoratedImage<double>(wrappers,
"D");
489 declareDecoratedImage<std::uint16_t>(wrappers,
"U");
490 declareDecoratedImage<std::uint64_t>(wrappers,
"L");
494 declareCastConstructor<int, float>(clsImageF);
495 declareCastConstructor<int, double>(clsImageD);
497 declareCastConstructor<float, double>(clsImageD);
499 declareCastConstructor<double, float>(clsImageF);
501 declareCastConstructor<std::uint16_t, float>(clsImageF);
502 declareCastConstructor<std::uint16_t, double>(clsImageD);
504 declareCastConstructor<std::uint64_t, float>(clsImageF);
505 declareCastConstructor<std::uint64_t, double>(clsImageD);
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Lifetime-management for memory that goes into FITS memory files.
The base class for all image classed (Image, Mask, MaskedImage, ...)
void setXY0(lsst::geom::Point2I const origin)
Set the ImageBase's origin.
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
typename ndarray::Array< PixelT, 2, 1 > Array
A mutable ndarray representation of the image.
A class to represent a 2-dimensional array of pixels.
Represent a 2-dimensional array of bitmask pixels.
A helper class for subdividing pybind11 module across multiple translation units (i....
void addSignatureDependency(std::string const &name)
Indicate an external module that provides a type used in function/method signatures.
void wrap(WrapperCallback function)
Add a set of wrappers without defining a class.
PyType wrapType(PyType cls, ClassWrapperCallback function, bool setModuleName=true)
Add a type (class or enum) wrapper, deferring method and other attribute definitions until finish() i...
pybind11::module module
The module object passed to the PYBIND11_MODULE block that contains this WrapperCollection.
const int DEFAULT_HDU
Specify that the default HDU should be read.
void checkBounds(geom::Point2I const &index, geom::Box2I const &bbox)
lsst::geom::Box2I bboxFromMetadata(daf::base::PropertySet &metadata)
Determine the image bounding box from its metadata (FITS header)
void wrapImage(lsst::cpputils::python::WrapperCollection &wrappers)
Options for writing an image to FITS.
g2d::python::Image< double > Image