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
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, CGColorSpaceRef},
    geometry::{CGRect, CGSize},
};
use libc::c_void;

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 enum CVImageBufferKeys {
    CGColorSpace,
    CleanAperture,
    PreferredCleanAperture,
    FieldCount,
    FieldDetail,
    PixelAspectRatio,
    DisplayDimensions,
    GammaLevel,
    ICCProfile,
    YCbCrMatrix,
    ColorPrimaries,
    TransferFunction,
    ChromaLocationTopField,
    ChromaLocationBottomField,
    ChromaSubsampling,
    AlphaChannelIsOpaque,
    AlphaChannelMode,
    MasteringDisplayColorVolume,
    ContentLightLevelInfo,
    AmbientViewingEnvironment,
    RegionOfInterest,
}

impl From<CVImageBufferKeys> for CFStringRef {
    fn from(key: CVImageBufferKeys) -> CFStringRef {
        unsafe {
            match key {
                CVImageBufferKeys::CGColorSpace => kCVImageBufferCGColorSpaceKey,
                CVImageBufferKeys::CleanAperture => kCVImageBufferCleanApertureKey,
                CVImageBufferKeys::PreferredCleanAperture => kCVImageBufferPreferredCleanApertureKey,
                CVImageBufferKeys::FieldCount => kCVImageBufferFieldCountKey,
                CVImageBufferKeys::FieldDetail => kCVImageBufferFieldDetailKey,
                CVImageBufferKeys::PixelAspectRatio => kCVImageBufferPixelAspectRatioKey,
                CVImageBufferKeys::DisplayDimensions => kCVImageBufferDisplayDimensionsKey,
                CVImageBufferKeys::GammaLevel => kCVImageBufferGammaLevelKey,
                CVImageBufferKeys::ICCProfile => kCVImageBufferICCProfileKey,
                CVImageBufferKeys::YCbCrMatrix => kCVImageBufferYCbCrMatrixKey,
                CVImageBufferKeys::ColorPrimaries => kCVImageBufferColorPrimariesKey,
                CVImageBufferKeys::TransferFunction => kCVImageBufferTransferFunctionKey,
                CVImageBufferKeys::ChromaLocationTopField => kCVImageBufferChromaLocationTopFieldKey,
                CVImageBufferKeys::ChromaLocationBottomField => kCVImageBufferChromaLocationBottomFieldKey,
                CVImageBufferKeys::ChromaSubsampling => kCVImageBufferChromaSubsamplingKey,
                CVImageBufferKeys::AlphaChannelIsOpaque => kCVImageBufferAlphaChannelIsOpaque,
                CVImageBufferKeys::AlphaChannelMode => kCVImageBufferAlphaChannelModeKey,
                CVImageBufferKeys::MasteringDisplayColorVolume => kCVImageBufferMasteringDisplayColorVolumeKey,
                CVImageBufferKeys::ContentLightLevelInfo => kCVImageBufferContentLightLevelInfoKey,
                CVImageBufferKeys::AmbientViewingEnvironment => kCVImageBufferAmbientViewingEnvironmentKey,
                CVImageBufferKeys::RegionOfInterest => kCVImageBufferRegionOfInterestKey,
            }
        }
    }
}

impl From<CVImageBufferKeys> for CFString {
    fn from(key: CVImageBufferKeys) -> CFString {
        unsafe { CFString::wrap_under_get_rule(CFStringRef::from(key)) }
    }
}

pub enum CVImageBufferFieldDetail {
    TemporalTopFirst,
    TemporalBottomFirst,
    SpatialFirstLineEarly,
    SpatialFirstLineLate,
}

impl From<CVImageBufferFieldDetail> for CFStringRef {
    fn from(field_detail: CVImageBufferFieldDetail) -> CFStringRef {
        unsafe {
            match field_detail {
                CVImageBufferFieldDetail::TemporalTopFirst => kCVImageBufferFieldDetailTemporalTopFirst,
                CVImageBufferFieldDetail::TemporalBottomFirst => kCVImageBufferFieldDetailTemporalBottomFirst,
                CVImageBufferFieldDetail::SpatialFirstLineEarly => kCVImageBufferFieldDetailSpatialFirstLineEarly,
                CVImageBufferFieldDetail::SpatialFirstLineLate => kCVImageBufferFieldDetailSpatialFirstLineLate,
            }
        }
    }
}

impl From<CVImageBufferFieldDetail> for CFString {
    fn from(field_detail: CVImageBufferFieldDetail) -> CFString {
        unsafe { CFString::wrap_under_get_rule(CFStringRef::from(field_detail)) }
    }
}

pub enum CVImageBufferPixelAspectRatio {
    HorizontalSpacing,
    VerticalSpacing,
}

impl From<CVImageBufferPixelAspectRatio> for CFStringRef {
    fn from(pixel_aspect_ratio: CVImageBufferPixelAspectRatio) -> CFStringRef {
        unsafe {
            match pixel_aspect_ratio {
                CVImageBufferPixelAspectRatio::HorizontalSpacing => kCVImageBufferPixelAspectRatioHorizontalSpacingKey,
                CVImageBufferPixelAspectRatio::VerticalSpacing => kCVImageBufferPixelAspectRatioVerticalSpacingKey,
            }
        }
    }
}

impl From<CVImageBufferPixelAspectRatio> for CFString {
    fn from(pixel_aspect_ratio: CVImageBufferPixelAspectRatio) -> CFString {
        unsafe { CFString::wrap_under_get_rule(CFStringRef::from(pixel_aspect_ratio)) }
    }
}

pub enum CVImageBufferYCbCrMatrix {
    ITU_R_709_2,
    ITU_R_601_4,
    SMPTE_240M_1995,
    DCI_P3,
    P3_D65,
    ITU_R_2020,
}

impl From<CVImageBufferYCbCrMatrix> for CFStringRef {
    fn from(ycbcr_matrix: CVImageBufferYCbCrMatrix) -> CFStringRef {
        unsafe {
            match ycbcr_matrix {
                CVImageBufferYCbCrMatrix::ITU_R_709_2 => kCVImageBufferYCbCrMatrix_ITU_R_709_2,
                CVImageBufferYCbCrMatrix::ITU_R_601_4 => kCVImageBufferYCbCrMatrix_ITU_R_601_4,
                CVImageBufferYCbCrMatrix::SMPTE_240M_1995 => kCVImageBufferYCbCrMatrix_SMPTE_240M_1995,
                CVImageBufferYCbCrMatrix::DCI_P3 => kCVImageBufferYCbCrMatrix_DCI_P3,
                CVImageBufferYCbCrMatrix::P3_D65 => kCVImageBufferYCbCrMatrix_P3_D65,
                CVImageBufferYCbCrMatrix::ITU_R_2020 => kCVImageBufferYCbCrMatrix_ITU_R_2020,
            }
        }
    }
}

impl From<CVImageBufferYCbCrMatrix> for CFString {
    fn from(ycbcr_matrix: CVImageBufferYCbCrMatrix) -> CFString {
        unsafe { CFString::wrap_under_get_rule(CFStringRef::from(ycbcr_matrix)) }
    }
}

pub enum CVImageBufferColorPrimaries {
    ITU_R_709_2,
    EBU_3213,
    SMPTE_C,
    P22,
    DCI_P3,
    P3_D65,
    ITU_R_2020,
}

impl From<CVImageBufferColorPrimaries> for CFStringRef {
    fn from(color_primaries: CVImageBufferColorPrimaries) -> CFStringRef {
        unsafe {
            match color_primaries {
                CVImageBufferColorPrimaries::ITU_R_709_2 => kCVImageBufferColorPrimaries_ITU_R_709_2,
                CVImageBufferColorPrimaries::EBU_3213 => kCVImageBufferColorPrimaries_EBU_3213,
                CVImageBufferColorPrimaries::SMPTE_C => kCVImageBufferColorPrimaries_SMPTE_C,
                CVImageBufferColorPrimaries::P22 => kCVImageBufferColorPrimaries_P22,
                CVImageBufferColorPrimaries::DCI_P3 => kCVImageBufferColorPrimaries_DCI_P3,
                CVImageBufferColorPrimaries::P3_D65 => kCVImageBufferColorPrimaries_P3_D65,
                CVImageBufferColorPrimaries::ITU_R_2020 => kCVImageBufferColorPrimaries_ITU_R_2020,
            }
        }
    }
}

impl From<CVImageBufferColorPrimaries> for CFString {
    fn from(color_primaries: CVImageBufferColorPrimaries) -> CFString {
        unsafe { CFString::wrap_under_get_rule(CFStringRef::from(color_primaries)) }
    }
}

pub enum CVImageBufferTransferFunction {
    ITU_R_709_2,
    SMPTE_240M_1995,
    UseGamma,
    sRGB,
    ITU_R_2020,
    SMPTE_ST_428_1,
    SMPTE_ST_2084_PQ,
    ITU_R_2100_HLG,
    Linear,
}

impl From<CVImageBufferTransferFunction> for CFStringRef {
    fn from(transfer_function: CVImageBufferTransferFunction) -> CFStringRef {
        unsafe {
            match transfer_function {
                CVImageBufferTransferFunction::ITU_R_709_2 => kCVImageBufferTransferFunction_ITU_R_709_2,
                CVImageBufferTransferFunction::SMPTE_240M_1995 => kCVImageBufferTransferFunction_SMPTE_240M_1995,
                CVImageBufferTransferFunction::UseGamma => kCVImageBufferTransferFunction_UseGamma,
                CVImageBufferTransferFunction::sRGB => kCVImageBufferTransferFunction_sRGB,
                CVImageBufferTransferFunction::ITU_R_2020 => kCVImageBufferTransferFunction_ITU_R_2020,
                CVImageBufferTransferFunction::SMPTE_ST_428_1 => kCVImageBufferTransferFunction_SMPTE_ST_428_1,
                CVImageBufferTransferFunction::SMPTE_ST_2084_PQ => kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ,
                CVImageBufferTransferFunction::ITU_R_2100_HLG => kCVImageBufferTransferFunction_ITU_R_2100_HLG,
                CVImageBufferTransferFunction::Linear => kCVImageBufferTransferFunction_Linear,
            }
        }
    }
}

impl From<CVImageBufferTransferFunction> for CFString {
    fn from(transfer_function: CVImageBufferTransferFunction) -> CFString {
        unsafe { CFString::wrap_under_get_rule(CFStringRef::from(transfer_function)) }
    }
}

pub enum CVImageBufferChromaLocation {
    Left,
    Center,
    TopLeft,
    Top,
    BottomLeft,
    Bottom,
    DV420,
}

impl From<CVImageBufferChromaLocation> for CFStringRef {
    fn from(chroma_location: CVImageBufferChromaLocation) -> CFStringRef {
        unsafe {
            match chroma_location {
                CVImageBufferChromaLocation::Left => kCVImageBufferChromaLocation_Left,
                CVImageBufferChromaLocation::Center => kCVImageBufferChromaLocation_Center,
                CVImageBufferChromaLocation::TopLeft => kCVImageBufferChromaLocation_TopLeft,
                CVImageBufferChromaLocation::Top => kCVImageBufferChromaLocation_Top,
                CVImageBufferChromaLocation::BottomLeft => kCVImageBufferChromaLocation_BottomLeft,
                CVImageBufferChromaLocation::Bottom => kCVImageBufferChromaLocation_Bottom,
                CVImageBufferChromaLocation::DV420 => kCVImageBufferChromaLocation_DV420,
            }
        }
    }
}

impl From<CVImageBufferChromaLocation> for CFString {
    fn from(chroma_location: CVImageBufferChromaLocation) -> CFString {
        unsafe { CFString::wrap_under_get_rule(CFStringRef::from(chroma_location)) }
    }
}

pub enum CVImageBufferChromaSubsampling {
    _420,
    _422,
    _411,
}

impl From<CVImageBufferChromaSubsampling> for CFStringRef {
    fn from(chroma_subsampling: CVImageBufferChromaSubsampling) -> CFStringRef {
        unsafe {
            match chroma_subsampling {
                CVImageBufferChromaSubsampling::_420 => kCVImageBufferChromaSubsampling_420,
                CVImageBufferChromaSubsampling::_422 => kCVImageBufferChromaSubsampling_422,
                CVImageBufferChromaSubsampling::_411 => kCVImageBufferChromaSubsampling_411,
            }
        }
    }
}

impl From<CVImageBufferChromaSubsampling> for CFString {
    fn from(chroma_subsampling: CVImageBufferChromaSubsampling) -> CFString {
        unsafe { CFString::wrap_under_get_rule(CFStringRef::from(chroma_subsampling)) }
    }
}

pub enum CVImageBufferAlphaChannelMode {
    StraightAlpha,
    PremultipliedAlpha,
}

impl From<CVImageBufferAlphaChannelMode> for CFStringRef {
    fn from(alpha_channel_mode: CVImageBufferAlphaChannelMode) -> CFStringRef {
        unsafe {
            match alpha_channel_mode {
                CVImageBufferAlphaChannelMode::StraightAlpha => kCVImageBufferAlphaChannelMode_StraightAlpha,
                CVImageBufferAlphaChannelMode::PremultipliedAlpha => kCVImageBufferAlphaChannelMode_PremultipliedAlpha,
            }
        }
    }
}

impl From<CVImageBufferAlphaChannelMode> for CFString {
    fn from(alpha_channel_mode: CVImageBufferAlphaChannelMode) -> CFString {
        unsafe { CFString::wrap_under_get_rule(CFStringRef::from(alpha_channel_mode)) }
    }
}

pub fn ycbcr_matrix_get_integer_code_point_for_string(ycbcr_matrix_string: CFString) -> i32 {
    unsafe { CVYCbCrMatrixGetIntegerCodePointForString(ycbcr_matrix_string.as_concrete_TypeRef()) }
}

pub fn color_primaries_get_integer_code_point_for_string(color_primaries_string: CFString) -> i32 {
    unsafe { CVColorPrimariesGetIntegerCodePointForString(color_primaries_string.as_concrete_TypeRef()) }
}

pub fn transfer_function_get_integer_code_point_for_string(transfer_function_string: CFString) -> i32 {
    unsafe { CVTransferFunctionGetIntegerCodePointForString(transfer_function_string.as_concrete_TypeRef()) }
}

pub fn ycbcr_matrix_get_string_for_integer_code_point(ycbcr_matrix_code_point: i32) -> CFString {
    unsafe { CFString::wrap_under_get_rule(CVYCbCrMatrixGetStringForIntegerCodePoint(ycbcr_matrix_code_point)) }
}

pub fn color_primaries_get_string_for_integer_code_point(color_primaries_code_point: i32) -> CFString {
    unsafe { CFString::wrap_under_get_rule(CVColorPrimariesGetStringForIntegerCodePoint(color_primaries_code_point)) }
}

pub fn transfer_function_get_string_for_integer_code_point(transfer_function_code_point: i32) -> CFString {
    unsafe { CFString::wrap_under_get_rule(CVTransferFunctionGetStringForIntegerCodePoint(transfer_function_code_point)) }
}

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 {
    #[inline]
    pub fn downcast<T: TCVImageBuffer>(&self) -> Option<T> {
        if self.instance_of::<T>() {
            unsafe { Some(T::wrap_under_get_rule(T::Ref::from_void_ptr(self.as_concrete_TypeRef() as *const c_void))) }
        } else {
            None
        }
    }

    #[inline]
    pub fn downcast_into<T: TCVImageBuffer>(self) -> Option<T> {
        if self.instance_of::<T>() {
            unsafe {
                let reference = T::Ref::from_void_ptr(self.as_concrete_TypeRef() as *const c_void);
                mem::forget(self);
                Some(T::wrap_under_create_rule(reference))
            }
        } else {
            None
        }
    }
}

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

    #[inline]
    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) }
    }

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

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

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

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

    #[inline]
    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(TCFType::wrap_under_create_rule(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(TCFType::wrap_under_create_rule(color_space))
        }
    }
}