Debian Codegraph @

third_party/rust/core-graphics/src/image.rs

raw
1 use std::ptr;
2
3 use base::CGFloat;
4 use core_foundation::base::{CFRetain, CFTypeID};
5 use core_foundation::data::CFData;
6 use color_space::CGColorSpace;
7 use data_provider::{CGDataProviderRef, CGDataProvider};
8 use geometry::CGRect;
9 use libc::size_t;
10 use foreign_types::{ForeignType, ForeignTypeRef};
11
12 #[repr(C)]
13 pub enum CGImageAlphaInfo {
14 CGImageAlphaNone, /* For example, RGB. */
15 CGImageAlphaPremultipliedLast, /* For example, premultiplied RGBA */
16 CGImageAlphaPremultipliedFirst, /* For example, premultiplied ARGB */
17 CGImageAlphaLast, /* For example, non-premultiplied RGBA */
18 CGImageAlphaFirst, /* For example, non-premultiplied ARGB */
19 CGImageAlphaNoneSkipLast, /* For example, RBGX. */
20 CGImageAlphaNoneSkipFirst, /* For example, XRBG. */
21 CGImageAlphaOnly /* No color data, alpha data only */
22 }
23
24 #[repr(C)]
25 pub enum CGImageByteOrderInfo {
26 CGImageByteOrderMask = 0x7000,
27 CGImageByteOrder16Little = 1 << 12,
28 CGImageByteOrder32Little = 2 << 12,
29 CGImageByteOrder16Big = 3 << 12,
30 CGImageByteOrder32Big = 4 << 12
31 }
32
33 foreign_type! {
34 #[doc(hidden)]
35 pub unsafe type CGImage {
36 type CType = ::sys::CGImage;
37 fn drop = CGImageRelease;
38 fn clone = |p| CFRetain(p as *const _) as *mut _;
39 }
40 }
41
42 impl CGImage {
43 pub fn new(width: size_t,
44 height: size_t,
45 bits_per_component: size_t,
46 bits_per_pixel: size_t,
47 bytes_per_row: size_t,
48 colorspace: &CGColorSpace,
49 bitmap_info: u32,
50 provider: &CGDataProvider,
51 should_interpolate: bool,
52 rendering_intent: u32)
53 -> Self {
54 unsafe {
55 let result = CGImageCreate(width,
56 height,
57 bits_per_component,
58 bits_per_pixel,
59 bytes_per_row,
60 colorspace.as_ptr(),
61 bitmap_info,
62 provider.as_ptr(),
63 ptr::null_mut(),
64 should_interpolate,
65 rendering_intent);
66 assert!(!result.is_null());
67 Self::from_ptr(result)
68 }
69 }
70
71 pub fn type_id() -> CFTypeID {
72 unsafe {
73 CGImageGetTypeID()
74 }
75 }
76 }
77
78 impl CGImageRef {
79 pub fn width(&self) -> size_t {
80 unsafe {
81 CGImageGetWidth(self.as_ptr())
82 }
83 }
84
85 pub fn height(&self) -> size_t {
86 unsafe {
87 CGImageGetHeight(self.as_ptr())
88 }
89 }
90
91 pub fn bits_per_component(&self) -> size_t {
92 unsafe {
93 CGImageGetBitsPerComponent(self.as_ptr())
94 }
95 }
96
97 pub fn bits_per_pixel(&self) -> size_t {
98 unsafe {
99 CGImageGetBitsPerPixel(self.as_ptr())
100 }
101 }
102
103 pub fn bytes_per_row(&self) -> size_t {
104 unsafe {
105 CGImageGetBytesPerRow(self.as_ptr())
106 }
107 }
108
109 pub fn color_space(&self) -> CGColorSpace {
110 unsafe {
111 let cs = CGImageGetColorSpace(self.as_ptr());
112 CFRetain(cs as *mut _);
113 CGColorSpace::from_ptr(cs)
114 }
115 }
116
117 /// Returns the raw image bytes wrapped in `CFData`. Note, the returned `CFData` owns the
118 /// underlying buffer.
119 pub fn data(&self) -> CFData {
120 let data_provider = unsafe {
121 CGDataProviderRef::from_ptr(CGImageGetDataProvider(self.as_ptr()))
122 };
123 data_provider.copy_data()
124 }
125
126 /// Returns a cropped image. If the `rect` specifies a rectangle which lies outside of the
127 /// image bounds, the `None` is returned.
128 pub fn cropped(&self, rect: CGRect) -> Option<CGImage> {
129 let image_ptr = unsafe { CGImageCreateWithImageInRect(self.as_ptr(), rect) };
130 if !image_ptr.is_null() {
131 Some(unsafe { CGImage::from_ptr(image_ptr) })
132 } else {
133 None
134 }
135 }
136 }
137
138 #[link(name = "CoreGraphics", kind = "framework")]
139 extern {
140 fn CGImageGetTypeID() -> CFTypeID;
141 fn CGImageGetWidth(image: ::sys::CGImageRef) -> size_t;
142 fn CGImageGetHeight(image: ::sys::CGImageRef) -> size_t;
143 fn CGImageGetBitsPerComponent(image: ::sys::CGImageRef) -> size_t;
144 fn CGImageGetBitsPerPixel(image: ::sys::CGImageRef) -> size_t;
145 fn CGImageGetBytesPerRow(image: ::sys::CGImageRef) -> size_t;
146 fn CGImageGetColorSpace(image: ::sys::CGImageRef) -> ::sys::CGColorSpaceRef;
147 fn CGImageGetDataProvider(image: ::sys::CGImageRef) -> ::sys::CGDataProviderRef;
148 fn CGImageRelease(image: ::sys::CGImageRef);
149 fn CGImageCreate(width: size_t,
150 height: size_t,
151 bitsPerComponent: size_t,
152 bitsPerPixel: size_t,
153 bytesPerRow: size_t,
154 space: ::sys::CGColorSpaceRef,
155 bitmapInfo: u32,
156 provider: ::sys::CGDataProviderRef,
157 decode: *const CGFloat,
158 shouldInterpolate: bool,
159 intent: u32)
160 -> ::sys::CGImageRef;
161 fn CGImageCreateWithImageInRect(image: ::sys::CGImageRef, rect: CGRect) -> ::sys::CGImageRef;
162
163 //fn CGImageGetAlphaInfo(image: ::sys::CGImageRef) -> CGImageAlphaInfo;
164 //fn CGImageCreateCopyWithColorSpace(image: ::sys::CGImageRef, space: ::sys::CGColorSpaceRef) -> ::sys::CGImageRef
165 }