6
// Permission is hereby granted, free of charge, to any person obtaining
7
// a copy of this software and associated documentation files (the
8
// "Software"), to deal in the Software without restriction, including
9
// without limitation the rights to use, copy, modify, merge, publish,
10
// distribute, sublicense, and/or sell copies of the Software, and to
11
// permit persons to whom the Software is furnished to do so, subject to
12
// the following conditions:
14
// The above copyright notice and this permission notice shall be
15
// included in all copies or substantial portions of the Software.
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
using System.Runtime.InteropServices;
30
using MonoMac.ObjCRuntime;
31
using MonoMac.Foundation;
32
using MonoMac.CoreFoundation;
33
using MonoMac.CoreGraphics;
35
namespace MonoMac.ImageIO {
36
public enum CGImageSourceStatus {
45
public class CGImageOptions {
46
static IntPtr kTypeIdentifierHint;
47
static IntPtr kShouldCache;
48
static IntPtr kShouldAllowFloat;
52
if (kTypeIdentifierHint != IntPtr.Zero)
55
IntPtr lib = Dlfcn.dlopen (Constants.ImageIOLibrary, 0);
56
kTypeIdentifierHint = Dlfcn.GetIntPtr (lib, "kCGImageSourceTypeIdentifierHint");
57
kShouldCache = Dlfcn.GetIntPtr (lib, "kCGImageSourceShouldCache");
58
kShouldAllowFloat = Dlfcn.GetIntPtr (lib, "kCGImageSourceShouldAllowFloat");
62
public CGImageOptions ()
67
public string BestGuessTypeIdentifier { get; set; }
68
public bool ShouldCache { get; set; }
69
public bool ShouldAllowFloat { get; set; }
71
internal virtual NSMutableDictionary ToDictionary ()
75
var dict = new NSMutableDictionary ();
77
if (BestGuessTypeIdentifier != null)
78
dict.LowlevelSetObject (new NSString (BestGuessTypeIdentifier), kTypeIdentifierHint);
80
dict.LowlevelSetObject (CFBoolean.False.Handle, kShouldCache);
82
dict.LowlevelSetObject (CFBoolean.True.Handle, kShouldAllowFloat);
88
public class CGImageThumbnailOptions : CGImageOptions {
89
static IntPtr kCreateThumbnailFromImageIfAbsent;
90
static IntPtr kCreateThumbnailFromImageAlways;
91
static IntPtr kThumbnailMaxPixelSize;
92
static IntPtr kCreateThumbnailWithTransform;
96
if (kCreateThumbnailWithTransform != IntPtr.Zero)
99
IntPtr lib = Dlfcn.dlopen (Constants.ImageIOLibrary, 0);
101
kCreateThumbnailFromImageIfAbsent = Dlfcn.GetIntPtr (lib, "kCGImageSourceCreateThumbnailFromImageIfAbsent");
102
kCreateThumbnailFromImageAlways = Dlfcn.GetIntPtr (lib, "kCGImageSourceCreateThumbnailFromImageAlways");
103
kThumbnailMaxPixelSize = Dlfcn.GetIntPtr (lib, "kCGImageSourceThumbnailMaxPixelSize");
104
kCreateThumbnailWithTransform = Dlfcn.GetIntPtr (lib, "kCGImageSourceCreateThumbnailWithTransform");
109
public bool CreateThumbnailFromImageIfAbsent { get; set; }
110
public bool CreateThumbnailFromImageAlways { get; set; }
111
public int? MaxPixelSize { get; set; }
112
public bool CreateThumbnailWithTransform { get; set; }
114
internal override NSMutableDictionary ToDictionary ()
118
var dict = base.ToDictionary ();
119
IntPtr thandle = CFBoolean.True.Handle;
121
if (CreateThumbnailFromImageIfAbsent)
122
dict.LowlevelSetObject (thandle, kCreateThumbnailFromImageIfAbsent);
123
if (CreateThumbnailFromImageAlways)
124
dict.LowlevelSetObject (thandle, kCreateThumbnailFromImageAlways);
125
if (MaxPixelSize.HasValue)
126
dict.LowlevelSetObject (new NSNumber (MaxPixelSize.Value), kThumbnailMaxPixelSize);
127
if (CreateThumbnailWithTransform)
128
dict.LowlevelSetObject (thandle, kCreateThumbnailWithTransform);
134
public class CGImageSource : INativeObject, IDisposable
136
[DllImport (Constants.ImageIOLibrary, EntryPoint="CGImageSourceGetTypeID")]
137
public extern static int GetTypeID ();
139
[DllImport (Constants.ImageIOLibrary)]
140
extern static IntPtr CGImageSourceCopyTypeIdentifiers ();
142
public static string [] TypeIdentifiers {
144
return NSArray.StringArrayFromHandle (CGImageSourceCopyTypeIdentifiers ());
148
internal IntPtr handle;
150
// invoked by marshallers
151
internal CGImageSource (IntPtr handle) : this (handle, false)
153
this.handle = handle;
156
[Preserve (Conditional=true)]
157
internal CGImageSource (IntPtr handle, bool owns)
159
this.handle = handle;
161
CFObject.CFRetain (handle);
169
public void Dispose ()
172
GC.SuppressFinalize (this);
175
public IntPtr Handle {
176
get { return handle; }
179
protected virtual void Dispose (bool disposing)
181
if (handle != IntPtr.Zero){
182
CFObject.CFRelease (handle);
183
handle = IntPtr.Zero;
187
[DllImport (Constants.ImageIOLibrary)]
188
extern static IntPtr CGImageSourceCreateWithURL(IntPtr url, IntPtr options);
190
public static CGImageSource FromUrl (NSUrl url)
192
return FromUrl (url, null);
195
public static CGImageSource FromUrl (NSUrl url, CGImageOptions options)
198
throw new ArgumentNullException ("url");
200
using (var dict = options == null ? null : options.ToDictionary ())
201
return new CGImageSource (CGImageSourceCreateWithURL (url.Handle, dict == null ? IntPtr.Zero : dict.Handle), true);
204
[DllImport (Constants.ImageIOLibrary)]
205
extern static IntPtr CGImageSourceCreateWithDataProvider (IntPtr provider, IntPtr options);
206
public static CGImageSource FromDataProvider (CGDataProvider provider)
208
return FromDataProvider (provider, null);
211
public static CGImageSource FromDataProvider (CGDataProvider provider, CGImageOptions options)
213
if (provider == null)
214
throw new ArgumentNullException ("provider");
216
using (var dict = options == null ? null : options.ToDictionary ())
217
return new CGImageSource (CGImageSourceCreateWithDataProvider (provider.Handle, dict == null ? IntPtr.Zero : dict.Handle), true);
220
[DllImport (Constants.ImageIOLibrary)]
221
extern static IntPtr CGImageSourceCreateWithData (IntPtr data, IntPtr options);
222
public static CGImageSource FromData (NSData data)
224
return FromData (data, null);
227
public static CGImageSource FromData (NSData data, CGImageOptions options)
230
throw new ArgumentNullException ("data");
232
using (var dict = options == null ? null : options.ToDictionary ())
233
return new CGImageSource (CGImageSourceCreateWithData (data.Handle, dict == null ? IntPtr.Zero : dict.Handle), true);
236
[DllImport (Constants.ImageIOLibrary)]
237
extern static IntPtr CGImageSourceGetType (IntPtr handle);
239
public string TypeIdentifier {
241
return NSString.FromHandle (CGImageSourceGetType (handle));
245
[DllImport (Constants.ImageIOLibrary)]
246
extern static int CGImageSourceGetCount (IntPtr handle);
248
public int ImageCount {
250
return CGImageSourceGetCount (handle);
254
[DllImport (Constants.ImageIOLibrary)]
255
extern static IntPtr CGImageSourceCopyProperties (IntPtr handle, IntPtr dictOptions);
256
public NSDictionary CopyProperties (NSDictionary dict)
258
return new NSDictionary (CGImageSourceCopyProperties (handle, dict == null ? IntPtr.Zero : dict.Handle));
261
[DllImport (Constants.ImageIOLibrary)]
262
extern static IntPtr CGImageSourceCopyPropertiesAtIndex (IntPtr handle, int idx, IntPtr dictOptions);
263
public NSDictionary CopyProperties (NSDictionary dict, int imageIndex)
265
return new NSDictionary (CGImageSourceCopyPropertiesAtIndex (handle, imageIndex, dict == null ? IntPtr.Zero : dict.Handle));
269
// TODO: we could introduce a strongly typed CopyProperties to more easily examine properties
270
// instead of a Dictionary like Obj-C does
274
[DllImport (Constants.ImageIOLibrary)]
275
extern static IntPtr CGImageSourceCreateImageAtIndex(IntPtr isrc, int index, IntPtr options);
276
public CGImage CreateImage (int index, CGImageOptions options)
278
using (var dict = options == null ? null : options.ToDictionary ()) {
279
var ret = CGImageSourceCreateImageAtIndex (handle, index, dict == null ? IntPtr.Zero : dict.Handle);
280
return new CGImage (ret, true);
284
[DllImport (Constants.ImageIOLibrary)]
285
extern static IntPtr CGImageSourceCreateThumbnailAtIndex (IntPtr isrc, int index, IntPtr options);
286
public CGImage CreateThumbnail (int index, CGImageThumbnailOptions options)
288
using (var dict = options == null ? null : options.ToDictionary ()) {
289
var ret = CGImageSourceCreateThumbnailAtIndex (handle, index, dict == null ? IntPtr.Zero : dict.Handle);
290
return new CGImage (ret, true);
294
[DllImport (Constants.ImageIOLibrary)]
295
extern static IntPtr CGImageSourceCreateIncremental (IntPtr options);
296
public static CGImageSource CreateIncremental (CGImageOptions options)
298
using (var dict = options == null ? null : options.ToDictionary ())
299
return new CGImageSource (CGImageSourceCreateIncremental (dict == null ? IntPtr.Zero : dict.Handle), true);
302
[DllImport (Constants.ImageIOLibrary)]
303
extern static void CGImageSourceUpdateData (IntPtr handle, IntPtr data, bool final);
305
public void UpdateData (NSData data, bool final)
308
throw new ArgumentNullException ("data");
309
CGImageSourceUpdateData (handle, data.Handle, final);
312
[DllImport (Constants.ImageIOLibrary)]
313
extern static void CGImageSourceUpdateDataProvider (IntPtr handle, IntPtr dataProvider);
315
public void UpdateDataProvider (CGDataProvider provider)
317
if (provider == null)
318
throw new ArgumentNullException ("provider");
319
CGImageSourceUpdateDataProvider (handle, provider.Handle);
322
[DllImport (Constants.ImageIOLibrary)]
323
extern static CGImageSourceStatus CGImageSourceGetStatus (IntPtr handle);
325
public CGImageSourceStatus GetStatus ()
327
return CGImageSourceGetStatus (handle);
330
[DllImport (Constants.ImageIOLibrary)]
331
extern static CGImageSourceStatus CGImageSourceGetStatusAtIndex (IntPtr handle, int idx);
333
public CGImageSourceStatus GetStatus (int index)
335
return CGImageSourceGetStatusAtIndex (handle, index);