23 #include "pybind11/pybind11.h"
24 #include "pybind11/stl.h"
30 using namespace pybind11::literals;
38 template <
typename ImagePixelT>
49 template <
typename FromPixelT,
typename ToPixelT>
50 void declareCastConstructor(PyMaskedImage<ToPixelT> &
cls) {
51 cls.def(
py::init<MaskedImage<FromPixelT>
const &,
bool const>(),
"src"_a,
"deep"_a);
54 template <
typename ImagePixelT>
56 using MI = MaskedImage<ImagePixelT>;
58 py::module::import(
"lsst.daf.base");
60 PyMaskedImage<ImagePixelT>
cls(mod, (
"MaskedImage" + suffix).c_str());
62 mod.def(
"makeMaskedImage", &makeMaskedImage<ImagePixelT, MaskPixel, VariancePixel>,
"image"_a,
63 "mask"_a =
nullptr,
"variance"_a =
nullptr);
68 cls.def(py::init<unsigned int, unsigned int, typename MI::MaskPlaneDict const &>(),
"width"_a,
"height"_a,
70 cls.def(py::init<lsst::geom::Extent2I, typename MI::MaskPlaneDict const &>(),
"dimensions"_a,
72 cls.def(py::init<typename MI::ImagePtr, typename MI::MaskPtr, typename MI::VariancePtr>(),
"image"_a,
73 "mask"_a =
nullptr,
"variance"_a =
nullptr);
74 cls.def(py::init<lsst::geom::Box2I const &, typename MI::MaskPlaneDict const &>(),
"bbox"_a,
81 "conformMasks"_a =
false,
"needAllHdus"_a =
false,
"imageMetadata"_a =
nullptr,
82 "maskMetadata"_a =
nullptr,
"varianceMetadata"_a =
nullptr,
"allowUnsafe"_a =
false);
88 "conformMasks"_a =
false,
"needAllHdus"_a =
false,
"imageMetadata"_a =
nullptr,
89 "maskMetadata"_a =
nullptr,
"varianceMetadata"_a =
nullptr,
"allowUnsafe"_a =
false);
90 cls.def(py::init<MI const &, bool>(),
"rhs"_a,
"deep"_a =
false);
91 cls.def(py::init<MI const &, lsst::geom::Box2I const &, ImageOrigin, bool>(),
"rhs"_a,
"bbox"_a,
92 "origin"_a =
PARENT,
"deep"_a =
false);
101 cls.def(
"subset", &MI::subset,
"bbox"_a,
"origin"_a =
PARENT);
103 cls.def(
"__iadd__", (MI & (MI::*)(ImagePixelT
const)) & MI::operator+=);
104 cls.def(
"__iadd__", (MI & (MI::*)(MI
const &)) & MI::operator+=);
105 cls.def(
"__iadd__", (MI & (MI::*)(Image<ImagePixelT>
const &)) & MI::operator+=);
106 cls.def(
"__iadd__", (MI & (MI::*)(math::Function2<double>
const &)) & MI::operator+=);
108 cls.def(
"__isub__", (MI & (MI::*)(ImagePixelT
const)) & MI::operator-=);
109 cls.def(
"__isub__", (MI & (MI::*)(MI
const &)) & MI::operator-=);
110 cls.def(
"__isub__", (MI & (MI::*)(Image<ImagePixelT>
const &)) & MI::operator-=);
111 cls.def(
"__isub__", (MI & (MI::*)(math::Function2<double>
const &)) & MI::operator-=);
112 cls.def(
"scaledMinus", &MI::scaledMinus);
113 cls.def(
"__imul__", (MI & (MI::*)(ImagePixelT
const)) & MI::operator*=);
114 cls.def(
"__imul__", (MI & (MI::*)(MI
const &)) & MI::operator*=);
115 cls.def(
"__imul__", (MI & (MI::*)(Image<ImagePixelT>
const &)) & MI::operator*=);
116 cls.def(
"scaledMultiplies", &MI::scaledMultiplies);
117 cls.def(
"__itruediv__", (MI & (MI::*)(ImagePixelT
const)) & MI::operator/=);
118 cls.def(
"__itruediv__", (MI & (MI::*)(MI
const &)) & MI::operator/=);
119 cls.def(
"__itruediv__", (MI & (MI::*)(Image<ImagePixelT>
const &)) & MI::operator/=);
120 cls.def(
"scaledDivides", &MI::scaledDivides);
155 [](MI &
self,
std::string const &filename, fits::ImageWriteOptions
const &imageOptions,
156 fits::ImageWriteOptions
const &maskOptions, fits::ImageWriteOptions
const &varianceOptions,
158 self.writeFits(filename, imageOptions, maskOptions, varianceOptions, header);
160 "filename"_a,
"imageOptions"_a,
"maskOptions"_a,
"varianceOptions"_a,
163 [](MI &
self, fits::MemFileManager &manager, fits::ImageWriteOptions
const &imageOptions,
164 fits::ImageWriteOptions
const &maskOptions, fits::ImageWriteOptions
const &varianceOptions,
166 self.writeFits(manager, imageOptions, maskOptions, varianceOptions, header);
168 "manager"_a,
"imageOptions"_a,
"maskOptions"_a,
"varianceOptions"_a,
171 [](MI &
self, fits::Fits &
fits, fits::ImageWriteOptions
const &imageOptions,
172 fits::ImageWriteOptions
const &maskOptions, fits::ImageWriteOptions
const &varianceOptions,
174 self.writeFits(
fits, imageOptions, maskOptions, varianceOptions, header);
176 "fits"_a,
"imageOptions"_a,
"maskOptions"_a,
"varianceOptions"_a,
179 cls.def_static(
"readFits", (MI(*)(
std::string const &))MI::readFits,
"filename"_a);
180 cls.def_static(
"readFits", (MI(*)(fits::MemFileManager &))MI::readFits,
"manager"_a);
181 cls.def(
"getImage", &MI::getImage);
182 cls.def(
"setImage", &MI::setImage);
183 cls.def_property(
"image", &MI::getImage, &MI::setImage);
184 cls.def(
"getMask", &MI::getMask);
185 cls.def(
"setMask", &MI::setMask);
186 cls.def_property(
"mask", &MI::getMask, &MI::setMask);
187 cls.def(
"getVariance", &MI::getVariance);
188 cls.def(
"setVariance", &MI::setVariance);
189 cls.def_property(
"variance", &MI::getVariance, &MI::setVariance);
190 cls.def(
"getWidth", &MI::getWidth);
191 cls.def(
"getHeight", &MI::getHeight);
192 cls.def(
"getDimensions", &MI::getDimensions);
193 cls.def(
"getBBox", &MI::getBBox,
"origin"_a =
PARENT);
194 cls.def(
"getX0", &MI::getX0);
195 cls.def(
"getY0", &MI::getY0);
196 cls.def(
"getXY0", &MI::getXY0);
197 cls.def(
"setXY0", (
void (MI::*)(
int const,
int const)) & MI::setXY0,
"x0"_a,
"y0"_a);
205 template <
typename ImagePixelT>
206 void declareMakeMaskedImage(
py::module &mod) {
207 mod.def(
"makeMaskedImage", makeMaskedImage<ImagePixelT, MaskPixel, VariancePixel>,
"image"_a,
208 "mask"_a =
nullptr,
"variance"_a =
nullptr);
211 template <
typename ImagePixelT1,
typename ImagePixelT2>
215 mod.def(
"imagesOverlap",
216 py::overload_cast<ImageBase<ImagePixelT1>
const &, ImageBase<ImagePixelT2>
const &>(
217 &imagesOverlap<ImagePixelT1, ImagePixelT2>),
218 "image1"_a,
"image2"_a);
220 mod.def(
"imagesOverlap",
221 py::overload_cast<MaskedImage<ImagePixelT1>
const &, MaskedImage<ImagePixelT2>
const &>(
222 &imagesOverlap<ImagePixelT1, ImagePixelT2>),
223 "image1"_a,
"image2"_a);
229 py::module::import(
"lsst.afw.image.image");
231 auto clsMaskedImageF = declareMaskedImage<float>(mod,
"F");
232 auto clsMaskedImageD = declareMaskedImage<double>(mod,
"D");
233 auto clsMaskedImageI = declareMaskedImage<int>(mod,
"I");
234 auto clsMaskedImageU = declareMaskedImage<std::uint16_t>(mod,
"U");
235 auto clsMaskedImageL = declareMaskedImage<std::uint64_t>(mod,
"L");
239 declareCastConstructor<int, float>(clsMaskedImageF);
240 declareCastConstructor<int, double>(clsMaskedImageD);
241 declareCastConstructor<float, double>(clsMaskedImageD);
242 declareCastConstructor<double, float>(clsMaskedImageF);
243 declareCastConstructor<std::uint16_t, float>(clsMaskedImageF);
244 declareCastConstructor<std::uint16_t, double>(clsMaskedImageD);
245 declareCastConstructor<std::uint64_t, float>(clsMaskedImageF);
246 declareCastConstructor<std::uint64_t, double>(clsMaskedImageD);
249 declareMakeMaskedImage<int>(mod);
250 declareMakeMaskedImage<float>(mod);
251 declareMakeMaskedImage<double>(mod);
252 declareMakeMaskedImage<std::uint16_t>(mod);
253 declareMakeMaskedImage<std::uint64_t>(mod);
255 declareImagesOverlap<int, int>(mod);
256 declareImagesOverlap<int, float>(mod);
257 declareImagesOverlap<int, double>(mod);
258 declareImagesOverlap<int, std::uint16_t>(mod);
259 declareImagesOverlap<int, std::uint64_t>(mod);
261 declareImagesOverlap<float, int>(mod);
262 declareImagesOverlap<float, float>(mod);
263 declareImagesOverlap<float, double>(mod);
264 declareImagesOverlap<float, std::uint16_t>(mod);
265 declareImagesOverlap<float, std::uint64_t>(mod);
267 declareImagesOverlap<double, int>(mod);
268 declareImagesOverlap<double, float>(mod);
269 declareImagesOverlap<double, double>(mod);
270 declareImagesOverlap<double, std::uint16_t>(mod);
271 declareImagesOverlap<double, std::uint64_t>(mod);
273 declareImagesOverlap<std::uint16_t, int>(mod);
274 declareImagesOverlap<std::uint16_t, float>(mod);
275 declareImagesOverlap<std::uint16_t, double>(mod);
276 declareImagesOverlap<std::uint16_t, std::uint16_t>(mod);
277 declareImagesOverlap<std::uint16_t, std::uint64_t>(mod);
279 declareImagesOverlap<std::uint64_t, int>(mod);
280 declareImagesOverlap<std::uint64_t, float>(mod);
281 declareImagesOverlap<std::uint64_t, double>(mod);
282 declareImagesOverlap<std::uint64_t, std::uint16_t>(mod);
283 declareImagesOverlap<std::uint64_t, std::uint64_t>(mod);