LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
offsetImage.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
30 #include <iterator>
32 #include "lsst/afw/geom/Box.h"
33 #include "lsst/afw/geom/Extent.h"
36 
37 namespace afwImage = lsst::afw::image;
38 namespace afwGeom = lsst::afw::geom;
39 
40 namespace lsst {
41 namespace afw {
42 namespace math {
43 
54 template<typename ImageT>
55 typename ImageT::Ptr offsetImage(ImageT const& inImage,
56  float dx,
57  float dy,
58  std::string const& algorithmName,
59  unsigned int buffer
60  ) {
65  SeparableKernel::Ptr offsetKernel = makeWarpingKernel(algorithmName);
66 
67  typename ImageT::Ptr buffImage;
68  if (buffer > 0) {
69  // Paste input image into buffered image
70  afwGeom::Extent2I const &dims = inImage.getDimensions();
71  typename ImageT::Ptr buffered(new ImageT(dims.getX() + 2 * buffer, dims.getY() + 2 * buffer));
72  buffImage = buffered;
73  afwGeom::Box2I box(afwGeom::Point2I(buffer, buffer), dims);
74  buffImage->assign(inImage, box);
75  } else {
76  buffImage = std::make_shared<ImageT>(inImage);
77  }
78 
79  if (offsetKernel->getWidth() > buffImage->getWidth() ||
80  offsetKernel->getHeight() > buffImage->getHeight()) {
81  throw LSST_EXCEPT(pexExcept::LengthError,
82  (boost::format("Image of size %dx%d is too small to offset using a %s kernel"
83  "(minimum %dx%d)") %
84  buffImage->getWidth() % buffImage->getHeight() % algorithmName %
85  offsetKernel->getWidth() % offsetKernel->getHeight()).str());
86  }
87 
88 // typename ImageT::Ptr convImage(new ImageT(buffImage, true)); // output image, a deep copy
89  typename ImageT::Ptr convImage(new ImageT(buffImage->getDimensions())); // Convolved image
90 
91  int dOrigX, dOrigY;
92  double fracX, fracY;
93  // If the offset in both axes is in (-1, 1) use it as is, and don't shift the origin
94  if (dx > -1 && dx < 1 && dy > -1 && dy < 1) {
95  dOrigX = 0;
96  dOrigY = 0;
97  fracX = dx;
98  fracY = dy;
99  } else {
100  dOrigX = static_cast<int>(std::floor(dx + 0.5));
101  dOrigY = static_cast<int>(std::floor(dy + 0.5));
102  fracX = dx - dOrigX;
103  fracY = dy - dOrigY;
104  }
105 
106  // We seem to have to pass -fracX, -fracY to setKernelParameters, for reasons RHL doesn't understand
107  double dKerX = -fracX;
108  double dKerY = -fracY;
109 
110  //
111  // If the shift is -ve, the generated shift kernel (e.g. Lanczos5) is quite asymmetric, with the
112  // largest coefficients to the left of centre. We therefore move the centre of calculated shift kernel
113  // one to the right to center up the largest coefficients
114  //
115  if (dKerX < 0) {
116  offsetKernel->setCtrX(offsetKernel->getCtrX() + 1);
117  }
118  if (dKerY < 0) {
119  offsetKernel->setCtrY(offsetKernel->getCtrY() + 1);
120  }
121 
122  offsetKernel->setKernelParameters(std::make_pair(dKerX, dKerY));
123 
124  convolve(*convImage, *buffImage, *offsetKernel, true, true);
125 
126  typename ImageT::Ptr outImage;
127  if (buffer > 0) {
128  afwGeom::Box2I box(afwGeom::Point2I(buffer, buffer), inImage.getDimensions());
129  typename ImageT::Ptr out(new ImageT(*convImage, box, afwImage::LOCAL, true));
130  outImage = out;
131  } else {
132  outImage = convImage;
133  }
134 
135  // adjust the origin; do this after convolution since convolution also sets XY0
136  outImage->setXY0(geom::Point2I(inImage.getX0() + dOrigX, inImage.getY0() + dOrigY));
137 
138  return outImage;
139 }
140 
141 /************************************************************************************************************/
142 //
143 // Explicit instantiations
144 //
146 #define INSTANTIATE(TYPE) \
147  template afwImage::Image<TYPE>::Ptr offsetImage(afwImage::Image<TYPE> const&, float, float, \
148  std::string const&, unsigned int); \
149  template afwImage::MaskedImage<TYPE>::Ptr offsetImage(afwImage::MaskedImage<TYPE> const&, float, float, \
150  std::string const&, unsigned int);
151 
152 INSTANTIATE(double)
153 INSTANTIATE(float)
154 INSTANTIATE(int)
156 
157 }}}
boost::shared_ptr< SeparableKernel > makeWarpingKernel(std::string name)
Return a warping kernel given its name.
ImageT::Ptr offsetImage(ImageT const &inImage, float dx, float dy, std::string const &algorithmName, unsigned int buffer)
Return an image offset by (dx, dy) using the specified algorithm.
Definition: offsetImage.cc:55
boost::shared_ptr< SeparableKernel > Ptr
Definition: Kernel.h:985
#define INSTANTIATE(T)
Image utility functions.
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
Extent< int, N > floor(Extent< double, N > const &input)
void convolve(OutImageT &convolvedImage, InImageT const &inImage, KernelT const &kernel, ConvolutionControl const &convolutionControl=ConvolutionControl())
Convolve an Image or MaskedImage with a Kernel, setting pixels of an existing output image...
Support for warping an image to a new WCS.
A coordinate class intended to represent offsets and dimensions.