summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/video4linux/test_video4linux.cc
blob: 8e526be46effcb4cbc92a3f9149d413fd5c8b85d (plain)
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
// test_video4linux.cc
// 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/
//      Email: robot AT efrei DOT fr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "video4linux.hh"
#include "utils/errno_exception.hh"

#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include <string>

int
main (int argc, char **argv)
{
    try
      {
	if ((argc != 3) && (argc != 4))
	    throw std::runtime_error ("Syntaxe : fichier "
				      "{rgb|bgr|yuv|yuv422} [luminosit´┐Ż]");
	std::string format (argv[2]);
	Image::PixelFormat pf;
	if (format == "rgb")
	    pf = Image::rgb;
	else if (format == "bgr")
	    pf = Image::bgr;
	else if (format == "yuv")
	    pf = Image::yuv;
	else if (format == "yuv422")
	    pf = Image::yuv422;
	else
	    throw std::invalid_argument ("Syntaxe : fichier {rgb|yuv422}");
	int lum = 58000;
	if (argc == 4)
	  {
	    std::istringstream isLum (argv[3]);
	    isLum >> 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);
	int s;
	s = Image::getBufSize (w, h, pf);
	uint8_t *buf = new uint8_t[s];
	for (int i = 0; i < 10; ++i)
	  {
	    vid.read (buf, s);
	    std::ostringstream os;
	    os << argv[1] << std::setw (3) << std::setfill ('0') << i << '.'
		<< argv[2];
	    int fd = open (os.str ().c_str (), O_WRONLY | O_CREAT, 0666);
	    if (fd == -1)
		throw errno_exception (argv[1], errno);
	    write (fd, buf, s);
	    close (fd);
	  }
	delete[] buf;
	return 0;
      }
    catch (const std::exception &e)
      {
	std::cerr << e.what () << std::endl;
	return 1;
      }
}