23 #include "pybind11/pybind11.h"
25 #include "numpy/arrayobject.h"
26 #include "ndarray/pybind11.h"
32 namespace py = pybind11;
33 using namespace pybind11::literals;
41 template <
typename PixelT>
42 using PyImageBase = py::class_<ImageBase<PixelT>, std::shared_ptr<ImageBase<PixelT>>, daf::base::Persistable,
45 template <
typename PixelT>
46 using PyImage = py::class_<Image<PixelT>, std::shared_ptr<Image<PixelT>>, ImageBase<PixelT>>;
48 template <
typename PixelT>
49 using PyDecoratedImage =
50 py::class_<DecoratedImage<PixelT>, std::shared_ptr<DecoratedImage<PixelT>>, daf::base::Persistable>;
60 template <
typename FromPixelT,
typename ToPixelT>
61 static void declareCastConstructor(PyImage<ToPixelT> &
cls) {
62 cls.def(py::init<Image<FromPixelT>
const &,
bool const>(),
"src"_a,
"deep"_a);
65 template <
typename PixelT>
66 static void declareImageBase(py::module &mod, std::string
const &suffix) {
67 PyImageBase<PixelT>
cls(mod, (
"ImageBase" + suffix).c_str());
69 using Array =
typename ImageBase<PixelT>::Array;
71 cls.def(py::init<geom::Extent2I const &>(),
"dimensions"_a =
geom::Extent2I());
72 cls.def(py::init<ImageBase<PixelT>
const &,
bool>(),
"src"_a,
"deep"_a =
false);
73 cls.def(py::init<ImageBase<PixelT>
const &, geom::Box2I
const &,
ImageOrigin,
bool>(),
"src"_a,
"bbox"_a,
75 cls.def(py::init<Array const &, bool, geom::Point2I const &>(),
"array"_a,
"deep"_a =
false,
78 cls.def(
"assign", &ImageBase<PixelT>::assign,
"rhs"_a,
"bbox"_a = geom::Box2I(),
"origin"_a =
PARENT,
81 cls.def(
"getWidth", &ImageBase<PixelT>::getWidth);
82 cls.def(
"getHeight", &ImageBase<PixelT>::getHeight);
83 cls.def(
"getX0", &ImageBase<PixelT>::getX0);
84 cls.def(
"getY0", &ImageBase<PixelT>::getY0);
85 cls.def(
"getXY0", &ImageBase<PixelT>::getXY0);
88 cls.def(
"getDimensions", &ImageBase<PixelT>::getDimensions);
89 cls.def(
"getArray", (Array (ImageBase<PixelT>::*)()) & ImageBase<PixelT>::getArray);
92 (Array (ImageBase<PixelT>::*)()) & ImageBase<PixelT>::getArray,
93 [](ImageBase<PixelT> &
self, ndarray::Array<PixelT const,2,0>
const & array) {
95 if (array.shallow() !=
self.getArray().shallow()) {
96 self.getArray().deep() = array;
100 cls.def(
"setXY0", (
void (ImageBase<PixelT>::*)(
geom::Point2I const)) & ImageBase<PixelT>::setXY0,
102 cls.def(
"setXY0", (
void (ImageBase<PixelT>::*)(
int const,
int const)) & ImageBase<PixelT>::setXY0,
"x0"_a,
104 cls.def(
"getBBox", &ImageBase<PixelT>::getBBox,
"origin"_a =
PARENT);
107 template <
typename PixelT>
108 static PyImage<PixelT> declareImage(py::module &mod,
const std::string &suffix) {
109 PyImage<PixelT>
cls(mod, (
"Image" + suffix).c_str());
112 cls.def(py::init<unsigned int, unsigned int, PixelT>(),
"width"_a,
"height"_a,
"intialValue"_a = 0);
113 cls.def(py::init<geom::Extent2I const &, PixelT>(),
"dimensions"_a =
geom::Extent2I(),
114 "initialValue"_a = 0);
115 cls.def(py::init<geom::Box2I const &, PixelT>(),
"bbox"_a,
"initialValue"_a = 0);
116 cls.def(py::init<Image<PixelT>
const &, geom::Box2I
const &,
ImageOrigin const,
const bool>(),
"rhs"_a,
118 cls.def(py::init<ndarray::Array<PixelT, 2, 1>
const &,
bool,
geom::Point2I const &>(),
"array"_a,
120 cls.def(py::init<std::string
const &,
int, std::shared_ptr<daf::base::PropertySet>, geom::Box2I
const &,
122 "fileName"_a,
"hdu"_a = INT_MIN,
"metadata"_a =
nullptr,
"bbox"_a = geom::Box2I(),
124 cls.def(py::init<fits::MemFileManager &,
int, std::shared_ptr<daf::base::PropertySet>,
126 "manager"_a,
"hdu"_a = INT_MIN,
"metadata"_a =
nullptr,
"bbox"_a = geom::Box2I(),
128 cls.def(py::init<fits::Fits &, std::shared_ptr<daf::base::PropertySet>, geom::Box2I
const &,
130 "fitsFile"_a,
"metadata"_a =
nullptr,
"bbox"_a = geom::Box2I(),
"origin"_a =
PARENT);
133 cls.def(
"__iadd__", [](Image<PixelT> &
self, PixelT
const &other) {
return self += other; });
134 cls.def(
"__iadd__", [](Image<PixelT> &
self, Image<PixelT>
const &other) {
return self += other; });
136 return self += other;
138 cls.def(
"__isub__", [](Image<PixelT> &
self, PixelT
const &other) {
return self -= other; });
139 cls.def(
"__isub__", [](Image<PixelT> &
self, Image<PixelT>
const &other) {
return self -= other; });
141 return self -= other;
143 cls.def(
"__imul__", [](Image<PixelT> &
self, PixelT
const &other) {
return self *= other; });
144 cls.def(
"__imul__", [](Image<PixelT> &
self, Image<PixelT>
const &other) {
return self *= other; });
145 cls.def(
"__itruediv__", [](Image<PixelT> &
self, PixelT
const &other) {
return self /= other; });
146 cls.def(
"__itruediv__", [](Image<PixelT> &
self, Image<PixelT>
const &other) {
return self /= other; });
150 cls.def(
"scaledMinus", &Image<PixelT>::scaledMinus);
151 cls.def(
"scaledMultiplies", &Image<PixelT>::scaledMultiplies);
152 cls.def(
"scaledDivides", &Image<PixelT>::scaledDivides);
155 (
void (Image<PixelT>::*)(std::string
const &, std::shared_ptr<daf::base::PropertySet const>,
156 std::string
const &)
const) &
157 Image<PixelT>::writeFits,
158 "fileName"_a,
"metadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
"mode"_a =
"w");
160 (
void (Image<PixelT>::*)(fits::MemFileManager &, std::shared_ptr<daf::base::PropertySet const>,
161 std::string
const &)
const) &
162 Image<PixelT>::writeFits,
163 "manager"_a,
"metadata"_a = std::shared_ptr<daf::base::PropertySet const>(),
"mode"_a =
"w");
165 (
void (Image<PixelT>::*)(fits::Fits &, std::shared_ptr<daf::base::PropertySet const>)
const) &
166 Image<PixelT>::writeFits,
167 "fitsfile"_a,
"metadata"_a = std::shared_ptr<daf::base::PropertySet const>());
168 cls.def_static(
"readFits", (Image<PixelT>(*)(std::string
const &,
int))Image<PixelT>::readFits,
169 "filename"_a,
"hdu"_a = INT_MIN);
170 cls.def_static(
"readFits", (Image<PixelT>(*)(fits::MemFileManager &,
int))Image<PixelT>::readFits,
171 "manager"_a,
"hdu"_a = INT_MIN);
172 cls.def(
"sqrt", &Image<PixelT>::sqrt);
175 cls.def(
"set", [](Image<PixelT> &img,
double val) { img = val; });
176 cls.def(
"set", [](Image<PixelT> &img,
int x,
int y,
double val) {
181 cls.def(
"set0", [](Image<PixelT> &img,
int x,
int y,
double val) {
184 cls.def(
"get0", [](Image<PixelT> &img,
int x,
int y) {
191 template <
typename PixelT>
192 static void declareDecoratedImage(py::module &mod, std::string
const &suffix) {
193 PyDecoratedImage<PixelT>
cls(mod, (
"DecoratedImage" + suffix).c_str());
196 cls.def(py::init<const lsst::afw::geom::Box2I &>(),
"bbox"_a);
197 cls.def(py::init<std::shared_ptr<Image<PixelT>>>(),
"rhs"_a);
198 cls.def(py::init<DecoratedImage<PixelT>
const &,
const bool>(),
"rhs"_a,
"deep"_a =
false);
199 cls.def(py::init<std::string const &, const int, lsst::afw::geom::Box2I const &, ImageOrigin const>(),
202 cls.def(
"getMetadata", &DecoratedImage<PixelT>::getMetadata);
203 cls.def(
"setMetadata", &DecoratedImage<PixelT>::setMetadata);
204 cls.def(
"getWidth", &DecoratedImage<PixelT>::getWidth);
205 cls.def(
"getHeight", &DecoratedImage<PixelT>::getHeight);
206 cls.def(
"getX0", &DecoratedImage<PixelT>::getX0);
207 cls.def(
"getY0", &DecoratedImage<PixelT>::getY0);
208 cls.def(
"getDimensions", &DecoratedImage<PixelT>::getDimensions);
210 cls.def(
"writeFits", &DecoratedImage<PixelT>::writeFits,
"fileName"_a,
211 "metadata"_a = std::shared_ptr<lsst::daf::base::PropertySet const>(),
"mode"_a =
"w");
212 cls.def(
"getImage", (
typename std::shared_ptr<Image<PixelT>> (DecoratedImage<PixelT>::*)()) &
213 DecoratedImage<PixelT>::getImage);
214 cls.def(
"getGain", &DecoratedImage<PixelT>::getGain);
215 cls.def(
"setGain", &DecoratedImage<PixelT>::setGain);
219 template <
typename PixelT>
220 static void addImageSliceOperators(
221 py::class_<Image<PixelT>, std::shared_ptr<Image<PixelT>>, ImageBase<PixelT>> &cls) {
223 [](Image<PixelT>
const &
self, ImageSlice<PixelT>
const &other) {
return self + other; },
226 [](Image<PixelT>
const &
self, ImageSlice<PixelT>
const &other) {
return self - other; },
229 [](Image<PixelT>
const &
self, ImageSlice<PixelT>
const &other) {
return self * other; },
231 cls.def(
"__truediv__",
232 [](Image<PixelT>
const &
self, ImageSlice<PixelT>
const &other) {
return self / other; },
234 cls.def(
"__iadd__", [](Image<PixelT> &
self, ImageSlice<PixelT>
const &other) {
238 cls.def(
"__isub__", [](Image<PixelT> &
self, ImageSlice<PixelT>
const &other) {
242 cls.def(
"__imul__", [](Image<PixelT> &
self, ImageSlice<PixelT>
const &other) {
246 cls.def(
"__itruediv__", [](Image<PixelT> &
self, ImageSlice<PixelT>
const &other) {
252 template <
typename PixelT,
typename PyClass>
253 static void addGeneralizedCopyConstructors(PyClass &cls) {
254 cls.def(py::init<Image<int>
const &,
const bool>(),
"rhs"_a,
"deep"_a =
false);
255 cls.def(py::init<Image<float>
const &,
const bool>(),
"rhs"_a,
"deep"_a =
false);
256 cls.def(py::init<Image<double>
const &,
const bool>(),
"rhs"_a,
"deep"_a =
false);
257 cls.def(py::init<Image<std::uint16_t>
const &,
const bool>(),
"rhs"_a,
"deep"_a =
false);
258 cls.def(py::init<Image<std::uint64_t>
const &,
const bool>(),
"rhs"_a,
"deep"_a =
false);
260 cls.def(
"convertI", [](Image<PixelT>
const &
self) {
return Image<int>(
self,
true); });
261 cls.def(
"convertF", [](Image<PixelT>
const &
self) {
return Image<float>(
self,
true); });
262 cls.def(
"convertD", [](Image<PixelT>
const &
self) {
return Image<double>(
self,
true); });
263 cls.def(
"convertU", [](Image<PixelT>
const &
self) {
return Image<std::uint16_t>(
self,
true); });
264 cls.def(
"convertL", [](Image<PixelT>
const &
self) {
return Image<std::uint64_t>(
self,
true); });
266 cls.def(
"convertFloat", [](Image<PixelT>
const &
self) {
return Image<float>(
self,
true); });
267 cls.def(
"convertDouble", [](Image<PixelT>
const &
self) {
return Image<double>(
self,
true); });
271 py::module mod(
"image");
273 if (_import_array() < 0) {
274 PyErr_SetString(PyExc_ImportError,
"numpy.core.multiarray failed to import");
278 py::enum_<ImageOrigin>(mod,
"ImageOrigin")
283 declareImageBase<int>(mod,
"I");
284 declareImageBase<float>(mod,
"F");
285 declareImageBase<double>(mod,
"D");
286 declareImageBase<std::uint16_t>(mod,
"U");
287 declareImageBase<std::uint64_t>(mod,
"L");
289 auto clsImageI = declareImage<int>(mod,
"I");
290 auto clsImageF = declareImage<float>(mod,
"F");
291 auto clsImageD = declareImage<double>(mod,
"D");
292 auto clsImageU = declareImage<std::uint16_t>(mod,
"U");
293 auto clsImageL = declareImage<std::uint64_t>(mod,
"L");
296 addGeneralizedCopyConstructors<int>(clsImageI);
297 addGeneralizedCopyConstructors<float>(clsImageF);
298 addGeneralizedCopyConstructors<double>(clsImageD);
299 addGeneralizedCopyConstructors<std::uint16_t>(clsImageU);
300 addGeneralizedCopyConstructors<std::uint64_t>(clsImageL);
303 addImageSliceOperators<float>(clsImageF);
304 addImageSliceOperators<double>(clsImageD);
306 declareDecoratedImage<int>(mod,
"I");
307 declareDecoratedImage<float>(mod,
"F");
308 declareDecoratedImage<double>(mod,
"D");
309 declareDecoratedImage<std::uint16_t>(mod,
"U");
310 declareDecoratedImage<std::uint64_t>(mod,
"L");
314 declareCastConstructor<int, float>(clsImageF);
315 declareCastConstructor<int, double>(clsImageD);
317 declareCastConstructor<float, double>(clsImageD);
319 declareCastConstructor<double, float>(clsImageF);
321 declareCastConstructor<std::uint16_t, float>(clsImageF);
322 declareCastConstructor<std::uint16_t, double>(clsImageD);
324 declareCastConstructor<std::uint64_t, float>(clsImageF);
325 declareCastConstructor<std::uint64_t, double>(clsImageD);
void scaledPlus(OutImageT &outImage, double c1, InImageT const &inImage1, double c2, InImageT const &inImage2)
Compute the scaled sum of two images.
double indexToPosition(double ind)
Convert image index to image position.
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
int positionToIndex(double pos)
Convert image position to nearest integer index.
Extent< int, 2 > Extent2I
A Function taking two arguments.
An integer coordinate rectangle.
PYBIND11_PLUGIN(_cameraPoint)
A class used to request that array accesses be checked.