~ubuntu-branches/ubuntu/wily/monodevelop/wily

« back to all changes in this revision

Viewing changes to external/maccore/src/ImageIO/CGImageSource.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (19.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20120527180820-fydl21qnbnfr8w2t
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Authors:
 
3
//   Duane Wandless
 
4
//   Miguel de Icaza
 
5
//
 
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:
 
13
// 
 
14
// The above copyright notice and this permission notice shall be
 
15
// included in all copies or substantial portions of the Software.
 
16
// 
 
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.
 
24
//
 
25
 
 
26
using System;
 
27
using System.Drawing;
 
28
using System.Runtime.InteropServices;
 
29
 
 
30
using MonoMac.ObjCRuntime;
 
31
using MonoMac.Foundation;
 
32
using MonoMac.CoreFoundation;
 
33
using MonoMac.CoreGraphics;
 
34
 
 
35
namespace MonoMac.ImageIO {
 
36
        public enum CGImageSourceStatus {
 
37
                Complete      = 0,
 
38
                Incomplete    = -1,
 
39
                ReadingHeader = -2,
 
40
                UnknownType   = -3,
 
41
                InvalidData   = -4,
 
42
                UnexpectedEOF = -5,
 
43
        }
 
44
        
 
45
        public class CGImageOptions {
 
46
                static IntPtr kTypeIdentifierHint;
 
47
                static IntPtr kShouldCache;
 
48
                static IntPtr kShouldAllowFloat;
 
49
                
 
50
                static void Init ()
 
51
                {
 
52
                        if (kTypeIdentifierHint != IntPtr.Zero)
 
53
                                return;
 
54
                        
 
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");
 
59
                        Dlfcn.dlclose (lib);
 
60
                }
 
61
 
 
62
                public CGImageOptions ()
 
63
                {
 
64
                        ShouldCache = true;
 
65
                }
 
66
                
 
67
                public string BestGuessTypeIdentifier { get; set; }
 
68
                public bool ShouldCache { get; set; }
 
69
                public bool ShouldAllowFloat { get; set; }
 
70
                
 
71
                internal virtual NSMutableDictionary ToDictionary ()
 
72
                {
 
73
                        Init ();
 
74
                        
 
75
                        var dict = new NSMutableDictionary ();
 
76
                        
 
77
                        if (BestGuessTypeIdentifier != null)
 
78
                                dict.LowlevelSetObject (new NSString (BestGuessTypeIdentifier), kTypeIdentifierHint);
 
79
                        if (!ShouldCache)
 
80
                                dict.LowlevelSetObject (CFBoolean.False.Handle, kShouldCache);
 
81
                        if (ShouldAllowFloat)
 
82
                                dict.LowlevelSetObject (CFBoolean.True.Handle, kShouldAllowFloat);
 
83
 
 
84
                        return dict;
 
85
                }
 
86
        }
 
87
 
 
88
        public class CGImageThumbnailOptions : CGImageOptions {
 
89
                static IntPtr kCreateThumbnailFromImageIfAbsent;
 
90
                static IntPtr kCreateThumbnailFromImageAlways;
 
91
                static IntPtr kThumbnailMaxPixelSize;
 
92
                static IntPtr kCreateThumbnailWithTransform;
 
93
 
 
94
                static void Init ()
 
95
                {
 
96
                        if (kCreateThumbnailWithTransform != IntPtr.Zero)
 
97
                                return;
 
98
                        
 
99
                        IntPtr lib = Dlfcn.dlopen (Constants.ImageIOLibrary, 0);
 
100
 
 
101
                        kCreateThumbnailFromImageIfAbsent = Dlfcn.GetIntPtr (lib, "kCGImageSourceCreateThumbnailFromImageIfAbsent");
 
102
                        kCreateThumbnailFromImageAlways = Dlfcn.GetIntPtr (lib, "kCGImageSourceCreateThumbnailFromImageAlways");
 
103
                        kThumbnailMaxPixelSize = Dlfcn.GetIntPtr (lib, "kCGImageSourceThumbnailMaxPixelSize");
 
104
                        kCreateThumbnailWithTransform = Dlfcn.GetIntPtr (lib, "kCGImageSourceCreateThumbnailWithTransform");
 
105
 
 
106
                        Dlfcn.dlclose (lib);
 
107
                }
 
108
 
 
109
                public bool CreateThumbnailFromImageIfAbsent { get; set; }
 
110
                public bool CreateThumbnailFromImageAlways { get; set; }
 
111
                public int? MaxPixelSize { get; set; }
 
112
                public bool CreateThumbnailWithTransform { get; set; }
 
113
                
 
114
                internal override NSMutableDictionary ToDictionary ()
 
115
                {
 
116
                        Init ();
 
117
                        
 
118
                        var dict = base.ToDictionary ();
 
119
                        IntPtr thandle = CFBoolean.True.Handle;
 
120
 
 
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);
 
129
                        
 
130
                        return dict;
 
131
                }
 
132
        }
 
133
        
 
134
        public class CGImageSource : INativeObject, IDisposable
 
135
        {
 
136
                [DllImport (Constants.ImageIOLibrary, EntryPoint="CGImageSourceGetTypeID")]
 
137
                public extern static int GetTypeID ();
 
138
 
 
139
                [DllImport (Constants.ImageIOLibrary)]
 
140
                extern static IntPtr CGImageSourceCopyTypeIdentifiers ();
 
141
 
 
142
                public static string [] TypeIdentifiers {
 
143
                        get {
 
144
                                return NSArray.StringArrayFromHandle (CGImageSourceCopyTypeIdentifiers ());
 
145
                        }
 
146
                }
 
147
                
 
148
                internal IntPtr handle;
 
149
 
 
150
                // invoked by marshallers
 
151
                internal CGImageSource (IntPtr handle) : this (handle, false)
 
152
                {
 
153
                        this.handle = handle;
 
154
                }
 
155
 
 
156
                [Preserve (Conditional=true)]
 
157
                internal CGImageSource (IntPtr handle, bool owns)
 
158
                {
 
159
                        this.handle = handle;
 
160
                        if (!owns)
 
161
                                CFObject.CFRetain (handle);
 
162
                }
 
163
 
 
164
                ~CGImageSource ()
 
165
                {
 
166
                        Dispose (false);
 
167
                }
 
168
                
 
169
                public void Dispose ()
 
170
                {
 
171
                        Dispose (true);
 
172
                        GC.SuppressFinalize (this);
 
173
                }
 
174
 
 
175
                public IntPtr Handle {
 
176
                        get { return handle; }
 
177
                }
 
178
                
 
179
                protected virtual void Dispose (bool disposing)
 
180
                {
 
181
                        if (handle != IntPtr.Zero){
 
182
                                CFObject.CFRelease (handle);
 
183
                                handle = IntPtr.Zero;
 
184
                        }
 
185
                }
 
186
                                
 
187
                [DllImport (Constants.ImageIOLibrary)]
 
188
                extern static IntPtr CGImageSourceCreateWithURL(IntPtr url, IntPtr options);
 
189
 
 
190
                public static CGImageSource FromUrl (NSUrl url)
 
191
                {
 
192
                        return FromUrl (url, null);
 
193
                }
 
194
                
 
195
                public static CGImageSource FromUrl (NSUrl url, CGImageOptions options)
 
196
                {
 
197
                        if (url == null)
 
198
                                throw new ArgumentNullException ("url");
 
199
 
 
200
                        using (var dict = options == null ? null : options.ToDictionary ())
 
201
                                return new CGImageSource (CGImageSourceCreateWithURL (url.Handle, dict == null ? IntPtr.Zero : dict.Handle), true);
 
202
                }
 
203
 
 
204
                [DllImport (Constants.ImageIOLibrary)]
 
205
                extern static IntPtr CGImageSourceCreateWithDataProvider (IntPtr provider, IntPtr options);
 
206
                public static CGImageSource FromDataProvider (CGDataProvider provider)
 
207
                {
 
208
                        return FromDataProvider (provider, null);
 
209
                }
 
210
                
 
211
                public static CGImageSource FromDataProvider (CGDataProvider provider, CGImageOptions options)
 
212
                {
 
213
                        if (provider == null)
 
214
                                throw new ArgumentNullException ("provider");
 
215
 
 
216
                        using (var dict = options == null ? null : options.ToDictionary ())
 
217
                                return new CGImageSource (CGImageSourceCreateWithDataProvider (provider.Handle, dict == null ? IntPtr.Zero : dict.Handle), true);
 
218
                }
 
219
 
 
220
                [DllImport (Constants.ImageIOLibrary)]
 
221
                extern static IntPtr CGImageSourceCreateWithData (IntPtr data, IntPtr options);
 
222
                public static CGImageSource FromData (NSData data)
 
223
                {
 
224
                        return FromData (data, null);
 
225
                }
 
226
                
 
227
                public static CGImageSource FromData (NSData data, CGImageOptions options)
 
228
                {
 
229
                        if (data == null)
 
230
                                throw new ArgumentNullException ("data");
 
231
 
 
232
                        using (var dict = options == null ? null : options.ToDictionary ())
 
233
                                return new CGImageSource (CGImageSourceCreateWithData (data.Handle, dict == null ? IntPtr.Zero : dict.Handle), true);
 
234
                }
 
235
 
 
236
                [DllImport (Constants.ImageIOLibrary)]
 
237
                extern static IntPtr CGImageSourceGetType (IntPtr handle);
 
238
                
 
239
                public string TypeIdentifier {
 
240
                        get {
 
241
                                return NSString.FromHandle (CGImageSourceGetType (handle));
 
242
                        }
 
243
                }
 
244
 
 
245
                [DllImport (Constants.ImageIOLibrary)]
 
246
                extern static int CGImageSourceGetCount (IntPtr handle);
 
247
                
 
248
                public int ImageCount {
 
249
                        get {
 
250
                                return CGImageSourceGetCount (handle);
 
251
                        }
 
252
                }
 
253
 
 
254
                [DllImport (Constants.ImageIOLibrary)]
 
255
                extern static IntPtr CGImageSourceCopyProperties (IntPtr handle, IntPtr dictOptions);
 
256
                public NSDictionary CopyProperties (NSDictionary dict)
 
257
                {
 
258
                        return new NSDictionary (CGImageSourceCopyProperties (handle, dict == null ? IntPtr.Zero : dict.Handle));
 
259
                }
 
260
 
 
261
                [DllImport (Constants.ImageIOLibrary)]
 
262
                extern static IntPtr CGImageSourceCopyPropertiesAtIndex (IntPtr handle, int idx, IntPtr dictOptions);
 
263
                public NSDictionary CopyProperties (NSDictionary dict, int imageIndex)
 
264
                {
 
265
                        return new NSDictionary (CGImageSourceCopyPropertiesAtIndex (handle, imageIndex, dict == null ? IntPtr.Zero : dict.Handle));
 
266
                }
 
267
                
 
268
                //
 
269
                // TODO: we could introduce a strongly typed CopyProperties to more easily examine properties
 
270
                // instead of a Dictionary like Obj-C does
 
271
                //
 
272
                
 
273
                
 
274
                [DllImport (Constants.ImageIOLibrary)]
 
275
                extern static IntPtr CGImageSourceCreateImageAtIndex(IntPtr isrc, int index, IntPtr options);
 
276
                public CGImage CreateImage (int index, CGImageOptions options)
 
277
                {
 
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);
 
281
                        }
 
282
                }
 
283
 
 
284
                [DllImport (Constants.ImageIOLibrary)]
 
285
                extern static IntPtr CGImageSourceCreateThumbnailAtIndex (IntPtr isrc, int index, IntPtr options);
 
286
                public CGImage CreateThumbnail (int index, CGImageThumbnailOptions options)
 
287
                {
 
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);
 
291
                        }
 
292
                }
 
293
 
 
294
                [DllImport (Constants.ImageIOLibrary)]
 
295
                extern static IntPtr CGImageSourceCreateIncremental (IntPtr options);
 
296
                public static CGImageSource CreateIncremental (CGImageOptions options)
 
297
                {
 
298
                        using (var dict = options == null ? null : options.ToDictionary ())
 
299
                                return new CGImageSource (CGImageSourceCreateIncremental (dict == null ? IntPtr.Zero : dict.Handle), true);
 
300
                }
 
301
 
 
302
                [DllImport (Constants.ImageIOLibrary)]
 
303
                extern static void CGImageSourceUpdateData (IntPtr handle, IntPtr data, bool final);
 
304
                
 
305
                public void UpdateData (NSData data, bool final)
 
306
                {
 
307
                        if (data == null)
 
308
                                throw new ArgumentNullException ("data");
 
309
                        CGImageSourceUpdateData (handle, data.Handle, final);
 
310
                }
 
311
 
 
312
                [DllImport (Constants.ImageIOLibrary)]
 
313
                extern static void CGImageSourceUpdateDataProvider (IntPtr handle, IntPtr dataProvider);
 
314
                
 
315
                public void UpdateDataProvider (CGDataProvider provider)
 
316
                {
 
317
                        if (provider == null)
 
318
                                throw new ArgumentNullException ("provider");
 
319
                        CGImageSourceUpdateDataProvider (handle, provider.Handle);
 
320
                }
 
321
 
 
322
                [DllImport (Constants.ImageIOLibrary)]
 
323
                extern static CGImageSourceStatus CGImageSourceGetStatus (IntPtr handle);
 
324
                
 
325
                public CGImageSourceStatus GetStatus ()
 
326
                {
 
327
                        return CGImageSourceGetStatus (handle);
 
328
                }
 
329
 
 
330
                [DllImport (Constants.ImageIOLibrary)]
 
331
                extern static CGImageSourceStatus CGImageSourceGetStatusAtIndex (IntPtr handle, int idx);               
 
332
 
 
333
                public CGImageSourceStatus GetStatus (int index)
 
334
                {
 
335
                        return CGImageSourceGetStatusAtIndex (handle, index);
 
336
                }
 
337
        }
 
338
}