Use foreign-types to manage CGColorSpace

This commit is contained in:
Kornel
2017-09-14 23:01:39 +01:00
parent 14f286f8e9
commit 045fa8ca48
4 changed files with 24 additions and 65 deletions
+12 -58
View File
@@ -7,81 +7,35 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType};
use std::mem;
use core_foundation::base::{CFRelease, CFRetain, CFTypeID};
use foreign_types::ForeignType;
// This is an enum due to zero-sized types warnings.
// For more details see https://github.com/rust-lang/rust/issues/27303
pub enum __CGColorSpace {}
pub type CGColorSpaceRef = *const __CGColorSpace;
pub struct CGColorSpace {
obj: CGColorSpaceRef,
foreign_type! {
type CType = ::sys::CGColorSpace;
fn drop = |p| CFRelease(p as *mut _);
fn clone = |p| CFRetain(p as *const _) as *mut _;
pub struct CGColorSpace;
pub struct CGColorSpaceRef;
}
impl Drop for CGColorSpace {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}
impl Clone for CGColorSpace {
fn clone(&self) -> CGColorSpace {
unsafe {
TCFType::wrap_under_get_rule(self.as_concrete_TypeRef())
}
}
}
impl TCFType<CGColorSpaceRef> for CGColorSpace {
#[inline]
fn as_concrete_TypeRef(&self) -> CGColorSpaceRef {
self.obj
}
#[inline]
unsafe fn wrap_under_get_rule(reference: CGColorSpaceRef) -> CGColorSpace {
let reference: CGColorSpaceRef = mem::transmute(CFRetain(mem::transmute(reference)));
TCFType::wrap_under_create_rule(reference)
}
#[inline]
fn as_CFTypeRef(&self) -> CFTypeRef {
unsafe {
mem::transmute(self.as_concrete_TypeRef())
}
}
#[inline]
unsafe fn wrap_under_create_rule(obj: CGColorSpaceRef) -> CGColorSpace {
CGColorSpace {
obj: obj,
}
}
#[inline]
fn type_id() -> CFTypeID {
impl CGColorSpace {
pub fn type_id() -> CFTypeID {
unsafe {
CGColorSpaceGetTypeID()
}
}
}
impl CGColorSpace {
pub fn create_device_rgb() -> CGColorSpace {
unsafe {
let result = CGColorSpaceCreateDeviceRGB();
TCFType::wrap_under_create_rule(result)
CGColorSpace::from_ptr(result)
}
}
}
#[link(name = "ApplicationServices", kind = "framework")]
extern {
fn CGColorSpaceCreateDeviceRGB() -> CGColorSpaceRef;
fn CGColorSpaceCreateDeviceRGB() -> ::sys::CGColorSpaceRef;
fn CGColorSpaceGetTypeID() -> CFTypeID;
}
+3 -3
View File
@@ -8,7 +8,7 @@
// except according to those terms.
use base::CGFloat;
use color_space::{CGColorSpace, CGColorSpaceRef};
use color_space::CGColorSpace;
use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, TCFType};
use libc::{c_void, c_int, size_t};
use std::mem;
@@ -104,7 +104,7 @@ impl CGContext {
height,
bits_per_component,
bytes_per_row,
space.as_concrete_TypeRef(),
space.as_ptr(),
bitmap_info);
assert!(!result.is_null());
TCFType::wrap_under_create_rule(result)
@@ -256,7 +256,7 @@ extern {
height: size_t,
bitsPerComponent: size_t,
bytesPerRow: size_t,
space: CGColorSpaceRef,
space: ::sys::CGColorSpaceRef,
bitmapInfo: u32)
-> CGContextRef;
fn CGBitmapContextGetData(context: CGContextRef) -> *mut c_void;
+6 -4
View File
@@ -1,6 +1,6 @@
use core_foundation::base::{CFRetain, CFTypeID, TCFType};
use core_foundation::data::CFData;
use color_space::{CGColorSpace, CGColorSpaceRef};
use color_space::CGColorSpace;
use data_provider::{CGDataProvider, CGDataProviderRef};
use libc::size_t;
use foreign_types::ForeignType;
@@ -75,7 +75,9 @@ impl CGImageRef {
pub fn color_space(&self) -> CGColorSpace {
unsafe {
TCFType::wrap_under_get_rule(CGImageGetColorSpace(self.as_ptr()))
let cs = CGImageGetColorSpace(self.as_ptr());
CFRetain(cs as *mut _);
CGColorSpace::from_ptr(cs)
}
}
@@ -97,10 +99,10 @@ extern {
fn CGImageGetBitsPerComponent(image: ::sys::CGImageRef) -> size_t;
fn CGImageGetBitsPerPixel(image: ::sys::CGImageRef) -> size_t;
fn CGImageGetBytesPerRow(image: ::sys::CGImageRef) -> size_t;
fn CGImageGetColorSpace(image: ::sys::CGImageRef) -> CGColorSpaceRef;
fn CGImageGetColorSpace(image: ::sys::CGImageRef) -> ::sys::CGColorSpaceRef;
fn CGImageGetDataProvider(image: ::sys::CGImageRef) -> CGDataProviderRef;
fn CGImageRelease(image: ::sys::CGImageRef);
//fn CGImageGetAlphaInfo(image: ::sys::CGImageRef) -> CGImageAlphaInfo;
//fn CGImageCreateCopyWithColorSpace(image: ::sys::CGImageRef, space: CGColorSpaceRef) -> ::sys::CGImageRef
//fn CGImageCreateCopyWithColorSpace(image: ::sys::CGImageRef, space: ::sys::CGColorSpaceRef) -> ::sys::CGImageRef
}
+3
View File
@@ -1,3 +1,6 @@
pub enum CGImage {}
pub type CGImageRef = *mut CGImage;
pub enum CGColorSpace {}
pub type CGColorSpaceRef = *mut CGColorSpace;