Permalink
Browse files

Adding v4l2 and video interface. In progress

Signed-off-by: Steven Lovegrove <stevenlovegrove@gmail.com>
1 parent e3f8978 commit 6b2a9278c6d79482f47585d04e47eb23f575fadc @stevenlovegrove committed Apr 10, 2011
View
22 LICENCE
@@ -0,0 +1,22 @@
+Copyright (c) 2011 Steven Lovegrove and Richard Newcombe
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -5,47 +5,140 @@
**/
#include <pangolin/pangolin.h>
-#include <pangolin/videosource.h>
+#include <pangolin/firewire.h>
+#include <pangolin/v4l.h>
using namespace pangolin;
-int main( int /*argc*/, char* argv[] )
+int firewire_sample()
{
- // Setup Firewire Camera
- FirewireVideo video(0,DC1394_VIDEO_MODE_640x480_RGB8,DC1394_FRAMERATE_30,DC1394_ISO_SPEED_400,50);
- const unsigned w = video.Width();
- const unsigned h = video.Height();
-
- // Create Glut window
- pangolin::CreateGlutWindowAndBind("Main",w,h);
+ // Setup Firewire Camera
+ FirewireVideo video(0,DC1394_VIDEO_MODE_640x480_RGB8,DC1394_FRAMERATE_30,DC1394_ISO_SPEED_400,50);
+ const unsigned w = video.Width();
+ const unsigned h = video.Height();
- // Create viewport for video with fixed aspect
- View& vVideo = Display("Video").SetAspect((float)w/h);
+ // Create Glut window
+ pangolin::CreateGlutWindowAndBind("Main",w,h);
- // OpenGl Texture for video frame
- GlTexture texVideo(w,h,GL_RGBA8);
+ // Create viewport for video with fixed aspect
+ View& vVideo = Display("Video").SetAspect((float)w/h);
- for(int frame=0; !pangolin::ShouldQuit(); ++frame)
- {
- glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
+ // OpenGl Texture for video frame
+ GlTexture texVideo(w,h,GL_RGBA8);
- // Get newest frame from camera and upload to GPU as texture
+ for(int frame=0; !pangolin::ShouldQuit(); ++frame)
{
- FirewireFrame frame = video.GetNewest(true);
- texVideo.Upload(frame.Image(),GL_RGB,GL_UNSIGNED_BYTE);
- video.PutFrame(frame);
+ glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
+
+ // Get newest frame from camera and upload to GPU as texture
+ {
+ FirewireFrame frame = video.GetNewest(true);
+ texVideo.Upload(frame.Image(),GL_RGB,GL_UNSIGNED_BYTE);
+ video.PutFrame(frame);
+ }
+
+ // Activate video viewport and render texture
+ vVideo.Activate();
+ texVideo.RenderToViewportFlipY();
+
+ // Swap back buffer with front
+ glutSwapBuffers();
+
+ // Process window events via GLUT
+ glutMainLoopEvent();
}
- // Activate video viewport and render texture
- vVideo.Activate();
- texVideo.RenderToViewportFlipY();
+ return 0;
+}
+
+// borrowed tempararily and altered from libfreenect
+// https://github.com/OpenKinect/libfreenect/blob/master/src/cameras.c
+#define CLAMP(x) if (x < 0) {x = 0;} if (x > 255) {x = 255;}
+static void convert_yuyv_to_rgb(uint8_t *raw_buf, uint8_t *proc_buf)
+{
+ int x, y;
+ for(y = 0; y < 480; ++y) {
+ for(x = 0; x < 640; x+=2) {
+ int i = (640 * y + x);
+ int y1 = raw_buf[2*i];
+ int u = raw_buf[2*i+1];
+ int y2 = raw_buf[2*i+2];
+ int v = raw_buf[2*i+3];
+ int r1 = (y1-16)*1164/1000 + (v-128)*1596/1000;
+ int g1 = (y1-16)*1164/1000 - (v-128)*813/1000 - (u-128)*391/1000;
+ int b1 = (y1-16)*1164/1000 + (u-128)*2018/1000;
+ int r2 = (y2-16)*1164/1000 + (v-128)*1596/1000;
+ int g2 = (y2-16)*1164/1000 - (v-128)*813/1000 - (u-128)*391/1000;
+ int b2 = (y2-16)*1164/1000 + (u-128)*2018/1000;
+ CLAMP(r1);
+ CLAMP(g1);
+ CLAMP(b1);
+ CLAMP(r2);
+ CLAMP(g2);
+ CLAMP(b2);
+ proc_buf[3*i] =r1;
+ proc_buf[3*i+1]=g1;
+ proc_buf[3*i+2]=b1;
+ proc_buf[3*i+3]=r2;
+ proc_buf[3*i+4]=g2;
+ proc_buf[3*i+5]=b2;
+ }
+ }
+}
+#undef CLAMP
+
+// !V4L interface subject to change dramatically
+int v4l_sample()
+{
+ // Setup Firewire Camera
+ V4lVideo video("/dev/video0");
+ // FirewireVideo video(0,DC1394_VIDEO_MODE_640x480_RGB8,DC1394_FRAMERATE_30,DC1394_ISO_SPEED_400,50);
+ const unsigned w = video.Width();
+ const unsigned h = video.Height();
+
+ // Create Glut window
+ pangolin::CreateGlutWindowAndBind("Main",w,h);
+
+ // Create viewport for video with fixed aspect
+ View& vVideo = Display("Video").SetAspect((float)w/h);
+
+ // OpenGl Texture for video frame
+ GlTexture texVideo(w,h,GL_RGBA8);
+
+ for(int frame=0; !pangolin::ShouldQuit(); ++frame)
+ {
+ glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
+
+ // Get newest frame from camera and upload to GPU as texture
+ // {
+ // FirewireFrame frame = video.GetNewest(true);
+ // texVideo.Upload(frame.Image(),GL_RGB,GL_UNSIGNED_BYTE);
+ // video.PutFrame(frame);
+ // }
+ {
+ static unsigned char yuv[640*480*3];
+ static unsigned char rgb[640*480*3];
+ video.GrabNext(yuv,true);
+ convert_yuyv_to_rgb(yuv,rgb);
+ texVideo.Upload(rgb,GL_RGB,GL_UNSIGNED_BYTE);
+ }
- // Swap back buffer with front
- glutSwapBuffers();
+ // Activate video viewport and render texture
+ vVideo.Activate();
+ texVideo.RenderToViewportFlipY();
- // Process window events via GLUT
- glutMainLoopEvent();
- }
+ // Swap back buffer with front
+ glutSwapBuffers();
- return 0;
+ // Process window events via GLUT
+ glutMainLoopEvent();
+ }
+
+ return 0;
+}
+
+int main( int /*argc*/, char* argv[] )
+{
+// firewire_sample();
+ v4l_sample();
}
View
@@ -11,7 +11,7 @@ SET(
widgets.h widgets.cpp
plotter.h plotter.cpp
gl.h glcuda.h cg.h
- videosource.h videosource.cpp
+ video.h firewire.h firewire.cpp v4l.h v4l.cpp
)
#######################################################
@@ -1,4 +1,4 @@
-#include "videosource.h"
+#include "firewire.h"
#ifdef HAVE_DC1394
#include <stdio.h>
@@ -25,10 +25,12 @@
* OTHER DEALINGS IN THE SOFTWARE.
*/
-#ifndef PANGOLIN_VIDEOSOURCE_H
-#define PANGOLIN_VIDEOSOURCE_H
+#ifndef PANGOLIN_FIREWIRE_H
+#define PANGOLIN_FIREWIRE_H
#include "pangolin.h"
+#include "video.h"
+
#ifdef HAVE_DC1394
#include <dc1394/dc1394.h>
@@ -40,14 +42,6 @@
namespace pangolin
{
-struct VideoException : std::exception
-{
- VideoException(std::string str) : desc(str) {}
- ~VideoException() throw() {}
- const char* what() const throw() { return desc.c_str(); }
- std::string desc;
-};
-
class FirewireFrame
{
friend class FirewireVideo;
@@ -68,7 +62,7 @@ struct Guid
uint64_t guid;
};
-class FirewireVideo
+class FirewireVideo : public VideoSource
{
public:
FirewireVideo(
@@ -89,21 +83,22 @@ class FirewireVideo
~FirewireVideo();
- int Width() const { return width; }
- int Height() const { return height; }
+ //! Implement VideoSource::Width()
+ unsigned Width() const { return width; }
+
+ //! Implement VideoSource::Height()
+ unsigned Height() const { return height; }
+ //! Implement VideoSource::Start()
void Start();
+
+ //! Implement VideoSource::Stop()
void Stop();
- //! Copy the next frame from the camera to image.
- //! Optionally wait for a frame if one isn't ready
- //! Returns true iff image was copied
+ //! Implement VideoSource::GrabNext()
bool GrabNext( unsigned char* image, bool wait = true );
- //! Copy the newest frame from the camera to image
- //! discarding all older frames.
- //! Optionally wait for a frame if one isn't ready
- //! Returns true iff image was copied
+ //! Implement VideoSource::GrabNewest()
bool GrabNewest( unsigned char* image, bool wait = true );
//! Return object containing reference to image data within
@@ -134,7 +129,7 @@ class FirewireVideo
bool running;
dc1394camera_t *camera;
- unsigned int width, height;
+ unsigned width, height;
//dc1394featureset_t features;
dc1394_t * d;
dc1394camera_list_t * list;
@@ -145,4 +140,4 @@ class FirewireVideo
#endif // HAVE_DC1394
-#endif // PANGOLIN_VIDEOSOURCE_H
+#endif // PANGOLIN_FIREWIRE_H
Oops, something went wrong.

0 comments on commit 6b2a927

Please sign in to comment.