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 derelict.cocoa.coregraphics;
8 
9 import derelict.cocoa.runtime;
10 import derelict.cocoa.foundation;
11 import dplug.core.sharedlib;
12 
13 import dplug.core.nogc;
14 
15 
16 version(OSX)
17     enum libNames = "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics";
18 else
19     enum libNames = "";
20 
21 
22 class DerelictCoreGraphicsLoader : SharedLibLoader
23 {
24     public
25     {
26         nothrow @nogc:
27         this()
28         {
29             super(libNames);
30         }
31 
32         override void loadSymbols()
33         {
34             bindFunc(cast(void**)&CGContextDrawImage, "CGContextDrawImage");
35 
36             bindFunc(cast(void**)&CGImageCreate, "CGImageCreate");
37             bindFunc(cast(void**)&CGImageRelease, "CGImageRelease");
38 
39             bindFunc(cast(void**)&CGDataProviderCreateWithData, "CGDataProviderCreateWithData");
40             bindFunc(cast(void**)&CGDataProviderRelease, "CGDataProviderRelease");            
41         }
42     }
43 }
44 
45 private __gshared DerelictCoreGraphicsLoader DerelictCoreGraphics;
46 
47 private __gshared loaderCounterCG = 0;
48 
49 // Call this each time a novel owner uses these functions
50 // TODO: hold a mutex, because this isn't thread-safe
51 void acquireCoreGraphicsFunctions() nothrow @nogc
52 {
53     if (DerelictCoreGraphics is null)  // You only live once
54     {
55         DerelictCoreGraphics = mallocNew!DerelictCoreGraphicsLoader();
56         DerelictCoreGraphics.load();
57     }
58 }
59 
60 // Call this each time a novel owner releases a Cocoa functions
61 // TODO: hold a mutex, because this isn't thread-safe
62 void releaseCoreGraphicsFunctions() nothrow @nogc
63 {
64     /*if (--loaderCounterCG == 0)
65     {
66         DerelictCoreServices.unload();
67         DerelictCoreServices.destroyFree();
68     }*/
69 }
70 
71 unittest
72 {
73     version(OSX)
74     {
75         acquireCoreGraphicsFunctions();
76         releaseCoreGraphicsFunctions();
77     }
78 }
79 
80 
81 alias CGPoint = NSPoint;
82 alias CGSize = NSSize;
83 alias CGRect = NSRect;
84 
85 alias CGMakeRect = NSMakeRect;
86 
87 CGRect NSRectToCGRect(NSRect rect) pure nothrow @nogc
88 {
89     return rect;
90 }
91 
92 NSRect CGRectToNSRect(CGRect rect) pure nothrow @nogc
93 {
94     return rect;
95 }
96 
97 alias CGColorSpaceRef = void*;
98 alias CGContextRef = void*;
99 
100 
101 extern(C) nothrow @nogc
102 {
103     alias da_CGContextDrawImage = void function(CGContextRef c, CGRect rect, CGImageRef image);
104 
105 }
106 __gshared
107 {
108     da_CGContextDrawImage CGContextDrawImage;
109 }
110 
111 alias CGImageRef = void*;
112 
113 extern(C) nothrow @nogc
114 {
115     alias da_CGImageCreate = CGImageRef function(size_t width, 
116                                                  size_t height, 
117                                                  size_t bitsPerComponent, 
118                                                  size_t bitsPerPixel, 
119                                                  size_t bytesPerRow, 
120                                                  CGColorSpaceRef space, 
121                                                  CGBitmapInfo bitmapInfo, 
122                                                  CGDataProviderRef provider, 
123                                                  const(CGFloat)*decode, 
124                                                  bool shouldInterpolate, 
125                                                  CGColorRenderingIntent intent);
126     alias da_CGImageRelease = void function(CGImageRef image);
127 }
128 __gshared
129 {
130     da_CGImageCreate CGImageCreate;
131     da_CGImageRelease CGImageRelease;
132 }
133 
134 alias CGImageByteOrderInfo = uint;
135 
136 enum : CGImageByteOrderInfo
137 {
138     kCGImageByteOrderMask     = 0x7000,
139     kCGImageByteOrderDefault  = (0 << 12),
140     kCGImageByteOrder16Little = (1 << 12),
141     kCGImageByteOrder32Little = (2 << 12),
142     kCGImageByteOrder16Big    = (3 << 12),
143     kCGImageByteOrder32Big    = (4 << 12)
144 }
145 
146 enum : uint
147 {
148     kCGImageAlphaNone               = 0,
149     kCGImageAlphaPremultipliedLast  = 1,
150     kCGImageAlphaPremultipliedFirst = 2,
151     kCGImageAlphaLast               = 3,
152     kCGImageAlphaFirst              = 4,
153     kCGImageAlphaNoneSkipLast       = 5,
154     kCGImageAlphaNoneSkipFirst      = 6,
155 }
156 
157 alias CGBitmapInfo = uint;
158 enum : CGBitmapInfo
159 {
160     kCGBitmapAlphaInfoMask = 0x1F,
161     kCGBitmapFloatInfoMask = 0xF00,
162     kCGBitmapFloatComponents = (1 << 8),
163     kCGBitmapByteOrderMask     = kCGImageByteOrderMask,
164     kCGBitmapByteOrderDefault  = kCGImageByteOrderDefault,
165     kCGBitmapByteOrder16Little = kCGImageByteOrder16Little,
166     kCGBitmapByteOrder32Little = kCGImageByteOrder32Little,
167     kCGBitmapByteOrder16Big    = kCGImageByteOrder16Big,
168     kCGBitmapByteOrder32Big    = kCGImageByteOrder32Big
169 }
170 
171 alias CGColorRenderingIntent = uint;
172 enum : CGColorRenderingIntent
173 {
174     kCGRenderingIntentDefault              = 0,
175     kCGRenderingIntentAbsoluteColorimetric = 1,
176     kCGRenderingIntentRelativeColorimetric = 2,
177     kCGRenderingIntentPerceptual           = 3,
178     kCGRenderingIntentSaturation           = 4,
179 }
180 
181 
182 
183 alias CGDataProviderRef = void*;
184 alias CGDataProviderReleaseDataCallback = void*;
185 
186 extern(C) nothrow @nogc
187 {
188     alias da_CGDataProviderCreateWithData = CGDataProviderRef function(void *info, 
189                                                                        const(void) *data, 
190                                                                        size_t size, 
191                                                                        CGDataProviderReleaseDataCallback releaseData);
192     alias da_CGDataProviderRelease = void function(CGDataProviderRef provider);
193 }
194 __gshared
195 {
196     da_CGDataProviderCreateWithData CGDataProviderCreateWithData;
197     da_CGDataProviderRelease CGDataProviderRelease;
198 }