@@ -41,6 +41,7 @@ V4L2Grabber::V4L2Grabber(const std::string &device, int input, VideoStandard vid
41
41
_ioMethod(IO_METHOD_MMAP),
42
42
_fileDescriptor(-1 ),
43
43
_buffers(),
44
+ _pixelFormat(0 ),
44
45
_width(width),
45
46
_height(height),
46
47
_cropWidth(cropHorizontal),
@@ -368,9 +369,15 @@ void V4L2Grabber::init_device(VideoStandard videoStandard, int input)
368
369
}
369
370
370
371
// check pixel format
371
- if (fmt.fmt .pix .pixelformat != V4L2_PIX_FMT_UYVY)
372
+
373
+ switch (fmt.fmt .pix .pixelformat )
372
374
{
373
- throw_exception (" Only pixel format UYVY is supported" );
375
+ case V4L2_PIX_FMT_UYVY:
376
+ case V4L2_PIX_FMT_YUYV:
377
+ _pixelFormat = fmt.fmt .pix .pixelformat ;
378
+ break ;
379
+ default :
380
+ throw_exception (" Only pixel formats UYVY and YUYV are supported" );
374
381
}
375
382
376
383
if (_width > 0 || _height > 0 )
@@ -648,9 +655,23 @@ void V4L2Grabber::process_image(const uint8_t * data)
648
655
for (int xSource = _cropWidth + _pixelDecimation/2 , xDest = 0 ; xSource < _width - _cropWidth; xSource += _pixelDecimation, ++xDest)
649
656
{
650
657
int index = (_width * ySource + xSource) * 2 ;
651
- uint8_t y = data[index+1 ];
652
- uint8_t u = (xSource%2 == 0 ) ? data[index] : data[index-2 ];
653
- uint8_t v = (xSource%2 == 0 ) ? data[index+2 ] : data[index];
658
+ uint8_t y = 0 ;
659
+ uint8_t u = 0 ;
660
+ uint8_t v = 0 ;
661
+
662
+ switch (_pixelFormat)
663
+ {
664
+ case V4L2_PIX_FMT_UYVY:
665
+ y = data[index+1 ];
666
+ u = (xSource%2 == 0 ) ? data[index ] : data[index-2 ];
667
+ v = (xSource%2 == 0 ) ? data[index+2 ] : data[index ];
668
+ break ;
669
+ case V4L2_PIX_FMT_YUYV:
670
+ y = data[index];
671
+ u = (xSource%2 == 0 ) ? data[index+1 ] : data[index-1 ];
672
+ v = (xSource%2 == 0 ) ? data[index+3 ] : data[index+1 ];
673
+ break ;
674
+ }
654
675
655
676
ColorRgb & rgb = image (xDest, yDest);
656
677
yuv2rgb (y, u, v, rgb.red , rgb.green , rgb.blue );
0 commit comments