maverick_os 0.1.10

Maverick OS
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use image::RgbaImage;

#[cfg(any(target_os = "ios", target_os = "macos"))]
mod apple;

#[cfg(any(target_os = "ios", target_os = "macos"))]
mod apple_custom_image;

#[cfg(any(target_os = "ios", target_os = "macos"))]
mod apple_custom_utils;

#[cfg(target_os = "android")]
mod android;

#[cfg(any(target_os = "macos", target_os = "ios"))]
use crate::hardware::camera::apple::AppleCamera;

#[cfg(any(target_os = "macos", target_os = "ios"))]
use crate::hardware::camera::apple_custom_image::AppleCustomCamera;
#[cfg(any(target_os = "ios", target_os = "macos"))]
pub use crate::hardware::camera::apple_custom_utils::ImageSettings;

#[cfg(target_os = "android")]
use crate::hardware::camera::android::AndroidCamera;

/// Errors that can occur when interacting with the camera.
#[derive(Debug)]
pub enum CameraError {
    AccessDenied,
    WaitingForAccess,
    FailedToGetFrame,
    FailedToOpenCamera,
}

#[derive(Debug, Clone)]
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub enum AppleCameraBackend {
    Standard(AppleCamera),
    Custom(AppleCustomCamera),
}

/// Access the device camera.
#[derive(Debug, Clone)]
pub struct Camera(
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    AppleCameraBackend,
    
    #[cfg(target_os = "android")]
    AndroidCamera,
);

impl Camera {
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn new() -> Result<Self, CameraError> {
        // println!("Creating standard Apple camera");
        let camera = AppleCamera::new();
        camera.open_camera();
        Ok(Camera(AppleCameraBackend::Standard(camera)))
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn new_unprocessed() -> Result<Self, CameraError> {
        // println!("Creating custom Apple camera");
        let mut camera = AppleCustomCamera::new();
        camera.open_camera().map_err(|_e| {
            // println!("Failed to open custom camera: {}", e);
            CameraError::FailedToOpenCamera
        })?;
        // println!("Custom camera opened successfully");
        Ok(Camera(AppleCameraBackend::Custom(camera)))
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios")))]
    pub fn new_unprocessed() -> Result<Self, CameraError> {
        Self::new()
    }

    #[cfg(target_os = "android")]
    pub fn new() -> Result<Self, CameraError> {
        let mut camera = AndroidCamera::new().map_err(|_| CameraError::AccessDenied)?;
        camera.open_camera();
        Ok(Camera(camera))
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn new() -> Result<Self, CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn get_frame(&mut self) -> Option<RgbaImage> {
        match &mut self.0 {
            AppleCameraBackend::Standard(_cam) => {
                // println!("Standard camera not supported for frame output");
                None
            }
            AppleCameraBackend::Custom(cam) => {
                // println!("Getting frame from custom camera");
                cam.get_latest_raw_frame()
            }
        }
    }

    #[cfg(target_os = "android")]
    pub fn get_frame(&mut self) -> Result<(Vec<u8>, usize, usize), CameraError> {
        self.0.get_latest_frame().map_err(|_| CameraError::FailedToGetFrame)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn get_frame(&mut self) -> Option<RgbaImage> {
        None
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn get_latest_raw_frame(&self) -> Option<RgbaImage> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                None
            }
            AppleCameraBackend::Custom(cam) => {
                cam.get_latest_raw_frame()
            }
        }
    }

    #[cfg(target_os = "android")]
    pub fn get_latest_raw_frame(&self) -> Option<RgbaImage> {
        None
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn get_latest_raw_frame(&self) -> Option<RgbaImage> {
        None
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn update_settings<F>(&self, update_fn: F) -> Result<(), CameraError>
    where F: FnOnce(&mut ImageSettings),
    {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                // Standard camera doesn't support settings updates
                Err(CameraError::FailedToGetFrame) // Reusing error type, could add a new one
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(update_fn);
                Ok(())
            }
        }
    }

    // Individual setter methods for all image processing parameters
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn set_brightness(&mut self, brightness: i16) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    settings.brightness = brightness;
                });
                Ok(())
            }
        }
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn set_contrast(&mut self, contrast: f32) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    settings.contrast = contrast;
                });
                Ok(())
            }
        }
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn set_saturation(&mut self, saturation: f32) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    settings.saturation = saturation;
                });
                Ok(())
            }
        }
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn set_gamma(&mut self, gamma: f32) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    settings.gamma = gamma;
                });
                Ok(())
            }
        }
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn set_exposure(&mut self, exposure: f32) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    settings.exposure = exposure;
                });
                Ok(())
            }
        }
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn set_temperature(&mut self, temperature: f32) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    settings.temperature = temperature;
                });
                Ok(())
            }
        }
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn set_white_balance_r(&mut self, white_balance_r: f32) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    settings.white_balance_r = white_balance_r;
                });
                Ok(())
            }
        }
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn set_white_balance_g(&mut self, white_balance_g: f32) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    settings.white_balance_g = white_balance_g;
                });
                Ok(())
            }
        }
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn set_white_balance_b(&mut self, white_balance_b: f32) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    settings.white_balance_b = white_balance_b;
                });
                Ok(())
            }
        }
    }

    /// Set white balance using RGB multipliers all at once
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn set_white_balance_rgb(&mut self, r: f32, g: f32, b: f32) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    settings.white_balance_r = r;
                    settings.white_balance_g = g;
                    settings.white_balance_b = b;
                });
                Ok(())
            }
        }
    }

    /// Reset all settings to default values
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn reset_settings(&mut self) -> Result<(), CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                cam.update_settings(|settings| {
                    *settings = ImageSettings::default();
                });
                Ok(())
            }
        }
    }

    // Stub implementations for Android
    #[cfg(target_os = "android")]
    pub fn set_brightness(&mut self, _brightness: i16) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn set_contrast(&mut self, _contrast: f32) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn set_saturation(&mut self, _saturation: f32) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn set_gamma(&mut self, _gamma: f32) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn set_exposure(&mut self, _exposure: f32) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn set_temperature(&mut self, _temperature: f32) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn set_white_balance_r(&mut self, _white_balance_r: f32) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn set_white_balance_g(&mut self, _white_balance_g: f32) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn set_white_balance_b(&mut self, _white_balance_b: f32) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn set_white_balance_rgb(&mut self, _r: f32, _g: f32, _b: f32) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn reset_settings(&mut self) -> Result<(), CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    // Stub implementations for unsupported platforms
    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn set_brightness(&mut self, _brightness: i16) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn set_contrast(&mut self, _contrast: f32) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn set_saturation(&mut self, _saturation: f32) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn set_gamma(&mut self, _gamma: f32) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn set_exposure(&mut self, _exposure: f32) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn set_temperature(&mut self, _temperature: f32) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn set_white_balance_r(&mut self, _white_balance_r: f32) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn set_white_balance_g(&mut self, _white_balance_g: f32) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn set_white_balance_b(&mut self, _white_balance_b: f32) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn set_white_balance_rgb(&mut self, _r: f32, _g: f32, _b: f32) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn reset_settings(&mut self) -> Result<(), CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn get_settings(&self) -> Result<ImageSettings, CameraError> {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                Err(CameraError::FailedToGetFrame)
            }
            AppleCameraBackend::Custom(cam) => {
                Ok(cam.get_settings())
            }
        }
    }

    #[cfg(target_os = "android")]
    pub fn update_settings<F>(&self, _update_fn: F) -> Result<(), CameraError>
    where F: FnOnce(&mut ImageSettings),
    {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(target_os = "android")]
    pub fn get_settings(&self) -> Result<ImageSettings, CameraError> {
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn update_settings<F>(&self, _update_fn: F) -> Result<(), CameraError>
    where F: FnOnce(&mut ImageSettings),
    {
        Err(CameraError::AccessDenied)
    }

    /// Get current camera image processing settings
    /// Not available for unsupported platforms
    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn get_settings(&self) -> Result<ImageSettings, CameraError> {
        Err(CameraError::AccessDenied)
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn open_and_get_frame() -> Result<(Vec<u8>, usize, usize), CameraError> {
        println!("Opening standard camera and getting frame (not supported)");
        Err(CameraError::FailedToGetFrame)
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn open_and_get_custom_frame() -> Option<RgbaImage> {
        // println!("Opening custom camera and getting frame");
        let mut camera = AppleCustomCamera::new();
        if let Err(_e) = camera.open_camera() {
            // println!("Failed to open custom camera: {}", e);
            return None;
        }

        let mut wrapper = Camera(AppleCameraBackend::Custom(camera));
        // println!("Waiting for custom camera to capture first frame...");
        for _ in 1..=10 {
            std::thread::sleep(std::time::Duration::from_millis(200));
            // println!("Attempt {} to get frame", attempt);
            if let Some(frame) = wrapper.get_frame() {
                // println!("Successfully got frame on attempt {}", attempt);
                return Some(frame);
            }
        }
        // println!("Failed to get frame after 10 attempts");
        None
    }

    #[cfg(target_os = "android")]
    pub fn open_and_get_frame() -> Result<(Vec<u8>, usize, usize), CameraError> {
        let mut camera = AndroidCamera::new().map_err(|_| CameraError::AccessDenied)?;
        camera.open_camera();
        let mut wrapper = Camera(camera);
        wrapper.get_frame()
    }

    #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
    pub fn open_and_get_frame() -> Option<RgbaImage> {
        None
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub fn stop_camera(&self) {
        match &self.0 {
            AppleCameraBackend::Standard(_cam) => {
                // println!("Stopping standard camera");
            }
            AppleCameraBackend::Custom(cam) => {
                // println!("Stopping custom camera");
                cam.stop_camera();
            }
        }
    }
}

impl Default for Camera {
    fn default() -> Self {
        Self::new().unwrap_or_else(|_| panic!("Failed to create default camera"))
    }
}

impl Drop for Camera {
    fn drop(&mut self) {
        #[cfg(any(target_os = "macos", target_os = "ios"))]
        self.stop_camera();
    }
}