1 /**
2 Utilities from the CoreGraphics framework.
3 
4 Copyright: Guillaume Piolat 2016.
5 License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6 */
7 module bindbc.cocoa.dynloadcoregraphics;
8 
9 import bindbc.cocoa.runtime;
10 import bindbc.cocoa.foundation;
11 
12 import dplug.core.nogc;
13 
14 import bindbc.loader;
15 
16 private {
17     SharedLib lib;
18     CoreGraphicsSupport loadedVersion;
19 }
20 
21 enum CoreGraphicsSupport {
22     noLibrary,
23     badLibrary,
24     coreGraphicsLibrary
25 }
26 
27 void unloadCoreGraphics()
28 {
29     if(lib != invalidHandle) {
30         lib.unload();
31     }
32 }
33 
34 CoreGraphicsSupport loadedCoreGraphicsVersion() { return loadedVersion; }
35 
36 bool isCoreGraphicsLoaded() { return lib != invalidHandle; }
37 
38 CoreGraphicsSupport loadCoreGraphics(const(char)* libName)
39 {
40     // If the library isn't yet loaded, load it now.
41     if(lib == invalidHandle) {
42         lib = load(libName);
43         if(lib == invalidHandle) {
44             return CoreGraphicsSupport.noLibrary;
45         }
46     }
47 
48     auto errCount = errorCount();
49 
50     lib.bindSymbol(cast(void**)&CGContextDrawImage, "CGContextDrawImage");
51 
52     lib.bindSymbol(cast(void**)&CGImageCreate, "CGImageCreate");
53     lib.bindSymbol(cast(void**)&CGImageRelease, "CGImageRelease");
54 
55     lib.bindSymbol(cast(void**)&CGDataProviderCreateWithData, "CGDataProviderCreateWithData");
56     lib.bindSymbol(cast(void**)&CGDataProviderRelease, "CGDataProviderRelease");
57     
58     if(errorCount() != errCount) loadedVersion = CoreGraphicsSupport.badLibrary;
59     else loadedVersion = CoreGraphicsSupport.coreGraphicsLibrary;
60 
61     return loadedVersion;
62 }
63 
64 alias CGPoint = NSPoint;
65 alias CGSize = NSSize;
66 alias CGRect = NSRect;
67 
68 alias CGMakeRect = NSMakeRect;
69 
70 CGRect NSRectToCGRect(NSRect rect) pure nothrow @nogc
71 {
72     return rect;
73 }
74 
75 NSRect CGRectToNSRect(CGRect rect) pure nothrow @nogc
76 {
77     return rect;
78 }
79 
80 alias CGColorSpaceRef = void*;
81 alias CGContextRef = void*;
82 
83 
84 extern(C) nothrow @nogc
85 {
86     alias da_CGContextDrawImage = void function(CGContextRef c, CGRect rect, CGImageRef image);
87 
88 }
89 __gshared
90 {
91     da_CGContextDrawImage CGContextDrawImage;
92 }
93 
94 alias CGImageRef = void*;
95 
96 extern(C) nothrow @nogc
97 {
98     alias da_CGImageCreate = CGImageRef function(size_t width, 
99                                                  size_t height, 
100                                                  size_t bitsPerComponent, 
101                                                  size_t bitsPerPixel, 
102                                                  size_t bytesPerRow, 
103                                                  CGColorSpaceRef space, 
104                                                  CGBitmapInfo bitmapInfo, 
105                                                  CGDataProviderRef provider, 
106                                                  const(CGFloat)*decode, 
107                                                  bool shouldInterpolate, 
108                                                  CGColorRenderingIntent intent);
109     alias da_CGImageRelease = void function(CGImageRef image);
110 }
111 __gshared
112 {
113     da_CGImageCreate CGImageCreate;
114     da_CGImageRelease CGImageRelease;
115 }
116 
117 alias CGImageByteOrderInfo = uint;
118 
119 enum : CGImageByteOrderInfo
120 {
121     kCGImageByteOrderMask     = 0x7000,
122     kCGImageByteOrderDefault  = (0 << 12),
123     kCGImageByteOrder16Little = (1 << 12),
124     kCGImageByteOrder32Little = (2 << 12),
125     kCGImageByteOrder16Big    = (3 << 12),
126     kCGImageByteOrder32Big    = (4 << 12)
127 }
128 
129 enum : uint
130 {
131     kCGImageAlphaNone               = 0,
132     kCGImageAlphaPremultipliedLast  = 1,
133     kCGImageAlphaPremultipliedFirst = 2,
134     kCGImageAlphaLast               = 3,
135     kCGImageAlphaFirst              = 4,
136     kCGImageAlphaNoneSkipLast       = 5,
137     kCGImageAlphaNoneSkipFirst      = 6,
138 }
139 
140 alias CGBitmapInfo = uint;
141 enum : CGBitmapInfo
142 {
143     kCGBitmapAlphaInfoMask = 0x1F,
144     kCGBitmapFloatInfoMask = 0xF00,
145     kCGBitmapFloatComponents = (1 << 8),
146     kCGBitmapByteOrderMask     = kCGImageByteOrderMask,
147     kCGBitmapByteOrderDefault  = kCGImageByteOrderDefault,
148     kCGBitmapByteOrder16Little = kCGImageByteOrder16Little,
149     kCGBitmapByteOrder32Little = kCGImageByteOrder32Little,
150     kCGBitmapByteOrder16Big    = kCGImageByteOrder16Big,
151     kCGBitmapByteOrder32Big    = kCGImageByteOrder32Big
152 }
153 
154 alias CGColorRenderingIntent = uint;
155 enum : CGColorRenderingIntent
156 {
157     kCGRenderingIntentDefault              = 0,
158     kCGRenderingIntentAbsoluteColorimetric = 1,
159     kCGRenderingIntentRelativeColorimetric = 2,
160     kCGRenderingIntentPerceptual           = 3,
161     kCGRenderingIntentSaturation           = 4,
162 }
163 
164 
165 
166 alias CGDataProviderRef = void*;
167 alias CGDataProviderReleaseDataCallback = void*;
168 
169 extern(C) nothrow @nogc
170 {
171     alias da_CGDataProviderCreateWithData = CGDataProviderRef function(void *info, 
172                                                                        const(void) *data, 
173                                                                        size_t size, 
174                                                                        CGDataProviderReleaseDataCallback releaseData);
175     alias da_CGDataProviderRelease = void function(CGDataProviderRef provider);
176 }
177 __gshared
178 {
179     da_CGDataProviderCreateWithData CGDataProviderCreateWithData;
180     da_CGDataProviderRelease CGDataProviderRelease;
181 }