iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CutPasteImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_IMAGEPROC_DRAWING_DETAILS_CUTPASTEIMPL_H
36 #define INCLUDE_NRT_IMAGEPROC_DRAWING_DETAILS_CUTPASTEIMPL_H
37 
38 #include <nrt/Core/Debugging/Log.H>
39 
40 // ######################################################################
41 template <class PixType> inline
42 void nrt::paste(nrt::Image<PixType> & destination, nrt::Image<PixType> const source,
43  nrt::Point2D<nrt::int32> const & pos)
44 {
45  nrt::Point2D<nrt::int32> bottomRight = pos;
46  bottomRight += nrt::Point2D<nrt::int32>(source.width(), source.height());
47 
48  if (!destination.dims().contains(pos) && !destination.dims().contains(bottomRight)) return;
49 
50  if (destination.size() == 0 || source.size() == 0) return;
51 
52  typename nrt::Image<PixType>::iterator destptr = destination.begin();
53  typename nrt::Image<PixType>::const_iterator sourceptr = source.const_begin();
54 
55  size_t dest_startx = std::max(0, pos.x());
56  size_t dest_endx = std::min(destination.width(), pos.x()+source.width());
57  size_t dest_starty = std::max(0, pos.y());
58  size_t dest_endy = std::min(destination.height(), pos.y()+source.height());
59 
60  size_t source_startx = size_t(std::max(0, -int(pos.x())));
61  size_t source_endx = std::min(source.width(), destination.width() - pos.x());
62  size_t source_starty = size_t(std::max(0, -int(pos.y())));
63  size_t source_endy = std::min(source.height(), destination.height() - pos.y());
64 
65  size_t num_rows = std::min(source_endy-source_starty, dest_endy- dest_starty);
66  size_t num_cols = std::min(source_endx-source_startx, dest_endx- dest_startx);
67 
68  for (size_t row = 0; row < num_rows; ++row)
69  memcpy(destptr + destination.width()*(row+dest_starty) + dest_startx,
70  sourceptr + source.width()*(row+source_starty) + source_startx,
71  sizeof(PixType) * num_cols);
72 }
73 
74 // ######################################################################
75 template<class PixType> inline
77 nrt::concatX(nrt::Image<PixType> const left, nrt::Image<PixType> const right, PixType const bgColor)
78 {
80  if (left.height() == right.height())
81  res = nrt::Image<PixType>(nrt::Dims<nrt::int32>(left.width() + right.width(), left.height()));
82  else
84  std::max(left.height(),right.height())),
85  bgColor);
86 
87 
88  paste(res, left, nrt::Point2D<nrt::int32>(0,0));
89  paste(res, right, nrt::Point2D<nrt::int32>(left.width(),0));
90  return res;
91 }
92 
93 // ######################################################################
94 template<class PixType> inline
96 nrt::concatY(nrt::Image<PixType> const top, nrt::Image<PixType> const bottom, PixType const bgColor)
97 {
99  if (top.width() == bottom.width())
100  res = nrt::Image<PixType>(nrt::Dims<nrt::int32>(top.width(),top.height()+bottom.height()));
101  else
102  res = nrt::Image<PixType>(nrt::Dims<nrt::int32>(std::max(top.width(), bottom.width()),
103  top.height()+bottom.height()),
104  bgColor);
105 
106  paste(res, top, nrt::Point2D<nrt::int32>(0, 0));
107  paste(res, bottom, nrt::Point2D<nrt::int32>(0, top.height()));
108  return res;
109 }
110 
111 // ######################################################################
112 template<class PixType> inline
114 nrt::crop(nrt::Image<PixType> const source, nrt::Rectangle<nrt::int32> const & rect)
115 {
116  nrt::Rectangle<nrt::int32> cropRect =
118 
119  if (cropRect.area() == 0) return nrt::Image<PixType>();
120 
121  nrt::Image<PixType> destination(cropRect.dims());
122  typename nrt::Image<PixType>::iterator destptr = destination.begin();
123  typename nrt::Image<PixType>::const_iterator sourceptr = source.const_begin();
124 
125  for (nrt::int32 row=0; row<destination.height(); ++row)
126  memcpy(destptr + row*destination.width(),
127  sourceptr + (row+cropRect.top())*source.width() + cropRect.left(),
128  sizeof(PixType) * destination.width());
129 
130  return destination;
131 }
132 
133 // ######################################################################
134 template<class PixType> inline
137 {
138  ImageSet<PixType> results;
139  for(size_t i=0; i<source.size(); i++)
140  results.push_back(crop(source.at(i), rect));
141 
142  return results;
143 }
144 
145 // ######################################################################
146 namespace
147 {
148  struct CropVisitor : public boost::static_visitor<nrt::GenericImage const>
149  {
150  inline CropVisitor(nrt::Rectangle<nrt::int32> const & rect_) : rect(rect_) { }
151 
152  template <class PixType> inline
153  nrt::GenericImage const operator()(nrt::Image<PixType> const im) const
154  { return nrt::GenericImage(nrt::crop(im, rect)); }
155 
156  private:
157  nrt::Rectangle<nrt::int32> const rect;
158  };
159 }
160 
161 inline nrt::GenericImage const
163 { return source.apply_visitor(CropVisitor(rect)); }
164 
165 // ######################################################################
166 template<class PixType> inline
168 nrt::cropAndFill(nrt::Image<PixType> const source, nrt::Rectangle<nrt::int32> const & rect, PixType const bgColor)
169 {
170  nrt::Rectangle<nrt::int32> cropRect =
172 
173  nrt::Image<PixType> destination(rect.dims(), bgColor);
174 
175  if (cropRect.area() == 0) return destination;
176 
177  nrt::Image<PixType> croppedImg = crop(source, cropRect);
178 
179  nrt::int32 pasteX = 0, pasteY = 0;
180  if (rect.left() < 0) pasteX = -rect.left();
181  if (rect.top() < 0) pasteY = -rect.top();
182 
183  paste(destination, croppedImg, nrt::Point2D<nrt::int32>(pasteX, pasteY));
184 
185  return destination;
186 }
187 
188 // ######################################################################
189 namespace
190 {
191  struct CropAndFillVisitor : public boost::static_visitor<nrt::GenericImage const>
192  {
193  inline CropAndFillVisitor(nrt::Rectangle<nrt::int32> const & rect_, nrt::PixRGB<float> const bg_) :
194  rect(rect_), bg(bg_) { }
195 
196  template <class PixType> inline
197  nrt::GenericImage const operator()(nrt::Image<PixType> const im) const
198  { return nrt::GenericImage(nrt::cropAndFill(im, rect, PixType(bg))); }
199 
200  private:
201  nrt::Rectangle<nrt::int32> const rect;
202  nrt::PixRGB<float> const bg;
203  };
204 }
205 
206 inline nrt::GenericImage const
208  nrt::PixRGB<float> const bgColor)
209 { return source.apply_visitor(CropAndFillVisitor(rect, bgColor)); }
210 
211 // ######################################################################
212 template <class PixType>
214  Eigen::AffineCompact2f const & m, PixType const bgColor)
215 {
217 
218  float const dx = float(rect.center().x()), dy = float(rect.center().y());
219  float const xmin = float(rect.left()) - dx, ymin = float(rect.top()) - dy;
220  float const xmax = float(rect.rightO()) - dx - 0.01F, ymax = float(rect.bottomO()) - dy - 0.01F;
221 
222  typename nrt::Image<PixType>::iterator dst = dest.begin();
223  Eigen::Matrix<float, 2, 3> const mm = m.matrix(); Eigen::Vector3f v(xmin, ymin, 1.0F); Eigen::Vector2f vv;
224 
225  while (v[1] < ymax)
226  {
227  while (v[0] < xmax)
228  {
229  vv = mm * v; float const x = vv[0] + dx, y = vv[1] + dy;
230  if (source.coordsOk(x, y)) *dst++ = source.interpolate(x, y); else *dst++ = bgColor;
231  v[0] += 1.0F;
232  }
233  v[1] += 1.0F; v[0] = xmin;
234  }
235  return dest;
236 }
237 
238 
239 #endif // INCLUDE_NRT_IMAGEPROC_DRAWING_DETAILS_CUTPASTEIMPL_H