1 /************************************************************************/
2 /* */
3 /* Copyright 1998-2002 by Ullrich Koethe */
4 /* Cognitive Systems Group, University of Hamburg, Germany */
5 /* */
6 /* This file is part of the VIGRA computer vision library. */
7 /* ( Version 1.5.0, Dec 07 2006 ) */
8 /* The VIGRA Website is */
9 /* http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/ */
10 /* Please direct questions, bug reports, and contributions to */
11 /* koethe@informatik.uni-hamburg.de or */
12 /* vigra@kogs1.informatik.uni-hamburg.de */
13 /* */
14 /* Permission is hereby granted, free of charge, to any person */
15 /* obtaining a copy of this software and associated documentation */
16 /* files (the "Software"), to deal in the Software without */
17 /* restriction, including without limitation the rights to use, */
18 /* copy, modify, merge, publish, distribute, sublicense, and/or */
19 /* sell copies of the Software, and to permit persons to whom the */
20 /* Software is furnished to do so, subject to the following */
21 /* conditions: */
22 /* */
23 /* The above copyright notice and this permission notice shall be */
24 /* included in all copies or substantial portions of the */
25 /* Software. */
26 /* */
27 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
28 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
29 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
30 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
31 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
32 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
33 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
34 /* OTHER DEALINGS IN THE SOFTWARE. */
35 /* */
36 /************************************************************************/
37
38
39 #ifndef VIGRA_INITIMAGE_HXX
40 #define VIGRA_INITIMAGE_HXX
41
42 #include "utilities.hxx"
43 #include "iteratortraits.hxx"
44 #include "functortraits.hxx"
45
46 namespace vigra {
47
48 /** \addtogroup InitAlgo Algorithms to Initialize Images
49
50 Init images or image borders
51 */
52 //@{
53
54 /********************************************************/
55 /* */
56 /* initLine */
57 /* */
58 /********************************************************/
59
60 template <class DestIterator, class DestAccessor, class VALUETYPE>
61 void
initLineImpl(DestIterator d,DestIterator dend,DestAccessor dest,VALUETYPE v,VigraFalseType)62 initLineImpl(DestIterator d, DestIterator dend, DestAccessor dest,
63 VALUETYPE v, VigraFalseType)
64 {
65 for(; d != dend; ++d)
66 dest.set(v, d);
67 }
68
69 template <class DestIterator, class DestAccessor, class FUNCTOR>
70 void
initLineImpl(DestIterator d,DestIterator dend,DestAccessor dest,FUNCTOR const & f,VigraTrueType)71 initLineImpl(DestIterator d, DestIterator dend, DestAccessor dest,
72 FUNCTOR const & f, VigraTrueType)
73 {
74 for(; d != dend; ++d)
75 dest.set(f(), d);
76 }
77
78 template <class DestIterator, class DestAccessor, class VALUETYPE>
79 inline void
initLine(DestIterator d,DestIterator dend,DestAccessor dest,VALUETYPE v)80 initLine(DestIterator d, DestIterator dend, DestAccessor dest,
81 VALUETYPE v)
82 {
83 initLineImpl(d, dend, dest, v, typename FunctorTraits<VALUETYPE>::isInitializer());
84 }
85
86 template <class DestIterator, class DestAccessor, class FUNCTOR>
87 inline void
initLineFunctor(DestIterator d,DestIterator dend,DestAccessor dest,FUNCTOR f)88 initLineFunctor(DestIterator d, DestIterator dend, DestAccessor dest,
89 FUNCTOR f)
90 {
91 initLineImpl(d, dend, dest, f, VigraTrueType());
92 }
93
94 template <class DestIterator, class DestAccessor,
95 class MaskIterator, class MaskAccessor,
96 class VALUETYPE>
97 void
initLineIfImpl(DestIterator d,DestIterator dend,DestAccessor dest,MaskIterator m,MaskAccessor mask,VALUETYPE v,VigraFalseType)98 initLineIfImpl(DestIterator d, DestIterator dend, DestAccessor dest,
99 MaskIterator m, MaskAccessor mask,
100 VALUETYPE v, VigraFalseType)
101 {
102 for(; d != dend; ++d, ++m)
103 if(mask(m))
104 dest.set(v, d);
105 }
106
107 template <class DestIterator, class DestAccessor,
108 class MaskIterator, class MaskAccessor,
109 class FUNCTOR>
110 void
initLineIfImpl(DestIterator d,DestIterator dend,DestAccessor dest,MaskIterator m,MaskAccessor mask,FUNCTOR const & f,VigraTrueType)111 initLineIfImpl(DestIterator d, DestIterator dend, DestAccessor dest,
112 MaskIterator m, MaskAccessor mask,
113 FUNCTOR const & f, VigraTrueType)
114 {
115 for(; d != dend; ++d, ++m)
116 if(mask(m))
117 dest.set(f(), d);
118 }
119
120 template <class DestIterator, class DestAccessor,
121 class MaskIterator, class MaskAccessor,
122 class VALUETYPE>
123 inline void
initLineIf(DestIterator d,DestIterator dend,DestAccessor dest,MaskIterator m,MaskAccessor mask,VALUETYPE v)124 initLineIf(DestIterator d, DestIterator dend, DestAccessor dest,
125 MaskIterator m, MaskAccessor mask,
126 VALUETYPE v)
127 {
128 initLineIfImpl(d, dend, dest, m, mask, v, typename FunctorTraits<VALUETYPE>::isInitializer());
129 }
130
131 template <class DestIterator, class DestAccessor,
132 class MaskIterator, class MaskAccessor,
133 class FUNCTOR>
134 void
initLineFunctorIf(DestIterator d,DestIterator dend,DestAccessor dest,MaskIterator m,MaskAccessor mask,FUNCTOR f)135 initLineFunctorIf(DestIterator d, DestIterator dend, DestAccessor dest,
136 MaskIterator m, MaskAccessor mask,
137 FUNCTOR f)
138 {
139 initLineIfImpl(d, dend, dest, m, mask, f, VigraTrueType());
140 }
141
142 /********************************************************/
143 /* */
144 /* initImage */
145 /* */
146 /********************************************************/
147
148 /** \brief Write a value to every pixel in an image or rectangular ROI.
149
150 This function can be used to init the image.
151 It uses an accessor to access the pixel data.
152
153 <b> Declarations:</b>
154
155 pass arguments explicitly:
156 \code
157 namespace vigra {
158 template <class ImageIterator, class Accessor, class VALUETYPE>
159 void
160 initImage(ImageIterator upperleft, ImageIterator lowerright,
161 Accessor a, VALUETYPE v)
162 }
163 \endcode
164
165 use argument objects in conjunction with \ref ArgumentObjectFactories:
166 \code
167 namespace vigra {
168 template <class ImageIterator, class Accessor, class VALUETYPE>
169 void
170 initImage(triple<ImageIterator, ImageIterator, Accessor> img, VALUETYPE v)
171 }
172 \endcode
173
174 <b> Usage:</b>
175
176 <b>\#include</b> "<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>"<br>
177 Namespace: vigra
178
179 \code
180 vigra::BImage img(100, 100);
181
182 // zero the image
183 vigra::initImage(destImageRange(img),
184 vigra::NumericTraits<vigra::BImage::PixelType>::zero());
185 \endcode
186
187 <b> Required Interface:</b>
188
189 \code
190 ImageIterator upperleft, lowerright;
191 ImageIterator::row_iterator ix = upperleft.rowIterator();
192
193 Accessor accessor;
194 VALUETYPE v;
195
196 accessor.set(v, ix);
197 \endcode
198
199 */
200 template <class ImageIterator, class Accessor, class VALUETYPE>
201 void
initImage(ImageIterator upperleft,ImageIterator lowerright,Accessor a,VALUETYPE v)202 initImage(ImageIterator upperleft, ImageIterator lowerright,
203 Accessor a, VALUETYPE v)
204 {
205 int w = lowerright.x - upperleft.x;
206
207 for(; upperleft.y < lowerright.y; ++upperleft.y)
208 {
209 initLine(upperleft.rowIterator(),
210 upperleft.rowIterator() + w, a, v);
211 }
212 }
213
214 template <class ImageIterator, class Accessor, class VALUETYPE>
215 inline
216 void
initImage(triple<ImageIterator,ImageIterator,Accessor> img,VALUETYPE v)217 initImage(triple<ImageIterator, ImageIterator, Accessor> img, VALUETYPE v)
218 {
219 initImage(img.first, img.second, img.third, v);
220 }
221
222 /********************************************************/
223 /* */
224 /* initImage */
225 /* */
226 /********************************************************/
227
228 /** \brief Write the result of a functor call to every pixel in an image or rectangular ROI.
229
230 This function can be used to init the image by calling the given
231 functor for each pixel.
232 It uses an accessor to access the pixel data.
233
234 <b> Declarations:</b>
235
236 pass arguments explicitly:
237 \code
238 namespace vigra {
239 template <class ImageIterator, class Accessor, class FUNCTOR>
240 void
241 initImageWithFunctor(ImageIterator upperleft, ImageIterator lowerright,
242 Accessor a, FUNCTOR f);
243 }
244 \endcode
245
246 use argument objects in conjunction with \ref ArgumentObjectFactories:
247 \code
248 namespace vigra {
249 template <class ImageIterator, class Accessor, class FUNCTOR>
250 void
251 initImageWithFunctor(triple<ImageIterator, ImageIterator, Accessor> img, FUNCTOR f);
252 }
253 \endcode
254
255 <b> Usage:</b>
256
257 <b>\#include</b> "<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>"<br>
258 Namespace: vigra
259
260 \code
261 struct Counter {
262 Counter() : count(0) {}
263
264 int operator()() const { return count++; }
265
266 mutable int count;
267 };
268
269 vigra::IImage img(100, 100);
270
271 // write the current count in every pixel
272 vigra::initImageWithFunctor(destImageRange(img), Counter());
273 \endcode
274
275 <b> Required Interface:</b>
276
277 \code
278 ImageIterator upperleft, lowerright;
279 ImageIterator::row_iterator ix = upperleft.rowIterator();
280
281 Accessor accessor;
282 Functor f;
283
284 accessor.set(f(), ix);
285 \endcode
286
287 */
288 template <class ImageIterator, class Accessor, class FUNCTOR>
289 void
initImageWithFunctor(ImageIterator upperleft,ImageIterator lowerright,Accessor a,FUNCTOR f)290 initImageWithFunctor(ImageIterator upperleft, ImageIterator lowerright,
291 Accessor a, FUNCTOR f)
292 {
293 int w = lowerright.x - upperleft.x;
294
295 for(; upperleft.y < lowerright.y; ++upperleft.y)
296 {
297 initLineFunctor(upperleft.rowIterator(),
298 upperleft.rowIterator() + w, a, f);
299 }
300 }
301
302 template <class ImageIterator, class Accessor, class FUNCTOR>
303 inline
304 void
initImageWithFunctor(triple<ImageIterator,ImageIterator,Accessor> img,FUNCTOR f)305 initImageWithFunctor(triple<ImageIterator, ImageIterator, Accessor> img, FUNCTOR f)
306 {
307 initImageWithFunctor(img.first, img.second, img.third, f);
308 }
309
310 /********************************************************/
311 /* */
312 /* initImageIf */
313 /* */
314 /********************************************************/
315
316 /** \brief Write value to pixel in the image if mask is true.
317
318 This function can be used to init a region-of-interest of the image.
319 It uses an accessor to access the pixel data.
320
321 <b> Declarations:</b>
322
323 pass arguments explicitly:
324 \code
325 namespace vigra {
326 template <class ImageIterator, class Accessor,
327 class MaskImageIterator, class MaskAccessor,
328 class VALUETYPE>
329 void
330 initImageIf(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
331 MaskImageIterator mask_upperleft, MaskAccessor ma,
332 VALUETYPE v)
333 }
334 \endcode
335
336 use argument objects in conjunction with \ref ArgumentObjectFactories:
337 \code
338 namespace vigra {
339 template <class ImageIterator, class Accessor,
340 class MaskImageIterator, class MaskAccessor,
341 class VALUETYPE>
342 void
343 initImageIf(triple<ImageIterator, ImageIterator, Accessor> img,
344 pair<MaskImageIterator, MaskAccessor> mask,
345 VALUETYPE v)
346 }
347 \endcode
348
349 <b> Usage:</b>
350
351 <b>\#include</b> "<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>"<br>
352 Namespace: vigra
353
354 \code
355 vigra::BImage img(100, 100);
356 vigra::BImage mask(100, 100);
357
358 // zero the ROI
359 vigra::initImageIf(destImageRange(img),
360 maskImage(mask),
361 vigra::NumericTraits<vigra::BImage::PixelType>::zero());
362 \endcode
363
364 <b> Required Interface:</b>
365
366 \code
367 ImageIterator upperleft, lowerright;
368 MaskImageIterator mask_upperleft;
369 ImageIterator::row_iterator ix = upperleft.rowIterator();
370 MaskImageIterator::row_iterator mx = mask_upperleft.rowIterator();
371
372 Accessor accessor;
373 MaskAccessor mask_accessor;
374 VALUETYPE v;
375
376 if(mask_accessor(mx)) accessor.set(v, ix);
377 \endcode
378
379 */
380 template <class ImageIterator, class Accessor,
381 class MaskImageIterator, class MaskAccessor,
382 class VALUETYPE>
383 void
initImageIf(ImageIterator upperleft,ImageIterator lowerright,Accessor a,MaskImageIterator mask_upperleft,MaskAccessor ma,VALUETYPE v)384 initImageIf(ImageIterator upperleft, ImageIterator lowerright, Accessor a,
385 MaskImageIterator mask_upperleft, MaskAccessor ma,
386 VALUETYPE v)
387 {
388 int w = lowerright.x - upperleft.x;
389
390 for(; upperleft.y < lowerright.y; ++upperleft.y, ++mask_upperleft.y)
391 {
392 initLineIf(upperleft.rowIterator(),
393 upperleft.rowIterator() + w, a,
394 mask_upperleft.rowIterator(), ma, v);
395 }
396 }
397
398 template <class ImageIterator, class Accessor,
399 class MaskImageIterator, class MaskAccessor,
400 class VALUETYPE>
401 inline
402 void
initImageIf(triple<ImageIterator,ImageIterator,Accessor> img,pair<MaskImageIterator,MaskAccessor> mask,VALUETYPE v)403 initImageIf(triple<ImageIterator, ImageIterator, Accessor> img,
404 pair<MaskImageIterator, MaskAccessor> mask,
405 VALUETYPE v)
406 {
407 initImageIf(img.first, img.second, img.third, mask.first, mask.second, v);
408 }
409
410 /********************************************************/
411 /* */
412 /* initImageBorder */
413 /* */
414 /********************************************************/
415
416 /** \brief Write value to the specified border pixels in the image.
417
418 A pixel is initialized if its distance to the border
419 is at most 'borderwidth'.
420 It uses an accessor to access the pixel data.
421
422 <b> Declarations:</b>
423
424 pass arguments explicitly:
425 \code
426 namespace vigra {
427 template <class ImageIterator, class Accessor, class VALUETYPE>
428 void
429 initImageBorder(ImageIterator upperleft, ImageIterator lowerright,
430 Accessor a, int border_width, VALUETYPE v)
431 }
432 \endcode
433
434 use argument objects in conjunction with \ref ArgumentObjectFactories:
435 \code
436 namespace vigra {
437 template <class ImageIterator, class Accessor, class VALUETYPE>
438 void
439 initImageBorder(triple<ImageIterator, ImageIterator, Accessor> img,
440 int border_width, VALUETYPE v)
441 }
442 \endcode
443
444 <b> Usage:</b>
445
446 <b>\#include</b> "<a href="initimage_8hxx-source.html">vigra/initimage.hxx</a>"<br>
447 Namespace: vigra
448
449 \code
450 vigra::BImage img(100, 100);
451
452 // zero a border of 5 pixel
453 vigra::initImageBorder(destImageRange(img),
454 5, vigra::NumericTraits<vigra::BImage::PixelType>::zero());
455 \endcode
456
457 <b> Required Interface:</b>
458
459 see \ref initImage()
460
461 */
462 template <class ImageIterator, class Accessor, class VALUETYPE>
463 inline
464 void
initImageBorder(ImageIterator upperleft,ImageIterator lowerright,Accessor a,int border_width,VALUETYPE v)465 initImageBorder(ImageIterator upperleft, ImageIterator lowerright,
466 Accessor a, int border_width, VALUETYPE v)
467 {
468 int w = lowerright.x - upperleft.x;
469 int h = lowerright.y - upperleft.y;
470
471 int hb = (border_width > h) ? h : border_width;
472 int wb = (border_width > w) ? w : border_width;
473
474 initImage(upperleft, upperleft+Diff2D(w,hb), a, v);
475 initImage(upperleft, upperleft+Diff2D(wb,h), a, v);
476 initImage(upperleft+Diff2D(0,h-hb), lowerright, a, v);
477 initImage(upperleft+Diff2D(w-wb,0), lowerright, a, v);
478 }
479
480 template <class ImageIterator, class Accessor, class VALUETYPE>
481 inline
482 void
initImageBorder(triple<ImageIterator,ImageIterator,Accessor> img,int border_width,VALUETYPE v)483 initImageBorder(triple<ImageIterator, ImageIterator, Accessor> img,
484 int border_width, VALUETYPE v)
485 {
486 initImageBorder(img.first, img.second, img.third, border_width, v);
487 }
488
489 //@}
490
491
492 } // namespace vigra
493
494 #endif // VIGRA_INITIMAGE_HXX
495