screencapturekit 3.0.1

Safe Rust bindings for Apple's ScreenCaptureKit framework - screen and audio capture on macOS
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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
//! `SCScreenshotManager` - Single-shot screenshot capture
//!
//! Available on macOS 14.0+.
//! Provides high-quality screenshot capture without the overhead of setting up a stream.
//!
//! ## When to Use
//!
//! Use `SCScreenshotManager` when you need:
//! - A single screenshot rather than continuous capture
//! - Quick capture without stream setup/teardown overhead
//! - Direct saving to image files
//!
//! For continuous capture, use [`SCStream`](crate::stream::SCStream) instead.
//!
//! ## Example
//!
//! ```no_run
//! use screencapturekit::screenshot_manager::{SCScreenshotManager, ImageFormat};
//! use screencapturekit::stream::{content_filter::SCContentFilter, configuration::SCStreamConfiguration};
//! use screencapturekit::shareable_content::SCShareableContent;
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let content = SCShareableContent::get()?;
//! let display = &content.displays()[0];
//! let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
//! let config = SCStreamConfiguration::new()
//!     .with_width(1920)
//!     .with_height(1080);
//!
//! // Capture as CGImage
//! let image = SCScreenshotManager::capture_image(&filter, &config)?;
//! println!("Screenshot: {}x{}", image.width(), image.height());
//!
//! // Save to file
//! image.save_png("/tmp/screenshot.png")?;
//!
//! // Or save as JPEG with quality
//! image.save("/tmp/screenshot.jpg", ImageFormat::Jpeg(0.85))?;
//! # Ok(())
//! # }
//! ```

use crate::error::SCError;
use crate::stream::configuration::SCStreamConfiguration;
use crate::stream::content_filter::SCContentFilter;
use crate::utils::completion::{error_from_cstr, SyncCompletion};
use std::ffi::c_void;

#[cfg(feature = "macos_15_2")]
use crate::cg::CGRect;

/// Image output format for saving screenshots
///
/// # Examples
///
/// ```no_run
/// use screencapturekit::screenshot_manager::ImageFormat;
///
/// // PNG for lossless quality
/// let format = ImageFormat::Png;
///
/// // JPEG with 80% quality
/// let format = ImageFormat::Jpeg(0.8);
///
/// // HEIC with 90% quality (smaller file size than JPEG)
/// let format = ImageFormat::Heic(0.9);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ImageFormat {
    /// PNG format (lossless)
    Png,
    /// JPEG format with quality (0.0-1.0)
    Jpeg(f32),
    /// TIFF format (lossless)
    Tiff,
    /// GIF format
    Gif,
    /// BMP format
    Bmp,
    /// HEIC format with quality (0.0-1.0) - efficient compression
    Heic(f32),
}

impl ImageFormat {
    fn to_format_id(self) -> i32 {
        match self {
            Self::Png => 0,
            Self::Jpeg(_) => 1,
            Self::Tiff => 2,
            Self::Gif => 3,
            Self::Bmp => 4,
            Self::Heic(_) => 5,
        }
    }

    fn quality(self) -> f32 {
        match self {
            Self::Jpeg(q) | Self::Heic(q) => q.clamp(0.0, 1.0),
            _ => 1.0,
        }
    }

    /// Get the typical file extension for this format
    #[must_use]
    pub const fn extension(&self) -> &'static str {
        match self {
            Self::Png => "png",
            Self::Jpeg(_) => "jpg",
            Self::Tiff => "tiff",
            Self::Gif => "gif",
            Self::Bmp => "bmp",
            Self::Heic(_) => "heic",
        }
    }
}

extern "C" fn image_callback(
    image_ptr: *const c_void,
    error_ptr: *const i8,
    user_data: *mut c_void,
) {
    if !error_ptr.is_null() {
        let error = unsafe { error_from_cstr(error_ptr) };
        unsafe { SyncCompletion::<CGImage>::complete_err(user_data, error) };
    } else if !image_ptr.is_null() {
        unsafe { SyncCompletion::complete_ok(user_data, CGImage::from_ptr(image_ptr)) };
    } else {
        unsafe { SyncCompletion::<CGImage>::complete_err(user_data, "Unknown error".to_string()) };
    }
}

extern "C" fn buffer_callback(
    buffer_ptr: *const c_void,
    error_ptr: *const i8,
    user_data: *mut c_void,
) {
    if !error_ptr.is_null() {
        let error = unsafe { error_from_cstr(error_ptr) };
        unsafe { SyncCompletion::<crate::cm::CMSampleBuffer>::complete_err(user_data, error) };
    } else if !buffer_ptr.is_null() {
        let buffer = unsafe { crate::cm::CMSampleBuffer::from_ptr(buffer_ptr.cast_mut()) };
        unsafe { SyncCompletion::complete_ok(user_data, buffer) };
    } else {
        unsafe {
            SyncCompletion::<crate::cm::CMSampleBuffer>::complete_err(
                user_data,
                "Unknown error".to_string(),
            );
        };
    }
}

#[cfg(feature = "macos_26_0")]
extern "C" fn screenshot_output_callback(
    output_ptr: *const c_void,
    error_ptr: *const i8,
    user_data: *mut c_void,
) {
    if !error_ptr.is_null() {
        let error = unsafe { error_from_cstr(error_ptr) };
        unsafe { SyncCompletion::<SCScreenshotOutput>::complete_err(user_data, error) };
    } else if !output_ptr.is_null() {
        unsafe {
            SyncCompletion::complete_ok(user_data, SCScreenshotOutput::from_ptr(output_ptr));
        };
    } else {
        unsafe {
            SyncCompletion::<SCScreenshotOutput>::complete_err(
                user_data,
                "Unknown error".to_string(),
            );
        };
    }
}

/// `CGImage` wrapper for screenshots
///
/// Represents a Core Graphics image returned from screenshot capture.
///
/// # Examples
///
/// ```no_run
/// # use screencapturekit::screenshot_manager::SCScreenshotManager;
/// # use screencapturekit::stream::{content_filter::SCContentFilter, configuration::SCStreamConfiguration};
/// # use screencapturekit::shareable_content::SCShareableContent;
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let content = SCShareableContent::get()?;
/// let display = &content.displays()[0];
/// let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
/// let config = SCStreamConfiguration::new()
///     .with_width(1920)
///     .with_height(1080);
///
/// let image = SCScreenshotManager::capture_image(&filter, &config)?;
/// println!("Screenshot size: {}x{}", image.width(), image.height());
/// # Ok(())
/// # }
/// ```
pub struct CGImage {
    ptr: *const c_void,
}

/// Internal selector for the channel ordering passed to the Swift renderer.
#[derive(Debug, Clone, Copy)]
enum PixelLayout {
    Rgba,
    Bgra,
}

impl PixelLayout {
    const fn name(self) -> &'static str {
        match self {
            Self::Rgba => "RGBA",
            Self::Bgra => "BGRA",
        }
    }

    /// Dispatch into the matching Swift bridge entry point.
    ///
    /// # Safety
    /// The destination must point to at least `capacity` bytes and `ptr` must
    /// be a live retained `CGImage`.
    unsafe fn render(self, ptr: *const c_void, dest: *mut u8, capacity: usize) -> usize {
        match self {
            Self::Rgba => crate::ffi::cgimage_render_rgba_into(ptr, dest, capacity),
            Self::Bgra => crate::ffi::cgimage_render_bgra_into(ptr, dest, capacity),
        }
    }
}

impl CGImage {
    pub(crate) fn from_ptr(ptr: *const c_void) -> Self {
        Self { ptr }
    }

    /// Get image width in pixels
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use screencapturekit::screenshot_manager::SCScreenshotManager;
    /// # use screencapturekit::stream::{content_filter::SCContentFilter, configuration::SCStreamConfiguration};
    /// # use screencapturekit::shareable_content::SCShareableContent;
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let content = SCShareableContent::get()?;
    /// # let display = &content.displays()[0];
    /// # let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
    /// # let config = SCStreamConfiguration::new().with_width(1920).with_height(1080);
    /// let image = SCScreenshotManager::capture_image(&filter, &config)?;
    /// let width = image.width();
    /// println!("Width: {}", width);
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn width(&self) -> usize {
        unsafe { crate::ffi::cgimage_get_width(self.ptr) }
    }

    /// Get image height in pixels
    #[must_use]
    pub fn height(&self) -> usize {
        unsafe { crate::ffi::cgimage_get_height(self.ptr) }
    }

    #[must_use]
    pub fn as_ptr(&self) -> *const c_void {
        self.ptr
    }

    /// Get raw RGBA pixel data
    ///
    /// Returns a vector containing RGBA bytes (4 bytes per pixel).
    /// The data is in row-major order.
    ///
    /// **Performance note:** every `ScreenCaptureKit`-produced `CGImage` is
    /// natively in **BGRA**. Forcing RGBA here makes `CGContext.draw` perform
    /// a per-pixel channel swap that costs ~20 ms on a 4K image. If your
    /// consumer accepts BGRA (Metal / wgpu / ffmpeg / most GPU pipelines),
    /// prefer [`bgra_data`](Self::bgra_data) which skips the conversion.
    ///
    /// **Allocation note:** this allocates a fresh `Vec<u8>` of
    /// `width*height*4` bytes per call (~33 MB for 4K). For sustained
    /// screenshot loops, prefer [`rgba_data_into`](Self::rgba_data_into)
    /// which writes into a caller-supplied buffer and lets you reuse the
    /// allocation across calls.
    ///
    /// # Errors
    /// Returns an error if the pixel data cannot be extracted
    pub fn rgba_data(&self) -> Result<Vec<u8>, SCError> {
        self.render_pixel_data(PixelLayout::Rgba)
    }

    /// Get raw **BGRA** pixel data — the native `ScreenCaptureKit` pixel layout.
    ///
    /// Returns a vector containing BGRA bytes (4 bytes per pixel) in row-major
    /// order. Each pixel is stored as `[B, G, R, A]`.
    ///
    /// This skips the BGRA → RGBA channel-swap that [`rgba_data`](Self::rgba_data)
    /// performs inside `CGContext.draw`, saving roughly **20 ms on a 4K screenshot**.
    /// Use this when the downstream consumer accepts BGRA natively — that
    /// includes Metal (`MTLPixelFormat::BGRA8Unorm`), wgpu (`Bgra8Unorm`),
    /// ffmpeg (`AV_PIX_FMT_BGRA`), and any direct upload to a `kCVPixelFormatType_32BGRA`
    /// pixel buffer.
    ///
    /// For sustained capture loops, see [`bgra_data_into`](Self::bgra_data_into)
    /// which writes into a caller-supplied buffer.
    ///
    /// # Errors
    /// Returns an error if the pixel data cannot be extracted.
    pub fn bgra_data(&self) -> Result<Vec<u8>, SCError> {
        self.render_pixel_data(PixelLayout::Bgra)
    }

    /// Render the image's RGBA bytes into a caller-supplied buffer.
    ///
    /// `dest` must hold at least `width * height * 4` bytes. Returns the
    /// number of bytes written on success. Use this for sustained screenshot
    /// loops to amortise the per-call ~33 MB-at-4K allocation across many
    /// frames — pre-allocate one `Vec<u8>::with_capacity(...)` (or set
    /// `dest.len() = capacity` once after the first call) and reuse it.
    ///
    /// # Errors
    /// Returns `SCError::InternalError` if `dest` is too small or the
    /// `CGContext` draw fails.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use screencapturekit::screenshot_manager::SCScreenshotManager;
    /// # use screencapturekit::stream::{content_filter::SCContentFilter, configuration::SCStreamConfiguration};
    /// # use screencapturekit::shareable_content::SCShareableContent;
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let content = SCShareableContent::get()?;
    /// # let display = &content.displays()[0];
    /// # let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
    /// # let config = SCStreamConfiguration::new().with_width(1920).with_height(1080);
    /// // Pre-allocate once, reuse across many screenshots.
    /// let mut buffer: Vec<u8> = vec![0; 1920 * 1080 * 4];
    /// for _ in 0..100 {
    ///     let img = SCScreenshotManager::capture_image(&filter, &config)?;
    ///     img.rgba_data_into(&mut buffer)?;
    ///     // process `buffer`...
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub fn rgba_data_into(&self, dest: &mut [u8]) -> Result<usize, SCError> {
        self.render_pixel_data_into(dest, PixelLayout::Rgba)
    }

    /// Render the image's **BGRA** bytes into a caller-supplied buffer.
    ///
    /// Same shape as [`rgba_data_into`](Self::rgba_data_into) but in the
    /// native source pixel layout — saves the per-pixel R↔B swap
    /// `rgba_data_into` performs. Combine with a reusable buffer for the
    /// fastest possible sustained-capture loop.
    ///
    /// # Errors
    /// Returns `SCError::InternalError` if `dest` is too small or the
    /// `CGContext` draw fails.
    pub fn bgra_data_into(&self, dest: &mut [u8]) -> Result<usize, SCError> {
        self.render_pixel_data_into(dest, PixelLayout::Bgra)
    }

    fn render_pixel_data(&self, layout: PixelLayout) -> Result<Vec<u8>, SCError> {
        let total_bytes = self.required_byte_size()?;
        if total_bytes == 0 {
            return Ok(Vec::new());
        }

        // Allocate uninitialised — the FFI draws straight into this buffer via
        // CGContext, writing every byte. The previous flow allocated three
        // times the data: CGContext buffer + Swift-owned malloc copy + Rust
        // .to_vec() copy. This single-buffer form measured ~28% end-to-end
        // faster on 4K screenshots; the BGRA variant additionally skips the
        // per-pixel channel swap CGContext.draw performs when targeting RGBA
        // (~20 ms saved on a 4K shot).
        let mut data: Vec<u8> = Vec::with_capacity(total_bytes);
        let written = unsafe { layout.render(self.ptr, data.as_mut_ptr(), total_bytes) };

        if written != total_bytes {
            return Err(SCError::internal_error(format!(
                "Failed to render CGImage into {} buffer",
                layout.name()
            )));
        }

        unsafe { data.set_len(total_bytes) };
        Ok(data)
    }

    fn render_pixel_data_into(
        &self,
        dest: &mut [u8],
        layout: PixelLayout,
    ) -> Result<usize, SCError> {
        let total_bytes = self.required_byte_size()?;
        if dest.len() < total_bytes {
            return Err(SCError::internal_error(format!(
                "Destination buffer too small: need {total_bytes} bytes, got {}",
                dest.len()
            )));
        }
        if total_bytes == 0 {
            return Ok(0);
        }

        let written = unsafe { layout.render(self.ptr, dest.as_mut_ptr(), total_bytes) };
        if written != total_bytes {
            return Err(SCError::internal_error(format!(
                "Failed to render CGImage into {} buffer",
                layout.name()
            )));
        }
        Ok(written)
    }

    fn required_byte_size(&self) -> Result<usize, SCError> {
        self.width()
            .checked_mul(self.height())
            .and_then(|n| n.checked_mul(4))
            .ok_or_else(|| SCError::internal_error("CGImage dimensions overflow usize"))
    }

    /// Save the image to a PNG file
    ///
    /// # Arguments
    /// * `path` - The file path to save the PNG to
    ///
    /// # Errors
    /// Returns an error if the image cannot be saved
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use screencapturekit::screenshot_manager::SCScreenshotManager;
    /// # use screencapturekit::stream::{content_filter::SCContentFilter, configuration::SCStreamConfiguration};
    /// # use screencapturekit::shareable_content::SCShareableContent;
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let content = SCShareableContent::get()?;
    /// # let display = &content.displays()[0];
    /// # let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
    /// # let config = SCStreamConfiguration::new().with_width(1920).with_height(1080);
    /// let image = SCScreenshotManager::capture_image(&filter, &config)?;
    /// image.save_png("/tmp/screenshot.png")?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn save_png(&self, path: &str) -> Result<(), SCError> {
        self.save(path, ImageFormat::Png)
    }

    /// Save the image to a file in the specified format
    ///
    /// # Arguments
    /// * `path` - The file path to save the image to
    /// * `format` - The output format (PNG, JPEG, TIFF, GIF, BMP, or HEIC)
    ///
    /// # Errors
    /// Returns an error if the image cannot be saved
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use screencapturekit::screenshot_manager::{SCScreenshotManager, ImageFormat};
    /// # use screencapturekit::stream::{content_filter::SCContentFilter, configuration::SCStreamConfiguration};
    /// # use screencapturekit::shareable_content::SCShareableContent;
    /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// # let content = SCShareableContent::get()?;
    /// # let display = &content.displays()[0];
    /// # let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
    /// # let config = SCStreamConfiguration::new().with_width(1920).with_height(1080);
    /// let image = SCScreenshotManager::capture_image(&filter, &config)?;
    ///
    /// // Save as PNG (lossless)
    /// image.save("/tmp/screenshot.png", ImageFormat::Png)?;
    ///
    /// // Save as JPEG with 85% quality
    /// image.save("/tmp/screenshot.jpg", ImageFormat::Jpeg(0.85))?;
    ///
    /// // Save as HEIC with 90% quality (smaller file size)
    /// image.save("/tmp/screenshot.heic", ImageFormat::Heic(0.9))?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn save(&self, path: &str, format: ImageFormat) -> Result<(), SCError> {
        let c_path = std::ffi::CString::new(path)
            .map_err(|_| SCError::internal_error("Path contains null bytes"))?;

        let success = unsafe {
            crate::ffi::cgimage_save_to_file(
                self.ptr,
                c_path.as_ptr(),
                format.to_format_id(),
                format.quality(),
            )
        };

        if success {
            Ok(())
        } else {
            Err(SCError::internal_error(format!(
                "Failed to save image as {}",
                format.extension().to_uppercase()
            )))
        }
    }
}

impl Drop for CGImage {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                crate::ffi::cgimage_release(self.ptr);
            }
        }
    }
}

impl std::fmt::Debug for CGImage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CGImage")
            .field("width", &self.width())
            .field("height", &self.height())
            .finish()
    }
}

unsafe impl Send for CGImage {}
unsafe impl Sync for CGImage {}

/// Manager for capturing single screenshots
///
/// Available on macOS 14.0+. Provides a simpler API than `SCStream` for one-time captures.
///
/// # Examples
///
/// ```no_run
/// use screencapturekit::screenshot_manager::SCScreenshotManager;
/// use screencapturekit::stream::{content_filter::SCContentFilter, configuration::SCStreamConfiguration};
/// use screencapturekit::shareable_content::SCShareableContent;
///
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let content = SCShareableContent::get()?;
/// let display = &content.displays()[0];
/// let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
/// let config = SCStreamConfiguration::new()
///     .with_width(1920)
///     .with_height(1080);
///
/// let image = SCScreenshotManager::capture_image(&filter, &config)?;
/// println!("Captured screenshot: {}x{}", image.width(), image.height());
/// # Ok(())
/// # }
/// ```
pub struct SCScreenshotManager;

impl SCScreenshotManager {
    /// Capture a single screenshot as a `CGImage`
    ///
    /// # Errors
    /// Returns an error if:
    /// - The system is not macOS 14.0+
    /// - Screen recording permission is not granted
    /// - The capture fails for any reason
    ///
    /// # Panics
    /// Panics if the internal mutex is poisoned.
    pub fn capture_image(
        content_filter: &SCContentFilter,
        configuration: &SCStreamConfiguration,
    ) -> Result<CGImage, SCError> {
        let (completion, context) = SyncCompletion::<CGImage>::new();

        unsafe {
            crate::ffi::sc_screenshot_manager_capture_image(
                content_filter.as_ptr(),
                configuration.as_ptr(),
                image_callback,
                context,
            );
        }

        completion.wait().map_err(SCError::ScreenshotError)
    }

    /// Capture a single screenshot as a `CMSampleBuffer`
    ///
    /// Returns the sample buffer for advanced processing.
    ///
    /// # Errors
    /// Returns an error if:
    /// - The system is not macOS 14.0+
    /// - Screen recording permission is not granted
    /// - The capture fails for any reason
    ///
    /// # Panics
    /// Panics if the internal mutex is poisoned.
    pub fn capture_sample_buffer(
        content_filter: &SCContentFilter,
        configuration: &SCStreamConfiguration,
    ) -> Result<crate::cm::CMSampleBuffer, SCError> {
        let (completion, context) = SyncCompletion::<crate::cm::CMSampleBuffer>::new();

        unsafe {
            crate::ffi::sc_screenshot_manager_capture_sample_buffer(
                content_filter.as_ptr(),
                configuration.as_ptr(),
                buffer_callback,
                context,
            );
        }

        completion.wait().map_err(SCError::ScreenshotError)
    }

    /// Capture a screenshot of a specific screen region (macOS 15.2+)
    ///
    /// This method captures the content within the specified rectangle,
    /// which can span multiple displays.
    ///
    /// # Arguments
    /// * `rect` - The rectangle to capture, in screen coordinates (points)
    ///
    /// # Errors
    /// Returns an error if:
    /// - The system is not macOS 15.2+
    /// - Screen recording permission is not granted
    /// - The capture fails for any reason
    ///
    /// # Examples
    /// ```no_run
    /// use screencapturekit::screenshot_manager::SCScreenshotManager;
    /// use screencapturekit::cg::CGRect;
    ///
    /// fn example() -> Result<(), screencapturekit::utils::error::SCError> {
    ///     let rect = CGRect::new(0.0, 0.0, 1920.0, 1080.0);
    ///     let image = SCScreenshotManager::capture_image_in_rect(rect)?;
    ///     Ok(())
    /// }
    /// ```
    #[cfg(feature = "macos_15_2")]
    pub fn capture_image_in_rect(rect: CGRect) -> Result<CGImage, SCError> {
        let (completion, context) = SyncCompletion::<CGImage>::new();

        unsafe {
            crate::ffi::sc_screenshot_manager_capture_image_in_rect(
                rect.x,
                rect.y,
                rect.width,
                rect.height,
                image_callback,
                context,
            );
        }

        completion.wait().map_err(SCError::ScreenshotError)
    }

    /// Capture a screenshot with advanced configuration (macOS 26.0+)
    ///
    /// This method uses the new `SCScreenshotConfiguration` for more control
    /// over the screenshot output, including HDR support and file saving.
    ///
    /// # Arguments
    /// * `content_filter` - The content filter specifying what to capture
    /// * `configuration` - The screenshot configuration
    ///
    /// # Errors
    /// Returns an error if the capture fails
    ///
    /// # Examples
    /// ```no_run
    /// use screencapturekit::screenshot_manager::{SCScreenshotManager, SCScreenshotConfiguration, SCScreenshotDynamicRange};
    /// use screencapturekit::stream::content_filter::SCContentFilter;
    /// use screencapturekit::shareable_content::SCShareableContent;
    ///
    /// fn example() -> Option<()> {
    ///     let content = SCShareableContent::get().ok()?;
    ///     let displays = content.displays();
    ///     let display = displays.first()?;
    ///     let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
    ///     let config = SCScreenshotConfiguration::new()
    ///         .with_width(1920)
    ///         .with_height(1080)
    ///         .with_dynamic_range(SCScreenshotDynamicRange::BothSDRAndHDR);
    ///
    ///     let output = SCScreenshotManager::capture_screenshot(&filter, &config).ok()?;
    ///     if let Some(sdr) = output.sdr_image() {
    ///         println!("SDR image: {}x{}", sdr.width(), sdr.height());
    ///     }
    ///     Some(())
    /// }
    /// ```
    #[cfg(feature = "macos_26_0")]
    pub fn capture_screenshot(
        content_filter: &SCContentFilter,
        configuration: &SCScreenshotConfiguration,
    ) -> Result<SCScreenshotOutput, SCError> {
        let (completion, context) = SyncCompletion::<SCScreenshotOutput>::new();

        unsafe {
            crate::ffi::sc_screenshot_manager_capture_screenshot(
                content_filter.as_ptr(),
                configuration.as_ptr(),
                screenshot_output_callback,
                context,
            );
        }

        completion.wait().map_err(SCError::ScreenshotError)
    }

    /// Capture a screenshot of a specific region with advanced configuration (macOS 26.0+)
    ///
    /// # Arguments
    /// * `rect` - The rectangle to capture, in screen coordinates (points)
    /// * `configuration` - The screenshot configuration
    ///
    /// # Errors
    /// Returns an error if the capture fails
    #[cfg(feature = "macos_26_0")]
    pub fn capture_screenshot_in_rect(
        rect: crate::cg::CGRect,
        configuration: &SCScreenshotConfiguration,
    ) -> Result<SCScreenshotOutput, SCError> {
        let (completion, context) = SyncCompletion::<SCScreenshotOutput>::new();

        unsafe {
            crate::ffi::sc_screenshot_manager_capture_screenshot_in_rect(
                rect.x,
                rect.y,
                rect.width,
                rect.height,
                configuration.as_ptr(),
                screenshot_output_callback,
                context,
            );
        }

        completion.wait().map_err(SCError::ScreenshotError)
    }
}

// ============================================================================
// SCScreenshotConfiguration (macOS 26.0+)
// ============================================================================

/// Display intent for screenshot rendering (macOS 26.0+)
#[cfg(feature = "macos_26_0")]
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SCScreenshotDisplayIntent {
    /// Render on the canonical display
    #[default]
    Canonical = 0,
    /// Render on the local display
    Local = 1,
}

/// Dynamic range for screenshot output (macOS 26.0+)
#[cfg(feature = "macos_26_0")]
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SCScreenshotDynamicRange {
    /// SDR output only
    #[default]
    SDR = 0,
    /// HDR output only
    HDR = 1,
    /// Both SDR and HDR output
    BothSDRAndHDR = 2,
}

/// Configuration for advanced screenshot capture (macOS 26.0+)
///
/// Provides fine-grained control over screenshot output including:
/// - Output dimensions
/// - Source and destination rectangles
/// - Shadow and clipping behavior
/// - HDR/SDR dynamic range
/// - File output
///
/// # Examples
///
/// ```no_run
/// use screencapturekit::screenshot_manager::{SCScreenshotConfiguration, SCScreenshotDynamicRange};
///
/// let config = SCScreenshotConfiguration::new()
///     .with_width(1920)
///     .with_height(1080)
///     .with_shows_cursor(true)
///     .with_dynamic_range(SCScreenshotDynamicRange::BothSDRAndHDR);
/// ```
#[cfg(feature = "macos_26_0")]
pub struct SCScreenshotConfiguration {
    ptr: *const c_void,
}

#[cfg(feature = "macos_26_0")]
impl SCScreenshotConfiguration {
    /// Create a new screenshot configuration
    ///
    /// # Panics
    /// Panics if the configuration cannot be created (requires macOS 26.0+)
    #[must_use]
    pub fn new() -> Self {
        let ptr = unsafe { crate::ffi::sc_screenshot_configuration_create() };
        assert!(!ptr.is_null(), "Failed to create SCScreenshotConfiguration");
        Self { ptr }
    }

    /// Set the output width in pixels
    #[must_use]
    #[allow(clippy::cast_possible_wrap)]
    pub fn with_width(self, width: usize) -> Self {
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_width(self.ptr, width as isize);
        }
        self
    }

    /// Set the output height in pixels
    #[must_use]
    #[allow(clippy::cast_possible_wrap)]
    pub fn with_height(self, height: usize) -> Self {
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_height(self.ptr, height as isize);
        }
        self
    }

    /// Set whether to show the cursor
    #[must_use]
    pub fn with_shows_cursor(self, shows_cursor: bool) -> Self {
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_shows_cursor(self.ptr, shows_cursor);
        }
        self
    }

    /// Set the source rectangle (subset of capture area)
    #[must_use]
    pub fn with_source_rect(self, rect: crate::cg::CGRect) -> Self {
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_source_rect(
                self.ptr,
                rect.x,
                rect.y,
                rect.width,
                rect.height,
            );
        }
        self
    }

    /// Set the destination rectangle (output area)
    #[must_use]
    pub fn with_destination_rect(self, rect: crate::cg::CGRect) -> Self {
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_destination_rect(
                self.ptr,
                rect.x,
                rect.y,
                rect.width,
                rect.height,
            );
        }
        self
    }

    /// Set whether to ignore shadows
    #[must_use]
    pub fn with_ignore_shadows(self, ignore_shadows: bool) -> Self {
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_ignore_shadows(self.ptr, ignore_shadows);
        }
        self
    }

    /// Set whether to ignore clipping
    #[must_use]
    pub fn with_ignore_clipping(self, ignore_clipping: bool) -> Self {
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_ignore_clipping(self.ptr, ignore_clipping);
        }
        self
    }

    /// Set whether to include child windows
    #[must_use]
    pub fn with_include_child_windows(self, include_child_windows: bool) -> Self {
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_include_child_windows(
                self.ptr,
                include_child_windows,
            );
        }
        self
    }

    /// Set the display intent
    #[must_use]
    pub fn with_display_intent(self, display_intent: SCScreenshotDisplayIntent) -> Self {
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_display_intent(
                self.ptr,
                display_intent as i32,
            );
        }
        self
    }

    /// Set the dynamic range
    #[must_use]
    pub fn with_dynamic_range(self, dynamic_range: SCScreenshotDynamicRange) -> Self {
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_dynamic_range(
                self.ptr,
                dynamic_range as i32,
            );
        }
        self
    }

    /// Set the output file URL
    ///
    /// # Panics
    /// Panics if the path contains null bytes
    #[must_use]
    pub fn with_file_path(self, path: &str) -> Self {
        let c_path = std::ffi::CString::new(path).expect("path should not contain null bytes");
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_file_url(self.ptr, c_path.as_ptr());
        }
        self
    }

    /// Set the content type (output format) using `UTType` identifier
    ///
    /// Common identifiers include:
    /// - `"public.png"` - PNG format
    /// - `"public.jpeg"` - JPEG format
    /// - `"public.heic"` - HEIC format
    /// - `"public.tiff"` - TIFF format
    ///
    /// Use [`supported_content_types()`](Self::supported_content_types) to get
    /// available formats.
    ///
    /// # Panics
    /// Panics if the identifier contains null bytes
    #[must_use]
    pub fn with_content_type(self, identifier: &str) -> Self {
        let c_id =
            std::ffi::CString::new(identifier).expect("identifier should not contain null bytes");
        unsafe {
            crate::ffi::sc_screenshot_configuration_set_content_type(self.ptr, c_id.as_ptr());
        }
        self
    }

    /// Get the current content type as `UTType` identifier
    pub fn content_type(&self) -> Option<String> {
        let mut buffer = vec![0i8; 256];
        let success = unsafe {
            crate::ffi::sc_screenshot_configuration_get_content_type(
                self.ptr,
                buffer.as_mut_ptr(),
                buffer.len(),
            )
        };
        if success {
            let c_str = unsafe { std::ffi::CStr::from_ptr(buffer.as_ptr()) };
            c_str.to_str().ok().map(ToString::to_string)
        } else {
            None
        }
    }

    /// Get the list of supported content types (`UTType` identifiers)
    ///
    /// Returns a list of `UTType` identifiers that can be used with
    /// [`with_content_type()`](Self::with_content_type).
    ///
    /// Common types include:
    /// - `"public.png"` - PNG format
    /// - `"public.jpeg"` - JPEG format
    /// - `"public.heic"` - HEIC format
    pub fn supported_content_types() -> Vec<String> {
        let count =
            unsafe { crate::ffi::sc_screenshot_configuration_get_supported_content_types_count() };
        let mut result = Vec::with_capacity(count);
        for i in 0..count {
            let mut buffer = vec![0i8; 256];
            let success = unsafe {
                crate::ffi::sc_screenshot_configuration_get_supported_content_type_at(
                    i,
                    buffer.as_mut_ptr(),
                    buffer.len(),
                )
            };
            if success {
                let c_str = unsafe { std::ffi::CStr::from_ptr(buffer.as_ptr()) };
                if let Ok(s) = c_str.to_str() {
                    result.push(s.to_string());
                }
            }
        }
        result
    }

    #[must_use]
    pub const fn as_ptr(&self) -> *const c_void {
        self.ptr
    }
}

#[cfg(feature = "macos_26_0")]
impl std::fmt::Debug for SCScreenshotConfiguration {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SCScreenshotConfiguration")
            .field("content_type", &self.content_type())
            .finish_non_exhaustive()
    }
}

#[cfg(feature = "macos_26_0")]
impl Default for SCScreenshotConfiguration {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "macos_26_0")]
impl Drop for SCScreenshotConfiguration {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                crate::ffi::sc_screenshot_configuration_release(self.ptr);
            }
        }
    }
}

#[cfg(feature = "macos_26_0")]
unsafe impl Send for SCScreenshotConfiguration {}
#[cfg(feature = "macos_26_0")]
unsafe impl Sync for SCScreenshotConfiguration {}

// ============================================================================
// SCScreenshotOutput (macOS 26.0+)
// ============================================================================

/// Output from advanced screenshot capture (macOS 26.0+)
///
/// Contains SDR and/or HDR images depending on the configuration,
/// and optionally the file URL where the image was saved.
#[cfg(feature = "macos_26_0")]
pub struct SCScreenshotOutput {
    ptr: *const c_void,
}

#[cfg(feature = "macos_26_0")]
impl SCScreenshotOutput {
    pub(crate) fn from_ptr(ptr: *const c_void) -> Self {
        Self { ptr }
    }

    /// Get the SDR image if available
    #[must_use]
    pub fn sdr_image(&self) -> Option<CGImage> {
        let ptr = unsafe { crate::ffi::sc_screenshot_output_get_sdr_image(self.ptr) };
        if ptr.is_null() {
            None
        } else {
            Some(CGImage::from_ptr(ptr))
        }
    }

    /// Get the HDR image if available
    #[must_use]
    pub fn hdr_image(&self) -> Option<CGImage> {
        let ptr = unsafe { crate::ffi::sc_screenshot_output_get_hdr_image(self.ptr) };
        if ptr.is_null() {
            None
        } else {
            Some(CGImage::from_ptr(ptr))
        }
    }

    /// Get the file URL where the image was saved, if applicable
    #[must_use]
    #[allow(clippy::cast_possible_wrap)]
    pub fn file_url(&self) -> Option<String> {
        let mut buffer = vec![0i8; 4096];
        let success = unsafe {
            crate::ffi::sc_screenshot_output_get_file_url(
                self.ptr,
                buffer.as_mut_ptr(),
                buffer.len() as isize,
            )
        };
        if success {
            let c_str = unsafe { std::ffi::CStr::from_ptr(buffer.as_ptr()) };
            c_str.to_str().ok().map(String::from)
        } else {
            None
        }
    }
}

#[cfg(feature = "macos_26_0")]
impl std::fmt::Debug for SCScreenshotOutput {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SCScreenshotOutput")
            .field(
                "sdr_image",
                &self.sdr_image().map(|i| (i.width(), i.height())),
            )
            .field(
                "hdr_image",
                &self.hdr_image().map(|i| (i.width(), i.height())),
            )
            .field("file_url", &self.file_url())
            .finish()
    }
}

#[cfg(feature = "macos_26_0")]
impl Drop for SCScreenshotOutput {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe {
                crate::ffi::sc_screenshot_output_release(self.ptr);
            }
        }
    }
}

#[cfg(feature = "macos_26_0")]
unsafe impl Send for SCScreenshotOutput {}
#[cfg(feature = "macos_26_0")]
unsafe impl Sync for SCScreenshotOutput {}