lsst.afw gf7f67fe209+bc275557c1
Loading...
Searching...
No Matches
binImage.cc
Go to the documentation of this file.
1/*
2 * LSST Data Management System
3 * Copyright 2008, 2009, 2010 LSST Corporation.
4 *
5 * This product includes software developed by the
6 * LSST Project (http://www.lsst.org/).
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the LSST License Statement and
19 * the GNU General Public License along with this program. If not,
20 * see <http://www.lsstcorp.org/LegalNotices/>.
21 */
22
23#include <memory>
24
25#include "lsst/pex/exceptions.h"
27
29
30namespace lsst {
31namespace afw {
32namespace math {
33
34/*
35 * Bin an Image or MaskedImage by an integral factor (the same in x and y)
36 */
37template <typename ImageT>
38std::shared_ptr<ImageT> binImage(ImageT const& in, int const binsize, lsst::afw::math::Property const flags) {
39 return binImage(in, binsize, binsize, flags);
40}
41
52template <typename T>
54
55template <typename T>
57 // Needed to resolve is_integral, is_integer, etc.
58 using ImageType = T;
59
61 // Placeholders are needed here, so they may as well be defaults
64
65 using MaskType = Mask;
66
67 // Needed for constexpr usage
68 static const bool has_mask = false;
69 static const bool has_variance = false;
70
71 static Image& get_image(image::Image<T>& in) { return in; }
72 static Image const& get_image_const(image::Image<T> const& in) { return in; }
73 static Mask* get_mask(image::Image<T>& in) { return nullptr; }
74 static Mask* const get_mask_const(image::Image<T> const& in) { return nullptr; }
75 static Variance* get_variance(image::Image<T>& in) { return nullptr; }
76 static Variance* const get_variance_const(image::Image<T> const& in) { return nullptr; }
77};
78
79template <typename T>
80struct ImagePixelType<image::MaskedImage<T>> {
81 // This is a math primitive (int, double) and not tuple/Pixel.
82 using ImageType = T;
83 // One could use this for most operations as the += operator is defined.
84 // However, signed and unsigned accumulators would still be needed.
86
90
92
93 // MaskedImage enforces initialization of a mask and variance. If the
94 // variance were allowed to be nullptr, this would have to change.
95 static const bool has_mask = true;
96 static const bool has_variance = true;
97
98 static Image& get_image(image::MaskedImage<T>& in) { return *(in.getImage()); }
99 static Image const& get_image_const(image::MaskedImage<T> const& in) { return *(in.getImage()); }
100 static Mask* get_mask(image::MaskedImage<T>& in) { return in.getMask().get(); }
101 static Mask* const get_mask_const(image::MaskedImage<T> const& in) { return in.getMask().get(); }
104 return in.getVariance().get();
105 }
106};
107
108template <typename ImageT>
109std::shared_ptr<ImageT> binImage(ImageT const& in_ref, int const binX, int const binY,
110 lsst::afw::math::Property const flags) {
111 if (flags != lsst::afw::math::MEAN) {
113 (boost::format("Only afwMath::MEAN is supported, saw 0x%x") % flags).str());
114 }
115 if (!((binX > 0) && (binY > 0))) {
117 (boost::format("Binning must be > 0, saw %dx%d") % binX % binY).str());
118 }
119
120 using ImageType = typename ImagePixelType<ImageT>::ImageType;
121 using ImagePlane = typename ImagePixelType<ImageT>::Image;
122 using MaskPlane = typename ImagePixelType<ImageT>::Mask;
123 using VariancePlane = typename ImagePixelType<ImageT>::Variance;
124 using MaskType = typename ImagePixelType<ImageT>::MaskType;
125
126 ImagePlane const& in = ImagePixelType<ImageT>::get_image_const(in_ref);
127 static constexpr bool has_mask = ImagePixelType<ImageT>::has_mask;
128 static constexpr bool has_variance = ImagePixelType<ImageT>::has_variance;
129 // These still have to exist even for Images where they're nullptr, because
130 // there is no special constexpr ternary to ignore the type of *Plane.
131 VariancePlane const* const in_var =
132 has_variance ? ImagePixelType<ImageT>::get_variance_const(in_ref) : nullptr;
133 MaskPlane const* const in_mask = has_mask ? ImagePixelType<ImageT>::get_mask_const(in_ref) : nullptr;
134
135 unsigned int binX_u = binX;
136 unsigned int binY_u = binY;
137
138 unsigned long long binXY_u = binX * binY;
139 long long binXY_l = binX * binY;
140 double binXY = double(binXY_u);
141
142 unsigned int const outWidth = in.getWidth() / binX_u;
143 unsigned int const outHeight = in.getHeight() / binY_u;
144
145 static constexpr bool is_integer = std::is_integral<ImageType>();
146 static constexpr bool is_signed = std::is_signed<ImageType>();
147
148 // Initialize pointers to each output plane, as for the inputs
150 std::shared_ptr<ImageT>(new ImageT(lsst::geom::Extent2I(outWidth, outHeight)));
151 out_ptr->setXY0(in.getXY0());
152 *out_ptr = typename ImageT::SinglePixel(0);
153 auto& out_image = ImagePixelType<ImageT>::get_image(*out_ptr);
154 auto* out_var = has_variance ? ImagePixelType<ImageT>::get_variance(*out_ptr) : nullptr;
155 auto* out_mask = has_mask ? ImagePixelType<ImageT>::get_mask(*out_ptr) : nullptr;
156
157 // Initialize all of the loop intermediate values
158 long long remainder_l;
159 unsigned long long remainder_u;
160 std::vector<long long> remainders;
162 if constexpr (is_integer) {
163 if constexpr (is_signed) {
164 remainders.resize(outWidth);
165 } else {
166 remainders_u.resize(outWidth);
167 }
168 }
169 unsigned int ir;
170
171 MaskType bitmask;
172 double sum;
173 long long sum_l;
174 unsigned long long sum_u;
175
176 double sum_var;
177
178 for (unsigned int oy = 0, iy = 0; oy < outHeight; ++oy) {
179 if constexpr (is_integer) {
180 if constexpr (is_signed) {
181 std::fill(remainders.begin(), remainders.end(), 0);
182 } else {
183 std::fill(remainders_u.begin(), remainders_u.end(), 0);
184 }
185 }
186 for (unsigned int i = 0; i != binY_u; ++i, ++iy) {
187 if constexpr (is_integer) {
188 ir = 0;
189 }
190 auto optr = out_image.row_begin(oy);
191 typename VariancePlane::x_iterator optr_var = has_variance ? out_var->row_begin(oy) : nullptr;
192 typename MaskPlane::x_iterator optr_mask = has_mask ? out_mask->row_begin(oy) : nullptr;
193
194 typename VariancePlane::x_iterator vptr = has_variance ? in_var->row_begin(iy) : nullptr;
195 typename MaskPlane::x_iterator mptr = has_mask ? in_mask->row_begin(iy) : nullptr;
196 for (typename ImagePlane::x_iterator iptr = in.row_begin(iy), iend = iptr + binX_u * outWidth;
197 iptr < iend;) {
198 // Use the highest-precision supertype of the image type
199 if constexpr (is_integer) {
200 if constexpr (is_signed) {
201 sum_l = 0;
202 } else {
203 sum_u = 0;
204 }
205 } else {
206 sum = 0;
207 }
208 if constexpr (has_variance) {
209 sum_var = 0;
210 }
211 if constexpr (has_mask) {
212 bitmask = 0;
213 }
214
215 for (unsigned int j = 0; j != binX_u; ++j) {
216 auto value = *(iptr++);
217 if constexpr (is_integer) {
218 if constexpr (is_signed) {
219 sum_l += value;
220 } else {
221 sum_u += value;
222 }
223 } else {
224 sum += value;
225 }
226 if constexpr (has_variance) {
227 // For consistency with variance_divides which
228 // divides by the square of the divisor.
229 sum_var += *(vptr++) / binX;
230 }
231 if constexpr (has_mask) {
232 bitmask |= *(mptr++);
233 }
234 }
235 if constexpr (is_integer) {
236 if constexpr (is_signed) {
237 remainders[ir++] += sum_l % binXY_l;
238 *(optr++) += sum_l / binXY_l;
239 } else {
240 remainders_u[ir++] += sum_u % binXY_u;
241 *(optr++) += sum_u / binXY_u;
242 }
243 } else {
244 sum /= binXY;
245 *(optr++) += sum;
246 }
247 if constexpr (has_variance) {
248 // For consistency with variance_divides which
249 // divides by the square of the divisor.
250 *(optr_var++) += sum_var / binY;
251 }
252 if constexpr (has_mask) {
253 *(optr_mask++) |= bitmask;
254 }
255 }
256 }
257 if constexpr (is_integer | has_variance) {
258 auto optr = out_image.row_begin(oy);
259 typename VariancePlane::x_iterator optr_var = has_variance ? out_var->row_begin(oy) : nullptr;
260 for (unsigned int ox = 0; ox < outWidth; ++ox) {
261 // The method used to do integer division, which is
262 // round-to-zero in C++ but floor division in Python.
263 // Rounding to the nearest integer seems to be the least
264 // surprising option here, even if the prior (undocumented)
265 // behaviour was to do C++ rounding.
266 if constexpr (is_integer) {
267 ImageType shift = 0;
268 double remainder_d;
269 if constexpr (is_signed) {
270 remainder_l = remainders[ox];
271 shift = remainder_l / binXY_l;
272 remainder_d = double(remainder_l % binXY_l);
273 } else {
274 remainder_u = double(remainders_u[ox]);
275 shift = remainder_u / binXY_u;
276 remainder_d = double(remainder_u % binXY_u);
277 }
278 *(optr) += shift;
279 shift = *optr;
280 remainder_d /= binXY;
281 if (remainder_d == 0.5) {
282 remainder_d = (shift % 2) != 0;
283 } else if (remainder_d == -0.5) {
284 remainder_d = -((shift % 2) != 0);
285 } else {
286 remainder_d = round(remainder_d);
287 }
288 *(optr++) += remainder_d;
289 }
290 if constexpr (has_variance) {
291 // Defer taking the mean until after all sums are done
292 // The divisions by binX and binY above will prevent
293 // overflow, whereas dividing here limits roundoff error
294 // for small values (though it might do worse for
295 // large values, so hopefully users don't have var>1e20
296 *(optr_var++) /= binXY;
297 }
298 }
299 }
300 }
301
302 return out_ptr;
303}
304
305//
306// Explicit instantiations
307//
309#define INSTANTIATE(TYPE) \
310 template std::shared_ptr<image::Image<TYPE>> binImage(image::Image<TYPE> const&, int, \
311 lsst::afw::math::Property const); \
312 template std::shared_ptr<image::Image<TYPE>> binImage(image::Image<TYPE> const&, int, int, \
313 lsst::afw::math::Property const); \
314 template std::shared_ptr<image::MaskedImage<TYPE>> binImage(image::MaskedImage<TYPE> const&, int, \
315 lsst::afw::math::Property const); \
316 template std::shared_ptr<image::MaskedImage<TYPE>> binImage(image::MaskedImage<TYPE> const&, int, int, \
317 lsst::afw::math::Property const);
318
320INSTANTIATE(int)
321INSTANTIATE(float)
322INSTANTIATE(double)
324} // namespace math
325} // namespace afw
326} // namespace lsst
#define INSTANTIATE(FROMSYS, TOSYS)
Definition Detector.cc:509
#define LSST_EXCEPT(type,...)
T begin(T... args)
A class to represent a 2-dimensional array of pixels.
Definition Image.h:51
A class to manipulate images, masks, and variance as a single object.
Definition MaskedImage.h:74
lsst::afw::image::Image< VariancePixelT > Variance
Definition MaskedImage.h:85
lsst::afw::image::Mask< MaskPixelT > Mask
Definition MaskedImage.h:87
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
lsst::afw::image::Image< ImagePixelT > Image
Definition MaskedImage.h:86
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage's mask.
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
A single pixel of the same type as a MaskedImage.
Definition Pixel.h:73
T end(T... args)
T fill(T... args)
T get(T... args)
Property
control what is calculated
Definition Statistics.h:53
@ MEAN
estimate sample mean
Definition Statistics.h:57
std::shared_ptr< ImageT > binImage(ImageT const &inImage, int const binX, int const binY, lsst::afw::math::Property const flags=lsst::afw::math::MEAN)
Definition binImage.cc:109
Extent< int, 2 > Extent2I
T resize(T... args)
static Mask *const get_mask_const(image::Image< T > const &in)
Definition binImage.cc:74
image::Image< image::MaskPixel > Mask
Definition binImage.cc:62
static Image const & get_image_const(image::Image< T > const &in)
Definition binImage.cc:72
static Mask * get_mask(image::Image< T > &in)
Definition binImage.cc:73
static Variance * get_variance(image::Image< T > &in)
Definition binImage.cc:75
static Variance *const get_variance_const(image::Image< T > const &in)
Definition binImage.cc:76
static Image & get_image(image::Image< T > &in)
Definition binImage.cc:71
image::Image< image::VariancePixel > Variance
Definition binImage.cc:63
static Image const & get_image_const(image::MaskedImage< T > const &in)
Definition binImage.cc:99
static Variance * get_variance(image::MaskedImage< T > &in)
Definition binImage.cc:102
static Image & get_image(image::MaskedImage< T > &in)
Definition binImage.cc:98
typename image::MaskedImage< T >::SinglePixel::MaskPixelT MaskType
Definition binImage.cc:91
static Mask * get_mask(image::MaskedImage< T > &in)
Definition binImage.cc:100
image::pixel::SinglePixel< double, image::MaskPixel > FloatAccumulator
Definition binImage.cc:85
static Variance *const get_variance_const(image::MaskedImage< T > const &in)
Definition binImage.cc:103
static Mask *const get_mask_const(image::MaskedImage< T > const &in)
Definition binImage.cc:101
Helper class to generalize Image and MaskedImage properties without directly using their Pixel templa...
Definition binImage.cc:53