tbf/TBF/Rig/Network/Camera/RoiForFixedStartCJMS11/common/Frame.cs
Michal Buzik 0daefb93bb Add CJMS11 enhancements, logging, and resolution support
- Extended CJMS11 camera handling with new functionalities, including image grabbing, resolution configuration, and improved ROI support.
- Added new `ToString` method implementations for enhanced debugging and logging in key classes like `JmsMessage`, `JmsPacket`, and `RoiCfg`.
- Introduced additional commands (`start_stream_images`, `stop_stream_images`) in `CommandM` and its enum.
- Implemented new resolution handling in `RoiCfg` with support for configurable image frames.
- Refactored resolution-related UI elements in `RoiCfgCtrl` to add a dropdown for selecting resolution.
- Made minor UX improvements by replacing inconsistent string formats with verbatim strings in console messages.
- Updated project dependencies with new resolution-related utilities (`Frame` and `ResolutionFrames`).
- Resolved camera-specific symbolic issues by transitioning to `CJMS11.Camera` over prior naming inconsistencies.
2025-07-18 08:50:22 +02:00

70 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace TBF.Rig.Network.Camera.RoiForFixedStartCJMS11.common
{
public class Frame
{
public int Width { get; set; }
public int Height { get; set; }
public Frame() // standard frame
{
this.Width = 640;
this.Height = 480;
}
public Frame(int width, int height)
{
Width = width;
Height = height;
}
public static Frame StandardFrame()
{
return new Frame();
}
public override string ToString() => $"{Width}x{Height}";
// Static method to parse string like "1920x1080"
public static Frame Parse(string resolution)
{
var match = Regex.Match(resolution, @"^\s*(\d+)\s*[xX]\s*(\d+)\s*$");
if (!match.Success)
{
throw new FormatException($"Invalid resolution format: '{resolution}'");
}
int width = int.Parse(match.Groups[1].Value);
int height = int.Parse(match.Groups[2].Value);
return new Frame(width, height);
}
///
/// Finds nearest frame based on selected dimension
/// <b> example: </b>
/// <code>
/// var input = new Frame(1500, 800);
/// //Nearest by Width
/// var nearestByWidth = input.FindNearest(allFrames, f => f.Width);
/// Console.WriteLine($"Nearest by width: {nearestByWidth}"); // → 1280x720
/// //Nearest by Height
/// var nearestByHeight = input.FindNearest(allFrames, f => f.Height);
/// Console.WriteLine($"Nearest by height: {nearestByHeight}"); // → 1280x720
/// </code>
public Frame FindNearest(IEnumerable<Frame> candidates, Func<Frame, int> dimensionSelector)
{
int target = dimensionSelector(this);
return candidates
.OrderBy(f => Math.Abs(dimensionSelector(f) - target))
.ThenBy(dimensionSelector) // tie-breaker: prefer smaller
.FirstOrDefault();
}
}
}