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(
"__ilshift__", &MI::operator<<=);
104 cls.def(
"__iadd__", (MI & (MI::*)(ImagePixelT
const)) & MI::operator+=);
105 cls.def(
"__iadd__", (MI & (MI::*)(MI
const &)) & MI::operator+=);
106 cls.def(
"__iadd__", (MI & (MI::*)(Image<ImagePixelT>
const &)) & MI::operator+=);
107 cls.def(
"__iadd__", (MI & (MI::*)(math::Function2<double>
const &)) & MI::operator+=);
109 cls.def(
"__isub__", (MI & (MI::*)(ImagePixelT
const)) & MI::operator-=);
110 cls.def(
"__isub__", (MI & (MI::*)(MI
const &)) & MI::operator-=);
111 cls.def(
"__isub__", (MI & (MI::*)(Image<ImagePixelT>
const &)) & MI::operator-=);
112 cls.def(
"__isub__", (MI & (MI::*)(math::Function2<double>
const &)) & MI::operator-=);
113 cls.def(
"scaledMinus", &MI::scaledMinus);
114 cls.def(
"__imul__", (MI & (MI::*)(ImagePixelT
const)) & MI::operator*=);
115 cls.def(
"__imul__", (MI & (MI::*)(MI
const &)) & MI::operator*=);
116 cls.def(
"__imul__", (MI & (MI::*)(Image<ImagePixelT>
const &)) & MI::operator*=);
117 cls.def(
"scaledMultiplies", &MI::scaledMultiplies);
118 cls.def(
"__itruediv__", (MI & (MI::*)(ImagePixelT
const)) & MI::operator/=);
119 cls.def(
"__itruediv__", (MI & (MI::*)(MI
const &)) & MI::operator/=);
120 cls.def(
"__itruediv__", (MI & (MI::*)(Image<ImagePixelT>
const &)) & MI::operator/=);
121 cls.def(
"scaledDivides", &MI::scaledDivides);
156 [](MI &
self,
std::string const &filename, fits::ImageWriteOptions
const &imageOptions,
157 fits::ImageWriteOptions
const &maskOptions, fits::ImageWriteOptions
const &varianceOptions,
159 self.writeFits(filename, imageOptions, maskOptions, varianceOptions, header);
161 "filename"_a,
"imageOptions"_a,
"maskOptions"_a,
"varianceOptions"_a,
164 [](MI &
self, fits::MemFileManager &manager, fits::ImageWriteOptions
const &imageOptions,
165 fits::ImageWriteOptions
const &maskOptions, fits::ImageWriteOptions
const &varianceOptions,
167 self.writeFits(manager, imageOptions, maskOptions, varianceOptions, header);
169 "manager"_a,
"imageOptions"_a,
"maskOptions"_a,
"varianceOptions"_a,
172 [](MI &
self, fits::Fits &
fits, fits::ImageWriteOptions
const &imageOptions,
173 fits::ImageWriteOptions
const &maskOptions, fits::ImageWriteOptions
const &varianceOptions,
175 self.writeFits(
fits, imageOptions, maskOptions, varianceOptions, header);
177 "fits"_a,
"imageOptions"_a,
"maskOptions"_a,
"varianceOptions"_a,
180 cls.def_static(
"readFits", (MI(*)(
std::string const &))MI::readFits,
"filename"_a);
181 cls.def_static(
"readFits", (MI(*)(fits::MemFileManager &))MI::readFits,
"manager"_a);
182 cls.def(
"getImage", &MI::getImage);
183 cls.def(
"setImage", &MI::setImage);
184 cls.def_property(
"image", &MI::getImage, &MI::setImage);
185 cls.def(
"getMask", &MI::getMask);
186 cls.def(
"setMask", &MI::setMask);
187 cls.def_property(
"mask", &MI::getMask, &MI::setMask);
188 cls.def(
"getVariance", &MI::getVariance);
189 cls.def(
"setVariance", &MI::setVariance);
190 cls.def_property(
"variance", &MI::getVariance, &MI::setVariance);
191 cls.def(
"getWidth", &MI::getWidth);
192 cls.def(
"getHeight", &MI::getHeight);
193 cls.def(
"getDimensions", &MI::getDimensions);
194 cls.def(
"getBBox", &MI::getBBox,
"origin"_a =
PARENT);
195 cls.def(
"getX0", &MI::getX0);
196 cls.def(
"getY0", &MI::getY0);
197 cls.def(
"getXY0", &MI::getXY0);
198 cls.def(
"setXY0", (
void (MI::*)(
int const,
int const)) & MI::setXY0,
"x0"_a,
"y0"_a);
206 template <
typename ImagePixelT>
207 void declareMakeMaskedImage(
py::module &mod) {
208 mod.def(
"makeMaskedImage", makeMaskedImage<ImagePixelT, MaskPixel, VariancePixel>,
"image"_a,
209 "mask"_a =
nullptr,
"variance"_a =
nullptr);
212 template <
typename ImagePixelT1,
typename ImagePixelT2>
216 mod.def(
"imagesOverlap",
217 py::overload_cast<ImageBase<ImagePixelT1>
const &, ImageBase<ImagePixelT2>
const &>(
218 &imagesOverlap<ImagePixelT1, ImagePixelT2>),
219 "image1"_a,
"image2"_a);
221 mod.def(
"imagesOverlap",
222 py::overload_cast<MaskedImage<ImagePixelT1>
const &, MaskedImage<ImagePixelT2>
const &>(
223 &imagesOverlap<ImagePixelT1, ImagePixelT2>),
224 "image1"_a,
"image2"_a);
230 py::module::import(
"lsst.afw.image.image");
232 auto clsMaskedImageF = declareMaskedImage<float>(mod,
"F");
233 auto clsMaskedImageD = declareMaskedImage<double>(mod,
"D");
234 auto clsMaskedImageI = declareMaskedImage<int>(mod,
"I");
235 auto clsMaskedImageU = declareMaskedImage<std::uint16_t>(mod,
"U");
236 auto clsMaskedImageL = declareMaskedImage<std::uint64_t>(mod,
"L");
240 declareCastConstructor<int, float>(clsMaskedImageF);
241 declareCastConstructor<int, double>(clsMaskedImageD);
242 declareCastConstructor<float, double>(clsMaskedImageD);
243 declareCastConstructor<double, float>(clsMaskedImageF);
244 declareCastConstructor<std::uint16_t, float>(clsMaskedImageF);
245 declareCastConstructor<std::uint16_t, double>(clsMaskedImageD);
246 declareCastConstructor<std::uint64_t, float>(clsMaskedImageF);
247 declareCastConstructor<std::uint64_t, double>(clsMaskedImageD);
250 declareMakeMaskedImage<int>(mod);
251 declareMakeMaskedImage<float>(mod);
252 declareMakeMaskedImage<double>(mod);
253 declareMakeMaskedImage<std::uint16_t>(mod);
254 declareMakeMaskedImage<std::uint64_t>(mod);
256 declareImagesOverlap<int, int>(mod);
257 declareImagesOverlap<int, float>(mod);
258 declareImagesOverlap<int, double>(mod);
259 declareImagesOverlap<int, std::uint16_t>(mod);
260 declareImagesOverlap<int, std::uint64_t>(mod);
262 declareImagesOverlap<float, int>(mod);
263 declareImagesOverlap<float, float>(mod);
264 declareImagesOverlap<float, double>(mod);
265 declareImagesOverlap<float, std::uint16_t>(mod);
266 declareImagesOverlap<float, std::uint64_t>(mod);
268 declareImagesOverlap<double, int>(mod);
269 declareImagesOverlap<double, float>(mod);
270 declareImagesOverlap<double, double>(mod);
271 declareImagesOverlap<double, std::uint16_t>(mod);
272 declareImagesOverlap<double, std::uint64_t>(mod);
274 declareImagesOverlap<std::uint16_t, int>(mod);
275 declareImagesOverlap<std::uint16_t, float>(mod);
276 declareImagesOverlap<std::uint16_t, double>(mod);
277 declareImagesOverlap<std::uint16_t, std::uint16_t>(mod);
278 declareImagesOverlap<std::uint16_t, std::uint64_t>(mod);
280 declareImagesOverlap<std::uint64_t, int>(mod);
281 declareImagesOverlap<std::uint64_t, float>(mod);
282 declareImagesOverlap<std::uint64_t, double>(mod);
283 declareImagesOverlap<std::uint64_t, std::uint16_t>(mod);
284 declareImagesOverlap<std::uint64_t, std::uint64_t>(mod);