Calculates a score for format selection based on configuration preferences The scoring algorithm prioritizes: 1. Exact resolution match (if configured) 2. Supported pixel format (if configured) 3. Closest resolution match (as fallback) Uses a single-pass algorithm to avoid duplicate iterations of configuration vectors. # Arguments `config` - Camera configuration with preferences and priorities
(config: &CameraConfig, w: u32, h: u32, fmt: PixelFormat)
| 393 | /// # Returns |
| 394 | /// An integer score where higher values indicate better matches |
| 395 | fn calculate_format_score(config: &CameraConfig, w: u32, h: u32, fmt: PixelFormat) -> i32 { |
| 396 | // Fast path: Check for exact resolution match while calculating distance in one pass |
| 397 | let mut resolution_score = 0i32; |
| 398 | let mut min_distance = i32::MAX; |
| 399 | |
| 400 | for (req_w, req_h, prio) in &config.resolution_req { |
| 401 | if w == *req_w && h == *req_h { |
| 402 | // Exact match found - highest priority |
| 403 | resolution_score = *prio as i32 * 10; |
| 404 | min_distance = 0; // Signal that exact match was found |
| 405 | break; |
| 406 | } else { |
| 407 | // Track closest resolution during iteration |
| 408 | let w_diff = (w as i32 - *req_w as i32).abs(); |
| 409 | let h_diff = (h as i32 - *req_h as i32).abs(); |
| 410 | let distance = w_diff + h_diff; |
| 411 | if distance < min_distance { |
| 412 | min_distance = distance; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // Score for supported format (multiplied by 10 for emphasis) |
| 418 | let format_score = config |
| 419 | .format_req |
| 420 | .iter() |
| 421 | .find(|(req_fmt, _)| fmt == *req_fmt) |
| 422 | .map_or(0, |(_, prio)| *prio as i32 * 10); |
| 423 | |
| 424 | // Resolution distance penalty for non-exact matches |
| 425 | let resolution_distance = if resolution_score > 0 { |
| 426 | // Exact match found - no distance penalty |
| 427 | 0 |
| 428 | } else if min_distance < i32::MAX { |
| 429 | // Approximate match - penalize by distance |
| 430 | -min_distance |
| 431 | } else if !config.resolution_req.is_empty() { |
| 432 | // Required formats specified but none found - large penalty |
| 433 | -1000 |
| 434 | } else { |
| 435 | // No resolution requirements specified |
| 436 | 0 |
| 437 | }; |
| 438 | |
| 439 | // Combined score: exact matches have priority, then format compatibility, |
| 440 | // then closest resolution match |
| 441 | resolution_score + format_score + resolution_distance |
| 442 | } |
| 443 | |
| 444 | /// Represents a negotiated video format between application requirements and device capabilities. |
| 445 | /// |
no test coverage detected