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
use std::mem;

use core_foundation::{
    base::{Boolean, CFGetTypeID, CFType, CFTypeID, CFTypeRef, TCFType, TCFTypeRef},
    dictionary::{CFDictionary, CFDictionaryRef},
    string::{CFString, CFStringRef},
};
use core_graphics::{
    color_space::CGColorSpace,
    geometry::{CGRect, CGSize},
    sys::CGColorSpaceRef,
};
use foreign_types::ForeignType;

use crate::buffer::{CVBuffer, CVBufferRef, CVBufferRelease, CVBufferRetain, TCVBuffer};

pub type CVImageBufferRef = CVBufferRef;

extern "C" {
    pub static kCVImageBufferCGColorSpaceKey: CFStringRef;
    pub static kCVImageBufferCleanApertureKey: CFStringRef;
    pub static kCVImageBufferCleanApertureWidthKey: CFStringRef;
    pub static kCVImageBufferCleanApertureHeightKey: CFStringRef;
    pub static kCVImageBufferCleanApertureHorizontalOffsetKey: CFStringRef;
    pub static kCVImageBufferCleanApertureVerticalOffsetKey: CFStringRef;
    pub static kCVImageBufferPreferredCleanApertureKey: CFStringRef;
    pub static kCVImageBufferFieldCountKey: CFStringRef;
    pub static kCVImageBufferFieldDetailKey: CFStringRef;
    pub static kCVImageBufferFieldDetailTemporalTopFirst: CFStringRef;
    pub static kCVImageBufferFieldDetailTemporalBottomFirst: CFStringRef;
    pub static kCVImageBufferFieldDetailSpatialFirstLineEarly: CFStringRef;
    pub static kCVImageBufferFieldDetailSpatialFirstLineLate: CFStringRef;
    pub static kCVImageBufferPixelAspectRatioKey: CFStringRef;
    pub static kCVImageBufferPixelAspectRatioHorizontalSpacingKey: CFStringRef;
    pub static kCVImageBufferPixelAspectRatioVerticalSpacingKey: CFStringRef;
    pub static kCVImageBufferDisplayDimensionsKey: CFStringRef;
    pub static kCVImageBufferDisplayWidthKey: CFStringRef;
    pub static kCVImageBufferDisplayHeightKey: CFStringRef;
    pub static kCVImageBufferGammaLevelKey: CFStringRef;
    pub static kCVImageBufferICCProfileKey: CFStringRef;
    pub static kCVImageBufferYCbCrMatrixKey: CFStringRef;
    pub static kCVImageBufferYCbCrMatrix_ITU_R_709_2: CFStringRef;
    pub static kCVImageBufferYCbCrMatrix_ITU_R_601_4: CFStringRef;
    pub static kCVImageBufferYCbCrMatrix_SMPTE_240M_1995: CFStringRef;
    pub static kCVImageBufferYCbCrMatrix_DCI_P3: CFStringRef;
    pub static kCVImageBufferYCbCrMatrix_P3_D65: CFStringRef;
    pub static kCVImageBufferYCbCrMatrix_ITU_R_2020: CFStringRef;
    pub static kCVImageBufferColorPrimariesKey: CFStringRef;
    pub static kCVImageBufferColorPrimaries_ITU_R_709_2: CFStringRef;
    pub static kCVImageBufferColorPrimaries_EBU_3213: CFStringRef;
    pub static kCVImageBufferColorPrimaries_SMPTE_C: CFStringRef;
    pub static kCVImageBufferColorPrimaries_P22: CFStringRef;
    pub static kCVImageBufferColorPrimaries_DCI_P3: CFStringRef;
    pub static kCVImageBufferColorPrimaries_P3_D65: CFStringRef;
    pub static kCVImageBufferColorPrimaries_ITU_R_2020: CFStringRef;
    pub static kCVImageBufferTransferFunctionKey: CFStringRef;
    pub static kCVImageBufferTransferFunction_ITU_R_709_2: CFStringRef;
    pub static kCVImageBufferTransferFunction_SMPTE_240M_1995: CFStringRef;
    pub static kCVImageBufferTransferFunction_UseGamma: CFStringRef;
    pub static kCVImageBufferTransferFunction_sRGB: CFStringRef;
    pub static kCVImageBufferTransferFunction_ITU_R_2020: CFStringRef;
    pub static kCVImageBufferTransferFunction_SMPTE_ST_428_1: CFStringRef;
    pub static kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ: CFStringRef;
    pub static kCVImageBufferTransferFunction_ITU_R_2100_HLG: CFStringRef;
    pub static kCVImageBufferTransferFunction_Linear: CFStringRef;
    pub static kCVImageBufferChromaLocationTopFieldKey: CFStringRef;
    pub static kCVImageBufferChromaLocationBottomFieldKey: CFStringRef;
    pub static kCVImageBufferChromaLocation_Left: CFStringRef;
    pub static kCVImageBufferChromaLocation_Center: CFStringRef;
    pub static kCVImageBufferChromaLocation_TopLeft: CFStringRef;
    pub static kCVImageBufferChromaLocation_Top: CFStringRef;
    pub static kCVImageBufferChromaLocation_BottomLeft: CFStringRef;
    pub static kCVImageBufferChromaLocation_Bottom: CFStringRef;
    pub static kCVImageBufferChromaLocation_DV420: CFStringRef;
    pub static kCVImageBufferChromaSubsamplingKey: CFStringRef;
    pub static kCVImageBufferChromaSubsampling_420: CFStringRef;
    pub static kCVImageBufferChromaSubsampling_422: CFStringRef;
    pub static kCVImageBufferChromaSubsampling_411: CFStringRef;
    pub static kCVImageBufferAlphaChannelIsOpaque: CFStringRef;
    pub static kCVImageBufferAlphaChannelModeKey: CFStringRef;
    pub static kCVImageBufferAlphaChannelMode_StraightAlpha: CFStringRef;
    pub static kCVImageBufferAlphaChannelMode_PremultipliedAlpha: CFStringRef;

    pub fn CVYCbCrMatrixGetIntegerCodePointForString(yCbCrMatrixString: CFStringRef) -> i32;
    pub fn CVColorPrimariesGetIntegerCodePointForString(colorPrimariesString: CFStringRef) -> i32;
    pub fn CVTransferFunctionGetIntegerCodePointForString(transferFunctionString: CFStringRef) -> i32;
    pub fn CVYCbCrMatrixGetStringForIntegerCodePoint(yCbCrMatrixCodePoint: i32) -> CFStringRef;
    pub fn CVColorPrimariesGetStringForIntegerCodePoint(colorPrimariesCodePoint: i32) -> CFStringRef;
    pub fn CVTransferFunctionGetStringForIntegerCodePoint(transferFunctionCodePoint: i32) -> CFStringRef;

    pub fn CVImageBufferGetEncodedSize(imageBuffer: CVImageBufferRef) -> CGSize;
    pub fn CVImageBufferGetDisplaySize(imageBuffer: CVImageBufferRef) -> CGSize;
    pub fn CVImageBufferGetCleanRect(imageBuffer: CVImageBufferRef) -> CGRect;
    pub fn CVImageBufferIsFlipped(imageBuffer: CVImageBufferRef) -> Boolean;
    pub fn CVImageBufferGetColorSpace(imageBuffer: CVImageBufferRef) -> CGColorSpaceRef;
    pub fn CVImageBufferCreateColorSpaceFromAttachments(attachments: CFDictionaryRef) -> CGColorSpaceRef;

    pub static kCVImageBufferMasteringDisplayColorVolumeKey: CFStringRef;
    pub static kCVImageBufferContentLightLevelInfoKey: CFStringRef;
    pub static kCVImageBufferAmbientViewingEnvironmentKey: CFStringRef;
    pub static kCVImageBufferRegionOfInterestKey: CFStringRef;
}

pub struct CVImageBuffer(CVImageBufferRef);

impl Drop for CVImageBuffer {
    fn drop(&mut self) {
        unsafe { CVBufferRelease(self.0) }
    }
}

impl CVImageBuffer {
    #[inline]
    pub fn as_concrete_TypeRef(&self) -> CVImageBufferRef {
        self.0
    }

    #[inline]
    pub fn as_CFType(&self) -> CFType {
        unsafe { CFType::wrap_under_get_rule(self.as_CFTypeRef()) }
    }

    #[inline]
    pub fn as_CFTypeRef(&self) -> CFTypeRef {
        self.as_concrete_TypeRef() as CFTypeRef
    }

    #[inline]
    pub fn into_CFType(self) -> CFType {
        let reference = self.as_CFTypeRef();
        mem::forget(self);
        unsafe { CFType::wrap_under_create_rule(reference) }
    }

    #[inline]
    pub unsafe fn wrap_under_create_rule(reference: CVImageBufferRef) -> CVImageBuffer {
        CVImageBuffer(reference)
    }

    #[inline]
    pub unsafe fn wrap_under_get_rule(reference: CVImageBufferRef) -> CVImageBuffer {
        CVImageBuffer(CVBufferRetain(reference))
    }

    #[inline]
    pub fn type_of(&self) -> CFTypeID {
        unsafe { CFGetTypeID(self.as_CFTypeRef()) }
    }

    #[inline]
    pub fn instance_of<T: TCFType>(&self) -> bool {
        self.type_of() == T::type_id()
    }
}

impl Clone for CVImageBuffer {
    fn clone(&self) -> CVImageBuffer {
        unsafe { CVImageBuffer::wrap_under_get_rule(self.0) }
    }
}

impl PartialEq for CVImageBuffer {
    fn eq(&self, other: &CVImageBuffer) -> bool {
        self.as_CFType().eq(&other.as_CFType())
    }
}

impl Eq for CVImageBuffer {}

impl_CFTypeDescription!(CVImageBuffer);

pub trait TCVImageBuffer: TCVBuffer {
    #[inline]
    fn as_image_buffer(&self) -> CVImageBuffer {
        unsafe { CVImageBuffer::wrap_under_get_rule(self.as_concrete_TypeRef().as_void_ptr() as CVImageBufferRef) }
    }

    #[inline]
    fn into_image_buffer(self) -> CVImageBuffer
    where
        Self: Sized,
    {
        let reference = self.as_concrete_TypeRef().as_void_ptr() as CVImageBufferRef;
        mem::forget(self);
        unsafe { CVImageBuffer::wrap_under_create_rule(reference) }
    }
}

impl CVImageBuffer {
    pub fn as_buffer(&self) -> CVBuffer {
        unsafe { CVBuffer::wrap_under_get_rule(self.as_concrete_TypeRef() as CVBufferRef) }
    }

    pub fn into_buffer(self) -> CVBuffer
    where
        Self: Sized,
    {
        let reference = self.as_concrete_TypeRef() as CVBufferRef;
        mem::forget(self);
        unsafe { CVBuffer::wrap_under_create_rule(reference) }
    }

    pub fn get_encoded_size(&self) -> CGSize {
        unsafe { CVImageBufferGetEncodedSize(self.as_concrete_TypeRef()) }
    }

    pub fn get_display_size(&self) -> CGSize {
        unsafe { CVImageBufferGetDisplaySize(self.as_concrete_TypeRef()) }
    }

    pub fn get_clean_rect(&self) -> CGRect {
        unsafe { CVImageBufferGetCleanRect(self.as_concrete_TypeRef()) }
    }

    pub fn is_flipped(&self) -> bool {
        unsafe { CVImageBufferIsFlipped(self.as_concrete_TypeRef()) != 0 }
    }

    pub fn get_color_space(&self) -> Option<CGColorSpace> {
        unsafe {
            let color_space = CVImageBufferGetColorSpace(self.as_concrete_TypeRef());
            if color_space.is_null() {
                None
            } else {
                Some(CGColorSpace::from_ptr(color_space))
            }
        }
    }
}

pub fn create_color_space_from_attachments(attachments: &CFDictionary<CFString, CFType>) -> Option<CGColorSpace> {
    unsafe {
        let color_space = CVImageBufferCreateColorSpaceFromAttachments(attachments.as_concrete_TypeRef());
        if color_space.is_null() {
            None
        } else {
            Some(CGColorSpace::from_ptr(color_space))
        }
    }
}