1 // This is mul/vil3d/vil3d_from_image_2d.h
2 #ifndef vil3d_from_image_2d_h_
3 #define vil3d_from_image_2d_h_
4 
5 
6 //:
7 // \file
8 // \author Kevin de Souza
9 
10 #include <vil/vil_image_view.h>
11 #include <vil3d/vil3d_image_view.h>
12 #include <cassert>
13 #ifdef _MSC_VER
14 #  include <vcl_msvc_warnings.h>
15 #endif
16 
17 
18 //: Return a 3D image view containing a single slice obtained from a 2D image.
19 //  result(x,y,0,p)=im(x,y,p)
20 // \relatesalso vil3d_image_view
21 // \relatesalso vil_image_view
22 // \note Assumes that input image planes (if more than 1) are stored separately,
23 // i.e. that im.planestep()==im.ni()*im.nj()
24 template <class T>
vil3d_from_image_2d(const vil_image_view<T> & im)25 inline vil3d_image_view<T> vil3d_from_image_2d(const vil_image_view<T>& im)
26 {
27   if (im.is_contiguous())
28   {
29     std::ptrdiff_t kstep = im.ni()*im.nj();
30     unsigned nk = 1;
31     std::ptrdiff_t pstep = im.planestep();
32 
33     // Insist on a particular ordering of input image data
34     assert(pstep == kstep);
35 
36     return vil3d_image_view<T>(im.memory_chunk(),
37                                im.top_left_ptr(),
38                                im.ni(), im.nj(), nk, im.nplanes(),
39                                im.istep(), im.jstep(), kstep, pstep);
40   }
41   else
42   {
43     return vil3d_image_view<T>();
44   }
45 }
46 
47 
48 #endif // vil3d_from_image_2d_h_
49