[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

copyimage.hxx |

1 /************************************************************************/
2 /* */
3 /* Copyright 1998-2002 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35
36
37 #ifndef VIGRA_COPYIMAGE_HXX
38 #define VIGRA_COPYIMAGE_HXX
39
40 #include "utilities.hxx"
41 #include "multi_shape.hxx"
42
43 namespace vigra {
44
45 /** \addtogroup CopyAlgo Algorithms to Copy Images
46 Copy images or regions
47 */
48 //@{
49
50 /********************************************************/
51 /* */
52 /* copyLine */
53 /* */
54 /********************************************************/
55
56 template <class SrcIterator, class SrcAccessor,
57 class DestIterator, class DestAccessor>
58 void
59 copyLine(SrcIterator s,
60 SrcIterator send, SrcAccessor src,
61 DestIterator d, DestAccessor dest)
62 {
63 for(; s != send; ++s, ++d)
64 dest.set(src(s), d);
65 }
66
67 template <class SrcIterator, class SrcAccessor,
68 class MaskIterator, class MaskAccessor,
69 class DestIterator, class DestAccessor>
70 void
71 copyLineIf(SrcIterator s,
72 SrcIterator send, SrcAccessor src,
73 MaskIterator m, MaskAccessor mask,
74 DestIterator d, DestAccessor dest)
75 {
76 for(; s != send; ++s, ++d, ++m)
77 if(mask(m))
78 dest.set(src(s), d);
79 }
80
81 template <class SrcIterator, class SrcAccessor,
82 class DestIterator, class DestAccessor>
83 void
84 swapLine(SrcIterator s,
85 SrcIterator send, SrcAccessor src,
86 DestIterator d, DestAccessor dest)
87 {
88 for(; s != send; ++s, ++d)
89 {
90 typename SrcAccessor::value_type t = src(s);
91 src.set(dest(d), s);
92 dest.set(t, d);
93 }
94 }
95
96 /********************************************************/
97 /* */
98 /* copyImage */
99 /* */
100 /********************************************************/
101
102 /** \brief Copy source image into destination image.
103
104 If necessary, type conversion takes place.
105 Some variants of this function use accessors to access the pixel data.
106
107 See \ref copyMultiArray() for a dimension-independent version of this algorithm.
108
109 <b> Declarations:</b>
110
111 pass 2D array views:
112 \code
113 namespace vigra {
114 template <class T1, class S1,
115 class T2, class S2>
116 void
117 copyImage(MultiArrayView<2, T1, S1> const & src,
118 MultiArrayView<2, T2, S2> dest);
119 }
120 \endcode
121
122 pass \ref ImageIterators and \ref DataAccessors :
123 \code
124 namespace vigra {
125 template <class SrcImageIterator, class SrcAccessor,
126 class DestImageIterator, class DestAccessor>
127 void
128 copyImage(SrcImageIterator src_upperleft, SrcImageIterator src_lowerright, SrcAccessor sa,
129 DestImageIterator dest_upperleft, DestAccessor da)
130 }
131 \endcode
132 use argument objects in conjunction with \ref ArgumentObjectFactories :
133 \code
134 namespace vigra {
135 template <class SrcImageIterator, class SrcAccessor,
136 class DestImageIterator, class DestAccessor>
137 void
138 copyImage(triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
139 pair<DestImageIterator, DestAccessor> dest)
140 }
141 \endcode
142
143 <b> Usage:</b>
144
145 <b>\#include</b> <vigra/copyimage.hxx><br>
146 Namespace: vigra
147
148 Use MultiArrayView API:
149 \code
150 MultiArray<2, int> src(Shape2(100, 200)),
151 dest(Shape2(100, 200));
152 ...
153
154 copyImage(src, dest);
155
156 // equivalent to
157 dest = src;
158 \endcode
159
160 Use iterator-based API with accessor:
161 \code
162 MultiArray<2, RGBValue<unsigned char> > src(Shape2(100, 200)),
163 MultiArray<2, float> dest(Shape2(100, 200));
164
165 // convert RGB to gray values in the fly
166 copyImage(srcImageRange(src, RGBToGrayAccessor<RGBValue<unsigned char> >()),
167 destImage(dest));
168 \endcode
169
170 <b> Required Interface:</b>
171
172 \code
173 SrcImageIterator src_upperleft, src_lowerright;
174 DestImageIterator dest_upperleft;
175 SrcImageIterator::row_iterator sx = src_upperleft.rowIterator();
176 DestImageIterator::row_iterator dx = dest_upperleft.rowIterator();
177
178 SrcAccessor src_accessor;
179 DestAccessor dest_accessor;
180
181 dest_accessor.set(src_accessor(sx), dx);
182 \endcode
183 */
184 doxygen_overloaded_function(template <...> void copyImage)
185
186 template <class SrcImageIterator, class SrcAccessor,
187 class DestImageIterator, class DestAccessor>
188 void
189 copyImage(SrcImageIterator src_upperleft,
190 SrcImageIterator src_lowerright, SrcAccessor sa,
191 DestImageIterator dest_upperleft, DestAccessor da)
192 {
193 int w = src_lowerright.x - src_upperleft.x;
194
195 for(; src_upperleft.y<src_lowerright.y; ++src_upperleft.y, ++dest_upperleft.y)
196 {
197 copyLine(src_upperleft.rowIterator(),
198 src_upperleft.rowIterator() + w, sa,
199 dest_upperleft.rowIterator(), da);
200 }
201 }
202
203 template <class SrcImageIterator, class SrcAccessor,
204 class DestImageIterator, class DestAccessor>
205 inline
206 void
207 copyImage(triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
208 pair<DestImageIterator, DestAccessor> dest)
209 {
210 copyImage(src.first, src.second, src.third,
211 dest.first, dest.second);
212 }
213
214 template <class T1, class S1,
215 class T2, class S2>
216 inline void
217 copyImage(MultiArrayView<2, T1, S1> const & src,
218 MultiArrayView<2, T2, S2> dest)
219 {
220 vigra_precondition(src.shape() == dest.shape(),
221 "copyImage(): shape mismatch between input and output.");
222 copyImage(srcImageRange(src), destImage(dest));
223 }
224
225 template <class SrcImageIterator, class SrcAccessor,
226 class DestImageIterator, class DestAccessor>
227 void
228 swapImageData(SrcImageIterator src_upperleft,
229 SrcImageIterator src_lowerright, SrcAccessor sa,
230 DestImageIterator dest_upperleft, DestAccessor da)
231 {
232 int w = src_lowerright.x - src_upperleft.x;
233
234 for(; src_upperleft.y<src_lowerright.y; ++src_upperleft.y, ++dest_upperleft.y)
235 {
236 swapLine(src_upperleft.rowIterator(),
237 src_upperleft.rowIterator() + w, sa,
238 dest_upperleft.rowIterator(), da);
239 }
240 }
241
242 template <class SrcImageIterator, class SrcAccessor,
243 class DestImageIterator, class DestAccessor>
244 inline
245 void
246 swapImageData(triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
247 pair<DestImageIterator, DestAccessor> dest)
248 {
249 swapImageData(src.first, src.second, src.third,
250 dest.first, dest.second);
251 }
252
253 template <class T1, class S1,
254 class T2, class S2>
255 inline
256 void
257 swapImageData(MultiArrayView<2, T1, S1> const & src,
258 MultiArrayView<2, T2, S2> dest)
259 {
260 vigra_precondition(src.shape() == dest.shape(),
261 "swapImageData(): shape mismatch between input and output.");
262 swapImageData(srcImageRange(src), destImage(dest));
263 }
264
265 /********************************************************/
266 /* */
267 /* copyImageIf */
268 /* */
269 /********************************************************/
270
271 /** \brief Copy source ROI into destination image.
272
273 Pixel values are copied whenever the return value of the mask's
274 accessor is not zero.
275 If necessary, type conversion takes place.
276 Some variants of this function use accessors to access the pixel data.
277
278 <b> Declarations:</b>
279
280 pass 2D array views:
281 \code
282 namespace vigra {
283 template <class T1, class S1,
284 class TM, class SM,
285 class T2, class S2>
286 void
287 copyImageIf(MultiArrayView<2, T1, S1> const & src,
288 MultiArrayView<2, TM, SM> const & mask,
289 MultiArrayView<2, T2, S2> dest);
290 }
291 \endcode
292
293 pass \ref ImageIterators and \ref DataAccessors :
294 \code
295 namespace vigra {
296 template <class SrcImageIterator, class SrcAccessor,
297 class MaskImageIterator, class MaskAccessor,
298 class DestImageIterator, clas DestAccessor>
299 void
300 copyImageIf(SrcImageIterator src_upperleft,
301 SrcImageIterator src_lowerright, SrcAccessor sa,
302 MaskImageIterator mask_upperleft, MaskAccessor ma,
303 DestImageIterator dest_upperleft, DestAccessor da)
304 }
305 \endcode
306 use argument objects in conjunction with \ref ArgumentObjectFactories :
307 \code
308 namespace vigra {
309 template <class SrcImageIterator, class SrcAccessor,
310 class MaskImageIterator, class MaskAccessor,
311 class DestImageIterator, clas DestAccessor>
312 void
313 copyImageIf(triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
314 pair<MaskImageIterator, MaskAccessor> mask,
315 pair<DestImageIterator, DestAccessor> dest)
316 }
317 \endcode
318
319 <b> Usage:</b>
320
321 <b>\#include</b> <vigra/copyimage.hxx><br>
322 Namespace: vigra
323
324 Use MultiArrayView API:
325 \code
326 MultiArray<2, int> src(Shape2(100, 200)),
327 mask(Shape2(100, 200)),
328 dest(Shape2(100, 200));
329 ...
330
331 copyImageIf(src, mask, dest);
332 \endcode
333
334 Use iterator-based API with accessor:
335 \code
336 MultiArray<2, RGBValue<unsigned char> > src(Shape2(100, 200)),
337 MultiArray<2, unsigned char> mask(Shape2(100, 200));
338 MultiArray<2, float> dest(Shape2(100, 200));
339
340 // convert RGB to gray values in the fly
341 copyImageIf(srcImageRange(src, RGBToGrayAccessor<RGBValue<unsigned char> >()),
342 maskImage(mask), destImage(dest));
343 \endcode
344
345 <b> Required Interface:</b>
346
347 \code
348 SrcImageIterator src_upperleft, src_lowerright;
349 DestImageIterator dest_upperleft;
350 MaskImageIterator mask_upperleft;
351 SrcImageIterator::row_iterator sx = src_upperleft.rowIterator();
352 MaskImageIterator::row_iterator mx = mask_upperleft.rowIterator();
353 DestImageIterator::row_iterator dx = dest_upperleft.rowIterator();
354
355 SrcAccessor src_accessor;
356 DestAccessor dest_accessor;
357 MaskAccessor mask_accessor;
358 Functor functor;
359
360 if(mask_accessor(mx))
361 dest_accessor.set(src_accessor(sx), dx);
362
363 \endcode
364 */
365 doxygen_overloaded_function(template <...> void copyImageIf)
366
367 template <class SrcImageIterator, class SrcAccessor,
368 class MaskImageIterator, class MaskAccessor,
369 class DestImageIterator, class DestAccessor>
370 void
371 copyImageIf(SrcImageIterator src_upperleft,
372 SrcImageIterator src_lowerright, SrcAccessor sa,
373 MaskImageIterator mask_upperleft, MaskAccessor ma,
374 DestImageIterator dest_upperleft, DestAccessor da)
375 {
376 int w = src_lowerright.x - src_upperleft.x;
377
378 for(; src_upperleft.y<src_lowerright.y;
379 ++src_upperleft.y, ++mask_upperleft.y, ++dest_upperleft.y)
380 {
381 copyLineIf(src_upperleft.rowIterator(),
382 src_upperleft.rowIterator() + w, sa,
383 mask_upperleft.rowIterator(), ma,
384 dest_upperleft.rowIterator(), da);
385 }
386 }
387
388 template <class SrcImageIterator, class SrcAccessor,
389 class MaskImageIterator, class MaskAccessor,
390 class DestImageIterator, class DestAccessor>
391 inline
392 void
393 copyImageIf(triple<SrcImageIterator, SrcImageIterator, SrcAccessor> src,
394 pair<MaskImageIterator, MaskAccessor> mask,
395 pair<DestImageIterator, DestAccessor> dest)
396 {
397 copyImageIf(src.first, src.second, src.third,
398 mask.first, mask.second,
399 dest.first, dest.second);
400 }
401
402 template <class T1, class S1,
403 class TM, class SM,
404 class T2, class S2>
405 inline void
406 copyImageIf(MultiArrayView<2, T1, S1> const & src,
407 MultiArrayView<2, TM, SM> const & mask,
408 MultiArrayView<2, T2, S2> dest)
409 {
410 vigra_precondition(src.shape() == mask.shape() && src.shape() == dest.shape(),
411 "copyImageIf(): shape mismatch between input and output.");
412 copyImageIf(srcImageRange(src),
413 maskImage(mask),
414 destImage(dest));
415 }
416
417 //@}
418
419 } // namespace vigra
420
421 #endif // VIGRA_COPYIMAGE_HXX
vigra
Definition: accessor.hxx:43
vigra::copyImageIf
void copyImageIf(...)
Copy source ROI into destination image.
vigra::copyImage
void copyImage(...)
Copy source image into destination image.

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.10.0