summaryrefslogtreecommitdiff
path: root/2005/i/robert
diff options
context:
space:
mode:
authorgaillaro2005-05-01 11:19:15 +0000
committergaillaro2005-05-01 11:19:15 +0000
commitf6f91b02b93a0dda3aed4753306ed67f4bdeb972 (patch)
tree95137b30a8db2fce80adb296ae87abbcf5cf91b2 /2005/i/robert
parentb8eada804c20bbb3a9e56fca62c38a08ee7de531 (diff)
reader de la nouvelle camera
Diffstat (limited to '2005/i/robert')
-rw-r--r--2005/i/robert/src/video4linux/test_video4linux.cc7
-rw-r--r--2005/i/robert/src/video4linux/video4linux.cc113
-rw-r--r--2005/i/robert/src/video4linux/video4linux.hh14
3 files changed, 75 insertions, 59 deletions
diff --git a/2005/i/robert/src/video4linux/test_video4linux.cc b/2005/i/robert/src/video4linux/test_video4linux.cc
index 73e1620..8e526be 100644
--- a/2005/i/robert/src/video4linux/test_video4linux.cc
+++ b/2005/i/robert/src/video4linux/test_video4linux.cc
@@ -2,6 +2,7 @@
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Nicolas Schodet
+// Modified by Olivier Gaillard
//
// Robot APB Team/Efrei 2005.
// Web: http://assos.efrei.fr/robot/
@@ -59,12 +60,14 @@ main (int argc, char **argv)
std::istringstream isLum (argv[3]);
isLum >> lum;
}
- Video4Linux vid ("/dev/video", pf, lum);
+ const unsigned width = 640;
+ const unsigned height = 480;
+ Video4Linux vid ("/dev/video", width, height, pf, lum);
vid.calibrate ();
int w, h;
vid.getParam (w, h, pf);
std::cout << w << ' ' << h << std::endl;
- vid.setAdaptive (0);
+// vid.setAdaptive (0);
int s;
s = Image::getBufSize (w, h, pf);
uint8_t *buf = new uint8_t[s];
diff --git a/2005/i/robert/src/video4linux/video4linux.cc b/2005/i/robert/src/video4linux/video4linux.cc
index 3c20004..ddffde9 100644
--- a/2005/i/robert/src/video4linux/video4linux.cc
+++ b/2005/i/robert/src/video4linux/video4linux.cc
@@ -2,6 +2,7 @@
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Nicolas Schodet
+// Modified by Olivier Gaillard
//
// Robot APB Team/Efrei 2005.
// Web: http://assos.efrei.fr/robot/
@@ -28,15 +29,14 @@
#include <unistd.h>
#include <fcntl.h>
-#include <linux/videodev.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <stdexcept>
/// Constructeur.
-Video4Linux::Video4Linux (const char *dev, Image::PixelFormat pixelFormat,
- int brightness /*58000*/)
- : fd_ (-1), width_ (-1), height_ (-1), pixelFormat_ (pixelFormat),
+Video4Linux::Video4Linux (const char *dev, int width, int height,
+ Image::PixelFormat pixelFormat, int brightness /*58000*/)
+ : width_ (width), height_ (height), fd_ (-1), pixelFormat_ (pixelFormat),
rgb_ (pixelFormat == Image::rgb || pixelFormat == Image::bgr),
brightness_ (brightness), contrast_ (32768)
{
@@ -48,6 +48,8 @@ Video4Linux::Video4Linux (const char *dev, Image::PixelFormat pixelFormat,
/// Destructeur.
Video4Linux::~Video4Linux (void)
{
+ // Munmap.
+ munmap (map_, bufSize_);
close ();
}
@@ -55,63 +57,59 @@ Video4Linux::~Video4Linux (void)
void
Video4Linux::read (uint8_t *buf, unsigned size)
{
- if (buf && size != width_ * height_ * bpp_)
+ static const unsigned nbPixels = 3;
+ if (buf && size != width_ * height_ * nbPixels)
throw std::invalid_argument ("Video4Linux::read");
- // Mmap.
- void *map;
- map = mmap (0, bufSize_, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);
+ static const unsigned widthHeight = width_*height_;
+ static const unsigned widthHeightDiv4 = widthHeight/4;
// Capture.
- video_mmap mmap;
- mmap.format = rgb_ ? VIDEO_PALETTE_RGB24 : VIDEO_PALETTE_YUV422;
- mmap.frame = 0;
- mmap.width = width_;
- mmap.height = height_;
- ioctl (fd_, VIDIOCMCAPTURE, &mmap);
+ ioctl (fd_, VIDIOCMCAPTURE, &mmap_);
// Sync.
- ioctl (fd_, VIDIOCSYNC, &mmap);
+ ioctl (fd_, VIDIOCSYNC, &mmap_.frame);
// Copy.
if (buf)
{
- unsigned char *src = reinterpret_cast<unsigned char *> (map);
+ unsigned char *srcY = reinterpret_cast<unsigned char *> (map_);
+ unsigned char *srcY2 = srcY + width_;
+ unsigned char *srcU = srcY + widthHeight;
+ unsigned char *srcV = srcU + widthHeightDiv4;
unsigned char *dst = reinterpret_cast<unsigned char *> (buf);
+ unsigned char *dst2 = dst + width_*3;
switch (pixelFormat_)
{
case Image::rgb:
- for (int i = 0; i < width_ * height_; ++i)
- {
- *dst++ = src[2];
- *dst++ = src[1];
- *dst++ = src[0];
- src += 3;
- }
- break;
- memcpy (buf, map, size);
+ memcpy (buf, map_, size);
break;
case Image::yuv:
- for (int i = 0; i < width_ * height_ / 2; ++i)
+ for (unsigned i = 0; i < height_/2; ++i)
{
- *dst++ = src[0]; // Y1
- *dst++ = src[1]; // U
- *dst++ = src[3]; // V
- *dst++ = src[2]; // Y2
- *dst++ = src[1]; // U
- *dst++ = src[3]; // V
- src += 4;
+ for (unsigned j = 0; j < width_/2; ++j)
+ {
+ *dst++ = *srcY++; *dst2++ = *srcY2++;
+ *dst++ = *srcU; *dst2++ = *srcU;
+ *dst++ = *srcV; *dst2++ = *srcV;
+ *dst++ = *srcY++; *dst2++ = *srcY2++;
+ *dst++ = *srcU; *dst2++ = *srcU;
+ *dst++ = *srcV; *dst2++ = *srcV;
+ srcU++; srcV++;
+ }
+ dst += width_*3;
+ dst2 += width_*3;
+ srcY += width_;
+ srcY2 += width_;
}
break;
case Image::bgr:
case Image::yuv422:
case Image::hsi:
- memcpy (buf, map, size);
+ memcpy (buf, map_, size);
break;
}
}
- // Munmap.
- munmap (map, bufSize_);
}
/// Active/désactive la calibration automatique de la luminosité
-void
+/*void
Video4Linux::setAdaptive (int a)
{
if (fd_ != -1)
@@ -119,14 +117,14 @@ Video4Linux::setAdaptive (int a)
if (ioctl(fd_, VIDIOCQCSADAPTIVE, &a) != 0)
throw errno_exception ("dev <camera>", errno);
}
-}
+}*/
/// Calibre la caméra.
void
Video4Linux::calibrate (void)
{
- for (int i = 0; i < 100; ++i)
+ for (int i = 0; i < 30; ++i)
read (0, 0);
}
@@ -148,6 +146,19 @@ Video4Linux::open (const char *dev)
fd_ = ::open (dev, O_RDWR);
if (fd_ == -1)
throw errno_exception (dev, errno);
+ // Récupères les infos sur la camera.
+ video_window win;
+ win.width = width_;
+ win.height = height_;
+ ioctl (fd_, VIDIOCSWIN, &win);
+ ioctl (fd_, VIDIOCGWIN, &win);
+ width_ = win.width;
+ height_ = win.height;
+ // Initialisation mmap.
+ video_mbuf mbuf;
+ ioctl (fd_, VIDIOCGMBUF, &mbuf);
+ bufSize_ = mbuf.size;
+
// On récupère les cap.
video_capability cap;
if (ioctl (fd_, VIDIOCGCAP, &cap) == -1)
@@ -155,26 +166,24 @@ Video4Linux::open (const char *dev)
// On veut du RGB24.
video_picture pic;
ioctl (fd_, VIDIOCGPICT, &pic);
- pic.palette = rgb_ ? VIDEO_PALETTE_RGB24 : VIDEO_PALETTE_YUV422;
- bpp_ = Image::getBytePerPixel (pixelFormat_);
+ pic.palette = VIDEO_PALETTE_YUV420P;
+ bpp_ = 1.5;
pic.brightness = brightness_;
pic.contrast = contrast_;
ioctl (fd_, VIDIOCSPICT, &pic);
// Channel.
video_channel chn;
ioctl (fd_, VIDIOCGCHAN, &chn);
- chn.channel = 0;
- chn.norm = VIDEO_MODE_PAL;
+ chn.channel = 1;
+ chn.norm = VIDEO_MODE_NTSC;
ioctl (fd_, VIDIOCSCHAN, &chn);
- // Récupères les infos sur la camera.
- video_window win;
- ioctl (fd_, VIDIOCGWIN, &win);
- width_ = win.width;
- height_ = win.height;
- // Initialisation mmap.
- video_mbuf mbuf;
- ioctl (fd_, VIDIOCGMBUF, &mbuf);
- bufSize_ = mbuf.size;
+ // Mmap.
+ map_ = mmap (0, bufSize_, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);
+ // Init du video_mmap
+ mmap_.format = VIDEO_PALETTE_YUV420P;
+ mmap_.frame = 0;
+ mmap_.width = width_;
+ mmap_.height = height_;
}
/// Ferme le périphérique.
diff --git a/2005/i/robert/src/video4linux/video4linux.hh b/2005/i/robert/src/video4linux/video4linux.hh
index 32f382f..4fd9949 100644
--- a/2005/i/robert/src/video4linux/video4linux.hh
+++ b/2005/i/robert/src/video4linux/video4linux.hh
@@ -4,6 +4,7 @@
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Nicolas Schodet
+// Modified by Olivier Gaillard
//
// Robot APB Team/Efrei 2005.
// Web: http://assos.efrei.fr/robot/
@@ -25,24 +26,27 @@
//
// }}}
#include "image/image_reader.hh"
+#include <linux/videodev.h>
#define VIDIOCQCSADAPTIVE _IOWR('v',227, int)
/// Classe d'accés à Video4Linux. C'est un ImageReader.
class Video4Linux : public ImageReader
{
private:
- int fd_;
int width_, height_;
+ int fd_;
unsigned bufSize_;
Image::PixelFormat pixelFormat_;
bool rgb_;
int brightness_, contrast_;
- unsigned bpp_;
+ double bpp_;
+ void *map_;
+ video_mmap mmap_;
public:
/// Constructeur.
- Video4Linux (const char *dev, Image::PixelFormat pixelFormat,
- int brightness = 58000);
+ Video4Linux (const char *dev, int width, int height,
+ Image::PixelFormat pixelFormat, int brightness = 58000);
/// Destructeur.
~Video4Linux (void);
/// Récupère les paramètres de l'image.
@@ -56,7 +60,7 @@ class Video4Linux : public ImageReader
/// Attend qu'une image soit disponible, retourne true si oui.
bool wait (int timeout = -1);
/// Active/désactive la calibration automatique de la luminosité
- void setAdaptive (int a);
+ //void setAdaptive (int a);
protected:
/// Ouvre le périphérique.
void open (const char *dev);