lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
rotateImage.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 /*
24  * Rotate an Image (or Mask or MaskedImage) by a fixed angle or number of quarter turns
25  */
26 #include <cstdint>
27 
29 
30 namespace afwImage = lsst::afw::image;
31 namespace afwGeom = lsst::afw::geom;
32 
33 namespace lsst {
34 namespace afw {
35 namespace math {
36 
37 template <typename ImageT>
38 std::shared_ptr<ImageT> rotateImageBy90(ImageT const& inImage, int nQuarter) {
39  std::shared_ptr<ImageT> outImage; // output image
40 
41  while (nQuarter < 0) {
42  nQuarter += 4;
43  }
44 
45  switch (nQuarter % 4) {
46  case 0:
47  outImage.reset(new ImageT(inImage, true)); // a deep copy of inImage
48  break;
49  case 1:
50  outImage.reset(new ImageT(afwGeom::Extent2I(inImage.getHeight(), inImage.getWidth())));
51 
52  for (int y = 0; y != inImage.getHeight(); ++y) {
53  typename ImageT::y_iterator optr = outImage->col_begin(inImage.getHeight() - y - 1);
54  for (typename ImageT::x_iterator iptr = inImage.row_begin(y), end = inImage.row_end(y);
55  iptr != end; ++iptr, ++optr) {
56  *optr = *iptr;
57  }
58  }
59 
60  break;
61  case 2:
62  outImage.reset(new ImageT(inImage.getDimensions()));
63 
64  for (int y = 0; y != inImage.getHeight(); ++y) {
65  typename ImageT::x_iterator optr =
66  outImage->x_at(inImage.getWidth() - 1, inImage.getHeight() - y - 1);
67  for (typename ImageT::x_iterator iptr = inImage.row_begin(y), end = inImage.row_end(y);
68  iptr != end; ++iptr, optr -= 1) {
69  *optr = *iptr;
70  }
71  }
72  break;
73  case 3:
74  outImage.reset(new ImageT(afwGeom::Extent2I(inImage.getHeight(), inImage.getWidth())));
75 
76  for (int y = 0; y != inImage.getHeight(); ++y) {
77  typename ImageT::y_iterator optr = outImage->y_at(y, inImage.getWidth() - 1);
78  for (typename ImageT::x_iterator iptr = inImage.row_begin(y), end = inImage.row_end(y);
79  iptr != end; ++iptr, optr -= 1) {
80  *optr = *iptr;
81  }
82  }
83 
84  break;
85  }
86 
87  return outImage;
88 }
89 
90 template <typename ImageT>
91 std::shared_ptr<ImageT> flipImage(ImageT const& inImage, bool flipLR, bool flipTB) {
92  std::shared_ptr<ImageT> outImage(new ImageT(inImage, true)); // Output image
93 
94  if (flipLR) {
95  if (flipTB) {
96  for (int y = 0; y != inImage.getHeight(); ++y) {
97  typename ImageT::x_iterator optr =
98  outImage->x_at(inImage.getWidth() - 1, inImage.getHeight() - y - 1);
99  for (typename ImageT::x_iterator iptr = inImage.row_begin(y), end = inImage.row_end(y);
100  iptr != end; ++iptr, optr -= 1) {
101  *optr = *iptr;
102  }
103  }
104  } else {
105  for (int y = 0; y != inImage.getHeight(); ++y) {
106  typename ImageT::x_iterator optr = outImage->x_at(inImage.getWidth() - 1, y);
107  for (typename ImageT::x_iterator iptr = inImage.row_begin(y), end = inImage.row_end(y);
108  iptr != end; ++iptr, optr -= 1) {
109  *optr = *iptr;
110  }
111  }
112  }
113  } else {
114  if (flipTB) {
115  for (int y = 0; y != inImage.getHeight(); ++y) {
116  typename ImageT::x_iterator optr = outImage->row_begin(inImage.getHeight() - y - 1);
117  for (typename ImageT::x_iterator iptr = inImage.row_begin(y), end = inImage.row_end(y);
118  iptr != end; ++iptr, ++optr) {
119  *optr = *iptr;
120  }
121  }
122  } else {
123  ; // nothing to do
124  }
125  }
126 
127  return outImage;
128 }
129 
130 //
131 // Explicit instantiations
132 //
134 #define INSTANTIATE(TYPE) \
135  template std::shared_ptr<afwImage::Image<TYPE>> rotateImageBy90(afwImage::Image<TYPE> const&, int); \
136  template std::shared_ptr<afwImage::MaskedImage<TYPE>> rotateImageBy90( \
137  afwImage::MaskedImage<TYPE> const&, int); \
138  template std::shared_ptr<afwImage::Image<TYPE>> flipImage(afwImage::Image<TYPE> const&, bool flipLR, \
139  bool flipTB); \
140  template std::shared_ptr<afwImage::MaskedImage<TYPE>> flipImage(afwImage::MaskedImage<TYPE> const&, \
141  bool flipLR, bool flipTB);
142 
143 INSTANTIATE(std::uint16_t)
144 INSTANTIATE(int)
145 INSTANTIATE(float)
146 INSTANTIATE(double)
147 template std::shared_ptr<afwImage::Mask<std::uint16_t>> rotateImageBy90(afwImage::Mask<std::uint16_t> const&,
148  int);
149 template std::shared_ptr<afwImage::Mask<std::uint16_t>> flipImage(afwImage::Mask<std::uint16_t> const&,
150  bool flipLR, bool flipTB);
152 }
153 }
154 }
std::shared_ptr< ImageT > flipImage(ImageT const &inImage, bool flipLR, bool flipTB)
Flip an image left–right and/or top–bottom.
Definition: rotateImage.cc:91
#define INSTANTIATE(TYPE)
std::shared_ptr< ImageT > rotateImageBy90(ImageT const &image, int nQuarter)
Rotate an image by an integral number of quarter turns.
Definition: rotateImage.cc:38
afw::table::Key< afw::table::Array< ImagePixelT > > image