Rev Author Line No. Line
178 kaklik 1 #include <boost/shared_array.hpp>
2 #include <errno.h>
3 #include "image_v4linput.h"
4  
5 #include "colourspace.h"
6  
7 using namespace boost;
8 using namespace std;
9  
10 namespace mimas {
11  
12  
13 template<>
14 void image_v4linput< rgba< unsigned char > >::read
15 (image< rgba< unsigned char > > &img ) throw (mimasexception)
16 {
17 // State-variable never becomes false. Input from video4linux only comes
18 // to an end by an error.
19 assert( state == true );
20  
21 shared_array< unsigned char > buffer;
22 void *src = NULL;
23  
24 if ( map != MAP_FAILED ) {
25 video_mmap vmap;
26 vmap.frame = 0;
27 vmap.width = win.width;
28 vmap.height = win.height;
29 vmap.format = pic.palette;
30  
31 MMERROR( xioctl( VIDIOCMCAPTURE, &vmap ) == 0, mimasexception, ,
32 "Error initiating capture of image: " << strerror( errno ) );
33  
34 MMERROR( xioctl( VIDIOCSYNC, &vmap ) == 0, mimasexception, ,
35 "Error on synchronising with capture of image: "
36 << strerror( errno ) );
37  
38 src = map;
39 } else {
40 int size;
41 switch ( pic.palette ) {
42 case VIDEO_PALETTE_RGB24:
43 size = 3 * win.width * win.height;
44 break;
45 case VIDEO_PALETTE_YUV420P:
46 size = win.width * win.height * 3 / 2;
47 break;
48 case VIDEO_PALETTE_UYVY:
49 case VIDEO_PALETTE_YUV422:
50 size = win.width * win.height * 2;
51 break;
52 default:
53 assert( false );
54 MMERROR( false, mimasexception, ,
55 "Software error in colour-grabbing code. Encountered "
56 "unsupported palette number " << pic.palette << "." );
57 };
58 buffer = shared_array< unsigned char >( new unsigned char[ size ] );
59 src = (void *)buffer.get();
60 MMERROR( ::read( fd, src, size ) != -1, mimasexception, ,
61 "Error reading from device: " << strerror( errno ) );
62 };
63  
64 img.init( win.width, win.height );
65  
66 #ifndef NDEBUG
67 std::cerr << img.rawData() << ' '
68 << img.getSize() << ' '
69 << img.getWidth() << ' ' << img.getHeight() << std::endl;
70  
71 #endif
72 switch ( pic.palette ) {
73 case VIDEO_PALETTE_RGB24:
74 rgb_to_rgba( (const char *)src, win.width, win.height,
75 (char *)img.rawData() );
76 break;
77 case VIDEO_PALETTE_YUV420P:
78 yuv420p_to_rgba( (const char *)src, win.width, win.height,
79 (char *)img.rawData() );
80 break;
81 case VIDEO_PALETTE_UYVY:
82 case VIDEO_PALETTE_YUV422:
83 uyvy_to_rgba( (const char *)src, win.width, win.height,
84 (char *)img.rawData() );
85 break;
86 default:
87 assert( false );
88 MMERROR( false, mimasexception, ,
89 "Software error in colour-grabbing code. Encountered "
90 "unsupported palette number " << pic.palette << "." );
91 };
92 }
93  
94 template<>
95 void image_v4linput< rgba< unsigned char > >::selectPalette(void)
96 throw(mimasexception)
97 {
98 boost::array< __u16, 14 > palette;
99 palette[ 0 ] = VIDEO_PALETTE_YUV420P;// ->RGBA supported
100 palette[ 1 ] = VIDEO_PALETTE_UYVY;// ->RGBA supported
101 palette[ 2 ] = VIDEO_PALETTE_YUV422;// -> RGBA supported
102 palette[ 3 ] = VIDEO_PALETTE_RGB24;// ->RGBA supported
103 palette[ 4 ] = VIDEO_PALETTE_HI240;
104 palette[ 5 ] = VIDEO_PALETTE_RGB565;
105 palette[ 6 ] = VIDEO_PALETTE_RGB555;
106 palette[ 7 ] = VIDEO_PALETTE_RGB32;
107 palette[ 8 ] = VIDEO_PALETTE_YUYV;
108 palette[ 9 ] = VIDEO_PALETTE_YUV420;
109 palette[ 10 ] = VIDEO_PALETTE_YUV411;
110 palette[ 11 ] = VIDEO_PALETTE_RAW;
111 palette[ 12 ] = VIDEO_PALETTE_YUV422P;
112 palette[ 13 ] = VIDEO_PALETTE_YUV411P;
113  
114 pic.brightness = pic.hue = pic.colour = pic.contrast = pic.whiteness = 32767;
115 pic.depth = 24;
116  
117 int selected = 0;
118  
119 while ( true ) {
120  
121 pic.palette = palette[ selected ];
122 int r = xioctl( VIDIOCSPICT, &pic );
123 if ( r == 0 ) {
124 #ifndef NDEBUG
125 std::cerr << "Using " << ( selected + 1 ) << "th supported option."
126 << std::endl;
127 #endif
128 break;
129 };
130 selected++;
131 MMERROR( selected < (signed)palette.size(),
132 mimasexception, ,
133 "Camera-driver doesn't offer a colour-video-palette known to "
134 "mimas." );
135 };
136 MMERROR( selected < 4, mimasexception, ,
137 "Colour-grabbing with video-palette "
138 << palette[ selected ] << " (kmando-index " << selected
139 << ") not supported by kmando. Please contact the developers." );
140 }
141  
142 };
143