1 // This is conversions/image/Image_to_vil1.h
2 #ifndef Image_to_vil1_h_
3 #define Image_to_vil1_h_
4 
5 #include <iostream>
6 #include <ImageClasses/Image.h>
7 #include <vil1/vil1_image.h>
8 
9 #include <vil1/vil1_load.h>
10 #include <vil1/vil1_memory_image.h>
11 
12 #ifdef _MSC_VER
13 #  include <vcl_msvc_warnings.h>
14 #endif
15 
Image_to_vil1(Image const * im)16 inline vil1_image Image_to_vil1(Image const* im)
17 {
18   // First try to load directly from file.  This will give wrong results
19   // when im was copied into memory from a file image and then modified.
20   const char* name = im->GetName();
21   if (name && *name)
22   {
23     vil1_image imo = vil1_load(name);
24     if (imo) return imo;
25   }
26   // was not able to open original file - probably this is an in-memory image:
27   int cmpts = (im->GetImageClass() == 'C') ? 3 : 1; // very rough ! FIXME
28   int width = im->GetSizeX();
29   int height = im->GetSizeY();
30   int bpc = im->GetBitsPixel() / cmpts;
31   vil1_component_format format =
32     (im->GetFormat() == 'A') ? VIL1_COMPONENT_FORMAT_IEEE_FLOAT :
33     (im->GetFormat() == 'Y') ? VIL1_COMPONENT_FORMAT_COMPLEX :
34                                VIL1_COMPONENT_FORMAT_UNSIGNED_INT;
35   if (im->GetBitsPixel() != bpc * cmpts)
36     { std::cerr << "Image_to_vil1: Error: pixel size\n"; return 0; }
37 
38   vil1_memory_image imo(width, height, cmpts, bpc, format);
39 
40   void* buf = im->GetSection((void*)0, 0, 0, width, height);
41   imo.put_section(buf, 0, 0, width, height);
42   delete[] (char*)buf;
43   return imo;
44 }
45 
46 #endif // Image_to_vil1_h_
47