File size: 3,863 Bytes
caf1218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**

 * @author  Steven Lovegrove

 * Copyright (C) 2010  Steven Lovegrove

 *                     Imperial College London

 **/

#include <pangolin/display/display.h>
#include <pangolin/display/view.h>
#include <pangolin/video/video_input.h>
#include <pangolin/gl/gl.h>


void SetGlFormat(GLint& glformat, GLenum& gltype, const pangolin::PixelFormat& fmt)

{
    switch( fmt.channels) {
    case 1: glformat = GL_LUMINANCE; break;
    case 3: glformat = GL_RGB; break;
    case 4: glformat = GL_RGBA; break;
    default: throw std::runtime_error("Unable to display video format");
    }

    switch (fmt.channel_bits[0]) {
    case 8: gltype = GL_UNSIGNED_BYTE; break;
    case 16: gltype = GL_UNSIGNED_SHORT; break;
    case 32: gltype = GL_FLOAT; break;
    default: throw std::runtime_error("Unknown channel format");
    }
}

void VideoSample(const std::string uri)

{
    // Setup Video Source
    pangolin::VideoInput video(uri);
    const pangolin::PixelFormat vid_fmt = video.PixFormat();
    const unsigned w = video.Width();
    const unsigned h = video.Height();

    // Work out appropriate GL channel and format options
    GLint glformat;
    GLenum gltype;
    SetGlFormat(glformat, gltype, vid_fmt);
    
    // Create OpenGL window
    pangolin::CreateWindowAndBind("Main",w,h);

    // Create viewport for video with fixed aspect
    pangolin::View& vVideo = pangolin::Display("Video").SetAspect((float)w/h);

    // OpenGl Texture for video frame.
    pangolin::GlTexture texVideo(w,h,glformat,false,0,glformat,gltype);

    unsigned char* img = new unsigned char[video.SizeBytes()];

    for(int frame=0; !pangolin::ShouldQuit(); ++frame)
    {
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

        if( video.GrabNext(img,true) ) {
            texVideo.Upload( img, glformat, gltype );
        }

        // Activate video viewport and render texture
        vVideo.Activate();
        texVideo.RenderToViewportFlipY();

        // Swap back buffer with front and process window events
        pangolin::FinishFrame();
    }

    delete[] img;
}


int main( int argc, char* argv[] )

{
    std::string uris[] = {
        "convert:[fmt=RGB24]//v4l:///dev/video0",
        "convert:[fmt=RGB24]//v4l:///dev/video1",
        "dc1394:[fps=30,dma=10,size=640x480,iso=400]//0",
        "openni:[img1=rgb]//",
        "test:[size=160x120,n=1,fmt=RGB24]//"
        ""
    };

    if( argc > 1 ) {
        const std::string uri = std::string(argv[1]);
        VideoSample(uri);
    }else{
        std::cout << "Usage  : SimpleRecord [video-uri]" << std::endl << std::endl;
        std::cout << "Where video-uri describes a stream or file resource, e.g." << std::endl;
        std::cout << "\tfile:[realtime=1]///home/user/video/movie.pvn" << std::endl;
        std::cout << "\tfile:///home/user/video/movie.avi" << std::endl;
        std::cout << "\tfiles:///home/user/seqiemce/foo%03d.jpeg" << std::endl;
        std::cout << "\tdc1394:[fmt=RGB24,size=640x480,fps=30,iso=400,dma=10]//0" << std::endl;
        std::cout << "\tdc1394:[fmt=FORMAT7_1,size=640x480,pos=2+2,iso=400,dma=10]//0" << std::endl;
        std::cout << "\tv4l:///dev/video0" << std::endl;
        std::cout << "\tconvert:[fmt=RGB24]//v4l:///dev/video0" << std::endl;
        std::cout << "\tmjpeg://http://127.0.0.1/?action=stream" << std::endl;
        std::cout << "\topenni:[img1=rgb]//" << std::endl;
        std::cout << std::endl;

        // Try to open some video device
        for(int i=0; !uris[i].empty(); ++i )
        {
            try{
                std::cout << "Trying: " << uris[i] << std::endl;
                VideoSample(uris[i]);
                return 0;
            }catch(const std::exception&) { }
        }
    }

    return 0;
}