crabcamera 0.8.3

Advanced cross-platform camera integration for Tauri applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Platform enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Platform {
    Windows,
    MacOS,
    Linux,
    Unknown,
}

impl Platform {
    /// Detect current platform
    pub fn current() -> Self {
        if cfg!(target_os = "windows") {
            Platform::Windows
        } else if cfg!(target_os = "macos") {
            Platform::MacOS
        } else if cfg!(target_os = "linux") {
            Platform::Linux
        } else {
            Platform::Unknown
        }
    }

    /// Get platform as string
    pub fn as_str(&self) -> &'static str {
        match self {
            Platform::Windows => "windows",
            Platform::MacOS => "macos",
            Platform::Linux => "linux",
            Platform::Unknown => "unknown",
        }
    }
}

/// Camera device information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CameraDeviceInfo {
    pub id: String,
    pub name: String,
    pub description: Option<String>,
    pub is_available: bool,
    pub supports_formats: Vec<CameraFormat>,
    pub platform: Platform,
}

impl CameraDeviceInfo {
    /// Create new camera device info
    pub fn new(id: String, name: String) -> Self {
        Self {
            id,
            name,
            description: None,
            is_available: true,
            supports_formats: Vec::new(),
            platform: Platform::current(),
        }
    }

    /// Set description
    pub fn with_description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }

    /// Set supported formats
    pub fn with_formats(mut self, formats: Vec<CameraFormat>) -> Self {
        self.supports_formats = formats;
        self
    }

    /// Set availability
    pub fn with_availability(mut self, available: bool) -> Self {
        self.is_available = available;
        self
    }
}

/// Camera format specification
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CameraFormat {
    pub width: u32,
    pub height: u32,
    pub fps: f32,
    pub format_type: String,
}

impl CameraFormat {
    /// Create new camera format
    pub fn new(width: u32, height: u32, fps: f32) -> Self {
        Self {
            width,
            height,
            fps,
            format_type: "RGB8".to_string(),
        }
    }

    /// Create high resolution format
    pub fn hd() -> Self {
        Self::new(1920, 1080, 30.0)
    }

    /// Create standard resolution format
    pub fn standard() -> Self {
        Self::new(1280, 720, 30.0)
    }

    /// Create low resolution format
    pub fn low() -> Self {
        Self::new(640, 480, 30.0)
    }

    /// Set format type
    pub fn with_format_type(mut self, format_type: String) -> Self {
        self.format_type = format_type;
        self
    }
}

/// Camera frame data with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CameraFrame {
    pub id: String,
    pub data: Vec<u8>,
    pub width: u32,
    pub height: u32,
    pub format: String,
    pub timestamp: DateTime<Utc>,
    pub device_id: String,
    pub size_bytes: usize,
    pub metadata: FrameMetadata,
}

impl CameraFrame {
    /// Create new camera frame
    pub fn new(data: Vec<u8>, width: u32, height: u32, device_id: String) -> Self {
        let size_bytes = data.len();
        Self {
            id: Uuid::new_v4().to_string(),
            data,
            width,
            height,
            format: "RGB8".to_string(),
            timestamp: Utc::now(),
            device_id,
            size_bytes,
            metadata: FrameMetadata::default(),
        }
    }

    /// Set format
    pub fn with_format(mut self, format: String) -> Self {
        self.format = format;
        self
    }

    /// Get frame aspect ratio
    pub fn aspect_ratio(&self) -> f32 {
        self.width as f32 / self.height as f32
    }

    /// Check if frame is valid
    pub fn is_valid(&self) -> bool {
        !self.data.is_empty() && self.width > 0 && self.height > 0
    }
}

/// Advanced camera controls for professional photography
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CameraControls {
    pub auto_focus: Option<bool>,
    pub focus_distance: Option<f32>, // 0.0 = infinity, 1.0 = closest
    pub auto_exposure: Option<bool>,
    pub exposure_time: Option<f32>,   // Seconds
    pub iso_sensitivity: Option<u32>, // ISO value
    pub white_balance: Option<WhiteBalance>,
    pub aperture: Option<f32>,   // f-stop value
    pub zoom: Option<f32>,       // Digital zoom factor
    pub brightness: Option<f32>, // -1.0 to 1.0
    pub contrast: Option<f32>,   // -1.0 to 1.0
    pub saturation: Option<f32>, // -1.0 to 1.0
    pub sharpness: Option<f32>,  // -1.0 to 1.0
    pub noise_reduction: Option<bool>,
    pub image_stabilization: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum WhiteBalance {
    Auto,
    Daylight,
    Fluorescent,
    Incandescent,
    Flash,
    Cloudy,
    Shade,
    Custom(u32), // Color temperature in Kelvin
}

impl Default for CameraControls {
    fn default() -> Self {
        Self {
            auto_focus: Some(true),
            focus_distance: None,
            auto_exposure: Some(true),
            exposure_time: None,
            iso_sensitivity: Some(400),
            white_balance: Some(WhiteBalance::Auto),
            aperture: None,
            zoom: Some(1.0),
            brightness: Some(0.0),
            contrast: Some(0.0),
            saturation: Some(0.0),
            sharpness: Some(0.0),
            noise_reduction: Some(true),
            image_stabilization: Some(true),
        }
    }
}

impl CameraControls {
    pub fn professional() -> Self {
        Self {
            auto_focus: Some(false),
            focus_distance: Some(0.5),
            auto_exposure: Some(false),
            exposure_time: Some(1.0 / 60.0),
            iso_sensitivity: Some(100),
            white_balance: Some(WhiteBalance::Daylight),
            aperture: Some(8.0),
            zoom: Some(1.0),
            brightness: Some(0.0),
            contrast: Some(0.3),
            saturation: Some(0.4),
            sharpness: Some(0.5),
            noise_reduction: Some(true),
            image_stabilization: Some(true),
        }
    }
}

/// Burst capture configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BurstConfig {
    pub count: u32,       // Number of photos
    pub interval_ms: u32, // Time between shots
    pub bracketing: Option<ExposureBracketing>,
    pub focus_stacking: bool, // Vary focus for each shot
    pub auto_save: bool,      // Automatically save all frames
    pub save_directory: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExposureBracketing {
    pub stops: Vec<f32>,    // EV adjustments: [-2.0, 0.0, +2.0]
    pub base_exposure: f32, // Base exposure time in seconds
}

impl BurstConfig {
    pub fn hdr_burst() -> Self {
        Self {
            count: 3,
            interval_ms: 200,
            bracketing: Some(ExposureBracketing {
                stops: vec![-1.0, 0.0, 1.0],
                base_exposure: 1.0 / 125.0,
            }),
            focus_stacking: false,
            auto_save: true,
            save_directory: Some("hdr_captures".to_string()),
        }
    }
}

/// Camera hardware capabilities
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CameraCapabilities {
    pub supports_auto_focus: bool,
    pub supports_manual_focus: bool,
    pub supports_auto_exposure: bool,
    pub supports_manual_exposure: bool,
    pub supports_white_balance: bool,
    pub supports_zoom: bool,
    pub supports_flash: bool,
    pub supports_burst_mode: bool,
    pub supports_hdr: bool,
    pub max_resolution: (u32, u32),
    pub max_fps: f32,
    pub exposure_range: Option<(f32, f32)>, // min, max exposure time
    pub iso_range: Option<(u32, u32)>,      // min, max ISO
    pub focus_range: Option<(f32, f32)>,    // min, max focus distance
}

impl Default for CameraCapabilities {
    fn default() -> Self {
        Self {
            supports_auto_focus: true,
            supports_manual_focus: false,
            supports_auto_exposure: true,
            supports_manual_exposure: false,
            supports_white_balance: true,
            supports_zoom: false,
            supports_flash: false,
            supports_burst_mode: true,
            supports_hdr: false,
            max_resolution: (1920, 1080),
            max_fps: 30.0,
            exposure_range: None,
            iso_range: None,
            focus_range: None,
        }
    }
}

/// Extended metadata for camera frames
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct FrameMetadata {
    pub exposure_time: Option<f32>,
    pub iso_sensitivity: Option<u32>,
    pub white_balance: Option<WhiteBalance>,
    pub focus_distance: Option<f32>,
    pub aperture: Option<f32>,
    pub flash_fired: Option<bool>,
    pub scene_mode: Option<String>,
    pub capture_settings: Option<CameraControls>,
}

/// Performance metrics for camera operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CameraPerformanceMetrics {
    pub capture_latency_ms: f32,
    pub processing_time_ms: f32,
    pub memory_usage_mb: f32,
    pub fps_actual: f32,
    pub dropped_frames: u32,
    pub buffer_overruns: u32,
    pub quality_score: f32,
}

impl Default for CameraPerformanceMetrics {
    fn default() -> Self {
        Self {
            capture_latency_ms: 0.0,
            processing_time_ms: 0.0,
            memory_usage_mb: 0.0,
            fps_actual: 0.0,
            dropped_frames: 0,
            buffer_overruns: 0,
            quality_score: 0.0,
        }
    }
}

/// Camera initialization parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CameraInitParams {
    pub device_id: String,
    pub format: CameraFormat,
    pub controls: CameraControls,
}

impl CameraInitParams {
    /// Create new initialization parameters
    pub fn new(device_id: String) -> Self {
        Self {
            device_id,
            format: CameraFormat::standard(),
            controls: CameraControls::default(),
        }
    }

    /// Set desired format
    pub fn with_format(mut self, format: CameraFormat) -> Self {
        self.format = format;
        self
    }

    /// Set camera controls
    pub fn with_controls(mut self, controls: CameraControls) -> Self {
        self.controls = controls;
        self
    }

    /// Enable/disable auto focus
    pub fn with_auto_focus(mut self, enabled: bool) -> Self {
        self.controls.auto_focus = Some(enabled);
        self
    }

    /// Enable/disable auto exposure  
    pub fn with_auto_exposure(mut self, enabled: bool) -> Self {
        self.controls.auto_exposure = Some(enabled);
        self
    }

    /// Create parameters optimized for professional photography
    pub fn professional(device_id: String) -> Self {
        Self {
            device_id,
            format: CameraFormat::new(2592, 1944, 15.0), // 5MP high quality
            controls: CameraControls::professional(),
        }
    }
}