use crate::error::CameraError;
pub type CameraResult<T> = Result<T, CameraError>;
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VideoFormat {
MJPEG,
YUYV,
UYVY,
NV12,
RGB,
H264,
Gray,
}
impl VideoFormat {
pub fn is_compressed(&self) -> bool {
matches!(self, VideoFormat::MJPEG | VideoFormat::H264)
}
pub fn bytes_per_pixel(&self) -> Option<usize> {
match self {
VideoFormat::YUYV | VideoFormat::UYVY => Some(2),
VideoFormat::NV12 => None, VideoFormat::RGB => Some(3),
VideoFormat::Gray => Some(1),
_ => None, }
}
pub fn frame_data_size(&self, width: u32, height: u32) -> Option<usize> {
let pixels = checked_pixel_count(width, height)?;
match self {
VideoFormat::YUYV | VideoFormat::UYVY => {
if !width.is_multiple_of(2) {
return None;
}
pixels.checked_mul(2)
}
VideoFormat::NV12 => {
if !width.is_multiple_of(2) || !height.is_multiple_of(2) {
return None;
}
pixels.checked_add(pixels / 2) }
VideoFormat::RGB => pixels.checked_mul(3),
VideoFormat::Gray => Some(pixels),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CameraConfig {
pub format: VideoFormat,
pub width: u32,
pub height: u32,
pub fps: u32,
pub fps_denominator: u32,
}
impl CameraConfig {
pub fn new(format: VideoFormat, width: u32, height: u32, fps: u32) -> Self {
Self {
format,
width,
height,
fps,
fps_denominator: 1,
}
}
pub fn default_mjpeg() -> Self {
Self::new(VideoFormat::MJPEG, 640, 480, 30)
}
pub fn hd_mjpeg() -> Self {
Self::new(VideoFormat::MJPEG, 1280, 720, 30)
}
pub fn full_hd_mjpeg() -> Self {
Self::new(VideoFormat::MJPEG, 1920, 1080, 30)
}
pub fn default_yuyv() -> Self {
Self::new(VideoFormat::YUYV, 640, 480, 30)
}
pub fn frame_size(&self) -> Option<usize> {
self.format.frame_data_size(self.width, self.height)
}
pub fn with_frame_rate(mut self, rate: crate::FrameRate) -> Self {
self.fps = rate.numerator();
self.fps_denominator = rate.denominator();
self
}
pub fn frame_rate(&self) -> CameraResult<crate::FrameRate> {
crate::FrameRate::new(self.fps, self.fps_denominator)
}
pub fn validate(&self) -> CameraResult<()> {
if self.width == 0 || self.height == 0 {
return Err(CameraError::invalid_config(
"Resolution cannot be 0".to_string(),
));
}
if self.fps == 0 || self.fps_denominator == 0 {
return Err(CameraError::invalid_config(
"Frame rate numerator and denominator must be positive".to_string(),
));
}
if !self.format.is_compressed() && self.frame_size().is_none() {
return Err(CameraError::invalid_config(format!(
"Resolution {}x{} cannot represent {:?} frame data",
self.width, self.height, self.format
)));
}
Ok(())
}
}
fn checked_pixel_count(width: u32, height: u32) -> Option<usize> {
if width == 0 || height == 0 {
return None;
}
let width = usize::try_from(width).ok()?;
let height = usize::try_from(height).ok()?;
width.checked_mul(height)
}
#[derive(Debug, Clone)]
pub struct CameraDeviceInfo {
pub index: u32,
pub name: String,
pub description: String,
pub vendor_id: Option<u16>,
pub product_id: Option<u16>,
pub serial_number: Option<String>,
pub device_path: Option<String>,
}
impl CameraDeviceInfo {
pub fn new(index: u32, name: String, description: String) -> Self {
Self {
index,
name,
description,
vendor_id: None,
product_id: None,
serial_number: None,
device_path: None,
}
}
pub fn unique_id(&self) -> String {
if let Some(path) = self.device_path.as_ref().filter(|p| !p.is_empty()) {
return path.clone();
}
if let (Some(vid), Some(pid)) = (self.vendor_id, self.product_id) {
if let Some(ref serial) = self.serial_number {
format!("{:04x}:{:04x}:{}", vid, pid, serial)
} else {
format!("{:04x}:{:04x}:{}", vid, pid, self.index)
}
} else {
format!("device:{}", self.index)
}
}
pub fn display_name(&self) -> &str {
&self.name
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CameraControlType {
Brightness,
Contrast,
Hue,
Saturation,
Sharpness,
Gamma,
WhiteBalance,
BacklightCompensation,
Gain,
Pan,
Tilt,
Zoom,
Exposure,
Iris,
Focus,
AutoExposure,
AutoFocus,
AutoWhiteBalance,
}
impl CameraControlType {
pub fn display_name(&self) -> &'static str {
match self {
Self::Brightness => "Brightness",
Self::Contrast => "Contrast",
Self::Hue => "Hue",
Self::Saturation => "Saturation",
Self::Sharpness => "Sharpness",
Self::Gamma => "Gamma",
Self::WhiteBalance => "White Balance",
Self::BacklightCompensation => "Backlight Compensation",
Self::Gain => "Gain",
Self::Pan => "Pan",
Self::Tilt => "Tilt",
Self::Zoom => "Zoom",
Self::Exposure => "Exposure",
Self::Iris => "Iris",
Self::Focus => "Focus",
Self::AutoExposure => "Auto Exposure",
Self::AutoFocus => "Auto Focus",
Self::AutoWhiteBalance => "Auto White Balance",
}
}
pub fn is_auto_control(&self) -> bool {
matches!(
self,
Self::AutoExposure | Self::AutoFocus | Self::AutoWhiteBalance
)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CameraControlValue {
Integer { value: i32, is_auto: bool },
Boolean(bool),
Float(f32),
}
impl CameraControlValue {
pub fn manual(value: i32) -> Self {
Self::Integer {
value,
is_auto: false,
}
}
pub fn auto(value: i32) -> Self {
Self::Integer {
value,
is_auto: true,
}
}
pub fn as_i32(&self) -> Option<i32> {
match self {
Self::Integer { value, .. } => Some(*value),
Self::Boolean(b) => Some(if *b { 1 } else { 0 }),
Self::Float(f) => Some(*f as i32),
}
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Boolean(b) => Some(*b),
Self::Integer { value, .. } => Some(*value != 0),
_ => None,
}
}
pub fn is_auto(&self) -> bool {
match self {
Self::Integer { is_auto, .. } => *is_auto,
_ => false,
}
}
}
impl From<bool> for CameraControlValue {
fn from(v: bool) -> Self {
Self::Boolean(v)
}
}
impl From<i32> for CameraControlValue {
fn from(v: i32) -> Self {
Self::Integer {
value: v,
is_auto: false,
}
}
}
impl From<f32> for CameraControlValue {
fn from(v: f32) -> Self {
Self::Float(v)
}
}
#[derive(Debug, Clone, Copy)]
pub struct CameraControlRange {
pub min: i32,
pub max: i32,
pub step: i32,
pub default: i32,
pub supports_auto: bool,
}
impl CameraControlRange {
pub fn new(min: i32, max: i32, step: i32, default: i32, supports_auto: bool) -> Self {
Self {
min,
max,
step,
default,
supports_auto,
}
}
pub fn is_in_range(&self, value: i32) -> bool {
let (min, max) = self.ordered_bounds();
value >= min && value <= max
}
pub fn clamp(&self, value: i32) -> i32 {
let (min, max) = self.ordered_bounds();
value.clamp(min, max)
}
fn ordered_bounds(&self) -> (i32, i32) {
if self.min <= self.max {
(self.min, self.max)
} else {
(self.max, self.min)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_video_format() {
assert!(VideoFormat::MJPEG.is_compressed());
assert!(!VideoFormat::YUYV.is_compressed());
assert_eq!(VideoFormat::YUYV.bytes_per_pixel(), Some(2));
assert_eq!(VideoFormat::RGB.bytes_per_pixel(), Some(3));
}
#[test]
fn test_camera_config() {
let config = CameraConfig::default_mjpeg();
assert_eq!(config.width, 640);
assert_eq!(config.height, 480);
assert_eq!(config.fps, 30);
assert!(config.validate().is_ok());
}
#[test]
fn test_config_validation() {
let mut config = CameraConfig::default_mjpeg();
config.width = 0;
assert!(config.validate().is_err());
config.width = 640;
config.fps = 0;
assert!(config.validate().is_err());
}
#[test]
fn test_frame_size() {
let config = CameraConfig::default_yuyv();
assert_eq!(config.frame_size(), Some(640 * 480 * 2));
let mjpeg_config = CameraConfig::default_mjpeg();
assert_eq!(mjpeg_config.frame_size(), None); }
#[test]
fn frame_size_returns_none_when_dimensions_overflow() {
assert_eq!(VideoFormat::RGB.frame_data_size(u32::MAX, u32::MAX), None);
let config = CameraConfig::new(VideoFormat::YUYV, u32::MAX, u32::MAX, 30);
assert_eq!(config.frame_size(), None);
}
#[test]
fn frame_size_returns_none_for_zero_dimensions() {
assert_eq!(VideoFormat::RGB.frame_data_size(0, 480), None);
assert_eq!(VideoFormat::Gray.frame_data_size(640, 0), None);
let config = CameraConfig::new(VideoFormat::YUYV, 0, 480, 30);
assert_eq!(config.frame_size(), None);
}
#[test]
fn packed_422_frame_size_rejects_odd_width() {
assert_eq!(VideoFormat::YUYV.frame_data_size(3, 2), None);
assert_eq!(VideoFormat::UYVY.frame_data_size(3, 2), None);
let config = CameraConfig::new(VideoFormat::YUYV, 3, 2, 30);
assert_eq!(config.frame_size(), None);
}
#[test]
fn nv12_frame_size_rejects_odd_dimensions() {
assert_eq!(VideoFormat::NV12.frame_data_size(3, 2), None);
assert_eq!(VideoFormat::NV12.frame_data_size(2, 3), None);
let config = CameraConfig::new(VideoFormat::NV12, 3, 2, 30);
assert_eq!(config.frame_size(), None);
}
#[test]
fn validate_rejects_unrepresentable_uncompressed_frame_sizes() {
let yuyv = CameraConfig::new(VideoFormat::YUYV, 3, 2, 30);
assert!(matches!(
yuyv.validate(),
Err(ref error) if error.kind() == crate::CameraErrorKind::InvalidArgument
));
let nv12 = CameraConfig::new(VideoFormat::NV12, 2, 3, 30);
assert!(matches!(
nv12.validate(),
Err(ref error) if error.kind() == crate::CameraErrorKind::InvalidArgument
));
let mjpeg = CameraConfig::new(VideoFormat::MJPEG, 3, 3, 30);
assert!(mjpeg.validate().is_ok());
}
#[test]
fn camera_control_range_handles_reversed_bounds_without_panicking() {
let range = CameraControlRange::new(100, 0, 1, 50, false);
assert!(range.is_in_range(50));
assert_eq!(range.clamp(-10), 0);
assert_eq!(range.clamp(110), 100);
}
}