function [devName, roiTop, roiLeft, roiWidth, roiHeight, width, height] = v4l2Setup(m_devName, roi, imSize, pixelFormat, simOutput)
%V4L2SETUP Compute V4L2 capture setup parameters.

% Copyright 2010 The MathWorks, Inc.

checkBlockParams(m_devName, roi, imSize, pixelFormat, simOutput);

% Place a terminator character at the end of string
devName = [uint8(m_devName), uint8(0)];

% Set parameters for region of interest
roiTop    = roi(1);
roiLeft   = roi(2);
roiWidth  = roi(3);
roiHeight = roi(4);

% Set image width and image height
width     = imSize(1);
height    = imSize(2);


%==========================================================================
function checkBlockParams(devName, roi, imSize, pixelFormat, simOutput)
if ~ischar(devName)
    ME = MException('Target:devNameNotChar', ...
        'Device name must be a character array.');
    throwAsCaller(ME);
end

% Check ROI
if (~isnumeric(roi) || (numel(roi) ~= 4) )
    ME = MException('Target:invalidRoi', ...
        'Region of interest must be a numeric four-element vector in the form [top, left, width, height].');
    throwAsCaller(ME);
end
if any(find(roi < 0))
    ME = MException('Target:invalidRoi', ...
        'Invalid entry for the Region of interest parameter. Top, left, width and height must be non-negative.');
    throwAsCaller(ME);
end
if any(find(roi(3:4) <= 0))
    ME = MException('Target:invalidRoi', ...
        'Invalid entry for the Region of interest parameter. Width and height must be positive.');
    throwAsCaller(ME);
end

%Check imSize
if (~isnumeric(imSize) || (numel(imSize) ~= 2) )
    ME = MException('Target:invalidImageSize', ...
        'Image size must be a numeric two-element vector in the form [width, height]. ');
    throwAsCaller(ME);
end
if any(find(imSize <= 0))
    ME = MException('Target:invalidImageSize', ...
        'Invalid entry for the Image size parameter. Width and height must be positive.');
    throwAsCaller(ME);
end
if ((pixelFormat == 2) && (mod(imSize(1), 2) ~= 0))
    ME = MException('Target:invalidImageSize', ...
        'In YUYV mode the image width must be a multiple of two.');
    throwAsCaller(ME);
end

%[EOF]

