47struct v4l2_format v4l2_fmt;
49static int xioctl (
int v4l_file,
int request,
void *arg)
53 do r = ioctl (v4l_file, request, arg);
54 while (-1 == r && EINTR == errno);
59void errno_exit (
const char *s)
61 fprintf (stderr,
"%s error %d, %s\n",s, errno, strerror (errno));
66V4L2IO::V4L2IO(
const string & fname, IOMode rw)
100int V4L2IO::globalinit(
const char *fsp,
int input,
int brt,
int cont,
int gamma,
int expos,
int gain)
105 int vfile = open (fsp, O_RDWR, 0);
108 struct v4l2_capability cap;
109 struct v4l2_cropcap cropcap;
110 struct v4l2_crop crop;
112 if (-1 == xioctl (vfile, VIDIOC_QUERYCAP, &cap)) {
113 if (EINVAL == errno) {
114 fprintf (stderr,
"%s is not a V4L2 device, try /dev/vbi*\n",fsp);
117 errno_exit (
"VIDIOC_QUERYCAP");
121 printf(
"driver: %s\ncard %s\n",cap.driver,cap.card);
123 if (-1 == xioctl (vfile, VIDIOC_S_INPUT, &input)) errno_exit (
"VIDIOC_S_INPUT");
125 int std=V4L2_STD_NTSC_M;
126 if (-1 == xioctl (vfile, VIDIOC_S_STD, &std)) printf(
"Can't set NTSC standard\n");
129 if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
130 fprintf (stderr,
"%s is not a video capture device\n",fsp);
134 if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
135 fprintf (stderr,
"%s does not support read i/o\n",fsp);
172 v4l2_fmt.type=V4L2_BUF_TYPE_VIDEO_CAPTURE;
173 if (-1 == xioctl (vfile, VIDIOC_G_FMT, &v4l2_fmt)) errno_exit (
"VIDIOC_G_FMT");
175 v4l2_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
176 v4l2_fmt.fmt.pix.width = 640;
177 v4l2_fmt.fmt.pix.height = 480;
179 v4l2_fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
184 if (-1 == xioctl (vfile, VIDIOC_S_FMT, &v4l2_fmt)) errno_exit (
"VIDIOC_S_FMT");
189 min = v4l2_fmt.fmt.pix.width * 2;
190 if (v4l2_fmt.fmt.pix.bytesperline < min)
191 v4l2_fmt.fmt.pix.bytesperline = min;
192 min = v4l2_fmt.fmt.pix.bytesperline * v4l2_fmt.fmt.pix.height;
193 if (v4l2_fmt.fmt.pix.sizeimage < min)
194 v4l2_fmt.fmt.pix.sizeimage = min;
196 printf(
"fmt.fmt.pix.width = %d\n",v4l2_fmt.fmt.pix.width);
197 printf(
"fmt.fmt.pix.height = %d\n",v4l2_fmt.fmt.pix.height);
198 printf(
"fmt.fmt.pix.pixelformat = %4s\n",&v4l2_fmt.fmt.pix.pixelformat);
199 printf(
"fmt.fmt.pix.bytesperline = %d\n",v4l2_fmt.fmt.pix.bytesperline);
200 printf(
"fmt.fmt.pix.sizeimage = %d\n",v4l2_fmt.fmt.pix.sizeimage);
203 struct v4l2_queryctrl qc;
204 struct v4l2_control con;
206 qc.id=V4L2_CID_BRIGHTNESS;
207 ioctl(vfile,VIDIOC_QUERYCTRL,&qc);
208 printf(
"brightness = %d - %d by %d %d\n",qc.minimum,qc.maximum,qc.step,qc.default_value);
210 qc.id=V4L2_CID_CONTRAST;
211 ioctl(vfile,VIDIOC_QUERYCTRL,&qc);
212 printf(
"contrast = %d - %d by %d %d\n",qc.minimum,qc.maximum,qc.step,qc.default_value);
214 qc.id=V4L2_CID_GAMMA;
215 ioctl(vfile,VIDIOC_QUERYCTRL,&qc);
216 printf(
"gamma = %d - %d by %d %d\n",qc.minimum,qc.maximum,qc.step,qc.default_value);
218 qc.id=V4L2_CID_EXPOSURE;
219 ioctl(vfile,VIDIOC_QUERYCTRL,&qc);
220 printf(
"exposure = %d - %d by %d %d\n",qc.minimum,qc.maximum,qc.step,qc.default_value);
223 ioctl(vfile,VIDIOC_QUERYCTRL,&qc);
224 printf(
"gain = %d - %d by %d %d\n",qc.minimum,qc.maximum,qc.step,qc.default_value);
226 con.id=V4L2_CID_AUTOGAIN;
228 ioctl(vfile,VIDIOC_S_CTRL,&con);
231 con.id=V4L2_CID_BRIGHTNESS;
233 ioctl(vfile,VIDIOC_S_CTRL,&con);
237 con.id=V4L2_CID_CONTRAST;
239 ioctl(vfile,VIDIOC_S_CTRL,&con);
243 con.id=V4L2_CID_GAMMA;
245 ioctl(vfile,VIDIOC_S_CTRL,&con);
249 con.id=V4L2_CID_EXPOSURE;
251 ioctl(vfile,VIDIOC_S_CTRL,&con);
255 con.id=V4L2_CID_GAIN;
257 ioctl(vfile,VIDIOC_S_CTRL,&con);
275 if(image_index == -1) {
279 if(image_index != 0) {
280 throw ImageReadException(
filename,
"no stack allowed for MRC image. For take 2D slice out of 3D image, read the 3D image first, then use get_clip().");
285 dict[
"nx"] = (int)(v4l2_fmt.fmt.pix.width);
286 dict[
"ny"] = (int)(v4l2_fmt.fmt.pix.height);
299 LOGWARN(
"V4L write is not supported.");
308 Region tmp(0,0,v4l2_fmt.fmt.pix.width,v4l2_fmt.fmt.pix.height);
309 if (!area) area=&tmp;
313 unsigned char *dbuf = (
unsigned char *)malloc(v4l2_fmt.fmt.pix.sizeimage);
315 read(
v4l_file,dbuf,v4l2_fmt.fmt.pix.sizeimage);
316 read(
v4l_file,dbuf,v4l2_fmt.fmt.pix.sizeimage);
331 for (
y=0;
y<area->
size[1];
y++) {
332 for (
x=0;
x<area->
size[0];
x++) {
337 data[(int)(area->
size[0]*(area->
size[1]-
y-1)+
x)]=dbuf[(
int)area->
size[0]*
y+
x]/256.0;
Dict is a dictionary to store <string, EMObject> pair.
EMDataType
Image pixel data type used in EMAN.
virtual void flush()=0
Flush the IO buffer.
virtual int write_header(const Dict &dict, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)=0
Write a header to an image.
virtual int write_data(float *data, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)=0
Write data to an image.
virtual bool is_complex_mode()=0
Is this an complex image or not.
virtual int read_header(Dict &dict, int image_index=0, const Region *area=0, bool is_3d=false)=0
Read the header from an image.
virtual int read_data(float *data, int image_index=0, const Region *area=0, bool is_3d=false)=0
Read the data from an image.
virtual void init()=0
Do some initialization before doing the read/write.
virtual bool is_image_big_endian()=0
Is this image in big endian or not.
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
static int globalinit(const char *fsp, int input=0, int brt=-1, int cont=-1, int gamma=-1, int expos=-1, int gain=-1)
static bool is_valid(const void *first_block)
#define ImageReadException(filename, desc)